diff options
| author | Kees Cook <keescook@chromium.org> | 2016-06-26 18:12:31 -0400 |
|---|---|---|
| committer | Kees Cook <keescook@chromium.org> | 2016-07-07 14:09:21 -0400 |
| commit | 0d9eb29b13f0e326c4e19b85d3a4ac46e335e6d2 (patch) | |
| tree | d0a513cca0604e822d083ef01c08cc08f243516c /drivers | |
| parent | a3dff71c1c88fc184a1ae5e425ba621d547d16ec (diff) | |
lkdtm: split memory permissions tests to separate file
This splits the EXEC_*, WRITE_* and related tests into the new lkdtm_perms.c
file to help separate things better for readability.
Signed-off-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/misc/Makefile | 1 | ||||
| -rw-r--r-- | drivers/misc/lkdtm.h | 14 | ||||
| -rw-r--r-- | drivers/misc/lkdtm_core.c | 174 | ||||
| -rw-r--r-- | drivers/misc/lkdtm_perms.c | 203 |
4 files changed, 237 insertions, 155 deletions
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index e6b2778731ff..9f6e95bc0635 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile | |||
| @@ -59,6 +59,7 @@ obj-$(CONFIG_CXL_BASE) += cxl/ | |||
| 59 | obj-$(CONFIG_PANEL) += panel.o | 59 | obj-$(CONFIG_PANEL) += panel.o |
| 60 | 60 | ||
| 61 | lkdtm-$(CONFIG_LKDTM) += lkdtm_core.o | 61 | lkdtm-$(CONFIG_LKDTM) += lkdtm_core.o |
| 62 | lkdtm-$(CONFIG_LKDTM) += lkdtm_perms.o | ||
| 62 | lkdtm-$(CONFIG_LKDTM) += lkdtm_rodata_objcopy.o | 63 | lkdtm-$(CONFIG_LKDTM) += lkdtm_rodata_objcopy.o |
| 63 | lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o | 64 | lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o |
| 64 | 65 | ||
diff --git a/drivers/misc/lkdtm.h b/drivers/misc/lkdtm.h index ef290a2c8816..40f681cd6efe 100644 --- a/drivers/misc/lkdtm.h +++ b/drivers/misc/lkdtm.h | |||
| @@ -1,6 +1,19 @@ | |||
| 1 | #ifndef __LKDTM_H | 1 | #ifndef __LKDTM_H |
| 2 | #define __LKDTM_H | 2 | #define __LKDTM_H |
| 3 | 3 | ||
| 4 | /* lkdtm_perms.c */ | ||
| 5 | void __init lkdtm_perms_init(void); | ||
| 6 | void lkdtm_WRITE_RO(void); | ||
| 7 | void lkdtm_WRITE_RO_AFTER_INIT(void); | ||
| 8 | void lkdtm_WRITE_KERN(void); | ||
| 9 | void lkdtm_EXEC_DATA(void); | ||
| 10 | void lkdtm_EXEC_STACK(void); | ||
| 11 | void lkdtm_EXEC_KMALLOC(void); | ||
| 12 | void lkdtm_EXEC_VMALLOC(void); | ||
| 13 | void lkdtm_EXEC_RODATA(void); | ||
| 14 | void lkdtm_EXEC_USERSPACE(void); | ||
| 15 | void lkdtm_ACCESS_USERSPACE(void); | ||
| 16 | |||
| 4 | /* lkdtm_rodata.c */ | 17 | /* lkdtm_rodata.c */ |
| 5 | void lkdtm_rodata_do_nothing(void); | 18 | void lkdtm_rodata_do_nothing(void); |
| 6 | 19 | ||
| @@ -16,4 +29,5 @@ void lkdtm_USERCOPY_STACK_FRAME_FROM(void); | |||
| 16 | void lkdtm_USERCOPY_STACK_BEYOND(void); | 29 | void lkdtm_USERCOPY_STACK_BEYOND(void); |
| 17 | void lkdtm_USERCOPY_KERNEL(void); | 30 | void lkdtm_USERCOPY_KERNEL(void); |
| 18 | 31 | ||
| 32 | |||
| 19 | #endif | 33 | #endif |
diff --git a/drivers/misc/lkdtm_core.c b/drivers/misc/lkdtm_core.c index 74376920ed55..0b3e3770068a 100644 --- a/drivers/misc/lkdtm_core.c +++ b/drivers/misc/lkdtm_core.c | |||
| @@ -44,9 +44,6 @@ | |||
| 44 | #include <linux/slab.h> | 44 | #include <linux/slab.h> |
| 45 | #include <scsi/scsi_cmnd.h> | 45 | #include <scsi/scsi_cmnd.h> |
| 46 | #include <linux/debugfs.h> | 46 | #include <linux/debugfs.h> |
| 47 | #include <linux/vmalloc.h> | ||
| 48 | #include <linux/mman.h> | ||
| 49 | #include <asm/cacheflush.h> | ||
| 50 | 47 | ||
| 51 | #ifdef CONFIG_IDE | 48 | #ifdef CONFIG_IDE |
| 52 | #include <linux/ide.h> | 49 | #include <linux/ide.h> |
| @@ -67,7 +64,6 @@ | |||
| 67 | #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2) | 64 | #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2) |
| 68 | 65 | ||
| 69 | #define DEFAULT_COUNT 10 | 66 | #define DEFAULT_COUNT 10 |
| 70 | #define EXEC_SIZE 64 | ||
| 71 | 67 | ||
| 72 | enum cname { | 68 | enum cname { |
| 73 | CN_INVALID, | 69 | CN_INVALID, |
| @@ -191,11 +187,6 @@ static int count = DEFAULT_COUNT; | |||
| 191 | static DEFINE_SPINLOCK(count_lock); | 187 | static DEFINE_SPINLOCK(count_lock); |
| 192 | static DEFINE_SPINLOCK(lock_me_up); | 188 | static DEFINE_SPINLOCK(lock_me_up); |
| 193 | 189 | ||
| 194 | static u8 data_area[EXEC_SIZE]; | ||
| 195 | |||
| 196 | static const unsigned long rodata = 0xAA55AA55; | ||
| 197 | static unsigned long ro_after_init __ro_after_init = 0x55AA5500; | ||
| 198 | |||
| 199 | module_param(recur_count, int, 0644); | 190 | module_param(recur_count, int, 0644); |
| 200 | MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test"); | 191 | MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test"); |
| 201 | module_param(cpoint_name, charp, 0444); | 192 | module_param(cpoint_name, charp, 0444); |
| @@ -348,18 +339,6 @@ static int recursive_loop(int remaining) | |||
| 348 | return recursive_loop(remaining - 1); | 339 | return recursive_loop(remaining - 1); |
| 349 | } | 340 | } |
| 350 | 341 | ||
| 351 | static void do_nothing(void) | ||
| 352 | { | ||
| 353 | return; | ||
| 354 | } | ||
| 355 | |||
| 356 | /* Must immediately follow do_nothing for size calculuations to work out. */ | ||
| 357 | static void do_overwritten(void) | ||
| 358 | { | ||
| 359 | pr_info("do_overwritten wasn't overwritten!\n"); | ||
| 360 | return; | ||
| 361 | } | ||
| 362 | |||
| 363 | static noinline void corrupt_stack(void) | 342 | static noinline void corrupt_stack(void) |
| 364 | { | 343 | { |
| 365 | /* Use default char array length that triggers stack protection. */ | 344 | /* Use default char array length that triggers stack protection. */ |
| @@ -368,38 +347,6 @@ static noinline void corrupt_stack(void) | |||
| 368 | memset((void *)data, 0, 64); | 347 | memset((void *)data, 0, 64); |
| 369 | } | 348 | } |
| 370 | 349 | ||
| 371 | static noinline void execute_location(void *dst, bool write) | ||
| 372 | { | ||
| 373 | void (*func)(void) = dst; | ||
| 374 | |||
| 375 | pr_info("attempting ok execution at %p\n", do_nothing); | ||
| 376 | do_nothing(); | ||
| 377 | |||
| 378 | if (write) { | ||
| 379 | memcpy(dst, do_nothing, EXEC_SIZE); | ||
| 380 | flush_icache_range((unsigned long)dst, | ||
| 381 | (unsigned long)dst + EXEC_SIZE); | ||
| 382 | } | ||
| 383 | pr_info("attempting bad execution at %p\n", func); | ||
| 384 | func(); | ||
| 385 | } | ||
| 386 | |||
| 387 | static void execute_user_location(void *dst) | ||
| 388 | { | ||
| 389 | /* Intentionally crossing kernel/user memory boundary. */ | ||
| 390 | void (*func)(void) = dst; | ||
| 391 | |||
| 392 | pr_info("attempting ok execution at %p\n", do_nothing); | ||
| 393 | do_nothing(); | ||
| 394 | |||
| 395 | if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE)) | ||
| 396 | return; | ||
| 397 | flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE); | ||
| 398 | pr_info("attempting bad execution at %p\n", func); | ||
| 399 | func(); | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | static void lkdtm_do_action(enum ctype which) | 350 | static void lkdtm_do_action(enum ctype which) |
| 404 | { | 351 | { |
| 405 | switch (which) { | 352 | switch (which) { |
| @@ -577,116 +524,35 @@ static void lkdtm_do_action(enum ctype which) | |||
| 577 | schedule(); | 524 | schedule(); |
| 578 | break; | 525 | break; |
| 579 | case CT_EXEC_DATA: | 526 | case CT_EXEC_DATA: |
| 580 | execute_location(data_area, true); | 527 | lkdtm_EXEC_DATA(); |
| 581 | break; | 528 | break; |
| 582 | case CT_EXEC_STACK: { | 529 | case CT_EXEC_STACK: |
| 583 | u8 stack_area[EXEC_SIZE]; | 530 | lkdtm_EXEC_STACK(); |
| 584 | execute_location(stack_area, true); | ||
| 585 | break; | 531 | break; |
| 586 | } | 532 | case CT_EXEC_KMALLOC: |
| 587 | case CT_EXEC_KMALLOC: { | 533 | lkdtm_EXEC_KMALLOC(); |
| 588 | u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL); | ||
| 589 | execute_location(kmalloc_area, true); | ||
| 590 | kfree(kmalloc_area); | ||
| 591 | break; | 534 | break; |
| 592 | } | 535 | case CT_EXEC_VMALLOC: |
| 593 | case CT_EXEC_VMALLOC: { | 536 | lkdtm_EXEC_VMALLOC(); |
| 594 | u32 *vmalloc_area = vmalloc(EXEC_SIZE); | ||
| 595 | execute_location(vmalloc_area, true); | ||
| 596 | vfree(vmalloc_area); | ||
| 597 | break; | 537 | break; |
| 598 | } | ||
| 599 | case CT_EXEC_RODATA: | 538 | case CT_EXEC_RODATA: |
| 600 | execute_location(lkdtm_rodata_do_nothing, false); | 539 | lkdtm_EXEC_RODATA(); |
| 601 | break; | 540 | break; |
| 602 | case CT_EXEC_USERSPACE: { | 541 | case CT_EXEC_USERSPACE: |
| 603 | unsigned long user_addr; | 542 | lkdtm_EXEC_USERSPACE(); |
| 604 | |||
| 605 | user_addr = vm_mmap(NULL, 0, PAGE_SIZE, | ||
| 606 | PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| 607 | MAP_ANONYMOUS | MAP_PRIVATE, 0); | ||
| 608 | if (user_addr >= TASK_SIZE) { | ||
| 609 | pr_warn("Failed to allocate user memory\n"); | ||
| 610 | return; | ||
| 611 | } | ||
| 612 | execute_user_location((void *)user_addr); | ||
| 613 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 614 | break; | 543 | break; |
| 615 | } | 544 | case CT_ACCESS_USERSPACE: |
| 616 | case CT_ACCESS_USERSPACE: { | 545 | lkdtm_ACCESS_USERSPACE(); |
| 617 | unsigned long user_addr, tmp = 0; | ||
| 618 | unsigned long *ptr; | ||
| 619 | |||
| 620 | user_addr = vm_mmap(NULL, 0, PAGE_SIZE, | ||
| 621 | PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| 622 | MAP_ANONYMOUS | MAP_PRIVATE, 0); | ||
| 623 | if (user_addr >= TASK_SIZE) { | ||
| 624 | pr_warn("Failed to allocate user memory\n"); | ||
| 625 | return; | ||
| 626 | } | ||
| 627 | |||
| 628 | if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) { | ||
| 629 | pr_warn("copy_to_user failed\n"); | ||
| 630 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 631 | return; | ||
| 632 | } | ||
| 633 | |||
| 634 | ptr = (unsigned long *)user_addr; | ||
| 635 | |||
| 636 | pr_info("attempting bad read at %p\n", ptr); | ||
| 637 | tmp = *ptr; | ||
| 638 | tmp += 0xc0dec0de; | ||
| 639 | |||
| 640 | pr_info("attempting bad write at %p\n", ptr); | ||
| 641 | *ptr = tmp; | ||
| 642 | |||
| 643 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 644 | |||
| 645 | break; | 546 | break; |
| 646 | } | 547 | case CT_WRITE_RO: |
| 647 | case CT_WRITE_RO: { | 548 | lkdtm_WRITE_RO(); |
| 648 | /* Explicitly cast away "const" for the test. */ | ||
| 649 | unsigned long *ptr = (unsigned long *)&rodata; | ||
| 650 | |||
| 651 | pr_info("attempting bad rodata write at %p\n", ptr); | ||
| 652 | *ptr ^= 0xabcd1234; | ||
| 653 | |||
| 654 | break; | 549 | break; |
| 655 | } | 550 | case CT_WRITE_RO_AFTER_INIT: |
| 656 | case CT_WRITE_RO_AFTER_INIT: { | 551 | lkdtm_WRITE_RO_AFTER_INIT(); |
| 657 | unsigned long *ptr = &ro_after_init; | ||
| 658 | |||
| 659 | /* | ||
| 660 | * Verify we were written to during init. Since an Oops | ||
| 661 | * is considered a "success", a failure is to just skip the | ||
| 662 | * real test. | ||
| 663 | */ | ||
| 664 | if ((*ptr & 0xAA) != 0xAA) { | ||
| 665 | pr_info("%p was NOT written during init!?\n", ptr); | ||
| 666 | break; | ||
| 667 | } | ||
| 668 | |||
| 669 | pr_info("attempting bad ro_after_init write at %p\n", ptr); | ||
| 670 | *ptr ^= 0xabcd1234; | ||
| 671 | |||
| 672 | break; | 552 | break; |
| 673 | } | 553 | case CT_WRITE_KERN: |
| 674 | case CT_WRITE_KERN: { | 554 | lkdtm_WRITE_KERN(); |
| 675 | size_t size; | ||
| 676 | unsigned char *ptr; | ||
| 677 | |||
| 678 | size = (unsigned long)do_overwritten - | ||
| 679 | (unsigned long)do_nothing; | ||
| 680 | ptr = (unsigned char *)do_overwritten; | ||
| 681 | |||
| 682 | pr_info("attempting bad %zu byte write at %p\n", size, ptr); | ||
| 683 | memcpy(ptr, (unsigned char *)do_nothing, size); | ||
| 684 | flush_icache_range((unsigned long)ptr, | ||
| 685 | (unsigned long)(ptr + size)); | ||
| 686 | |||
| 687 | do_overwritten(); | ||
| 688 | break; | 555 | break; |
| 689 | } | ||
| 690 | case CT_ATOMIC_UNDERFLOW: { | 556 | case CT_ATOMIC_UNDERFLOW: { |
| 691 | atomic_t under = ATOMIC_INIT(INT_MIN); | 557 | atomic_t under = ATOMIC_INIT(INT_MIN); |
| 692 | 558 | ||
| @@ -1024,11 +890,9 @@ static int __init lkdtm_module_init(void) | |||
| 1024 | int i; | 890 | int i; |
| 1025 | 891 | ||
| 1026 | /* Handle test-specific initialization. */ | 892 | /* Handle test-specific initialization. */ |
| 893 | lkdtm_perms_init(); | ||
| 1027 | lkdtm_usercopy_init(); | 894 | lkdtm_usercopy_init(); |
| 1028 | 895 | ||
| 1029 | /* Make sure we can write to __ro_after_init values during __init */ | ||
| 1030 | ro_after_init |= 0xAA; | ||
| 1031 | |||
| 1032 | /* Register debugfs interface */ | 896 | /* Register debugfs interface */ |
| 1033 | lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL); | 897 | lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL); |
| 1034 | if (!lkdtm_debugfs_root) { | 898 | if (!lkdtm_debugfs_root) { |
diff --git a/drivers/misc/lkdtm_perms.c b/drivers/misc/lkdtm_perms.c new file mode 100644 index 000000000000..8201006502e2 --- /dev/null +++ b/drivers/misc/lkdtm_perms.c | |||
| @@ -0,0 +1,203 @@ | |||
| 1 | /* | ||
| 2 | * This is for all the tests related to validating kernel memory | ||
| 3 | * permissions: non-executable regions, non-writable regions, and | ||
| 4 | * even non-readable regions. | ||
| 5 | */ | ||
| 6 | #define pr_fmt(fmt) "lkdtm: " fmt | ||
| 7 | |||
| 8 | #include <linux/kernel.h> | ||
| 9 | #include <linux/slab.h> | ||
| 10 | #include <linux/vmalloc.h> | ||
| 11 | #include <linux/mman.h> | ||
| 12 | #include <linux/uaccess.h> | ||
| 13 | #include <asm/cacheflush.h> | ||
| 14 | |||
| 15 | #include "lkdtm.h" | ||
| 16 | |||
| 17 | /* Whether or not to fill the target memory area with do_nothing(). */ | ||
| 18 | #define CODE_WRITE true | ||
| 19 | #define CODE_AS_IS false | ||
| 20 | |||
| 21 | /* How many bytes to copy to be sure we've copied enough of do_nothing(). */ | ||
| 22 | #define EXEC_SIZE 64 | ||
| 23 | |||
| 24 | /* This is non-const, so it will end up in the .data section. */ | ||
| 25 | static u8 data_area[EXEC_SIZE]; | ||
| 26 | |||
| 27 | /* This is cost, so it will end up in the .rodata section. */ | ||
| 28 | static const unsigned long rodata = 0xAA55AA55; | ||
| 29 | |||
| 30 | /* This is marked __ro_after_init, so it should ultimately be .rodata. */ | ||
| 31 | static unsigned long ro_after_init __ro_after_init = 0x55AA5500; | ||
| 32 | |||
| 33 | /* | ||
| 34 | * This just returns to the caller. It is designed to be copied into | ||
| 35 | * non-executable memory regions. | ||
| 36 | */ | ||
| 37 | static void do_nothing(void) | ||
| 38 | { | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | /* Must immediately follow do_nothing for size calculuations to work out. */ | ||
| 43 | static void do_overwritten(void) | ||
| 44 | { | ||
| 45 | pr_info("do_overwritten wasn't overwritten!\n"); | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | static noinline void execute_location(void *dst, bool write) | ||
| 50 | { | ||
| 51 | void (*func)(void) = dst; | ||
| 52 | |||
| 53 | pr_info("attempting ok execution at %p\n", do_nothing); | ||
| 54 | do_nothing(); | ||
| 55 | |||
| 56 | if (write == CODE_WRITE) { | ||
| 57 | memcpy(dst, do_nothing, EXEC_SIZE); | ||
| 58 | flush_icache_range((unsigned long)dst, | ||
| 59 | (unsigned long)dst + EXEC_SIZE); | ||
| 60 | } | ||
| 61 | pr_info("attempting bad execution at %p\n", func); | ||
| 62 | func(); | ||
| 63 | } | ||
| 64 | |||
| 65 | static void execute_user_location(void *dst) | ||
| 66 | { | ||
| 67 | /* Intentionally crossing kernel/user memory boundary. */ | ||
| 68 | void (*func)(void) = dst; | ||
| 69 | |||
| 70 | pr_info("attempting ok execution at %p\n", do_nothing); | ||
| 71 | do_nothing(); | ||
| 72 | |||
| 73 | if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE)) | ||
| 74 | return; | ||
| 75 | flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE); | ||
| 76 | pr_info("attempting bad execution at %p\n", func); | ||
| 77 | func(); | ||
| 78 | } | ||
| 79 | |||
| 80 | void lkdtm_WRITE_RO(void) | ||
| 81 | { | ||
| 82 | /* Explicitly cast away "const" for the test. */ | ||
| 83 | unsigned long *ptr = (unsigned long *)&rodata; | ||
| 84 | |||
| 85 | pr_info("attempting bad rodata write at %p\n", ptr); | ||
| 86 | *ptr ^= 0xabcd1234; | ||
| 87 | } | ||
| 88 | |||
| 89 | void lkdtm_WRITE_RO_AFTER_INIT(void) | ||
| 90 | { | ||
| 91 | unsigned long *ptr = &ro_after_init; | ||
| 92 | |||
| 93 | /* | ||
| 94 | * Verify we were written to during init. Since an Oops | ||
| 95 | * is considered a "success", a failure is to just skip the | ||
| 96 | * real test. | ||
| 97 | */ | ||
| 98 | if ((*ptr & 0xAA) != 0xAA) { | ||
| 99 | pr_info("%p was NOT written during init!?\n", ptr); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | pr_info("attempting bad ro_after_init write at %p\n", ptr); | ||
| 104 | *ptr ^= 0xabcd1234; | ||
| 105 | } | ||
| 106 | |||
| 107 | void lkdtm_WRITE_KERN(void) | ||
| 108 | { | ||
| 109 | size_t size; | ||
| 110 | unsigned char *ptr; | ||
| 111 | |||
| 112 | size = (unsigned long)do_overwritten - (unsigned long)do_nothing; | ||
| 113 | ptr = (unsigned char *)do_overwritten; | ||
| 114 | |||
| 115 | pr_info("attempting bad %zu byte write at %p\n", size, ptr); | ||
| 116 | memcpy(ptr, (unsigned char *)do_nothing, size); | ||
| 117 | flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size)); | ||
| 118 | |||
| 119 | do_overwritten(); | ||
| 120 | } | ||
| 121 | |||
| 122 | void lkdtm_EXEC_DATA(void) | ||
| 123 | { | ||
| 124 | execute_location(data_area, CODE_WRITE); | ||
| 125 | } | ||
| 126 | |||
| 127 | void lkdtm_EXEC_STACK(void) | ||
| 128 | { | ||
| 129 | u8 stack_area[EXEC_SIZE]; | ||
| 130 | execute_location(stack_area, CODE_WRITE); | ||
| 131 | } | ||
| 132 | |||
| 133 | void lkdtm_EXEC_KMALLOC(void) | ||
| 134 | { | ||
| 135 | u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL); | ||
| 136 | execute_location(kmalloc_area, CODE_WRITE); | ||
| 137 | kfree(kmalloc_area); | ||
| 138 | } | ||
| 139 | |||
| 140 | void lkdtm_EXEC_VMALLOC(void) | ||
| 141 | { | ||
| 142 | u32 *vmalloc_area = vmalloc(EXEC_SIZE); | ||
| 143 | execute_location(vmalloc_area, CODE_WRITE); | ||
| 144 | vfree(vmalloc_area); | ||
| 145 | } | ||
| 146 | |||
| 147 | void lkdtm_EXEC_RODATA(void) | ||
| 148 | { | ||
| 149 | execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); | ||
| 150 | } | ||
| 151 | |||
| 152 | void lkdtm_EXEC_USERSPACE(void) | ||
| 153 | { | ||
| 154 | unsigned long user_addr; | ||
| 155 | |||
| 156 | user_addr = vm_mmap(NULL, 0, PAGE_SIZE, | ||
| 157 | PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| 158 | MAP_ANONYMOUS | MAP_PRIVATE, 0); | ||
| 159 | if (user_addr >= TASK_SIZE) { | ||
| 160 | pr_warn("Failed to allocate user memory\n"); | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | execute_user_location((void *)user_addr); | ||
| 164 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 165 | } | ||
| 166 | |||
| 167 | void lkdtm_ACCESS_USERSPACE(void) | ||
| 168 | { | ||
| 169 | unsigned long user_addr, tmp = 0; | ||
| 170 | unsigned long *ptr; | ||
| 171 | |||
| 172 | user_addr = vm_mmap(NULL, 0, PAGE_SIZE, | ||
| 173 | PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| 174 | MAP_ANONYMOUS | MAP_PRIVATE, 0); | ||
| 175 | if (user_addr >= TASK_SIZE) { | ||
| 176 | pr_warn("Failed to allocate user memory\n"); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) { | ||
| 181 | pr_warn("copy_to_user failed\n"); | ||
| 182 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | ptr = (unsigned long *)user_addr; | ||
| 187 | |||
| 188 | pr_info("attempting bad read at %p\n", ptr); | ||
| 189 | tmp = *ptr; | ||
| 190 | tmp += 0xc0dec0de; | ||
| 191 | |||
| 192 | pr_info("attempting bad write at %p\n", ptr); | ||
| 193 | *ptr = tmp; | ||
| 194 | |||
| 195 | vm_munmap(user_addr, PAGE_SIZE); | ||
| 196 | } | ||
| 197 | |||
| 198 | void __init lkdtm_perms_init(void) | ||
| 199 | { | ||
| 200 | /* Make sure we can write to __ro_after_init values during __init */ | ||
| 201 | ro_after_init |= 0xAA; | ||
| 202 | |||
| 203 | } | ||
