Skip to contentSkip to conversation

LSM: Multiple concurrent LSMs

PATCH Superseded Casey Schaufler 1 participant 1 message Nov 20, 2012

Subsystems (6)

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Casey Schaufler

author maintainer
#0 lore raw

PATCH v8 2/2 LSM: Multiple concurrent LSMs

Subject: [PATCH v8 2/2] LSM: Multiple concurrent LSMs

Hopefully this version will get past mail list filters.

The LSM composer seems past "can it work" and is on to the "does it work well" phase. I have not tried to reintroduce LSMs as loadable modules, in spite of the vigor with which it has been requested. I see that as work for another day, and a seperate battle to fight. The current implementation of reset_security_ops() is not up to the rigors required of a real load/unload mechanism.

Version 8 of the patch addresses:

1. An error in the indexing in out-of-memory
error handling in non-macroed alloc hooks.
2. Broken up for size. Not bisectable.

Version 7 of the patch addresses:

1. security/capability.c has been removed and all the special case code for when there is no LSM hook has been moved to the function in security/security.c. There is no longer a capability_ops vector.
2. Tetsuo Handa suggested that putting list headers into the security_operations structure rather than allocating the list elements on the fly would reduce the amount of out-of-memory error code required. It also makes a lot of the list initialization and management easier.
3. Macros are back in security_hook functions by reviewer request.
4. The component of the memory leak introduced by allocated list entries when calling reset_security_ops() is no longer there. The Portion created by LSM code remains.

Version 6 of the patch addresses:

1. The array based hook calling loops have been replaced by hook lists. The blobs remain array based.
2. Hooks are inserted into the lists based on the order specified in the security= boot parameter. If you really want AppArmor called before Yama you can do that. If there is no security= parameter it goes back to first come, first called.
3. prctl is a special case that assumes a single provider for any given option.
4. The security_hook funtions have been un-macroed. This should make dealing with special cases easier at the cost of code bulk.
5. Hooks from the capability vector are called directly rather than getting put on lists. This makes the security_reset_ops process rational. It also makes it easy to change the cap_hook to always getting called.

Version 5 of the patch addresses:

1. Tetsuo Handa pointed out that handling of failures alloc hooks was still not correct in v4. The code now only calls the free hook for LSMs that have had their alloc hook successfully called. The alloc hooks have been de-macro-ized, too.
2. Removed the Yama special case. It is no longer necessary.

Version 4 of the patch addresses:

1. Removed the conditional CONFIG_SECURITY_COMPOSER. This removes the option for LSMs to opt out of the multiple concurrent LSM mechanism. It also prevents breaking the change into meaningful subsets.
2. Pulled the trivial hooks out of the capability LSM as the security_hook calls make calling them unnecessary.
3. Removed register_security as it has nothing to do.
4. Changed the way security_hook calls the LSM specific hooks so that the capability hook is only called if no other LSM has supplied a hook, rather than looking to see if there is a capability hook.
5. Removed security_fixup_ops. The capability LSM only contains "real" hooks. Removed security_fixdown_ops as well, for the same reason.
6. Simplified security_reset_ops and made it reasonably safe.

Version 3 of the patchset addresses:

1. Improvements to allocations in lsm_read for the securityfs lsm interface.
2. A repair to the ordering of an NULL check in security_module_enable.
3. Sharing of the inode_getsecurity, inode_setsecurity and inode_listsecurity hooks, even thougth there is no current contention for them.

Version 2 of the patchset addresses:

1. The lsm_set functions did not handle error cases. The on demand allocation has been replaced with a more robust scheme within the security_alloc hooks. This requires more change in security/security.c but has the advantage of being more likely to be correct.
2. reset_security_ops() didn't work, causing a panic on invocation. That's been fixed. The change is somewhat invasive relative to the functionality.
3. Add registration time detection of LSMs that are going to use hooks that can't (currently) be shared.

Provide a mechanism for using multiple LSMs on the same running kernel. This mechanism is not backward compatible. All LSMs must conform to it.

As David Howells suggested some time back, making Smack and SELinux available at that same time has proven quite a challenge. That work has been deferred and that particular configuration disallowed.

