cregit-Linux how code gets into the kernel

Release 4.14 drivers/misc/lkdtm_bugs.c

Directory: drivers/misc
// SPDX-License-Identifier: GPL-2.0
/*
 * 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.
 */
#include "lkdtm.h"
#include <linux/list.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/sched/task_stack.h>
#include <linux/uaccess.h>


struct lkdtm_list {
	
struct list_head node;
};

/*
 * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook48100.00%1100.00%
Total48100.00%1100.00%

/* 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; }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook28100.00%1100.00%
Total28100.00%1100.00%


void lkdtm_PANIC(void) { panic("dumptest"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook12100.00%1100.00%
Total12100.00%1100.00%


void lkdtm_BUG(void) { BUG(); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook10100.00%1100.00%
Total10100.00%1100.00%


void lkdtm_WARNING(void) { WARN_ON(1); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook12100.00%1100.00%
Total12100.00%1100.00%


void lkdtm_EXCEPTION(void) { *((volatile int *) 0) = 0; }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook1894.74%150.00%
Michael Davidson15.26%150.00%
Total19100.00%2100.00%


void lkdtm_LOOP(void) { for (;;) ; }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook13100.00%1100.00%
Total13100.00%1100.00%


void lkdtm_OVERFLOW(void) { (void) recursive_loop(recur_count); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook15100.00%1100.00%
Total15100.00%1100.00%


static noinline void __lkdtm_CORRUPT_STACK(void *stack) { memset(stack, '\xff', 64); }

Contributors

PersonTokensPropCommitsCommitProp
Arnd Bergmann1995.00%150.00%
Kees Cook15.00%150.00%
Total20100.00%2100.00%

/* This should trip the stack canary, not corrupt the return address. */
noinline void lkdtm_CORRUPT_STACK(void) { /* Use default char array length that triggers stack protection. */ char data[8] __aligned(sizeof(void *)); __lkdtm_CORRUPT_STACK(&data); pr_info("Corrupted stack containing char array ...\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook3088.24%250.00%
Arnd Bergmann25.88%125.00%
Michael Ellerman25.88%125.00%
Total34100.00%4100.00%

/* Same as above but will only get a canary with -fstack-protector-strong */
noinline void lkdtm_CORRUPT_STACK_STRONG(void) { union { unsigned short shorts[4]; unsigned long *ptr; } data __aligned(sizeof(void *)); __lkdtm_CORRUPT_STACK(&data); pr_info("Corrupted stack containing union ...\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook4193.18%266.67%
Michael Ellerman36.82%133.33%
Total44100.00%3100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook72100.00%1100.00%
Total72100.00%1100.00%


void lkdtm_SOFTLOCKUP(void) { preempt_disable(); for (;;) cpu_relax(); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook18100.00%1100.00%
Total18100.00%1100.00%


void lkdtm_HARDLOCKUP(void) { local_irq_disable(); for (;;) cpu_relax(); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook18100.00%1100.00%
Total18100.00%1100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook21100.00%1100.00%
Total21100.00%1100.00%


void lkdtm_HUNG_TASK(void) { set_current_state(TASK_UNINTERRUPTIBLE); schedule(); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook15100.00%1100.00%
Total15100.00%1100.00%


void lkdtm_CORRUPT_LIST_ADD(void) { /* * Initially, an empty list via LIST_HEAD: * test_head.next = &test_head * test_head.prev = &test_head */ LIST_HEAD(test_head); struct lkdtm_list good, bad; void *target[2] = { }; void *redirection = &target; pr_info("attempting good list addition\n"); /* * Adding to the list performs these actions: * test_head.next->prev = &good.node * good.node.next = test_head.next * good.node.prev = test_head * test_head.next = good.node */ list_add(&good.node, &test_head); pr_info("attempting corrupted list addition\n"); /* * In simulating this "write what where" primitive, the "what" is * the address of &bad.node, and the "where" is the address held * by "redirection". */ test_head.next = redirection; list_add(&bad.node, &test_head); if (target[0] == NULL && target[1] == NULL) pr_err("Overwrite did not happen, but no BUG?!\n"); else pr_err("list_add() corruption not detected!\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook102100.00%1100.00%
Total102100.00%1100.00%


void lkdtm_CORRUPT_LIST_DEL(void) { LIST_HEAD(test_head); struct lkdtm_list item; void *target[2] = { }; void *redirection = &target; list_add(&item.node, &test_head); pr_info("attempting good list removal\n"); list_del(&item.node); pr_info("attempting corrupted list removal\n"); list_add(&item.node, &test_head); /* As with the list_add() test above, this corrupts "next". */ item.node.next = redirection; list_del(&item.node); if (target[0] == NULL && target[1] == NULL) pr_err("Overwrite did not happen, but no BUG?!\n"); else pr_err("list_del() corruption not detected!\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook116100.00%1100.00%
Total116100.00%1100.00%

/* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
void lkdtm_CORRUPT_USER_DS(void) { pr_info("setting bad task size limit\n"); set_fs(KERNEL_DS); /* Make sure we do not keep running with a KERNEL_DS! */ force_sig(SIGKILL, current); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook25100.00%1100.00%
Total25100.00%1100.00%

/* Test that VMAP_STACK is actually allocating with a leading guard page */
void lkdtm_STACK_GUARD_PAGE_LEADING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack - 1; volatile unsigned char byte; pr_info("attempting bad read from page below current stack\n"); byte = *ptr; pr_err("FAIL: accessed page before stack!\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook48100.00%1100.00%
Total48100.00%1100.00%

/* Test that VMAP_STACK is actually allocating with a trailing guard page */
void lkdtm_STACK_GUARD_PAGE_TRAILING(void) { const unsigned char *stack = task_stack_page(current); const unsigned char *ptr = stack + THREAD_SIZE; volatile unsigned char byte; pr_info("attempting bad read from page above current stack\n"); byte = *ptr; pr_err("FAIL: accessed page after stack!\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Kees Cook48100.00%1100.00%
Total48100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Kees Cook78696.56%660.00%
Arnd Bergmann212.58%110.00%
Michael Ellerman50.61%110.00%
Michael Davidson10.12%110.00%
Greg Kroah-Hartman10.12%110.00%
Total814100.00%10100.00%
Directory: drivers/misc
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.