From: Gao Xiang Date: Thu, 26 Dec 2024 04:58:07 +0000 (+0800) Subject: erofs-utils: lib: tar: fix LIBARCHIVE.xattr base64 decoding X-Git-Tag: accepted/tizen/unified/20250610.081809~76 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5e771458ef72fa958da1c31cb42e1086033bcbf9;p=platform%2Fupstream%2Ferofs-utils.git erofs-utils: lib: tar: 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") Signed-off-by: Gao Xiang Link: https://lore.kernel.org/r/20241226045808.95101-2-hsiangkao@linux.alibaba.com --- diff --git a/lib/tar.c b/lib/tar.c index 12bf595..9642e2e 100644 --- a/lib/tar.c +++ b/lib/tar.c @@ -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; }