cpio HEAD writes outside the extraction directory when a member name
has ".." in the middle and --no-absolute-filenames is used.
Archive with "a" (directory) and "a/../../escaped" (file):
cd empty-dir
cpio -id --no-absolute-filenames < archive
"escaped" is created one level above empty-dir. Exit status 0, no
warning. cpio 2.15 stays inside.
Cause: paxutils commit 063408c changed safer_name_suffix to strip only
leading "/" and "../". The old function stripped up to the last "..".
This was posted before:
- 9 October 2026, Adrian Martinez:
https://lists.gnu.org/archive/html/bug-cpio/2026-10/msg00001.html
- 20 November 2026, matlailpajri1:
https://lists.gnu.org/archive/html/bug-cpio/2026-11/msg00019.html
No fix yet. The October patch still turns a/..//..//pwn9 into /pwn9.
The November patch rewrites safe names like x/../y to y.
This version removes only the part of the name that points outside.
x/../y stays as is. Hard link targets are covered too. It has a test:
tests/no-absolute-filenames.at.
make check passes 19/19, also under ASan. distcheck passes. Extraction
is about 1% slower.
Patch attached. Applies to e5bb73f.
--
Armandas Kvietkus
diff --git a/src/util.c b/src/util.c
index efa73ca..6eacc3f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1271,6 +1271,47 @@ cpio_set_c_name (struct cpio_file_stat *file_hdr, char *name)
memmove (file_hdr->c_name, name, len);
}
+/* Return the length of the longest prefix of the relative file name NAME
+ that must be skipped so that the remaining suffix cannot escape the
+ current directory by means of ".." components. Do not skip anything
+ if the name is already safe. */
+
+static size_t
+escaping_prefix_len (char const *name)
+{
+ size_t depth = 0; /* number of components below the current directory */
+ size_t skip = 0; /* end of the last component that escapes it */
+ char const *p = name;
+
+ /* Most names have no ".." component; avoid walking all of them. */
+ if (!strstr (name, ".."))
+ return 0;
+
+ while (*p)
+ {
+ char const *component;
+
+ while (ISSLASH (*p))
+ ++p;
+ if (!*p)
+ break;
+ component = p;
+ while (*p && !ISSLASH (*p))
+ ++p;
+ size_t len = p - component;
+ if (len == 2 && component[0] == '.' && component[1] == '.')
+ {
+ if (depth == 0)
+ skip = p - name;
+ else
+ depth--;
+ }
+ else if (! (len == 1 && component[0] == '.'))
+ depth++;
+ }
+ return skip;
+}
+
/* Do we have to ignore absolute paths, and if so, does the filename
have an absolute path? Before calling this function make sure that the
allocated NAME buffer has capacity at least 2 bytes. */
@@ -1288,6 +1329,22 @@ cpio_safer_name_suffix (char *name, bool link_target, bool absolute_names,
while (*p == '/')
++p;
}
+ if (!absolute_names)
+ {
+ /* safer_name_suffix only skips leading prefixes that escape the
+ current directory. A later ".." component can still escape, so
+ skip the longest such prefix here. */
+ size_t skip = escaping_prefix_len (p);
+ if (skip)
+ {
+ static char const dot[] = ".";
+ p += skip;
+ while (ISSLASH (*p))
+ ++p;
+ if (!*p)
+ p = (char *) dot;
+ }
+ }
if (p != name)
/* The 'p' string is shortened version of 'name' with one exception; when
the 'name' points to an empty string (buffer where name[0] == '\0') the
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5096e5..b70d847 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -58,7 +58,8 @@ TESTSUITE_AT = \
CVE-2019-14866.at\
linktime.at\
linktime01.at\
- symlink-tar-abs.at
+ symlink-tar-abs.at\
+ no-absolute-filenames.at
TESTSUITE = $(srcdir)/testsuite
diff --git a/tests/no-absolute-filenames.at b/tests/no-absolute-filenames.at
new file mode 100644
index 0000000..e3f177e
--- /dev/null
+++ b/tests/no-absolute-filenames.at
@@ -0,0 +1,64 @@
+# 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 archive contains a directory and a file whose name has an interior
+# ".." component. With --no-absolute-filenames the member must be
+# extracted inside the current directory, not above it.
+
+AT_SETUP([--no-absolute-filenames: escape via .. inside a member name])
+AT_KEYWORDS([copyin no-absolute-filenames])
+
+# mkhdr creates a newc header. mkhdr NAME MODE SIZE
+AT_CHECK([
+mkhdr () {
+ name=$1
+ mode=$2
+ size=$3
+ nsize=$(expr "$name" : '.*')
+ nsize=$((nsize + 1))
+ printf '070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X' \
+ 1 "$mode" 0 0 1 0 "$size" 0 0 0 0 "$nsize" 0
+ printf '%s\0' "$name"
+ total=$((110 + nsize))
+ pad=$(( (4 - total % 4) % 4 ))
+ i=0
+ while test $i -lt $pad; do
+ printf '\0'
+ i=$((i + 1))
+ done
+}
+
+mkarc () {
+ mkhdr a 16877 0
+ mkhdr a/../../escaped 33188 8
+ printf 'ESCAPED\n'
+ mkhdr 'TRAILER!!!' 0 0
+}
+
+mkarc > arc
+mkdir dir
+cd dir
+cpio --quiet -i --no-absolute-filenames < ../arc
+test ! -e ../escaped || exit 1
+test -f escaped || exit 1
+cat escaped
+],
+[0],
+[ESCAPED
+],
+[])
+
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 099c1e9..4300f1f 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -48,4 +48,6 @@ m4_include([linktime.at])
m4_include([linktime01.at])
+m4_include([no-absolute-filenames.at])
+
m4_include([symlink-tar-abs.at])