Skip to contentSkip to conversation

riscv: Prevent NULL pointer dereference in machine_kexec_prepare()

PATCH New Tao Liu 1 participant 1 message Jul 5

Subsystems (6)
lore
Share & tools
lore

Tao Liu

author
#0 lore raw

PATCH v5 1/1 riscv: Prevent NULL pointer dereference in machine_kexec_prepare()

A NULL pointer dereference issue is noticed in riscv's machine_kexec_prepare(), where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from ima_add_kexec_buffer(), where kbuf is added by kexec_add_buffer(), but kbuf.buffer is NULL, then it is copied without a check in machine_kexec_prepare():

kexec_file_load

    -> kimage_file_alloc_init()
       -> kimage_file_prepare_segments()
          -> ima_add_kexec_buffer()
             -> kexec_add_buffer()
    -> machine_kexec_prepare()
       -> memcpy()

Address this by adding a check before the data copy attempt.

---

v5 -> v4: 1) Add parentheses to function name in patch subject

link to v1: https://lore.kernel.org/linux-riscv/[email protected]/
link to v2: https://lore.kernel.org/linux-riscv/[email protected]/
link to v3: https://lore.kernel.org/linux-riscv/[email protected]/
link to v4: https://lore.kernel.org/linux-riscv/[email protected]/

---
 arch/riscv/kernel/machine_kexec.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/machine_kexec.c +3 -0
index 2306ce3e5f22..738df176ff6f 100644
@@ -41,6 +41,9 @@ machine_kexec_prepare(struct kimage *image)
 		if (image->segment[i].memsz <= sizeof(fdt))
 			continue;
 
+		if (!image->segment[i].buf)
+			continue;
+
 		if (image->file_mode)
 			memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
 		else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
Signature · 1 line
2.54.0