[libc-commits] [libc] [libc] Copy the path in posix_spawn_file_actions_addopen (PR #239224)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Nov 5 05:16:16 PST 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/239224

POSIX says "The string described by path shall be copied by the posix_spawn_file_actions_addopen() function", but we only stored the pointer. If the caller reused or freed the buffer before calling posix_spawn, the child would open whatever happened to be there at that point. Our tests didn't notice because they only ever passed string literals. Now they overwrite the buffer right after the call.

I'm allocating the path together with the action, which now ends with a flexible array member. make_unique can't do variable-sized allocations, so SpawnFileOpenAction gets a create() function, which works like make_unique, but uses a class-specific operator new that takes the size of the path. This operator new also hides the global one, so it's not possible to (accidentally) allocate an action without room for the path. The matching operator delete is unsized, because the allocation is bigger than sizeof(SpawnFileOpenAction). It lives in the base class, so all actions share it.

While in there:
- switch posix_spawn_file_actions_add{close,dup2} to make_unique too
- make add_action take a unique_ptr, so that the ownership transfer is visible in its signature. It also checks that the action is trivially destructible, which lets posix_spawn_file_actions_destroy free the actions through a BaseSpawnFileAction pointer, instead of switching on the action type (a delete expression would need a virtual destructor for that).
- crash on a null path (when LIBC_ADD_NULL_CHECKS is enabled) instead of deferring the failure to the open call in the child.

Assisted-by: Claude

>From d52e59fbff55de7c4e0aea302c773c67b5033b75 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Wed, 4 Nov 2026 17:39:45 +0000
Subject: [PATCH] [libc] Copy the path in posix_spawn_file_actions_addopen

POSIX says "The string described by path shall be copied by the
posix_spawn_file_actions_addopen() function", but we only stored the
pointer. If the caller reused or freed the buffer before calling
posix_spawn, the child would open whatever happened to be there at that
point. Our tests didn't notice because they only ever passed string
literals. Now they overwrite the buffer right after the call.

I'm allocating the path together with the action, which now ends with a
flexible array member. make_unique can't do variable-sized allocations,
so SpawnFileOpenAction gets a create() function, which works like
make_unique, but uses a class-specific operator new that takes the size
of the path. This operator new also hides the global one, so it's not
possible to (accidentally) allocate an action without room for the path.
The matching operator delete is unsized, because the allocation is bigger
than sizeof(SpawnFileOpenAction). It lives in the base class, so all actions
share it.

While in there:
- switch posix_spawn_file_actions_add{close,dup2} to make_unique too
- make add_action take a unique_ptr, so that the ownership transfer is
  visible in its signature. It also checks that the action is trivially
  destructible, which lets posix_spawn_file_actions_destroy free the
  actions through a BaseSpawnFileAction pointer, instead of switching on
  the action type (a delete expression would need a virtual destructor
  for that).
- crash on a null path (when LIBC_ADD_NULL_CHECKS is enabled) instead
  of deferring the failure to the open call in the child.

Assisted-by: Claude
---
 libc/src/spawn/CMakeLists.txt                 | 16 +++--
 libc/src/spawn/file_actions.h                 | 68 ++++++++++++++++---
 .../posix_spawn_file_actions_addclose.cpp     | 10 ++-
 .../posix_spawn_file_actions_adddup2.cpp      | 10 ++-
 .../posix_spawn_file_actions_addopen.cpp      | 11 ++-
 .../posix_spawn_file_actions_destroy.cpp      | 16 ++---
 .../src/spawn/posix_spawn_test.cpp            |  8 ++-
 .../spawn/posix_spawn_file_actions_test.cpp   | 27 +++++++-
 8 files changed, 119 insertions(+), 47 deletions(-)

diff --git a/libc/src/spawn/CMakeLists.txt b/libc/src/spawn/CMakeLists.txt
index 230b920108d3b4..8ca29077e3ddb2 100644
--- a/libc/src/spawn/CMakeLists.txt
+++ b/libc/src/spawn/CMakeLists.txt
@@ -8,7 +8,16 @@ add_header_library(
     file_actions.h
   DEPENDS
     libc.hdr.stdint_proxy
+    libc.hdr.types.size_t
     libc.include.spawn
+    libc.src.__support.alloc_checker
+    libc.src.__support.checked_ptr
+    libc.src.__support.CPP.new
+    libc.src.__support.CPP.type_traits
+    libc.src.__support.CPP.unique_ptr
+    libc.src.__support.macros.attributes
+    libc.src.string.memory_utils.inline_memcpy
+    libc.src.string.string_utils
 )
 
 add_entrypoint_object(
@@ -31,7 +40,6 @@ add_entrypoint_object(
     .file_actions
     libc.hdr.errno_macros
     libc.include.spawn
-    libc.src.__support.CPP.new
 )
 
 add_entrypoint_object(
@@ -44,7 +52,7 @@ add_entrypoint_object(
     .file_actions
     libc.hdr.errno_macros
     libc.include.spawn
-    libc.src.__support.CPP.new
+    libc.src.__support.checked_ptr
 )
 
 add_entrypoint_object(
@@ -57,7 +65,7 @@ add_entrypoint_object(
     .file_actions
     libc.hdr.errno_macros
     libc.include.spawn
-    libc.src.__support.CPP.new
+    libc.src.__support.macros.null_check
 )
 
 add_entrypoint_object(
@@ -70,7 +78,7 @@ add_entrypoint_object(
     .file_actions
     libc.hdr.errno_macros
     libc.include.spawn
-    libc.src.__support.CPP.new
+    libc.src.__support.checked_ptr
 )
 
 add_entrypoint_object(
diff --git a/libc/src/spawn/file_actions.h b/libc/src/spawn/file_actions.h
index c69640a3ddd0d0..fc158e152c09d5 100644
--- a/libc/src/spawn/file_actions.h
+++ b/libc/src/spawn/file_actions.h
@@ -10,7 +10,16 @@
 #define LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H
 
 #include "hdr/stdint_proxy.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/CPP/unique_ptr.h"
+#include "src/__support/alloc-checker.h"
+#include "src/__support/checked_ptr.h"
+#include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/string/string_utils.h"
 #include <spawn.h> // For mode_t
 
 namespace LIBC_NAMESPACE_DECL {
@@ -25,15 +34,29 @@ struct BaseSpawnFileAction {
   ActionType type;
   BaseSpawnFileAction *next;
 
-  static void add_action(posix_spawn_file_actions_t *actions,
-                         BaseSpawnFileAction *act) {
+  /// Frees memory allocated for any of the actions. This is deliberately not
+  /// the sized version, as SpawnFileOpenAction allocations are larger than the
+  /// type.
+  LIBC_INLINE static void operator delete(void *ptr) noexcept {
+    ::operator delete(ptr);
+  }
+
+  /// Appends `act` to `actions`, which takes ownership of it.
+  template <typename T>
+  LIBC_INLINE static void add_action(posix_spawn_file_actions_t *actions,
+                                     cpp::unique_ptr<T> act) {
+    // posix_spawn_file_actions_destroy frees the actions through a
+    // BaseSpawnFileAction pointer, without running their destructors.
+    static_assert(cpp::is_trivially_destructible_v<T>,
+                  "Spawn file actions must be trivially destructible");
+    BaseSpawnFileAction *new_act = act.release();
     if (actions->__back != nullptr) {
       auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back);
-      back->next = act;
-      actions->__back = act;
+      back->next = new_act;
+      actions->__back = new_act;
     } else {
       // First action is being added.
-      actions->__front = actions->__back = act;
+      actions->__front = actions->__back = new_act;
     }
   }
 
@@ -42,14 +65,41 @@ struct BaseSpawnFileAction {
 };
 
 struct SpawnFileOpenAction : public BaseSpawnFileAction {
-  const char *path;
   int fd;
   int oflag;
   mode_t mode;
+  // A copy of the path passed to posix_spawn_file_actions_addopen. It is
+  // allocated together with the action (see create()).
+  char path[];
+
+  /// Allocates an open action, together with a copy of `p`. Returns a null
+  /// CheckedPtr if the allocation fails.
+  LIBC_INLINE static CheckedPtr<SpawnFileOpenAction>
+  create(const char *p, int fdesc, int flags, mode_t m) {
+    size_t path_size = internal::string_length(p) + 1;
+    // As in make_unique, there is no need to inspect `ac`: operator new is
+    // noexcept, so if it fails, no object is constructed and the
+    // new-expression evaluates to nullptr.
+    AllocChecker ac;
+    return CheckedPtr<SpawnFileOpenAction>(
+        new (ac, path_size) SpawnFileOpenAction(p, path_size, fdesc, flags, m));
+  }
 
-  SpawnFileOpenAction(const char *p, int fdesc, int flags, mode_t m)
-      : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), path(p), fd(fdesc),
-        oflag(flags), mode(m) {}
+  /// Allocates storage for the object, followed by `path_size` bytes for the
+  /// path. Declaring this also hides the global operator new, which would not
+  /// leave room for the path.
+  LIBC_INLINE static void *operator new(size_t size, AllocChecker &ac,
+                                        size_t path_size) noexcept {
+    return ::operator new(size + path_size, ac);
+  }
+
+private:
+  LIBC_INLINE SpawnFileOpenAction(const char *p, size_t path_size, int fdesc,
+                                  int flags, mode_t m)
+      : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), fd(fdesc), oflag(flags),
+        mode(m) {
+    inline_memcpy(path, p, path_size);
+  }
 };
 
 struct SpawnFileCloseAction : public BaseSpawnFileAction {
diff --git a/libc/src/spawn/posix_spawn_file_actions_addclose.cpp b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
index f6a346d92f2c4e..baf063df766e39 100644
--- a/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp
@@ -10,8 +10,7 @@
 
 #include "file_actions.h"
 #include "hdr/errno_macros.h"
-#include "src/__support/CPP/new.h"
-#include "src/__support/alloc-checker.h"
+#include "src/__support/checked_ptr.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
@@ -26,11 +25,10 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addclose,
   if (fd < 0)
     return EBADF;
 
-  AllocChecker ac;
-  auto *act = new (ac) SpawnFileCloseAction(fd);
-  if (!ac)
+  auto act = make_unique<SpawnFileCloseAction>(fd);
+  if (!act)
     return ENOMEM;
-  BaseSpawnFileAction::add_action(actions, act);
+  BaseSpawnFileAction::add_action(actions, act.take_unique());
 
   return 0;
 }
diff --git a/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
index cbf4889f2c2dbd..2a27fa57c50151 100644
--- a/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp
@@ -10,8 +10,7 @@
 
 #include "file_actions.h"
 #include "hdr/errno_macros.h"
-#include "src/__support/CPP/new.h"
-#include "src/__support/alloc-checker.h"
+#include "src/__support/checked_ptr.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
@@ -26,11 +25,10 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_adddup2,
   if (fd < 0 || newfd < 0)
     return EBADF;
 
-  AllocChecker ac;
-  auto *act = new (ac) SpawnFileDup2Action(fd, newfd);
-  if (!ac)
+  auto act = make_unique<SpawnFileDup2Action>(fd, newfd);
+  if (!act)
     return ENOMEM;
-  BaseSpawnFileAction::add_action(actions, act);
+  BaseSpawnFileAction::add_action(actions, act.take_unique());
 
   return 0;
 }
diff --git a/libc/src/spawn/posix_spawn_file_actions_addopen.cpp b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
index 064de976320baa..7ee90192d310ab 100644
--- a/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp
@@ -10,10 +10,9 @@
 
 #include "file_actions.h"
 #include "hdr/errno_macros.h"
-#include "src/__support/CPP/new.h"
-#include "src/__support/alloc-checker.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 
 #include <spawn.h>
 
@@ -26,12 +25,12 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addopen,
     return EINVAL;
   if (fd < 0)
     return EBADF;
+  LIBC_CRASH_ON_NULLPTR(path);
 
-  AllocChecker ac;
-  auto *act = new (ac) SpawnFileOpenAction(path, fd, oflag, mode);
-  if (act == nullptr)
+  auto act = SpawnFileOpenAction::create(path, fd, oflag, mode);
+  if (!act)
     return ENOMEM;
-  BaseSpawnFileAction::add_action(actions, act);
+  BaseSpawnFileAction::add_action(actions, act.take_unique());
 
   return 0;
 }
diff --git a/libc/src/spawn/posix_spawn_file_actions_destroy.cpp b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
index 4aebf7c52e5355..8ca9001d7ac7fc 100644
--- a/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
+++ b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp
@@ -11,7 +11,6 @@
 #include "file_actions.h"
 
 #include "hdr/errno_macros.h"
-#include "src/__support/CPP/new.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
@@ -35,17 +34,10 @@ LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_destroy,
   while (act != nullptr) {
     auto *temp = act;
     act = act->next;
-    switch (temp->type) {
-    case BaseSpawnFileAction::OPEN:
-      delete reinterpret_cast<SpawnFileOpenAction *>(temp);
-      break;
-    case BaseSpawnFileAction::CLOSE:
-      delete reinterpret_cast<SpawnFileCloseAction *>(temp);
-      break;
-    case BaseSpawnFileAction::DUP2:
-      delete reinterpret_cast<SpawnFileDup2Action *>(temp);
-      break;
-    }
+    // A delete expression would need a virtual destructor, but the actions are
+    // trivially destructible (add_action checks that), so we can just free the
+    // memory.
+    BaseSpawnFileAction::operator delete(temp);
   }
 
   return 0;
diff --git a/libc/test/integration/src/spawn/posix_spawn_test.cpp b/libc/test/integration/src/spawn/posix_spawn_test.cpp
index 9e10ef1d5ec15a..47938f6d7ac475 100644
--- a/libc/test/integration/src/spawn/posix_spawn_test.cpp
+++ b/libc/test/integration/src/spawn/posix_spawn_test.cpp
@@ -31,8 +31,12 @@ void spawn_and_wait_for_normal_exit(char **envp) {
   pid_t cpid;
   posix_spawn_file_actions_t file_actions;
   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&file_actions), 0);
-  LIBC_NAMESPACE::posix_spawn_file_actions_addopen(
-      &file_actions, CHILD_FD, "testdata/posix_spawn.test", O_RDONLY, 0);
+  // The path must be copied, so the buffer can be reused before posix_spawn.
+  char path[] = "testdata/posix_spawn.test";
+  ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(
+                &file_actions, CHILD_FD, path, O_RDONLY, 0),
+            0);
+  path[0] = '\0';
   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn(&cpid, arg0, &file_actions, nullptr,
                                         argv, envp),
             0);
diff --git a/libc/test/src/spawn/posix_spawn_file_actions_test.cpp b/libc/test/src/spawn/posix_spawn_file_actions_test.cpp
index 20ab312f1f999d..090733f9bf932a 100644
--- a/libc/test/src/spawn/posix_spawn_file_actions_test.cpp
+++ b/libc/test/src/spawn/posix_spawn_file_actions_test.cpp
@@ -31,9 +31,12 @@ TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
 
   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, 11, 12),
             0);
