Skip to contentSkip to conversation

lkdtm: Missing kmalloc check

PATCH In review Vasyl Gomonovych 3 participants 3 messages Nov 23, 2017 – Nov 27, 2017 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lkdtm: Missing kmalloc check

Handling a possible memory allocation failure.

---
 drivers/misc/lkdtm_heap.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/misc/lkdtm_heap.c +4 -0
index f5494a6d4be5..65026d7de130 100644
@@ -16,6 +16,8 @@ void lkdtm_OVERWRITE_ALLOCATION(void)
 {
 	size_t len = 1020;
 	u32 *data = kmalloc(len, GFP_KERNEL);
+	if (!data)
+		return;
 
 	data[1024 / sizeof(u32)] = 0x12345678;
 	kfree(data);
@@ -33,6 +35,8 @@ void lkdtm_WRITE_AFTER_FREE(void)
 	size_t offset = (len / sizeof(*base)) / 2;
 
 	base = kmalloc(len, GFP_KERNEL);
+	if (!base)
+		return;
 	pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
 	pr_info("Attempting bad write to freed memory at %p\n",
 		&base[offset]);
Signature · 1 line
1.9.1
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Vasyl Gomonovych

Handling a possible memory allocation failure.

Greg, can you please take this?

-Kees

Quoted 29 lines
---
 drivers/misc/lkdtm_heap.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/misc/lkdtm_heap.c +4 -1
index f5494a6d4be5..65026d7de130 100644
@@ -16,6 +16,8 @@ void lkdtm_OVERWRITE_ALLOCATION(void)
 {
        size_t len = 1020;
        u32 *data = kmalloc(len, GFP_KERNEL);
+       if (!data)
+               return;
 
        data[1024 / sizeof(u32)] = 0x12345678;
        kfree(data);
@@ -33,6 +35,8 @@ void lkdtm_WRITE_AFTER_FREE(void)
        size_t offset = (len / sizeof(*base)) / 2;
 
        base = kmalloc(len, GFP_KERNEL);
+       if (!base)
+               return;
        pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
        pr_info("Attempting bad write to freed memory at %p\n",
                &base[offset]);
--

1.9.1

Signature · 2 lines
Kees Cook
Pixel Security