From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7E2703858420 for ; Fri, 12 Apr 2024 15:21:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E2703858420 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 7E2703858420 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712935295; cv=none; b=Yo1a1bS0Doer55UFrKOXsYM97/aFvmb13rWS3iYNI1KJN1fGYcEd1m3DGUaf7DTLOucyugfvbWd9HVaJpdlVE6XzF6/kgf047qjiOqXOF5KysmPngxE1uEyeMs4PoOcDxzaYwZgJ1LZJ9Qv8ZwuZarHraA04hMC/p13d/4ltWpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712935295; c=relaxed/simple; bh=PFxs8zmQT+AqtMHz6uGvETuJ9RPCGC9P+Bt7/IbX7FY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PV8YLNQWGteqpnc2v2HeLDNDKD3nRQLZthn1OHZcDYw77jXo4JkVtk+BJqDWkqZEaUjPAe9j0luZJYA4GvjVMllZ2948YdiHVkRLiSIh/VD9w9HdQMZDGIQugj6ps64dXDvYddWvuSRp4zHxkwQJUK5M+acI+d3xGKtHJuYGDNg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712935292; 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=HuJw23UeNWoSKJkiljYb6Kna118ytnw7mWvmXSKEJ10=; b=ievCAXl6TP8ZURItT0BvWg8v3rX3X7L58w3/S4JpyaU32LHo+LS04YqZJCO+LpQt8/glFJ GTen/m0nAL7m/OIV4yReVwlUes++hHm90dpTzNBEDL8hHw1dIdbTTmYiZxFRx+WJBdUHoK 535c8DqLnisAfvvi9WMY2wkBcxtmVYc= 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-20-G6TwkzqYMNymrxMSsTIA9w-1; Fri, 12 Apr 2024 11:21:31 -0400 X-MC-Unique: G6TwkzqYMNymrxMSsTIA9w-1 Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-618891b439eso8148057b3.3 for ; Fri, 12 Apr 2024 08:21:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712935289; x=1713540089; 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=uV3RsGbWYtH6WFVSHtyQjxmyZHEi5NFfJR0IzWx3gsY=; b=XDiHbIsTmstECMK+gR9eeqq9Kiivml89mEA2ybO3nyYiHmLsJYxaz0xlMRo+XOT+1l j8Gg7tE9svsxocMVyJExiHBiAuRcNJz6IFbOyijBzWoBEracBtJNisCfrCc2u7N0P06n rWABZ+FPjokWx0TbeWC4EIHbeuvcxQCM/BuCalnHrkJJhCPba1/I+goE/GdyTbFnpeQC +gNxFzDPbJ328KLJ3zRrmaW4oGxbMzQOwr+b11cX8fxBwsj09xDW93av6KI/UKVuIhwT nZdh+bB1qgPsQZZhjiESoq/UIrEcUbSUymcZ1+W20G3o/Xb7G82Z8wsH6G3xuIhnl28e ikSw== X-Gm-Message-State: AOJu0YxUdnROaPbCqxAUhPo1UXqTifYVttm5st84lEvd6JA6gmLUGgGN EwWU/+yyBNsSuLigYn8bVax5PKHj2OH1jktg0E7QJXvmO5ETAZbiKkhAxwCyh4o6DpFiZDoyaY1 0bO0EjXNoqN7TyCw1UZ9F7ZeneIiok5l0P5PA2y9QmOvyUqhhMSH22lYQb/ixBTiRmhCbN+sinW A9gnopuTU7sPP96x3pRNTzSX5pfDcMA9QimTplg4uzxA== X-Received: by 2002:a05:6902:c2:b0:dc6:49d3:dd52 with SMTP id i2-20020a05690200c200b00dc649d3dd52mr2772843ybs.34.1712935289607; Fri, 12 Apr 2024 08:21:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHs+T/QdkY9SmXoq8RZsYJSoNuzmYDFryl/TZfVAddw3baC439rdcIEXqfkeVcQRsMxVKi4Ww== X-Received: by 2002:a05:6902:c2:b0:dc6:49d3:dd52 with SMTP id i2-20020a05690200c200b00dc649d3dd52mr2772818ybs.34.1712935289159; Fri, 12 Apr 2024 08:21:29 -0700 (PDT) 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 c16-20020a0ce150000000b006994394017dsm2413773qvl.27.2024.04.12.08.21.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 08:21:28 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Andreas Schwab , Carlos O'Donell , Adhemerval Zanella Netto Subject: [PATCH v4] elf/rtld: Count skipped environment variables for enable_secure Date: Fri, 12 Apr 2024 11:21:18 -0400 Message-ID: <20240412152126.663092-1-josimmon@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_INVALID,DKIM_SIGNED,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 List-Id: When using the glibc.rtld.enable_secure tunable we need to keep track of the count of environment variables we skip due to __libc_enable_secure being set and adjust the auxv section of the stack. This fixes an assertion when running ld.so directly with glibc.rtld.enable_secure set. Add a testcase that ensures the assert is not hit. elf/rtld.c:1324 assert (auxv == sp + 1); --- Changes to v3: * Run the testcase with ld.so explicitly. Changes to v2: * Add testcase that triggers the assertion before this patch and does not after the patch. Changes to v1: * Add comment explaining how and why skip_env is adjusted. elf/Makefile | 10 +++++++++ elf/rtld.c | 31 +++++++++++++++++++------- elf/tst-tunables-enable_secure-env.c | 33 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 elf/tst-tunables-enable_secure-env.c diff --git a/elf/Makefile b/elf/Makefile index 6dad11bcfb..ea8a078f3e 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -315,6 +315,7 @@ tests := \ tst-leaks1 \ tst-stringtable \ tst-tls9 \ + tst-tunables-enable_secure-env \ # tests tests-internal := \ @@ -1189,6 +1190,7 @@ tests-special += \ $(objpfx)tst-trace3.out \ $(objpfx)tst-trace4.out \ $(objpfx)tst-trace5.out \ + $(objpfx)tst-tunables-enable_secure-env.out \ $(objpfx)tst-unused-dep-cmp.out \ $(objpfx)tst-unused-dep.out \ # tests-special @@ -2216,6 +2218,14 @@ $(objpfx)tst-unused-dep-cmp.out: $(objpfx)tst-unused-dep.out cmp $< /dev/null > $@; \ $(evaluate-test) +$(objpfx)tst-tunables-enable_secure-env.out: $(objpfx)tst-tunables-enable_secure-env + $(test-wrapper-env) \ + GLIBC_TUNABLES=glibc.rtld.enable_secure=1 \ + $(rtld-prefix) \ + $< > $@; \ + $(evaluate-test) + + $(objpfx)tst-audit11.out: $(objpfx)tst-auditmod11.so $(objpfx)tst-audit11mod1.so tst-audit11-ENV = LD_AUDIT=$(objpfx)tst-auditmod11.so $(objpfx)tst-audit11mod1.so: $(objpfx)tst-audit11mod2.so diff --git a/elf/rtld.c b/elf/rtld.c index 18d73f19c6..d116a436f5 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -155,7 +155,7 @@ static void dl_main_state_init (struct dl_main_state *state); Since all of them start with `LD_' we are a bit smarter while finding all the entries. */ extern char **_environ attribute_hidden; -static void process_envvars (struct dl_main_state *state); +static int process_envvars (struct dl_main_state *state); int _dl_argc attribute_relro attribute_hidden; char **_dl_argv attribute_relro = NULL; @@ -1289,7 +1289,7 @@ rtld_setup_main_map (struct link_map *main_map) _dl_argv and _dl_argc accordingly. Those arguments are removed from argv here. */ static void -_dl_start_args_adjust (int skip_args) +_dl_start_args_adjust (int skip_args, int skip_env) { void **sp = (void **) (_dl_argv - skip_args - 1); void **p = sp + skip_args; @@ -1321,7 +1321,7 @@ _dl_start_args_adjust (int skip_args) while (*p != NULL); #ifdef HAVE_AUX_VECTOR - void **auxv = (void **) GLRO(dl_auxv) - skip_args; + void **auxv = (void **) GLRO(dl_auxv) - skip_args - skip_env; GLRO(dl_auxv) = (ElfW(auxv_t) *) auxv; /* Aliasing violation. */ assert (auxv == sp + 1); @@ -1352,6 +1352,7 @@ dl_main (const ElfW(Phdr) *phdr, unsigned int i; bool rtld_is_main = false; void *tcbp = NULL; + int skip_env = 0; struct dl_main_state state; dl_main_state_init (&state); @@ -1365,7 +1366,7 @@ dl_main (const ElfW(Phdr) *phdr, #endif /* Process the environment variable which control the behaviour. */ - process_envvars (&state); + skip_env = process_envvars (&state); #ifndef HAVE_INLINED_SYSCALLS /* Set up a flag which tells we are just starting. */ @@ -1630,7 +1631,7 @@ dl_main (const ElfW(Phdr) *phdr, _dl_argv[0] = argv0; /* Adjust arguments for the application entry point. */ - _dl_start_args_adjust (_dl_argv - orig_argv); + _dl_start_args_adjust (_dl_argv - orig_argv, skip_env); } else { @@ -2534,11 +2535,12 @@ a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n"); } } -static void +static int process_envvars_secure (struct dl_main_state *state) { char **runp = _environ; char *envline; + int skip_env = 0; while ((envline = _dl_next_ld_env_entry (&runp)) != NULL) { @@ -2580,6 +2582,14 @@ process_envvars_secure (struct dl_main_state *state) const char *nextp = UNSECURE_ENVVARS; do { + /* Keep track of the number of environment variables that were set in + the environment and are unset below. Use getenv() which returns + non-NULL if the variable is set in the environment. This count is + needed if we need to adjust the location of the AUX vector on the + stack when running ld.so directly. */ + if (getenv (nextp) != NULL) + skip_env++; + unsetenv (nextp); nextp = strchr (nextp, '\0') + 1; } @@ -2592,6 +2602,8 @@ process_envvars_secure (struct dl_main_state *state) || state->mode != rtld_mode_normal || state->version_info) _exit (5); + + return skip_env; } static void @@ -2745,13 +2757,16 @@ process_envvars_default (struct dl_main_state *state) } } -static void +static int process_envvars (struct dl_main_state *state) { + int skip_env = 0; if (__glibc_unlikely (__libc_enable_secure)) - process_envvars_secure (state); + skip_env += process_envvars_secure (state); else process_envvars_default (state); + + return skip_env; } #if HP_TIMING_INLINE diff --git a/elf/tst-tunables-enable_secure-env.c b/elf/tst-tunables-enable_secure-env.c new file mode 100644 index 0000000000..24e846f299 --- /dev/null +++ b/elf/tst-tunables-enable_secure-env.c @@ -0,0 +1,33 @@ +/* Check enable_secure tunable handles removed ENV variables without + assertions. + 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 + +static int +do_test (int argc, char *argv[]) +{ + /* Ensure that no assertions are hit when a dynamically linked application + runs. This test requires that GLIBC_TUNABLES=glibc.rtld.enable_secure=1 + is set. */ + return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include -- 2.44.0