Description: Fix CVE-2026-66485: unbounded alloca() in make_path
 Do not use alloca() for archive-controlled path lengths (upstream 3cd51403).
Origin: upstream, https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=3cd514031371d8aeeaf2048aa10103e02831aaa9
Bug-Debian: https://bugs.debian.org/1144387
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2026-66485
Forwarded: not-needed
Last-Update: 2026-10-31
---
diff --git a/src/makepath.c b/src/makepath.c
index 35dbc73..c6329bf 100644
--- a/src/makepath.c
+++ b/src/makepath.c
@@ -46,24 +46,19 @@
    Return 0 if ARGPATH exists as a directory with the proper
    ownership and permissions when done, otherwise 1.  */
 
-int
-make_path (char const *argpath,
-	   uid_t owner,
-	   gid_t group,
-	   const char *verbose_fmt_string)
+static int
+make_path0 (char *dirpath,
+	    uid_t owner,
+	    gid_t group,
+	    const char *verbose_fmt_string)
 {
-  char *dirpath;		/* A copy we can scribble NULs on.  */
   struct stat stats;
-  int retval = 0;
   mode_t tmpmode;
   mode_t invert_permissions;
   int we_are_root = getuid () == 0;
-  dirpath = alloca (strlen (argpath) + 1);
-
-  strcpy (dirpath, argpath);
 
   if (stat (dirpath, &stats))
-    {
+  {
       tmpmode = MODE_RWX & ~ newdir_umask;
       invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ tmpmode;
 
@@ -156,5 +151,19 @@ make_path (char const *argpath,
 
     }
 
+  return 0;
+}
+
+int
+make_path (char const *argpath,
+	   uid_t owner,
+	   gid_t group,
+	   const char *verbose_fmt_string)
+{
+  char *dirpath = xstrdup (argpath);
+  int retval = make_path0 (dirpath, owner, group, verbose_fmt_string);
+  free (dirpath);
   return retval;
 }
+
+
diff --git a/src/userspec.c b/src/userspec.c
index 2a2b324..1a2bfa0 100644
--- a/src/userspec.c
+++ b/src/userspec.c
@@ -18,7 +18,6 @@
 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
 
 #include <system.h>
-#include <alloca.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
@@ -32,18 +31,6 @@
 # define endgrent()
 #endif
 
-/* Perform the equivalent of the statement `dest = strdup (src);',
-   but obtaining storage via alloca instead of from the heap.  */
-
-#define V_STRDUP(dest, src)						\
-  do									\
-    {									\
-      int _len = strlen ((src));					\
-      (dest) = (char *) alloca (_len + 1);				\
-      strcpy (dest, src);						\
-    }									\
-  while (0)
-
 /* Return nonzero if STR represents an unsigned decimal integer,
    otherwise return 0. */
 
@@ -56,6 +43,18 @@ isnumber_p (const char *str)
   return 1;
 }
 
+static void
+store_string (char **bufptr, size_t *buflen, char *str)
+{
+  size_t len = strlen (str) + 1;
+  if (len > *buflen)
+    {
+      *bufptr = xrealloc (*bufptr, len);
+      *buflen = len;
+    }
+  strcpy (*bufptr, str);
+}
+
 /* Extract from NAME, which has the form "[user][:.][group]",
    a USERNAME, UID U, GROUPNAME, and GID G.
    Either user or group, or both, must be present.
@@ -69,23 +68,21 @@ isnumber_p (const char *str)
    Return NULL if successful, a static error message string if not.  */
 
 const char *
-parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
-		 char **username_arg, char **groupname_arg)
+parse_user_spec0 (char *spec, uid_t *uid, gid_t *gid,
+		  char **username_arg, char **groupname_arg)
 {
   static const char *tired = "virtual memory exhausted";
   const char *error_msg;
-  char *spec;			/* A copy we can write on.  */
   struct passwd *pwd;
   struct group *grp;
   char *g, *u, *separator;
-  char *groupname;
+  char *groupname = NULL;
+  size_t grouplen = 0;
 
   error_msg = NULL;
   *username_arg = *groupname_arg = NULL;
   groupname = NULL;
 
-  V_STRDUP (spec, spec_arg);
-
   /* Find the separator if there is one.  */
   separator = strchr (spec, ':');
   if (separator == NULL)
@@ -142,11 +139,12 @@ parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
 	      if (grp == NULL)
 		{
 		  char nbuf[UINTMAX_STRSIZE_BOUND];
-		  V_STRDUP (groupname, umaxtostr (pwd->pw_gid, nbuf));
+		  store_string (&groupname, &grouplen,
+				umaxtostr (pwd->pw_gid, nbuf));
 		}
 	      else
 		{
-		  V_STRDUP (groupname, grp->gr_name);
+		  store_string (&groupname, &grouplen, grp->gr_name);
 		}
 	      endgrent ();
 	    }
@@ -177,7 +175,7 @@ parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
       endgrent ();		/* Save a file descriptor.  */
 
       if (error_msg == NULL)
-	V_STRDUP (groupname, g);
+	store_string (&groupname, &grouplen, g);
     }
 
   if (error_msg == NULL)
@@ -190,23 +188,24 @@ parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
 	}
 
       if (groupname != NULL && error_msg == NULL)
-	{
-	  *groupname_arg = strdup (groupname);
-	  if (*groupname_arg == NULL)
-	    {
-	      if (*username_arg != NULL)
-		{
-		  free (*username_arg);
-		  *username_arg = NULL;
-		}
-	      error_msg = tired;
-	    }
-	}
+	*groupname_arg = groupname;
     }
+  else
+    free (groupname);
 
   return error_msg;
 }
 
+const char *
+parse_user_spec (const char *spec_arg, uid_t *uid, gid_t *gid,
+		 char **username, char **groupname)
+{
+  char *spec = xstrdup (spec_arg);
+  const char *retval = parse_user_spec0 (spec, uid, gid, username, groupname);
+  free (spec);
+  return retval;
+}
+
 #ifdef TEST
 
 #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
