Skip to contentSkip to conversation

kho: validate order in deserialize_bitmap()

PATCH In review Marco Elver 2 participants 2 messages Feb 14 – Feb 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kho: validate order in deserialize_bitmap()

The function deserialize_bitmap() calculates the reservation size using:

    int sz = 1 << (order + PAGE_SHIFT);

If a corrupted KHO image provides an order >= 20 (on systems with 4KB pages), the shift amount becomes >= 32, which overflows the 32-bit integer. This results in a zero-size memory reservation.

Furthermore, the physical address calculation:

    phys_addr_t phys = elm->phys_start + (bit << (order + PAGE_SHIFT));

can also overflow and wrap around if the order is large. This allows a corrupt KHO image to cause out-of-bounds updates to page->private of arbitrary physical pages during early boot.

Fix this by adding a bounds check for the order field.

Fixes fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
---
 kernel/liveupdate/kexec_handover.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/liveupdate/kexec_handover.c +5 -0
index b851b09a8e99..ec353e4b68a6 100644
@@ -463,6 +463,11 @@ static void __init deserialize_bitmap(unsigned int order,
 	struct kho_mem_phys_bits *bitmap = KHOSER_LOAD_PTR(elm->bitmap);
 	unsigned long bit;
 
+	if (order > MAX_PAGE_ORDER) {
+		pr_warn("invalid order %u for preserved bitmap\n", order);
+		return;
+	}
+
 	for_each_set_bit(bit, bitmap->preserve, PRESERVE_BITS) {
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
Signature · 1 line
2.53.0.335.g19a08e0c02-goog
maintainer
#1 lore raw

Hi Marco,

Quoted 29 lines · Marco Elver

The function deserialize_bitmap() calculates the reservation size using:

    int sz = 1 << (order + PAGE_SHIFT);

If a corrupted KHO image provides an order >= 20 (on systems with 4KB pages), the shift amount becomes >= 32, which overflows the 32-bit integer. This results in a zero-size memory reservation.

Furthermore, the physical address calculation:

    phys_addr_t phys = elm->phys_start + (bit << (order + PAGE_SHIFT));

can also overflow and wrap around if the order is large. This allows a corrupt KHO image to cause out-of-bounds updates to page->private of arbitrary physical pages during early boot.

Fix this by adding a bounds check for the order field.

Fixes fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
---
 kernel/liveupdate/kexec_handover.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/liveupdate/kexec_handover.c +0 -0
index b851b09a8e99..ec353e4b68a6 100644

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 466

Quoted 4 lines
kernel/liveupdate/kexec_handover.c +1 -0
@@ -463,6 +463,11 @@ static void __init deserialize_bitmap(unsigned int order,
 	struct kho_mem_phys_bits *bitmap = KHOSER_LOAD_PTR(elm->bitmap);
 	unsigned long bit;
 
+	if (order > MAX_PAGE_ORDER) {

Preserved order can be larger than MAX_PAGE_ORDER.
Let's make 'sz' unsigned long and add checks that calculations won't overflow.

Quoted 9 lines
kernel/liveupdate/kexec_handover.c +4 -0
+		pr_warn("invalid order %u for preserved bitmap\n", order);
+		return;
+	}
+
 	for_each_set_bit(bit, bitmap->preserve, PRESERVE_BITS) {
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
Signature · 1 line
2.53.0.335.g19a08e0c02-goog
Signature · 2 lines
Sincerely yours,
Mike.