Skip to contentSkip to conversation

lkdtm/heap: Avoid __alloc_size hint warning

PATCH In review Kees Cook 2 participants 3 messages Aug 18, 2021

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 lkdtm/heap: Avoid __alloc_size hint warning

Once __alloc_size hints have been added, the compiler will (correctly!) see this as an overflow. We are, however, trying to test for this condition, so work around it with a volatile int.

---
 drivers/misc/lkdtm/heap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/heap.c +3 -1
index 3d9aae5821a0..e59fcbe00ae0 100644
@@ -12,6 +12,8 @@ static struct kmem_cache *double_free_cache;
 static struct kmem_cache *a_cache;
 static struct kmem_cache *b_cache;
 
+static volatile int __offset = 1;
+
 /*
  * If there aren't guard pages, it's likely that a consecutive allocation will
  * let us overflow into the second allocation without overwriting something real.
@@ -24,7 +26,7 @@ void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
 	two = vzalloc(PAGE_SIZE);
 
 	pr_info("Attempting vmalloc linear overflow ...\n");
-	memset(one, 0xAA, PAGE_SIZE + 1);
+	memset(one, 0xAA, PAGE_SIZE + __offset);
 
 	vfree(two);
 	vfree(one);
Signature · 1 line
2.30.2
#1 lore raw
Quoted 14 lines · Kees Cook

Once __alloc_size hints have been added, the compiler will (correctly!) see this as an overflow. We are, however, trying to test for this condition, so work around it with a volatile int.

---
 drivers/misc/lkdtm/heap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/heap.c +0 -0
index 3d9aae5821a0..e59fcbe00ae0 100644

drivers/misc/lkdtm/heap.c static struct kmem_cache *double_free_cache; · line 15

Quoted 4 lines
drivers/misc/lkdtm/heap.c +1 -0
@@ -12,6 +12,8 @@ static struct kmem_cache *double_free_cache;
 static struct kmem_cache *a_cache;
 static struct kmem_cache *b_cache;
 
+static volatile int __offset = 1;

Perhaps a comment here as to why volatile is ok to use? That feels like it is a hack around the compiler of today, what happens tomorrow when newer versions decide to ignore volatile as it "knows" no one ever changes it?

thanks,

greg k-h

author
#2 lore raw
Quoted 18 lines · Greg Kroah-Hartman
Quoted 17 lines · Kees Cook

Once __alloc_size hints have been added, the compiler will (correctly!) see this as an overflow. We are, however, trying to test for this condition, so work around it with a volatile int.

---
 drivers/misc/lkdtm/heap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/lkdtm/heap.c +0 -0
index 3d9aae5821a0..e59fcbe00ae0 100644
@@ -12,6 +12,8 @@ static struct kmem_cache *double_free_cache;
 static struct kmem_cache *a_cache;
 static struct kmem_cache *b_cache;
 
Quoted 6 lines

drivers/misc/lkdtm/heap.c static struct kmem_cache *double_free_cache; · line 15

Quoted 1 line
drivers/misc/lkdtm/heap.c +1 -0
+static volatile int __offset = 1;

Perhaps a comment here as to why volatile is ok to use? That feels like it is a hack around the compiler of today, what happens tomorrow when newer versions decide to ignore volatile as it "knows" no one ever changes it?

Sure, I can do that. LKDTM uses this a lot because it, by definition, means the compiler cannot assume it knows anything about its value. (And as such reloads from memory at every use, which is why it's frowned upon anywhere else in the kernel.)

Signature · 1 line
Kees Cook