The Smack LSM behavior has been tested. AppArmor, TOMOYO, Yama and SELinux have been shown to boot, but have not been functionally tested beyond the lack of obvious error messages and complaints from kernel debugging facilities. The kernels have been tested with Ubuntu 12.04 and Fedora 17.

---

 include/linux/lsm.h                 |  194 ++++
 include/linux/security.h            |  216 +++-
 security/Kconfig                    |   75 +-
 security/Makefile                   |    3 +-
 security/apparmor/context.c         |   10 +-
 security/apparmor/domain.c          |    8 +-
 security/apparmor/include/context.h |   13 +-
 security/apparmor/lsm.c             |   45 +-
 security/capability.c               | 1075 -------------------
 security/inode.c                    |   46 +-
 security/selinux/hooks.c            |  355 ++++---
 security/selinux/include/objsec.h   |    2 +
 security/selinux/include/xfrm.h     |    2 +-
 security/selinux/netlabel.c         |   13 +-
 security/selinux/selinuxfs.c        |    6 +-
 security/selinux/xfrm.c             |    9 +-
 security/smack/Kconfig              |    1 -
 security/smack/smack.h              |   14 +-
 security/smack/smack_access.c       |    2 +-
 security/smack/smack_lsm.c          |  319 +++---
 security/smack/smackfs.c            |   92 +-
 security/tomoyo/common.h            |    6 +-
 security/tomoyo/domain.c            |    2 +-
 security/tomoyo/securityfs_if.c     |    9 +-
 security/tomoyo/tomoyo.c            |   41 +-
 security/yama/Kconfig               |    7 -
 security/yama/yama_lsm.c            |    9 -
 28 files changed, 2698 insertions(+), 1830 deletions(-)
