From nobody Mon Feb 9 10:26:44 2026 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 61539C6FD1F for ; Sat, 25 Mar 2023 15:46:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbjCYPp6 (ORCPT ); Sat, 25 Mar 2023 11:45:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229977AbjCYPpt (ORCPT ); Sat, 25 Mar 2023 11:45:49 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8CFEA12589 for ; Sat, 25 Mar 2023 08:45:47 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 32PFjQOW008051; Sat, 25 Mar 2023 16:45:26 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux@weissschuh.net, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 6/8] tools/nolibc: tests: add test for -fstack-protector Date: Sat, 25 Mar 2023 16:45:14 +0100 Message-Id: <20230325154516.7995-7-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230325154516.7995-1-w@1wt.eu> References: <20230325154516.7995-1-w@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Wei=C3=9Fschuh Test the previously introduce stack protector functionality in nolibc. Signed-off-by: Thomas Wei=C3=9Fschuh Signed-off-by: Willy Tarreau --- 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/selfte= sts/nolibc/Makefile index 4469dcb0c9d7..e516e53775d4 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -76,6 +76,9 @@ else Q=3D@ endif =20 +CFLAGS_STACKPROTECTOR =3D -DNOLIBC_STACKPROTECTOR \ + $(call cc-option,-mstack-protector-guard=3Dglobal) \ + $(call cc-option,-fstack-protector-all) CFLAGS_s390 =3D -m64 CFLAGS ?=3D -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/s= elftests/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; } =20 +#if defined(__clang__) +__attribute__((optnone)) +#elif defined(__GNUC__) +__attribute__((optimize("O0"))) +#endif +static int smash_stack(void) +{ + char buf[100]; + + for (size_t i =3D 0; i < 200; i++) + buf[i] =3D 'P'; + + return 1; +} + +static int run_protection(int min, int max) +{ + pid_t pid; + int llen =3D 0, status; + + llen +=3D printf("0 -fstackprotector "); + +#if !defined(NOLIBC_STACKPROTECTOR) + llen +=3D printf("not supported"); + pad_spc(llen, 64, "[SKIPPED]\n"); + return 0; +#endif + + pid =3D -1; + pid =3D fork(); + + switch (pid) { + case -1: + llen +=3D printf("fork()"); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + + case 0: + close(STDOUT_FILENO); + close(STDERR_FILENO); + + smash_stack(); + return 1; + + default: + pid =3D waitpid(pid, &status, 0); + + if (pid =3D=3D -1 || !WIFSIGNALED(status) || WTERMSIG(status) !=3D SIGAB= RT) { + llen +=3D 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[] =3D { /* add new tests here */ - { .name =3D "syscall", .func =3D run_syscall }, - { .name =3D "stdlib", .func =3D run_stdlib }, + { .name =3D "syscall", .func =3D run_syscall }, + { .name =3D "stdlib", .func =3D run_stdlib }, + { .name =3D "protection", .func =3D run_protection }, { 0 } }; =20 --=20 2.17.5