Skip to content

Commit e37d021

Browse files
Avnish ChouhanDaniel Kiper
authored andcommitted
kern/ieee1275/openfw: Add a check for invalid partition number
The grub_strtoul() may fail in several scenarios like invalid input, overflow, etc. Lack of proper check may lead to unexpected failures in the code further. Signed-off-by: Avnish Chouhan <avnish@linux.ibm.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1 parent f94eae0 commit e37d021

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

grub-core/kern/ieee1275/openfw.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,28 @@ grub_ieee1275_encode_devname (const char *path)
512512
}
513513
if (partition && partition[0])
514514
{
515-
unsigned int partno = grub_strtoul (partition, 0, 0);
515+
unsigned long partno;
516+
const char *endptr;
517+
518+
partno = grub_strtoul (partition, &endptr, 0);
519+
grub_errno = GRUB_ERR_NONE;
520+
if (*endptr != '\0' || partno > 65535 ||
521+
(partno == 0 && ! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS)))
522+
{
523+
grub_free (partition);
524+
grub_free (device);
525+
grub_free (encoding);
526+
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid partition number"));
527+
return NULL;
528+
}
516529

517530
*optr++ = ',';
518531

519532
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
520533
/* GRUB partition 1 is OF partition 0. */
521534
partno++;
522535

523-
grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
536+
grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%lu", partno);
524537
}
525538
else
526539
*optr = '\0';

0 commit comments

Comments
 (0)