Skip to contentSkip to conversation

LSM: Multiple concurrent LSMs

PATCH Superseded Casey Schaufler 2 participants 5 messages Nov 30, 2012 – Dec 10, 2012 14 messages not in the archive

Subsystems (3)

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/2 LSM: Multiple concurrent LSMs

Subject: [PATCH v9 1/2] LSM: Multiple concurrent LSMs

Infrastructure changes in support of multiple concurrent Linux Security Modules.

Replaces the single vector of security operations with a vector of security operation lists. Moves the default capability operations into the basic hook handling. Pulls commoncap function use out of the LSMs. Manages sets of security blobs.

---

 include/linux/lsm.h      |  172 ++++
 include/linux/security.h |  218 +++++-
 security/Kconfig         |   64 +-
 security/Makefile        |    3 +-
 security/capability.c    | 1075 -------------------------
 security/commoncap.c     |    6 -
 security/inode.c         |   46 +-
 security/security.c      | 1944 +++++++++++++++++++++++++++++++++++++++-------
 8 files changed, 2106 insertions(+), 1422 deletions(-)
include/linux/lsm.h new file +172 -0
new file mode 100644
index 0000000..4177d59
@@ -0,0 +1,172 @@
+/*
+ *
+ * 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)
+{
+	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)
+{
+	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 -6

259 lines not on this page · raw message

security/Kconfig +35 -29

84 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/capability.c deleted +0 -1075

1,075 lines not on this page · raw message

security/commoncap.c +0 -6
index 6dbae46..813b35d 100644
@@ -979,9 +979,3 @@ int cap_mmap_addr(unsigned long addr)
 	}
 	return ret;
 }
-
-int cap_mmap_file(struct file *file, unsigned long reqprot,
-		  unsigned long prot, unsigned long flags)
-{
-	return 0;
-}
security/inode.c +44 -2

65 lines not on this page · raw message

security/security.c +1642 -302

1,326 lines not on this page · 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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 4 lines · Tetsuo Handa

Below is an approach for allowing the callers to determine how many of /name=value/ pairs passed to setprocattr() have succeeded. Returns -ve if none processed and returns offset if at least one processed. (Well, should we use '\0' as separater rather than '/' ?)

I don't want to use '\0' because then you can't use:

echo "/smack=foo/apparmor=/usr/bin/cupsd (enforce)/" > /proc/self/attr/current

I have however identified where my scheme is flawed. If you did the above when AppArmor is not configured you get a smack label of "foo/apparmor=/usr/bin/cupsd". So it has to change. What do y'all think of:

<smack>foo</smack><apparmor>/usr/bin/cupsd (enforce)</apparmor>

Yeah, it's verbose but it wouldn't be any worse to parse than what I have now and it addresses the problem.

Or, offer a solution you like better, but it needs to be plain text, no embedded null characters.

Quoted 98 lines
security/security.c +35 -48
@@ -2061,66 +2061,51 @@ int security_setprocattr(struct task_struct *p, char *name, void *value,
 				size_t size)
 {
 	struct security_operations *sop;
-	char searches[COMPOSER_MAX][SECURITY_NAME_MAX + 4];
 	char *data = value;
-	char *values[COMPOSER_MAX];
-	int eos = 0;
-	int formatted = 0;
-	int only = 0;
-	int thisrc;
-	int rc = size;
-
-	if (lsm_present)
-		return present_setprocattr(p, name, value, size);
+	char * const start = data;
+	int processed = 0;
+	int rc = -EINVAL;
 
-	if (size > PAGE_SIZE - 1)
+	if (size && data[0] != '/') {
+		if (lsm_present)
+			return present_setprocattr(p, name, value, size);
 		return -EINVAL;
-	if (size > 0)
-		eos = size - 1;
-	while (eos > 0 && (data[eos] == '\0' || data[eos] == '\n'))
-		eos--;
-
-	if (data[0] == '/' && data[eos] == '/') {
-		/*
-		 * "/smack=Howdy/apparmor=confined/bandl=12/1,2,6/"
-		 */
-		formatted = 1;
-		data[eos] = '\0';
-		for_each_hook(sop, setprocattr) {
-			sprintf(searches[sop->order], "/%s=", sop->name);
-			values[sop->order] = strnstr(data,
-						searches[sop->order], size);
-		}
-		for_each_hook(sop, setprocattr) {
-			if (values[sop->order] != NULL) {
-				*values[sop->order] = '\0';
-				values[sop->order] = values[sop->order] +
-					strlen(searches[sop->order]);
-			}
-		}
 	}
