[Toybox] switch_root: mnt_ns->root stays on the initramfs since c04b565

Yunjie Li yunjieli at google.com
Tue Sep 29 21:38:51 UTC 2026


Hi Rob,
Reposting this from github since it looks like nobody saw it there --
issue #557 <https://github.com/landley/toybox/issues/557> from last July
and PR #593 <https://github.com/landley/toybox/pull/593> from January, no
replies on either.
Change c04b565
<https://github.com/landley/toybox/commit/c04b565204eb6b7e3508ac8dd42539ab97752635>
("Teach switch_root to move mountpoints into the new root")
dropped the `*mount(".", "/", NULL, MS_MOVE, NULL)*` that used to run right
before `*chroot(".")*` in `*switch_root*`. That line wasn't cosmetic: `
*chroot()*`
only changes the process's `*fs->root*`, it's the `*MS_MOVE*` onto `*/*`
that makes the new root the mount namespace's root (`*mnt_ns->root*`).
Without it
`*mnt_ns->root*` keeps pointing at the initramfs rootfs that `*del_node()*`
just
emptied, and every mount namespace created later inherits that via
`*copy_mnt_ns()*`.
Nothing notices until something joins a mount namespace. `*setns()*` goes
through `*mntns_install()*`, which resets the caller's root by walking down
from `*mnt_ns->root*`, so it lands in the empty initramfs dir. On a 6.9.12
guest booted with toybox 0.8.14 `*switch_root`:*
```
* nsenter -m -t $pid -- cat /proc/sys/kernel/cap_last_cap*
* -> nsenter: failed to execute /bin/cat: No such file or directory*
* nsenter -m -r -t $pid -- cat /proc/sys/kernel/cap_last_cap*
* -> 40*
* docker exec $ctr /bin/true*
* -> exit 126, OCI runtime exec failed: ... open*
* /proc/sys/kernel/cap_last_cap: no such file or directory*
```
`*runc exec*` is a bare `*setns*`, hence the 126; `*-r*` works because it
chroots to `*/proc/PID/root*` instead. issue #557
<https://github.com/landley/toybox/issues/557> is the same bug seen from
systemd's side.
Repro without docker: boot from an initramfs whose init ends in
"*exec switch_root /newroot /sbin/init*", then
```
* unshare -m sleep 1000 &*
* nsenter -m -t $! ls /*
```
and you get the leftover mountpoint dir instead of the real root.
PR #593 <https://github.com/landley/toybox/pull/593> just puts the line
back, after the submount moves and before the
chroot. Same order as util-linux
<https://github.com/util-linux/util-linux/blob/master/sys-utils/switch_root.c,>
(`*mount(newroot, "/", NULL, MS_MOVE, NULL)*` then `*chroot(".")*`) and as
Documentation/filesystems/ramfs-rootfs-initramfs.rst
("*cd /newmount; mount --move . /; chroot .*").
But I think the best approach is put the line back. It doesn't look like
you meant to delete this line in c04b565
<https://github.com/landley/toybox/commit/c04b565204eb6b7e3508ac8dd42539ab97752635>.

As a workaround on our side I changed our init to do the equivalent
(`*cd /newroot; mount -o move . /; exec chroot . /sbin/init*`). On the same
6.9.12 / 0.8.14 setup that takes docker exec from 126 to 0, `nsenter -m`
prints 40, and the submount moving from c04b565
<https://github.com/landley/toybox/commit/c04b565204eb6b7e3508ac8dd42539ab97752635>
still works.
But I think the right fix is to put the line back in `*switch_root*` -- it
doesn't look like deleting it was intentional in c04b565
<https://github.com/landley/toybox/commit/c04b565204eb6b7e3508ac8dd42539ab97752635>
.
Thanks! Yunjie Li
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260929/88c73986/attachment-0001.htm>


More information about the Toybox mailing list