Hi Sergey, here is a fix for the delayed status of directories in copy-in mode, with two autotest cases.
A directory that lacks the owner write bit gets its final status only after the last member has been extracted (apply_delayed_set_stat in util.c). When a later member with the same name replaces the directory, try_existing_file removes it, but its entry stays in the list, and the status is then applied by name to whatever is there: - a symbolic link: chmod and chown follow it, so the file it points to gets the mode of the directory. In setstat06 a file outside the extraction directory goes from 644 to 500, with and without --no-absolute-filenames, also when run as an ordinary user; - a regular file: it gets the mode of the directory; - a new directory of the same name: it gets the status of the removed one (setstat07: 500 instead of 555). The patch drops the entry when the directory is removed, as GNU tar does in safer_rmdir. It also records device and inode when the status is delayed and compares them with lstat before applying it, so that a name that no longer refers to the directory is skipped with an error. That second check is a safety net: with the first part in place the two tests do not reach it. Both tests fail before the change and pass after it. Full suite here: 18 of 18 before, 20 of 20 after. The patch is against master e5bb73f and does not depend on my patch from 10 October. Regards, Nexory
From 52fd9b5f8a482afd14b75bea95501f370b00189f Mon Sep 17 00:00:00 2001 From: Nexory <[email protected]> Date: Wed, 21 Oct 2026 12:50:46 +0200 Subject: [PATCH] Forget the delayed status of a directory that has been removed. When a member replaces a directory whose status is still to be set, try_existing_file removes the directory but leaves its entry in the delayed_set_stat list. apply_delayed_set_stat then applies that status by name: to the target of a symbolic link that took the name, to a regular file, or to a new directory of the same name. Drop the entry when the directory is removed. In addition, record device and inode when the status is delayed, and skip an entry whose name no longer refers to that directory. * src/util.c (struct delayed_set_stat): New members identified, st_dev, st_ino. (delay_cpio_set_stat): Record them. (forget_delayed_set_stat): New function. (apply_delayed_set_stat): Skip entries whose name no longer refers to the recorded directory. * src/extern.h (forget_delayed_set_stat): New proto. * src/copyin.c (try_existing_file): Forget the delayed status of a directory that has been removed. * tests/setstat06.at: New file. * tests/setstat07.at: New file. * tests/Makefile.am: Add new test cases. * tests/testsuite.at: Likewise. --- src/copyin.c | 2 ++ src/extern.h | 1 + src/util.c | 57 +++++++++++++++++++++++++++++++++++++++++++--- tests/Makefile.am | 2 ++ tests/setstat06.at | 48 ++++++++++++++++++++++++++++++++++++++ tests/setstat07.at | 45 ++++++++++++++++++++++++++++++++++++ tests/testsuite.at | 2 ++ 7 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 tests/setstat06.at create mode 100644 tests/setstat07.at diff --git a/src/copyin.c b/src/copyin.c index 46d5503..43576bd 100644 --- a/src/copyin.c +++ b/src/copyin.c @@ -219,6 +219,8 @@ try_existing_file (struct cpio_file_stat* file_hdr, int in_file_des, tape_skip_padding (in_file_des, file_hdr->c_filesize); return -1; /* Go to the next file. */ } + else if (S_ISDIR (file_stat.st_mode)) + forget_delayed_set_stat (&file_stat); } return 0; } diff --git a/src/extern.h b/src/extern.h index b0cf3e5..1beaa60 100644 --- a/src/extern.h +++ b/src/extern.h @@ -231,6 +231,7 @@ void delay_cpio_set_stat (struct cpio_file_stat *file_stat, void delay_set_stat (char const *file_name, struct stat *st, mode_t invert_permissions); int repair_delayed_set_stat (struct cpio_file_stat *file_hdr); +void forget_delayed_set_stat (struct stat const *st); void apply_delayed_set_stat (void); int arf_stores_inode_p (enum archive_format arf); diff --git a/src/util.c b/src/util.c index efa73ca..52b7cff 100644 --- a/src/util.c +++ b/src/util.c @@ -1314,6 +1314,10 @@ struct delayed_set_stat struct delayed_set_stat *next; struct cpio_file_stat stat; mode_t invert_permissions; + /* Device and inode of the directory when it was created. */ + bool identified; + dev_t st_dev; + ino_t st_ino; }; static struct delayed_set_stat *delayed_set_stat_head; @@ -1325,11 +1329,19 @@ delay_cpio_set_stat (struct cpio_file_stat *file_stat, size_t file_name_len = strlen (file_stat->c_name); struct delayed_set_stat *data = xmalloc (sizeof (struct delayed_set_stat) + file_name_len + 1); + struct stat st; data->next = delayed_set_stat_head; memcpy (&data->stat, file_stat, sizeof data->stat); data->stat.c_name = (char*) (data + 1); strcpy (data->stat.c_name, file_stat->c_name); data->invert_permissions = invert_permissions; + data->identified = (lstat (file_stat->c_name, &st) == 0 + && S_ISDIR (st.st_mode)); + if (data->identified) + { + data->st_dev = st.st_dev; + data->st_ino = st.st_ino; + } delayed_set_stat_head = data; } @@ -1395,17 +1407,56 @@ repair_delayed_set_stat (struct cpio_file_stat *file_hdr) return 1; } +/* Forget the delayed status of the directory identified by ST. Call + this after the directory has been removed. */ +void +forget_delayed_set_stat (struct stat const *st) +{ + struct delayed_set_stat **prev = &delayed_set_stat_head; + struct delayed_set_stat *data; + + while ((data = *prev) != NULL) + { + if (data->identified + && data->st_dev == st->st_dev + && data->st_ino == st->st_ino) + { + *prev = data->next; + free (data); + } + else + prev = &data->next; + } +} + void apply_delayed_set_stat () { while (delayed_set_stat_head) { struct delayed_set_stat *data = delayed_set_stat_head; - if (data->invert_permissions) + struct stat st; + + /* The name may have been given to another file since the directory + was created, e.g. by a symbolic link. set_perms follows symbolic + links, so apply the status only to the directory itself. */ + if (lstat (data->stat.c_name, &st) < 0) + stat_error (data->stat.c_name); + else if (!data->identified + || !S_ISDIR (st.st_mode) + || st.st_dev != data->st_dev + || st.st_ino != data->st_ino) + paxerror (0, + _("%s: Directory replaced before its status could be set"), + quotearg_colon (data->stat.c_name)); + else { - data->stat.c_mode ^= data->invert_permissions; + if (data->invert_permissions) + { + data->stat.c_mode ^= data->invert_permissions; + } + set_perms (-1, &data->stat); } - set_perms (-1, &data->stat); delayed_set_stat_head = data->next; free (data); } diff --git a/tests/Makefile.am b/tests/Makefile.am index e5096e5..617cc90 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -48,6 +48,8 @@ TESTSUITE_AT = \ setstat03.at\ setstat04.at\ setstat05.at\ + setstat06.at\ + setstat07.at\ symlink.at\ symlink-bad-length.at\ symlink-long.at\ diff --git a/tests/setstat06.at b/tests/setstat06.at new file mode 100644 index 0000000..85cacf6 --- /dev/null +++ b/tests/setstat06.at @@ -0,0 +1,48 @@ +# Process this file with autom4te to create testsuite. -*- Autotest -*- +# Copyright (C) 2026 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# The status of a directory that is not writable is set after the last +# member has been extracted. If a later member of the archive has the +# same name and replaces the directory with a symbolic link, the status +# must not be applied to the file the link points to. + +AT_SETUP([delayed setstat of a directory replaced by a symlink]) +AT_KEYWORDS([setstat setstat06]) + +AT_CHECK([ +for opt in "" --no-absolute-filenames +do + rm -rf src dest ctl archive + mkdir src dest + echo control > ctl + chmod 644 ctl + mkdir src/d + chmod 500 src/d + (cd src && echo d | cpio -o --quiet > ../archive) + chmod 700 src/d + rmdir src/d + ln -s ../ctl src/d || AT_SKIP_TEST + (cd src && echo d | cpio -o --quiet -A -F ../archive) + (cd dest && cpio -i --quiet -u $opt < ../archive) + genfile --stat=mode.777 ctl +done +], +[0], +[644 +644 +]) + +AT_CLEANUP diff --git a/tests/setstat07.at b/tests/setstat07.at new file mode 100644 index 0000000..476e054 --- /dev/null +++ b/tests/setstat07.at @@ -0,0 +1,45 @@ +# Process this file with autom4te to create testsuite. -*- Autotest -*- +# Copyright (C) 2026 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# When a later member replaces a directory whose status is still to be +# set, that status must be forgotten. If yet another member creates a +# directory of the same name, it gets its own status. + +AT_SETUP([delayed setstat of a directory removed and created again]) +AT_KEYWORDS([setstat setstat07]) + +AT_CHECK([ +mkdir src dest +(cd src + mkdir d + chmod 500 d + echo d | cpio -o --quiet > ../archive + chmod 700 d + rmdir d + echo data > d + echo d | cpio -o --quiet -A -F ../archive + rm -f d + mkdir d + chmod 555 d + echo d | cpio -o --quiet -A -F ../archive) +(cd dest && cpio -i --quiet -u < ../archive) +genfile --stat=mode.777 dest/d +], +[0], +[555 +]) + +AT_CLEANUP diff --git a/tests/testsuite.at b/tests/testsuite.at index 099c1e9..9afd10e 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -39,6 +39,8 @@ m4_include([setstat02.at]) m4_include([setstat03.at]) m4_include([setstat04.at]) m4_include([setstat05.at]) +m4_include([setstat06.at]) +m4_include([setstat07.at]) m4_include([big-block-size.at]) m4_include([CVE-2015-1197.at]) -- 2.47.3
