disk/lvm: Don't blast past the end of the circular metadata buffer

This catches at least some OOB reads, and it's possible I suppose that
if 2 * mda_size is less than GRUB_LVM_MDA_HEADER_SIZE it might catch some
OOB writes too (although that hasn't showed up as a crash in fuzzing yet).

It's a bit ugly and I'd appreciate better suggestions.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Daniel Axtens 2021-01-21 18:19:51 +11:00 committed by Daniel Kiper
parent a8cc95de74
commit 27a79bf38e

View File

@ -251,6 +251,16 @@ grub_lvm_detect (grub_disk_t disk,
if (grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) >
grub_le_to_cpu64 (mdah->size))
{
if (2 * mda_size < GRUB_LVM_MDA_HEADER_SIZE ||
(grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) -
grub_le_to_cpu64 (mdah->size) > mda_size - GRUB_LVM_MDA_HEADER_SIZE))
{
#ifdef GRUB_UTIL
grub_util_info ("cannot copy metadata wrap in circular buffer");
#endif
goto fail2;
}
/* Metadata is circular. Copy the wrap in place. */
grub_memcpy (metadatabuf + mda_size,
metadatabuf + GRUB_LVM_MDA_HEADER_SIZE,