// SPDX-License-Identifier: GPL-2.0 /* * This is for all the tests related to validating kernel memory * permissions: non-executable regions, non-writable regions, and * even non-readable regions. */ #include "lkdtm.h" #include #include #include #include #include /* Whether or not to fill the target memory area with do_nothing(). */ #define CODE_WRITE true #define CODE_AS_IS false /* How many bytes to copy to be sure we've copied enough of do_nothing(). */ #define EXEC_SIZE 64 /* This is non-const, so it will end up in the .data section. */ static u8 data_area[EXEC_SIZE]; /* This is cost, so it will end up in the .rodata section. */ static const unsigned long rodata = 0xAA55AA55; /* This is marked __ro_after_init, so it should ultimately be .rodata. */ 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) { return; } /* Must immediately follow do_nothing for size calculuations to work out. */ static void do_overwritten(void) { pr_info("do_overwritten wasn't overwritten!\n"); return; } static noinline void execute_location(void *dst, bool write) { void (*func)(void) = dst; pr_info("attempting ok execution at %px\n", do_nothing); do_nothing(); if (write == CODE_WRITE) { memcpy(dst, do_nothing, EXEC_SIZE); flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE); } pr_info("attempting bad execution at %px\n", func); func(); } static void execute_user_location(void *dst) { int copied; /* Intentionally crossing kernel/user memory boundary. */ void (*func)(void) = dst; pr_info("attempting ok execution at %px\n", do_nothing); do_nothing(); copied = access_process_vm(current, (unsigned long)dst, do_nothing, EXEC_SIZE, FOLL_WRITE); if (copied < EXEC_SIZE) return; pr_info("attempting bad execution at %px\n", func); func(); } void lkdtm_WRITE_RO(void) { /* Explicitly cast away "const" for the test. */ unsigned long *ptr = (unsigned long *)&rodata; pr_info("attempting bad rodata write at %px\n", ptr); *ptr ^= 0xabcd1234; } void lkdtm_WRITE_RO_AFTER_INIT(void) { unsigned long *ptr = &ro_after_init; /* * Verify we were written to during init. Since an Oops * is considered a "success", a failure is to just skip the * real test. */ if ((*ptr & 0xAA) != 0xAA) { pr_info("%p was NOT written during init!?\n", ptr); return; } pr_info("attempting bad ro_after_init write at %px\n", ptr); *ptr ^= 0xabcd1234; } void lkdtm_WRITE_KERN(void) { size_t size; unsigned char *ptr; size = (unsigned long)do_overwritten - (unsigned long)do_nothing; ptr = (unsigned char *)do_overwritten; pr_info("attempting bad %zu byte write at %px\n", size, ptr); memcpy(ptr, (unsigned char *)do_nothing, size); flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size)); do_overwritten(); } void lkdtm_EXEC_DATA(void) { execute_location(data_area, CODE_WRITE); } void lkdtm_EXEC_STACK(void) { u8 stack_area[EXEC_SIZE]; execute_location(stack_area, CODE_WRITE); } void lkdtm_EXEC_KMALLOC(void) { u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL); execute_location(kmalloc_area, CODE_WRITE); kfree(kmalloc_area); } void lkdtm_EXEC_VMALLOC(void) { u32 *vmalloc_area = vmalloc(EXEC_SIZE); execute_location(vmalloc_area, CODE_WRITE); vfree(vmalloc_area); } void lkdtm_EXEC_RODATA(void) { execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS); } void lkdtm_EXEC_USERSPACE(void) { unsigned long user_addr; user_addr = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, 0); if (user_addr >= TASK_SIZE) { pr_warn("Failed to allocate user memory\n"); return; } execute_user_location((void *)user_addr); vm_munmap(user_addr, PAGE_SIZE); } void lkdtm_EXEC_NULL(void) { execute_location(NULL, CODE_AS_IS); } void lkdtm_ACCESS_USERSPACE(void) { unsigned long user_addr, tmp = 0; unsigned long *ptr; user_addr = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, 0); if (user_addr >= TASK_SIZE) { pr_warn("Failed to allocate user memory\n"); return; } if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) { pr_warn("copy_to_user failed\n"); vm_munmap(user_addr, PAGE_SIZE); return; } ptr = (unsigned long *)user_addr; pr_info("attempting bad read at %px\n", ptr); tmp = *ptr; tmp += 0xc0dec0de; pr_info("attempting bad write at %px\n", ptr); *ptr = tmp; vm_munmap(user_addr, PAGE_SIZE); } void lkdtm_ACCESS_NULL(void) { unsigned long tmp; unsigned long *ptr = (unsigned long *)NULL; pr_info("attempting bad read at %px\n", ptr); tmp = *ptr; tmp += 0xc0dec0de; pr_info("attempting bad write at %px\n", ptr); *ptr = tmp; } void __init lkdtm_perms_init(void) { /* Make sure we can write to __ro_after_init values during __init */ ro_after_init |= 0xAA; } value='linux-5.17.y'>linux-5.17.y Hosts the 0x221E linux distro kernel.Ubuntu
summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2021-05-20tty: n_hdlc: Fix a little doc-rot in n_hdlc_tty_read()Lee Jones
2021-05-13tty: no checking of tty_unregister_ldiscJiri Slaby
2021-05-13tty: make tty_ldisc_ops a param in tty_unregister_ldiscJiri Slaby
2021-05-13tty: set tty_ldisc_ops::num staticallyJiri Slaby
2021-05-13tty: make fp of tty_ldisc_ops::receive_buf{,2} constJiri Slaby
2021-04-15tty: clean include/linux/tty.h upGreg Kroah-Hartman
2021-03-10tty: remove TTY_LDISC_MAGICJiri Slaby
2021-01-20tty: convert tty_ldisc_ops 'read()' function to take a kernel pointerLinus Torvalds
2020-08-31Merge 5.9-rc3 into tty-nextGreg Kroah-Hartman
2020-08-23treewide: Use fallthrough pseudo-keywordGustavo A. R. Silva
2020-08-18tty: ldiscs, fix kernel-docJiri Slaby
2020-05-29n_hdlc_tty_read(): remove pointless access_ok()Al Viro
2020-03-16tty: n_hdlc, remove FILE and LINE from pr_debugJiri Slaby
2020-02-21n_hdlc: wrap a comment properlyJiri Slaby
2020-02-21n_hdlc: fix whitespace around binary operatorsJiri Slaby
2020-02-21n_hdlc: add missing spaces after commasJiri Slaby
2020-02-21n_hdlc: remove spaces between function name and (Jiri Slaby
2020-02-21n_hdlc: remove useless whitespace at line wrapsJiri Slaby
2020-02-21n_hdlc: use __func__ and pr_ print helpersJiri Slaby
2020-02-21n_hdlc: remove unneeded ifdefJiri Slaby
2020-02-21n_hdlc: switch tbusy and woke_up to boolsJiri Slaby
2020-02-21n_hdlc: move tty_ldisc_ops to the bottomJiri Slaby
2020-02-21n_hdlc: add helper for buffers allocationJiri Slaby
2020-02-21n_hdlc: remove checking of n_hdlcJiri Slaby
2020-02-21n_hdlc: remove cached ttyJiri Slaby
2020-02-21n_hdlc: inline n_hdlc_releaseJiri Slaby
2020-02-21n_hdlc: expand tty2n_hdlc macroJiri Slaby
2020-02-21n_hdlc: remove unused backup_ttyJiri Slaby
2020-02-21n_hdlc: remove unused flagsJiri Slaby
2020-02-21n_hdlc: invert conditions in n_hdlc_tty_close and n_hdlc_tty_pollJiri Slaby
2020-02-21n_hdlc: simplify freeing of buffer listJiri Slaby
2020-02-21n_hdlc: use clamp() for maxframeJiri Slaby
2020-02-21n_hdlc: cleanup messages during registrationJiri Slaby
2020-02-21n_hdlc: put init/exit strings directly to printsJiri Slaby
2020-02-21n_hdlc: convert debuglevel use to pr_debugJiri Slaby
2020-02-21n_hdlc: remove unused macrosJiri Slaby
2020-02-21n_hdlc: remove tracing debug printsJiri Slaby
2020-01-23tty: n_hdlc: Use flexible-array member and struct_size() helperGustavo A. R. Silva