Why is chroot(2) unavailable to unprivileged users

chrootSecurity

Why is chroot(2) unavailable to unprivileged users?

I don't understand existing answers on the Internet. For example this one https://lists.freebsd.org/pipermail/freebsd-security/2003-April/000123.html.

Would sudo really work if /etc/sudoers and /etc were not owned by root? An unprivileged user can't just create root owned setuid binaries inside chroot, can she?

Exactly how can an unprivileged user subvert the chroot environment?

I can only think of something like that

ln /mnt/backup/XYZ/etc/sudoers $CHROOT/etc/sudoers
ln /usr/bin/sudo $CHROOT/usr/bin/sudo

where XYZ denotes some backup instance where admin really screwed up and allowed my user something dangerous. But this is kinda special. Is there any more straightforward way to exploit chroot(2) if it was available to unprivileged users?

Best Answer

An ordinary user can't create a setuid binary, but there's nothing preventing him from creating a hard link to an existing setuid binary. So if he has write permission to a directory on the same filesystem as /usr/bin, he can put the jail in this directory, create a hard link to su or sudo in it, and put a custom /etc/passwd and /etc/sudoers in the jail.

Maybe that won't work for sudo, as it might check that /etc/sudoers is owned by root. But I'll bet su doesn't check the ownership of /etc/passwd.

Related Question