All of lore.kernel.org
 help / color / mirror / Atom feed
From: Elena Reshetova <elena.reshetova@intel.com>
To: kernel-hardening@lists.openwall.com
Cc: keescook@chromium.org, arnd@arndb.de, tglx@linutronix.de,
	mingo@redhat.com, h.peter.anvin@intel.com, peterz@infradead.org,
	will.deacon@arm.com, Elena Reshetova <elena.reshetova@intel.com>,
	Hans Liljestrand <ishkamiel@gmail.com>
Subject: [kernel-hardening] [RFC v4 PATCH 13/13] lkdtm: add tests for atomic over-/underflow
Date: Thu, 10 Nov 2016 22:24:48 +0200	[thread overview]
Message-ID: <1478809488-18303-14-git-send-email-elena.reshetova@intel.com> (raw)
In-Reply-To: <1478809488-18303-1-git-send-email-elena.reshetova@intel.com>

This adds additional tests for the remaining atomic functions.
Since the bulk of the logic is identical, the
functions are generated with macros.
Also adds debugfs entries for the manipulated atomic values,
so that the saturation values can be validated after a test is executed.

Based on work by Hans Liljestrand and AKASHI Takahiro.

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
---
 drivers/misc/lkdtm.h      |  46 +++++++++++++++--
 drivers/misc/lkdtm_bugs.c | 127 +++++++++++++++++++++++++++++++++++-----------
 drivers/misc/lkdtm_core.c |  20 +++++---
 3 files changed, 153 insertions(+), 40 deletions(-)

diff --git a/drivers/misc/lkdtm.h b/drivers/misc/lkdtm.h
index fdf954c..ce73dcc 100644
--- a/drivers/misc/lkdtm.h
+++ b/drivers/misc/lkdtm.h
@@ -4,9 +4,50 @@
 #define pr_fmt(fmt) "lkdtm: " fmt
 
 #include <linux/kernel.h>
+#include <linux/fs.h>
+
+/*
+ * Handle the various atomic function prototypes (potentially ignoring
+ * return values).
+ */
+#define ATOMIC_ARG_X(func, x)			func(x)
+#define ATOMIC_ARG_1_X(func, x)			func(1, x)
+#define ATOMIC_RET_ARG_X(func, x)		if (func(x)) ;
+#define ATOMIC_RET_ARG_1_X(func, x)		if (func(1, x)) ;
+#define ATOMIC_RET_ARG_X_1_0(func, x)		if (func(x, 1, 0)) ;
+
+/* The list of all tested atomic operations. */
+#define LKDTM_ATOMIC_OPERATIONS(macro)				\
+	macro(UNDERFLOW, dec,		ATOMIC_ARG_X)		\
+	macro(UNDERFLOW, dec_return,	ATOMIC_RET_ARG_X)	\
+	macro(UNDERFLOW, dec_and_test,	ATOMIC_RET_ARG_X)	\
+	macro(UNDERFLOW, sub,		ATOMIC_ARG_1_X)		\
+	macro(UNDERFLOW, sub_return,	ATOMIC_RET_ARG_1_X)	\
+	macro(UNDERFLOW, sub_and_test,	ATOMIC_RET_ARG_1_X)	\
+	macro(OVERFLOW,  inc,		ATOMIC_ARG_X)		\
+	macro(OVERFLOW,  inc_return,	ATOMIC_RET_ARG_X)	\
+	macro(OVERFLOW,  inc_and_test,	ATOMIC_RET_ARG_X)	\
+	macro(OVERFLOW,  add,		ATOMIC_ARG_1_X)		\
+	macro(OVERFLOW,  add_return,	ATOMIC_RET_ARG_1_X)	\
+	macro(OVERFLOW,  add_unless,	ATOMIC_RET_ARG_X_1_0)	\
+	macro(OVERFLOW,  add_negative,	ATOMIC_RET_ARG_1_X)
+
+/* The list of all tested atomic types. */
+#define LKDTM_ATOMIC_TYPES(macro, name, operation)		\
+	macro(name,	atomic,		operation)		\
+	macro(name,	atomic_long,	operation)		\
+	macro(name,	local,		operation)		\
+	macro(name,	atomic64,	operation)
+
+/* Generate function prototypes for all atomic operation/type combinations. */
+#define LKDTM_ATOMIC_FUNCDECL(name, atomic_type, operation)		\
+	void lkdtm_##name##_##atomic_type##_##operation(void);
+
+#define LKDTM_ATOMIC_FUNCDECLS(name, operation, ignored)		\
+	LKDTM_ATOMIC_TYPES(LKDTM_ATOMIC_FUNCDECL, name, operation)
 
 /* lkdtm_bugs.c */
