]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: bsd: add checksum support
authorThomas Weißschuh <thomas@t-8ch.de>
Mon, 26 Dec 2022 23:57:34 +0000 (23:57 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Mon, 26 Dec 2022 23:57:48 +0000 (23:57 +0000)
libblkid/src/partitions/bsd.c

index 7a0b23195e8e0b3387bcec48f699a4389185b785..ba120195ea08bc9a1aa988de02aa5fdeb797e7ce 100644 (file)
 #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;