+	/*
+	 * "/smack=Howdy/apparmor=confined/bandl=12/1,2,6/"
+	 *
+	 * Parse and dispatch sequentially, and use fail and bail so that
+	 * the caller can determine that setprocattr() has partially
+	 * succeeded via "return value" less than "size" argument.
+	 */
+next:
 	for_each_hook(sop, setprocattr) {
-		if (formatted && values[sop->order] == NULL)
+		const char *cp = sop->name;
+		size_t len = strlen(cp);
+		if (size < len + 2 || memcmp(data + 1, cp, len) ||
+		    data[len + 1] != '=')
 			continue;
-		if (only == 0)
-			only = sop->order;
-		else
-			only = -1;
-
-		if (formatted)
-			thisrc = sop->setprocattr(p, name, values[sop->order],
-					strlen(values[sop->order]) + 1);
-		else
-			thisrc = sop->setprocattr(p, name, value, size);
-
-		if (thisrc < 0)
-			rc = thisrc;
+		len += 2;
+		data += len;
+		size -= len;
+		cp = memchr(data, '/', size);
+		if (!cp || cp == data)
+			break;
+		len = cp - data;
+		rc = sop->setprocattr(p, name, data, len);
+		if (rc < 0)
+			break;
+		rc = -EINVAL;
+		data = (char *) cp;
+		size -= len;
+		processed = data - start + 1;
+		if (size > 1 && data[0] == '/')
+			goto next;
+		break;
 	}

security/security.c int security_setprocattr(struct task_struct *p, char *name, void *value, · line 2111

Quoted 10 lines
security/security.c +2 -4
-
-	if (only == 0)
-		return -EINVAL;
+	if (processed)
+		return processed;
 	return rc;
-
 }
 
 int security_netlink_send(struct sock *sk, struct sk_buff *skb)

--
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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CAGXu5j+jLx9y=MfBumy5sy8C=goWdicZ9dngqZVe2mjP=LoU3w@mail.gmail.com

#11 lore raw
Quoted 38 lines · Kees Cook
Quoted 36 lines · John Johansen
Quoted 20 lines · Tetsuo Handa
Quoted 3 lines · John Johansen

This is not
something I want to do but if that is what is necessary to get stacking done I will do it.

Sharing existing interfaces by managing the format is a thorny path. Future LSM modules, if they are to use existing interfaces, might want to allow attributes up to 4000 bytes when overall length is limited to 4096 bytes.

Quoted 3 lines

Of course modifying apparmor doesn't take care of other LSMs so either certain restrictions have to be part of the api for this interface or as Tetsuo suggests a new interface would be needed.

TOMOYO uses /sys/kernel/security/tomoyo/self_domain (handled by stateless tomoyo_read_self()/tomoyo_write_self()) for reading/updating current thread's attribute and /sys/kernel/security/tomoyo/.process_status (handled by stateful tomoyo_write_pid()/tomoyo_read_pid()) for reading arbitrary thread's attribute. I think that most programs will, if need to access attribute information, need to access only current thread's attribute. Therefore, /sys/kernel/security/tomoyo/self_domain handles only current thread and /sys/kernel/security/tomoyo/.process_status handles arbitrary threads.

Quoted 6 lines

If AppArmor can migrate from existing /proc/pid/attr/ to its own interfaces like TOMOYO does, we can immediately get LSM stacking with "optionally either SELinux or SMACK" + "optionally AppArmor" + "optionally TOMOYO" + "optionally Yama" + "optionally other LSM modules". SELinux and SMACK are forever exclusive due to conflicts on other hooks.