include/linux/lsm.h new file +194 -0
new file mode 100644
index 0000000..06f53ff
@@ -0,0 +1,194 @@
+/*
+ *
+ * Copyright (C) 2012 Casey Schaufler <[email protected]>
+ * Copyright (C) 2012 Intel Corporation
+ *
+ *	This program is free software; you can redistribute it and/or modify
+ *	it under the terms of the GNU General Public License as published by
+ *	the Free Software Foundation, version 2.
+ *
+ * Author:
+ *	Casey Schaufler <[email protected]>
+ *
+ */
+#ifndef _LINUX_LSM_H
+#define _LINUX_LSM_H
+
+#include <linux/cred.h>
+#include <linux/fs.h>
+#include <linux/msg.h>
+#include <linux/key.h>
+#include <net/sock.h>
+
+/*
+ * Maximum number of LSMs that can be used at a time.
+ */
+#define COMPOSER_MAX		CONFIG_SECURITY_COMPOSER_MAX
+#define COMPOSER_NAMES_MAX	((SECURITY_NAME_MAX + 1) * COMPOSER_MAX)
+
+#include <linux/security.h>
+
+/*
+ * Just a set of slots for each LSM to keep its blob in.
+ */
+struct lsm_blob {
+	int	lsm_setcount;			/* Number of blobs set */
+	void	*lsm_blobs[COMPOSER_MAX];	/* LSM specific blobs */
+};
+
+static inline struct lsm_blob *lsm_alloc_blob(gfp_t gfp)
+{
+	return kzalloc(sizeof(struct lsm_blob), gfp);
+}
+
+static inline void *lsm_get_blob(const struct lsm_blob *bp, const int lsm)
+{
+	if (bp == NULL)
+		return NULL;
+	return bp->lsm_blobs[lsm];
+}
+
+static inline void lsm_set_blob(void **vpp, void *value, const int lsm)
+{
+	struct lsm_blob *bp = *vpp;
+
+	if (value == NULL && bp->lsm_blobs[lsm] != NULL)
+		bp->lsm_setcount--;
+	if (value != NULL && bp->lsm_blobs[lsm] == NULL)
+		bp->lsm_setcount++;
+
+	bp->lsm_blobs[lsm] = value;
+}
+
+static inline void *lsm_get_cred(const struct cred *cred,
+					const struct security_operations *sop)
+{
+#ifdef CONFIG_SECURITY_COMPOSER_DEBUG
+	if (cred == NULL) {
+		pr_warn("%s: cred is NULL, LSM is %s.\n", __func__, sop->name);
+		return NULL;
+	}
+	if (cred->security == NULL) {
+		pr_warn("%s: cred blob NULL, LSM is %s.\n",
+			__func__, sop->name);
+		return NULL;
+	}
+#endif /* CONFIG_SECURITY_COMPOSER_DEBUG */
+	return lsm_get_blob(cred->security, sop->order);
+}
+
+static inline void lsm_set_cred(struct cred *cred, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&cred->security, value, sop->order);
+}
+
+static inline int lsm_set_init_cred(struct cred *cred, void *value,
+					const struct security_operations *sop)
+{
+	if (cred->security == NULL) {
+		cred->security = lsm_alloc_blob(GFP_KERNEL);
+		if (cred->security == NULL)
+			return -ENOMEM;
+	}
+
+	lsm_set_blob(&cred->security, value, sop->order);
+	return 0;
+}
+
+static inline void *lsm_get_file(const struct file *file,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(file->f_security, sop->order);
+}
+
+static inline void lsm_set_file(struct file *file, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&file->f_security, value, sop->order);
+}
+
+static inline void *lsm_get_inode(const struct inode *inode,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(inode->i_security, sop->order);
+}
+
+static inline void lsm_set_inode(struct inode *inode, void *value,
+					const struct security_operations *sop)
+{
+#ifdef CONFIG_SECURITY_COMPOSER_DEBUG
+	if (inode == NULL) {
+		pr_warn("%s: inode is NULL, LSM is %s.\n", __func__, sop->name);
+		return;
+	}
+	if (inode->i_security == NULL) {
+		pr_warn("%s: inode blob is NULL, LSM is %s.\n",
+			__func__, sop->name);
+		inode->i_security = lsm_alloc_blob(GFP_KERNEL);
+	}
+#endif /* CONFIG_SECURITY_COMPOSER_DEBUG */
+	lsm_set_blob(&inode->i_security, value, sop->order);
+}
+
+static inline void *lsm_get_super(const struct super_block *super,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(super->s_security, sop->order);
+}
+
+static inline void lsm_set_super(struct super_block *super, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&super->s_security, value, sop->order);
+}
+
+static inline void *lsm_get_ipc(const struct kern_ipc_perm *ipc,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(ipc->security, sop->order);
+}
+
+static inline void lsm_set_ipc(struct kern_ipc_perm *ipc, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&ipc->security, value, sop->order);
+}
+
+static inline void *lsm_get_msg(const struct msg_msg *msg,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(msg->security, sop->order);
+}
+
+static inline void lsm_set_msg(struct msg_msg *msg, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&msg->security, value, sop->order);
+}
+
+static inline void *lsm_get_key(const struct key *key,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(key->security, sop->order);
+}
+
+static inline void lsm_set_key(struct key *key, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&key->security, value, sop->order);
+}
+
+static inline void *lsm_get_sock(const struct sock *sock,
+					const struct security_operations *sop)
+{
+	return lsm_get_blob(sock->sk_security, sop->order);
+}
+
+static inline void lsm_set_sock(struct sock *sock, void *value,
+					const struct security_operations *sop)
+{
+	lsm_set_blob(&sock->sk_security, value, sop->order);
+}
+
+#endif /* ! _LINUX_LSM_H */
include/linux/security.h +212 -4

251 lines not on this page · raw message

security/Kconfig +46 -29

95 lines not on this page · raw message

security/Makefile +1 -2
index c26c81e..b1875b1 100644
@@ -14,9 +14,8 @@ obj-y					+= commoncap.o
 obj-$(CONFIG_MMU)			+= min_addr.o
 
 # Object file lists
-obj-$(CONFIG_SECURITY)			+= security.o capability.o
+obj-$(CONFIG_SECURITY)			+= security.o
 obj-$(CONFIG_SECURITYFS)		+= inode.o
