Abhishekmishra2808 commented on code in PR #19900:
URL: https://github.com/apache/nuttx/pull/19900#discussion_r3837960178


##########
sched/group/group_create.c:
##########
@@ -98,6 +98,49 @@ 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.
+ *
+ ****************************************************************************/
+
+static int group_inherit_chroot(FAR struct task_group_s *group)
+{
+  FAR struct tcb_s *rtcb          = this_task();
+  FAR struct task_group_s *rgroup = rtcb->group;
+
+  DEBUGASSERT(group != NULL && rgroup != NULL);
+
+  if (rgroup->tg_root == NULL)
+    {
+      return OK;
+    }
+
+  group->tg_root = rgroup->tg_root;

Review Comment:
   Fixed



##########
sched/group/group_create.c:
##########
@@ -98,6 +98,49 @@ 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.
+ *
+ ****************************************************************************/
+
+static int group_inherit_chroot(FAR struct task_group_s *group)
+{
+  FAR struct tcb_s *rtcb          = this_task();
+  FAR struct task_group_s *rgroup = rtcb->group;
+
+  DEBUGASSERT(group != NULL && rgroup != NULL);
+
+  if (rgroup->tg_root == NULL)
+    {
+      return OK;
+    }
+
+  group->tg_root = rgroup->tg_root;
+  inode_addref(group->tg_root);
+
+  if (rgroup->tg_rootrel != NULL)
+    {
+      size_t len = strlen(rgroup->tg_rootrel) + 1;
+
+      group->tg_rootrel = kmm_malloc(len);

Review Comment:
   Fixed



##########
sched/group/group_create.c:
##########
@@ -98,6 +98,49 @@ 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.
+ *
+ ****************************************************************************/
+
+static int group_inherit_chroot(FAR struct task_group_s *group)
+{
+  FAR struct tcb_s *rtcb          = this_task();
+  FAR struct task_group_s *rgroup = rtcb->group;
+
+  DEBUGASSERT(group != NULL && rgroup != NULL);
+
+  if (rgroup->tg_root == NULL)
+    {
+      return OK;
+    }
+
+  group->tg_root = rgroup->tg_root;

Review Comment:
   Fixed



##########
sched/group/group_create.c:
##########
@@ -190,6 +233,19 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
 
   group_inherit_identity(group);
 
+#ifdef CONFIG_FS_CHROOT
+  /* Kernel threads share g_kthread_group and must not inherit a user jail */
+
+  if (ttype != TCB_FLAG_TTYPE_KERNEL)

Review Comment:
   Fixed



##########
sched/group/group_create.c:
##########
@@ -190,6 +233,19 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
 
   group_inherit_identity(group);
 
+#ifdef CONFIG_FS_CHROOT

Review Comment:
   Fixed



##########
fs/vfs/fs_chroot.c:
##########
@@ -0,0 +1,263 @@
+/****************************************************************************
+ * fs/vfs/fs_chroot.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: chroot_strip_slash
+ ****************************************************************************/
+
+static void chroot_strip_slash(FAR char *path)
+{
+  size_t len;
+
+  if (path == NULL)
+    {
+      return;
+    }
+
+  len = strlen(path);
+  while (len > 1 && path[len - 1] == '/')
+    {
+      path[--len] = '\0';
+    }
+}
+
+/****************************************************************************
+ * Name: chroot_set_pwd
+ *
+ * Description:
+ *   Rewrite PWD so relative lookups stay inside the jail.  If the current
+ *   directory is not under the new root, PWD becomes "/".
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_ENVIRON
+static void chroot_set_pwd(FAR struct inode *root, FAR const char *relpath)
+{
+  char rootpath[PATH_MAX];
+  FAR const char *pwd;
+  size_t rootlen;
+
+  rootpath[0] = '\0';
+  if (root != NULL)
+    {
+      if (inode_getpath(root, rootpath, sizeof(rootpath)) < 0)
+        {
+          setenv("PWD", "/", TRUE);
+          return;
+        }
+    }
+  else
+    {
+      strlcpy(rootpath, "/", sizeof(rootpath));
+    }
+
+  if (relpath != NULL && relpath[0] != '\0')
+    {
+      chroot_strip_slash(rootpath);

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to