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

via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 03:52:03 PST 2026


Author: Aman Maurya
Date: 2026-11-12T17:21:58+05:30
New Revision: f3980a1d478ec0a0e117237ea0ba9cc3575cdab6

URL: https://github.com/llvm/llvm-project/commit/f3980a1d478ec0a0e117237ea0ba9cc3575cdab6
DIFF: https://github.com/llvm/llvm-project/commit/f3980a1d478ec0a0e117237ea0ba9cc3575cdab6.diff

LOG: [libc] Support execution of scripts without shebangs in execvpe (#240040)

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.

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

Fixes #232374.

Added: 
    

Modified: 
    libc/src/unistd/linux/exec_utils.h
    libc/test/integration/src/unistd/execlp_test.cpp
    libc/test/integration/src/unistd/testdata/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/src/unistd/linux/exec_utils.h b/libc/src/unistd/linux/exec_utils.h
index 8260027a1a1d0d..944eb3d758200e 100644
--- a/libc/src/unistd/linux/exec_utils.h
+++ b/libc/src/unistd/linux/exec_utils.h
@@ -63,18 +63,47 @@ 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> execvpe(cpp::string_view file, char *const argv[],
+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];
+#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);
+}
+
+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);
 
   // 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_view.find_first_of('/') != cpp::string_view::npos) {
+    auto ret = linux_syscalls::execve(file, argv, envp);
+    if (ret || ret.error() != ENOEXEC)
+      return ret;
+    return execve_shell_fallback(file, argv, envp);
+  }
 
   // Otherwise treat file as the name of a file in a directory, search the
   // directories listed in PATH.
@@ -99,15 +128,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)
@@ -121,6 +150,8 @@ LIBC_INLINE ErrorOr<int> execvpe(cpp::string_view file, char *const argv[],
     case ENOTDIR:
       last_error = err;
       break;
+    case ENOEXEC:
+      return execve_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..d8a4bbbb7354cf 100644
--- a/libc/test/integration/src/unistd/execlp_test.cpp
+++ b/libc/test/integration/src/unistd/execlp_test.cpp
@@ -130,6 +130,59 @@ 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);
+}
+
+void fork_and_execlp_shebangless_script_with_args() {
+  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", "17",
+                           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), 17);
+}
+
 TEST_MAIN([[maybe_unused]] int argc, [[maybe_unused]] char **argv,
           [[maybe_unused]] char **envp) {
   fork_and_execlp_slash_path();
@@ -139,5 +192,8 @@ 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();
+  fork_and_execlp_shebangless_script_with_args();
   return 0;
 }

diff  --git a/libc/test/integration/src/unistd/testdata/CMakeLists.txt b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
index 3fcce1d8856692..9e5cb9930afdbb 100644
--- a/libc/test/integration/src/unistd/testdata/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/testdata/CMakeLists.txt
@@ -18,3 +18,10 @@ 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} "if [ -n \"\${1:-}\" ]; then exit \"\$1\"; fi\nexit 42\n")
+file(CHMOD ${shebangless_file}
+  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)


        


More information about the libc-commits mailing list