fs,sched: add chroot() filesystem jail - #19900
Conversation
chroot() holds a directory inode in the task group, so sched needs the existing refcount helpers instead of duplicating them. Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
|
Nice work @Abhishekmishra2808 ! |
|
| void fs_initialize(void); | ||
|
|
||
| /**************************************************************************** | ||
| * Name: inode_addref |
There was a problem hiding this comment.
do you need remove the prototype from the internal header file
There was a problem hiding this comment.
Dropped the public prototypes; they stay in fs/inode/inode.h
| # define group_inherit_identity(group) | ||
| #endif | ||
|
|
||
| #ifdef CONFIG_FS_CHROOT |
There was a problem hiding this comment.
where we define this config
There was a problem hiding this comment.
fs/Kconfig
| FAR const char *end = src; | ||
| size_t seglen; | ||
|
|
||
| while (*end != '\0' && *end != '/') |
There was a problem hiding this comment.
why not assign to out directly? we should touch in only once in the loop
There was a problem hiding this comment.
Should we copy into out in one pass without walking in again?
| /* chroot() jail **********************************************************/ | ||
|
|
||
| FAR struct inode *tg_root; /* NULL means global pseudo-root */ | ||
| FAR char *tg_rootrel; /* Relpath prefix if tg_root is a mount */ |
There was a problem hiding this comment.
why not save the absolute path directly? it's complex to handle tg_root and tg_rootrel in the late patch.
BTW, the same path may remount to other file system, the current implementation can't handle it correctly.
There was a problem hiding this comment.
Store the jail as an absolute path and re-resolve it on lookup instead of tg_root / tg_rootrel?
There was a problem hiding this comment.
Yes, this make the code simpler and handle the mount point destroy/create correctly
| FAR struct inode *above = NULL; | ||
| FAR const char *relpath = NULL; | ||
| #ifdef CONFIG_FS_CHROOT | ||
| FAR struct inode *search_root = g_root_inode; |
There was a problem hiding this comment.
it's more simple to:
- prepend the root to the path
- normalize the new path
- ensure the result is under the root
- continue the original flow
There was a problem hiding this comment.
Prepend jail path, normalize, check still under root, then keep the original walk?
Store tg_root / tg_rootrel on the task group, copy them to children, and drop the inode when the last member leaves. Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
Add CONFIG_FS_CHROOT and POSIX chroot(). Pin a directory as the group root, rewrite PWD, and require euid 0 when user identity is enabled. Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
Walk from tg_root instead of the global pseudo-root, clamp ".." at the
jail, and normalize absolute paths so chroot(".") under a mount does
not pass a leftover "." as relpath.
Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
Describe the jail, leftover pre-opened fds, the NSH command-form scrub, and the flat-build trust boundary shared with credentials. Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
f3fbd17 to
5b250cc
Compare
Summary
CONFIG_FS_CHROOTadds POSIXchroot()so a task group can pin a directory as its filesystem root. Absolute lookups start at that inode, children inherit the jail, andPWDis rewritten so relative paths cannot walk out. This is a filesystem jail, not a container: descriptors opened beforechroot()that already point outside the tree remain usable.Impact
The option is off by default. When enabled,
chroot()is a new syscall and, withSCHED_USER_IDENTITY, requires effective UID 0. OnCONFIG_BUILD_FLATtheeuid == 0gate andtg_rootshare the same trust boundary as credentials. Companion apps change: apache/nuttx-apps#3735.Testing
Host: WSL2 x86_64. Board:
sim(CONFIG_FS_CHROOT=y). Companion apps PR: apache/nuttx-apps#3735.NSH no-command form (syscall from userspace;
chrootbuiltin is in apache/nuttx-apps#3735):ostest (kernel jail, leftover fd, inheritance):