Description: tar format: memory safety fixes from upstream
 Upstream e8931bed (possible stack overflow in write_out_tar_header), b5c7d6e2
 (off-by-one write in tarnamecpy) and e5bb73f0 (uname/gname must be
 NUL-terminated; avoids an over-read on crafted archives). No CVE ids assigned.
Origin: upstream, https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=e8931bebb9e159a67db64ac1ac86314566ad24d8 https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=b5c7d6ee27520c3bf94fe29b3ceed86421759cb0 https://git.savannah.gnu.org/cgit/cpio.git/commit/?id=e5bb73f8c2f32a999bdec31cdcaea8068875677c
Bug-Debian: https://bugs.debian.org/1144387
Forwarded: not-needed
Last-Update: 2026-10-31
---
--- a/src/tar.c
+++ b/src/tar.c
@@ -133,6 +133,13 @@ tar_checksum (struct tar_header *tar_hdr
     }							    \
   while (0)
 
+static void
+tarnamecpy (char *buf, char const *name, size_t size)
+{
+  strncpy (buf, name, size-1);
+  buf[size-1] = 0;
+}
+
 /* Write out header FILE_HDR, including the file name, to file
    descriptor OUT_DES.  */
 
@@ -221,10 +228,10 @@ write_out_tar_header (struct cpio_file_s
 
       name = getuser (file_hdr->c_uid);
       if (name)
-	strcpy (tar_hdr->uname, name);
+	tarnamecpy (tar_hdr->uname, name, sizeof (tar_hdr->uname));
       name = getgroup (file_hdr->c_gid);
       if (name)
-	strcpy (tar_hdr->gname, name);
+	tarnamecpy (tar_hdr->gname, name, sizeof (tar_hdr->gname));
 
       TO_OCT (file_hdr, c_rdev_maj, 8, tar_hdr, devmajor);
       TO_OCT (file_hdr, c_rdev_min, 8, tar_hdr, devminor);
@@ -325,16 +332,23 @@ read_in_tar_header (struct cpio_file_sta
       file_hdr->c_nlink = 1;
       file_hdr->c_mode = FROM_OCTAL (tar_hdr->mode);
       file_hdr->c_mode = file_hdr->c_mode & 07777;
-  /* Debian hack: This version of cpio uses the -n flag also to extract
-     tar archives using the numeric UID/GID instead of the user/group
-     names in /etc/passwd and /etc/groups.  (98/10/15) -BEM */
+
+      /* Make sure uname and gname are nul-terminated */
+      if (!memchr (tar_hdr->uname, 0, sizeof (tar_hdr->uname)))
+	tar_hdr->uname[0] = 0;
+
+      if (!memchr (tar_hdr->gname, 0, sizeof (tar_hdr->gname)))
+	tar_hdr->gname[0] = 0;
+
       if (archive_format == arf_ustar && !numeric_uid
+	  && tar_hdr->uname[0]
 	  && (uidp = getuidbyname (tar_hdr->uname)))
 	file_hdr->c_uid = *uidp;
       else
 	file_hdr->c_uid = FROM_OCTAL (tar_hdr->uid);
 
       if (archive_format == arf_ustar && !numeric_uid
+	  && tar_hdr->gname[0]
 	  && (gidp = getgidbyname (tar_hdr->gname)))
 	file_hdr->c_gid = *gidp;
       else
