Skip to contentSkip to conversation

erofs-utils: lib: tar: ignore useless fields of PAX extended headers

PATCH New Gao Xiang 1 participant 3 messages Dec 26, 2024 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: lib: tar: ignore useless fields of PAX extended headers

Since some unknown writer just leaves zero-filled mtime, e.g. registry.k8s.io/pause:3.6 --platform windows

Layer sha256: bc8517709e9cfff223cb034ff5be8fcbfa5409de286cdac9ae1b8878ebea6b84

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 78 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 42 insertions(+), 36 deletions(-)
lib/tar.c +42 -36
index 0dd990e..12bf595 100644
@@ -659,6 +659,7 @@ int tarerofs_parse_tar(struct erofs_inode *root, struct erofs_tarfile *tar)
 	struct erofs_sb_info *sbi = root->sbi;
 	bool whout, opq, e = false;
 	struct stat st;
+	mode_t mode;
 	erofs_off_t tar_offset, dataoff;
 
 	struct tar_header *th;
@@ -751,26 +752,6 @@ out_eot:
 		goto out;
 	}
 
-	st.st_mode = tarerofs_otoi(th->mode, sizeof(th->mode));
-	if (errno)
-		goto invalid_tar;
-
-	if (eh.use_uid) {
-		st.st_uid = eh.st.st_uid;
-	} else {
-		st.st_uid = tarerofs_parsenum(th->uid, sizeof(th->uid));
-		if (errno)
-			goto invalid_tar;
-	}
-
-	if (eh.use_gid) {
-		st.st_gid = eh.st.st_gid;
-	} else {
-		st.st_gid = tarerofs_parsenum(th->gid, sizeof(th->gid));
-		if (errno)
-			goto invalid_tar;
-	}
-
 	if (eh.use_size) {
 		st.st_size = eh.st.st_size;
 	} else {
@@ -779,16 +760,6 @@ out_eot:
 			goto invalid_tar;
 	}
 
-	if (eh.use_mtime) {
-		st.st_mtime = eh.st.st_mtime;
-		ST_MTIM_NSEC_SET(&st, ST_MTIM_NSEC(&eh.st));
-	} else {
-		st.st_mtime = tarerofs_parsenum(th->mtime, sizeof(th->mtime));
-		if (errno)
-			goto invalid_tar;
-		ST_MTIM_NSEC_SET(&st, 0);
-	}
-
 	if (th->typeflag <= '7' && !eh.path) {
 		eh.path = path;
 		j = 0;
@@ -810,28 +781,29 @@ out_eot:
 
 	dataoff = tar->offset;
 	tar->offset += st.st_size;
+	st.st_mode = 0;
 	switch(th->typeflag) {
 	case '0':
 	case '7':
 	case '1':
-		st.st_mode |= S_IFREG;
+		st.st_mode = S_IFREG;
 		if (tar->headeronly_mode || tar->ddtaridx_mode)
 			tar->offset -= st.st_size;
 		break;
 	case '2':
-		st.st_mode |= S_IFLNK;
+		st.st_mode = S_IFLNK;
 		break;
 	case '3':
-		st.st_mode |= S_IFCHR;
+		st.st_mode = S_IFCHR;
 		break;
 	case '4':
-		st.st_mode |= S_IFBLK;
+		st.st_mode = S_IFBLK;
 		break;
 	case '5':
-		st.st_mode |= S_IFDIR;
+		st.st_mode = S_IFDIR;
 		break;
 	case '6':
-		st.st_mode |= S_IFIFO;
+		st.st_mode = S_IFIFO;
 		break;
 	case 'g':
 		ret = tarerofs_parse_pax_header(&tar->ios, &tar->global,
@@ -876,6 +848,40 @@ out_eot:
 		goto out;
 	}
 
+	mode = tarerofs_otoi(th->mode, sizeof(th->mode));
+	if (errno)
+		goto invalid_tar;
+	if (__erofs_unlikely(mode & S_IFMT) &&
+	    (mode & S_IFMT) != (st.st_mode & S_IFMT))
+		erofs_warn("invalid ustar mode %05o @ %llu", mode, tar_offset);
+	st.st_mode |= mode & ~S_IFMT;
+
+	if (eh.use_uid) {
+		st.st_uid = eh.st.st_uid;
+	} else {
+		st.st_uid = tarerofs_parsenum(th->uid, sizeof(th->uid));
+		if (errno)
+			goto invalid_tar;
+	}
+
+	if (eh.use_gid) {
+		st.st_gid = eh.st.st_gid;
+	} else {
+		st.st_gid = tarerofs_parsenum(th->gid, sizeof(th->gid));
+		if (errno)
+			goto invalid_tar;
+	}
+
+	if (eh.use_mtime) {
+		st.st_mtime = eh.st.st_mtime;
+		ST_MTIM_NSEC_SET(&st, ST_MTIM_NSEC(&eh.st));
+	} else {
+		st.st_mtime = tarerofs_parsenum(th->mtime, sizeof(th->mtime));
+		if (errno)
+			goto invalid_tar;
+		ST_MTIM_NSEC_SET(&st, 0);
+	}
+
 	st.st_rdev = 0;
 	if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
 		int major, minor;
Signature · 1 line
2.39.3 (Apple Git-146)
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: fix LIBARCHIVE.xattr base64 decoding

Base64 is described in RFC1521 [1], except that LIBARCHIVE.xattr base64 may not have padding at the end of the data using the '=' character.

[1] https://datatracker.ietf.org/doc/html/rfc1521#section-5.2

Fixes c0063a73b01b ("erofs-utils: lib: support importing xattrs from tarerofs")
---
 lib/tar.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
lib/tar.c +11 -5
index 12bf595..9642e2e 100644
@@ -399,25 +399,27 @@ int tarerofs_apply_xattrs(struct erofs_inode *inode, struct list_head *xattrs)
 }
 
 static const char lookup_table[65] =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 static int base64_decode(const char *src, int len, u8 *dst)
 {
 	int i, bits = 0, ac = 0;
 	const char *p;
 	u8 *cp = dst;
+	bool padding = false;
 
-	if(!(len % 4)) {
+	if(len && !(len % 4)) {
 		/* Check for and ignore any end padding */
 		if (src[len - 2] == '=' && src[len - 1] == '=')
 			len -= 2;
 		else if (src[len - 1] == '=')
 			--len;
+		padding = true;
 	}
 
 	for (i = 0; i < len; i++) {
 		p = strchr(lookup_table, src[i]);
-		if (p == NULL || src[i] == 0)
+		if (!p || !src[i])
 			return -2;
 		ac += (p - lookup_table) << bits;
 		bits += 6;
@@ -427,8 +429,12 @@ static int base64_decode(const char *src, int len, u8 *dst)
 			bits -= 8;
 		}
 	}
-	if (ac)
-		return -1;
+	if (ac) {
+		if (padding || ac > 0xff)
+			return -1;
+		else
+			*cp++ = ac & 0xff;
+	}
 	return cp - dst;
 }
 
Signature · 1 line
2.39.3 (Apple Git-146)
author
#2 lore raw

PATCH 3/3 erofs-utils: lib: fix LIBARCHIVE.xattr URL-encoded keys

In reply to Gao Xiang

As tar(5) [1] mentioned: The key value is URL-encoded: All non-ASCII characters and the two special characters "=" and "%" are encoded as "%" followed by two uppercase hexadecimal digits.

Fix it now.

[1] https://man.freebsd.org/cgi/man.cgi?tar(5)

Fixes c0063a73b01b ("erofs-utils: lib: support importing xattrs from tarerofs")
---
 lib/tar.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 7 deletions(-)
lib/tar.c +52 -7
index 9642e2e..2ea3858 100644
@@ -438,6 +438,47 @@ static int base64_decode(const char *src, int len, u8 *dst)
 	return cp - dst;
 }
 
