summaryrefslogtreecommitdiff
path: root/drivers/misc/lkdtm/cfi.c
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2019-08-08 11:37:45 -0700
committerKees Cook <keescook@chromium.org>2019-08-12 15:28:51 -0700
commitb0eb93cfd516201ccf0e4d36e226cfe1b16cc1fe (patch)
treef560f6bb85c3dd5d7e846012c4e03769a7cfa725 /drivers/misc/lkdtm/cfi.c
parent609488bc979f99f805f34e9a32c1e3b71179d10b (diff)
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>
Diffstat (limited to 'drivers/misc/lkdtm/cfi.c')
-rw-r--r--drivers/misc/lkdtm/cfi.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/drivers/misc/lkdtm/cfi.c b/drivers/misc/lkdtm/cfi.c
new file mode 100644
index 000000000000..e73ebdbfa806
--- /dev/null
+++ b/drivers/misc/lkdtm/cfi.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This is for all the tests relating directly to Control Flow Integrity.
+ */
+#include "lkdtm.h"
+
+static int called_count;
+
+/* Function taking one argument, without a return value. */
+static noinline void lkdtm_increment_void(int *counter)
+{
+ (*counter)++;
+}
+
+/* Function taking one argument, returning int. */
+static noinline int lkdtm_increment_int(int *counter)
+{
+ (*counter)++;
+
+ return *counter;
+}
+/*
+ * This tries to call an indirect function with a mismatched prototype.
+ */
+void lkdtm_CFI_FORWARD_PROTO(void)
+{
+ /*
+ * Matches lkdtm_increment_void()'s prototype, but not
+ * lkdtm_increment_int()'s prototype.
+ */
+ void (*func)(int *);
+
+ pr_info("Calling matched prototype ...\n");
+ func = lkdtm_increment_void;
+ func(&called_count);
+
+ pr_info("Calling mismatched prototype ...\n");
+ func = (void *)lkdtm_increment_int;
+ func(&called_count);
+
+ pr_info("Fail: survived mismatched prototype function call!\n");
+}