]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Fix size_t cast in read_mac_metadata_blob 1570/head
authorSamanta Navarro <ferivoz@riseup.net>
Sat, 28 Aug 2021 11:58:00 +0000 (11:58 +0000)
committerSamanta Navarro <ferivoz@riseup.net>
Sat, 28 Aug 2021 11:58:20 +0000 (11:58 +0000)
The size_t data type on 32 bit systems is smaller than int64_t. Check
the int64_t value before casting to size_t. If the value is too large
then stop operation instead of continuing operation with truncated
value.

libarchive/archive_read_support_format_tar.c

index 7e8febacf68607fd8298234000a02651da89457e..773796a5d86296844126512fefaf06f9c5a601e6 100644 (file)
@@ -1396,6 +1396,7 @@ read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
     struct archive_entry *entry, const void *h, size_t *unconsumed)
 {
        int64_t size;
+       size_t msize;
        const void *data;
        const char *p, *name;
        const wchar_t *wp, *wname;
@@ -1434,6 +1435,11 @@ read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
 
        /* Read the body as a Mac OS metadata blob. */
        size = archive_entry_size(entry);
+       msize = (size_t)size;
+       if (size < 0 || (uintmax_t)msize != (uintmax_t)size) {
+               *unconsumed = 0;
+               return (ARCHIVE_FATAL);
+       }
 
        /*
         * TODO: Look beyond the body here to peek at the next header.
@@ -1447,13 +1453,13 @@ read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
         * Q: Is the above idea really possible?  Even
         * when there are GNU or pax extension entries?
         */
-       data = __archive_read_ahead(a, (size_t)size, NULL);
+       data = __archive_read_ahead(a, msize, NULL);
        if (data == NULL) {
                *unconsumed = 0;
                return (ARCHIVE_FATAL);
        }
-       archive_entry_copy_mac_metadata(entry, data, (size_t)size);
-       *unconsumed = (size_t)((size + 511) & ~ 511);
+       archive_entry_copy_mac_metadata(entry, data, msize);
+       *unconsumed = (msize + 511) & ~ 511;
        tar_flush_unconsumed(a, unconsumed);
        return (tar_read_header(a, tar, entry, unconsumed));
 }