From bbd32db81dd1a81a36dbfaafeeae1254200f73e7 Mon Sep 17 00:00:00 2001
From: Mamat9q <matlailpajri1@gmail.com>
Date: Fri, 20 Nov 2026 09:00:58 +0000
Subject: [PATCH] Collapse interior ".." components under
 --no-absolute-filenames

safer_name_suffix() strips only leading "/" and "../" sequences, so a
member name such as "a/../../evil" passed through unchanged and was
opened outside the extraction directory, bypassing the containment
that CVE-2015-1197 established for leading components.  Collapse ".."
components lexically in cpio_safer_name_suffix() when absolute names
are not allowed: "." and empty components are dropped, and ".."
removes the last kept component (or is dropped when nothing was kept),
so the result can never escape the target directory.  Default
behaviour without --no-absolute-filenames is unchanged.
---
 src/util.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/src/util.c b/src/util.c
index efa73ca..457f30e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1271,6 +1271,56 @@ cpio_set_c_name (struct cpio_file_stat *file_hdr, char *name)
   memmove (file_hdr->c_name, name, len);
 }
 
+/* Collapse ".." components in NAME lexically, in place, so the result
+   cannot escape the directory it is relative to.  NAME is a suffix
+   already stripped of leading "/" and "../" sequences by
+   safer_name_suffix(); that function does not touch interior "..", so
+   e.g. "a/../../evil" would otherwise be opened outside the
+   extraction directory even with --no-absolute-filenames (the
+   containment that CVE-2015-1197 established for leading components
+   only).  Components are processed left to right: "." and empty
+   components are dropped, and ".." removes the last kept component,
+   or is dropped when nothing was kept.  The result is never longer
+   than the input.  */
+
+static void
+collapse_dotdot_components (char *name)
+{
+  char *r = name, *w = name;
+
+  while (*r)
+    {
+      while (*r == '/')
+	++r;
+      if (*r == '\0')
+	break;
+      char *comp = r;
+      while (*r != '\0' && *r != '/')
+	++r;
+      size_t len = (size_t) (r - comp);
+      if (len == 1 && comp[0] == '.')
+	continue;
+      if (len == 2 && comp[0] == '.' && comp[1] == '.')
+	{
+	  if (w > name)
+	    {
+	      if (w[-1] == '/')
+		--w;
+	      while (w > name && w[-1] != '/')
+		--w;
+	    }
+	  continue;
+	}
+      if (w != name)
+	*w++ = '/';
+      memmove (w, comp, len);
+      w += len;
+    }
+  if (w == name)
+    *w++ = '.';
+  *w = '\0';
+}
+
 /* 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 +1338,10 @@ cpio_safer_name_suffix (char *name, bool link_target, bool absolute_names,
 	while (*p == '/')
 	  ++p;
       }
+  if (!absolute_names && p[0] != '\0' && !(p[0] == '.' && p[1] == '\0'))
+    /* Collapse interior ".." components.  Do not touch the static "."
+       string safer_name_suffix() may return for an empty name.  */
+    collapse_dotdot_components (p);
   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
-- 
2.43.0

