From patchwork Thu Dec 21 18:59:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1879403 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=vZUNeIeU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Sx09x3BT6z20Gb for ; Fri, 22 Dec 2023 06:00:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 211E83847718 for ; Thu, 21 Dec 2023 19:00:19 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 823BC385B532 for ; Thu, 21 Dec 2023 18:59:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 823BC385B532 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 823BC385B532 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703185193; cv=none; b=IdRsD5rjuyP4kUkr2VasKiLiZ+kXlaw0I0bAzQ0ZU6UAsQCxfpcrhicWIaVx2rRyUtXncGo7JuC/cGhku8c+UHDSCVRffVTXoAWI8N5f01/+r0NefPFjrj4fXiqN9xzi2dbJ74oL1WcvXBfEYi9qFFyPTNPZE57Ql1wYyg+F95o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703185193; c=relaxed/simple; bh=e694cuZ44CNyt5dfALndjqMXp3Fa5Cj+qkRwaNVsNPw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=N5BxeyWo/WIDKhcBNo6DEG5j5QSi91x2kMifwzxReLd4YBvoLtkYAmHu3xonDZPMztOOyIxYkR1Wk+6gpDkEHjusxBIIHvlrH2HlLWgC13K5CX+23kxpgMlPTce1ylOSpPuk5htoWLtmcVH2ySjKQz/fI0/UVlZWQ2fiWbzLq58= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-35fd5a13285so2942555ab.3 for ; Thu, 21 Dec 2023 10:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1703185190; x=1703789990; 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=vv53uQpYbZuGOr4ynPza99NntS1MMyxrhLUOdBkxv+4=; b=vZUNeIeU6p9X1ToHtUItVMu2pY3PuCX5rakRU5UENrVAuhmdlfEKUpXxYlxdag2cRv CZ8xCzwTNnY47Ai8JHFcIX8kjJMc7PZZUU8G/WeJky1f3rY1IyHn6wXtwi0dOsTyNTWY W7aMBdCPLjeM93keq/hE892lqdjo6OegC5EKs0+ZuhA8comf8Eua7QTKrWbnfP66kkbW FAtXttGQ65aeDPGCu8IjfxUZy4DLOvSs4UF346S2A3uT/mi32TPdC9kFy77NxNo9GwUf 4ranvwwgRgTYS5tn8+KigUZpIM7IZ48QHxobxqFsvOH0FQERhLzByp5md9eDEx2GYIMo zU3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703185190; x=1703789990; 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=vv53uQpYbZuGOr4ynPza99NntS1MMyxrhLUOdBkxv+4=; b=e/09rwBpM0wwGQQGcPeqOJfUfzPJYa3Xq4sAu+u8+bS6IhF+kqFUKptAzkg5qGL9Y6 lm9/xy8A2Fa9AeYz+Jk9QF3oIdoD7obiWy6vXZAlm1ohj7vyZuao8f6u2PHqXidmgYx5 Am1qABRRrfLnYmbuN3FMG0B20t9ogaVdFe283n+YLptvR/w3tS1TE8ujFRcFCvBMu3/u rCiixX3BECX1ymDXK+zbgJ1X9b5MNFBt+3uBtKIM97AiBsLpZnhfAL0MrcLZ2PmFnHem dxuZsCrK+ty70quGqQUvx9W9Mzknw0yoLIAayncd0vEoKX8+x87ZWsfrW9+VuPozwVM4 GUig== X-Gm-Message-State: AOJu0YwGfA54jjh6/YIZykdW6EQvDHkSoLW0DlFjEOED0Ge+MOMmUmgy JDcVTDRpHMameaduSEH6s2r9nb8tee1FG3FgjJNup1cvnMc= X-Google-Smtp-Source: AGHT+IHYYTGwmgJb1LUxoMCUpQniuQ8Dl0oZS5tVYHgOxdGqiwpptedgXd66gmzfL73Z6DuQQS8ZFg== X-Received: by 2002:a05:6e02:1b02:b0:35f:ac66:e863 with SMTP id i2-20020a056e021b0200b0035fac66e863mr110439ilv.50.1703185190187; Thu, 21 Dec 2023 10:59:50 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:8192:ecd7:d327:bea0:14dc]) by smtp.gmail.com with ESMTPSA id a9-20020a63e409000000b005cdbebd61d8sm1946165pgi.9.2023.12.21.10.59.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 10:59:49 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH 04/15] debug: Add fortify syslog tests Date: Thu, 21 Dec 2023 15:59:18 -0300 Message-Id: <20231221185929.1307116-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231221185929.1307116-1-adhemerval.zanella@linaro.org> References: <20231221185929.1307116-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@sourceware.org It requires to be in a container tests to avoid logging bogus information on the system. The syslog also requires to be checked in a different process because the internal printf call will abort with the internal syslog lock taken (which makes subsequent syslog calls deadlock). Checked on aarch64, armhf, x86_64, and i686. Reviewed-by: Siddhesh Poyarekar --- debug/Makefile | 5 ++ debug/tst-fortify-syslog.c | 128 +++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 debug/tst-fortify-syslog.c diff --git a/debug/Makefile b/debug/Makefile index c49e5d86ec..fdc250e209 100644 --- a/debug/Makefile +++ b/debug/Makefile @@ -179,6 +179,7 @@ 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 +CFLAGS-tst-fortify-syslog.c += $(no-fortify-source),-D_FORTIFY_SOURCE=2 # _FORTIFY_SOURCE tests. # Auto-generate tests for _FORTIFY_SOURCE for different levels, compilers and @@ -293,6 +294,10 @@ tests-time64 += \ $(tests-all-time64-chk) \ # tests-time64 +tests-container += \ + tst-fortify-syslog \ + # tests-container + ifeq ($(have-ssp),yes) tests += tst-ssp-1 endif diff --git a/debug/tst-fortify-syslog.c b/debug/tst-fortify-syslog.c new file mode 100644 index 0000000000..26a93d9be8 --- /dev/null +++ b/debug/tst-fortify-syslog.c @@ -0,0 +1,128 @@ +/* Fortify tests for syslog interface. + Copyright (C) 2023 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + 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 + +static const char *str2 = "F"; +static char buf2[10] = "%s"; + +static volatile int chk_fail_ok; +static jmp_buf chk_fail_buf; + +static void +handler (int sig) +{ + if (chk_fail_ok) + { + chk_fail_ok = 0; + longjmp (chk_fail_buf, 1); + } + else + _exit (127); +} + +#define FAIL() \ + do { \ + printf ("Failure on line %d\n", __LINE__); \ + support_record_failure (); \ + } while (0) +#define CHK_FAIL_START \ + chk_fail_ok = 1; \ + if (! setjmp (chk_fail_buf)) \ + { +#define CHK_FAIL_END \ + chk_fail_ok = 0; \ + FAIL (); \ + } + +static void +call_vsyslog (int priority, const char *format, ...) +{ + va_list va; + va_start (va, format); + vsyslog (priority, format, va); + va_end (va); +} + +static void +run_syslog_chk (void *closure) +{ + int n1; + CHK_FAIL_START + syslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1); + CHK_FAIL_END +} + +static void +run_vsyslog_chk (void *closure) +{ + int n1; + CHK_FAIL_START + call_vsyslog (LOG_USER | LOG_DEBUG, buf2, str2, &n1, str2, &n1); + CHK_FAIL_END +} + +static int +do_test (void) +{ + set_fortify_handler (handler); + + int n1, n2; + + n1 = n2 = 0; + syslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2); + TEST_COMPARE (n1, 1); + TEST_COMPARE (n2, 2); + + n1 = n2 = 0; + call_vsyslog (LOG_USER | LOG_DEBUG, "%s%n%s%n", str2, &n1, str2, &n2); + TEST_COMPARE (n1, 1); + TEST_COMPARE (n2, 2); + + strcpy (buf2 + 2, "%n%s%n"); + + /* The wrapper tests need to be in a subprocess because the abort called by + printf does not unlock the internal syslog lock. */ + { + struct support_capture_subprocess result + = support_capture_subprocess (run_syslog_chk, NULL); + support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr); + support_capture_subprocess_free (&result); + } + + { + struct support_capture_subprocess result + = support_capture_subprocess (run_vsyslog_chk, NULL); + support_capture_subprocess_check (&result, "syslog", 0, sc_allow_stderr); + support_capture_subprocess_free (&result); + } + + return 0; +} + +#include