Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80C3FC6FD1D for ; Mon, 20 Mar 2023 15:51:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232271AbjCTPvX (ORCPT ); Mon, 20 Mar 2023 11:51:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233335AbjCTPts (ORCPT ); Mon, 20 Mar 2023 11:49:48 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05ADB2FCEC; Mon, 20 Mar 2023 08:41:24 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1679326882; bh=JyOrsnSClXzOy2qb2h3WcW0lX0xzEzpyA/yocZM6rxk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ATWiycxwuKiBrIWlMK6qq40IkPG4fH++buyOoo6EGOYoFOtSIGAzoZsAqXb+2dGJQ ZEsaZVM8N7TMR8AO7nan8A8YlRSv4SuFmUL/dpDSXCBUDA7W2CYyCUYR8urCh5DKeO //bGbQYqU1BLQYufbCKrxNIL1vihCbzzHJHDHQpo= Date: Mon, 20 Mar 2023 15:41:06 +0000 Subject: [PATCH v2 6/8] tools/nolibc: tests: add test for -fstack-protector MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20230223-nolibc-stackprotector-v2-6-4c938e098d67@weissschuh.net> References: <20230223-nolibc-stackprotector-v2-0-4c938e098d67@weissschuh.net> In-Reply-To: <20230223-nolibc-stackprotector-v2-0-4c938e098d67@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1679326878; l=2836; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=JyOrsnSClXzOy2qb2h3WcW0lX0xzEzpyA/yocZM6rxk=; b=a8YNdJ/4b1JkbNuPWAJxJ2gYEKeTzYJ/KBfT3JFHFpkMiK7scEvSx+YJxAFnH7vcPBtu7lnN1 1oEQfg/daTYBQT1LG3XdxgmNwz9NXGeuhjVPrKHGPlSaeDbU9zJpMMv X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Test the previously introduce stack protector functionality in nolibc. Signed-off-by: Thomas Weißschuh --- tools/testing/selftests/nolibc/Makefile | 3 ++ tools/testing/selftests/nolibc/nolibc-test.c | 62 +++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile index 236c0364f5fb..b4f818547f35 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -76,6 +76,9 @@ else Q=@ endif +CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ + $(call cc-option,-mstack-protector-guard=global) \ + $(call cc-option,-fstack-protector-all) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index fb2d4872fac9..21bacc928bf7 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -667,6 +667,63 @@ int run_stdlib(int min, int max) return ret; } +#if defined(__clang__) +__attribute__((optnone)) +#elif defined(__GNUC__) +__attribute__((optimize("O0"))) +#endif +static int smash_stack(void) +{ + char buf[100]; + + for (size_t i = 0; i < 200; i++) + buf[i] = 'P'; + + return 1; +} + +static int run_protection(int min, int max) +{ + pid_t pid; + int llen = 0, status; + + llen += printf("0 -fstackprotector "); + +#if !defined(NOLIBC_STACKPROTECTOR) + llen += printf("not supported"); + pad_spc(llen, 64, "[SKIPPED]\n"); + return 0; +#endif + + pid = -1; + pid = fork(); + + switch (pid) { + case -1: + llen += printf("fork()"); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + + case 0: + close(STDOUT_FILENO); + close(STDERR_FILENO); + + smash_stack(); + return 1; + + default: + pid = waitpid(pid, &status, 0); + + if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) { + llen += printf("waitpid()"); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + pad_spc(llen, 64, " [OK]\n"); + return 0; + } +} + /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */ int prepare(void) { @@ -719,8 +776,9 @@ int prepare(void) /* This is the definition of known test names, with their functions */ static const struct test test_names[] = { /* add new tests here */ - { .name = "syscall", .func = run_syscall }, - { .name = "stdlib", .func = run_stdlib }, + { .name = "syscall", .func = run_syscall }, + { .name = "stdlib", .func = run_stdlib }, + { .name = "protection", .func = run_protection }, { 0 } }; -- 2.40.0