Skip to contentSkip to conversation

lkdtm: Prevent the compiler from optimising lkdtm_CORRUPT_STACK()

PATCH In review Michael Ellerman 3 participants 3 messages Nov 15, 2016 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lkdtm: Prevent the compiler from optimising lkdtm_CORRUPT_STACK()

At least on powerpc with GCC 6, the compiler is smart enough to optimise lkdtm_CORRUPT_STACK() into an empty function that just returns.

If we print the buffer after we've written to it that prevents the compiler from optimising away data and the memset().

---
 drivers/misc/lkdtm_bugs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/misc/lkdtm_bugs.c +2 -1
index 182ae1894b32..30e62dd7e7ca 100644
@@ -80,7 +80,8 @@ noinline void lkdtm_CORRUPT_STACK(void)
 	/* Use default char array length that triggers stack protection. */
 	char data[8];
 
-	memset((void *)data, 0, 64);
+	memset((void *)data, 'a', 64);
+	pr_info("Corrupted stack with '%16s'...\n", data);
 }
 
 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Michael Ellerman

At least on powerpc with GCC 6, the compiler is smart enough to optimise lkdtm_CORRUPT_STACK() into an empty function that just returns.

Sneaky. Thanks!

Quoted 4 lines

If we print the buffer after we've written to it that prevents the compiler from optimising away data and the memset().

Greg, can you add this to the drivers/misc?

-Kees

Quoted 21 lines
---
 drivers/misc/lkdtm_bugs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/misc/lkdtm_bugs.c +2 -2
index 182ae1894b32..30e62dd7e7ca 100644
@@ -80,7 +80,8 @@ noinline void lkdtm_CORRUPT_STACK(void)
        /* Use default char array length that triggers stack protection. */
        char data[8];
 
-       memset((void *)data, 0, 64);
+       memset((void *)data, 'a', 64);
+       pr_info("Corrupted stack with '%16s'...\n", data);
 }
 
 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
--

2.7.4

Signature · 2 lines
Kees Cook
Nexus Security
maintainer
#2 lore raw
Quoted 6 lines · Kees Cook
Quoted 2 lines · Michael Ellerman

At least on powerpc with GCC 6, the compiler is smart enough to optimise lkdtm_CORRUPT_STACK() into an empty function that just returns.

Sneaky. Thanks!

Quoted 8 lines
Quoted 4 lines

If we print the buffer after we've written to it that prevents the compiler from optimising away data and the memset().

Greg, can you add this to the drivers/misc?

Now added, thanks.

greg k-h