This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit cb2220f2a8e90496e1fc991ec6259c20ee6578df
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 21:44:03 2026 +0100

    binfmt/exec_module: Fix the error path after nxtask_init().
    
    An error after nxtask_init() (addrenv_attach(), addrenv_restore() or
    spawn_execattrs()) goes to errout_with_tcbinit.  After nxtask_uninit(),
    that path goes on into the error path of the steps before nxtask_init():
    
    - It releases the argument list, the environment list and the file
      actions again.  exec_module() releases them after nxtask_init().  In a
      kernel build the three lists are copies in the kernel heap, so the
      kernel heap frees them twice.
    - spawn_execattrs() runs after addrenv_restore().  After its error the
      path calls addrenv_restore() a second time, which gives back a
      reference to the address environment of the caller.  And
      nxtask_uninit() frees the user stack of the new task with kumm_free()
      while the address environment of the caller is selected: the stack
      lies in the user heap of the new process, so the free goes into the
      heap of the caller ("Assertion failed" in mm_heap/mm_malloc_size.c, or
      a broken heap of the caller).
    
    posix_spawn() takes this path with POSIX_SPAWN_SETSCHEDPARAM and a
    priority that nxsched_set_param() refuses (for example 0), and with
    POSIX_SPAWN_SETSCHEDULER and a policy that is not configured.
    
    Now spawn_execattrs() runs before addrenv_restore(), with a copy of the
    attributes that exec_module() makes in the address environment of the
    caller.  So every error after nxtask_init() comes while the address
    environment of the new task is selected: nxtask_uninit() releases the
    task there, and the path goes on to the one addrenv_restore() of
    errout_with_addrenv.  The list pointers are NULL after their release,
    so the labels after it do not release them again.  addrenv_restore()
    after spawn_execattrs() is not checked any more: addrenv_switch()
    returns OK on every path, as the other calls of this file assume.
    
    Builds without CONFIG_BUILD_KERNEL do not copy the lists (only a new
    list for the arguments of a loader, which was not freed twice) and do
    not select the address environment of the new task.  There the order of
    spawn_execattrs() does not matter.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 binfmt/binfmt_execmodule.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c
index b9f48bcee67..ca043a4ce70 100644
--- a/binfmt/binfmt_execmodule.c
+++ b/binfmt/binfmt_execmodule.c
@@ -208,6 +208,7 @@ int exec_module(FAR struct binary_s *binp,
   FAR struct arch_addrenv_s *addrenv = &binp->addrenv->addrenv;
   FAR void *vheap;
   char name[PATH_MAX];
+  posix_spawnattr_t kattr;
 #endif
   FAR void *stackaddr = NULL;
 #if defined(CONFIG_BINFMT_LOADABLE) && defined(CONFIG_BUILD_FLAT)
@@ -269,6 +270,16 @@ int exec_module(FAR struct binary_s *binp,
       filename = name;
     }
 
+  /* spawn_execattrs() runs in the address environment of the new task:
+   * copy the attributes of the caller.
+   */
+
+  if (attr != NULL)
+    {
+      kattr = *attr;
+      attr  = &kattr;
+    }
+
   /* Instantiate the address environment containing the user heap */
 
   ret = addrenv_select(binp->addrenv, &binp->oldenv);
@@ -326,6 +337,9 @@ int exec_module(FAR struct binary_s *binp,
   binfmt_freeactions(actions);
   binfmt_freeargv(argv, binp->argv);
   binfmt_freeenv(envp);
+  actions = NULL;
+  argv    = NULL;
+  envp    = NULL;
 
   /* The task has its arguments: the list of the loader is not needed */
 
@@ -391,17 +405,6 @@ int exec_module(FAR struct binary_s *binp,
 
   pid = tcb->pid;
 
-#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
-  /* Restore the address environment of the caller */
-
-  ret = addrenv_restore(binp->oldenv);
-  if (ret < 0)
-    {
-      berr("ERROR: addrenv_restore() failed: %d\n", ret);
-      goto errout_with_tcbinit;
-    }
-#endif
-
   /* Set the attributes */
 
   if (attr)
@@ -413,6 +416,12 @@ int exec_module(FAR struct binary_s *binp,
         }
     }
 
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
+  /* Restore the address environment of the caller */
+
+  addrenv_restore(binp->oldenv);
+#endif
+
 #ifdef CONFIG_BINFMT_LOADABLE
   tcb->group->tg_bininfo = binp;
 #endif
@@ -443,6 +452,10 @@ errout_with_tcbinit:
     }
 #endif
 
+  /* nxtask_uninit() releases the TCB and its address environment.  The
+   * copied lists are already released.
+   */
+
   nxtask_uninit(tcb);
   tcb = NULL;
 

Reply via email to