From patchwork Thu Feb 29 20:00:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 86608 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 A485F3858413 for ; Thu, 29 Feb 2024 20:01:37 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D232A3858C5F for ; Thu, 29 Feb 2024 20:01:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D232A3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D232A3858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709236876; cv=none; b=WcF4shKRi7hiLNC+9vS0TMOmc3qyKC3GsW4yFiRlL/x7m38jU1XhU8GieRuh/5DlijKDqRNFTs1inhniCVzALkUqrK0HXxeUy4U3/01T2o4Erzd5VszuOCCcQf946Szcl2gqI5dNI8zi3k/YZsUhkCR3BNVmqP5UR0K1TOeeg0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709236876; c=relaxed/simple; bh=vAN9Vse6jyImRKoj6KxKnnT09vHkXunE7ri2wkXH0L8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=isj5YlPyOPiOg6bTea6F4PO04+RO/c4qsFMM8RGxaw2j0AD2nXTkVPi2jD9OUh2VC3Ewwix3RFT6mH09fnE8kyUpv76gzTpaJgitbZKYw++Wey4BjYmq0osk47T31WzoT0J5PbmJciwfD88jQfCt2Jc0H6lHX7yRfbCSzyyogGw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709236874; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=OdBYGqg1JaN4JRxX0X4SzElmxznjeS6p9e4hfNOEBY4=; b=GJ3Hs9g18mVrrYyk2WlrzwNWdJ/d44ovkAAleQetO4m5XJGYXiMPxLzrJSH0VHDM5ewXQY Evrfei6YeGZ3KAyoYuUGyTZrbuQlksngwNuwZ0+pVappHq/KMvTlNkLDw9ysp0xQOw/qhI hdqEy8nII2v1HMPfiNX7dbTj1xnqPcI= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-OsgzXF3SOBSfDblzIQvJXQ-1; Thu, 29 Feb 2024 15:01:13 -0500 X-MC-Unique: OsgzXF3SOBSfDblzIQvJXQ-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-68f75cca6aaso13154326d6.1 for ; Thu, 29 Feb 2024 12:01:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709236872; x=1709841672; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OdBYGqg1JaN4JRxX0X4SzElmxznjeS6p9e4hfNOEBY4=; b=hR/DKA7MzUq1Y6zu5o49rh+dfDdQX/rFw/OxO3mYjG4aRGOPNx3LY+Mq9LKYgkY/tH 8KmgMQsBY9AWrEJom91XhMhvIllzt/tSL86EJ509j4yXeEbUF3AHbzVT6HXwPNQezmFL Ubj3u8U2XNFlGjaradn8WfVjOi9Zmz6FdbCAswe2ZXiMqRjsF7uULs7YgEJ8XQV3Pq7E M7m+D9E5I8+WKEumeI3lD50pBl3a83RMdZLdkXU6OT2/CerpRkDXEeOC+wqgZnawVxUe RNgFcsFiT7Cm8G1ksDIsd6WmquvNPJEDHLubwKA7x+Bozv50c/39botgm+5RShF8ePlK M+Jw== X-Gm-Message-State: AOJu0Ywq78Fssk3zs7UxTDKBaqd9yB7wnp+fC5fPwFJIPm7JZqiZbD1u cUU8m/u37bHQgK2LNNZ5pJSZSvy1qkt3ulosn7+a7BeHbKvqG1cqhFlmwSO18MqubBriXFyJd27 kZymFrGy/Ry0WMJftkJDwa19oT3XnyB+GYCWD87fMDMGvsQ/imEu1qjedfS4NUqfZ6dG/b5T6xQ TIF4WgAJOlhUUwk4d/KRMGzCzp/9dCX8GS3Srot8AJPw== X-Received: by 2002:ad4:4f93:0:b0:68f:a76c:1e0c with SMTP id em19-20020ad44f93000000b0068fa76c1e0cmr3373500qvb.48.1709236871999; Thu, 29 Feb 2024 12:01:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNlokfC7ngbkTI8i9INItd0FVif9MktmS5phsv4NNtfifBNZReiefMbOntRYDXw7vgBq17Kw== X-Received: by 2002:ad4:4f93:0:b0:68f:a76c:1e0c with SMTP id em19-20020ad44f93000000b0068fa76c1e0cmr3373478qvb.48.1709236871581; Thu, 29 Feb 2024 12:01:11 -0800 (PST) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id l14-20020a056214028e00b0068fecdef142sm1060506qvv.130.2024.02.29.12.01.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 12:01:10 -0800 (PST) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Siddhesh Poyarekar Subject: [committed] rtld: Add glibc.rtld.enable_secure tunable. Date: Thu, 29 Feb 2024 15:00:47 -0500 Message-ID: <20240229200107.2700623-1-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org Add a tunable for setting __libc_enable_secure to 1. Do not set __libc_enable_secure to 0 if the tunable is set to 0. Ignore all tunables if glib.rtld.enable_secure is set. One use-case for this addition is to enable testing code paths that depend on __libc_enable_secure being set without the need to use setxid binaries. Reviewed-by: Siddhesh Poyarekar --- NEWS | 5 ++ elf/Makefile | 2 + elf/dl-tunables.c | 11 +++ elf/dl-tunables.list | 6 ++ elf/tst-rtld-list-tunables.exp | 1 + elf/tst-tunables-enable_secure.c | 126 +++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+) create mode 100644 elf/tst-tunables-enable_secure.c diff --git a/NEWS b/NEWS index 2d8eaffc58..19f4e93ea3 100644 --- a/NEWS +++ b/NEWS @@ -103,6 +103,11 @@ Major new features: exp, expf, exp10, exp10f, exp2, exp2f, expm1, expm1f, log, logf, log10, log10f, log1p, log1pf, log2, log2f, sin, sinf, tan, tanf. +* A new tunable, glibc.rtld.enable_secure, used to run a program + as if it were a setuid process. This is currently a testing tool to allow + more extensive verification tests for AT_SECURE programs and not meant to + be a security feature. + Deprecated and removed features, and other changes affecting compatibility: * The ldconfig program now skips file names containing ';' or ending in diff --git a/elf/Makefile b/elf/Makefile index f0439ef634..520a270f0f 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -285,6 +285,7 @@ tests-static-internal := \ tst-tls1-static \ tst-tls1-static-non-pie \ tst-tunables \ + tst-tunables-enable_secure \ # tests-static-internal CRT-tst-tls1-static-non-pie := $(csu-objpfx)crt1.o @@ -2681,6 +2682,7 @@ $(objpfx)tst-glibc-hwcaps-mask.out: \ $(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps tst-tunables-ARGS = -- $(host-test-program-cmd) +tst-tunables-enable_secure-ARGS = -- $(host-test-program-cmd) $(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so $(SHELL) $< $(objpfx)ld.so '$(test-wrapper-env)' \ diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 03e1a68675..d3ccd2ecd4 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -223,6 +223,17 @@ parse_tunables_string (const char *valstring, struct tunable_toset_t *tunables) { tunables[ntunables++] = (struct tunable_toset_t) { cur, value, p - value }; + + /* Ignore tunables if enable_secure is set */ + if (tunable_is_name ("glibc.rtld.enable_secure", name)) + { + tunable_num_t val = (tunable_num_t) _dl_strtoul (value, NULL); + if (val == 1) + { + __libc_enable_secure = 1; + return 0; + } + } break; } } diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 1b40407814..1186272c81 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -136,6 +136,12 @@ glibc { minval: 0 default: 512 } + enable_secure { + type: INT_32 + minval: 0 + maxval: 1 + default: 0 + } } mem { diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp index 2233ea9c7c..db0e1c86e9 100644 --- a/elf/tst-rtld-list-tunables.exp +++ b/elf/tst-rtld-list-tunables.exp @@ -12,5 +12,6 @@ glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0x[f]+) glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+) glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+) glibc.rtld.dynamic_sort: 2 (min: 1, max: 2) +glibc.rtld.enable_secure: 0 (min: 0, max: 1) glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10) glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+) diff --git a/elf/tst-tunables-enable_secure.c b/elf/tst-tunables-enable_secure.c new file mode 100644 index 0000000000..e31e1f7faa --- /dev/null +++ b/elf/tst-tunables-enable_secure.c @@ -0,0 +1,126 @@ +/* Check GLIBC_TUNABLES parsing for enable_secure. + Copyright (C) 2024 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 + +static int restart; +#define CMDLINE_OPTIONS \ + { "restart", no_argument, &restart, 1 }, + +static const struct test_t +{ + const char *env; + int32_t expected_malloc_check; + int32_t expected_enable_secure; +} tests[] = +{ + /* Expected tunable format. */ + /* Tunables should be ignored if enable_secure is set. */ + { + "glibc.malloc.check=2:glibc.rtld.enable_secure=1", + 0, + 1, + }, + /* Tunables should be ignored if enable_secure is set. */ + { + "glibc.rtld.enable_secure=1:glibc.malloc.check=2", + 0, + 1, + }, + /* Tunables should be set if enable_secure is unset. */ + { + "glibc.rtld.enable_secure=0:glibc.malloc.check=2", + 2, + 0, + }, +}; + +static int +handle_restart (int i) +{ + if (tests[i].expected_enable_secure == 1) + { + TEST_COMPARE (1, __libc_enable_secure); + } + else + { + TEST_COMPARE (tests[i].expected_malloc_check, + TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL)); + TEST_COMPARE (tests[i].expected_enable_secure, + TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t, + NULL)); + } + return 0; +} + +static int +do_test (int argc, char *argv[]) +{ + /* We must have either: + - One or four parameters left if called initially: + + path to ld.so optional + + "--library-path" optional + + the library path optional + + the application name + + the test to check */ + + TEST_VERIFY_EXIT (argc == 2 || argc == 5); + + if (restart) + return handle_restart (atoi (argv[1])); + + char nteststr[INT_BUFSIZE_BOUND (int)]; + + char *spargv[10]; + { + int i = 0; + for (; i < argc - 1; i++) + spargv[i] = argv[i + 1]; + spargv[i++] = (char *) "--direct"; + spargv[i++] = (char *) "--restart"; + spargv[i++] = nteststr; + spargv[i] = NULL; + } + + for (int i = 0; i < array_length (tests); i++) + { + snprintf (nteststr, sizeof nteststr, "%d", i); + + printf ("[%d] Spawned test for %s\n", i, tests[i].env); + setenv ("GLIBC_TUNABLES", tests[i].env, 1); + struct support_capture_subprocess result + = support_capture_subprogram (spargv[0], spargv); + support_capture_subprocess_check (&result, "tst-tunables-enable_secure", + 0, sc_allow_stderr); + support_capture_subprocess_free (&result); + } + + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include