Fix --preserve-root
I'm fairly certain that if the --preserve-root in chgrp was wrong, it is wrong in other utilities as well.
chgrp will still follow a bind mount to root and continue:
mkdir /root/root
mount -o bind / /root/root
chgrp --preserve-root -R root /root -v
The same applies to a symbolic link (with -H).
ln -s / /root/root
./target/debug/deps/chgrp --preserve-root -R root /root -v -H
Though it will detect a file system loop if a symbolic link is pointing to a parent directory other than the root directory (ln -s /tmp /tmp/tmp).
I'm pretty sure the problem right now is because we are only checking if it's a bind mount to root or a symbolic link to root in traverse(), which is only called once. The WalkDir in dive_into() doesn't check for either of these cases, so it just keeps going unless you specifically give a bind mount to root or a symbolic link on the command-line.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi, all. Has this been resolved?
resolved on main (fixed by PR #6042, later hardened with safe traversal), both symlink-to-/ with -HR and direct / with -R correctly warn and exit 1. I recommend closing #1004.
# build binary
cargo build -p uu_chgrp --release
# 1) Symlink to / with -H (follow symlink at top) and -R
tmpdir=$(mktemp -d); cd "$tmpdir"; ln -s / root_link
/path/to/repo/target/release/chgrp --preserve-root -HR wheel root_link; echo "(exit=$?)"
# 2) Direct / with -R
/path/to/repo/target/release/chgrp --preserve-root -R wheel /; echo "(exit=$?)"
Observed output:
it is dangerous to operate recursively on 'root_link' (same as '/')
use --no-preserve-root to override this failsafe
(exit=1)
it is dangerous to operate recursively on '/'
use --no-preserve-root to override this failsafe
(exit=1)