Skip to content

Commit b0eb93c

Browse files
committed
lkdtm: Add Control Flow Integrity test
This adds a simple test for forward CFI (indirect function calls) with function prototype granularity (as implemented by Clang's CFI). Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 609488b commit b0eb93c

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎drivers/misc/lkdtm/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ lkdtm-$(CONFIG_LKDTM) += refcount.o
99
lkdtm-$(CONFIG_LKDTM) += rodata_objcopy.o
1010
lkdtm-$(CONFIG_LKDTM) += usercopy.o
1111
lkdtm-$(CONFIG_LKDTM) += stackleak.o
12+
lkdtm-$(CONFIG_LKDTM) += cfi.o
1213

1314
KASAN_SANITIZE_stackleak.o := n
1415
KCOV_INSTRUMENT_rodata.o := n

‎drivers/misc/lkdtm/cfi.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* This is for all the tests relating directly to Control Flow Integrity.
4+
*/
5+
#include "lkdtm.h"
6+
7+
static int called_count;
8+
9+
/* Function taking one argument, without a return value. */
10+
static noinline void lkdtm_increment_void(int *counter)
11+
{
12+
(*counter)++;
13+
}
14+
15+
/* Function taking one argument, returning int. */
16+
static noinline int lkdtm_increment_int(int *counter)
17+
{
18+
(*counter)++;
19+
20+
return *counter;
21+
}
22+
/*
23+
* This tries to call an indirect function with a mismatched prototype.
24+
*/
25+
void lkdtm_CFI_FORWARD_PROTO(void)
26+
{
27+
/*
28+
* Matches lkdtm_increment_void()'s prototype, but not
29+
* lkdtm_increment_int()'s prototype.
30+
*/
31+
void (*func)(int *);
32+
33+
pr_info("Calling matched prototype ...\n");
34+
func = lkdtm_increment_void;
35+
func(&called_count);
36+
37+
pr_info("Calling mismatched prototype ...\n");
38+
func = (void *)lkdtm_increment_int;
39+
func(&called_count);
40+
41+
pr_info("Fail: survived mismatched prototype function call!\n");
42+
}

‎drivers/misc/lkdtm/core.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static const struct crashtype crashtypes[] = {
169169
CRASHTYPE(USERCOPY_KERNEL),
170170
CRASHTYPE(USERCOPY_KERNEL_DS),
171171
CRASHTYPE(STACKLEAK_ERASING),
172+
CRASHTYPE(CFI_FORWARD_PROTO),
172173
};
173174

174175

‎drivers/misc/lkdtm/lkdtm.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,7 @@ void lkdtm_USERCOPY_KERNEL_DS(void);
9595
/* lkdtm_stackleak.c */
9696
void lkdtm_STACKLEAK_ERASING(void);
9797

98+
/* cfi.c */
99+
void lkdtm_CFI_FORWARD_PROTO(void);
100+
98101
#endif

0 commit comments

Comments
 (0)