Skip to content

Commit 39d2192

Browse files
keesbroonie
authored andcommitted
lkdtm: Verify that '__ro_after_init' works correctly
commit 7cca071ccbd2a293ea69168ace6abbcdce53098e upstream. The new __ro_after_init section should be writable before init, but not after. Validate that it gets updated at init and can't be written to afterwards. Signed-off-by: Kees Cook <keescook@chromium.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: David Brown <david.brown@linaro.org> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Emese Revfy <re.emese@gmail.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mathias Krause <minipli@googlemail.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: PaX Team <pageexec@freemail.hu> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: kernel-hardening@lists.openwall.com Cc: linux-arch <linux-arch@vger.kernel.org> Link: http://lkml.kernel.org/r/1455748879-21872-6-git-send-email-keescook@chromium.org Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: David Brown <david.brown@linaro.org> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 7ece66e commit 39d2192

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

drivers/misc/lkdtm.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ enum ctype {
103103
CT_EXEC_USERSPACE,
104104
CT_ACCESS_USERSPACE,
105105
CT_WRITE_RO,
106+
CT_WRITE_RO_AFTER_INIT,
106107
CT_WRITE_KERN,
107108
};
108109

@@ -140,6 +141,7 @@ static char* cp_type[] = {
140141
"EXEC_USERSPACE",
141142
"ACCESS_USERSPACE",
142143
"WRITE_RO",
144+
"WRITE_RO_AFTER_INIT",
143145
"WRITE_KERN",
144146
};
145147

@@ -162,6 +164,7 @@ static DEFINE_SPINLOCK(lock_me_up);
162164
static u8 data_area[EXEC_SIZE];
163165

164166
static const unsigned long rodata = 0xAA55AA55;
167+
static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
165168

166169
module_param(recur_count, int, 0644);
167170
MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
@@ -503,11 +506,28 @@ static void lkdtm_do_action(enum ctype which)
503506
break;
504507
}
505508
case CT_WRITE_RO: {
506-
unsigned long *ptr;
509+
/* Explicitly cast away "const" for the test. */
510+
unsigned long *ptr = (unsigned long *)&rodata;
507511

508-
ptr = (unsigned long *)&rodata;
512+
pr_info("attempting bad rodata write at %p\n", ptr);
513+
*ptr ^= 0xabcd1234;
509514

510-
pr_info("attempting bad write at %p\n", ptr);
515+
break;
516+
}
517+
case CT_WRITE_RO_AFTER_INIT: {
518+
unsigned long *ptr = &ro_after_init;
519+
520+
/*
521+
* Verify we were written to during init. Since an Oops
522+
* is considered a "success", a failure is to just skip the
523+
* real test.
524+
*/
525+
if ((*ptr & 0xAA) != 0xAA) {
526+
pr_info("%p was NOT written during init!?\n", ptr);
527+
break;
528+
}
529+
530+
pr_info("attempting bad ro_after_init write at %p\n", ptr);
511531
*ptr ^= 0xabcd1234;
512532

513533
break;
@@ -817,6 +837,9 @@ static int __init lkdtm_module_init(void)
817837
int n_debugfs_entries = 1; /* Assume only the direct entry */
818838
int i;
819839

840+
/* Make sure we can write to __ro_after_init values during __init */
841+
ro_after_init |= 0xAA;
842+
820843
/* Register debugfs interface */
821844
lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
822845
if (!lkdtm_debugfs_root) {

0 commit comments

Comments
 (0)