// SPDX-License-Identifier: GPL-2.0 /* * This is for all the tests related to copy_to_user() and copy_from_user() * hardening.
*/ #include"lkdtm.h" #include <linux/slab.h> #include <linux/highmem.h> #include <linux/vmalloc.h> #include <linux/sched/task_stack.h> #include <linux/mman.h> #include <linux/uaccess.h> #include <asm/cacheflush.h>
/* * Many of the tests here end up using const sizes, but those would * normally be ignored by hardened usercopy, so force the compiler * into choosing the non-const path to make sure we trigger the * hardened usercopy checks by added "unconst" to all the const copies, * and making sure "cache_size" isn't optimized into a const.
*/ staticvolatile size_t unconst; staticvolatile size_t cache_size = 1024; staticstruct kmem_cache *whitelist_cache;
staticconstunsignedchar 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 unsignedchar *trick_compiler(unsignedchar *stack)
{ return stack + unconst;
}
static noinline unsignedchar *do_usercopy_stack_callee(int value)
{ unsignedchar buf[128]; int i;
/* Exercise stack to avoid everything living in registers. */ for (i = 0; i < sizeof(buf); i++) {
buf[i] = value & 0xff;
}
/* * Put the target buffer in the middle of stack allocation * so that we don't step on future stack users regardless * of stack growth direction.
*/ return trick_compiler(&buf[(128/2)-32]);
}
/* 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(unsignedlong);
}
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,
unconst + 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,
unconst + 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,
unconst + 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,
unconst + sizeof(good_stack))) {
pr_warn("copy_from_user failed, but lacked Oops\n"); goto free_user;
}
}
free_user:
vm_munmap(user_addr, PAGE_SIZE);
}
/* * This checks for whole-object size validation with hardened usercopy, * with or without usercopy whitelisting.
*/ staticvoid do_usercopy_slab_size(bool to_user)
{ unsignedlong user_addr; unsignedchar *one, *two; void __user *test_user_addr; void *test_kern_addr;
size_t size = unconst + 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;
}
if (to_user) {
pr_info("attempting good copy_to_user of correct size\n"); if (copy_to_user(test_user_addr, test_kern_addr, size / 2)) {
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(test_user_addr, test_kern_addr, 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(test_kern_addr, test_user_addr, size / 2)) {
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(test_kern_addr, test_user_addr, size)) {
pr_warn("copy_from_user failed, but lacked Oops\n"); goto free_user;
}
}
pr_err("FAIL: bad usercopy not detected!\n");
pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
/* * This checks for the specific whitelist window within an object. If this * test passes, then do_usercopy_slab_size() tests will pass too.
*/ staticvoid do_usercopy_slab_whitelist(bool to_user)
{ unsignedlong user_alloc; unsignedchar *buf = NULL; unsignedchar __user *user_addr;
size_t offset, size;
/* Make sure cache was prepared. */ if (!whitelist_cache) {
pr_warn("Failed to allocate kernel cache\n"); return;
}
/* * Allocate a buffer with a whitelisted window in the buffer.
*/
buf = kmem_cache_alloc(whitelist_cache, GFP_KERNEL); if (!buf) {
pr_warn("Failed to allocate buffer from whitelist cache\n"); goto free_alloc;
}
/* Allocate user memory we'll poke at. */
user_alloc = vm_mmap(NULL, 0, PAGE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE, 0); if (user_alloc >= TASK_SIZE) {
pr_warn("Failed to allocate user memory\n"); goto free_alloc;
}
user_addr = (void __user *)user_alloc;
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: %px\n",
test_text); if (copy_to_user((void __user *)user_addr, test_text,
unconst + sizeof(test_text))) {
pr_warn("copy_to_user failed unexpectedly?!\n"); goto free_user;
}
pr_info("attempting bad copy_to_user from kernel text: %px\n",
vm_mmap); if (copy_to_user((void __user *)user_addr, vm_mmap,
unconst + PAGE_SIZE)) {
pr_warn("copy_to_user failed, but lacked Oops\n"); goto free_user;
}
pr_err("FAIL: bad copy_to_user() not detected!\n");
pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
free_user:
vm_munmap(user_addr, PAGE_SIZE);
}
/* * This expects "kaddr" to point to a PAGE_SIZE allocation, which means * a more complete test that would include copy_from_user() would risk * memory corruption. Just test copy_to_user() here, as that exercises * almost exactly the same code paths.
*/ staticvoid do_usercopy_page_span(constchar *name, void *kaddr)
{ unsignedlong uaddr;
uaddr = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, 0); if (uaddr >= TASK_SIZE) {
pr_warn("Failed to allocate user memory\n"); return;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.