Skip to content

Commit dc2b9e9

Browse files
keesgregkh
authored andcommitted
lkdtm: add "WRITE_KERN" test
Add "WRITE_KERN" crash target to validate that kernel executable memory is not writable. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent aac416f commit dc2b9e9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

drivers/misc/lkdtm.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum ctype {
102102
CT_EXEC_USERSPACE,
103103
CT_ACCESS_USERSPACE,
104104
CT_WRITE_RO,
105+
CT_WRITE_KERN,
105106
};
106107

107108
static char* cp_name[] = {
@@ -138,6 +139,7 @@ static char* cp_type[] = {
138139
"EXEC_USERSPACE",
139140
"ACCESS_USERSPACE",
140141
"WRITE_RO",
142+
"WRITE_KERN",
141143
};
142144

143145
static struct jprobe lkdtm;
@@ -317,6 +319,13 @@ static void do_nothing(void)
317319
return;
318320
}
319321

322+
/* Must immediately follow do_nothing for size calculuations to work out. */
323+
static void do_overwritten(void)
324+
{
325+
pr_info("do_overwritten wasn't overwritten!\n");
326+
return;
327+
}
328+
320329
static noinline void corrupt_stack(void)
321330
{
322331
/* Use default char array length that triggers stack protection. */
@@ -496,6 +505,22 @@ static void lkdtm_do_action(enum ctype which)
496505

497506
break;
498507
}
508+
case CT_WRITE_KERN: {
509+
size_t size;
510+
unsigned char *ptr;
511+
512+
size = (unsigned long)do_overwritten -
513+
(unsigned long)do_nothing;
514+
ptr = (unsigned char *)do_overwritten;
515+
516+
pr_info("attempting bad %zu byte write at %p\n", size, ptr);
517+
memcpy(ptr, (unsigned char *)do_nothing, size);
518+
flush_icache_range((unsigned long)ptr,
519+
(unsigned long)(ptr + size));
520+
521+
do_overwritten();
522+
break;
523+
}
499524
case CT_NONE:
500525
default:
501526
break;

0 commit comments

Comments
 (0)