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

via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 13:40:05 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Aman Maurya (amanmaurya92)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/240040.diff


3 Files Affected:

- (modified) libc/src/unistd/linux/exec_utils.h (+43-7) 
- (modified) libc/test/integration/src/unistd/execlp_test.cpp (+37) 
- (modified) libc/test/integration/src/unistd/testdata/CMakeLists.txt (+8) 


``````````diff
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)
+

``````````

</details>


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


More information about the libc-commits mailing list