erofs-utils: lib: tar: fix LIBARCHIVE.xattr base64 decoding
authorGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 26 Dec 2024 04:58:07 +0000 (12:58 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 26 Dec 2024 05:21:50 +0000 (13:21 +0800)
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 <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20241226045808.95101-2-hsiangkao@linux.alibaba.com
lib/tar.c

index 12bf595fe5578de29986ec4ca0921b6248d23915..9642e2e4895d6482836f3f6fc90de0f22b93d2ff 100644 (file)
--- 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;
 }