From c366525850cfdfd2884ab9a214be21a4f66505bd Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang via Toybox Date: Sat, 24 Apr 2021 01:52:39 +0800 Subject: cpio: Don't lchown() if -t is specified When using -t to inspect an archive, cpio would try to set the owner of any symlink in the archive, even though the symlink wasn't created by the command previously. This would lead to two results, either the command fails with a "No such file or directory" message when trying to lchown() the symlink path, or an existing file, with the name of the symlink, is lchown()-ed. Guard the lchown() function call with a "if (!FLAG(t))" block, and add regression test for this. --- tests/cpio.test | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/cpio.test b/tests/cpio.test index 34794841..183dadde 100755 --- a/tests/cpio.test +++ b/tests/cpio.test @@ -63,3 +63,22 @@ testing "skip NUL" "for i in a b; do dd if=/dev/zero bs=512 count=1 2>/dev/null; rm -rf a a.cpio testing "error on empty file" "cpio -i 2>/dev/null || echo err" "err\n" "" "" + +mkdir a +touch a/file +ln -s a/symlink a/symlink +mkdir a/dir +find a | cpio -o -H newc >a.cpio +if [ "$(id -u)" -eq 0 ]; then + # We chown between user "root" and the last user in /etc/passwd, + # and group "root" and the last group in /etc/group. + USR="$(sed -n '$s/:.*//p' /etc/passwd)" + GRP="$(sed -n '$s/:.*//p' /etc/group)" + # Or if that fails, we assume we're on Android... + : "${USR:=shell}" + : "${GRP:=shell}" + chown -h "${USR}:${GRP}" a/file a/symlink a/dir +fi +skipnot [ $(id -u) -eq 0 ] +testing "-t preserve ownership" "cpio -t /dev/null && stat -c '%U:%G' a/file a/symlink a/dir" "${USR}:${GRP}\n${USR}:${GRP}\n${USR}:${GRP}\n" "" "" +rm -rf a a.cpio -- cgit v1.2.3