disk: part_efi: fix two out-of-bounds reads on crafted GPT
PATCH 1/1 disk: part_efi: fix two out-of-bounds reads on crafted GPT
Two attacker-controlled header fields are used without bounds when parsing a GPT, which is done automatically while probing a block device:
1) alloc_read_gpt_entries(): count = num_partition_entries * sizeof_partition_entry is evaluated in 32-bit and wraps (even on 64-bit), under-sizing the entry buffer while part_print_efi() / part_get_info_efi() iterate the full num_partition_entries with a sizeof(gpt_entry) stride -> heap OOB read and partition-name info leak (CWE-190/125). Compute in size_t so an oversized array fails allocation, and require sizeof_partition_entry == sizeof(gpt_entry) so the buffer and the iteration stride agree.
2) validate_gpt_header(): efi_crc32(gpt_h, header_size) runs over an attacker-controlled header_size on a single-block buffer -> OOB read of up to ~4 GiB before the CRC is even compared (CWE-125). Bound header_size to [sizeof(gpt_header), block_size] in is_gpt_valid().
--- disk/part_efi.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
disk/part_efi.c +29 -1
@@ -1095,6 +1095,17 @@ static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,return 2 ; } +/* +* header_size is used as the length for the header CRC. Bound it to the +* block that was read so efi_crc32() cannot run off the buffer. +*/ +if (le32_to_cpu (pgpt_head->header_size) <sizeof (gpt_header) || +le32_to_cpu (pgpt_head->header_size) > desc->blksz) { +log_debug ("GPT: invalid header_size %u\n" , +le32_to_cpu (pgpt_head->header_size)); +return 0 ; + } +if (validate_gpt_header (pgpt_head, (lbaint_t )lba, desc->lba))return 0 ;
@@ -1177,7 +1188,24 @@ static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,return NULL ; } - count =le32_to_cpu (pgpt_head->num_partition_entries) * +/* +* Both fields are u32, so the product is evaluated in 32-bit and can +* wrap (even on 64-bit builds), under-sizing the allocation while the +* callers still iterate the full num_partition_entries. Compute in +* 64-bit so an oversized array fails allocation rather than overflowing. +*/ +/* +* The entry array is indexed as gpt_entry[i] (sizeof(gpt_entry) stride), +* so the on-disk entry size must match or the buffer and the iteration +* disagree. Reject anything else. +*/ +if (le32_to_cpu (pgpt_head->sizeof_partition_entry) !=sizeof (gpt_entry)) { +log_debug ("GPT: unexpected sizeof_partition_entry %u\n" , +le32_to_cpu (pgpt_head->sizeof_partition_entry)); +return NULL ; + } + + count = (size_t )le32_to_cpu (pgpt_head->num_partition_entries) *le32_to_cpu (pgpt_head->sizeof_partition_entry); log_debug("count = %u * %u = %lu\n",
Signature · 1 line
2.53.0