From patchwork Tue Dec 5 17:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 81440 X-Patchwork-Delegate: siddhesh@gotplt.org 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 4CBF7386181F for ; Tue, 5 Dec 2023 17:45:49 +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 716AA3858D32 for ; Tue, 5 Dec 2023 17:45:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 716AA3858D32 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 716AA3858D32 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=1701798337; cv=none; b=Lszcp+ATx2VIjRn4VOFjQ2NpPY028f83sfhWNbInopIV9nwBCYFxKb3nhn/rw4jOWwWQZ9q1qsfa1OS8JiJVwKOJBOHm6BxYL6ApjwkK7zRXirLlU1Cp/uAcKAnqQrQ7rcRGsKL8RaFDmMBD5NKXsfcHn1bsvN60tVQgk8JzTs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701798337; c=relaxed/simple; bh=+ULUpjZAQiQbc9LNTF7VNkS+EVY/wH5T233yRygftyM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qM0gg2hY2KjGBEIXlRgHFaNzQADwU0N6TUx82zQLKHTvRxvajZppsKtGjcJAM+CUnFrIirhXcmScU4nQEtLNAmBGloA6Wss8VjyBB9QtL1pUPQjyw1MK5E9/JSgCD3oGtTH2adbT6WZN4rhIUrMIvDkmwieRovaVXCRVy6j2jTs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701798334; 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=NVtfBze/rTV4ZQvPNMdFTnHErM2Sr7I6ZFrWMakXC1I=; b=d45N0LQIQgnEglSYJSDUo4BP9vbDE/i2YTonwOPzd6JAH+69Hb+vGEW62FSp+Faun308ni 5YlNjyZne8huYKVzDsrZYjoBOykmwopLlij6M+C6U7xiPmWMJzjSdPvAZbJYHM8z8xUWVe fRAe9pdEGGTYmyDEXAi5+BvOBRBUsjA= Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-219-poJu2NswPAqX4DQ3Jlb8_g-1; Tue, 05 Dec 2023 12:45:32 -0500 X-MC-Unique: poJu2NswPAqX4DQ3Jlb8_g-1 Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-5cb6271b225so92337447b3.1 for ; Tue, 05 Dec 2023 09:45:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701798331; x=1702403131; 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=NVtfBze/rTV4ZQvPNMdFTnHErM2Sr7I6ZFrWMakXC1I=; b=bInApWIKyXMHzMrnTEzqLgvVais4YTsDw5gsx6BhUATlsd0/8t9Yi58jQWTOuShu6K GTxNBd7u9j/xZdnHBycMxoh2El+uDWLJ8n+cLYHCvStNcI2ceveQFlG6HHSffnN94cEC fa9SC8vG7LU/86k4gtuck7Ul2m1docWINxO85Y1xtBNpcNUuFWcXnSIrN5x5fLN/eV3w dw3f8L57AyuldNZ/sE1I48WOvUWXKaQEH7D5cIFy8+oLAhSVPU0l4TVzvSbt8v1ho6TD 0Ytqbb7zyFDcdCSGDNNURU2wWtzQlrjFUnLgKP3S6DwLLi5PNyGX7Sp01z+we6KqYpWL xH5g== X-Gm-Message-State: AOJu0Yz3vH6TkoqViRt/f5dSqm+zNU69z7N7HkqNoUUpfOmaGx/a43mB C7wvPPeD5xTGvldJddMw8BbbnHZ33JlldhYTIiHl7bJpYhMJZbeNrMUI62bTyi7L5rfiS2l5R86 C6wIcA341D/5w/nsFzflc3J+Epo+B3B0NocKFMBB+2sywNOCxC+5boL+t8E+0ZIIMUec+ioEdWl GBkQYg X-Received: by 2002:a81:48d4:0:b0:5d8:67b8:6d13 with SMTP id v203-20020a8148d4000000b005d867b86d13mr3583725ywa.76.1701798331111; Tue, 05 Dec 2023 09:45:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEiUJHUlkPXToSHc2BfiffiLVPQf+063XZGRlL0CodwFpdCsGdNoA/xydQ3Xp3o6t4FzYz6Ng== X-Received: by 2002:a81:48d4:0:b0:5d8:67b8:6d13 with SMTP id v203-20020a8148d4000000b005d867b86d13mr3583713ywa.76.1701798330672; Tue, 05 Dec 2023 09:45:30 -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 n205-20020a0dcbd6000000b005d39efe78f4sm4096692ywd.50.2023.12.05.09.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 09:45:29 -0800 (PST) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] rtld: Add glibc.rtld.enable_secure tunable. Date: Tue, 5 Dec 2023 12:45:20 -0500 Message-ID: <20231205174527.1689844-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=-12.6 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_H4, RCVD_IN_MSPIKE_WL, 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. Does not set __libc_enable_secure to 0 if the tunable is set to 0. Ignores any tunables following glib.rtld.enable_secure. One use-case for this addition is to enable testing code paths that depend on __libc_eanble_secure being set without the need to use setxid binaries. --- Changes to v1: * handle the tunable for the dynamic loader as well. NOTE: I'm not certain I've picked the appropriate place to handle glibc.rtld.enable_secure. I tried to make it happen as early as possible to minimize and places where __libc_enable_secure might be checked before the tunable initialization takes place. NEWS | 4 + csu/libc-start.c | 4 + elf/Makefile | 2 + elf/dl-tunables.c | 8 +- elf/dl-tunables.h | 11 +++ elf/dl-tunables.list | 6 ++ elf/tst-rtld-list-tunables.exp | 1 + elf/tst-tunables-enable_secure.c | 115 ++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/dl-sysdep.c | 3 + 9 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 elf/tst-tunables-enable_secure.c diff --git a/NEWS b/NEWS index 8c1c149f91..b04fb15064 100644 --- a/NEWS +++ b/NEWS @@ -43,6 +43,10 @@ Major new features: on thread stack created by pthread_create or memory allocated by malloc). +* A new tunable, glibc.rtld.enable_secure, used to set __libc_enable_secure + to 1. Setting the tunable to 0 does *NOT* set __libc_enable_secure to 0. + Once the tunable is encountered all following tunables are ignored. + Deprecated and removed features, and other changes affecting compatibility: * The ldconfig program now skips file names containing ';' or ending in diff --git a/csu/libc-start.c b/csu/libc-start.c index c3bb6d09bc..2f546a3677 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -266,6 +266,10 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), __tunables_init (__environ); + int32_t tes = TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t, NULL); + if (tes == 1) + __libc_enable_secure = 1; + ARCH_INIT_CPU_FEATURES (); /* Do static pie self relocation after tunables and cpu features diff --git a/elf/Makefile b/elf/Makefile index afec7be084..6c38a72b0d 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 @@ -2672,6 +2673,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 644d21d1b0..d6c55cfaf2 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -250,7 +250,13 @@ parse_tunables (char *valstring) } for (int i = 0; i < ntunables; i++) - tunable_initialize (tunables[i].t, tunables[i].value); + { + tunable_initialize (tunables[i].t, tunables[i].value); + + /* Ignore all tunables after encountering "glibc.rtld.enable_secure" */ + if (tunable_strcmp(tunables[i].t->name, "glibc.rtld.enable_secure") == 0) + break; + } } /* Initialize the tunables list from the environment. For now we only use the diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h index 0df4dde24e..2d4fc84d74 100644 --- a/elf/dl-tunables.h +++ b/elf/dl-tunables.h @@ -164,4 +164,15 @@ tunable_is_name (const char *orig, const char *envname) return false; } +/* Compare two strings. */ +static __always_inline int +tunable_strcmp (const char *left, const char *right) +{ + for (;*left != '\0' && *right != '\0'; right++, left++) + if (*left != *right) + break; + + return *left - *right; +} + #endif diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 1b23fc9473..cb1b35854e 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..e749ec9f17 --- /dev/null +++ b/elf/tst-tunables-enable_secure.c @@ -0,0 +1,115 @@ +/* Check GLIBC_TUNABLES parsing for enable_secure. + 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 + +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. */ + { + "glibc.malloc.check=2:glibc.rtld.enable_secure=1", + 2, + 1, + }, + /* Tunables encountered after enable_secure should be ignored. */ + { + "glibc.rtld.enable_secure=1:glibc.malloc.check=2", + 0, + 1, + }, +}; + +static int +handle_restart (int i) +{ + 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)); + if (tests[i].expected_enable_secure == 1) + { + TEST_COMPARE (1, __libc_enable_secure); + } + 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 diff --git a/sysdeps/unix/sysv/linux/dl-sysdep.c b/sysdeps/unix/sysv/linux/dl-sysdep.c index 1b3dd869b5..d3a30e8d7a 100644 --- a/sysdeps/unix/sysv/linux/dl-sysdep.c +++ b/sysdeps/unix/sysv/linux/dl-sysdep.c @@ -108,6 +108,9 @@ _dl_sysdep_start (void **start_argptr, dl_hwcap_check (); __tunables_init (_environ); + int32_t tes = TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t, NULL); + if (tes == 1) + __libc_enable_secure = 1; /* Initialize DSO sorting algorithm after tunables. */ _dl_sort_maps_init ();