Skip to content

Commit

Permalink
lkdtm: Add tests for NULL pointer dereference
Browse files Browse the repository at this point in the history
[ Upstream commit 59a1220 ]

Introduce lkdtm tests for NULL pointer dereference: check access or exec
at NULL address, since these errors tend to be reported differently from
the general fault error text. For example from x86:

    pr_alert("BUG: unable to handle kernel %s at %px\n",
        address < PAGE_SIZE ? "NULL pointer dereference" : "paging request",
        (void *)address);

Signed-off-by: Christophe Leroy <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
chleroy authored and gregkh committed Apr 20, 2019
1 parent b035faf commit f2778b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/misc/lkdtm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void lkdtm_EXEC_KMALLOC(void);
void lkdtm_EXEC_VMALLOC(void);
void lkdtm_EXEC_RODATA(void);
void lkdtm_EXEC_USERSPACE(void);
void lkdtm_EXEC_NULL(void);
void lkdtm_ACCESS_USERSPACE(void);
void lkdtm_ACCESS_NULL(void);

/* lkdtm_refcount.c */
void lkdtm_REFCOUNT_INC_OVERFLOW(void);
Expand Down
2 changes: 2 additions & 0 deletions drivers/misc/lkdtm_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ struct crashtype crashtypes[] = {
CRASHTYPE(EXEC_VMALLOC),
CRASHTYPE(EXEC_RODATA),
CRASHTYPE(EXEC_USERSPACE),
CRASHTYPE(EXEC_NULL),
CRASHTYPE(ACCESS_USERSPACE),
CRASHTYPE(ACCESS_NULL),
CRASHTYPE(WRITE_RO),
CRASHTYPE(WRITE_RO_AFTER_INIT),
CRASHTYPE(WRITE_KERN),
Expand Down
18 changes: 18 additions & 0 deletions drivers/misc/lkdtm_perms.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ void lkdtm_EXEC_USERSPACE(void)
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;
Expand Down Expand Up @@ -195,6 +200,19 @@ void lkdtm_ACCESS_USERSPACE(void)
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 */
Expand Down

0 comments on commit f2778b3

Please sign in to comment.