Skip to content

nshlib,testing: add chroot command and ostest - #3735

Open
Abhishekmishra2808 wants to merge 3 commits into
apache:masterfrom
Abhishekmishra2808:feature/nsh-chroot
Open

nshlib,testing: add chroot command and ostest#3735
Abhishekmishra2808 wants to merge 3 commits into
apache:masterfrom
Abhishekmishra2808:feature/nsh-chroot

Conversation

@Abhishekmishra2808

@Abhishekmishra2808 Abhishekmishra2808 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an NSH chroot helper that chdirs into the new root, calls chroot("."), then chdir("/") so relative paths stay in the jail. The command form chroot <newroot> <command> closes non-stdio, non-O_CLOEXEC descriptors before execvp so inherited host fds cannot bypass the jail. ostest covers jail visibility, host-path isolation, child inheritance, and leftover pre-opened fds.

Impact

The NSH command is gated by CONFIG_FS_CHROOT and can be disabled with CONFIG_NSH_DISABLE_CHROOT. ostest runs the new case only when CONFIG_FS_CHROOT and CONFIG_SCHED_WAITPID are set and the build is not kernel. Companion kernel change is apache/nuttx#19900.

Testing

Host: WSL2 x86_64. Board: sim (CONFIG_FS_CHROOT=y, CONFIG_TESTING_OSTEST=y), with nuttx feature/fs-chroot.

$ ./tools/checkpatch.sh -f apps/nshlib/nsh_envcmds.c apps/testing/ostest/chroot.c
✔️ All checks pass.
login: root
User Logged-in!
nsh> ostest
...
user_main: chroot test
chroot_test: Starting test
chroot_test: /marker is visible inside the jail
chroot_test: host paths are not visible inside the jail
chroot_test: pre-opened host fd still usable after chroot
chroot_test: grandchild still sees the jail
chroot_test: PASSED

chdir into the new root, call chroot("."), then chdir("/") so relative
paths stay in the jail. With a command, execvp() after the jail is set.

Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
chroot <newroot> <command> closes non-stdio, non-O_CLOEXEC descriptors
before execvp so inherited host fds cannot bypass the jail.

Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
Check jail visibility, host-path isolation, child inheritance, and that
a pre-opened host fd remains usable after chroot().

Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants