Skip to content

Commit 80e6480

Browse files
thejhaxboe
authored andcommitted
partitions: mac: fix handling of bogus partition table
Fix several issues in partition probing: - The bailout for a bad partoffset must use put_dev_sector(), since the preceding read_part_sector() succeeded. - If the partition table claims a silly sector size like 0xfff bytes (which results in partition table entries straddling sector boundaries), bail out instead of accessing out-of-bounds memory. - We must not assume that the partition table contains proper NUL termination - use strnlen() and strncmp() instead of strlen() and strcmp(). Cc: stable@vger.kernel.org Signed-off-by: Jann Horn <jannh@google.com> Link: https://lore.kernel.org/r/20250214-partition-mac-v1-1-c1c626dffbd5@google.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 1f47ed2 commit 80e6480

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

block/partitions/mac.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,25 @@ int mac_partition(struct parsed_partitions *state)
5353
}
5454
secsize = be16_to_cpu(md->block_size);
5555
put_dev_sector(sect);
56+
57+
/*
58+
* If the "block size" is not a power of 2, things get weird - we might
59+
* end up with a partition straddling a sector boundary, so we wouldn't
60+
* be able to read a partition entry with read_part_sector().
61+
* Real block sizes are probably (?) powers of two, so just require
62+
* that.
63+
*/
64+
if (!is_power_of_2(secsize))
65+
return -1;
5666
datasize = round_down(secsize, 512);
5767
data = read_part_sector(state, datasize / 512, &sect);
5868
if (!data)
5969
return -1;
6070
partoffset = secsize % 512;
61-
if (partoffset + sizeof(*part) > datasize)
71+
if (partoffset + sizeof(*part) > datasize) {
72+
put_dev_sector(sect);
6273
return -1;
74+
}
6375
part = (struct mac_partition *) (data + partoffset);
6476
if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
6577
put_dev_sector(sect);
@@ -112,8 +124,8 @@ int mac_partition(struct parsed_partitions *state)
112124
int i, l;
113125

114126
goodness++;
115-
l = strlen(part->name);
116-
if (strcmp(part->name, "/") == 0)
127+
l = strnlen(part->name, sizeof(part->name));
128+
if (strncmp(part->name, "/", sizeof(part->name)) == 0)
117129
goodness++;
118130
for (i = 0; i <= l - 4; ++i) {
119131
if (strncasecmp(part->name + i, "root",

0 commit comments

Comments
 (0)