From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dormouse.ash.relay.mailchannels.net (dormouse.ash.relay.mailchannels.net [23.83.222.50]) by sourceware.org (Postfix) with ESMTPS id 506E2385802E for ; Wed, 20 Oct 2021 05:24:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 506E2385802E X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 9C9B322227; Wed, 20 Oct 2021 05:24:55 +0000 (UTC) Received: from pdx1-sub0-mail-a82.g.dreamhost.com (100-96-11-21.trex.outbound.svc.cluster.local [100.96.11.21]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 4027522280; Wed, 20 Oct 2021 05:24:55 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a82.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.11.21 (trex/6.4.3); Wed, 20 Oct 2021 05:24:55 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Arithmetic-Bored: 73d9aa870aa60c60_1634707495501_1854550916 X-MC-Loop-Signature: 1634707495501:1597715348 X-MC-Ingress-Time: 1634707495501 Received: from pdx1-sub0-mail-a82.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a82.g.dreamhost.com (Postfix) with ESMTP id F104989D50; Tue, 19 Oct 2021 22:24:54 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.123.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a82.g.dreamhost.com (Postfix) with ESMTPSA id 2A12D89D5F; Tue, 19 Oct 2021 22:24:52 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a82 From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Subject: [PATCH v2 2/2] debug: Add tests for _FORTIFY_SOURCE=3 Date: Wed, 20 Oct 2021 10:54:34 +0530 Message-Id: <20211020052434.1221167-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211020052434.1221167-1-siddhesh@sourceware.org> References: <20211012161629.302696-1-siddhesh@sourceware.org> <20211020052434.1221167-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3493.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_NUMSUBJECT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Oct 2021 05:25:02 -0000 Add some testing coverage for _FORTIFY_SOURCE=3D3. Signed-off-by: Siddhesh Poyarekar --- debug/Makefile | 13 ++++-- debug/tst-chk1.c | 102 +++++++++++++++++++++++++--------------------- debug/tst-chk7.c | 2 + debug/tst-chk8.cc | 2 + 4 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 debug/tst-chk7.c create mode 100644 debug/tst-chk8.cc diff --git a/debug/Makefile b/debug/Makefile index 6893111cbf..357f888246 100644 --- a/debug/Makefile +++ b/debug/Makefile @@ -120,6 +120,8 @@ CFLAGS-tst-chk3.c +=3D -Wno-format -Wno-deprecated-de= clarations -Wno-error CFLAGS-tst-chk4.cc +=3D -Wno-format -Wno-deprecated-declarations -Wno-er= ror CFLAGS-tst-chk5.cc +=3D -Wno-format -Wno-deprecated-declarations -Wno-er= ror CFLAGS-tst-chk6.cc +=3D -Wno-format -Wno-deprecated-declarations -Wno-er= ror +CFLAGS-tst-chk7.c +=3D -Wno-format -Wno-deprecated-declarations -Wno-err= or +CFLAGS-tst-chk8.cc +=3D -Wno-format -Wno-deprecated-declarations -Wno-er= ror CFLAGS-tst-lfschk1.c +=3D -Wno-format -Wno-deprecated-declarations -Wno-= error CFLAGS-tst-lfschk2.c +=3D -Wno-format -Wno-deprecated-declarations -Wno-= error CFLAGS-tst-lfschk3.c +=3D -Wno-format -Wno-deprecated-declarations -Wno-= error @@ -129,6 +131,7 @@ CFLAGS-tst-lfschk6.cc +=3D -Wno-format -Wno-deprecate= d-declarations -Wno-error LDLIBS-tst-chk4 =3D -lstdc++ LDLIBS-tst-chk5 =3D -lstdc++ LDLIBS-tst-chk6 =3D -lstdc++ +LDLIBS-tst-chk8 =3D -lstdc++ LDLIBS-tst-lfschk4 =3D -lstdc++ LDLIBS-tst-lfschk5 =3D -lstdc++ LDLIBS-tst-lfschk6 =3D -lstdc++ @@ -150,16 +153,16 @@ CFLAGS-tst-ssp-1.c +=3D -fstack-protector-all =20 tests =3D backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \ tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \ - tst-chk4 tst-chk5 tst-chk6 tst-lfschk4 tst-lfschk5 tst-lfschk6 \ - tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 tst-backtrace4 \ - tst-backtrace5 tst-backtrace6 + tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \ + tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \ + tst-backtrace4 tst-backtrace5 tst-backtrace6 =20 ifeq ($(have-ssp),yes) tests +=3D tst-ssp-1 endif =20 ifeq (,$(CXX)) -tests-unsupported =3D tst-chk4 tst-chk5 tst-chk6 \ +tests-unsupported =3D tst-chk4 tst-chk5 tst-chk6 tst-chk8 \ tst-lfschk4 tst-lfschk5 tst-lfschk6 endif =20 @@ -193,6 +196,8 @@ $(objpfx)tst-chk3.out: $(gen-locales) $(objpfx)tst-chk4.out: $(gen-locales) $(objpfx)tst-chk5.out: $(gen-locales) $(objpfx)tst-chk6.out: $(gen-locales) +$(objpfx)tst-chk7.out: $(gen-locales) +$(objpfx)tst-chk8.out: $(gen-locales) $(objpfx)tst-lfschk1.out: $(gen-locales) $(objpfx)tst-lfschk2.out: $(gen-locales) $(objpfx)tst-lfschk3.out: $(gen-locales) diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c index 6712770201..d11e536f87 100644 --- a/debug/tst-chk1.c +++ b/debug/tst-chk1.c @@ -82,8 +82,14 @@ handler (int sig) _exit (127); } =20 +#if __USE_FORTIFY_LEVEL =3D=3D 3 +volatile size_t buf_size =3D 10; +#else char buf[10]; wchar_t wbuf[10]; +#define buf_size sizeof (buf) +#endif + volatile size_t l0; volatile char *p; volatile wchar_t *wp; @@ -122,6 +128,10 @@ int num2 =3D 987654; static int do_test (void) { +#if __USE_FORTIFY_LEVEL =3D=3D 3 + char *buf =3D (char *) malloc (buf_size); + wchar_t *wbuf =3D (wchar_t *) malloc (buf_size * sizeof (wchar_t)); +#endif set_fortify_handler (handler); =20 struct A { char buf1[9]; char buf2[1]; } a; @@ -946,93 +956,93 @@ do_test (void) =20 rewind (stdin); =20 - if (fgets (buf, sizeof (buf), stdin) !=3D buf + if (fgets (buf, buf_size, stdin) !=3D buf || memcmp (buf, "abcdefgh\n", 10)) FAIL (); - if (fgets (buf, sizeof (buf), stdin) !=3D buf || memcmp (buf, "ABCDEFG= HI", 10)) + if (fgets (buf, buf_size, stdin) !=3D buf || memcmp (buf, "ABCDEFGHI",= 10)) FAIL (); =20 rewind (stdin); =20 - if (fgets (buf, l0 + sizeof (buf), stdin) !=3D buf + if (fgets (buf, l0 + buf_size, stdin) !=3D buf || memcmp (buf, "abcdefgh\n", 10)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (fgets (buf, sizeof (buf) + 1, stdin) !=3D buf) + if (fgets (buf, buf_size + 1, stdin) !=3D buf) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (fgets (buf, l0 + sizeof (buf) + 1, stdin) !=3D buf) + if (fgets (buf, l0 + buf_size + 1, stdin) !=3D buf) FAIL (); CHK_FAIL_END #endif =20 rewind (stdin); =20 - if (fgets_unlocked (buf, sizeof (buf), stdin) !=3D buf + if (fgets_unlocked (buf, buf_size, stdin) !=3D buf || memcmp (buf, "abcdefgh\n", 10)) FAIL (); - if (fgets_unlocked (buf, sizeof (buf), stdin) !=3D buf + if (fgets_unlocked (buf, buf_size, stdin) !=3D buf || memcmp (buf, "ABCDEFGHI", 10)) FAIL (); =20 rewind (stdin); =20 - if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) !=3D buf + if (fgets_unlocked (buf, l0 + buf_size, stdin) !=3D buf || memcmp (buf, "abcdefgh\n", 10)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) !=3D buf) + if (fgets_unlocked (buf, buf_size + 1, stdin) !=3D buf) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) !=3D buf) + if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) !=3D buf) FAIL (); CHK_FAIL_END #endif =20 rewind (stdin); =20 - if (fread (buf, 1, sizeof (buf), stdin) !=3D sizeof (buf) + if (fread (buf, 1, buf_size, stdin) !=3D buf_size || memcmp (buf, "abcdefgh\nA", 10)) FAIL (); - if (fread (buf, sizeof (buf), 1, stdin) !=3D 1 + if (fread (buf, buf_size, 1, stdin) !=3D 1 || memcmp (buf, "BCDEFGHI\na", 10)) FAIL (); =20 rewind (stdin); =20 - if (fread (buf, l0 + 1, sizeof (buf), stdin) !=3D sizeof (buf) + if (fread (buf, l0 + 1, buf_size, stdin) !=3D buf_size || memcmp (buf, "abcdefgh\nA", 10)) FAIL (); - if (fread (buf, sizeof (buf), l0 + 1, stdin) !=3D 1 + if (fread (buf, buf_size, l0 + 1, stdin) !=3D 1 || memcmp (buf, "BCDEFGHI\na", 10)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (fread (buf, 1, sizeof (buf) + 1, stdin) !=3D sizeof (buf) + 1) + if (fread (buf, 1, buf_size + 1, stdin) !=3D buf_size + 1) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) !=3D 1) + if (fread (buf, buf_size + 1, l0 + 1, stdin) !=3D 1) FAIL (); CHK_FAIL_END #endif =20 rewind (stdin); =20 - if (fread_unlocked (buf, 1, sizeof (buf), stdin) !=3D sizeof (buf) + if (fread_unlocked (buf, 1, buf_size, stdin) !=3D buf_size || memcmp (buf, "abcdefgh\nA", 10)) FAIL (); - if (fread_unlocked (buf, sizeof (buf), 1, stdin) !=3D 1 + if (fread_unlocked (buf, buf_size, 1, stdin) !=3D 1 || memcmp (buf, "BCDEFGHI\na", 10)) FAIL (); =20 @@ -1047,100 +1057,100 @@ do_test (void) =20 rewind (stdin); =20 - if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) !=3D sizeof (buf= ) + if (fread_unlocked (buf, l0 + 1, buf_size, stdin) !=3D buf_size || memcmp (buf, "abcdefgh\nA", 10)) FAIL (); - if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) !=3D 1 + if (fread_unlocked (buf, buf_size, l0 + 1, stdin) !=3D 1 || memcmp (buf, "BCDEFGHI\na", 10)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) !=3D sizeof (buf)= + 1) + if (fread_unlocked (buf, 1, buf_size + 1, stdin) !=3D buf_size + 1) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) !=3D 1) + if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) !=3D 1) FAIL (); CHK_FAIL_END #endif =20 lseek (fileno (stdin), 0, SEEK_SET); =20 - if (read (fileno (stdin), buf, sizeof (buf) - 1) !=3D sizeof (buf) - 1 + if (read (fileno (stdin), buf, buf_size - 1) !=3D buf_size - 1 || memcmp (buf, "abcdefgh\n", 9)) FAIL (); - if (read (fileno (stdin), buf, sizeof (buf) - 1) !=3D sizeof (buf) - 1 + if (read (fileno (stdin), buf, buf_size - 1) !=3D buf_size - 1 || memcmp (buf, "ABCDEFGHI", 9)) FAIL (); =20 lseek (fileno (stdin), 0, SEEK_SET); =20 - if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) !=3D sizeof (buf= ) - 1 + if (read (fileno (stdin), buf, l0 + buf_size - 1) !=3D buf_size - 1 || memcmp (buf, "abcdefgh\n", 9)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (read (fileno (stdin), buf, sizeof (buf) + 1) !=3D sizeof (buf) + 1= ) + if (read (fileno (stdin), buf, buf_size + 1) !=3D buf_size + 1) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (read (fileno (stdin), buf, l0 + sizeof (buf) + 1) !=3D sizeof (buf= ) + 1) + if (read (fileno (stdin), buf, l0 + buf_size + 1) !=3D buf_size + 1) FAIL (); CHK_FAIL_END #endif =20 - if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2) - !=3D sizeof (buf) - 1 + if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2) + !=3D buf_size - 1 || memcmp (buf, "\nABCDEFGH", 9)) FAIL (); - if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) !=3D sizeof (buf)= - 1 + if (pread (fileno (stdin), buf, buf_size - 1, 0) !=3D buf_size - 1 || memcmp (buf, "abcdefgh\n", 9)) FAIL (); - if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - = 3) - !=3D sizeof (buf) - 1 + if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3) + !=3D buf_size - 1 || memcmp (buf, "h\nABCDEFG", 9)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf)) - !=3D sizeof (buf) + 1) + if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size) + !=3D buf_size + 1) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (pread (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (buf= )) - !=3D sizeof (buf) + 1) + if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size) + !=3D buf_size + 1) FAIL (); CHK_FAIL_END #endif =20 - if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2) - !=3D sizeof (buf) - 1 + if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2) + !=3D buf_size - 1 || memcmp (buf, "\nABCDEFGH", 9)) FAIL (); - if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) !=3D sizeof (bu= f) - 1 + if (pread64 (fileno (stdin), buf, buf_size - 1, 0) !=3D buf_size - 1 || memcmp (buf, "abcdefgh\n", 9)) FAIL (); - if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) = - 3) - !=3D sizeof (buf) - 1 + if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3) + !=3D buf_size - 1 || memcmp (buf, "h\nABCDEFG", 9)) FAIL (); =20 #if __USE_FORTIFY_LEVEL >=3D 1 CHK_FAIL_START - if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf)) - !=3D sizeof (buf) + 1) + if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size) + !=3D buf_size + 1) FAIL (); CHK_FAIL_END =20 CHK_FAIL_START - if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) + 1, 2 * sizeof (b= uf)) - !=3D sizeof (buf) + 1) + if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size) + !=3D buf_size + 1) FAIL (); CHK_FAIL_END #endif @@ -1178,7 +1188,7 @@ do_test (void) CHK_FAIL2_END =20 CHK_FAIL2_START - snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4); + snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4); CHK_FAIL2_END =20 int sp[2]; diff --git a/debug/tst-chk7.c b/debug/tst-chk7.c new file mode 100644 index 0000000000..2a7b323812 --- /dev/null +++ b/debug/tst-chk7.c @@ -0,0 +1,2 @@ +#define _FORTIFY_SOURCE 3 +#include "tst-chk1.c" diff --git a/debug/tst-chk8.cc b/debug/tst-chk8.cc new file mode 100644 index 0000000000..2a7b323812 --- /dev/null +++ b/debug/tst-chk8.cc @@ -0,0 +1,2 @@ +#define _FORTIFY_SOURCE 3 +#include "tst-chk1.c" --=20 2.31.1