split_long_name() can return a prefix length of TARPREFIXSIZE+1 when
a slash sits at that offset, but the tar header prefix field has only
TARPREFIXSIZE bytes.  write_out_tar_header() then wrote one byte past
the field into the record padding.  Reject such names in
is_tar_filename_too_long() so they take the existing "file name too
long" path.

* src/tar.c (is_tar_filename_too_long): Reject prefix_name_len greater
than TARPREFIXSIZE.
* tests/tar-prefix-overflow.at: New test.
* tests/Makefile.am: Add it.
* tests/testsuite.at: Include it.

Signed-off-by: Mohammed Mustafa <[email protected]>
---
 src/tar.c                    |  1 +
 tests/Makefile.am            |  3 ++-
 tests/tar-prefix-overflow.at | 34 ++++++++++++++++++++++++++++++++++
 tests/testsuite.at           |  2 ++
 4 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 tests/tar-prefix-overflow.at

diff --git a/src/tar.c b/src/tar.c
index fc9bde9..9a52ffd 100644
--- a/src/tar.c
+++ b/src/tar.c
@@ -509,6 +509,7 @@ is_tar_filename_too_long (char *name)
      TARNAMESIZE characters can't be stored, because the prefix
      would be empty, and thus ignored.  */
   if (prefix_name_len == 0
+      || prefix_name_len > TARPREFIXSIZE
       || whole_name_len - prefix_name_len - 1 > TARNAMESIZE)
     return true;

diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5096e5..febbd66 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\
+ tar-prefix-overflow.at

 TESTSUITE = $(srcdir)/testsuite

diff --git a/tests/tar-prefix-overflow.at b/tests/tar-prefix-overflow.at
new file mode 100644
index 0000000..c90f5c5
--- /dev/null
+++ b/tests/tar-prefix-overflow.at
@@ -0,0 +1,34 @@
+# 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/>.
+
+AT_SETUP([ustar name with oversized prefix])
+AT_KEYWORDS([tar prefix overflow])
+
+# A slash at offset TARPREFIXSIZE+1 makes split_long_name() return a
+# prefix length one byte larger than the tar header's prefix field.
+# Such a name cannot be stored in a ustar header and must be rejected
+# rather than written with a byte past the prefix field.
+AT_CHECK([
+prefix=`printf '%156s' '' | tr ' ' a`
+suffix=`printf '%99s' '' | tr ' ' b`
+mkdir "$prefix" || exit 1
+touch "$prefix/$suffix" || exit 1
+printf '%s\n' "$prefix/$suffix" | cpio -o -H ustar >/dev/null 2>err
+],
+[0],
+[],
+[])
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 099c1e9..d0723ec 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -49,3 +49,5 @@ m4_include([linktime01.at])


 m4_include([symlink-tar-abs.at])
+
+m4_include([tar-prefix-overflow.at])
-- 
2.56.1

Reply via email to