So moving apparmor to use its own interface for setting values is reasonable, though being able to fall back to /proc/<pid>/attr/* for none stacking cases to support older userspace releases would be good.

However getprocattr is a little more problematic as leveraging standard tools that have been modified to support the security context (eg. ps -Z, etc) has been standard practice, and is used a lot more than writing to /proc/<pid>/attr/* Moving to our own interface looses this standard tool support.

Quoted 4 lines
Quoted 2 lines

There are possible solutions, like multiplexing reading of /proc/<pid>/attr/*, but not writes (yes I know its still problematic).

Does it make sense to make attr read-only and leave its output multiplexed so "ps -Z" output is human readable?

This may work long term, but would break backward compatibility.

Quoted 1 line

Regardless, how about we just give attr to the "primary"

I much prefer referring to this as the "presented" LSM than as the "primary". All LSMs are equal, one just gets to talk louder.

Quoted 1 line

LSM for now,

The disadvantage is that ps -Z, which simply reads /proc/pid/attr/current has to get smarter all of a sudden.

Quoted 4 lines

just to land this patch series? It's already a big patch, and I think it would be beneficial to get the bulk of the stacking logic in. We can improve the attr handling going forward, since that doesn't seem to affect the _structure_ of the changes.

For my next trick I'll try using the CONFIG_SECURITY_PRESENT value if provided and the first LSM that includes a getprocattr hook otherwise.

Let me know if that sounds stupid.

--
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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw
Quoted 24 lines · Tetsuo Handa

John Johansen wrote:

(1) Start LSM stacking with a limitation that only one LSM module can use /proc/pid/attr/* interface.

(2) Introduce /proc/pid/attributes interface which contains all of the content of /proc/pid/attr/* with the name of each LSM module
(i.e. $type $lsmname $value format).
The content looks like

$ cat /proc/pid/attributes
current smack current_foo
prev smack prev_foo
exec smack exec_foo
fscreate smack fscreate_foo
keycreate smack keycreate_foo
sockcreate smack sockcreate_foo
current apparmor current_bar
prev apparmor prev_bar
exec apparmor exec_bar
fscreate apparmor fscreate_bar
keycreate apparmor keycreate_bar
sockcreate apparmor sockcreate_bar

Quoted 7 lines

and the operation for -Z option looks like

awk ' { if ($1 == "current" ) print $2"="$3; } ' /proc/pid/attributes

. When writing, the operation looks like

$ echo 'current smack new_current_foo' > /proc/self/attributes

That sounds great as a toy used as nothing but a developer to play with, but text file processing kind of sucks to do pragmatically. How often do you want to see another process's sockcreate, vs current? Every time we want to know the current smack label we'd have to process sockcreate for apparmor and all of the rest of this text file?
No thank you. We could make such a text file for admins if there is
a need, but I'd much rather see /proc/pid/attr/[LSM]/* than most of these proposals. Have 1 presenting LSM which uses /proc/pid/attr/* and if people want to stack they gotta move to a new interface...

Quoted 5 lines

(4) Add WARN_ONCE() upon accessing /proc/pid/attr/* in order to tell that /proc/pid/attr/* interface was replaced by /proc/self/attributes interface.

(5) Remove /proc/pid/attr/ and remove the limitation that only one LSM module can use /proc/pid/attr/* interface.

Not sure this is a ever possible. akpm runs really really really old userspace and gets mighty pissed at me if I break anything to do with SELinux.

Quoted 1 line

(6) Eventually, userspace tools will forget about /proc/pid/attr/* .

[....]

Quoted 2 lines

Keeping old and new interfaces within the same filesystem makes it easier for userspace tools to check and fall back.

Tools don't care. Heck, most tools ALREADY look at /proc/pid/attr/* and /sys/fs. Seems like a moot point.

Quoted 4 lines

Centralizing information to /proc/pid/attributes would be more suitable for handling a per-pid data than separated interfaces which the -Z option have to know the location and protocol for each LSM module's (e.g. selinuxfs, smackfs) mount point.

/proc/pid/ is definitely a better place than [lsm]fs. Things like ps -Z need to know the context of other pids, not just self. So we at least agree that a replacement interface needs to be in /proc.

My opinion is that ps -Z should NOT be responsible in any way for parsing files. It should make library calls to get answers about LSMs. It might need to make 1 library call per LSM but that library should understand how to retrieve the information it needs and every library shouldn't be re-parsing the same text file over and over again...

--
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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 67 lines · John Johansen
Quoted 42 lines · Eric Paris
Quoted 23 lines · Tetsuo Handa

John Johansen wrote:
(1) Start LSM stacking with a limitation that only one LSM module can use /proc/pid/attr/* interface.

(2) Introduce /proc/pid/attributes interface which contains all of the content of /proc/pid/attr/* with the name of each LSM module
(i.e. $type $lsmname $value format).
The content looks like

$ cat /proc/pid/attributes
current smack current_foo
prev smack prev_foo
exec smack exec_foo
fscreate smack fscreate_foo
keycreate smack keycreate_foo
sockcreate smack sockcreate_foo
current apparmor current_bar
prev apparmor prev_bar
exec apparmor exec_bar
fscreate apparmor fscreate_bar
keycreate apparmor keycreate_bar
sockcreate apparmor sockcreate_bar

Quoted 7 lines

and the operation for -Z option looks like

awk ' { if ($1 == "current" ) print $2"="$3; } ' /proc/pid/attributes

. When writing, the operation looks like

$ echo 'current smack new_current_foo' > /proc/self/attributes

That sounds great as a toy used as nothing but a developer to play with, but text file processing kind of sucks to do pragmatically. How often do you want to see another process's sockcreate, vs current? Every time we want to know the current smack label we'd have to process sockcreate for apparmor and all of the rest of this text file?
No thank you. We could make such a text file for admins if there is
a need, but I'd much rather see /proc/pid/attr/[LSM]/* than most of these proposals. Have 1 presenting LSM which uses /proc/pid/attr/* and if people want to stack they gotta move to a new interface...

Quoted 9 lines
Quoted 5 lines

(4) Add WARN_ONCE() upon accessing /proc/pid/attr/* in order to tell that /proc/pid/attr/* interface was replaced by /proc/self/attributes interface.

(5) Remove /proc/pid/attr/ and remove the limitation that only one LSM module can use /proc/pid/attr/* interface.

Not sure this is a ever possible. akpm runs really really really old userspace and gets mighty pissed at me if I break anything to do with SELinux.

right we need to keep backwards compatibility with the old interface at least when not stacking.

Quoted 12 lines
Quoted 1 line

(6) Eventually, userspace tools will forget about /proc/pid/attr/* .

[....]

Quoted 2 lines

Keeping old and new interfaces within the same filesystem makes it easier for userspace tools to check and fall back.

Tools don't care. Heck, most tools ALREADY look at /proc/pid/attr/* and /sys/fs. Seems like a moot point.

Quoted 4 lines

Centralizing information to /proc/pid/attributes would be more suitable for handling a per-pid data than separated interfaces which the -Z option have to know the location and protocol for each LSM module's (e.g. selinuxfs, smackfs) mount point.

Quoted 5 lines
Quoted 4 lines

/proc/pid/ is definitely a better place than [lsm]fs. Things like ps -Z need to know the context of other pids, not just self. So we at least agree that a replacement interface needs to be in /proc.

yeah I agree /proc is better for this as well

There are a some options for adding the requisite /proc interfaces.

1. Statically add all of them. This is an extension of what's done today and the easiest.
A. Use CONFIG_SECURITY_XXX and ifdef out unused interfaces.
On a system without SELinux you see no attr/sockcreate.
B. Put the LSM specific files in subdirectories,
for example attr/smack/current.
C. Put the LSM specific files directly in attr. This is how it
is done today (e.g. attr/sockcreate). Use a prefix convention (attr/smack-current attr/apparmor-current) to identify the LSM.
D. Like C, but use a suffix (e.g. selinux.current).
2. Add a security_addprocfs hook so that LSMs can add interfaces without changing the procfs code each time. This would be lots more work and could result in an attr directory filled with junk.
A. Leave it all up to the LSMs and don't create any entries
unless requested to.
B. Create the "traditional" entries always.

My initial thought is 1AD.

Quoted 8 lines
Quoted 7 lines

My opinion is that ps -Z should NOT be responsible in any way for parsing files. It should make library calls to get answers about LSMs. It might need to make 1 library call per LSM but that library should understand how to retrieve the information it needs and every library shouldn't be re-parsing the same text file over and over again...

agreed

% echo cat /proc/<pid>/attr/*.current

--
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.