[libc-commits] [libc] [libc] Support execution of scripts without shebangs in execvpe (PR #240040)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 07:46:17 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/240040

>From 5a13f9ff492adbad0198ab3ef44c1593edde36ab Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 03:08:33 +0530
Subject: [PATCH 1/3] [libc] Support execution of scripts without shebangs in
 execvpe

Per POSIX.1-2024, if execve fails with ENOEXEC, execvpe/execvp/execlp
must execute the file using the system shell (/bin/sh) with the file
as the first argument and the remaining arguments appended.

- Implements execve_with_shell_fallback in exec_utils.h.
- Falls back to /bin/sh on ENOEXEC both for direct paths and PATH searches.
- Adds integration tests in test/integration/src/unistd/execlp_test.cpp.

Fixes #232374.
---
 libc/src/unistd/linux/exec_utils.h            | 50 ++++++++++++++++---
 .../integration/src/unistd/execlp_test.cpp    | 37 ++++++++++++++
 .../src/unistd/testdata/CMakeLists.txt        |  8 +++
 3 files changed, 88 insertions(+), 7 deletions(-)

diff --git a/libc/src/unistd/linux/exec_utils.h b/libc/src/unistd/linux/exec_utils.h
index 8260027a1a1d0d..942efa8f44b012 100644
--- a/libc/src/unistd/linux/exec_utils.h
+++ b/libc/src/unistd/linux/exec_utils.h
@@ -63,18 +63,53 @@ LIBC_INLINE ErrorOr<int> exec_with_arg_list(const char *arg0, va_list *args,
   return exec(argv);
 }
 
-// TODO: POSIX requires that if execve fails with ENOEXEC, the file is executed
-// using the system shell (as if by `sh -- file args...`). The kernel only
-// handles ELF, "#!" scripts, and binfmt_misc formats, so shebang-less scripts
-// currently fail with ENOEXEC instead of being run by /bin/sh.
+LIBC_INLINE ErrorOr<int> execve_with_shell_fallback(const char *path,
+                                                    char *const argv[],
+                                                    char *const envp[]) {
+  auto ret = linux_syscalls::execve(path, argv, envp);
+  if (!ret && ret.error() == ENOEXEC) {
+    size_t argc = 0;
+    if (argv != nullptr) {
+      while (argv[argc] != nullptr)
+        ++argc;
+    }
+    size_t count = (argc > 0) ? argc : 1;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wvla"
+#if __has_warning("-Wvla-cxx-extension")
+#pragma GCC diagnostic ignored "-Wvla-cxx-extension"
+#endif
+    char *new_argv[count + 2];
+#pragma GCC diagnostic pop
+    new_argv[0] = const_cast<char *>("sh");
+    new_argv[1] = const_cast<char *>(path);
+    if (argv != nullptr && argc > 1) {
+      for (size_t i = 1; i < argc; ++i)
+        new_argv[i + 1] = argv[i];
+    }
+    new_argv[count + 1] = nullptr;
+
+    constexpr const char *DEFAULT_BSHELL = "/bin/sh";
+    return linux_syscalls::execve(DEFAULT_BSHELL, new_argv, envp);
+  }
+  return ret;
+}
+
 LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
                                  char *const envp[]) {
   if (file.empty())
     return Error(ENOENT);
 
+  char buf[PATH_MAX];
+
   // If there's a / in file, treat it as a path.
-  if (file.find_first_of('/') != cpp::string_view::npos)
-    return linux_syscalls::execve(file.data(), argv, envp);
+  if (file.find_first_of('/') != cpp::string_view::npos) {
+    if (file.size() >= PATH_MAX)
+      return Error(ENAMETOOLONG);
+    inline_memcpy(buf, file.data(), file.size());
+    buf[file.size()] = '\0';
+    return execve_with_shell_fallback(buf, argv, envp);
+  }
 
   // Otherwise treat file as the name of a file in a directory, search the
   // directories listed in PATH.
@@ -88,7 +123,6 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
   cpp::string_view path_env =
       env_path != nullptr ? cpp::string_view(env_path) : DEFAULT_PATH;
 
-  char buf[PATH_MAX];
   bool seen_eacces = false;
   int last_error = ENOENT;
 
@@ -121,6 +155,8 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
     case ENOTDIR:
       last_error = err;
       break;
+    case ENOEXEC:
+      return execve_with_shell_fallback(buf, argv, envp);
     default:
       return Error(err);
     }
diff --git a/libc/test/integration/src/unistd/execlp_test.cpp b/libc/test/integration/src/unistd/execlp_test.cpp
index 73aaddbe6b5340..68b84cdd846b76 100644
--- a/libc/test/integration/src/unistd/execlp_test.cpp
+++ b/libc/test/integration/src/unistd/execlp_test.cpp
@@ -130,6 +130,41 @@ void fork_and_execlp_signal_exit() {
   ASSERT_TRUE(WTERMSIG(status) == SIGUSR1);
 }
 