-# Must precede capability.o in order to stack properly.
 obj-$(CONFIG_SECURITY_SELINUX)		+= selinux/built-in.o
 obj-$(CONFIG_SECURITY_SMACK)		+= smack/built-in.o
 obj-$(CONFIG_AUDIT)			+= lsm_audit.o
security/apparmor/context.c +5 -5
index 8a9b502..3d9e460 100644
@@ -76,7 +76,7 @@ void aa_dup_task_context(struct aa_task_cxt *new, const struct aa_task_cxt *old)
  */
 int aa_replace_current_profile(struct aa_profile *profile)
 {
-	struct aa_task_cxt *cxt = current_cred()->security;
+	struct aa_task_cxt *cxt = lsm_get_cred(current_cred(), &apparmor_ops);
 	struct cred *new;
 	BUG_ON(!profile);
 
@@ -87,7 +87,7 @@ int aa_replace_current_profile(struct aa_profile *profile)
 	if (!new)
 		return -ENOMEM;
 
-	cxt = new->security;
+	cxt = lsm_get_cred(new, &apparmor_ops);
 	if (unconfined(profile) || (cxt->profile->ns != profile->ns)) {
 		/* if switching to unconfined or a different profile namespace
 		 * clear out context state
@@ -123,7 +123,7 @@ int aa_set_current_onexec(struct aa_profile *profile)
 	if (!new)
 		return -ENOMEM;
 
-	cxt = new->security;
+	cxt = lsm_get_cred(new, &apparmor_ops);
 	aa_get_profile(profile);
 	aa_put_profile(cxt->onexec);
 	cxt->onexec = profile;
@@ -150,7 +150,7 @@ int aa_set_current_hat(struct aa_profile *profile, u64 token)
 		return -ENOMEM;
 	BUG_ON(!profile);
 
-	cxt = new->security;
+	cxt = lsm_get_cred(new, &apparmor_ops);
 	if (!cxt->previous) {
 		/* transfer refcount */
 		cxt->previous = cxt->profile;
@@ -187,7 +187,7 @@ int aa_restore_previous_profile(u64 token)
 	if (!new)
 		return -ENOMEM;
 
-	cxt = new->security;
+	cxt = lsm_get_cred(new, &apparmor_ops);
 	if (cxt->token != token) {
 		abort_creds(new);
 		return -EACCES;
security/apparmor/domain.c +4 -4
index 60f0c76..bf9d1c1 100644
@@ -360,7 +360,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm)
 	if (bprm->cred_prepared)
 		return 0;
 
-	cxt = bprm->cred->security;
+	cxt = lsm_get_cred(bprm->cred, &apparmor_ops);
 	BUG_ON(!cxt);
 
 	profile = aa_get_profile(aa_newest_version(cxt->profile));
@@ -557,7 +557,7 @@ int apparmor_bprm_secureexec(struct linux_binprm *bprm)
 void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
 {
 	struct aa_profile *profile = __aa_current_profile();
-	struct aa_task_cxt *new_cxt = bprm->cred->security;
+	struct aa_task_cxt *new_cxt = lsm_get_cred(bprm->cred, &apparmor_ops);
 
 	/* bail out if unconfined or not changing profile */
 	if ((new_cxt->profile == profile) ||
@@ -634,7 +634,7 @@ int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
 
 	/* released below */
 	cred = get_current_cred();
-	cxt = cred->security;
+	cxt = lsm_get_cred(cred, &apparmor_ops);
 	profile = aa_cred_profile(cred);
 	previous_profile = cxt->previous;
 
@@ -770,7 +770,7 @@ int aa_change_profile(const char *ns_name, const char *hname, bool onexec,
 	}
 
 	cred = get_current_cred();
-	cxt = cred->security;
+	cxt = lsm_get_cred(cred, &apparmor_ops);
 	profile = aa_cred_profile(cred);
 
 	/*
security/apparmor/include/context.h +10 -3
index a9cbee4..8484e55 100644
@@ -18,6 +18,7 @@
 #include <linux/cred.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
+#include <linux/lsm.h>
 
 #include "policy.h"
 
@@ -81,6 +82,8 @@ int aa_set_current_onexec(struct aa_profile *profile);
 int aa_set_current_hat(struct aa_profile *profile, u64 token);
 int aa_restore_previous_profile(u64 cookie);
 
+extern struct security_operations apparmor_ops;
+
 /**
  * __aa_task_is_confined - determine if @task has any confinement
  * @task: task to check confinement of  (NOT NULL)
@@ -89,7 +92,9 @@ int aa_restore_previous_profile(u64 cookie);
  */
 static inline bool __aa_task_is_confined(struct task_struct *task)
 {
-	struct aa_task_cxt *cxt = __task_cred(task)->security;
+	struct aa_task_cxt *cxt;
+
+	cxt = lsm_get_cred(__task_cred(task), &apparmor_ops);
 
 	BUG_ON(!cxt || !cxt->profile);
 	if (unconfined(aa_newest_version(cxt->profile)))
@@ -108,7 +113,7 @@ static inline bool __aa_task_is_confined(struct task_struct *task)
  */
 static inline struct aa_profile *aa_cred_profile(const struct cred *cred)
 {
-	struct aa_task_cxt *cxt = cred->security;
+	struct aa_task_cxt *cxt = lsm_get_cred(cred, &apparmor_ops);
 	BUG_ON(!cxt || !cxt->profile);
 	return aa_newest_version(cxt->profile);
 }
@@ -136,8 +141,10 @@ static inline struct aa_profile *__aa_current_profile(void)
  */
 static inline struct aa_profile *aa_current_profile(void)
 {
-	const struct aa_task_cxt *cxt = current_cred()->security;
+	const struct aa_task_cxt *cxt;
 	struct aa_profile *profile;
+
+	cxt = lsm_get_cred(current_cred(), &apparmor_ops);
 	BUG_ON(!cxt || !cxt->profile);
 
 	profile = aa_newest_version(cxt->profile);
security/apparmor/lsm.c +19 -26

136 lines not on this page · raw message

security/capability.c deleted +0 -1075

1,075 lines not on this page · raw message

security/inode.c +44 -2

65 lines not on this page · raw message

security/selinux/hooks.c +202 -153

1,058 lines not on this page · raw message

security/selinux/include/objsec.h +2 -0
index 26c7eee..1d1dd10 100644

Lines of this file are not shown. View the raw message.

security/selinux/include/xfrm.h +1 -1
index 65f67cb..1219221 100644

Lines of this file are not shown. View the raw message.

security/selinux/netlabel.c +7 -6
index da4b8b2..7a9cbd0 100644

Lines of this file are not shown. View the raw message.

security/selinux/selinuxfs.c +3 -3
index 3a6e873..415c6b7 100644

Lines of this file are not shown. View the raw message.

security/selinux/xfrm.c +6 -3
index 48665ec..02979a3 100644

Lines of this file are not shown. View the raw message.

security/smack/Kconfig +0 -1
index 603b087..2c018a2 100644

Lines of this file are not shown. View the raw message.

security/smack/smack.h +9 -5
index 99b3612..c012d94 100644

Lines of this file are not shown. View the raw message.

security/smack/smack_access.c +1 -1
index db14689..b4b4044 100644

Lines of this file are not shown. View the raw message.

security/smack/smack_lsm.c +159 -160

319 lines not on this page · raw message

security/smack/smackfs.c +85 -7

92 lines not on this page · raw message

security/tomoyo/common.h +4 -2
index d4f166b..ef0cdcc 100644

Lines of this file are not shown. View the raw message.

security/tomoyo/domain.c +1 -1
index 3865145..15042e7 100644

Lines of this file are not shown. View the raw message.

security/tomoyo/securityfs_if.c +6 -3
index 8592f2f..37feaf5 100644

Lines of this file are not shown. View the raw message.

security/tomoyo/tomoyo.c +27 -14

41 lines not on this page · raw message

security/yama/Kconfig +0 -7
index 20ef514..a99aa1d 100644

Lines of this file are not shown. View the raw message.

security/yama/yama_lsm.c +0 -9
index b4c2984..a68da89 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Part of the diff is not shown here. View the raw message.