@@ -99,6 +99,21 @@ bool volume_read(struct volume *volume, void *buffer, uint64_t loc, uint64_t cou
9999 return true;
100100}
101101
102+ static bool partition_range_valid (struct volume * volume ,
103+ uint64_t first_sect , uint64_t sect_count ) {
104+ if (sect_count == 0 ) {
105+ return false;
106+ }
107+
108+ uint64_t end_sect = CHECKED_ADD (first_sect , sect_count , return false );
109+
110+ if (volume -> sect_count != (uint64_t )-1 && end_sect > volume -> sect_count ) {
111+ return false;
112+ }
113+
114+ return true;
115+ }
116+
102117struct gpt_table_header {
103118 // the head
104119 char signature [8 ];
@@ -245,6 +260,10 @@ static int gpt_get_part(struct volume *ret, struct volume *volume, int partition
245260 uint64_t partition_blocks = partition_size + 1 ;
246261 uint64_t sect_count_result = CHECKED_MUL (partition_blocks , sect_multiplier , return NO_PARTITION );
247262
263+ if (!partition_range_valid (volume , first_sect_result , sect_count_result )) {
264+ return NO_PARTITION ;
265+ }
266+
248267#if defined (UEFI )
249268 ret -> efi_handle = volume -> efi_handle ;
250269 ret -> block_io = volume -> block_io ;
@@ -405,10 +424,15 @@ static int mbr_get_logical_part(struct volume *ret, struct volume *extended_part
405424 return NO_PARTITION ;
406425 }
407426
408- // Check for overflow in first_sect calculation
409- uint64_t first_sect_64 = CHECKED_ADD (extended_part -> first_sect , ebr_sector , return NO_PARTITION );
410- first_sect_64 = CHECKED_ADD (first_sect_64 , entry .first_sect , return NO_PARTITION );
411- (void )CHECKED_ADD (first_sect_64 , entry .sect_count , return NO_PARTITION );
427+ uint64_t logical_rel_first = CHECKED_ADD (ebr_sector , entry .first_sect , return NO_PARTITION );
428+ if (!partition_range_valid (extended_part , logical_rel_first , entry .sect_count )) {
429+ return NO_PARTITION ;
430+ }
431+
432+ uint64_t first_sect_64 = CHECKED_ADD (extended_part -> first_sect , logical_rel_first , return NO_PARTITION );
433+ if (!partition_range_valid (extended_part -> backing_dev , first_sect_64 , entry .sect_count )) {
434+ return NO_PARTITION ;
435+ }
412436
413437#if defined (UEFI )
414438 ret -> efi_handle = extended_part -> efi_handle ;
@@ -469,6 +493,10 @@ static int mbr_get_part(struct volume *ret, struct volume *volume, int partition
469493 continue ;
470494 }
471495
496+ if (!partition_range_valid (volume , entry .first_sect , entry .sect_count )) {
497+ continue ;
498+ }
499+
472500 struct volume extended_part = {0 };
473501
474502#if defined (UEFI )
@@ -506,6 +534,10 @@ static int mbr_get_part(struct volume *ret, struct volume *volume, int partition
506534 return NO_PARTITION ;
507535 }
508536
537+ if (!partition_range_valid (volume , entry .first_sect , entry .sect_count )) {
538+ return NO_PARTITION ;
539+ }
540+
509541#if defined (UEFI )
510542 ret -> efi_handle = volume -> efi_handle ;
511543 ret -> block_io = volume -> block_io ;
0 commit comments