+void fork_and_execlp_shebangless_script() {
+  pid_t pid = LIBC_NAMESPACE::fork();
+  if (pid == 0) {
+    ASSERT_EQ(LIBC_NAMESPACE::setenv("PATH", "testdata", 1), 0);
+    const char *file = "shebangless.sh";
+    LIBC_NAMESPACE::execlp(file, "shebangless.sh",
+                           static_cast<char *>(nullptr));
+    LIBC_NAMESPACE::_exit(errno);
+  }
+  ASSERT_TRUE(pid > 0);
+  int status;
+  pid_t cpid = LIBC_NAMESPACE::waitpid(pid, &status, 0);
+  ASSERT_TRUE(cpid > 0);
+  ASSERT_EQ(cpid, pid);
+  ASSERT_TRUE(WIFEXITED(status));
+  ASSERT_EQ(WEXITSTATUS(status), 42);
+}
+
+void fork_and_execlp_shebangless_slash_path() {
+  pid_t pid = LIBC_NAMESPACE::fork();
+  if (pid == 0) {
+    const char *path = "testdata/shebangless.sh";
+    LIBC_NAMESPACE::execlp(path, "shebangless.sh",
+                           static_cast<char *>(nullptr));
+    LIBC_NAMESPACE::_exit(errno);
+  }
+  ASSERT_TRUE(pid > 0);
+  int status;
+  pid_t cpid = LIBC_NAMESPACE::waitpid(pid, &status, 0);
+  ASSERT_TRUE(cpid > 0);
+  ASSERT_EQ(cpid, pid);
+  ASSERT_TRUE(WIFEXITED(status));
+  ASSERT_EQ(WEXITSTATUS(status), 42);
+}
+
 TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
           [[maybe_unused]] char **envp) {
   fork_and_execlp_slash_path();
@@ -139,5 +174,7 @@ TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
   execlp_nonexistent_file();
   execlp_reports_eacces();
   fork_and_execlp_signal_exit();
+  fork_and_execlp_shebangless_script();
+  fork_and_execlp_shebangless_slash_path();
   return 0;
 }
