From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 5D4AC3858D38 for ; Fri, 12 Apr 2024 14:14:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D4AC3858D38 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 5D4AC3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712931245; cv=none; b=hzp5OU44NwawEFkIa0q8k01NaxGQ97F/DMotN2q9lr1GDcLkNPzuYb7jdM67TKhwGDLtmzk++pZCik5Gj5Jx5lP7HicY+TY9i8FL18lJKNiBcdkXt7zNWc/7Imt1wz5IXZotuuGa/sMJEIKeZTBWxTQin4szXaxziDHv8MWr5do= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712931245; c=relaxed/simple; bh=w2oOtZs7FVEQ/+RzQu8pHHfRWEFLGv4wBV0FrVHUhsI=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=W4BSKplmdqQqtmWzHXrkCjx7pc8GkEWF1Sp7R0vUiA6xgA6VF5AnvH8t4jQAwcPnIG/ekfihI7C5RP0KaBwrtakXDcuJjP2mpSYECN41FP0wBqS0pWD1MaKXbeYQ8BYDIcpbIX3mG5wSxe1lNVNn5iaZK5uIEKXG2lYCJ9QpdAg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1e0b889901bso8345315ad.1 for ; Fri, 12 Apr 2024 07:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712931240; x=1713536040; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:cc:organization:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=/nRxYqbYQDBPt50PaYed0PXLQPgJDJsSqbe4IP9AqJU=; b=h2Rsnph5i/NPhsa7geLIED4XQFmtkNfTLhdOM5ttBqXYKiI0+ON/PsVaMJ+3PpuRRv pZmxx24f/Kb9eI9YbFcMvHZ7KZsnQXxeshIYwoQeIBARpRjqGlWkVZV7cWvz8LwcZxhV 9/vJid/2v037l/LgSUwL2jBUCzRZAdMFkRJNzhB4EegG+h1HSTgKJCLbMoSCfNn2SYei t9XPZAMtEIORhzYZkdwBZHaUNLqzGRwUMD4g9RHFMCr2iLqTzLYWBY40/J24HGOtDX4o PhSoJ60+2hyHcGkr2td0U79gtYzApdWSKz1CGNStxSrGcUtpOkzHVTIpaZM+9FmJKFCb mcpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712931240; x=1713536040; h=content-transfer-encoding:in-reply-to:cc:organization:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/nRxYqbYQDBPt50PaYed0PXLQPgJDJsSqbe4IP9AqJU=; b=eFCuxz4exSt9S86P1gfEoluY1UQv3JIf6teEbJA6VjoUyb7fo9/KrXMqqsUfcYPMEE PJfxRUrmDSb1h/qFNCJHqUfpCGw97jrl4hyArNOGmC/EKf8OXD90dXWXZnuqkSWxOBFI qtXufwfr0NQgApD1sEOG9ozLPSJocOJPLInTc7HsCc2jJ3C5TXULGUKKlveyR/ir1xea Sv6VCtow6y8SozPuzVEZ2B/U5rWOkNwTCFH+ZbOb2eyW8vhFOnG6SJbBcgJjTizWOmiu ClqGPa0Ru3ImJ+Z+m/5uHAYtUL3hc9Vuvi1Eqr1DmIhP1R9SG5bsZQiYW7So0oshR0OY D2YA== X-Gm-Message-State: AOJu0Yz9PkvNsxWduVALywyZ5puZMtBTf39P7GG40zcgIGsujXCZsjM/ DvB3wCJ2qZidyiZ6V2ZraburyP5czz1Z+m3MMjrovQkkYs65FHQP8Uma98GS59iGc4ETj0HvdQ8 c X-Google-Smtp-Source: AGHT+IF/f+9NW9ZLvbOHNH6PTWXegJ9TI4evAT275eR6YQzw/egG7aNSfkMpLWnGC10ZO1BDKEVxlw== X-Received: by 2002:a17:902:6505:b0:1e5:c06c:68a9 with SMTP id b5-20020a170902650500b001e5c06c68a9mr929432plk.12.1712931240463; Fri, 12 Apr 2024 07:14:00 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c0:d8e5:c10b:67bb:5fcf:ca20? ([2804:1b3:a7c0:d8e5:c10b:67bb:5fcf:ca20]) by smtp.gmail.com with ESMTPSA id d13-20020a170903230d00b001e5c13246ccsm909708plh.39.2024.04.12.07.13.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 Apr 2024 07:13:59 -0700 (PDT) Message-ID: <2c2922df-4900-4efb-b317-6d6760c5a61c@linaro.org> Date: Fri, 12 Apr 2024 11:13:57 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] elf/rtld: Count skipped environment variables for enable_secure To: libc-alpha@sourceware.org, Joe Simmons-Talbott References: <20240411165342.3511107-1-josimmon@redhat.com> Content-Language: en-US From: Adhemerval Zanella Netto Organization: Linaro Cc: Andreas Schwab , Carlos O'Donell In-Reply-To: <20240411165342.3511107-1-josimmon@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.8 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 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: On 11/04/24 13:53, Joe Simmons-Talbott wrote: > 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 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 | 3 +++ > elf/rtld.c | 31 +++++++++++++++++++------- > elf/tst-tunables-enable_secure-env.c | 33 ++++++++++++++++++++++++++++ > 3 files changed, 59 insertions(+), 8 deletions(-) > create mode 100644 elf/tst-tunables-enable_secure-env.c > > diff --git a/elf/Makefile b/elf/Makefile > index 6dad11bcfb..5c35db1799 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 := \ > @@ -336,6 +337,8 @@ tst-tls9-static-ENV = $(static-dlopen-environment) > tst-single_threaded-static-dlopen-ENV = $(static-dlopen-environment) > tst-rootdir-ENV = LD_LIBRARY_PATH=/ > > +tst-tunables-enable_secure-env-ENV = GLIBC_TUNABLES=glibc.rtld.enable_secure=1 > + I think since the idea is to check for running ld.so directly, you will need to explicit issue the loader: diff --git a/elf/Makefile b/elf/Makefile index 5c35db1799..1598fbd6d7 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -337,8 +337,6 @@ tst-tls9-static-ENV = $(static-dlopen-environment) tst-single_threaded-static-dlopen-ENV = $(static-dlopen-environment) tst-rootdir-ENV = LD_LIBRARY_PATH=/ -tst-tunables-enable_secure-env-ENV = GLIBC_TUNABLES=glibc.rtld.enable_secure=1 - tests += \ argv0test \ constload1 \ @@ -1192,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 @@ -2219,6 +2218,13 @@ $(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 Otherwise this won't check the bug with --enable-hardcoded-path-in-tests. The rest looks ok. > tests += \ > argv0test \ > constload1 \ > 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