From: Thomas Weißschuh Date: Mon, 26 Dec 2022 23:57:34 +0000 (+0000) Subject: libblkid: bsd: add checksum support X-Git-Tag: v2.39-rc1~353^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c8cc50647261cd7f8181c9390850c25e7a001d0d;p=thirdparty%2Futil-linux.git libblkid: bsd: add checksum support --- diff --git a/libblkid/src/partitions/bsd.c b/libblkid/src/partitions/bsd.c index 7a0b23195e..ba120195ea 100644 --- a/libblkid/src/partitions/bsd.c +++ b/libblkid/src/partitions/bsd.c @@ -26,6 +26,18 @@ #define BLKID_MAG_LASTOFFSET(_mag) \ (BLKID_MAG_OFFSET(_mag) - (BLKID_MAG_SECTOR(_mag) << 9)) +static uint16_t bsd_checksum(const struct bsd_disklabel *l) +{ + uint16_t v, csum = 0; + const char *end = (const char *) (l + 1); + + for (const char *c = (const char *) l; c < end; c += sizeof(uint16_t)) { + memcpy(&v, c, sizeof(v)); + csum ^= v; + } + return csum ^ le16_to_cpu(l->d_checksum); +} + static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag) { struct bsd_disklabel *l; @@ -52,6 +64,11 @@ static int probe_bsd_pt(blkid_probe pr, const struct blkid_idmag *mag) l = (struct bsd_disklabel *) (data + BLKID_MAG_LASTOFFSET(mag)); + if (!blkid_probe_verify_csum(pr, bsd_checksum(l), le16_to_cpu(l->d_checksum))) { + rc = BLKID_PROBE_NONE; + goto nothing; + } + ls = blkid_probe_get_partlist(pr); if (!ls) goto nothing;