diff --git a/libc/test/integration/src/unistd/testdata/CMakeLists.txt b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
index 3fcce1d8856692..a0fb9a2bef1b4f 100644
--- a/libc/test/integration/src/unistd/testdata/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
@@ -18,3 +18,11 @@ set(execvp_noexec_file
 file(WRITE ${execvp_noexec_file} "#!/bin/sh\nexit 1\n")
 file(CHMOD ${execvp_noexec_file}
   PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
+
+# A script without a shebang to test fallback execution via /bin/sh on ENOEXEC.
+set(shebangless_file
+  ${CMAKE_CURRENT_BINARY_DIR}/shebangless.sh)
+file(WRITE ${shebangless_file} "exit 42\n")
+file(CHMOD ${shebangless_file}
+  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+

>From 7dfac6065eb3a912ff4adf6ca793b1a7ae4279a3 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 12:08:32 +0530
Subject: [PATCH 2/3] [libc] Format testdata/CMakeLists.txt with
 cmake_format.py

---
 libc/test/integration/src/unistd/testdata/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/integration/src/unistd/testdata/CMakeLists.txt b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
index a0fb9a2bef1b4f..1f26044edff1b5 100644
--- a/libc/test/integration/src/unistd/testdata/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
@@ -25,4 +25,3 @@ set(shebangless_file
 file(WRITE ${shebangless_file} "exit 42\n")
 file(CHMOD ${shebangless_file}
   PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
-

>From 9eeca75a9924bbca7b6bff63e93d25468642674a Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 11 Nov 2026 21:15:25 +0530
Subject: [PATCH 3/3] [libc] Refine execvpe const char* path handling and avoid
 duplicate execve on ENOEXEC

---
 libc/src/unistd/linux/exec_utils.h | 74 +++++++++++++++---------------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git a/libc/src/unistd/linux/exec_utils.h b/libc/src/unistd/linux/exec_utils.h
index 942efa8f44b012..0d395891b45d04 100644
--- a/libc/src/unistd/linux/exec_utils.h
+++ b/libc/src/unistd/linux/exec_utils.h
@@ -63,53 +63,52 @@ LIBC_INLINE ErrorOr<int> exec_with_arg_list(const char *arg0, va_list *args,
   return exec(argv);
 }
 
-LIBC_INLINE ErrorOr<int> execve_with_shell_fallback(const char *path,
-                                                    char *const argv[],
-                                                    char *const envp[]) {
-  auto ret = linux_syscalls::execve(path, argv, envp);
-  if (!ret && ret.error() == ENOEXEC) {
-    size_t argc = 0;
-    if (argv != nullptr) {
-      while (argv[argc] != nullptr)
-        ++argc;
-    }
-    size_t count = (argc > 0) ? argc : 1;
+LIBC_INLINE ErrorOr<int> execve_shell_fallback(const char *path,
+                                               char *const argv[],
+                                               char *const envp[]) {
+  size_t argc = 0;
+  if (argv != nullptr) {
+    while (argv[argc] != nullptr)
+      ++argc;
+  }
+  size_t count = (argc > 0) ? argc : 1;
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wvla"
 #if __has_warning("-Wvla-cxx-extension")
 #pragma GCC diagnostic ignored "-Wvla-cxx-extension"
 #endif
-    char *new_argv[count + 2];
+  char *new_argv[count + 2];
 #pragma GCC diagnostic pop
-    new_argv[0] = const_cast<char *>("sh");
-    new_argv[1] = const_cast<char *>(path);
-    if (argv != nullptr && argc > 1) {
-      for (size_t i = 1; i < argc; ++i)
-        new_argv[i + 1] = argv[i];
-    }
-    new_argv[count + 1] = nullptr;
-
-    constexpr const char *DEFAULT_BSHELL = "/bin/sh";
-    return linux_syscalls::execve(DEFAULT_BSHELL, new_argv, envp);
+  new_argv[0] = const_cast<char *>("sh");
+  new_argv[1] = const_cast<char *>(path);
+  if (argv != nullptr && argc > 1) {
+    for (size_t i = 1; i < argc; ++i)
+      new_argv[i + 1] = argv[i];
   }
+  new_argv[count + 1] = nullptr;
+
+  constexpr const char *DEFAULT_BSHELL = "/bin/sh";
+  return linux_syscalls::execve(DEFAULT_BSHELL, new_argv, envp);
+}
+
+LIBC_INLINE ErrorOr<int> execve_with_shell_fallback(const char *path,
+                                                    char *const argv[],
+                                                    char *const envp[]) {
+  auto ret = linux_syscalls::execve(path, argv, envp);
+  if (!ret && ret.error() == ENOEXEC)
+    return execve_shell_fallback(path, argv, envp);
   return ret;
 }
 
-LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
+LIBC_INLINE ErrorOr<int> execvpe(const char *file, char *const argv[],
                                  char *const envp[]) {
-  if (file.empty())
+  cpp::string_view file_view(file);
+  if (file_view.empty())
     return Error(ENOENT);
 
-  char buf[PATH_MAX];
-
   // If there's a / in file, treat it as a path.
-  if (file.find_first_of('/') != cpp::string_view::npos) {
-    if (file.size() >= PATH_MAX)
-      return Error(ENAMETOOLONG);
-    inline_memcpy(buf, file.data(), file.size());
-    buf[file.size()] = '\0';
-    return execve_with_shell_fallback(buf, argv, envp);
-  }
+  if (file_view.find_first_of('/') != cpp::string_view::npos)
+    return execve_with_shell_fallback(file, argv, envp);
 
   // Otherwise treat file as the name of a file in a directory, search the
   // directories listed in PATH.
@@ -123,6 +122,7 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
   cpp::string_view path_env =
       env_path != nullptr ? cpp::string_view(env_path) : DEFAULT_PATH;
 
+  char buf[PATH_MAX];
   bool seen_eacces = false;
   int last_error = ENOENT;
 
@@ -133,15 +133,15 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
     if (dir.empty())
       dir = ".";
 
-    if (dir.size() + 1 + file.size() >= PATH_MAX) {
+    if (dir.size() + 1 + file_view.size() >= PATH_MAX) {
       last_error = ENAMETOOLONG;
       continue;
     }
 
     inline_memcpy(buf, dir.data(), dir.size());
     buf[dir.size()] = '/';
-    inline_memcpy(buf + dir.size() + 1, file.data(), file.size());
-    buf[dir.size() + 1 + file.size()] = '\0';
+    inline_memcpy(buf + dir.size() + 1, file_view.data(), file_view.size());
+    buf[dir.size() + 1 + file_view.size()] = '\0';
 
     auto ret = linux_syscalls::execve(buf, argv, envp);
     if (ret)
@@ -156,7 +156,7 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
       last_error = err;
       break;
     case ENOEXEC:
-      return execve_with_shell_fallback(buf, argv, envp);
+      return execve_shell_fallback(buf, argv, envp);
     default:
       return Error(err);
     }



More information about the libc-commits mailing list