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 6c7f604f79d7f1f7dc6e8b14dd6636db3fd02219
Author: Abhishek Mishra <[email protected]>
AuthorDate: Wed Aug 19 08:07:17 2026 +0000

    sched: add per-group filesystem jail root
    
    Store the jail as an absolute path on the task group, copy it to
    children, and free it when the last member leaves.
    
    Signed-off-by: Abhishek Mishra <[email protected]>
---
 include/nuttx/sched.h      |  6 ++++
 sched/group/group_create.c | 70 +++++++++++++++++++++++++++++++++++++++++++++-
 sched/group/group_leave.c  | 15 ++++++++++
 3 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 2c89ebf83fa..390bb00a99a 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -558,6 +558,12 @@ struct task_group_s
 
   struct fdlist tg_fdlist;          /* Maps file descriptor to file         */
 
+#ifdef CONFIG_FS_CHROOT
+  /* chroot() jail **********************************************************/
+
+  FAR char *tg_root;                /* Absolute jail path, NULL = no jail   */
+#endif
+
   /* Virtual memory mapping info ********************************************/
 
   struct mm_map_s tg_mm_map;        /* Task group virtual memory mappings   */
diff --git a/sched/group/group_create.c b/sched/group/group_create.c
index 1fb32bf7ff2..ed23dfc966b 100644
--- a/sched/group/group_create.c
+++ b/sched/group/group_create.c
@@ -33,7 +33,6 @@
 
 #include <nuttx/debug.h>
 #include <nuttx/irq.h>
-#include <nuttx/fs/fs.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/sched.h>
@@ -42,6 +41,10 @@
 #include "group/group.h"
 #include "tls/tls.h"
 
+#ifdef CONFIG_FS_CHROOT
+#  include "../../fs/fs_heap.h"
+#endif
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -98,6 +101,57 @@ static inline void group_inherit_identity(FAR struct 
task_group_s *group)
 #  define group_inherit_identity(group)
 #endif
 
+#ifdef CONFIG_FS_CHROOT
+/****************************************************************************
+ * Name: group_inherit_chroot
+ *
+ * Description:
+ *   Inherit the chroot jail from the parent task group.  Kernel threads
+ *   share g_kthread_group and must not inherit a user jail.
+ *   CONFIG_FS_CHROOT is selected in fs/Kconfig.
+ *
+ * Input Parameters:
+ *   group - The new task group.
+ *   ttype - The type of the new thread (TCB_FLAG_TTYPE_* value).
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int group_inherit_chroot(FAR struct task_group_s *group,
+                                 uint8_t ttype)
+{
+  FAR struct tcb_s *rtcb;
+  FAR struct task_group_s *rgroup;
+
+  if (ttype == TCB_FLAG_TTYPE_KERNEL)
+    {
+      return OK;
+    }
+
+  rtcb   = this_task();
+  rgroup = rtcb->group;
+
+  DEBUGASSERT(group != NULL && rgroup != NULL);
+
+  if (rgroup->tg_root == NULL)
+    {
+      return OK;
+    }
+
+  group->tg_root = fs_heap_strdup(rgroup->tg_root);
+  if (group->tg_root == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  return OK;
+}
+#else
+#  define group_inherit_chroot(group, ttype) (0)
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -190,6 +244,12 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
 
   group_inherit_identity(group);
 
+  ret = group_inherit_chroot(group, ttype);
+  if (ret < 0)
+    {
+      goto errout_with_group;
+    }
+
   /* Initialize file descriptors for the TCB */
 
   fdlist_init(&group->tg_fdlist);
@@ -219,6 +279,14 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
   return OK;
 
 errout_with_group:
+#ifdef CONFIG_FS_CHROOT
+  if (group->tg_root != NULL)
+    {
+      fs_heap_free(group->tg_root);
+      group->tg_root = NULL;
+    }
+#endif
+
   kmm_free(group);
   return ret;
 }
diff --git a/sched/group/group_leave.c b/sched/group/group_leave.c
index afe62da8c07..466f44be481 100644
--- a/sched/group/group_leave.c
+++ b/sched/group/group_leave.c
@@ -33,6 +33,7 @@
 #include <nuttx/debug.h>
 #include <nuttx/irq.h>
 #include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
 #include <nuttx/net/net.h>
 #include <nuttx/sched.h>
 #include <nuttx/spinlock.h>
@@ -48,6 +49,10 @@
 #include "group/group.h"
 #include "tls/tls.h"
 
+#ifdef CONFIG_FS_CHROOT
+#  include "../../fs/fs_heap.h"
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -104,6 +109,16 @@ static inline void group_release(FAR struct task_group_s 
*group)
 
   fdlist_free(&group->tg_fdlist);
 
+#ifdef CONFIG_FS_CHROOT
+  /* Drop the chroot jail path */
+
+  if (group->tg_root != NULL)
+    {
+      fs_heap_free(group->tg_root);
+      group->tg_root = NULL;
+    }
+#endif
+
   /* Release all shared environment variables */
 
   env_release(group);

Reply via email to