unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Joe Simmons-Talbott <josimmon@redhat.com>
To: libc-alpha@sourceware.org
Cc: Joe Simmons-Talbott <josimmon@redhat.com>,
	Andreas Schwab <schwab@suse.de>,
	Carlos O'Donell <carlos@redhat.com>
Subject: [PATCH v3] elf/rtld: Count skipped environment variables for enable_secure
Date: Thu, 11 Apr 2024 12:53:12 -0400	[thread overview]
Message-ID: <20240411165342.3511107-1-josimmon@redhat.com> (raw)

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
+
 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");
     }
 }
 \f
-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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+
+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 <support/test-driver.c>
-- 
2.44.0


             reply	other threads:[~2024-04-11 16:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11 16:53 Joe Simmons-Talbott [this message]
2024-04-12 14:13 ` [PATCH v3] elf/rtld: Count skipped environment variables for enable_secure Adhemerval Zanella Netto
2024-04-12 15:23   ` Joe Simmons-Talbott

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240411165342.3511107-1-josimmon@redhat.com \
    --to=josimmon@redhat.com \
    --cc=carlos@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).