lkdtm updates for -next
- Test for new usercopy memory regions - avoid GCC 12 warnings - update expected CONFIGs for selftests -----BEGIN PGP SIGNATURE----- iQJKBAABCgA0FiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmKEGMoWHGtlZXNjb29r QGNocm9taXVtLm9yZwAKCRCJcvTf3G3AJjWvEACa7AYibyhPaikabil0r+qnU5Vr GBbRZbs7+lzzOL7UAJ8VRwNnw3sqsaEKo7phqJRqYvo/etzUGUNtIUFCPMHpw15R sICXpedImOjeVjMYs4FCccEKGlIz0DMXKhNVvMT/IgLANiSwTMjCUGAVWVIf6OtO oXPl5nE3ZIyNYhMPv7hpJ2DB+eEsKqpKv0xkAiZnGpWFhJF0Zex7wb88QAWWO1BP po02NdnvA/f6PIYdpMBlm3UjpGSCD3PfiH+grjZGilGMgVcFoVQJdSSvGPhip6/G 3DVmkSZZe4ldRePn0aJKrwm8bZnqfXgYlu7IKi7y9PSmD8URAtC5PkmnGwkcYxq7 esA9NPJFflzeNxxB6HbH3Dq1DOXiGUi3vQ6SiNieGOtNnCgynrmOtnRrjumG8sZg hdcuXbL0TCOkvy5jQVaLcZfUbH4/hLg1x5qZ2GOl2UKZ69BM0812IQkXOQqBGbKV bQvjflwvBvZ/reuBNfQz10BDTnHL60jFjCNkA5K6+/iziat0/D2JfQ8dDk6I/ywn rXrFnZmXio0WF4lSbEp54l+a1gcNz3Ngi1VJnzDwRifykfIXGFkp8gnWOFymyJ9Z A4W0+yjDkwVsyQcZktS5r9MXhOiEJY+V15k0Rj6mOLRK67sqtgpYbNYZahiyHux4 91DKbOLQlVN7562GYg== =CiSx -----END PGP SIGNATURE----- Merge tag 'lkdtm-next' of https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux into char-misc-next Kees writes: lkdtm updates for -next - Test for new usercopy memory regions - avoid GCC 12 warnings - update expected CONFIGs for selftests * tag 'lkdtm-next' of https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: lkdtm/heap: Hide allocation size from -Warray-bounds selftests/lkdtm: Add configs for stackleak and "after free" tests lkdtm/usercopy: Check vmalloc and >0-order folios lkdtm/usercopy: Rename "heap" to "slab" lkdtm: cfi: Fix type width for masking PAC bits
This commit is contained in:
commit
dc6a7effb4
@ -56,6 +56,7 @@ static void lkdtm_SLAB_LINEAR_OVERFLOW(void)
|
||||
return;
|
||||
|
||||
pr_info("Attempting slab linear overflow ...\n");
|
||||
OPTIMIZER_HIDE_VAR(data);
|
||||
data[1024 / sizeof(u32)] = 0x12345678;
|
||||
kfree(data);
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
#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>
|
||||
@ -130,7 +131,7 @@ free_user:
|
||||
* This checks for whole-object size validation with hardened usercopy,
|
||||
* with or without usercopy whitelisting.
|
||||
*/
|
||||
static void do_usercopy_heap_size(bool to_user)
|
||||
static void do_usercopy_slab_size(bool to_user)
|
||||
{
|
||||
unsigned long user_addr;
|
||||
unsigned char *one, *two;
|
||||
@ -196,9 +197,9 @@ free_kernel:
|
||||
|
||||
/*
|
||||
* This checks for the specific whitelist window within an object. If this
|
||||
* test passes, then do_usercopy_heap_size() tests will pass too.
|
||||
* test passes, then do_usercopy_slab_size() tests will pass too.
|
||||
*/
|
||||
static void do_usercopy_heap_whitelist(bool to_user)
|
||||
static void do_usercopy_slab_whitelist(bool to_user)
|
||||
{
|
||||
unsigned long user_alloc;
|
||||
unsigned char *buf = NULL;
|
||||
@ -272,24 +273,24 @@ free_alloc:
|
||||
}
|
||||
|
||||
/* Callable tests. */
|
||||
static void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
|
||||
static void lkdtm_USERCOPY_SLAB_SIZE_TO(void)
|
||||
{
|
||||
do_usercopy_heap_size(true);
|
||||
do_usercopy_slab_size(true);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
|
||||
static void lkdtm_USERCOPY_SLAB_SIZE_FROM(void)
|
||||
{
|
||||
do_usercopy_heap_size(false);
|
||||
do_usercopy_slab_size(false);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_HEAP_WHITELIST_TO(void)
|
||||
static void lkdtm_USERCOPY_SLAB_WHITELIST_TO(void)
|
||||
{
|
||||
do_usercopy_heap_whitelist(true);
|
||||
do_usercopy_slab_whitelist(true);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_HEAP_WHITELIST_FROM(void)
|
||||
static void lkdtm_USERCOPY_SLAB_WHITELIST_FROM(void)
|
||||
{
|
||||
do_usercopy_heap_whitelist(false);
|
||||
do_usercopy_slab_whitelist(false);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_STACK_FRAME_TO(void)
|
||||
@ -341,6 +342,86 @@ 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.
|
||||
*/
|
||||
static void do_usercopy_page_span(const char *name, void *kaddr)
|
||||
{
|
||||
unsigned long 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;
|
||||
}
|
||||
|
||||
/* Initialize contents. */
|
||||
memset(kaddr, 0xAA, PAGE_SIZE);
|
||||
|
||||
/* Bump the kaddr forward to detect a page-spanning overflow. */
|
||||
kaddr += PAGE_SIZE / 2;
|
||||
|
||||
pr_info("attempting good copy_to_user() from kernel %s: %px\n",
|
||||
name, kaddr);
|
||||
if (copy_to_user((void __user *)uaddr, kaddr,
|
||||
unconst + (PAGE_SIZE / 2))) {
|
||||
pr_err("copy_to_user() failed unexpectedly?!\n");
|
||||
goto free_user;
|
||||
}
|
||||
|
||||
pr_info("attempting bad copy_to_user() from kernel %s: %px\n",
|
||||
name, kaddr);
|
||||
if (copy_to_user((void __user *)uaddr, kaddr, unconst + PAGE_SIZE)) {
|
||||
pr_warn("Good, 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(uaddr, PAGE_SIZE);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_VMALLOC(void)
|
||||
{
|
||||
void *addr;
|
||||
|
||||
addr = vmalloc(PAGE_SIZE);
|
||||
if (!addr) {
|
||||
pr_err("vmalloc() failed!?\n");
|
||||
return;
|
||||
}
|
||||
do_usercopy_page_span("vmalloc", addr);
|
||||
vfree(addr);
|
||||
}
|
||||
|
||||
static void lkdtm_USERCOPY_FOLIO(void)
|
||||
{
|
||||
struct folio *folio;
|
||||
void *addr;
|
||||
|
||||
/*
|
||||
* FIXME: Folio checking currently misses 0-order allocations, so
|
||||
* allocate and bump forward to the last page.
|
||||
*/
|
||||
folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, 1);
|
||||
if (!folio) {
|
||||
pr_err("folio_alloc() failed!?\n");
|
||||
return;
|
||||
}
|
||||
addr = folio_address(folio);
|
||||
if (addr)
|
||||
do_usercopy_page_span("folio", addr + PAGE_SIZE);
|
||||
else
|
||||
pr_err("folio_address() failed?!\n");
|
||||
folio_put(folio);
|
||||
}
|
||||
|
||||
void __init lkdtm_usercopy_init(void)
|
||||
{
|
||||
/* Prepare cache that lacks SLAB_USERCOPY flag. */
|
||||
@ -358,13 +439,15 @@ void __exit lkdtm_usercopy_exit(void)
|
||||
}
|
||||
|
||||
static struct crashtype crashtypes[] = {
|
||||
CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
|
||||
CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
|
||||
CRASHTYPE(USERCOPY_HEAP_WHITELIST_TO),
|
||||
CRASHTYPE(USERCOPY_HEAP_WHITELIST_FROM),
|
||||
CRASHTYPE(USERCOPY_SLAB_SIZE_TO),
|
||||
CRASHTYPE(USERCOPY_SLAB_SIZE_FROM),
|
||||
CRASHTYPE(USERCOPY_SLAB_WHITELIST_TO),
|
||||
CRASHTYPE(USERCOPY_SLAB_WHITELIST_FROM),
|
||||
CRASHTYPE(USERCOPY_STACK_FRAME_TO),
|
||||
CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
|
||||
CRASHTYPE(USERCOPY_STACK_BEYOND),
|
||||
CRASHTYPE(USERCOPY_VMALLOC),
|
||||
CRASHTYPE(USERCOPY_FOLIO),
|
||||
CRASHTYPE(USERCOPY_KERNEL),
|
||||
};
|
||||
|
||||
|
@ -2,8 +2,10 @@ CONFIG_LKDTM=y
|
||||
CONFIG_DEBUG_LIST=y
|
||||
CONFIG_SLAB_FREELIST_HARDENED=y
|
||||
CONFIG_FORTIFY_SOURCE=y
|
||||
CONFIG_GCC_PLUGIN_STACKLEAK=y
|
||||
CONFIG_HARDENED_USERCOPY=y
|
||||
CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT=y
|
||||
CONFIG_INIT_ON_FREE_DEFAULT_ON=y
|
||||
CONFIG_INIT_ON_ALLOC_DEFAULT_ON=y
|
||||
CONFIG_UBSAN=y
|
||||
CONFIG_UBSAN_BOUNDS=y
|
||||
|
@ -64,10 +64,10 @@ REFCOUNT_DEC_AND_TEST_SATURATED Saturation detected: still saturated
|
||||
REFCOUNT_SUB_AND_TEST_SATURATED Saturation detected: still saturated
|
||||
#REFCOUNT_TIMING timing only
|
||||
#ATOMIC_TIMING timing only
|
||||
USERCOPY_HEAP_SIZE_TO
|
||||
USERCOPY_HEAP_SIZE_FROM
|
||||
USERCOPY_HEAP_WHITELIST_TO
|
||||
USERCOPY_HEAP_WHITELIST_FROM
|
||||
USERCOPY_SLAB_SIZE_TO
|
||||
USERCOPY_SLAB_SIZE_FROM
|
||||
USERCOPY_SLAB_WHITELIST_TO
|
||||
USERCOPY_SLAB_WHITELIST_FROM
|
||||
USERCOPY_STACK_FRAME_TO
|
||||
USERCOPY_STACK_FRAME_FROM
|
||||
USERCOPY_STACK_BEYOND
|
||||
|
Loading…
Reference in New Issue
Block a user