All of lore.kernel.org
 help / color / mirror / Atom feed
From: Aleksandr Nogikh <a.nogikh@gmail.com>
To: jmorris@namei.org, serge@hallyn.com, akinobu.mita@gmail.com
Cc: andreyknvl@google.com, dvyukov@google.com, elver@google.com,
	glider@google.com, keescook@google.com, casey@schaufler-ca.com,
	penguin-kernel@i-love.sakura.ne.jp, linux-kernel@vger.kernel.org,
	linux-security-module@vger.kernel.org, mortonm@chromium.org,
	Aleksandr Nogikh <nogikh@google.com>,
	Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Subject: [PATCH v4 1/2] security: add fault injection capability
Date: Wed, 11 Nov 2020 10:44:08 +0000	[thread overview]
Message-ID: <20201111104409.1530957-2-a.nogikh@gmail.com> (raw)
In-Reply-To: <20201111104409.1530957-1-a.nogikh@gmail.com>

From: Aleksandr Nogikh <nogikh@google.com>

Add a fault injection capability to call_int_hook macro. This will
facilitate testing of fault tolerance of the code that invokes
security hooks as well as the fault tolerance of the LSM
implementations themselves.

Add a KConfig option (CONFIG_FAIL_LSM_HOOKS) that controls whether the
capability is enabled. In order to enable configuration from the user
space, add the standard debugfs entries for fault injection (if
CONFIG_FAULT_INJECTION_DEBUG_FS is enabled).

Signed-off-by: Aleksandr Nogikh <nogikh@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Reviewed-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reviewed-by: Andrey Konovalov <andreyknvl@google.com>
---
v4:
- Changed retval debugfs file type - now it keeps a signed integer.
- Made CONFIG_FAIL_LSM_HOOKS depend on CONFIG_SECURITY.
v2:
- Renamed should_fail_lsm_hook() to lsm_hooks_inject_fail().
---
 lib/Kconfig.debug   |  6 ++++
 security/security.c | 69 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 6140413174be..5f4399816019 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1813,6 +1813,12 @@ config FAIL_MAKE_REQUEST
 	help
 	  Provide fault-injection capability for disk IO.
 
+config FAIL_LSM_HOOKS
+	bool "Fault-injection capability for LSM hooks"
+	depends on FAULT_INJECTION && SECURITY
+	help
+	  Provide fault-injection capability for LSM hooks.
+
 config FAIL_IO_TIMEOUT
 	bool "Fault-injection capability for faking disk interrupts"
 	depends on FAULT_INJECTION && BLOCK
diff --git a/security/security.c b/security/security.c
index 69ff6e2e2cd4..be3a3c7c6d6a 100644
--- a/security/security.c
+++ b/security/security.c
@@ -28,6 +28,7 @@
 #include <linux/backing-dev.h>
 #include <linux/string.h>
 #include <linux/msg.h>
+#include <linux/fault-inject.h>
 #include <net/flow.h>
 
 #define MAX_LSM_EVM_XATTR	2
@@ -669,6 +670,67 @@ static void __init lsm_early_task(struct task_struct *task)
 		panic("%s: Early task alloc failed.\n", __func__);
 }
 
+
+#ifdef CONFIG_FAIL_LSM_HOOKS
+
+static struct {
+	struct fault_attr attr;
+	int retval;
+} fail_lsm_hooks = {
+	.attr = FAULT_ATTR_INITIALIZER,
+	.retval = -EACCES
+};
+
+static int __init setup_fail_lsm_hooks(char *str)
+{
+	return setup_fault_attr(&fail_lsm_hooks.attr, str);
+}
+__setup("fail_lsm_hooks=", setup_fail_lsm_hooks);
+
+static int lsm_hooks_inject_fail(void)
+{
+	return should_fail(&fail_lsm_hooks.attr, 1) ? fail_lsm_hooks.retval : 0;
+}
+
+#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
+
+static int fail_lsm_retval_set(void *data, u64 val)
+{
+	fail_lsm_hooks.retval = (int)val;
+	return 0;
+}
+
+static int fail_lsm_retval_get(void *data, u64 *val)
+{
+	*val = (u64)fail_lsm_hooks.retval;
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fail_lsm_retval_ops, fail_lsm_retval_get,
+						 fail_lsm_retval_set, "%lld\n");
+
+static int __init fail_lsm_hooks_debugfs(void)
+{
+	umode_t mode = S_IFREG | 0600;
+	struct dentry *dir;
+
+	dir = fault_create_debugfs_attr("fail_lsm_hooks", NULL,
+					&fail_lsm_hooks.attr);
+	debugfs_create_file("retval", mode, dir, NULL,
+						&fail_lsm_retval_ops);
+	return 0;
+}
+
+late_initcall(fail_lsm_hooks_debugfs);
+
+#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
+
+#else
+
+static inline int lsm_hooks_inject_fail(void) { return 0; }
+
+#endif /* CONFIG_FAIL_LSM_HOOKS */
+
 /*
  * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
  * can be accessed with:
@@ -707,16 +769,17 @@ static void __init lsm_early_task(struct task_struct *task)
 	} while (0)
 
 #define call_int_hook(FUNC, IRC, ...) ({			\
-	int RC = IRC;						\
-	do {							\
+	int RC = lsm_hooks_inject_fail();			\
+	if (RC == 0) {								\
 		struct security_hook_list *P;			\
+		RC = IRC;								\
 								\
 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
 			RC = P->hook.FUNC(__VA_ARGS__);		\
 			if (RC != 0)				\
 				break;				\
 		}						\
-	} while (0);						\
+	}							\
 	RC;							\
 })
 
-- 
2.29.2.222.g5d2a92d10f8-goog


  reply	other threads:[~2020-11-11 10:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 10:44 [PATCH v4 0/2] security: add fault injection to LSM hooks Aleksandr Nogikh
2020-11-11 10:44 ` Aleksandr Nogikh [this message]
2020-11-11 11:48   ` [PATCH v4 1/2] security: add fault injection capability Alexander Potapenko
2020-11-11 10:44 ` [PATCH v4 2/2] docs: add fail_lsm_hooks info to fault-injection.rst Aleksandr Nogikh
2020-11-18 21:20 ` [PATCH v4 0/2] security: add fault injection to LSM hooks Aleksandr Nogikh

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=20201111104409.1530957-2-a.nogikh@gmail.com \
    --to=a.nogikh@gmail.com \
    --cc=akinobu.mita@gmail.com \
    --cc=andreyknvl@google.com \
    --cc=casey@schaufler-ca.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=jmorris@namei.org \
    --cc=keescook@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mortonm@chromium.org \
    --cc=nogikh@google.com \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=serge@hallyn.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.