Skip to contentSkip to conversation

lkdtm: fix memory copy size for WRITE_KERN

PATCH Superseded Candle Sun 2 participants 3 messages Jan 27, 2021 – Jan 28, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Candle Sun

author
#0 lore raw

PATCH v2 1/1 lkdtm: fix memory copy size for WRITE_KERN

From: Candle Sun <[email protected]>

Though do_overwritten() follows do_nothing() in source code, the final memory address order is determined by compiler. We can't always assume address of do_overwritten() is bigger than do_nothing(). At least the Clang we are using places do_overwritten() before do_nothing() in the object. This causes the copy size in lkdtm_WRITE_KERN() is *really* big and WRITE_KERN test on ARM32 arch will fail.

Get absolute value of the address substraction for memcpy() size.

---
Changes in v2:
- Use abs() in place of address comparison.

---
 drivers/misc/lkdtm/perms.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/misc/lkdtm/perms.c +3 -3
index 2dede2ef658f..fbb7f4554054 100644
@@ -31,13 +31,13 @@ static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
  * This just returns to the caller. It is designed to be copied into
  * non-executable memory regions.
  */
-static void do_nothing(void)
+static noinline void do_nothing(void)
 {
 	return;
 }
 
 /* Must immediately follow do_nothing for size calculuations to work out. */
-static void do_overwritten(void)
+static noinline void do_overwritten(void)
 {
 	pr_info("do_overwritten wasn't overwritten!\n");
 	return;
@@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
 	size_t size;
 	volatile unsigned char *ptr;
 
-	size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
+	size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
 	ptr = (unsigned char *)do_overwritten;
 
 	pr_info("attempting bad %zu byte write at %px\n", size, ptr);
Signature · 1 line
2.17.0
#1 lore raw
Quoted 5 lines · Candle Sun

From: Candle Sun <[email protected]>

Though do_overwritten() follows do_nothing() in source code, the final memory address order is determined by compiler. We can't always assume

^ "by the compiler."

Quoted 3 lines

address of do_overwritten() is bigger than do_nothing(). At least the Clang we are using places do_overwritten() before do_nothing() in the object. This causes the copy size in lkdtm_WRITE_KERN() is *really*

Hopefully nothing else gets placed in between the two, otherwise we're going to overwrite that, too.

Quoted 3 lines

big and WRITE_KERN test on ARM32 arch will fail.

Get absolute value of the address substraction for memcpy() size.

^ typo: subtraction

Quoted 14 lines

---
Changes in v2:
- Use abs() in place of address comparison.

---
 drivers/misc/lkdtm/perms.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/misc/lkdtm/perms.c +0 -0
index 2dede2ef658f..fbb7f4554054 100644

drivers/misc/lkdtm/perms.c static unsigned long ro_after_init __ro_after_init = 0x55AA5500; · line 39

Quoted 10 lines
drivers/misc/lkdtm/perms.c +1 -1
@@ -31,13 +31,13 @@ static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
  * This just returns to the caller. It is designed to be copied into
  * non-executable memory regions.
  */
-static void do_nothing(void)
+static noinline void do_nothing(void)
 {
        return;
 }
 
 /* Must immediately follow do_nothing for size calculuations to work out. */

^ This comment is now obsolete and should be removed together with this patch. That will also fix the typo in it.

Quoted 17 lines
drivers/misc/lkdtm/perms.c +2 -3
-static void do_overwritten(void)
+static noinline void do_overwritten(void)
 {
        pr_info("do_overwritten wasn't overwritten!\n");
        return;
@@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
        size_t size;
        volatile unsigned char *ptr;
 
-       size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
+       size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
        ptr = (unsigned char *)do_overwritten;
 
        pr_info("attempting bad %zu byte write at %px\n", size, ptr);
--

2.17.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
author
#2 lore raw
Quoted 64 lines · Nick Desaulniers
Quoted 5 lines · Candle Sun

From: Candle Sun <[email protected]>

Though do_overwritten() follows do_nothing() in source code, the final memory address order is determined by compiler. We can't always assume

^ "by the compiler."

Quoted 3 lines

address of do_overwritten() is bigger than do_nothing(). At least the Clang we are using places do_overwritten() before do_nothing() in the object. This causes the copy size in lkdtm_WRITE_KERN() is *really*

Hopefully nothing else gets placed in between the two, otherwise we're going to overwrite that, too.

Quoted 3 lines

big and WRITE_KERN test on ARM32 arch will fail.

Get absolute value of the address substraction for memcpy() size.

^ typo: subtraction

Quoted 14 lines

---
Changes in v2:
- Use abs() in place of address comparison.

---
 drivers/misc/lkdtm/perms.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/misc/lkdtm/perms.c +0 -0
index 2dede2ef658f..fbb7f4554054 100644

drivers/misc/lkdtm/perms.c static unsigned long ro_after_init __ro_after_init = 0x55AA5500; · line 39

Quoted 10 lines
drivers/misc/lkdtm/perms.c +1 -1
@@ -31,13 +31,13 @@ static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
  * This just returns to the caller. It is designed to be copied into
  * non-executable memory regions.
  */
-static void do_nothing(void)
+static noinline void do_nothing(void)
 {
        return;
 }
 
 /* Must immediately follow do_nothing for size calculuations to work out. */

^ This comment is now obsolete and should be removed together with this patch. That will also fix the typo in it.

Quoted 13 lines
drivers/misc/lkdtm/perms.c +2 -2
-static void do_overwritten(void)
+static noinline void do_overwritten(void)
 {
        pr_info("do_overwritten wasn't overwritten!\n");
        return;
@@ -113,7 +113,7 @@ void lkdtm_WRITE_KERN(void)
        size_t size;
        volatile unsigned char *ptr;
 
-       size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
+       size = (size_t)abs((uintptr_t)do_overwritten - (uintptr_t)do_nothing);
        ptr = (unsigned char *)do_overwritten;
 
Quoted 9 lines
Quoted 4 lines
drivers/misc/lkdtm/perms.c +0 -1
        pr_info("attempting bad %zu byte write at %px\n", size, ptr);
--

2.17.0

--
Thanks,
~Nick Desaulniers

Thanks Nick. I will clean the typo and the comment line in the next patch.

Regards,
Candle