]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: cramfs: handle cross-endianess for checksums
authorThomas Weißschuh <thomas@t-8ch.de>
Mon, 24 Oct 2022 14:48:18 +0000 (16:48 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Mon, 24 Oct 2022 14:56:00 +0000 (16:56 +0200)
libblkid/src/superblocks/cramfs.c

index 094367d4c653dcc0b30164444f4a1d4d7c5c3c06..76b5611e9a639e0a5b0c9f7d28d2cd63d02ba6f1 100644 (file)
@@ -37,16 +37,31 @@ struct cramfs_super
 
 #define CRAMFS_FLAG_FSID_VERSION_2     0x00000001      /* fsid version #2 */
 
+static int cramfs_is_little_endian(const struct blkid_idmag *mag)
+{
+       assert(mag->len == 4);
+       return memcmp(mag->magic, "\x45\x3d\xcd\x28", 4) == 0;
+}
+
+static uint32_t cfs32_to_cpu(int le, uint32_t value)
+{
+       if (le)
+               return le32_to_cpu(value);
+       else
+               return be32_to_cpu(value);
+}
+
 static int cramfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag, struct cramfs_super *cs)
 {
        uint32_t crc, expected, csummed_size;
        unsigned char *csummed;
+       int le = cramfs_is_little_endian(mag);
 
-       if (!(cs->flags & CRAMFS_FLAG_FSID_VERSION_2))
+       if (!(cfs32_to_cpu(le, cs->flags) & CRAMFS_FLAG_FSID_VERSION_2))
                return 1;
 
-       expected = le32_to_cpu(cs->info.crc);
-       csummed_size = le32_to_cpu(cs->size);
+       expected = cfs32_to_cpu(le, cs->info.crc);
+       csummed_size = cfs32_to_cpu(le, cs->size);
 
        if (csummed_size > (1 << 16)
            || csummed_size < sizeof(struct cramfs_super))