From: Thomas Weißschuh Date: Thu, 13 Oct 2022 22:33:54 +0000 (+0200) Subject: libblkid: cramfs: add checksum support X-Git-Tag: v2.39-rc1~482^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3703ce084235a92b8b1c143bef3089c8b3e022b9;p=thirdparty%2Futil-linux.git libblkid: cramfs: add checksum support --- diff --git a/libblkid/src/superblocks/cramfs.c b/libblkid/src/superblocks/cramfs.c index 2c41cc5deb..fd893fc188 100644 --- a/libblkid/src/superblocks/cramfs.c +++ b/libblkid/src/superblocks/cramfs.c @@ -16,6 +16,7 @@ #include #include "superblocks.h" +#include "crc32.h" struct cramfs_super { @@ -34,6 +35,24 @@ struct cramfs_super uint8_t name[16]; } __attribute__((packed)); +#define CRAMFS_FLAG_FSID_VERSION_2 0x00000001 /* fsid version #2 */ + +static int cramfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag, struct cramfs_super *cs) +{ + if (!(cs->flags & CRAMFS_FLAG_FSID_VERSION_2)) + return 1; + + uint32_t expected = le32_to_cpu(cs->info.crc); + uint32_t csummed_size = le32_to_cpu(cs->size); + if (csummed_size > 1 << 16) + return 0; + + unsigned char *csummed = blkid_probe_get_sb_buffer(pr, mag, csummed_size); + memset(csummed + offsetof(struct cramfs_super, info.crc), 0, sizeof(uint32_t)); + uint32_t crc = ~ul_crc32(~0LL, csummed, csummed_size); + return blkid_probe_verify_csum(pr, crc, expected); +} + static int probe_cramfs(blkid_probe pr, const struct blkid_idmag *mag) { struct cramfs_super *cs; @@ -42,6 +61,9 @@ static int probe_cramfs(blkid_probe pr, const struct blkid_idmag *mag) if (!cs) return errno ? -errno : 1; + if (!cramfs_verify_csum(pr, mag, cs)) + return 1; + blkid_probe_set_label(pr, cs->name, sizeof(cs->name)); return 0; }