+static int tohex(int c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	else if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+	else if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
+	return -1;
+}
+
+static unsigned int url_decode(char *str, unsigned int len)
+{
+	const char *s = str;
+	char *d = str;
+	int d1, d2;
+
+	for (; len && *s != '\0' && *s != '%'; ++d, ++s, --len);
+	if (!len || *s == '\0')
+		return d - str;
+
+	while (len && *s != '\0') {
+		if (*s == '%' && len > 2) {
+			/* Try to convert % escape */
+			d1 = tohex(s[1]), d2 = tohex(s[2]);
+
+			/* Look good, consume three chars */
+			if (d1 >= 0 && d2 >= 0) {
+				s += 3;
+				len -= 3;
+				*d++ = (d1 << 4) | d2;
+				continue;
+			}
+			/* Otherwise, treat '%' as normal char */
+		}
+		*d++ = *s++;
+		--len;
+	}
+	return d - str;
+}
+
 int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 			      struct erofs_pax_header *eh, u32 size)
 {
@@ -454,7 +495,7 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 		goto out;
 
 	while (p < buf + size) {
-		char *kv, *value;
+		char *kv, *key, *value;
 		int len, n;
 		/* extended records are of the format: "LEN NAME=VALUE\n" */
 		ret = sscanf(p, "%d %n", &len, &n);
@@ -537,8 +578,7 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 				eh->use_gid = true;
 			} else if (!strncmp(kv, "SCHILY.xattr.",
 				   sizeof("SCHILY.xattr.") - 1)) {
-				char *key = kv + sizeof("SCHILY.xattr.") - 1;
-
+				key = kv + sizeof("SCHILY.xattr.") - 1;
 				--len; /* p[-1] == '\0' */
 				ret = tarerofs_insert_xattr(&eh->xattrs, key,
 						value - key - 1,
@@ -547,9 +587,10 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 					goto out;
 			} else if (!strncmp(kv, "LIBARCHIVE.xattr.",
 				   sizeof("LIBARCHIVE.xattr.") - 1)) {
-				char *key;
-				key = kv + sizeof("LIBARCHIVE.xattr.") - 1;
+				int namelen;
 
+				key = kv + sizeof("LIBARCHIVE.xattr.") - 1;
+				namelen = url_decode(key, value - key - 1);
 				--len; /* p[-1] == '\0' */
 				ret = base64_decode(value, len - (value - kv),
 						    (u8 *)value);
@@ -558,9 +599,13 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 					goto out;
 				}
 
+				if (namelen != value - key - 1) {
+					key[namelen] = '=';
+					memmove(key + namelen + 1, value, ret);
+					value = key + namelen + 1;
+				}
 				ret = tarerofs_insert_xattr(&eh->xattrs, key,
-						value - key - 1,
-						value - key + ret, false);
+						namelen, namelen + 1 + ret, false);
 				if (ret)
 					goto out;
 			} else {
Signature · 1 line
2.39.3 (Apple Git-146)