-void __init lkdtm_bugs_init(int *recur_param);
+void __init lkdtm_bugs_init(int *recur_param, struct dentry *parent);
 void lkdtm_PANIC(void);
 void lkdtm_BUG(void);
 void lkdtm_WARNING(void);
@@ -19,8 +60,7 @@ void lkdtm_SOFTLOCKUP(void);
 void lkdtm_HARDLOCKUP(void);
 void lkdtm_SPINLOCKUP(void);
 void lkdtm_HUNG_TASK(void);
-void lkdtm_ATOMIC_UNDERFLOW(void);
-void lkdtm_ATOMIC_OVERFLOW(void);
+LKDTM_ATOMIC_OPERATIONS(LKDTM_ATOMIC_FUNCDECLS);
 
 /* lkdtm_heap.c */
 void lkdtm_OVERWRITE_ALLOCATION(void);
diff --git a/drivers/misc/lkdtm_bugs.c b/drivers/misc/lkdtm_bugs.c
index 182ae18..b1da9c5 100644
--- a/drivers/misc/lkdtm_bugs.c
+++ b/drivers/misc/lkdtm_bugs.c
@@ -5,7 +5,9 @@
  * test source files.
  */
 #include "lkdtm.h"
+#include <linux/debugfs.h>
 #include <linux/sched.h>
+#include <asm/local.h>
 
 /*
  * Make sure our attempts to over run the kernel stack doesn't trigger
@@ -35,15 +37,6 @@ static int recursive_loop(int remaining)
 		return recursive_loop(remaining - 1);
 }
 
-/* If the depth is negative, use the default, otherwise keep parameter. */
-void __init lkdtm_bugs_init(int *recur_param)
-{
-	if (*recur_param < 0)
-		*recur_param = recur_count;
-	else
-		recur_count = *recur_param;
-}
-
 void lkdtm_PANIC(void)
 {
 	panic("dumptest");
@@ -123,26 +116,100 @@ void lkdtm_HUNG_TASK(void)
 	schedule();
 }
 
