Skip to content

Commit

Permalink
Merge branch 'libblkid/ddf' of https://github.com/t-8ch/util-linux
Browse files Browse the repository at this point in the history
* 'libblkid/ddf' of https://github.com/t-8ch/util-linux:
  libblkid: ddf_read: validate header checksum
  libc/crc32: make fill value of excluded area configurable
  libblkid: ddf_raid: drop little-endian handling
  libblkid: ddf_raid: respect constness of buffer
  • Loading branch information
karelzak committed Mar 3, 2025
2 parents fcc7d76 + e60a965 commit 8a69523
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion include/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

extern uint32_t ul_crc32(uint32_t seed, const unsigned char *buf, size_t len);
extern uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t len,
size_t exclude_off, size_t exclude_len);
size_t exclude_off, size_t exclude_len,
uint8_t exclude_fill);

#endif

4 changes: 2 additions & 2 deletions lib/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ uint32_t ul_crc32(uint32_t seed, const unsigned char *buf, size_t len)
}

uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t len,
size_t exclude_off, size_t exclude_len)
size_t exclude_off, size_t exclude_len, uint8_t exclude_fill)
{
uint32_t crc = seed;
const unsigned char *p = buf;
Expand All @@ -132,7 +132,7 @@ uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t
unsigned char x = *p++;

if (i >= exclude_off && i < exclude_off + exclude_len)
x = 0;
x = exclude_fill;

crc = crc32_add_char(crc, x);
}
Expand Down
2 changes: 1 addition & 1 deletion libblkid/src/partitions/gpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct gpt_entry {
static inline uint32_t count_crc32(const unsigned char *buf, size_t len,
size_t exclude_off, size_t exclude_len)
{
return (ul_crc32_exclude_offset(~0L, buf, len, exclude_off, exclude_len) ^ ~0L);
return (ul_crc32_exclude_offset(~0L, buf, len, exclude_off, exclude_len, 0) ^ ~0L);
}

static inline const unsigned char *get_lba_buffer(blkid_probe pr,
Expand Down
2 changes: 1 addition & 1 deletion libblkid/src/superblocks/cramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static int cramfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag,

crc = ~ul_crc32_exclude_offset(~0LL, csummed, csummed_size,
offsetof(struct cramfs_super, info.crc),
sizeof_member(struct cramfs_super, info.crc));
sizeof_member(struct cramfs_super, info.crc), 0);

return blkid_probe_verify_csum(pr, crc, expected);
}
Expand Down
29 changes: 21 additions & 8 deletions libblkid/src/superblocks/ddf_raid.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string.h>
#include <stdint.h>

#include "crc32.h"
#include "superblocks.h"

/* http://www.snia.org/standards/home */
Expand Down Expand Up @@ -71,35 +72,47 @@ struct ddf_header {
uint8_t pad4[256]; /* 0xff */
} __attribute__((packed));

static int ddf_verify_csum(blkid_probe pr, const struct ddf_header *ddf)
{
uint32_t expected, crc;

expected = be32_to_cpu(ddf->crc);

crc = ul_crc32_exclude_offset(0,
(const unsigned char *)ddf, sizeof(*ddf),
offsetof(__typeof__(*ddf), crc),
sizeof_member(__typeof__(*ddf), crc),
0xff);

return blkid_probe_verify_csum(pr, crc, expected);
}

static int probe_ddf(blkid_probe pr,
const struct blkid_idmag *mag __attribute__((__unused__)))
{
int hdrs[] = { 1, 257 };
size_t i;
struct ddf_header *ddf = NULL;
const struct ddf_header *ddf = NULL;
char version[DDF_REV_LENGTH + 1];
uint64_t off = 0, lba;

for (i = 0; i < ARRAY_SIZE(hdrs); i++) {
off = ((pr->size / 0x200) - hdrs[i]) * 0x200;

ddf = (struct ddf_header *) blkid_probe_get_buffer(pr,
ddf = (const struct ddf_header *) blkid_probe_get_buffer(pr,
off,
sizeof(struct ddf_header));
if (!ddf)
return errno ? -errno : 1;
if (ddf->signature == cpu_to_be32(DDF_MAGIC) ||
ddf->signature == cpu_to_le32(DDF_MAGIC))
if (ddf->signature == cpu_to_be32(DDF_MAGIC) && ddf_verify_csum(pr, ddf))
break;
ddf = NULL;
}

if (!ddf)
return 1;

lba = ddf->signature == cpu_to_be32(DDF_MAGIC) ?
be64_to_cpu(ddf->primary_lba) :
le64_to_cpu(ddf->primary_lba);
lba = be64_to_cpu(ddf->primary_lba);

if (lba > 0) {
/* check primary header */
Expand All @@ -123,7 +136,7 @@ static int probe_ddf(blkid_probe pr,
return 1;
if (blkid_probe_set_magic(pr, off,
sizeof(ddf->signature),
(unsigned char *) &ddf->signature))
(const unsigned char *) &ddf->signature))
return 1;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion libblkid/src/superblocks/zonefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static int zonefs_verify_csum(blkid_probe pr, const struct zonefs_super *sb)
uint32_t expected = le32_to_cpu(sb->s_crc);
uint32_t crc = ul_crc32_exclude_offset(
~0LL, (unsigned char *) sb, sizeof(*sb),
offsetof(__typeof__(*sb), s_crc), sizeof(sb->s_crc));
offsetof(__typeof__(*sb), s_crc), sizeof(sb->s_crc), 0);
return blkid_probe_verify_csum(pr, crc, expected);
}

Expand Down
2 changes: 1 addition & 1 deletion libfdisk/src/gpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ static unsigned char *gpt_read_entries(struct fdisk_context *cxt,
static inline uint32_t count_crc32(const unsigned char *buf, size_t len,
size_t ex_off, size_t ex_len)
{
return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len) ^ ~0L);
return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len, 0) ^ ~0L);
}

static inline uint32_t gpt_header_count_crc32(struct gpt_header *header)
Expand Down

0 comments on commit 8a69523

Please sign in to comment.