Abhishekmishra2808 commented on code in PR #19900:
URL: https://github.com/apache/nuttx/pull/19900#discussion_r3837997903
##########
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:
Should we copy into `out` in one pass without walking `in` again?
##########
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:
Store the jail as an absolute path and re-resolve it on lookup instead of
`tg_root` / `tg_rootrel`?
##########
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:
Prepend jail path, normalize, check still under root, then keep the original
walk?
--
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]