From c8cc50647261cd7f8181c9390850c25e7a001d0d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Mon, 26 Dec 2022 23:57:34 +0000 Subject: [PATCH] libblkid: bsd: add checksum support --- libblkid/src/partitions/bsd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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; -- 2.47.3