-  ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(
-                &actions, 13, "path/to/file", 0, 0),
+  // The path must be copied, so the caller can reuse the buffer right away.
+  char path[] = "path/to/file";
+  ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(&actions, 13, path,
+                                                             42, 0644),
             0);
+  path[0] = 'X';
 
   LIBC_NAMESPACE::BaseSpawnFileAction *act =
       reinterpret_cast<LIBC_NAMESPACE::BaseSpawnFileAction *>(actions.__front);
@@ -48,6 +51,11 @@ TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
     }
     if (action_count == 3) {
       ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::OPEN);
+      auto *open_act = static_cast<LIBC_NAMESPACE::SpawnFileOpenAction *>(act);
+      ASSERT_EQ(open_act->fd, 13);
+      ASSERT_EQ(open_act->oflag, 42);
+      ASSERT_EQ(open_act->mode, mode_t(0644));
+      ASSERT_STREQ(open_act->path, "path/to/file");
     }
     act = act->next;
   }
@@ -78,3 +86,18 @@ TEST(LlvmLibcPosixSpawnFileActionsTest, InvalidActions) {
             EBADF);
   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&actions), 0);
 }
+
+#if defined(LIBC_ADD_NULL_CHECKS)
+
+TEST(LlvmLibcPosixSpawnFileActionsTest, CrashOnNullPath) {
+  ASSERT_DEATH(
+      []() {
+        posix_spawn_file_actions_t actions;
+        LIBC_NAMESPACE::posix_spawn_file_actions_init(&actions);
+        LIBC_NAMESPACE::posix_spawn_file_actions_addopen(&actions, 1, nullptr,
+                                                         0, 0);
+      },
+      WITH_SIGNAL(-1));
+}
+
+#endif // defined(LIBC_ADD_NULL_CHECKS)



More information about the libc-commits mailing list