-void lkdtm_ATOMIC_UNDERFLOW(void)
-{
-	atomic_t under = ATOMIC_INIT(INT_MIN);
-
-	pr_info("attempting good atomic increment\n");
-	atomic_inc(&under);
-	atomic_dec(&under);
-
-	pr_info("attempting bad atomic underflow\n");
-	atomic_dec(&under);
-}
-
-void lkdtm_ATOMIC_OVERFLOW(void)
-{
-	atomic_t over = ATOMIC_INIT(INT_MAX);
-
-	pr_info("attempting good atomic decrement\n");
-	atomic_dec(&over);
-	atomic_inc(&over);
+#define GENERATE_LKDTM_ATOMIC_TYPE(ignore1, atomic_name, ignore2)	\
+									\
+static atomic_name##_t atomic_name##_var;				\
+									\
+static int debugfs_##atomic_name##_get(void *data, u64 *val)		\
+{									\
+	*val = atomic_name##_read((atomic_name##_t *)data);		\
+	return 0;							\
+}									\
+									\
+DEFINE_DEBUGFS_ATTRIBUTE(fops_##atomic_name,				\
+			debugfs_##atomic_name##_get,			\
+			NULL, "%lld\n");				\
+									\
+static struct dentry *debugfs_create_##atomic_name(const char *item,	\
+				umode_t mode,				\
+				struct dentry *parent,			\
+				atomic_name##_t *value)			\
+{									\
+	return debugfs_create_file_unsafe(item, mode, parent, value,	\
+					&fops_##atomic_name);		\
+}
+
+LKDTM_ATOMIC_TYPES(GENERATE_LKDTM_ATOMIC_TYPE, 0, 0)
+
+#define ATOMIC_TEST(name, atomic_name, init_func, start, safe_func,	\
+					test_func_proto, testfunc)	\
+									\
+void lkdtm_##name##_##testfunc(void)					\
+{									\
+	atomic_name##_set(&atomic_name##_var, start);			\
+									\
+	pr_info("attempting good " #testfunc "\n");			\
+	safe_func(&atomic_name##_var);					\
+	test_func_proto(testfunc, &atomic_name##_var);			\
+									\
+	pr_info("attempting bad " #testfunc "\n");			\
+	test_func_proto(testfunc, &atomic_name##_var);			\
+}
+
+/* Declare underflow test functions for atomic_t and atomic_long_t types. */
+#define LKDTM_ATOMIC_UNDERFLOW(operation, test_func_proto)		\
+	ATOMIC_TEST(UNDERFLOW, atomic, ATOMIC_INIT, INT_MIN,		\
+		    atomic_inc, test_func_proto, atomic_##operation)	\
+	ATOMIC_TEST(UNDERFLOW, atomic_long, ATOMIC_LONG_INIT,		\
+		    LONG_MIN, atomic_long_inc, test_func_proto,		\
+		    atomic_long_##operation)				\
+	ATOMIC_TEST(UNDERFLOW, atomic64, ATOMIC64_INIT,			\
+		    S64_MIN, atomic64_inc, test_func_proto,		\
+		    atomic64_##operation)				\
+	ATOMIC_TEST(UNDERFLOW, local, LOCAL_INIT,			\
+		    LONG_MIN, local_inc, test_func_proto,		\
+		    local_##operation)
+
+/* Declare overflow test functions for atomic_t and atomic_long_t types. */
+#define LKDTM_ATOMIC_OVERFLOW(operation, test_func_proto)		\
+	ATOMIC_TEST(OVERFLOW, atomic, ATOMIC_INIT, INT_MAX,		\
+		    atomic_dec, test_func_proto, atomic_##operation)	\
+	ATOMIC_TEST(OVERFLOW, atomic_long, ATOMIC_LONG_INIT,		\
+		    LONG_MAX, atomic_long_dec, test_func_proto,		\
+		    atomic_long_##operation)				\
+	ATOMIC_TEST(OVERFLOW, atomic64, ATOMIC64_INIT,			\
+		    S64_MAX, atomic64_dec, test_func_proto,		\
+		    atomic64_##operation)				\
+	ATOMIC_TEST(OVERFLOW, local, LOCAL_INIT,			\
+		    LONG_MAX, local_dec, test_func_proto,		\
+		    local_##operation)
+
+#define GENERATE_LKDTM_ATOMIC_TEST(name, operation, test_func_proto)	\
+	LKDTM_ATOMIC_##name(operation, test_func_proto)
+
+LKDTM_ATOMIC_OPERATIONS(GENERATE_LKDTM_ATOMIC_TEST)
+
+#define GENERATE_LKDTM_ATOMIC_DEBUGFS(ignore1, atomic_name, ignore2)	\
+	de = debugfs_create_##atomic_name(#atomic_name, 0644,		\
+					  atomic_dir,			\
+					  &atomic_name##_var);		\
+	if (de == NULL)							\
+		pr_err("could not create " #atomic_name " in debugfs\n");
+
+void __init lkdtm_bugs_init(int *recur_param, struct dentry *parent) {
+	struct dentry *atomic_dir, *de;
+
+	/* If the depth is negative, use default, otherwise keep parameter. */
+	if (*recur_param < 0)
+		*recur_param = recur_count;
+	else
+		recur_count = *recur_param;
 
-	pr_info("attempting bad atomic overflow\n");
-	atomic_inc(&over);
+	/* Don't treat failures to create the atomic value tree as fatal. */
+	atomic_dir = debugfs_create_dir("atomic", parent);
+	if (atomic_dir) {
+		LKDTM_ATOMIC_TYPES(GENERATE_LKDTM_ATOMIC_DEBUGFS, 0, 0);
+	} else {
+		pr_err("creating atomic dir failed\n");
+ }
 }
diff --git a/drivers/misc/lkdtm_core.c b/drivers/misc/lkdtm_core.c
index f9154b8..defa1af 100644
--- a/drivers/misc/lkdtm_core.c
+++ b/drivers/misc/lkdtm_core.c
@@ -189,6 +189,13 @@ struct crashtype {
 		.func = lkdtm_ ## _name,	\
 	}
 
+/* Generate crashtype entries for all atomic operation/type combinations. */
+#define CRASHTYPE_ATOMIC(name, atomic_name, operation)		\
+	CRASHTYPE(name##_##atomic_name##_##operation),		\
+
+#define CRASHTYPE_ATOMICS(name, operation, ignored)		\
+	LKDTM_ATOMIC_TYPES(CRASHTYPE_ATOMIC, name, operation)
+
 /* Define the possible types of crashes that can be triggered. */
 struct crashtype crashtypes[] = {
 	CRASHTYPE(PANIC),
@@ -218,8 +225,6 @@ struct crashtype crashtypes[] = {
 	CRASHTYPE(WRITE_RO),
 	CRASHTYPE(WRITE_RO_AFTER_INIT),
 	CRASHTYPE(WRITE_KERN),
-	CRASHTYPE(ATOMIC_UNDERFLOW),
-	CRASHTYPE(ATOMIC_OVERFLOW),
 	CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
 	CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
 	CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
@@ -228,6 +233,7 @@ struct crashtype crashtypes[] = {
 	CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
 	CRASHTYPE(USERCOPY_STACK_BEYOND),
 	CRASHTYPE(USERCOPY_KERNEL),
+	LKDTM_ATOMIC_OPERATIONS(CRASHTYPE_ATOMICS)
 };
 
 
@@ -481,11 +487,6 @@ static int __init lkdtm_module_init(void)
 	crash_count = cpoint_count;
 #endif
 
-	/* Handle test-specific initialization. */
-	lkdtm_bugs_init(&recur_count);
-	lkdtm_perms_init();
-	lkdtm_usercopy_init();
-
 	/* Register debugfs interface */
 	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
 	if (!lkdtm_debugfs_root) {
@@ -493,6 +494,11 @@ static int __init lkdtm_module_init(void)
 		return -ENODEV;
 	}
 
+	/* Handle test-specific initialization. */
+	lkdtm_bugs_init(&recur_count, lkdtm_debugfs_root);
+	lkdtm_perms_init();
+	lkdtm_usercopy_init();
+
 	/* Install debugfs trigger files. */
 	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
 		struct crashpoint *cur = &crashpoints[i];
-- 
2.7.4

  parent reply	other threads:[~2016-11-10 20:24 UTC|newest]

Thread overview: 104+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-10 20:24 [kernel-hardening] [RFC v4 PATCH 00/13] HARDENED_ATOMIC Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 01/13] Add architecture independent hardened atomic base Elena Reshetova
2016-11-10 20:41   ` [kernel-hardening] " David Windsor
2016-11-10 21:09     ` Peter Zijlstra
2016-11-10 21:35   ` Peter Zijlstra
2016-11-11  9:06     ` Reshetova, Elena
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 02/13] percpu-refcount: leave atomic counter unprotected Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 03/13] kernel: identify wrapping atomic usage Elena Reshetova
2016-11-10 21:58   ` [kernel-hardening] " Peter Zijlstra
2016-11-11  8:49     ` [kernel-hardening] " Reshetova, Elena
2016-11-19 13:28   ` [kernel-hardening] " Paul E. McKenney
2016-11-19 21:39     ` Kees Cook
2016-11-21 20:13       ` Paul E. McKenney
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 04/13] mm: " Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 05/13] fs: " Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 06/13] net: " Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 07/13] net: atm: " Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 08/13] security: " Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 09/13] drivers: identify wrapping atomic usage (part 1/2) Elena Reshetova
2016-11-10 21:48   ` [kernel-hardening] " Will Deacon
2016-11-11  8:57     ` [kernel-hardening] " Reshetova, Elena
2016-11-11 12:35       ` Mark Rutland
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 10/13] drivers: identify wrapping atomic usage (part 2/2) Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 11/13] x86: identify wrapping atomic usage Elena Reshetova
2016-11-10 20:24 ` [kernel-hardening] [RFC v4 PATCH 12/13] x86: implementation for HARDENED_ATOMIC Elena Reshetova
2016-11-10 20:40   ` [kernel-hardening] " Peter Zijlstra
2016-11-10 21:04     ` Kees Cook
2016-11-10 21:16       ` Peter Zijlstra
2016-11-10 21:32         ` Kees Cook
2016-11-10 21:46           ` Peter Zijlstra
2016-11-10 22:50     ` Peter Zijlstra
2016-11-10 23:07       ` Kees Cook
2016-11-10 23:30         ` Peter Zijlstra
2016-11-11  9:32           ` [kernel-hardening] " Reshetova, Elena
2016-11-11 10:29             ` [kernel-hardening] " Peter Zijlstra
2016-11-11 18:00           ` Kees Cook
2016-11-11 20:19             ` Peter Zijlstra
2016-11-10 21:33   ` Peter Zijlstra
2016-11-11  9:20     ` [kernel-hardening] " Reshetova, Elena
2016-11-10 20:24 ` Elena Reshetova [this message]
2016-11-10 20:37 ` [RFC v4 PATCH 00/13] HARDENED_ATOMIC Peter Zijlstra
2016-11-10 20:37   ` [kernel-hardening] " Peter Zijlstra
2016-11-10 20:48   ` Will Deacon
2016-11-10 20:48     ` [kernel-hardening] " Will Deacon
2016-11-10 21:01     ` Kees Cook
2016-11-10 21:01       ` [kernel-hardening] " Kees Cook
2016-11-10 21:23       ` David Windsor
2016-11-10 21:27         ` Kees Cook
2016-11-10 21:27           ` Kees Cook
2016-11-10 21:39           ` David Windsor
2016-11-10 21:39             ` David Windsor
2016-11-10 21:39         ` Peter Zijlstra
2016-11-10 21:13     ` Peter Zijlstra
2016-11-10 21:13       ` [kernel-hardening] " Peter Zijlstra
2016-11-10 21:23       ` Kees Cook
2016-11-10 21:23         ` [kernel-hardening] " Kees Cook
2016-11-11  4:25         ` Rik van Riel
2016-11-10 22:27       ` Greg KH
2016-11-10 23:15         ` Kees Cook
2016-11-10 23:15           ` Kees Cook
2016-11-10 23:38           ` Greg KH
2016-11-10 23:38             ` Greg KH
2016-11-11  7:50             ` David Windsor
2016-11-11 17:43               ` Kees Cook
2016-11-11 17:46                 ` Peter Zijlstra
2016-11-11 18:04                   ` Kees Cook
2016-11-11 20:17                     ` Peter Zijlstra
2016-11-14 20:31                       ` Kees Cook
2016-11-15  8:01                         ` Peter Zijlstra
2016-11-15 16:50                         ` Rik van Riel
2016-11-15 17:23                           ` Kees Cook
2016-11-16 17:09                             ` Rik van Riel
2016-11-16 17:32                               ` Peter Zijlstra
2016-11-16 17:41                                 ` Rik van Riel
2016-11-16 17:34                               ` Reshetova, Elena
2016-11-17  8:37                                 ` Peter Zijlstra
2016-11-17  9:04                                   ` Reshetova, Elena
2016-11-17  9:36                                     ` Peter Zijlstra
2016-11-17  9:36                                   ` Julia Lawall
2016-11-17 10:16                                     ` Peter Zijlstra
2016-11-17 11:19                                       ` Mark Rutland
2016-11-17 11:32                                         ` Julia Lawall
2016-11-17 12:59                                       ` Julia Lawall
2016-11-11 18:47                   ` Mark Rutland
2016-11-11 19:39                     ` Will Deacon
2016-11-11 18:31                 ` Mark Rutland
2016-11-11 20:05                   ` Peter Zijlstra
2016-11-15 10:36                     ` Mark Rutland
2016-11-15 11:21                       ` Peter Zijlstra
2016-11-15 18:02                         ` Mark Rutland
2016-11-10 23:57           ` Peter Zijlstra
2016-11-10 23:57             ` Peter Zijlstra
2016-11-11  0:29             ` Colin Vidal
2016-11-11 12:41               ` Mark Rutland
2016-11-11 12:47                 ` Peter Zijlstra
2016-11-11 13:00                   ` Peter Zijlstra
2016-11-11 14:39                     ` Thomas Gleixner
2016-11-11 14:48                       ` Peter Zijlstra
2016-11-11 23:07                     ` Peter Zijlstra
2016-11-13 11:03             ` Greg KH
2016-11-13 11:03               ` Greg KH
2016-11-10 20:56   ` Kees Cook
2016-11-10 20:56     ` [kernel-hardening] " Kees Cook
2016-11-11  3:20     ` David Windsor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1478809488-18303-14-git-send-email-elena.reshetova@intel.com \
    --to=elena.reshetova@intel.com \
    --cc=arnd@arndb.de \
    --cc=h.peter.anvin@intel.com \
    --cc=ishkamiel@gmail.com \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.