]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
posix: execvpe: skip $PATH components that are too long [BZ #33626]
authorPádraig Brady <P@draigBrady.com>
Fri, 13 Feb 2026 19:41:13 +0000 (19:41 +0000)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 18 Feb 2026 12:34:26 +0000 (09:34 -0300)
* posix/execvpe.c (__execvpe_common): Rather than error out
with ENAMETOOLONG, just ignore and try the next path.
Note we know the FILE length is <= NAME_MAX, so the ENAMETOOLONG
almost certainly pertains to the current $PATH entry.
* posix/tst-execvpe7.c: A new test based on tst-execvp3.c.
* posix/Makefile: Reference the new test.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
posix/Makefile
posix/execvpe.c
posix/tst-execvpe7.c [new file with mode: 0644]

index f37794846287ff2318b290a5d25419c0375d1dd0..ec28b9e1dabcf5d22f1e69b6b2bff531e8d804a1 100644 (file)
@@ -277,6 +277,7 @@ tests := \
   tst-execvpe4 \
   tst-execvpe5 \
   tst-execvpe6 \
+  tst-execvpe7 \
   tst-fexecve \
   tst-fnmatch \
   tst-fnmatch2 \
index 29e8bcd3d13b04d238914a53ffaf2d0bc37ca7e9..ec56e297b05f124b6162cabb6280aec8c72e2f69 100644 (file)
@@ -157,6 +157,9 @@ __execvpe_common (const char *file, char *const argv[], char *const envp[],
          /* Those errors indicate the file is missing or not executable
             by us, in which case we want to just try the next path
             directory.  */
+         case ENAMETOOLONG:
+         /* We've already verified that the FILE length is < NAME_MAX,
+            so this implies a path component is too long, so skip it.  */
          case ENODEV:
          case ETIMEDOUT:
          /* Some strange filesystems like AFS return even
diff --git a/posix/tst-execvpe7.c b/posix/tst-execvpe7.c
new file mode 100644 (file)
index 0000000..193d68b
--- /dev/null
@@ -0,0 +1,74 @@
+/* Check execvpe does not fail when encountering long $PATH components.
+   Copyright (C) 2026 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 <stdlib.h>
+#include <libgen.h>
+#include <limits.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
+
+static char *fname;
+
+static void
+do_prepare (int c, char **argv)
+{
+  int fd;
+  TEST_VERIFY_EXIT ((fd = create_temp_file ("testscript", &fname)) != -1);
+
+  dprintf (fd, "echo foo\n");
+  TEST_VERIFY_EXIT (fchmod (fd, 0700) == 0);
+  xclose (fd);
+}
+#define PREPARE do_prepare
+
+static void
+run_execvpe (void *closure)
+{
+  char *argv[] = { fname, NULL };
+  execvpe (basename (fname), argv, NULL);
+
+  support_record_failure ();
+}
+
+static int
+do_test (void)
+{
+  /* Linux 6.17 at least will give ENAMETOOLONG once any
+     path component is > NAME_MAX, so ensure we skip such paths
+     rather than failing.  [BZ #33626] */
+  char *path = xasprintf ("%*s:%s", NAME_MAX+1, "", test_dir);
+  TEST_VERIFY_EXIT (setenv ("PATH", path, 1) == 0);
+  free (path);
+
+  struct support_capture_subprocess result
+    = support_capture_subprocess (run_execvpe, NULL);
+  support_capture_subprocess_check (&result, "execvpe", 0, sc_allow_stdout);
+
+  TEST_COMPARE_STRING (result.out.buffer, "foo\n");
+
+  support_capture_subprocess_free (&result);
+
+  return 0;
+}
+
+#include <support/test-driver.c>