Skip to content

Commit 9a49a52

Browse files
committed
lkdtm: add function for testing .rodata section
This adds a function that lives in the .rodata section. The section flags are corrected using objcopy since there is no way with gcc to declare section flags in an architecture-agnostic way. Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 426f3a5 commit 9a49a52

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎drivers/misc/Makefile‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ obj-$(CONFIG_CXL_BASE) += cxl/
5959
obj-$(CONFIG_PANEL) += panel.o
6060

6161
lkdtm-$(CONFIG_LKDTM) += lkdtm_core.o
62+
lkdtm-$(CONFIG_LKDTM) += lkdtm_rodata_objcopy.o
63+
64+
OBJCOPYFLAGS :=
65+
OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \
66+
--set-section-flags .text=alloc,readonly \
67+
--rename-section .text=.rodata
68+
$(obj)/lkdtm_rodata_objcopy.o: $(obj)/lkdtm_rodata.o
69+
$(call if_changed,objcopy)

‎drivers/misc/lkdtm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __LKDTM_H
2+
#define __LKDTM_H
3+
4+
void lkdtm_rodata_do_nothing(void);
5+
6+
#endif

‎drivers/misc/lkdtm_core.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#include <linux/ide.h>
5353
#endif
5454

55+
#include "lkdtm.h"
56+
5557
/*
5658
* Make sure our attempts to over run the kernel stack doesn't trigger
5759
* a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
@@ -103,6 +105,7 @@ enum ctype {
103105
CT_EXEC_STACK,
104106
CT_EXEC_KMALLOC,
105107
CT_EXEC_VMALLOC,
108+
CT_EXEC_RODATA,
106109
CT_EXEC_USERSPACE,
107110
CT_ACCESS_USERSPACE,
108111
CT_WRITE_RO,
@@ -145,6 +148,7 @@ static char* cp_type[] = {
145148
"EXEC_STACK",
146149
"EXEC_KMALLOC",
147150
"EXEC_VMALLOC",
151+
"EXEC_RODATA",
148152
"EXEC_USERSPACE",
149153
"ACCESS_USERSPACE",
150154
"WRITE_RO",
@@ -346,15 +350,18 @@ static noinline void corrupt_stack(void)
346350
memset((void *)data, 0, 64);
347351
}
348352

349-
static void noinline execute_location(void *dst)
353+
static noinline void execute_location(void *dst, bool write)
350354
{
351355
void (*func)(void) = dst;
352356

353357
pr_info("attempting ok execution at %p\n", do_nothing);
354358
do_nothing();
355359

356-
memcpy(dst, do_nothing, EXEC_SIZE);
357-
flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
360+
if (write) {
361+
memcpy(dst, do_nothing, EXEC_SIZE);
362+
flush_icache_range((unsigned long)dst,
363+
(unsigned long)dst + EXEC_SIZE);
364+
}
358365
pr_info("attempting bad execution at %p\n", func);
359366
func();
360367
}
@@ -551,25 +558,28 @@ static void lkdtm_do_action(enum ctype which)
551558
schedule();
552559
break;
553560
case CT_EXEC_DATA:
554-
execute_location(data_area);
561+
execute_location(data_area, true);
555562
break;
556563
case CT_EXEC_STACK: {
557564
u8 stack_area[EXEC_SIZE];
558-
execute_location(stack_area);
565+
execute_location(stack_area, true);
559566
break;
560567
}
561568
case CT_EXEC_KMALLOC: {
562569
u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
563-
execute_location(kmalloc_area);
570+
execute_location(kmalloc_area, true);
564571
kfree(kmalloc_area);
565572
break;
566573
}
567574
case CT_EXEC_VMALLOC: {
568575
u32 *vmalloc_area = vmalloc(EXEC_SIZE);
569-
execute_location(vmalloc_area);
576+
execute_location(vmalloc_area, true);
570577
vfree(vmalloc_area);
571578
break;
572579
}
580+
case CT_EXEC_RODATA:
581+
execute_location(lkdtm_rodata_do_nothing, false);
582+
break;
573583
case CT_EXEC_USERSPACE: {
574584
unsigned long user_addr;
575585

‎drivers/misc/lkdtm_rodata.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* This includes functions that are meant to live entirely in .rodata
3+
* (via objcopy tricks), to validate the non-executability of .rodata.
4+
*/
5+
#include <linux/kernel.h>
6+
7+
void lkdtm_rodata_do_nothing(void)
8+
{
9+
/* Does nothing. We just want an architecture agnostic "return". */
10+
}

0 commit comments

Comments
 (0)