From patchwork Tue Oct 3 18:13:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 77050 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CCADF3856DD5 for ; Tue, 3 Oct 2023 18:14:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 4BAB1385773A for ; Tue, 3 Oct 2023 18:13:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4BAB1385773A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-27747002244so878881a91.2 for ; Tue, 03 Oct 2023 11:13:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696356831; x=1696961631; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=iTaEQ1VSLBck2JCC3Asss3ZyZh+vjL62IT30wKkwM1A=; b=hV+M/SNUfFYmiQqvZ95NLLIUpM4UiXqSClbRxKPYUk6qlL2NnoQhmY4NRZc4aI/muQ PQ7CwLaFERfxobW2gLstwFPMZwG5NZjCGDgG1MBr3g39rPdFfPe5ImlUv5a7kxHieJi4 hSfR/qBXC2vYOX5M8qw7YPTEcMV8n3UrtPNBRiK+gmrhYKvoxYVOcYtVukGRvJdC0LEV ZSt0vghILLoQkfmhpJAjFRdQHFd/xod35c9zzSP84hX79BuXFLRp+80BIlSsAOZEkCNc gC5PLFyNfqCwajUeJUcTmOczm1h3fAY+rScewmmX6ngOu7oeVthEOOh97rs4GtUlJf8y AT0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696356831; x=1696961631; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iTaEQ1VSLBck2JCC3Asss3ZyZh+vjL62IT30wKkwM1A=; b=uTyGLt3bK7k0ojsMva3ODl/TF+0s0EUM9cJJ5DQ2nz3+1fOLoPOosa0MIrNmoiMrhT I6IjXjXLI7PMklRErW1IvAs2HFNWp5LLFSgiVG2Ns79UqIwNCyPfhku9AlkPvJdp0WSL RbLUBoumNA7Sco+2a0wY32iJ/Ik6Fu8l7UuJu80/9jBXsT4UWkJ2PV4U31eU7B+AVDhd VNCyq+esSKyrQ0uOI7mM4PXZkuuNX3SSg/z8THQEPBUcClDWFzLt5Z0r2dqibSZTV8LP a0v3cO73LQrYYHpaO1Ff0ATwKHzJt0kUdQ73mi5sBM0hTHKuH6wHVA12Rqt49bX/mLoj kdVA== X-Gm-Message-State: AOJu0Yy72huF5sJOLKdTNg1+JJTSDE25dxE6oilA51zTmwBsYCVW1dhx I0inx+IVWyenXkAOQHNWJ6WYmhkqMOVTRUGlUv8c0g== X-Google-Smtp-Source: AGHT+IH/oFTPUJJZbuuilkQs5c3+uVPVN8DMTdMWAK9Ah7sucarb9vUIslP0X5njlgSUcJCqRbkrwg== X-Received: by 2002:a17:90a:ce92:b0:268:2523:652c with SMTP id g18-20020a17090ace9200b002682523652cmr97732pju.31.1696356830694; Tue, 03 Oct 2023 11:13:50 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:feaf:31ef:b40c:b4e5:77c]) by smtp.gmail.com with ESMTPSA id a15-20020a17090abe0f00b00256b67208b1sm8373520pjs.56.2023.10.03.11.13.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 11:13:50 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] debug: Add regression tests for BZ 30932 Date: Tue, 3 Oct 2023 15:13:46 -0300 Message-Id: <20231003181346.3726845-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Checked on x86_64-linux-gnu. --- debug/Makefile | 5 ++ debug/tst-sprintf-fortify-rdonly.c | 82 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 debug/tst-sprintf-fortify-rdonly.c diff --git a/debug/Makefile b/debug/Makefile index 434e52f780..d7e021a1c8 100644 --- a/debug/Makefile +++ b/debug/Makefile @@ -178,6 +178,7 @@ CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables CPPFLAGS-tst-longjmp_chk3.c += $(no-fortify-source),-D_FORTIFY_SOURCE=1 CPPFLAGS-tst-realpath-chk.c += $(no-fortify-source),-D_FORTIFY_SOURCE=2 CPPFLAGS-tst-chk-cancel.c += $(no-fortify-source),-D_FORTIFY_SOURCE=2 +CFLAGS-tst-sprintf-fortify-rdonly.c += $(no-fortify-source),-D_FORTIFY_SOURCE=2 # _FORTIFY_SOURCE tests. # Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and @@ -284,6 +285,7 @@ tests = \ tst-longjmp_chk \ tst-longjmp_chk2 \ tst-realpath-chk \ + tst-sprintf-fortify-rdonly \ tst-sprintf-fortify-unchecked \ # tests @@ -291,6 +293,9 @@ tests-time64 += \ $(tests-all-time64-chk) \ # tests-time64 +tests-container += \ + # tests-container + ifeq ($(have-ssp),yes) tests += tst-ssp-1 endif diff --git a/debug/tst-sprintf-fortify-rdonly.c b/debug/tst-sprintf-fortify-rdonly.c new file mode 100644 index 0000000000..7d5c447598 --- /dev/null +++ b/debug/tst-sprintf-fortify-rdonly.c @@ -0,0 +1,82 @@ +/* Testcase for BZ 30932. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +jmp_buf chk_fail_buf; +bool chk_fail_ok; + +const char *str2 = "F"; +char buf2[10] = "%s"; + +static int +do_test (void) +{ + struct rlimit rl; + int max_fd = 24; + + if (getrlimit (RLIMIT_NOFILE, &rl) == -1) + FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m"); + + max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd); + rl.rlim_cur = max_fd; + + if (setrlimit (RLIMIT_NOFILE, &rl) == 1) + FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m"); + + /* Exhaust the file descriptor limit with temporary files. */ + int nfiles = 0; + for (; nfiles < max_fd; nfiles++) + { + int fd = create_temp_file ("tst-spawn3.", NULL); + if (fd == -1) + { + if (errno != EMFILE) + FAIL_EXIT1 ("create_temp_file: %m"); + break; + } + } + TEST_VERIFY_EXIT (nfiles != 0); + + /* When the format string is writable and contains %n, + with -D_FORTIFY_SOURCE=2 it causes __chk_fail. However, if libc can not + open procfs to check if the input format string in within a writable + memory segment, the fortify version can not perform the check. */ + char buf[128]; + int n1; + int n2; + + strcpy (buf2 + 2, "%n%s%n"); + if (sprintf (buf, buf2, str2, &n1, str2, &n2) != 2 + || n1 != 1 || n2 != 2) + FAIL_EXIT1 ("sprintf failed: %s %d %d", buf, n1, n2); + + return 0; +} + +#include