summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
authorbernard <[email protected]>2026-11-03 07:17:49 +0800
committerRbb666 <[email protected]>2026-11-04 21:30:29 +0800
commitd59285201ac87c2a4942c1185b60e9c586fc1382 (patch)
tree3860ec4135d04dbfcba2072398e7f03ce3bd98ee /components/drivers
parent1f024e372ba4a26478e28dfd54657d87689b2e96 (diff)
[drivers][block] reject GPT entries with ending_lba before starting_lba
is_pte_valid() accepted a GPT entry as long as both LBAs were inside the disk, so a crafted image could set ending_lba < starting_lba. The size computed in efi_partition() then underflowed to a huge value and was registered as the logical partition size. Reject entries whose end LBA is below the start LBA, and compute the partition start/size only after the entry passed validation. This avoids evaluating the underflow at all and reuses the validated start value for the size calculation. Fixes RT-Thread/rt-thread#11271. Co-authored-by: DeepSeek 4.1 Flash <[email protected]>
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/block/partitions/efi.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/components/drivers/block/partitions/efi.c b/components/drivers/block/partitions/efi.c
index 522d5f6076..6d3ebf7f53 100644
--- a/components/drivers/block/partitions/efi.c
+++ b/components/drivers/block/partitions/efi.c
@@ -545,9 +545,13 @@ _fail:
*/
rt_inline rt_bool_t is_pte_valid(const gpt_entry *pte, const rt_size_t lastlba)
{
+ rt_uint64_t start = rt_le64_to_cpu(pte->starting_lba);
+ rt_uint64_t end = rt_le64_to_cpu(pte->ending_lba);
+
if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
- rt_le64_to_cpu(pte->starting_lba) > lastlba ||
- rt_le64_to_cpu(pte->ending_lba) > lastlba)
+ start > lastlba ||
+ end > lastlba ||
+ end < start)
{
return RT_FALSE;
}
@@ -823,15 +827,17 @@ rt_err_t efi_partition(struct rt_blk_disk *disk)
for (int i = 0; i < entries_nr && i < disk->max_partitions; ++i)
{
- rt_uint64_t start = rt_le64_to_cpu(ptes[i].starting_lba);
- rt_uint64_t size = rt_le64_to_cpu(ptes[i].ending_lba) -
- rt_le64_to_cpu(ptes[i].starting_lba) + 1ULL;
+ rt_uint64_t start;
+ rt_uint64_t size;
if (!is_pte_valid(&ptes[i], last_lba(disk)))
{
continue;
}
+ start = rt_le64_to_cpu(ptes[i].starting_lba);
+ size = rt_le64_to_cpu(ptes[i].ending_lba) - start + 1ULL;
+
if (blk_put_partition(disk, "gpt", start, size, i) == -RT_ENOMEM)
{
break;