Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp664606rwl; Sat, 25 Mar 2023 08:47:21 -0700 (PDT) X-Google-Smtp-Source: AKy350ZKr1XXTN6iYz/KNaBNUUiknp6tqKlWDbeb8mYdNjY6zrRyKEUrtUsdjf/NcTKZFRqU72KS X-Received: by 2002:aa7:8f15:0:b0:5a8:31a1:2f05 with SMTP id x21-20020aa78f15000000b005a831a12f05mr6877812pfr.5.1679759241135; Sat, 25 Mar 2023 08:47:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679759241; cv=none; d=google.com; s=arc-20160816; b=FqUYNKzE3/zPQz//ukSq2vcOjBhqPBToj/tCXwcF6e7IujlOk5fGNWlLqL87xQ4w3e qHUV6GcX54zgPGg6kOb9+1Nwh5IkGs+Oo5hjpv+bibcAHtfsoKIBqHxo8nImjlMav6q1 6pZAymNuBuqSdrT1On1Ukb0Webfc3Y8CDXca1plkMSVykYQpDlJkcqBKLrPhbr3/zkNG jK0//lnh6d65hhwlIYpYef0otzZ2QGwIbW1fYRhCQky8G/Jcw20AlGHLDkkYqY0Yx4Om raRjA7BB0q85kzZe7or1KQYR+Fr5mMKlkOiHdI3Cb7H0IkrV4gKJa7NnFMGc4Pzdf9ym Doxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Gj6OLTt/3//PnNoIAeAXXOH2PEyxFtErO/H0iDUUuPo=; b=J2Ir+dkvgauh8cIv/ar/Lr9rLQXQlkiRfbZg7kqZ4lHg/daP5G689NZNZ0b88FEOpy lvMx+u7DZD6Ry+WSFxbFNFp65xcChspCOT6jbAIvirJUJLecuzDnF3xbBzng1exDqXnL +7XvOk6Oh+73fRFNH5bKEhk+1kaAbPiRbNvgTAym5ojTDz7tUewld55Ag3HL4uDtN14i ai5IM/55P8XRmm6uzlL/or5lR87PQZKj6m7/w45yExU77GAfurQ1qo2kwOG/PRWWy60j b0IO6ru2/1Aa64W+syru+TzXCcrFoCWeA1+HiELulp6N68yHQykHiyT7ekDvnl9Azp7P DgjA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l10-20020a056a0016ca00b00625c2bd736asi24853363pfc.20.2023.03.25.08.47.09; Sat, 25 Mar 2023 08:47:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231561AbjCYPp4 (ORCPT + 99 others); Sat, 25 Mar 2023 11:45:56 -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: 8bit X-Spam-Status: No, score=-0.0 required=5.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Weißschuh Test the previously introduce stack protector functionality in nolibc. Signed-off-by: Thomas Weißschuh 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/selftests/nolibc/Makefile index 4469dcb0c9d7..e516e53775d4 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.17.5