Abhishekmishra2808 commented on code in PR #19900:
URL: https://github.com/apache/nuttx/pull/19900#discussion_r3968483568
##########
fs/inode/fs_inodesearch.c:
##########
@@ -193,6 +199,120 @@ static int _inode_linktarget(FAR struct inode *inode,
}
#endif
+#ifdef CONFIG_FS_CHROOT
+/****************************************************************************
+ * Name: inode_get_chroot
+ ****************************************************************************/
+
+static FAR struct inode *inode_get_chroot(FAR const char **relpath)
+{
+ FAR struct tcb_s *tcb = nxsched_self();
+
+ if (relpath != NULL)
+ {
+ *relpath = NULL;
+ }
+
+ if (tcb != NULL && tcb->group != NULL && tcb->group->tg_root != NULL)
+ {
+ if (relpath != NULL)
+ {
+ *relpath = tcb->group->tg_rootrel;
+ }
+
+ return tcb->group->tg_root;
+ }
+
+ return g_root_inode;
+}
+
+/****************************************************************************
+ * Name: inode_normalize_abs
+ *
+ * Description:
+ * Normalize an absolute path: drop empty and "." segments, and clamp
+ * ".." at the search root. Needed even before a jail is installed:
+ * chroot(".") becomes "$PWD/.", and if PWD sits under a mountpoint
+ * (tmpfs /tmp) the leftover "." is passed to the filesystem as
+ * relpath and fails with ENOENT.
+ *
+ ****************************************************************************/
+
+static int inode_normalize_abs(FAR const char *in, FAR char *out,
+ size_t outlen)
+{
+ FAR char *dst;
+ FAR const char *src = in;
+
+ if (outlen < 2)
+ {
+ return -ENAMETOOLONG;
+ }
+
+ out[0] = '/';
+ dst = out + 1;
+
+ while (*src == '/')
+ {
+ src++;
+ }
+
+ while (*src != '\0')
+ {
+ FAR const char *end = src;
+ size_t seglen;
+
+ while (*end != '\0' && *end != '/')
Review Comment:
Implemented as you suggested.
##########
include/nuttx/sched.h:
##########
@@ -558,6 +558,13 @@ struct task_group_s
struct fdlist tg_fdlist; /* Maps file descriptor to file */
+#ifdef CONFIG_FS_CHROOT
+ /* chroot() jail **********************************************************/
+
+ FAR struct inode *tg_root; /* NULL means global pseudo-root */
+ FAR char *tg_rootrel; /* Relpath prefix if tg_root is a mount */
Review Comment:
Implemented as you suggested.
##########
fs/inode/fs_inodesearch.c:
##########
@@ -221,6 +341,11 @@ static int _inode_search(FAR struct inode_search_s *desc)
FAR struct inode *left = NULL;
FAR struct inode *above = NULL;
FAR const char *relpath = NULL;
+#ifdef CONFIG_FS_CHROOT
+ FAR struct inode *search_root = g_root_inode;
Review Comment:
Implemented as you suggested.
##########
sched/group/group_create.c:
##########
@@ -98,6 +98,62 @@ 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.
+ *
+ * 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;
+ }
+
+ if (rgroup->tg_rootrel != NULL)
+ {
+ group->tg_rootrel = strdup(rgroup->tg_rootrel);
+ if (group->tg_rootrel == NULL)
+ {
+ return -ENOMEM;
+ }
+ }
+
+ group->tg_root = rgroup->tg_root;
Review Comment:
Fixed in latest push.
##########
sched/group/group_create.c:
##########
@@ -98,6 +98,62 @@ 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.
+ *
+ * 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;
+ }
+
+ if (rgroup->tg_rootrel != NULL)
Review Comment:
Fixed in latest push.
##########
fs/driver/fs_findblockdriver.c:
##########
@@ -77,7 +77,12 @@ int find_blockdriver(FAR const char *pathname, int
mountflags,
/* Find the inode registered with this pathname */
- SETUP_SEARCH(&desc, pathname, false);
+ ret = inode_search_setup(&desc, pathname, false);
+ if (ret < 0)
+ {
+ inode_search_release(&desc);
Review Comment:
Fixed in latest push.
##########
fs/driver/fs_finddriver.c:
##########
@@ -51,12 +51,17 @@ FAR void *find_driver(FAR const char *pathname)
{
struct inode_search_s desc;
FAR void *drvr = NULL;
+ int ret;
DEBUGASSERT(pathname != NULL);
/* Find the inode registered with this pathname */
- SETUP_SEARCH(&desc, pathname, false);
+ ret = inode_search_setup(&desc, pathname, false);
Review Comment:
Fixed in latest push.
##########
fs/driver/fs_findmtddriver.c:
##########
@@ -70,7 +70,12 @@ int find_mtddriver(FAR const char *pathname, FAR struct
inode **ppinode)
/* Find the inode registered with this pathname */
- SETUP_SEARCH(&desc, pathname, false);
+ ret = inode_search_setup(&desc, pathname, false);
+ if (ret < 0)
+ {
+ inode_search_release(&desc);
Review Comment:
Fixed in latest push.
--
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]