refactoring for multiple source files and better layout
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1 Comment: Kees Cook <kees@outflux.net> iQIcBAABCgAGBQJXfpuqAAoJEIly9N/cbcAmXQYQALT1502N/hUp0v0upjS/zVvO AzYK5LnkjMMDdTT5tvOngd44aLg9/h24VnoB4luRM4KGBS/tNOuMFjuEYKEUKsH0 EbVcspWKAGTI07e4lVfpmB+BPENt0HwVtR46RxN68Q0/kp5H25ByLlv2Nj2pAz/3 0JcC3R6/A1k4Hh00a+Lf9uu6rU1gYBmLyx3pWpkjJ7GquXe/t0f4RsbDTU3vKRjj er1aBsBR0peCcE8K2z32HS49dQNk7dCD4ZoAzYoREKPqn2GTzRN+dScdSuKdD1TM YDte3iTf2DIcF7uzzpSKFqnarPaaK7/+rCbvQoVH+isCiaQnlI9KKW9H6XWgEeZg 9J3BmIyqH4+9Iw8Lbs8KoS/Mj5JHfOnZP3N59lyy94vFBecbkT4SshKEfPsScdnW 1Zv8CJu7IP8j5zcGtYCniWAx5Rv37oy9yAAaYhRUr3NQ9ASFaMhkG5QTfx/EZR7C 9TwGoWz+zA6qVkpxCnRKUIZUwwHnDKlZhfbf/cRgxMOnEP/YiUBgXAJ2LDNCbes9 OzJP3lpL7iejGf1CVd+ZlT8ouIUrw/KWd5z4uOeUW2j4CMu3qJisy+idDD253m0W L+2kzPpAFirsWuOhr78Ya7k83+bIkiK9T5Ds2NwcuGj49RZ610IXON4/Wx272mqQ NTw3NJcqTiaVETniTHhU =168T -----END PGP SIGNATURE----- Merge tag 'lkdtm-next' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux into char-misc-testing Kees writes: refactoring for multiple source files and better layout
This commit is contained in:
commit
2a7fbcec95
@ -59,7 +59,11 @@ obj-$(CONFIG_CXL_BASE) += cxl/
|
||||
obj-$(CONFIG_PANEL) += panel.o
|
||||
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_core.o
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_bugs.o
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_heap.o
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_perms.o
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_rodata_objcopy.o
|
||||
lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o
|
||||
|
||||
OBJCOPYFLAGS :=
|
||||
OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \
|
||||
|
@ -1,6 +1,57 @@
|
||||
#ifndef __LKDTM_H
|
||||
#define __LKDTM_H
|
||||
|
||||
/* lkdtm_bugs.c */
|
||||
void __init lkdtm_bugs_init(int *recur_param);
|
||||
void lkdtm_PANIC(void);
|
||||
void lkdtm_BUG(void);
|
||||
void lkdtm_WARNING(void);
|
||||
void lkdtm_EXCEPTION(void);
|
||||
void lkdtm_LOOP(void);
|
||||
void lkdtm_OVERFLOW(void);
|
||||
void lkdtm_CORRUPT_STACK(void);
|
||||
void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void);
|
||||
void lkdtm_SOFTLOCKUP(void);
|
||||
void lkdtm_HARDLOCKUP(void);
|
||||
void lkdtm_SPINLOCKUP(void);
|
||||
void lkdtm_HUNG_TASK(void);
|
||||
void lkdtm_ATOMIC_UNDERFLOW(void);
|
||||
void lkdtm_ATOMIC_OVERFLOW(void);
|
||||
|
||||
/* lkdtm_heap.c */
|
||||
void lkdtm_OVERWRITE_ALLOCATION(void);
|
||||
void lkdtm_WRITE_AFTER_FREE(void);
|
||||
void lkdtm_READ_AFTER_FREE(void);
|
||||
void lkdtm_WRITE_BUDDY_AFTER_FREE(void);
|
||||
void lkdtm_READ_BUDDY_AFTER_FREE(void);
|
||||
|
||||
/* lkdtm_perms.c */
|
||||
void __init lkdtm_perms_init(void);
|
||||
void lkdtm_WRITE_RO(void);
|
||||
void lkdtm_WRITE_RO_AFTER_INIT(void);
|
||||
void lkdtm_WRITE_KERN(void);
|
||||
void lkdtm_EXEC_DATA(void);
|
||||
void lkdtm_EXEC_STACK(void);
|
||||
void lkdtm_EXEC_KMALLOC(void);
|
||||
void lkdtm_EXEC_VMALLOC(void);
|
||||
void lkdtm_EXEC_RODATA(void);
|
||||
void lkdtm_EXEC_USERSPACE(void);
|
||||
void lkdtm_ACCESS_USERSPACE(void);
|
||||
|
||||
/* lkdtm_rodata.c */
|
||||
void lkdtm_rodata_do_nothing(void);
|
||||
|
||||
/* lkdtm_usercopy.c */
|
||||
void __init lkdtm_usercopy_init(void);
|
||||
void __exit lkdtm_usercopy_exit(void);
|
||||
void lkdtm_USERCOPY_HEAP_SIZE_TO(void);
|
||||
void lkdtm_USERCOPY_HEAP_SIZE_FROM(void);
|
||||
void lkdtm_USERCOPY_HEAP_FLAG_TO(void);
|
||||
void lkdtm_USERCOPY_HEAP_FLAG_FROM(void);
|
||||
void lkdtm_USERCOPY_STACK_FRAME_TO(void);
|
||||
void lkdtm_USERCOPY_STACK_FRAME_FROM(void);
|
||||
void lkdtm_USERCOPY_STACK_BEYOND(void);
|
||||
void lkdtm_USERCOPY_KERNEL(void);
|
||||
|
||||
|
||||
#endif
|
||||
|
152
drivers/misc/lkdtm_bugs.c
Normal file
152
drivers/misc/lkdtm_bugs.c
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* This is for all the tests related to logic bugs (e.g. bad dereferences,
|
||||
* bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
|
||||
* lockups) along with other things that don't fit well into existing LKDTM
|
||||
* test source files.
|
||||
*/
|
||||
#define pr_fmt(fmt) "lkdtm: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/sched.h>
|
||||
|
||||
#include "lkdtm.h"
|
||||
|
||||
/*
|
||||
* Make sure our attempts to over run the kernel stack doesn't trigger
|
||||
* a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
|
||||
* recurse past the end of THREAD_SIZE by default.
|
||||
*/
|
||||
#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
|
||||
#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
|
||||
#else
|
||||
#define REC_STACK_SIZE (THREAD_SIZE / 8)
|
||||
#endif
|
||||
#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
|
||||
|
||||
static int recur_count = REC_NUM_DEFAULT;
|
||||
|
||||
static DEFINE_SPINLOCK(lock_me_up);
|
||||
|
||||
static int recursive_loop(int remaining)
|
||||
{
|
||||
char buf[REC_STACK_SIZE];
|
||||
|
||||
/* Make sure compiler does not optimize this away. */
|
||||
memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
|
||||
if (!remaining)
|
||||
return 0;
|
||||
else
|
||||
return recursive_loop(remaining - 1);
|
||||
}
|
||||
|
||||
/* If the depth is negative, use the default, otherwise keep parameter. */
|
||||
void __init lkdtm_bugs_init(int *recur_param)
|
||||
{
|
||||
if (*recur_param < 0)
|
||||
*recur_param = recur_count;
|
||||
else
|
||||
recur_count = *recur_param;
|
||||
}
|
||||
|
||||
void lkdtm_PANIC(void)
|
||||
{
|
||||
panic("dumptest");
|
||||
}
|
||||
|
||||
void lkdtm_BUG(void)
|
||||
{
|
||||
BUG();
|
||||
}
|
||||
|
||||
void lkdtm_WARNING(void)
|
||||
{
|
||||
WARN_ON(1);
|
||||
}
|
||||
|
||||
void lkdtm_EXCEPTION(void)
|
||||
{
|
||||
*((int *) 0) = 0;
|
||||
}
|
||||
|
||||
void lkdtm_LOOP(void)
|
||||
{
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
|
||||
void lkdtm_OVERFLOW(void)
|
||||
{
|
||||
(void) recursive_loop(recur_count);
|
||||
}
|
||||
|
||||
noinline void lkdtm_CORRUPT_STACK(void)
|
||||
{
|
||||
/* Use default char array length that triggers stack protection. */
|
||||
char data[8];
|
||||
|
||||
memset((void *)data, 0, 64);
|
||||
}
|
||||
|
||||
void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
|
||||
{
|
||||
static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
|
||||
u32 *p;
|
||||
u32 val = 0x12345678;
|
||||
|
||||
p = (u32 *)(data + 1);
|
||||
if (*p == 0)
|
||||
val = 0x87654321;
|
||||
*p = val;
|
||||
}
|
||||
|
||||
void lkdtm_SOFTLOCKUP(void)
|
||||
{
|
||||
preempt_disable();
|
||||
for (;;)
|
||||
cpu_relax();
|
||||
}
|
||||
|
||||
void lkdtm_HARDLOCKUP(void)
|
||||
{
|
||||
local_irq_disable();
|
||||
for (;;)
|
||||
cpu_relax();
|
||||
}
|
||||
|
||||
void lkdtm_SPINLOCKUP(void)
|
||||
{
|
||||
/* Must be called twice to trigger. */
|
||||
spin_lock(&lock_me_up);
|
||||
/* Let sparse know we intended to exit holding the lock. */
|
||||
__release(&lock_me_up);
|
||||
}
|
||||
|
||||
void lkdtm_HUNG_TASK(void)
|
||||
{
|
||||
set_current_state(TASK_UNINTERRUPTIBLE);
|
||||
schedule();
|
||||
}
|
||||
|
||||
void lkdtm_ATOMIC_UNDERFLOW(void)
|
||||
{
|
||||
atomic_t under = ATOMIC_INIT(INT_MIN);
|
||||
|
||||
pr_info("attempting good atomic increment\n");
|
||||
atomic_inc(&under);
|
||||
atomic_dec(&under);
|
||||
|
||||
pr_info("attempting bad atomic underflow\n");
|
||||
atomic_dec(&under);
|
||||
}
|
||||
|
||||
void lkdtm_ATOMIC_OVERFLOW(void)
|
||||
{
|
||||
atomic_t over = ATOMIC_INIT(INT_MAX);
|
||||
|
||||
pr_info("attempting good atomic decrement\n");
|
||||
atomic_dec(&over);
|
||||
atomic_inc(&over);
|
||||
|
||||
pr_info("attempting bad atomic overflow\n");
|
||||
atomic_inc(&over);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
146
drivers/misc/lkdtm_heap.c
Normal file
146
drivers/misc/lkdtm_heap.c
Normal file
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* This is for all the tests relating directly to heap memory, including
|
||||
* page allocation and slab allocations.
|
||||
*/
|
||||
#define pr_fmt(fmt) "lkdtm: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
#include "lkdtm.h"
|
||||
|
||||
/*
|
||||
* This tries to stay within the next largest power-of-2 kmalloc cache
|
||||
* to avoid actually overwriting anything important if it's not detected
|
||||
* correctly.
|
||||
*/
|
||||
void lkdtm_OVERWRITE_ALLOCATION(void)
|
||||
{
|
||||
size_t len = 1020;
|
||||
u32 *data = kmalloc(len, GFP_KERNEL);
|
||||
|
||||
data[1024 / sizeof(u32)] = 0x12345678;
|
||||
kfree(data);
|
||||
}
|
||||
|
||||
void lkdtm_WRITE_AFTER_FREE(void)
|
||||
{
|
||||
int *base, *again;
|
||||
size_t len = 1024;
|
||||
/*
|
||||
* The slub allocator uses the first word to store the free
|
||||
* pointer in some configurations. Use the middle of the
|
||||
* allocation to avoid running into the freelist
|
||||
*/
|
||||
size_t offset = (len / sizeof(*base)) / 2;
|
||||
|
||||
base = kmalloc(len, GFP_KERNEL);
|
||||
pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
|
||||
pr_info("Attempting bad write to freed memory at %p\n",
|
||||
&base[offset]);
|
||||
kfree(base);
|
||||
base[offset] = 0x0abcdef0;
|
||||
/* Attempt to notice the overwrite. */
|
||||
again = kmalloc(len, GFP_KERNEL);
|
||||
kfree(again);
|
||||
if (again != base)
|
||||
pr_info("Hmm, didn't get the same memory range.\n");
|
||||
}
|
||||
|
||||
void lkdtm_READ_AFTER_FREE(void)
|
||||
{
|
||||
int *base, *val, saw;
|
||||
size_t len = 1024;
|
||||
/*
|
||||
* The slub allocator uses the first word to store the free
|
||||
* pointer in some configurations. Use the middle of the
|
||||
* allocation to avoid running into the freelist
|
||||
*/
|
||||
size_t offset = (len / sizeof(*base)) / 2;
|
||||
|
||||
base = kmalloc(len, GFP_KERNEL);
|
||||
if (!base) {
|
||||
pr_info("Unable to allocate base memory.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
val = kmalloc(len, GFP_KERNEL);
|
||||
if (!val) {
|
||||
pr_info("Unable to allocate val memory.\n");
|
||||
kfree(base);
|
||||
return;
|
||||
}
|
||||
|
||||
*val = 0x12345678;
|
||||
base[offset] = *val;
|
||||
pr_info("Value in memory before free: %x\n", base[offset]);
|
||||
|
||||
kfree(base);
|
||||
|
||||
pr_info("Attempting bad read from freed memory\n");
|
||||
saw = base[offset];
|
||||
if (saw != *val) {
|
||||
/* Good! Poisoning happened, so declare a win. */
|
||||
pr_info("Memory correctly poisoned (%x)\n", saw);
|
||||
BUG();
|
||||
}
|
||||
pr_info("Memory was not poisoned\n");
|
||||
|
||||
kfree(val);
|
||||
}
|
||||
|
||||
void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
|
||||
{
|
||||
unsigned long p = __get_free_page(GFP_KERNEL);
|
||||
if (!p) {
|
||||
pr_info("Unable to allocate free page\n");
|
||||
return;
|
||||
}
|
||||
|
||||
pr_info("Writing to the buddy page before free\n");
|
||||
memset((void *)p, 0x3, PAGE_SIZE);
|
||||
free_page(p);
|
||||
schedule();
|
||||
pr_info("Attempting bad write to the buddy page after free\n");
|
||||
memset((void *)p, 0x78, PAGE_SIZE);
|
||||
/* Attempt to notice the overwrite. */
|
||||
p = __get_free_page(GFP_KERNEL);
|
||||
free_page(p);
|
||||
schedule();
|
||||
}
|
||||
|
||||
void lkdtm_READ_BUDDY_AFTER_FREE(void)
|
||||
{
|
||||
unsigned long p = __get_free_page(GFP_KERNEL);
|
||||
int saw, *val;
|
||||
int *base;
|
||||
|
||||
if (!p) {
|
||||
pr_info("Unable to allocate free page\n");
|
||||
return;
|
||||
}
|
||||
|
||||
val = kmalloc(1024, GFP_KERNEL);
|
||||
if (!val) {
|
||||
pr_info("Unable to allocate val memory.\n");
|
||||
free_page(p);
|
||||
return;
|
||||
}
|
||||
|
||||
base = (int *)p;
|
||||
|
||||
*val = 0x12345678;
|
||||
base[0] = *val;
|
||||
pr_info("Value in memory before free: %x\n", base[0]);
|
||||
free_page(p);
|
||||
pr_info("Attempting to read from freed memory\n");
|
||||
saw = base[0];
|
||||
if (saw != *val) {
|
||||
/* Good! Poisoning happened, so declare a win. */
|
||||
pr_info("Memory correctly poisoned (%x)\n", saw);
|
||||
BUG();
|
||||
}
|
||||
pr_info("Buddy page was not poisoned\n");
|
||||
|
||||
kfree(val);
|
||||
}
|
203
drivers/misc/lkdtm_perms.c
Normal file
203
drivers/misc/lkdtm_perms.c
Normal file
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* This is for all the tests related to validating kernel memory
|
||||
* permissions: non-executable regions, non-writable regions, and
|
||||
* even non-readable regions.
|
||||
*/
|
||||
#define pr_fmt(fmt) "lkdtm: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/vmalloc.h>
|
||||
#include <linux/mman.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <asm/cacheflush.h>
|
||||
|
||||
#include "lkdtm.h"
|
||||
|
||||
/* 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 %p\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 %p\n", func);
|
||||
func();
|
||||
}
|
||||
|
||||
static void execute_user_location(void *dst)
|
||||
{
|
||||
/* Intentionally crossing kernel/user memory boundary. */
|
||||
void (*func)(void) = dst;
|
||||
|
||||
pr_info("attempting ok execution at %p\n", do_nothing);
|
||||
do_nothing();
|
||||
|
||||
if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
|
||||
return;
|
||||
flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
|
||||
pr_info("attempting bad execution at %p\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 %p\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 %p\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 %p\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_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 %p\n", ptr);
|
||||
tmp = *ptr;
|
||||
tmp += 0xc0dec0de;
|
||||
|
||||
pr_info("attempting bad write at %p\n", ptr);
|
||||
*ptr = tmp;
|
||||
|
||||
vm_munmap(user_addr, PAGE_SIZE);
|
||||
}
|
||||
|
||||
void __init lkdtm_perms_init(void)
|
||||
{
|
||||
/* Make sure we can write to __ro_after_init values during __init */
|
||||
ro_after_init |= 0xAA;
|
||||
|
||||
}
|
315
drivers/misc/lkdtm_usercopy.c
Normal file
315
drivers/misc/lkdtm_usercopy.c
Normal file
@ -0,0 +1,315 @@
|
||||
/*
|
||||
* This is for all the tests related to copy_to_user() and copy_from_user()
|
||||
* hardening.
|
||||
*/
|
||||
#define pr_fmt(fmt) "lkdtm: " fmt
|
||||
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/vmalloc.h>
|
||||
#include <linux/mman.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <asm/cacheflush.h>
|
||||
|
||||
static size_t cache_size = 1024;
|
||||
static struct kmem_cache *bad_cache;
|
||||
|
||||
static const unsigned char test_text[] = "This is a test.\n";
|
||||
|
||||
/*
|
||||
* Instead of adding -Wno-return-local-addr, just pass the stack address
|
||||
* through a function to obfuscate it from the compiler.
|
||||
*/
|
||||
static noinline unsigned char *trick_compiler(unsigned char *stack)
|
||||
{
|
||||
return stack + 0;
|
||||
}
|
||||
|
||||
static noinline unsigned char *do_usercopy_stack_callee(int value)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
int i;
|
||||
|
||||
/* Exercise stack to avoid everything living in registers. */
|
||||
for (i = 0; i < sizeof(buf); i++) {
|
||||
buf[i] = value & 0xff;
|
||||
}
|
||||
|
||||
return trick_compiler(buf);
|
||||
}
|
||||
|
||||
static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
|
||||
{
|
||||
unsigned long user_addr;
|
||||
unsigned char good_stack[32];
|
||||
unsigned char *bad_stack;
|
||||
int i;
|
||||
|
||||
/* Exercise stack to avoid everything living in registers. */
|
||||
for (i = 0; i < sizeof(good_stack); i++)
|
||||
good_stack[i] = test_text[i % sizeof(test_text)];
|
||||
|
||||
/* This is a pointer to outside our current stack frame. */
|
||||
if (bad_frame) {
|
||||
bad_stack = do_usercopy_stack_callee((uintptr_t)bad_stack);
|
||||
} else {
|
||||
/* Put start address just inside stack. */
|
||||
bad_stack = task_stack_page(current) + THREAD_SIZE;
|
||||
bad_stack -= sizeof(unsigned long);
|
||||
}
|
||||
|
||||
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 (to_user) {
|
||||
pr_info("attempting good copy_to_user of local stack\n");
|
||||
if (copy_to_user((void __user *)user_addr, good_stack,
|
||||
sizeof(good_stack))) {
|
||||
pr_warn("copy_to_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_to_user of distant stack\n");
|
||||
if (copy_to_user((void __user *)user_addr, bad_stack,
|
||||
sizeof(good_stack))) {
|
||||
pr_warn("copy_to_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* There isn't a safe way to not be protected by usercopy
|
||||
* if we're going to write to another thread's stack.
|
||||
*/
|
||||
if (!bad_frame)
|
||||
goto free_user;
|
||||
|
||||
pr_info("attempting good copy_from_user of local stack\n");
|
||||
if (copy_from_user(good_stack, (void __user *)user_addr,
|
||||
sizeof(good_stack))) {
|
||||
pr_warn("copy_from_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_from_user of distant stack\n");
|
||||
if (copy_from_user(bad_stack, (void __user *)user_addr,
|
||||
sizeof(good_stack))) {
|
||||
pr_warn("copy_from_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
}
|
||||
|
||||
free_user:
|
||||
vm_munmap(user_addr, PAGE_SIZE);
|
||||
}
|
||||
|
||||
static void do_usercopy_heap_size(bool to_user)
|
||||
{
|
||||
unsigned long user_addr;
|
||||
unsigned char *one, *two;
|
||||
const size_t size = 1024;
|
||||
|
||||
one = kmalloc(size, GFP_KERNEL);
|
||||
two = kmalloc(size, GFP_KERNEL);
|
||||
if (!one || !two) {
|
||||
pr_warn("Failed to allocate kernel memory\n");
|
||||
goto free_kernel;
|
||||
}
|
||||
|
||||
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");
|
||||
goto free_kernel;
|
||||
}
|
||||
|
||||
memset(one, 'A', size);
|
||||
memset(two, 'B', size);
|
||||
|
||||
if (to_user) {
|
||||
pr_info("attempting good copy_to_user of correct size\n");
|
||||
if (copy_to_user((void __user *)user_addr, one, size)) {
|
||||
pr_warn("copy_to_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_to_user of too large size\n");
|
||||
if (copy_to_user((void __user *)user_addr, one, 2 * size)) {
|
||||
pr_warn("copy_to_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
} else {
|
||||
pr_info("attempting good copy_from_user of correct size\n");
|
||||
if (copy_from_user(one, (void __user *)user_addr, size)) {
|
||||
pr_warn("copy_from_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_from_user of too large size\n");
|
||||
if (copy_from_user(one, (void __user *)user_addr, 2 * size)) {
|
||||
pr_warn("copy_from_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
}
|
||||
|
||||
free_user:
|
||||
vm_munmap(user_addr, PAGE_SIZE);
|
||||
free_kernel:
|
||||
kfree(one);
|
||||
kfree(two);
|
||||
}
|
||||
|
||||
static void do_usercopy_heap_flag(bool to_user)
|
||||
{
|
||||
unsigned long user_addr;
|
||||
unsigned char *good_buf = NULL;
|
||||
unsigned char *bad_buf = NULL;
|
||||
|
||||
/* Make sure cache was prepared. */
|
||||
if (!bad_cache) {
|
||||
pr_warn("Failed to allocate kernel cache\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate one buffer from each cache (kmalloc will have the
|
||||
* SLAB_USERCOPY flag already, but "bad_cache" won't).
|
||||
*/
|
||||
good_buf = kmalloc(cache_size, GFP_KERNEL);
|
||||
bad_buf = kmem_cache_alloc(bad_cache, GFP_KERNEL);
|
||||
if (!good_buf || !bad_buf) {
|
||||
pr_warn("Failed to allocate buffers from caches\n");
|
||||
goto free_alloc;
|
||||
}
|
||||
|
||||
/* Allocate user memory we'll poke at. */
|
||||
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");
|
||||
goto free_alloc;
|
||||
}
|
||||
|
||||
memset(good_buf, 'A', cache_size);
|
||||
memset(bad_buf, 'B', cache_size);
|
||||
|
||||
if (to_user) {
|
||||
pr_info("attempting good copy_to_user with SLAB_USERCOPY\n");
|
||||
if (copy_to_user((void __user *)user_addr, good_buf,
|
||||
cache_size)) {
|
||||
pr_warn("copy_to_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_to_user w/o SLAB_USERCOPY\n");
|
||||
if (copy_to_user((void __user *)user_addr, bad_buf,
|
||||
cache_size)) {
|
||||
pr_warn("copy_to_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
} else {
|
||||
pr_info("attempting good copy_from_user with SLAB_USERCOPY\n");
|
||||
if (copy_from_user(good_buf, (void __user *)user_addr,
|
||||
cache_size)) {
|
||||
pr_warn("copy_from_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_from_user w/o SLAB_USERCOPY\n");
|
||||
if (copy_from_user(bad_buf, (void __user *)user_addr,
|
||||
cache_size)) {
|
||||
pr_warn("copy_from_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
}
|
||||
|
||||
free_user:
|
||||
vm_munmap(user_addr, PAGE_SIZE);
|
||||
free_alloc:
|
||||
if (bad_buf)
|
||||
kmem_cache_free(bad_cache, bad_buf);
|
||||
kfree(good_buf);
|
||||
}
|
||||
|
||||
/* Callable tests. */
|
||||
void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
|
||||
{
|
||||
do_usercopy_heap_size(true);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
|
||||
{
|
||||
do_usercopy_heap_size(false);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_HEAP_FLAG_TO(void)
|
||||
{
|
||||
do_usercopy_heap_flag(true);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_HEAP_FLAG_FROM(void)
|
||||
{
|
||||
do_usercopy_heap_flag(false);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_STACK_FRAME_TO(void)
|
||||
{
|
||||
do_usercopy_stack(true, true);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
|
||||
{
|
||||
do_usercopy_stack(false, true);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_STACK_BEYOND(void)
|
||||
{
|
||||
do_usercopy_stack(true, false);
|
||||
}
|
||||
|
||||
void lkdtm_USERCOPY_KERNEL(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;
|
||||
}
|
||||
|
||||
pr_info("attempting good copy_to_user from kernel rodata\n");
|
||||
if (copy_to_user((void __user *)user_addr, test_text,
|
||||
sizeof(test_text))) {
|
||||
pr_warn("copy_to_user failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_to_user from kernel text\n");
|
||||
if (copy_to_user((void __user *)user_addr, vm_mmap, PAGE_SIZE)) {
|
||||
pr_warn("copy_to_user failed, but lacked Oops\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
free_user:
|
||||
vm_munmap(user_addr, PAGE_SIZE);
|
||||
}
|
||||
|
||||
void __init lkdtm_usercopy_init(void)
|
||||
{
|
||||
/* Prepare cache that lacks SLAB_USERCOPY flag. */
|
||||
bad_cache = kmem_cache_create("lkdtm-no-usercopy", cache_size, 0,
|
||||
0, NULL);
|
||||
}
|
||||
|
||||
void __exit lkdtm_usercopy_exit(void)
|
||||
{
|
||||
kmem_cache_destroy(bad_cache);
|
||||
}
|
Loading…
Reference in New Issue
Block a user