Skip to contentSkip to conversation

LSM: Stacking for major security modules

RFC PATCH In review Casey Schaufler 4 participants 21 messages Aug 29, 2017 – Aug 31, 2017

Subsystems (15)
lore
Share & tools
lore

Casey Schaufler

author maintainer
#0 lore raw

cover LSM: Stacking for major security modules

Subject: [PATCH RFC 00/11] LSM: Stacking for major security modules

I am again sending this as an RFC. There are significant differences from the previous versions. These are in response to feedback on the mechanisms for dealing with security contexts outside of the security modules, including user space code.

This patch set implements stacking for "major" security modules that use cred and file blobs. Management of security blobs is moved from the security modules and into the LSM infrastructure. This has been proposed in the past by Serge Hallyn and David Howells. This implementation owes much to their work.

The bulk of the change is in abstracting use of blobs within the security modules. This allows the modules to share a single blob and hides the details from the code. Modules are required to declare the amount of space they require for each blob they use. Because modules deal with blobs during their initialization the blob sizes must be declared prior to module initialization. The module initialization becomes a two step process.

Security module stacking is optional. If stacking is not configured, the CONFIG_DEFAULT_SECURITY value is used, just as before. If stacking is configured using CONFIG_SECURITY_STACKING the modules desired for the stack are selected individually. AppArmor would be selected by specifying CONFIG_SECURITY_APPARMOR_STACKED. The CONFIG_DEFAULT_SECURITY is ignored. The security= boot option is still respected and has the same behavior as before, allowing a single module to be used instead of the specified stack.

A prctl() interface is provided to direct the LSM interface as to which module's information should be reported to user space. If not specified, the first registered module will be presented.

Some filesystems, including kernfs, use security context strings to represent the security attributes on a file. To accommodate multiple active modules a security "context" is defined to use a regular format:

	lsmname='lsmvalue'[,lsmname='lsmvalue']...

This is not exposed to user space run time code except in the /proc/.../attr/context interface.

I have tested these patches in various configurations of Ubuntu and Fedora. Smack and SELinux together pass test suites with some exceptions. Smack does not (yet) deal with overlayfs, so those tests are omitted. There are conflicts with the way the modules treat network configurations. These conflicts are under investigation, and changes to Smack (and possibly SELinux) to reconcile the worst of the issues are in development.

Patch 01 Adds a smack subdirectory in /proc/.../attr Patch 02 Move management of the cred blob to the LSM infrastructure. Patch 03 Move management of the file blob to the LSM infrastructure. Patch 04 Move management of the task blob to the LSM infrastructure. Patch 05 Infrastructure blob management for IPC, keys, sockets. Patch 06 Allow stacking of modules except for SELinux and Smack Patch 07 Mapping from module secids to system secids (tokens) Patch 08 Fix superblock blobs in Smack.
Patch 09 Mount options for multiple modules.
Patch 10 Allocate task blobs.
Patch 11 Allow stacking of all modules.

These patches can be found in git at:

	https://github.com/cschaufler/smack-next.git#stacking-4.13-rc2

---

 Documentation/admin-guide/LSM/index.rst |   31 +-
 fs/btrfs/super.c                        |   10 +-
 fs/proc/base.c                          |   96 ++-
 fs/proc/internal.h                      |    1 +
 fs/xattr.c                              |    6 +-
 include/linux/lsm_audit.h               |    4 +
 include/linux/lsm_hooks.h               |   86 +-
 include/linux/security.h                |   69 +-
 include/net/request_sock.h              |    2 +
 include/uapi/linux/prctl.h              |    6 +
 kernel/cred.c                           |   13 -
 kernel/fork.c                           |    3 +
 net/netlabel/netlabel_unlabeled.c       |    2 +-
 security/Kconfig                        |   90 +++
 security/Makefile                       |    1 +
 security/apparmor/context.c             |    2 -
 security/apparmor/include/context.h     |   24 +-
 security/apparmor/include/file.h        |    2 +-
 security/apparmor/lsm.c                 |  105 ++-
 security/security.c                     | 1332 +++++++++++++++++++++++++++++--
 security/selinux/hooks.c                |  651 ++++++---------
 security/selinux/include/objsec.h       |  100 ++-
 security/selinux/include/security.h     |    3 +-
 security/selinux/include/xfrm.h         |    2 +-
 security/selinux/netlabel.c             |   20 +-
 security/selinux/selinuxfs.c            |    5 +-
 security/selinux/ss/services.c          |    7 +-
 security/selinux/xfrm.c                 |   10 +-
 security/smack/smack.h                  |  101 ++-
 security/smack/smack_access.c           |    2 +-
 security/smack/smack_lsm.c              |  655 ++++++---------
 security/smack/smack_netfilter.c        |   12 +-
 security/smack/smackfs.c                |   21 +-
 security/stacking.c                     |  198 +++++
 security/tomoyo/common.h                |   30 +-
 security/tomoyo/domain.c                |    4 +-
 security/tomoyo/securityfs_if.c         |   13 +-
 security/tomoyo/tomoyo.c                |   52 +-
 38 files changed, 2739 insertions(+), 1032 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#1 lore raw

PATCH 1/11 procfs: add smack subdir to attrs

Subject: [PATCH 01/11] procfs: add smack subdir to attrs

Back in 2007 I made what turned out to be a rather serious mistake in the implementation of the Smack security module. The SELinux module used an interface in /proc to manipulate the security context on processes. Rather than use a similar interface, I used the same interface. The AppArmor team did likewise. Now /proc/.../attr/current will tell you the security "context" of the process, but it will be different depending on the security module you're using.

This patch provides a subdirectory in /proc/.../attr for Smack. Smack user space can use the "current" file in this subdirectory and never have to worry about getting SELinux attributes by mistake. Programs that use the old interface will continue to work (or fail, as the case may be) as before.

This patch does not include subdirectories for SELinux or AppArmor. I do have a patch that provides those, and will happily make it available should anyone see value in it.

The original implementation is by Kees Cook.

---
 Documentation/admin-guide/LSM/index.rst | 13 +++++--
 fs/proc/base.c                          | 63 ++++++++++++++++++++++++++++-----
 fs/proc/internal.h                      |  1 +
 include/linux/security.h                | 15 +++++---
 security/security.c                     | 30 +++++++++++++---
 5 files changed, 101 insertions(+), 21 deletions(-)
Documentation/admin-guide/LSM/index.rst +10 -3
index c980dfe9abf1..9842e21afd4a 100644
@@ -17,9 +17,8 @@ MAC extensions, other extensions can be built using the LSM to provide
 specific changes to system operation when these tweaks are not available
 in the core functionality of Linux itself.
 
-Without a specific LSM built into the kernel, the default LSM will be the
-Linux capabilities system. Most LSMs choose to extend the capabilities
-system, building their checks on top of the defined capability hooks.
+The Linux capabilities modules will always be included. This may be
+followed by any number of "minor" modules and at most one "major" module.
 For more details on capabilities, see ``capabilities(7)`` in the Linux
 man-pages project.
 
@@ -30,6 +29,14 @@ order in which checks are made. The capability module will always
 be first, followed by any "minor" modules (e.g. Yama) and then
 the one "major" module (e.g. SELinux) if there is one configured.
 
+Process attributes associated with "major" security modules should
+be accessed and maintained using the special files in ``/proc/.../attr``.
+A security module may maintain a module specific subdirectory there,
+named after the module. ``/proc/.../attr/smack`` is provided by the Smack
+security module and contains all its special files. The files directly
+in ``/proc/.../attr`` remain as legacy interfaces for modules that provide
+subdirectories.
+
 .. toctree::
    :maxdepth: 1
 
fs/proc/base.c +54 -9

90 lines not on this page · raw message

fs/proc/internal.h +1 -0
index aa2b89071630..ba0a8a889ed5 100644
@@ -58,6 +58,7 @@ union proc_op {
 	int (*proc_show)(struct seq_file *m,
 		struct pid_namespace *ns, struct pid *pid,
 		struct task_struct *task);
+	const char *lsm;
 };
 
 struct proc_inode {
include/linux/security.h +10 -5
index 458e24bea2d4..8317ace3c30f 100644
@@ -375,8 +375,10 @@ int security_sem_semctl(struct sem_array *sma, int cmd);
 int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 			unsigned nsops, int alter);
 void security_d_instantiate(struct dentry *dentry, struct inode *inode);
-int security_getprocattr(struct task_struct *p, char *name, char **value);
-int security_setprocattr(const char *name, void *value, size_t size);
+int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
+			 char **value);
+int security_setprocattr(const char *lsm, const char *name, void *value,
+			 size_t size);
 int security_netlink_send(struct sock *sk, struct sk_buff *skb);
 int security_ismaclabel(const char *name);
 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen);
@@ -1133,15 +1135,18 @@ static inline int security_sem_semop(struct sem_array *sma,
 	return 0;
 }
 
-static inline void security_d_instantiate(struct dentry *dentry, struct inode *inode)
+static inline void security_d_instantiate(struct dentry *dentry,
+					  struct inode *inode)
 { }
 
-static inline int security_getprocattr(struct task_struct *p, char *name, char **value)
+static inline int security_getprocattr(struct task_struct *p, const char *lsm,
+				       char *name, char **value)
 {
 	return -EINVAL;
 }
 
-static inline int security_setprocattr(char *name, void *value, size_t size)
+static inline int security_setprocattr(const char *lsm, char *name,
+				       void *value, size_t size)
 {
 	return -EINVAL;
 }
security/security.c +26 -4
index 55b5997e4b72..1618798a5e29 100644
@@ -1258,14 +1258,36 @@ void security_d_instantiate(struct dentry *dentry, struct inode *inode)
 }
 EXPORT_SYMBOL(security_d_instantiate);
 
-int security_getprocattr(struct task_struct *p, char *name, char **value)
+int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
+				char **value)
 {
-	return call_int_hook(getprocattr, -EINVAL, p, name, value);
+	struct security_hook_list *hp;
+	int rc;
+
+	list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
+		if (lsm != NULL && strcmp(lsm, hp->lsm))
+			continue;
+		rc = hp->hook.getprocattr(p, name, value);
+		if (rc != -ENOENT)
+			return rc;
+	}
+	return -EINVAL;
 }
 
-int security_setprocattr(const char *name, void *value, size_t size)
+int security_setprocattr(const char *lsm, const char *name, void *value,
+			 size_t size)
 {
-	return call_int_hook(setprocattr, -EINVAL, name, value, size);
+	struct security_hook_list *hp;
+	int rc;
+
+	list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
+		if (lsm != NULL && strcmp(lsm, hp->lsm))
+			continue;
+		rc = hp->hook.setprocattr(name, value, size);
+		if (rc != -ENOENT)
+			return rc;
+	}
+	return -EINVAL;
 }
 
 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#3 lore raw

Subject: [PATCH 02/11] LSM: manage credential security blobs

In reply to Casey Schaufler

Subject: [PATCH 02/11] LSM: manage credential security blobs

Move the management of credential security blobs from the individual security modules to the security infrastructure. The security modules using credential blobs have been updated accordingly. Modules are required to identify the space they require at module initialization. In some cases a module no longer needs to supply blob management hook, in which case the hook has been removed.

---
 include/linux/lsm_hooks.h           |  14 ++++
 kernel/cred.c                       |  13 ----
 security/Kconfig                    |  11 +++
 security/apparmor/context.c         |   2 -
 security/apparmor/include/context.h |   9 ++-
 security/apparmor/lsm.c             |  44 ++++--------
 security/security.c                 |  98 +++++++++++++++++++++++++-
 security/selinux/hooks.c            | 115 ++++++++++++-------------------
 security/selinux/include/objsec.h   |   9 +++
 security/selinux/selinuxfs.c        |   1 +
 security/selinux/xfrm.c             |   4 +-
 security/smack/smack.h              |  15 +++-
 security/smack/smack_access.c       |   2 +-
 security/smack/smack_lsm.c          | 134 +++++++++++++++---------------------
 security/smack/smackfs.c            |  18 ++---
 security/tomoyo/common.h            |  20 +++++-
 security/tomoyo/domain.c            |   4 +-
 security/tomoyo/securityfs_if.c     |  13 ++--
 security/tomoyo/tomoyo.c            |  49 ++++++++++---
 19 files changed, 346 insertions(+), 229 deletions(-)
include/linux/lsm_hooks.h +14 -0
index ce02f76a6188..4ecb4ed572cf 100644
@@ -1919,6 +1919,13 @@ struct security_hook_list {
 } __randomize_layout;
 
 /*
+ * Security blob size or offset data.
+ */
+struct lsm_blob_sizes {
+	int	lbs_cred;
+};
+
+/*
  * Initializing a security_hook_list structure takes
  * up a lot of space in a source file. This macro takes
  * care of the common case and reduces the amount of
@@ -1930,6 +1937,7 @@ struct security_hook_list {
 extern struct security_hook_heads security_hook_heads;
 extern char *lsm_names;
 
+extern void security_add_blobs(struct lsm_blob_sizes *needed);
 extern void security_add_hooks(struct security_hook_list *hooks, int count,
 				char *lsm);
 
@@ -1976,4 +1984,10 @@ void __init loadpin_add_hooks(void);
 static inline void loadpin_add_hooks(void) { };
 #endif
 
+extern int lsm_cred_alloc(struct cred *cred, gfp_t gfp);
+
+#ifdef CONFIG_SECURITY
+void lsm_early_cred(struct cred *cred);
+#endif
+
 #endif /* ! __LINUX_LSM_HOOKS_H */
kernel/cred.c +0 -13
index ecf03657e71c..fa2061ee4955 100644
@@ -704,19 +704,6 @@ bool creds_are_invalid(const struct cred *cred)
 {
 	if (cred->magic != CRED_MAGIC)
 		return true;
-#ifdef CONFIG_SECURITY_SELINUX
-	/*
-	 * cred->security == NULL if security_cred_alloc_blank() or
-	 * security_prepare_creds() returned an error.
-	 */
-	if (selinux_is_enabled() && cred->security) {
-		if ((unsigned long) cred->security < PAGE_SIZE)
-			return true;
-		if ((*(u32 *)cred->security & 0xffffff00) ==
-		    (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
-			return true;
-	}
-#endif
 	return false;
 }
 EXPORT_SYMBOL(creds_are_invalid);
security/Kconfig +11 -0
index e8e449444e65..f3464fb5a8b0 100644
@@ -36,6 +36,17 @@ config SECURITY_WRITABLE_HOOKS
 	bool
 	default n
 
+config SECURITY_LSM_DEBUG
+	bool "Enable debugging of the LSM infrastructure"
+	depends on SECURITY
+	help
+	  This allows you to choose debug messages related to
+	  security modules configured into your kernel. These
+	  messages may be helpful in determining how a security
+	  module is using security blobs.
+
+	  If you are unsure how to answer this question, answer N.
+
 config SECURITYFS
 	bool "Enable the securityfs filesystem"
 	help
security/apparmor/context.c +0 -2
index c95f1ac6190b..db203ee24db8 100644
@@ -50,8 +50,6 @@ void aa_free_task_context(struct aa_task_ctx *ctx)
 		aa_put_label(ctx->label);
 		aa_put_label(ctx->previous);
 		aa_put_label(ctx->onexec);
-
-		kzfree(ctx);
 	}
 }
 
security/apparmor/include/context.h +7 -2
index 6ae07e9aaa17..301ab3a0dd04 100644
@@ -18,11 +18,12 @@
 #include <linux/cred.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
+#include <linux/lsm_hooks.h>
 
 #include "label.h"
 #include "policy_ns.h"
 
-#define cred_ctx(X) ((X)->security)
+#define cred_ctx(X) apparmor_cred(X)
 #define current_ctx() cred_ctx(current_cred())
 
 /**
@@ -54,6 +55,10 @@ int aa_set_current_hat(struct aa_label *label, u64 token);
 int aa_restore_previous_label(u64 cookie);
 struct aa_label *aa_get_task_label(struct task_struct *task);
 
+static inline struct aa_task_ctx *apparmor_cred(const struct cred *cred)
+{
+	return cred->security;
+}
 
 /**
  * aa_cred_raw_label - obtain cred's label
@@ -65,7 +70,7 @@ struct aa_label *aa_get_task_label(struct task_struct *task);
  */
 static inline struct aa_label *aa_cred_raw_label(const struct cred *cred)
 {
-	struct aa_task_ctx *ctx = cred_ctx(cred);
+	struct aa_task_ctx *ctx = apparmor_cred(cred);
 
 	AA_BUG(!ctx || !ctx->label);
 	return ctx->label;
security/apparmor/lsm.c +15 -29

84 lines not on this page · raw message

security/security.c +97 -1

135 lines not on this page · raw message

security/selinux/hooks.c +43 -72

302 lines not on this page · raw message

security/selinux/include/objsec.h +9 -0
index 6ebc61e370ff..f447b21f6be0 100644
@@ -25,6 +25,9 @@
 #include <linux/binfmts.h>
 #include <linux/in.h>
 #include <linux/spinlock.h>
+#include <linux/lsm_hooks.h>
+#include <linux/msg.h>
+#include <net/sock.h>
 #include <net/net_namespace.h>
 #include "flask.h"
 #include "avc.h"
@@ -151,5 +154,11 @@ struct pkey_security_struct {
 };
 
 extern unsigned int selinux_checkreqprot;
+extern struct lsm_blob_sizes selinux_blob_sizes;
+
+static inline struct task_security_struct *selinux_cred(const struct cred *cred)
+{
+	return cred->security;
+}
 
 #endif /* _SELINUX_OBJSEC_H_ */
security/selinux/selinuxfs.c +1 -0
index 00eed842c491..855a13053a81 100644
@@ -30,6 +30,7 @@
 #include <linux/uaccess.h>
 #include <linux/kobject.h>
 #include <linux/ctype.h>
+#include <linux/lsm_hooks.h>
 
 /* selinuxfs pseudo filesystem for exporting the security policy API.
    Based on the proc code and the fs/nfsd/nfsctl.c code. */
security/selinux/xfrm.c +2 -2
index 56e354fcdfc6..789d07bd900f 100644
@@ -79,7 +79,7 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp,
 				   gfp_t gfp)
 {
 	int rc;
-	const struct task_security_struct *tsec = current_security();
+	const struct task_security_struct *tsec = selinux_cred(current_cred());
 	struct xfrm_sec_ctx *ctx = NULL;
 	u32 str_len;
 
@@ -136,7 +136,7 @@ static void selinux_xfrm_free(struct xfrm_sec_ctx *ctx)
  */
 static int selinux_xfrm_delete(struct xfrm_sec_ctx *ctx)
 {
-	const struct task_security_struct *tsec = current_security();
+	const struct task_security_struct *tsec = selinux_cred(current_cred());
 
 	if (!ctx)
 		return 0;
security/smack/smack.h +12 -3
index 6a71fc7831ab..ab1d217800e2 100644
@@ -24,6 +24,7 @@
 #include <linux/list.h>
 #include <linux/rculist.h>
 #include <linux/lsm_audit.h>
+#include <linux/msg.h>
 
 /*
  * Use IPv6 port labeling if IPv6 is enabled and secmarks
@@ -355,6 +356,11 @@ extern struct list_head smack_onlycap_list;
 #define SMACK_HASH_SLOTS 16
 extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
 
+static inline struct task_smack *smack_cred(const struct cred *cred)
+{
+	return cred->security;
+}
+
 /*
  * Is the directory transmuting?
  */
@@ -381,13 +387,16 @@ static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
 	return tsp->smk_task;
 }
 
-static inline struct smack_known *smk_of_task_struct(const struct task_struct *t)
+static inline struct smack_known *smk_of_task_struct(
+						const struct task_struct *t)
 {
 	struct smack_known *skp;
+	const struct cred *cred;
 
 	rcu_read_lock();
-	skp = smk_of_task(__task_cred(t)->security);
+	cred = __task_cred(t);
 	rcu_read_unlock();
+	skp = smk_of_task(smack_cred(cred));
 	return skp;
 }
 
@@ -404,7 +413,7 @@ static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
  */
 static inline struct smack_known *smk_of_current(void)
 {
-	return smk_of_task(current_security());
+	return smk_of_task(smack_cred(current_cred()));
 }
 
 /*
security/smack/smack_access.c +1 -1
index 1a3004189447..e1d304c65fe3 100644
@@ -275,7 +275,7 @@ int smk_tskacc(struct task_smack *tsp, struct smack_known *obj_known,
 int smk_curacc(struct smack_known *obj_known,
 	       u32 mode, struct smk_audit_info *a)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_tskacc(tsp, obj_known, mode, a);
 }
security/smack/smack_lsm.c +55 -79

349 lines not on this page · raw message

security/smack/smackfs.c +9 -9
index f6482e53d55a..9d2dde608298 100644
@@ -2208,14 +2208,14 @@ static const struct file_operations smk_logging_ops = {
 
 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_start(s, pos, &tsp->smk_rules);
 }
 
 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_next(s, v, pos, &tsp->smk_rules);
 }
@@ -2262,7 +2262,7 @@ static int smk_open_load_self(struct inode *inode, struct file *file)
 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
 			      size_t count, loff_t *ppos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
 				    &tsp->smk_rules_lock, SMK_FIXED24_FMT);
@@ -2414,14 +2414,14 @@ static const struct file_operations smk_load2_ops = {
 
 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_start(s, pos, &tsp->smk_rules);
 }
 
 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_next(s, v, pos, &tsp->smk_rules);
 }
@@ -2467,7 +2467,7 @@ static int smk_open_load_self2(struct inode *inode, struct file *file)
 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
 			      size_t count, loff_t *ppos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
 				    &tsp->smk_rules_lock, SMK_LONG_FMT);
@@ -2681,14 +2681,14 @@ static const struct file_operations smk_syslog_ops = {
 
 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_start(s, pos, &tsp->smk_relabel);
 }
 
 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 
 	return smk_seq_next(s, v, pos, &tsp->smk_relabel);
 }
@@ -2736,7 +2736,7 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file)
 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
 				size_t count, loff_t *ppos)
 {
-	struct task_smack *tsp = current_security();
+	struct task_smack *tsp = smack_cred(current_cred());
 	char *data;
 	int rc;
 	LIST_HEAD(list_tmp);
security/tomoyo/common.h +18 -2
index 361e7a284699..cbcfccc84784 100644
@@ -28,6 +28,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <linux/un.h>
+#include <linux/lsm_hooks.h>
 #include <net/sock.h>
 #include <net/af_unix.h>
 #include <net/ip.h>
@@ -1196,13 +1197,26 @@ static inline void tomoyo_put_group(struct tomoyo_group *group)
 }
 
 /**
+ * tomoyo_cred - Get a pointer to the tomoyo cred security blob
+ * @cred - the relevant cred
+ *
+ * Returns pointer to the tomoyo cred blob.
+ */
+static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred)
+{
+	return cred->security;
+}
+
+/**
  * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
  *
  * Returns pointer to "struct tomoyo_domain_info" for current thread.
  */
 static inline struct tomoyo_domain_info *tomoyo_domain(void)
 {
-	return current_cred()->security;
+	struct tomoyo_domain_info **blob = tomoyo_cred(current_cred());
+
+	return *blob;
 }
 
 /**
@@ -1215,7 +1229,9 @@ static inline struct tomoyo_domain_info *tomoyo_domain(void)
 static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
 							    *task)
 {
-	return task_cred_xxx(task, security);
+	struct tomoyo_domain_info **blob = tomoyo_cred(get_task_cred(task));
+
+	return *blob;
 }
 
 /**
security/tomoyo/domain.c +3 -1
index 00d223e9fb37..80ebb422c02b 100644
@@ -677,6 +677,7 @@ static int tomoyo_environ(struct tomoyo_execve *ee)
  */
 int tomoyo_find_next_domain(struct linux_binprm *bprm)
 {
+	struct tomoyo_domain_info **blob;
 	struct tomoyo_domain_info *old_domain = tomoyo_domain();
 	struct tomoyo_domain_info *domain = NULL;
 	const char *original_name = bprm->filename;
@@ -842,7 +843,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
 		domain = old_domain;
 	/* Update reference count on "struct tomoyo_domain_info". */
 	atomic_inc(&domain->users);
-	bprm->cred->security = domain;
+	blob = tomoyo_cred(bprm->cred);
+	*blob = domain;
 	kfree(exename.name);
 	if (!retval) {
 		ee->r.domain = domain;
security/tomoyo/securityfs_if.c +9 -4
index 06ab41b1ff28..9289f2a16036 100644
@@ -70,9 +70,12 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf,
 				if (!cred) {
 					error = -ENOMEM;
 				} else {
-					struct tomoyo_domain_info *old_domain =
-						cred->security;
-					cred->security = new_domain;
+					struct tomoyo_domain_info **blob;
+					struct tomoyo_domain_info *old_domain;
+
+					blob = tomoyo_cred(cred);
+					old_domain = *blob;
+					*blob = new_domain;
 					atomic_inc(&new_domain->users);
 					atomic_dec(&old_domain->users);
 					commit_creds(cred);
@@ -233,10 +236,12 @@ static void __init tomoyo_create_entry(const char *name, const umode_t mode,
  */
 static int __init tomoyo_initerface_init(void)
 {
+	struct tomoyo_domain_info *domain;
 	struct dentry *tomoyo_dir;
 
+	domain = tomoyo_domain();
 	/* Don't create securityfs entries unless registered. */
-	if (current_cred()->security != &tomoyo_kernel_domain)
+	if (domain != &tomoyo_kernel_domain)
 		return 0;
 
 	tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
security/tomoyo/tomoyo.c +40 -9

109 lines not on this page · raw message

Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#4 lore raw

PATCH 3/11 LSM: Manage file security blobs

In reply to Casey Schaufler

Subject: [PATCH 03/11] LSM: Manage file security blobs

Move the management of file security blobs from the individual security modules to the security infrastructure. The security modules using file blobs have been updated accordingly. Modules are required to identify the space they need at module initialization. In some cases a module no longer needs to supply a blob management hook, in which case the hook has been removed.

---
 include/linux/lsm_hooks.h           |  1 +
 security/apparmor/include/context.h |  5 +++++
 security/apparmor/include/file.h    |  2 +-
 security/apparmor/lsm.c             | 19 +++++++++--------
 security/security.c                 | 32 +++++++++++++++++++++++++++++
 security/selinux/hooks.c            | 41 +++++++++----------------------------
 security/selinux/include/objsec.h   |  5 +++++
 security/smack/smack.h              |  5 +++++
 security/smack/smack_lsm.c          | 26 ++++++++---------------
 9 files changed, 78 insertions(+), 58 deletions(-)
include/linux/lsm_hooks.h +1 -0
index 4ecb4ed572cf..0603c57726e4 100644
@@ -1923,6 +1923,7 @@ struct security_hook_list {
  */
 struct lsm_blob_sizes {
 	int	lbs_cred;
+	int	lbs_file;
 };
 
 /*
security/apparmor/include/context.h +5 -0
index 301ab3a0dd04..c6e106a533e8 100644
@@ -87,6 +87,11 @@ static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
 	return aa_get_newest_label(aa_cred_raw_label(cred));
 }
 
+static inline struct aa_file_ctx *apparmor_file(const struct file *file)
+{
+	return file->f_security;
+}
+
 /**
  * __aa_task_raw_label - retrieve another task's label
  * @task: task to query  (NOT NULL)
security/apparmor/include/file.h +1 -1
index 001e40073ff9..af87b23700d7 100644
@@ -32,7 +32,7 @@ struct path;
 				 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
 				 AA_EXEC_MMAP | AA_MAY_LINK)
 
-#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
+#define file_ctx(X) apparmor_file(X)
 
 /* struct aa_file_ctx - the AppArmor context the file was opened in
  * @lock: lock to update the ctx
security/apparmor/lsm.c +10 -9
index 827df7012fe4..fb317cc94510 100644
@@ -400,21 +400,21 @@ static int apparmor_file_open(struct file *file, const struct cred *cred)
 
 static int apparmor_file_alloc_security(struct file *file)
 {
-	int error = 0;
-
-	/* freed by apparmor_file_free_security */
+	struct aa_file_ctx *ctx = file_ctx(file);
 	struct aa_label *label = begin_current_label_crit_section();
-	file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
-	if (!file_ctx(file))
-		error = -ENOMEM;
-	end_current_label_crit_section(label);
 
-	return error;
+	spin_lock_init(&ctx->lock);
+	rcu_assign_pointer(ctx->label, aa_get_label(label));
+	end_current_label_crit_section(label);
+	return 0;
 }
 
 static void apparmor_file_free_security(struct file *file)
 {
-	aa_free_file_ctx(file_ctx(file));
+	struct aa_file_ctx *ctx = file_ctx(file);
+
+	if (ctx)
+		aa_put_label(rcu_access_pointer(ctx->label));
 }
 
 static int common_file_perm(const char *op, struct file *file, u32 mask)
@@ -635,6 +635,7 @@ static int apparmor_task_setrlimit(struct task_struct *task,
 
 struct lsm_blob_sizes apparmor_blob_sizes = {
 	.lbs_cred = sizeof(struct aa_task_ctx),
+	.lbs_file = sizeof(struct aa_file_ctx),
 };
 
 static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
security/security.c +32 -0
index 89d43c65630c..b9346db8a2d4 100644
@@ -91,6 +91,7 @@ int __init security_init(void)
 
 #ifdef CONFIG_SECURITY_LSM_DEBUG
 	pr_info("LSM: cred blob size       = %d\n", blob_sizes.lbs_cred);
+	pr_info("LSM: file blob size       = %d\n", blob_sizes.lbs_file);
 #endif
 
 	return 0;
@@ -267,6 +268,30 @@ static void __init lsm_set_size(int *need, int *lbs)
 void __init security_add_blobs(struct lsm_blob_sizes *needed)
 {
 	lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
+	lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
+}
+
+/**
+ * lsm_file_alloc - allocate a composite file blob
+ * @file: the file that needs a blob
+ *
+ * Allocate the file blob for all the modules
+ *
+ * Returns 0, or -ENOMEM if memory can't be allocated.
+ */
+int lsm_file_alloc(struct file *file)
+{
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	if (file->f_security)
+		pr_info("%s: Inbound file blob is not NULL.\n", __func__);
+#endif
+	if (blob_sizes.lbs_file == 0)
+		return 0;
+
+	file->f_security = kzalloc(blob_sizes.lbs_file, GFP_KERNEL);
+	if (file->f_security == NULL)
+		return -ENOMEM;
+	return 0;
 }
 
 /*
@@ -957,12 +982,19 @@ int security_file_permission(struct file *file, int mask)
 
 int security_file_alloc(struct file *file)
 {
+	int rc = lsm_file_alloc(file);
+
+	if (rc)
+		return rc;
 	return call_int_hook(file_alloc_security, 0, file);
 }
 
 void security_file_free(struct file *file)
 {
 	call_void_hook(file_free_security, file);
+
+	kfree(file->f_security);
+	file->f_security = NULL;
 }
 
 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
security/selinux/hooks.c +10 -31

133 lines not on this page · raw message

security/selinux/include/objsec.h +5 -0
index f447b21f6be0..1f39105731ee 100644
@@ -161,4 +161,9 @@ static inline struct task_security_struct *selinux_cred(const struct cred *cred)
 	return cred->security;
 }
 
+static inline struct file_security_struct *selinux_file(const struct file *file)
+{
+	return file->f_security;
+}
+
 #endif /* _SELINUX_OBJSEC_H_ */
security/smack/smack.h +5 -0
index ab1d217800e2..d14e8d17eea0 100644
@@ -361,6 +361,11 @@ static inline struct task_smack *smack_cred(const struct cred *cred)
 	return cred->security;
 }
 
+static inline struct smack_known **smack_file(const struct file *file)
+{
+	return file->f_security;
+}
+
 /*
  * Is the directory transmuting?
  */
security/smack/smack_lsm.c +9 -17
index d67ba55f0123..c2a585cfef98 100644
@@ -1601,25 +1601,13 @@ static void smack_inode_getsecid(struct inode *inode, u32 *secid)
  */
 static int smack_file_alloc_security(struct file *file)
 {
-	struct smack_known *skp = smk_of_current();
+	struct smack_known **blob = smack_file(file);
 
-	file->f_security = skp;
+	*blob = smk_of_current();
 	return 0;
 }
 
 /**
- * smack_file_free_security - clear a file security blob
- * @file: the object
- *
- * The security blob for a file is a pointer to the master
- * label list, so no memory is freed.
- */
-static void smack_file_free_security(struct file *file)
-{
-	file->f_security = NULL;
-}
-
-/**
  * smack_file_ioctl - Smack check on ioctls
  * @file: the object
  * @cmd: what to do
@@ -1843,7 +1831,9 @@ static int smack_mmap_file(struct file *file,
  */
 static void smack_file_set_fowner(struct file *file)
 {
-	file->f_security = smk_of_current();
+	struct smack_known **blob = smack_file(file);
+
+	*blob = smk_of_current();
 }
 
 /**
@@ -1860,6 +1850,7 @@ static void smack_file_set_fowner(struct file *file)
 static int smack_file_send_sigiotask(struct task_struct *tsk,
 				     struct fown_struct *fown, int signum)
 {
+	struct smack_known **blob;
 	struct smack_known *skp;
 	struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
 	struct file *file;
@@ -1872,7 +1863,8 @@ static int smack_file_send_sigiotask(struct task_struct *tsk,
 	file = container_of(fown, struct file, f_owner);
 
 	/* we don't log here as rc can be overriden */
-	skp = file->f_security;
+	blob = smack_file(file);
+	skp = *blob;
 	rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
 	rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
 	if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
@@ -4604,6 +4596,7 @@ static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
 
 struct lsm_blob_sizes smack_blob_sizes = {
 	.lbs_cred = sizeof(struct task_smack),
+	.lbs_file = sizeof(struct smack_known *),
 };
 
 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
@@ -4643,7 +4636,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
 
 	LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
-	LSM_HOOK_INIT(file_free_security, smack_file_free_security),
 	LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
 	LSM_HOOK_INIT(file_lock, smack_file_lock),
 	LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#8 lore raw

PATCH 4/11 LSM: manage task security blobs

In reply to Casey Schaufler

Subject: [PATCH 04/11] LSM: manage task security blobs

Move management of task security blobs into the security infrastructure. Modules are required to identify the space they require. At this time there are no modules that use task blobs.

---
 include/linux/lsm_hooks.h |  1 +
 security/security.c       | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/linux/lsm_hooks.h +1 -0
index 0603c57726e4..8d6e757e78dc 100644
@@ -1924,6 +1924,7 @@ struct security_hook_list {
 struct lsm_blob_sizes {
 	int	lbs_cred;
 	int	lbs_file;
+	int	lbs_task;
 };
 
 /*
security/security.c +28 -0
index b9346db8a2d4..3ab260b6ae96 100644
@@ -92,6 +92,7 @@ int __init security_init(void)
 #ifdef CONFIG_SECURITY_LSM_DEBUG
 	pr_info("LSM: cred blob size       = %d\n", blob_sizes.lbs_cred);
 	pr_info("LSM: file blob size       = %d\n", blob_sizes.lbs_file);
+	pr_info("LSM: task blob size       = %d\n", blob_sizes.lbs_task);
 #endif
 
 	return 0;
@@ -269,6 +270,7 @@ void __init security_add_blobs(struct lsm_blob_sizes *needed)
 {
 	lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
 	lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
+	lsm_set_size(&needed->lbs_task, &blob_sizes.lbs_task);
 }
 
 /**
@@ -294,6 +296,29 @@ int lsm_file_alloc(struct file *file)
 	return 0;
 }
 
+/**
+ * lsm_task_alloc - allocate a composite task blob
+ * @task: the task that needs a blob
+ *
+ * Allocate the task blob for all the modules
+ *
+ * Returns 0, or -ENOMEM if memory can't be allocated.
+ */
+int lsm_task_alloc(struct task_struct *task)
+{
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	if (task->security)
+		pr_info("%s: Inbound task blob is not NULL.\n", __func__);
+#endif
+	if (blob_sizes.lbs_task == 0)
+		return 0;
+
+	task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
+	if (task->security == NULL)
+		return -ENOMEM;
+	return 0;
+}
+
 /*
  * Hook list operation macros.
  *
@@ -1102,6 +1127,9 @@ int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
 void security_task_free(struct task_struct *task)
 {
 	call_void_hook(task_free, task);
+
+	kfree(task->security);
+	task->security = NULL;
 }
 
 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#9 lore raw

PATCH 5/11 LSM: Infrastructure management of the remaining blobs

In reply to Casey Schaufler

Subject: [PATCH 05/11] LSM: Infrastructure management of the remaining blobs

Move management of the inode, ipc, key, msg_msg, sock and superblock security blobs from the security modules to the infrastructure. Use of the blob pointers is abstracted in the security modules.

---
 include/linux/lsm_hooks.h         |   8 +
 security/security.c               | 259 +++++++++++++++++++++++++++-
 security/selinux/hooks.c          | 333 ++++++++++++------------------------
 security/selinux/include/objsec.h |  65 +++++++-
 security/selinux/netlabel.c       |  15 +-
 security/selinux/selinuxfs.c      |   4 +-
 security/selinux/ss/services.c    |   3 +-
 security/smack/smack.h            |  61 ++++++-
 security/smack/smack_lsm.c        | 343 +++++++++++---------------------------
 security/smack/smack_netfilter.c  |   8 +-
 10 files changed, 599 insertions(+), 500 deletions(-)
include/linux/lsm_hooks.h +8 -0
index 8d6e757e78dc..36e848e51782 100644
@@ -1924,6 +1924,12 @@ struct security_hook_list {
 struct lsm_blob_sizes {
 	int	lbs_cred;
 	int	lbs_file;
+	int	lbs_inode;
+	int	lbs_ipc;
+	int	lbs_key;
+	int	lbs_msg_msg;
+	int	lbs_sock;
+	int	lbs_superblock;
 	int	lbs_task;
 };
 
@@ -1987,9 +1993,11 @@ static inline void loadpin_add_hooks(void) { };
 #endif
 
 extern int lsm_cred_alloc(struct cred *cred, gfp_t gfp);
+extern int lsm_inode_alloc(struct inode *inode);
 
 #ifdef CONFIG_SECURITY
 void lsm_early_cred(struct cred *cred);
+void lsm_early_inode(struct inode *inode);
 #endif
 
 #endif /* ! __LINUX_LSM_HOOKS_H */
security/security.c +257 -2

381 lines not on this page · raw message

security/selinux/hooks.c +104 -229

874 lines not on this page · raw message

security/selinux/include/objsec.h +61 -4

75 lines not on this page · raw message

security/selinux/netlabel.c +8 -7
index aaba6677ee2e..0b0091c04688 100644
@@ -32,6 +32,7 @@
 #include <linux/gfp.h>
 #include <linux/ip.h>
 #include <linux/ipv6.h>
+#include <linux/lsm_hooks.h>
 #include <net/sock.h>
 #include <net/netlabel.h>
 #include <net/ip.h>
@@ -82,7 +83,7 @@ static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
 static struct netlbl_lsm_secattr *selinux_netlbl_sock_genattr(struct sock *sk)
 {
 	int rc;
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 	struct netlbl_lsm_secattr *secattr;
 
 	if (sksec->nlbl_secattr != NULL)
@@ -114,7 +115,7 @@ static struct netlbl_lsm_secattr *selinux_netlbl_sock_getattr(
 							const struct sock *sk,
 							u32 sid)
 {
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 	struct netlbl_lsm_secattr *secattr = sksec->nlbl_secattr;
 
 	if (secattr == NULL)
@@ -249,7 +250,7 @@ int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
 	 * being labeled by it's parent socket, if it is just exit */
 	sk = skb_to_full_sk(skb);
 	if (sk != NULL) {
-		struct sk_security_struct *sksec = sk->sk_security;
+		struct sk_security_struct *sksec = selinux_sock(sk);
 		if (sksec->nlbl_state != NLBL_REQSKB)
 			return 0;
 		secattr = selinux_netlbl_sock_getattr(sk, sid);
@@ -311,7 +312,7 @@ int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
  */
 void selinux_netlbl_inet_csk_clone(struct sock *sk, u16 family)
 {
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 
 	if (family == PF_INET)
 		sksec->nlbl_state = NLBL_LABELED;
@@ -332,7 +333,7 @@ void selinux_netlbl_inet_csk_clone(struct sock *sk, u16 family)
 int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)
 {
 	int rc;
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 	struct netlbl_lsm_secattr *secattr;
 
 	if (family != PF_INET && family != PF_INET6)
@@ -446,7 +447,7 @@ int selinux_netlbl_socket_setsockopt(struct socket *sock,
 {
 	int rc = 0;
 	struct sock *sk = sock->sk;
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 	struct netlbl_lsm_secattr secattr;
 
 	if (selinux_netlbl_option(level, optname) &&
@@ -482,7 +483,7 @@ int selinux_netlbl_socket_setsockopt(struct socket *sock,
 int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr)
 {
 	int rc;
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct sk_security_struct *sksec = selinux_sock(sk);
 	struct netlbl_lsm_secattr *secattr;
 
 	if (sksec->nlbl_state != NLBL_REQSKB &&
security/selinux/selinuxfs.c +2 -2
index 855a13053a81..1b4bed79101e 100644
@@ -1300,7 +1300,7 @@ static int sel_make_bools(void)
 		if (len >= PAGE_SIZE)
 			goto out;
 
-		isec = (struct inode_security_struct *)inode->i_security;
+		isec = (struct inode_security_struct *)selinux_inode(inode);
 		ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
 		if (ret) {
 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
@@ -1841,7 +1841,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
 		goto err;
 
 	inode->i_ino = ++sel_last_ino;
-	isec = (struct inode_security_struct *)inode->i_security;
+	isec = (struct inode_security_struct *)selinux_inode(inode);
 	isec->sid = SECINITSID_DEVNULL;
 	isec->sclass = SECCLASS_CHR_FILE;
 	isec->initialized = LABEL_INITIALIZED;
security/selinux/ss/services.c +2 -1
index 2f02fa67ec2e..4558a23d160e 100644
@@ -52,6 +52,7 @@
 #include <linux/selinux.h>
 #include <linux/flex_array.h>
 #include <linux/vmalloc.h>
+#include <linux/lsm_hooks.h>
 #include <net/netlabel.h>
 
 #include "flask.h"
@@ -2654,7 +2655,7 @@ int security_fs_use(struct super_block *sb)
 {
 	int rc = 0;
 	struct ocontext *c;
-	struct superblock_security_struct *sbsec = sb->s_security;
+	struct superblock_security_struct *sbsec = selinux_superblock(sb);
 	const char *fstype = sb->s_type->name;
 
 	read_lock(&policy_rwlock);
security/smack/smack.h +59 -2

78 lines not on this page · raw message

security/smack/smack_lsm.c +94 -249

808 lines not on this page · raw message

security/smack/smack_netfilter.c +4 -4
index cdeb0f3243dd..a5155295551f 100644
@@ -31,8 +31,8 @@ static unsigned int smack_ipv6_output(void *priv,
 	struct socket_smack *ssp;
 	struct smack_known *skp;
 
-	if (sk && sk->sk_security) {
-		ssp = sk->sk_security;
+	if (sk && smack_sock(sk)) {
+		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
 		skb->secmark = skp->smk_secid;
 	}
@@ -49,8 +49,8 @@ static unsigned int smack_ipv4_output(void *priv,
 	struct socket_smack *ssp;
 	struct smack_known *skp;
 
-	if (sk && sk->sk_security) {
-		ssp = sk->sk_security;
+	if (sk && smack_sock(sk)) {
+		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
 		skb->secmark = skp->smk_secid;
 	}
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#11 lore raw

PATCH 6/11 LSM: general but not extreme module stacking

In reply to Casey Schaufler

Subject: [PATCH 06/11] LSM: general but not extreme module stacking

Leverage the infrastructure management of the credential and file security blobs to allow stacking of security modules in all but the most extreme case. Security modules are informed of the location of their data within the blobs at module initialization.

Stacking is optional. If stacking is not configured the old limit of one "major" security module applies. If stacking is configured any combination that does not include both SELinux and Smack is allowed.

A subdirectory has been added to /proc/.../attr for each of SELinux and AppArmor (Smack introduced such a subdirectory earlier) to disambiguate what data is provided in the proc/.../attr interfaces. An entry "context" is added to /proc/.../attr and to each of the subdirectories. The "context" entry provides process attribute information in the form:

        lsm-name='lsm-data'[,lsm-name='lsm-data']...
---
 Documentation/admin-guide/LSM/index.rst |  22 ++++-
 fs/proc/base.c                          |  33 ++++++++
 include/linux/lsm_hooks.h               |   2 +-
 security/Kconfig                        |  94 +++++++++++++++++++++
 security/apparmor/include/context.h     |  10 +++
 security/apparmor/lsm.c                 |  44 ++++++++--
 security/security.c                     | 142 +++++++++++++++++++++++++++++++-
 security/selinux/hooks.c                |  24 +++++-
 security/selinux/include/objsec.h       |   8 ++
 security/smack/smack.h                  |   9 ++
 security/smack/smack_lsm.c              |  23 +++---
 security/tomoyo/common.h                |  12 ++-
 security/tomoyo/tomoyo.c                |   3 +-
 13 files changed, 398 insertions(+), 28 deletions(-)
Documentation/admin-guide/LSM/index.rst +18 -4
index 9842e21afd4a..bb5c682c046a 100644
@@ -17,10 +17,16 @@ MAC extensions, other extensions can be built using the LSM to provide
 specific changes to system operation when these tweaks are not available
 in the core functionality of Linux itself.
 
-The Linux capabilities modules will always be included. This may be
-followed by any number of "minor" modules and at most one "major" module.
-For more details on capabilities, see ``capabilities(7)`` in the Linux
-man-pages project.
+The Linux capabilities modules will always be included. For more details
+on capabilities, see ``capabilities(7)`` in the Linux man-pages project.
+
+Security modules that do not use the security data blobs maintained
+by the LSM infrastructure are considered "minor" modules. These may be
+included at compile time and stacked explicitly. Security modules that
+use the LSM maintained security blobs are considered "major" modules.
+These may only be stacked if the CONFIG_LSM_STACKED configuration
+option is used. If this is chosen all of the security modules selected
+will be used.
 
 A list of the active security modules can be found by reading
 ``/sys/kernel/security/lsm``. This is a comma separated list, and
@@ -37,6 +43,14 @@ security module and contains all its special files. The files directly
 in ``/proc/.../attr`` remain as legacy interfaces for modules that provide
 subdirectories.
 
+The files named "context" in the attr directories contain the
+same information as the "current" files, but formatted to
+identify the module it comes from.
+
+if selinux is the active security module:
+	/proc/self/attr/context could contain selinux='unconfined_t'
+	/proc/self/attr/selinux/context could contain selinux='unconfined_t'
+
 .. toctree::
    :maxdepth: 1
 
fs/proc/base.c +33 -0
index 6a25a4b1592a..76f24175b8a5 100644
@@ -2592,13 +2592,37 @@ static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
 	.setattr	= proc_setattr, \
 }
 
+#ifdef CONFIG_SECURITY_SELINUX
+static const struct pid_entry selinux_attr_dir_stuff[] = {
+	ATTR("selinux", "current",	0666),
+	ATTR("selinux", "prev",		0444),
+	ATTR("selinux", "exec",		0666),
+	ATTR("selinux", "fscreate",	0666),
+	ATTR("selinux", "keycreate",	0666),
+	ATTR("selinux", "sockcreate",	0666),
+	ATTR("selinux", "context",	0666),
+};
+LSM_DIR_OPS(selinux);
+#endif
+
 #ifdef CONFIG_SECURITY_SMACK
 static const struct pid_entry smack_attr_dir_stuff[] = {
 	ATTR("smack", "current",	0666),
+	ATTR("smack", "context",	0666),
 };
 LSM_DIR_OPS(smack);
 #endif
 
+#ifdef CONFIG_SECURITY_APPARMOR
+static const struct pid_entry apparmor_attr_dir_stuff[] = {
+	ATTR("apparmor", "current",	0666),
+	ATTR("apparmor", "prev",	0444),
+	ATTR("apparmor", "exec",	0666),
+	ATTR("apparmor", "context",	0666),
+};
+LSM_DIR_OPS(apparmor);
+#endif
+
 static const struct pid_entry attr_dir_stuff[] = {
 	ATTR(NULL, "current",		0666),
 	ATTR(NULL, "prev",		0444),
@@ -2606,10 +2630,19 @@ static const struct pid_entry attr_dir_stuff[] = {
 	ATTR(NULL, "fscreate",		0666),
 	ATTR(NULL, "keycreate",		0666),
 	ATTR(NULL, "sockcreate",	0666),
+	ATTR(NULL, "context",		0666),
+#ifdef CONFIG_SECURITY_SELINUX
+	DIR("selinux",			0555,
+	    proc_selinux_attr_dir_inode_ops, proc_selinux_attr_dir_ops),
+#endif
 #ifdef CONFIG_SECURITY_SMACK
 	DIR("smack",			0555,
 	    proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
 #endif
+#ifdef CONFIG_SECURITY_APPARMOR
+	DIR("apparmor",			0555,
+	    proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
+#endif
 };
 
 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
include/linux/lsm_hooks.h +1 -1
index 36e848e51782..dfe4dab1ff8d 100644
@@ -1979,7 +1979,7 @@ static inline void security_delete_hooks(struct security_hook_list *hooks,
 #define __lsm_ro_after_init	__ro_after_init
 #endif /* CONFIG_SECURITY_WRITABLE_HOOKS */
 
-extern int __init security_module_enable(const char *module);
+extern bool __init security_module_enable(const char *lsm, const bool stacked);
 extern void __init capability_add_hooks(void);
 #ifdef CONFIG_SECURITY_YAMA
 extern void __init yama_add_hooks(void);
security/Kconfig +94 -0

109 lines not on this page · raw message

security/apparmor/include/context.h +10 -0
index c6e106a533e8..c6d5dbbd18b0 100644
@@ -55,9 +55,15 @@ int aa_set_current_hat(struct aa_label *label, u64 token);
 int aa_restore_previous_label(u64 cookie);
 struct aa_label *aa_get_task_label(struct task_struct *task);
 
+extern struct lsm_blob_sizes apparmor_blob_sizes;
+
 static inline struct aa_task_ctx *apparmor_cred(const struct cred *cred)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return cred->security + apparmor_blob_sizes.lbs_cred;
+#else
 	return cred->security;
+#endif
 }
 
 /**
@@ -89,7 +95,11 @@ static inline struct aa_label *aa_get_newest_cred_label(const struct cred *cred)
 
 static inline struct aa_file_ctx *apparmor_file(const struct file *file)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return file->f_security + apparmor_blob_sizes.lbs_file;
+#else
 	return file->f_security;
+#endif
 }
 
 /**
security/apparmor/lsm.c +38 -6

82 lines not on this page · raw message

security/security.c +140 -2

182 lines not on this page · raw message

security/selinux/hooks.c +21 -3
index 4c022182bb21..84d533335924 100644
@@ -5733,6 +5733,8 @@ static int selinux_getprocattr(struct task_struct *p,
 
 	if (!strcmp(name, "current"))
 		sid = __tsec->sid;
+	else if (!strcmp(name, "context"))
+		sid = __tsec->sid;
 	else if (!strcmp(name, "prev"))
 		sid = __tsec->osid;
 	else if (!strcmp(name, "exec"))
@@ -5752,7 +5754,19 @@ static int selinux_getprocattr(struct task_struct *p,
 	if (!sid)
 		return 0;
 
-	error = security_sid_to_context(sid, value, &len);
+	if (strcmp(name, "context")) {
+		error = security_sid_to_context(sid, value, &len);
+	} else {
+		char *vp;
+
+		error = security_sid_to_context(sid, &vp, &len);
+		if (!error) {
+			*value = kasprintf(GFP_KERNEL, "selinux='%s'", vp);
+			if (*value == NULL)
+				error = -ENOMEM;
+		}
+	}
+
 	if (error)
 		return error;
 	return len;
@@ -5788,6 +5802,9 @@ static int selinux_setprocattr(const char *name, void *value, size_t size)
 	else if (!strcmp(name, "current"))
 		error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
 				     PROCESS__SETCURRENT, NULL);
+	else if (!strcmp(name, "context"))
+		error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
+				     PROCESS__SETCURRENT, NULL);
 	else
 		error = -EINVAL;
 	if (error)
@@ -5848,7 +5865,7 @@ static int selinux_setprocattr(const char *name, void *value, size_t size)
 		tsec->keycreate_sid = sid;
 	} else if (!strcmp(name, "sockcreate")) {
 		tsec->sockcreate_sid = sid;
-	} else if (!strcmp(name, "current")) {
+	} else if (!strcmp(name, "current") || !strcmp(name, "context")) {
 		error = -EINVAL;
 		if (sid == 0)
 			goto abort_change;
@@ -6294,7 +6311,8 @@ static __init int selinux_init(void)
 {
 	static int finish;
 
-	if (!security_module_enable("selinux")) {
+	if (!security_module_enable("selinux",
+				IS_ENABLED(CONFIG_SECURITY_SELINUX_STACKED))) {
 		selinux_enabled = 0;
 		return 0;
 	}
security/selinux/include/objsec.h +8 -0
index 11a372c0e919..306863675614 100644
@@ -155,12 +155,20 @@ extern struct lsm_blob_sizes selinux_blob_sizes;
 
 static inline struct task_security_struct *selinux_cred(const struct cred *cred)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return cred->security + selinux_blob_sizes.lbs_cred;
+#else
 	return cred->security;
+#endif
 }
 
 static inline struct file_security_struct *selinux_file(const struct file *file)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return file->f_security + selinux_blob_sizes.lbs_file;
+#else
 	return file->f_security;
+#endif
 }
 
 static inline struct inode_security_struct *selinux_inode(
security/smack/smack.h +9 -0
index 1b875c2f3d9d..e7611de071f1 100644
@@ -336,6 +336,7 @@ extern struct smack_known *smack_syslog_label;
 extern struct smack_known *smack_unconfined;
 #endif
 extern int smack_ptrace_rule;
+extern struct lsm_blob_sizes smack_blob_sizes;
 
 extern struct smack_known smack_known_floor;
 extern struct smack_known smack_known_hat;
@@ -358,12 +359,20 @@ extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
 
 static inline struct task_smack *smack_cred(const struct cred *cred)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return cred->security + smack_blob_sizes.lbs_cred;
+#else
 	return cred->security;
+#endif
 }
 
 static inline struct smack_known **smack_file(const struct file *file)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return file->f_security + smack_blob_sizes.lbs_file;
+#else
 	return file->f_security;
+#endif
 }
 
 static inline struct inode_smack *smack_inode(const struct inode *inode)
security/smack/smack_lsm.c +13 -10
index bbc27d444662..1e9ab7bdaf55 100644
@@ -3453,18 +3453,20 @@ static int smack_getprocattr(struct task_struct *p, char *name, char **value)
 {
 	struct smack_known *skp = smk_of_task_struct(p);
 	char *cp;
-	int slen;
 
-	if (strcmp(name, "current") != 0)
+	if (strcmp(name, "current") == 0) {
+		cp = kstrdup(skp->smk_known, GFP_KERNEL);
+		if (cp == NULL)
+			return -ENOMEM;
+	} else if (strcmp(name, "context") == 0) {
+		cp = kasprintf(GFP_KERNEL, "smack='%s'", skp->smk_known);
+		if (cp == NULL)
+			return -ENOMEM;
+	} else
 		return -EINVAL;
 
-	cp = kstrdup(skp->smk_known, GFP_KERNEL);
-	if (cp == NULL)
-		return -ENOMEM;
-
-	slen = strlen(cp);
 	*value = cp;
-	return slen;
+	return strlen(cp);
 }
 
 /**
@@ -3492,7 +3494,7 @@ static int smack_setprocattr(const char *name, void *value, size_t size)
 	if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
 		return -EINVAL;
 
-	if (strcmp(name, "current") != 0)
+	if (strcmp(name, "current") != 0 && strcmp(name, "context") != 0)
 		return -EINVAL;
 
 	skp = smk_import_entry(value, size);
@@ -4622,7 +4624,8 @@ static __init int smack_init(void)
 	struct cred *cred = (struct cred *) current->cred;
 	struct task_smack *tsp;
 
-	if (!security_module_enable("smack"))
+	if (!security_module_enable("smack",
+				IS_ENABLED(CONFIG_SECURITY_SMACK_STACKED)))
 		return 0;
 
 	if (!finish) {
security/tomoyo/common.h +11 -1
index cbcfccc84784..2eed9d44eec1 100644
@@ -1085,6 +1085,7 @@ extern struct tomoyo_domain_info tomoyo_kernel_domain;
 extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
 extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
 extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
+extern struct lsm_blob_sizes tomoyo_blob_sizes;
 
 /********** Inlined functions. **********/
 
@@ -1204,7 +1205,11 @@ static inline void tomoyo_put_group(struct tomoyo_group *group)
  */
 static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred)
 {
+#ifdef CONFIG_SECURITY_STACKING
+	return cred->security + tomoyo_blob_sizes.lbs_cred;
+#else
 	return cred->security;
+#endif
 }
 
 /**
@@ -1214,8 +1219,13 @@ static inline struct tomoyo_domain_info **tomoyo_cred(const struct cred *cred)
  */
 static inline struct tomoyo_domain_info *tomoyo_domain(void)
 {
-	struct tomoyo_domain_info **blob = tomoyo_cred(current_cred());
+	const struct cred *cred = current_cred();
+	struct tomoyo_domain_info **blob;
+
+	if (cred->security == NULL)
+		return NULL;
 
+	blob = tomoyo_cred(cred);
 	return *blob;
 }
 
security/tomoyo/tomoyo.c +2 -1
index 901fa7835337..aa52fff4bbe9 100644
@@ -561,7 +561,8 @@ static int __init tomoyo_init(void)
 	struct cred *cred = (struct cred *) current_cred();
 	struct tomoyo_domain_info **blob;
 
-	if (!security_module_enable("tomoyo"))
+	if (!security_module_enable("tomoyo",
+				IS_ENABLED(CONFIG_SECURITY_TOMOYO_STACKED)))
 		return 0;
 
 	if (!finish) {
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#13 lore raw

PATCH 7/11 LSM: Shared secids by token

In reply to Casey Schaufler

Subject: [PATCH 07/11] LSM: Shared secids by token

Introduces a mechanism for mapping a set of security module secids to and from a "token". The module interfaces are changed to generally hide the mechanism from both the security modules and the callers of the security hooks.

---
 include/linux/lsm_hooks.h        |  54 ++++++++-
 security/Makefile                |   1 +
 security/security.c              | 248 ++++++++++++++++++++++++++++++++++-----
 security/selinux/hooks.c         |  31 +++--
 security/selinux/include/xfrm.h  |   2 +-
 security/selinux/xfrm.c          |   6 +-
 security/smack/smack.h           |   1 +
 security/smack/smack_lsm.c       |  19 ++-
 security/smack/smack_netfilter.c |  17 ++-
 security/stacking.c              | 165 ++++++++++++++++++++++++++
 10 files changed, 497 insertions(+), 47 deletions(-)
 create mode 100644 security/stacking.c
include/linux/lsm_hooks.h +52 -2
index dfe4dab1ff8d..75d95854f2ed 100644
@@ -1627,7 +1627,7 @@ union security_list_options {
 	void (*secmark_refcount_inc)(void);
 	void (*secmark_refcount_dec)(void);
 	void (*req_classify_flow)(const struct request_sock *req,
-					struct flowi *fl);
+					u32 *fl_secid);
 	int (*tun_dev_alloc_security)(void **security);
 	void (*tun_dev_free_security)(void *security);
 	int (*tun_dev_create)(void);
@@ -1663,7 +1663,7 @@ union security_list_options {
 					u8 dir);
 	int (*xfrm_state_pol_flow_match)(struct xfrm_state *x,
 						struct xfrm_policy *xp,
-						const struct flowi *fl);
+						u32 fl_secid);
 	int (*xfrm_decode_session)(struct sk_buff *skb, u32 *secid, int ckall);
 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
 
@@ -1916,9 +1916,59 @@ struct security_hook_list {
 	struct list_head		*head;
 	union security_list_options	hook;
 	char				*lsm;
+	int				lsm_index;
 } __randomize_layout;
 
 /*
+ * The maximum number of major security modules.
+ * Used to avoid excessive memory management while
+ * mapping global and module specific secids.
+ *
+ * Currently SELinux, Smack, AppArmor, TOMOYO
+ * Oh, but Casey needs to come up with the right way
+ * to identify a "major" module, so use the total number
+ * of modules (including minor) for now.
+ * Minor: Capability, Yama, LoadPin
+ */
+#define	LSM_MAX_MAJOR	8
+
+#ifdef CONFIG_SECURITY_STACKING
+struct lsm_secids {
+	u32	secid[LSM_MAX_MAJOR];
+};
+
+extern u32 lsm_secids_to_token(const struct lsm_secids *secids);
+extern void lsm_token_to_secids(const u32 token, struct lsm_secids *secids);
+extern u32 lsm_token_to_module_secid(const u32 token, int lsm);
+extern void lsm_secids_init(struct lsm_secids *secids);
+#else /* CONFIG_SECURITY_STACKING */
+struct lsm_secids {
+	u32	secid;
+};
+
+static inline u32 lsm_secids_to_token(const struct lsm_secids *secids)
+{
+	return secids->secid;
+}
+
+static inline void lsm_token_to_secids(const u32 token,
+				       struct lsm_secids *secids)
+{
+	secids->secid = token;
+}
+
+static inline u32 lsm_token_to_module_secid(const u32 token, int lsm)
+{
+	return token;
+}
+
+static inline void lsm_secids_init(struct lsm_secids *secids)
+{
+	secids->secid = 0;
+}
+#endif /* CONFIG_SECURITY_STACKING */
+
+/*
  * Security blob size or offset data.
  */
 struct lsm_blob_sizes {
security/Makefile +1 -0
index f2d71cdb8e19..05e6d525b5a1 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_SECURITY_APPARMOR)		+= apparmor/
 obj-$(CONFIG_SECURITY_YAMA)		+= yama/
 obj-$(CONFIG_SECURITY_LOADPIN)		+= loadpin/
 obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
+obj-$(CONFIG_SECURITY_STACKING)		+= stacking.o
 
 # Object integrity file lists
 subdir-$(CONFIG_INTEGRITY)		+= integrity
security/security.c +221 -27

369 lines not on this page · raw message

security/selinux/hooks.c +24 -7
index 84d533335924..389f09ebd374 100644
@@ -100,6 +100,9 @@
 /* SECMARK reference count */
 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
 
+/* Index into lsm_secids */
+static int selinux_secids_index;
+
 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
 int selinux_enforcing;
 
@@ -4610,6 +4613,11 @@ static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
 			    SECCLASS_NODE, NODE__RECVFROM, ad);
 }
 
+static u32 selinux_secmark_to_secid(u32 token)
+{
+	return lsm_token_to_module_secid(token, selinux_secids_index);
+}
+
 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
 				       u16 family)
 {
@@ -4629,7 +4637,9 @@ static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
 		return err;
 
 	if (selinux_secmark_enabled()) {
-		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
+		err = avc_has_perm(sk_sid,
+				   selinux_secmark_to_secid(skb->secmark),
+				   SECCLASS_PACKET,
 				   PACKET__RECV, &ad);
 		if (err)
 			return err;
@@ -4703,7 +4713,9 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 	}
 
 	if (secmark_active) {
-		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
+		err = avc_has_perm(sk_sid,
+				   selinux_secmark_to_secid(skb->secmark),
+				   SECCLASS_PACKET,
 				   PACKET__RECV, &ad);
 		if (err)
 			return err;
@@ -4902,9 +4914,9 @@ static void selinux_secmark_refcount_dec(void)
 }
 
 static void selinux_req_classify_flow(const struct request_sock *req,
-				      struct flowi *fl)
+				      u32 *fl_secid)
 {
-	fl->flowi_secid = req->secid;
+	*fl_secid = req->secid;
 }
 
 static int selinux_tun_dev_alloc_security(void **security)
@@ -5066,7 +5078,8 @@ static unsigned int selinux_ip_forward(struct sk_buff *skb,
 	}
 
 	if (secmark_active)
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				 selinux_secmark_to_secid(skb->secmark),
 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
 			return NF_DROP;
 
@@ -5178,7 +5191,8 @@ static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
 		return NF_DROP;
 
 	if (selinux_secmark_enabled())
-		if (avc_has_perm(sksec->sid, skb->secmark,
+		if (avc_has_perm(sksec->sid,
+				 selinux_secmark_to_secid(skb->secmark),
 				 SECCLASS_PACKET, PACKET__SEND, &ad))
 			return NF_DROP_ERR(-ECONNREFUSED);
 
@@ -5301,7 +5315,8 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb,
 		return NF_DROP;
 
 	if (secmark_active)
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				 selinux_secmark_to_secid(skb->secmark),
 				 SECCLASS_PACKET, secmark_perm, &ad))
 			return NF_DROP_ERR(-ECONNREFUSED);
 
@@ -6339,6 +6354,8 @@ static __init int selinux_init(void)
 
 	security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
 
+	selinux_secids_index = selinux_hooks[0].lsm_index;
+
 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
security/selinux/include/xfrm.h +1 -1
index 1450f85b946d..475a328248b3 100644
@@ -25,7 +25,7 @@ int selinux_xfrm_state_delete(struct xfrm_state *x);
 int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
 int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
 				      struct xfrm_policy *xp,
-				      const struct flowi *fl);
+				      u32 fl_secid);
 
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
 extern atomic_t selinux_xfrm_refcount;
security/selinux/xfrm.c +3 -3
index 789d07bd900f..d71e2c32b5da 100644
@@ -174,7 +174,7 @@ int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
  */
 int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
 				      struct xfrm_policy *xp,
-				      const struct flowi *fl)
+				      u32 fl_secid)
 {
 	u32 state_sid;
 
@@ -196,13 +196,13 @@ int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
 
 	state_sid = x->security->ctx_sid;
 
-	if (fl->flowi_secid != state_sid)
+	if (fl_secid != state_sid)
 		return 0;
 
 	/* We don't need a separate SA Vs. policy polmatch check since the SA
 	 * is now of the same label as the flow and a flow Vs. policy polmatch
 	 * check had already happened in selinux_xfrm_policy_lookup() above. */
-	return (avc_has_perm(fl->flowi_secid, state_sid,
+	return (avc_has_perm(fl_secid, state_sid,
 			    SECCLASS_ASSOCIATION, ASSOCIATION__SENDTO,
 			    NULL) ? 0 : 1);
 }
security/smack/smack.h +1 -0
index e7611de071f1..e9fd586e0ec1 100644
@@ -328,6 +328,7 @@ void smk_destroy_label_list(struct list_head *list);
  * Shared data.
  */
 extern int smack_enabled;
+extern int smack_secids_index;
 extern int smack_cipso_direct;
 extern int smack_cipso_mapped;
 extern struct smack_known *smack_net_ambient;
security/smack/smack_lsm.c +14 -5
index 1e9ab7bdaf55..51daf9b05f17 100644
@@ -57,6 +57,7 @@ static LIST_HEAD(smk_ipv6_port_list);
 #endif
 static struct kmem_cache *smack_inode_cache;
 int smack_enabled;
+int smack_secids_index;
 
 static const match_table_t smk_mount_tokens = {
 	{Opt_fsdefault, SMK_FSDEFAULT "%s"},
@@ -3788,6 +3789,13 @@ static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
 }
 #endif /* CONFIG_IPV6 */
 
+#ifdef CONFIG_SECURITY_SMACK_NETFILTER
+static u32 smk_of_secmark(u32 secmark)
+{
+	return lsm_token_to_module_secid(secmark, smack_secids_index);
+}
+#endif
+
 /**
  * smack_socket_sock_rcv_skb - Smack packet delivery access check
  * @sk: socket
@@ -3819,7 +3827,7 @@ static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 		 * The secmark is assumed to reflect policy better.
 		 */
 		if (skb && skb->secmark != 0) {
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb->secmark));
 			goto access_check;
 		}
 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
@@ -3864,7 +3872,7 @@ static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 			break;
 #ifdef SMACK_IPV6_SECMARK_LABELING
 		if (skb && skb->secmark != 0)
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb->secmark));
 		else
 			skp = smack_ipv6host_label(&sadd);
 		if (skp == NULL)
@@ -3962,7 +3970,7 @@ static int smack_socket_getpeersec_dgram(struct socket *sock,
 		break;
 	case PF_INET:
 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);
 		if (s != 0)
 			break;
 #endif
@@ -3981,7 +3989,7 @@ static int smack_socket_getpeersec_dgram(struct socket *sock,
 		break;
 	case PF_INET6:
 #ifdef SMACK_IPV6_SECMARK_LABELING
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);
 #endif
 		break;
 	}
@@ -4060,7 +4068,7 @@ static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
 	 * The secmark is assumed to reflect policy better.
 	 */
 	if (skb && skb->secmark != 0) {
-		skp = smack_from_secid(skb->secmark);
+		skp = smack_from_secid(smk_of_secmark(skb->secmark));
 		goto access_check;
 	}
 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
@@ -4650,6 +4658,7 @@ static __init int smack_init(void)
 	 * Register with LSM
 	 */
 	security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
+	smack_secids_index = smack_hooks[0].lsm_index;
 	smack_enabled = 1;
 
 	pr_info("Smack:  Initializing.\n");
security/smack/smack_netfilter.c +15 -2
index a5155295551f..510661ba6c16 100644
@@ -23,6 +23,19 @@
 
 #if IS_ENABLED(CONFIG_IPV6)
 
+/*
+ * Reinvestigate this soon?
+ *
+ */
+static u32 smack_to_secmark(u32 secid)
+{
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+	secids.secid[smack_secids_index] = secid;
+	return lsm_secids_to_token(&secids);
+}
+
 static unsigned int smack_ipv6_output(void *priv,
 					struct sk_buff *skb,
 					const struct nf_hook_state *state)
@@ -34,7 +47,7 @@ static unsigned int smack_ipv6_output(void *priv,
 	if (sk && smack_sock(sk)) {
 		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 	}
 
 	return NF_ACCEPT;
@@ -52,7 +65,7 @@ static unsigned int smack_ipv4_output(void *priv,
 	if (sk && smack_sock(sk)) {
 		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 	}
 
 	return NF_ACCEPT;
security/stacking.c new file +165 -0

165 lines not on this page · raw message

Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info@ http://vger.kernel.org/majordomo-info.html
author maintainer
#17 lore raw

PATCH 8/11 LSM: Complete abstraction of superblock blob in Smack

In reply to Casey Schaufler

Subject: [PATCH 08/11] LSM: Complete abstraction of superblock blob in Smack

Three cases of the abstraction of the superblock blob where missed in the Smack code. This fixes the omission.

---
 security/smack/smack_lsm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/smack/smack_lsm.c +3 -3
index 51daf9b05f17..3523072b5548 100644
@@ -1166,7 +1166,7 @@ static int smack_inode_rename(struct inode *old_inode,
  */
 static int smack_inode_permission(struct inode *inode, int mask)
 {
-	struct superblock_smack *sbsp = inode->i_sb->s_security;
+	struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
 	struct smk_audit_info ad;
 	int no_block = mask & MAY_NOT_BLOCK;
 	int rc;
@@ -1408,7 +1408,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)
 	 */
 	if (strcmp(name, XATTR_NAME_SMACK) == 0) {
 		struct super_block *sbp = dentry->d_sb;
-		struct superblock_smack *sbsp = sbp->s_security;
+		struct superblock_smack *sbsp = smack_superblock(sbp);
 
 		isp->smk_inode = sbsp->smk_default;
 	} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
@@ -1683,7 +1683,7 @@ static int smack_mmap_file(struct file *file,
 	isp = smack_inode(file_inode(file));
 	if (isp->smk_mmap == NULL)
 		return 0;
-	sbsp = file_inode(file)->i_sb->s_security;
+	sbsp = smack_superblock(file_inode(file)->i_sb);
 	if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
 	    isp->smk_mmap != sbsp->smk_root)
 		return -EACCES;
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#18 lore raw

PATCH 9/11 LSM: Multiple security mount option support

In reply to Casey Schaufler

Subject: [PATCH 09/11] LSM: Multiple security mount option support

There needs to be separate data for each of the
security modules that support mount options.
Expand the security_mnt_opts structure to include
an entry for each security module that uses them.

It would be better to have a variable size blob,
but there isn't a convenient place to hang such.

---
 fs/btrfs/super.c           | 10 +++---
 include/linux/security.h   | 54 +++++++++++++++++++++-------
 security/security.c        | 15 ++++++--
 security/selinux/hooks.c   | 90 +++++++++++++++++++++++-----------------------
 security/smack/smack_lsm.c | 51 +++++++++++++-------------
 5 files changed, 131 insertions(+), 89 deletions(-)
fs/btrfs/super.c +5 -5
index 12540b6104b5..1b54bd0a0806 100644
@@ -1498,15 +1498,15 @@ static int setup_security_options(struct btrfs_fs_info *fs_info,
 		return ret;
 
 #ifdef CONFIG_SECURITY
-	if (!fs_info->security_opts.num_mnt_opts) {
+	if (fs_info->security_opts.selinux.num_mnt_opts != 0 ||
+	    fs_info->security_opts.smack.num_mnt_opts != 0) {
 		/* first time security setup, copy sec_opts to fs_info */
 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
 	} else {
 		/*
-		 * Since SELinux (the only one supporting security_mnt_opts)
-		 * does NOT support changing context during remount/mount of
-		 * the same sb, this must be the same or part of the same
-		 * security options, just free it.
+		 * Since no modules support changing context during
+		 * remount/mount of the same sb, this must be the same
+		 * or part of the same security options, just free it.
 		 */
 		security_free_mnt_opts(sec_opts);
 	}
include/linux/security.h +42 -12

76 lines not on this page · raw message

security/security.c +12 -3
index 9d402d954cef..6ebcc89004ef 100644
@@ -770,9 +770,18 @@ int security_sb_set_mnt_opts(struct super_block *sb,
 				unsigned long kern_flags,
 				unsigned long *set_kern_flags)
 {
-	return call_int_hook(sb_set_mnt_opts,
-				opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
-				opts, kern_flags, set_kern_flags);
+	int nobody = 0;
+
+#ifdef SECURITY_EXTREME_STACKING
+	if (opts->selinux.num_mnt_opts != 0 || opts->smack.num_mnt_opts != 0)
+		nobody = -EOPNOTSUPP;
+#else
+	if (opts->selinux.num_mnt_opts != 0)
+		nobody = -EOPNOTSUPP;
+#endif
+
+	return call_int_hook(sb_set_mnt_opts, nobody, sb, opts, kern_flags,
+				set_kern_flags);
 }
 EXPORT_SYMBOL(security_sb_set_mnt_opts);
 
security/selinux/hooks.c +45 -45

192 lines not on this page · raw message

security/smack/smack_lsm.c +27 -24

111 lines not on this page · raw message

Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#19 lore raw

PATCH 10/11 LSM: Complete task_alloc hook

In reply to Casey Schaufler

Subject: [PATCH 10/11] LSM: Complete task_alloc hook

The Task alloc hook needs to allocate the data.

---
 security/security.c | 4 ++++
 1 file changed, 4 insertions(+)
security/security.c +4 -0
index 6ebcc89004ef..a66663ac932b 100644
@@ -1387,6 +1387,10 @@ int security_file_open(struct file *file, const struct cred *cred)
 
 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
 {
+	int rc = lsm_task_alloc(task);
+
+	if (rc)
+		return rc;
 	return call_int_hook(task_alloc, 0, task, clone_flags);
 }
 
Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#20 lore raw

PATCH 11/11 LSM: Allow stacking of all existing security

In reply to Casey Schaufler

Subject: [PATCH 11/11] LSM: Allow stacking of all existing security

Allow any combination of existing security modules, including those using secids and security marked networking.

The interfaces used by filesystems to maintain security attributes:
security_inode_setsecctx security_inode_getsecctx security_inode_notifysecctx
have been trained to keep a full set of attributes using the "lsm1='data1',lsm2='data2'" format.

A prtcl interface has been added to identify which security module should be invoked when secids are translated to secctx and back. If none is specified the first module will be used. This eliminates the ambiguity of what data will be seen in user-space at the cost of requiring user-space code to be explicit about what it wants to see.

Issues remain with the use of netlabel, as SELinux and Smack use the interfaces differently.

Audit has not been fully tested, and may not always be providing the correct security module information.

---
 fs/xattr.c                          |   6 +-
 include/linux/lsm_audit.h           |   4 +
 include/linux/lsm_hooks.h           |  10 +-
 include/net/request_sock.h          |   2 +
 include/uapi/linux/prctl.h          |   6 +
 kernel/fork.c                       |   3 +
 net/netlabel/netlabel_unlabeled.c   |   2 +-
 security/Kconfig                    |  41 +--
 security/security.c                 | 658 ++++++++++++++++++++++++++++++------
 security/selinux/hooks.c            |  41 +--
 security/selinux/include/objsec.h   |  13 +
 security/selinux/include/security.h |   3 +-
 security/selinux/netlabel.c         |   5 +-
 security/selinux/ss/services.c      |   4 +-
 security/smack/smack.h              |  10 +
 security/smack/smack_lsm.c          |  79 ++---
 security/smack/smack_netfilter.c    |  17 +-
 security/smack/smackfs.c            |   3 +-
 security/stacking.c                 |  63 +++-
 19 files changed, 733 insertions(+), 237 deletions(-)
fs/xattr.c +5 -1
index 464c94bf65f9..01983733651c 100644
@@ -249,7 +249,11 @@ xattr_getsecurity(struct inode *inode, const char *name, void *value,
 	}
 	memcpy(value, buffer, len);
 out:
-	security_release_secctx(buffer, len);
+	/*
+	 * security_inode_getsecurity() does not put a secctx
+	 * into buffer, it puts an allocated string into buffer.
+	 */
+	kfree(buffer);
 out_noalloc:
 	return len;
 }
include/linux/lsm_audit.h +4 -0
index 22b5d4e687ce..44004e477d97 100644
@@ -94,7 +94,11 @@ struct common_audit_data {
 		struct lsm_ibendport_audit *ibendport;
 	} u;
 	/* this union contains LSM specific data */
+#ifdef CONFIG_SECURITY_STACKING
+	struct {
+#else
 	union {
+#endif
 #ifdef CONFIG_SECURITY_SMACK
 		struct smack_audit_data *smack_audit_data;
 #endif
include/linux/lsm_hooks.h +8 -2
index 75d95854f2ed..61460f930adb 100644
@@ -1939,7 +1939,8 @@ struct lsm_secids {
 
 extern u32 lsm_secids_to_token(const struct lsm_secids *secids);
 extern void lsm_token_to_secids(const u32 token, struct lsm_secids *secids);
-extern u32 lsm_token_to_module_secid(const u32 token, int lsm);
+extern u32 lsm_token_get_secid(const u32 token, int lsm);
+extern u32 lsm_token_set_secid(const u32 token, u32 lsecid, int lsm);
 extern void lsm_secids_init(struct lsm_secids *secids);
 #else /* CONFIG_SECURITY_STACKING */
 struct lsm_secids {
@@ -1957,11 +1958,16 @@ static inline void lsm_token_to_secids(const u32 token,
 	secids->secid = token;
 }
 
-static inline u32 lsm_token_to_module_secid(const u32 token, int lsm)
+static inline u32 lsm_token_get_secid(const u32 token, int lsm)
 {
 	return token;
 }
 
+static inline u32 lsm_token_set_secid(const u32 token, u32 lsecid, int lsm)
+{
+	return lsecid;
+}
+
 static inline void lsm_secids_init(struct lsm_secids *secids)
 {
 	secids->secid = 0;
include/net/request_sock.h +2 -0
index 23e22054aa60..07c0e919c4e7 100644
@@ -102,6 +102,8 @@ reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener,
 	sk_tx_queue_clear(req_to_sk(req));
 	req->saved_syn = NULL;
 	refcount_set(&req->rsk_refcnt, 0);
+	req->secid = 0;
+	req->peer_secid = 0;
 
 	return req;
 }
include/uapi/linux/prctl.h +6 -0
index a8d0759a9e40..e7234c5cb7de 100644
@@ -197,4 +197,10 @@ struct prctl_mm_map {
 # define PR_CAP_AMBIENT_LOWER		3
 # define PR_CAP_AMBIENT_CLEAR_ALL	4
 
+/*
+ * Control the LSM specific information reported by
+ * SO_PEERSEC and /proc/.../attr/current
+ */
+#define	PR_GET_DISPLAY_LSM	48
+#define	PR_SET_DISPLAY_LSM	49
 #endif /* _LINUX_PRCTL_H */
kernel/fork.c +3 -0
index 5ff0ebcaafc3..01b90e4f76eb 100644
@@ -1696,6 +1696,9 @@ static __latent_entropy struct task_struct *copy_process(
 	p->sequential_io	= 0;
 	p->sequential_io_avg	= 0;
 #endif
+#ifdef CONFIG_SECURITY
+	p->security = NULL;
+#endif
 
 	/* Perform scheduler related setup. Assign this task to a CPU. */
 	retval = sched_fork(clone_flags, p);
net/netlabel/netlabel_unlabeled.c +1 -1
index 22dc1b9d6362..751cfac79b3e 100644
@@ -899,7 +899,7 @@ static int netlbl_unlabel_staticadd(struct sk_buff *skb,
 
 	/* Don't allow users to add both IPv4 and IPv6 addresses for a
 	 * single entry.  However, allow users to create two entries, one each
-	 * for IPv4 and IPv4, with the same LSM security context which should
+	 * for IPv4 and IPv6, with the same LSM security context which should
 	 * achieve the same result. */
 	if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
 	    !info->attrs[NLBL_UNLABEL_A_IFACE] ||
security/Kconfig +13 -28

55 lines not on this page · raw message

security/security.c +555 -103

1,012 lines not on this page · raw message

security/selinux/hooks.c +21 -20

133 lines not on this page · raw message

security/selinux/include/objsec.h +13 -0
index 306863675614..939db01e86dc 100644
@@ -231,4 +231,17 @@ static inline struct sk_security_struct *selinux_sock(const struct sock *sock)
 #endif
 }
 
+extern int selinux_secids_index;
+
+static inline u32 selinux_token_to_secid(u32 token)
+{
+	return lsm_token_get_secid(token, selinux_secids_index);
+}
+
+static inline u32 selinux_token_from_secid(u32 token, u32 secid)
+{
+	return lsm_token_set_secid(token, secid, selinux_secids_index);
+}
+
+
 #endif /* _SELINUX_OBJSEC_H_ */
security/selinux/include/security.h +1 -2
index e91f08c16c0b..88bcf6a111c6 100644
@@ -160,8 +160,7 @@ int security_member_sid(u32 ssid, u32 tsid,
 int security_change_sid(u32 ssid, u32 tsid,
 	u16 tclass, u32 *out_sid);
 
-int security_sid_to_context(u32 sid, char **scontext,
-	u32 *scontext_len);
+int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len);
 
 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len);
 
security/selinux/netlabel.c +3 -2
index 0b0091c04688..fe556c3ca1b3 100644
@@ -122,7 +122,7 @@ static struct netlbl_lsm_secattr *selinux_netlbl_sock_getattr(
 		return NULL;
 
 	if ((secattr->flags & NETLBL_SECATTR_SECID) &&
-	    (secattr->attr.secid == sid))
+	    (selinux_token_to_secid(secattr->attr.secid) == sid))
 		return secattr;
 
 	return NULL;
@@ -291,7 +291,8 @@ int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
 		return 0;
 
 	netlbl_secattr_init(&secattr);
-	rc = security_netlbl_sid_to_secattr(req->secid, &secattr);
+	rc = security_netlbl_sid_to_secattr(selinux_token_to_secid(req->secid),
+					    &secattr);
 	if (rc != 0)
 		goto inet_conn_request_return;
 	rc = netlbl_req_setattr(req, &secattr);
security/selinux/ss/services.c +2 -2
index 4558a23d160e..303460b8f551 100644
@@ -3445,7 +3445,7 @@ int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
 	if (secattr->flags & NETLBL_SECATTR_CACHE)
 		*sid = *(u32 *)secattr->cache->data;
 	else if (secattr->flags & NETLBL_SECATTR_SECID)
-		*sid = secattr->attr.secid;
+		*sid = selinux_token_to_secid(secattr->attr.secid);
 	else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
 		rc = -EIDRM;
 		ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
@@ -3516,7 +3516,7 @@ int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
 	if (secattr->domain == NULL)
 		goto out;
 
-	secattr->attr.secid = sid;
+	secattr->attr.secid = selinux_token_from_secid(secattr->attr.secid, sid);
 	secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
 	mls_export_netlbl_lvl(ctx, secattr);
 	rc = mls_export_netlbl_cat(ctx, secattr);
security/smack/smack.h +10 -0
index e9fd586e0ec1..7d606e49dea0 100644
@@ -433,6 +433,16 @@ static inline struct smack_known **smack_key(const struct key *key)
 }
 #endif /* CONFIG_KEYS */
 
+static inline u32 smack_from_token(u32 token)
+{
+	return lsm_token_get_secid(token, smack_secids_index);
+}
+
+static inline u32 smack_to_token(u32 token, u32 secid)
+{
+	return lsm_token_set_secid(token, secid, smack_secids_index);
+}
+
 /*
  * Is the directory transmuting?
  */
security/smack/smack_lsm.c +34 -45

153 lines not on this page · raw message

security/smack/smack_netfilter.c +2 -15
index 510661ba6c16..ee5c32c62643 100644
@@ -23,19 +23,6 @@
 
 #if IS_ENABLED(CONFIG_IPV6)
 
-/*
- * Reinvestigate this soon?
- *
- */
-static u32 smack_to_secmark(u32 secid)
-{
-	struct lsm_secids secids;
-
-	lsm_secids_init(&secids);
-	secids.secid[smack_secids_index] = secid;
-	return lsm_secids_to_token(&secids);
-}
-
 static unsigned int smack_ipv6_output(void *priv,
 					struct sk_buff *skb,
 					const struct nf_hook_state *state)
@@ -47,7 +34,7 @@ static unsigned int smack_ipv6_output(void *priv,
 	if (sk && smack_sock(sk)) {
 		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
-		skb->secmark = smack_to_secmark(skp->smk_secid);
+		skb->secmark = smack_to_token(skb->secmark, skp->smk_secid);
 	}
 
 	return NF_ACCEPT;
@@ -65,7 +52,7 @@ static unsigned int smack_ipv4_output(void *priv,
 	if (sk && smack_sock(sk)) {
 		ssp = smack_sock(sk);
 		skp = ssp->smk_out;
-		skb->secmark = smack_to_secmark(skp->smk_secid);
+		skb->secmark = smack_to_token(skb->secmark, skp->smk_secid);
 	}
 
 	return NF_ACCEPT;
security/smack/smackfs.c +2 -1
index 9d2dde608298..a4274f1e1e9b 100644
@@ -1284,7 +1284,8 @@ static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
 	if (rc == 0 && skp != NULL)
 		rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
 			&snp->smk_host, &snp->smk_mask, PF_INET,
-			snp->smk_label->smk_secid, &audit_info);
+			smack_to_token(0, snp->smk_label->smk_secid),
+			&audit_info);
 
 	if (rc == 0)
 		rc = count;
security/stacking.c +48 -15

122 lines not on this page · raw message

Signature · 7 lines
2.13.0


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info@ http://vger.kernel.org/majordomo-info.html
maintainer
#12 lore raw

PATCH 6/11 LSM: general but not extreme module stacking

In reply to Casey Schaufler
Quoted 16 lines · Casey Schaufler
security/Kconfig +16 -0
+	config SECURITY_SELINUX_STACKED
+		bool "SELinux" if SECURITY_SELINUX=y
+	help
+	  Add the SELinux security module to the stack. At this
+	  time the Smack security module is incompatible with this
+	  module.
+	  Please be sure your user space code is accomodating of
+	  this security module.
+
+	config SECURITY_SMACK_STACKED
+		bool "Simplified Mandatory Access Control" if SECURITY_SMACK=y
+	help
+	  Add the Smack security module to the stack. At this
+	  time the SELinux security module is incompatible with this
+	  module.
+	  Please be sure your user space code is accomodating of

security/Kconfig config DEFAULT_SECURITY · line 329

Quoted 10 lines
security/Kconfig +10 -0
+	  this security module.
+
+	config SECURITY_NOTHING_STACKED
+		bool "Use no 'extreme' security module"
+	help
+	  Add neither the SELinux security module nor the Smack security
+	  module to the stack.
+	  Please be sure your user space code does not require either of
+	  these security modules.
+

These help texts are likely to be confusing. e.g. "the stack" usually refers to a kernel or task stack, and which "user space code" needs what exactly?

Signature · 7 lines
James Morris
<[email protected]>

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#2 lore raw

PATCH 1/11 procfs: add smack subdir to attrs

In reply to Casey Schaufler
Quoted 55 lines · Casey Schaufler

Subject: [PATCH 01/11] procfs: add smack subdir to attrs

Back in 2007 I made what turned out to be a rather serious mistake in the implementation of the Smack security module. The SELinux module used an interface in /proc to manipulate the security context on processes. Rather than use a similar interface, I used the same interface. The AppArmor team did likewise. Now /proc/.../attr/current will tell you the security "context" of the process, but it will be different depending on the security module you're using.

This patch provides a subdirectory in /proc/.../attr for Smack. Smack user space can use the "current" file in this subdirectory and never have to worry about getting SELinux attributes by mistake. Programs that use the old interface will continue to work (or fail, as the case may be) as before.

This patch does not include subdirectories for SELinux or AppArmor. I do have a patch that provides those, and will happily make it available should anyone see value in it.

The original implementation is by Kees Cook.

---
 Documentation/admin-guide/LSM/index.rst | 13 +++++--
 fs/proc/base.c                          | 63 ++++++++++++++++++++++++++++-----
 fs/proc/internal.h                      |  1 +
 include/linux/security.h                | 15 +++++---
 security/security.c                     | 30 +++++++++++++---
 5 files changed, 101 insertions(+), 21 deletions(-)
Documentation/admin-guide/LSM/index.rst +3 -3
index c980dfe9abf1..9842e21afd4a 100644
@@ -17,9 +17,8 @@ MAC extensions, other extensions can be built using the LSM to provide
 specific changes to system operation when these tweaks are not available
 in the core functionality of Linux itself.
 
-Without a specific LSM built into the kernel, the default LSM will be the
-Linux capabilities system. Most LSMs choose to extend the capabilities
-system, building their checks on top of the defined capability hooks.
+The Linux capabilities modules will always be included. This may be
+followed by any number of "minor" modules and at most one "major" module.
 For more details on capabilities, see ``capabilities(7)`` in the Linux
 man-pages project.
 
@@ -30,6 +29,14 @@ order in which checks are made. The capability module will always
 be first, followed by any "minor" modules (e.g. Yama) and then
 the one "major" module (e.g. SELinux) if there is one configured.
 
+Process attributes associated with "major" security modules should

Documentation/admin-guide/LSM/index.rst order in which checks are made. The capability module will always · line 42

Quoted 10 lines
Documentation/admin-guide/LSM/index.rst +7 -0
+be accessed and maintained using the special files in ``/proc/.../attr``.
+A security module may maintain a module specific subdirectory there,
+named after the module. ``/proc/.../attr/smack`` is provided by the Smack
+security module and contains all its special files. The files directly
+in ``/proc/.../attr`` remain as legacy interfaces for modules that provide
+subdirectories.
+
 .. toctree::
    :maxdepth: 1
 

<< snip >>

Quoted 22 lines
Documentation/admin-guide/LSM/index.rst +15 -3
-int security_getprocattr(struct task_struct *p, char *name, char **value)
+int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
+				char **value)
 {
-	return call_int_hook(getprocattr, -EINVAL, p, name, value);
+	struct security_hook_list *hp;
+	int rc;
+
+	list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
+		if (lsm != NULL && strcmp(lsm, hp->lsm))
+			continue;
+		rc = hp->hook.getprocattr(p, name, value);
+		if (rc != -ENOENT)
+			return rc;
+	}
+	return -EINVAL;
 }
 
-int security_setprocattr(const char *name, void *value, size_t size)
+int security_setprocattr(const char *lsm, const char *name, void *value,
+			 size_t size)
 {

Documentation/admin-guide/LSM/index.rst

Quoted 10 lines
Documentation/admin-guide/LSM/index.rst +9 -1
-	return call_int_hook(setprocattr, -EINVAL, name, value, size);
+	struct security_hook_list *hp;
+	int rc;
+
+	list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
+		if (lsm != NULL && strcmp(lsm, hp->lsm))
+			continue;
+		rc = hp->hook.setprocattr(name, value, size);
+		if (rc != -ENOENT)
+			return rc;

why
if (rc != -ENOENT), here and above in getprocattr?

this breaks apparmor, as -ENOENT is returned when trying to set a label that doesn't exist

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

PATCH 3/11 LSM: Manage file security blobs

In reply to Casey Schaufler
Quoted 208 lines · Casey Schaufler

Subject: [PATCH 03/11] LSM: Manage file security blobs

Move the management of file security blobs from the individual security modules to the security infrastructure. The security modules using file blobs have been updated accordingly. Modules are required to identify the space they need at module initialization. In some cases a module no longer needs to supply a blob management hook, in which case the hook has been removed.

---

?include/linux/lsm_hooks.h???????????|??1 +
?security/apparmor/include/context.h |??5 +++++
?security/apparmor/include/file.h????|??2 +-
?security/apparmor/lsm.c?????????????| 19 +++++++++--------
?security/security.c?????????????????| 32
+++++++++++++++++++++++++++++
?security/selinux/hooks.c????????????| 41 +++++++++----------------
------------
?security/selinux/include/objsec.h???|??5 +++++
?security/smack/smack.h??????????????|??5 +++++
?security/smack/smack_lsm.c??????????| 26 ++++++++---------------

?9 files changed, 78 insertions(+), 58 deletions(-)

include/linux/lsm_hooks.h +0 -0
index 4ecb4ed572cf..0603c57726e4 100644
@@ -1923,6 +1923,7 @@ struct security_hook_list {

? */
?struct lsm_blob_sizes {
? int lbs_cred;

include/linux/lsm_hooks.h +1 -0
+	int	lbs_file;

?};
?
?/*

security/apparmor/include/context.h +0 -0
index 301ab3a0dd04..c6e106a533e8 100644
@@ -87,6 +87,11 @@ static inline struct aa_label
 *aa_get_newest_cred_label(const struct cred *cred)
 ?	return aa_get_newest_label(aa_cred_raw_label(cred));
 ?}

?
+static inline struct aa_file_ctx *apparmor_file(const struct file *file)

security/apparmor/include/context.h +4 -0
+{
+	return file->f_security;
+}
+

?/**
? * __aa_task_raw_label - retrieve another task's label ? * @task: task to query??(NOT NULL)

security/apparmor/include/file.h +0 -0
index 001e40073ff9..af87b23700d7 100644
@@ -32,7 +32,7 @@ struct path;

? ?AA_MAY_CHMOD | AA_MAY_CHOWN |
AA_MAY_LOCK | \
? ?AA_EXEC_MMAP | AA_MAY_LINK)
?

security/apparmor/include/file.h +1 -1
-#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
+#define file_ctx(X) apparmor_file(X)

?
?/* struct aa_file_ctx - the AppArmor context the file was opened in ? * @lock: lock to update the ctx

security/apparmor/lsm.c +0 -0
index 827df7012fe4..fb317cc94510 100644
@@ -400,21 +400,21 @@ static int apparmor_file_open(struct file
 *file, const struct cred *cred)

?
?static int apparmor_file_alloc_security(struct file *file) ?{

security/apparmor/lsm.c +1 -3
-	int error = 0;
-
-	/* freed by apparmor_file_free_security */
+	struct aa_file_ctx *ctx = file_ctx(file);

? struct aa_label *label = begin_current_label_crit_section();

security/apparmor/lsm.c +0 -4
-	file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
-	if (!file_ctx(file))
-		error = -ENOMEM;
-	end_current_label_crit_section(label);

?

security/apparmor/lsm.c +4 -1
-	return error;
+	spin_lock_init(&ctx->lock);
+	rcu_assign_pointer(ctx->label, aa_get_label(label));
+	end_current_label_crit_section(label);
+	return 0;

?}
?
?static void apparmor_file_free_security(struct file *file) ?{

security/apparmor/lsm.c +4 -1
-	aa_free_file_ctx(file_ctx(file));
+	struct aa_file_ctx *ctx = file_ctx(file);
+
+	if (ctx)
+		aa_put_label(rcu_access_pointer(ctx->label));

?}
?
?static int common_file_perm(const char *op, struct file *file, u32 mask)

security/apparmor/lsm.c +0 -0
@@ -635,6 +635,7 @@ static int apparmor_task_setrlimit(struct
 task_struct *task,

?
?struct lsm_blob_sizes apparmor_blob_sizes = {
? .lbs_cred = sizeof(struct aa_task_ctx),

security/apparmor/lsm.c +1 -0
+	.lbs_file = sizeof(struct aa_file_ctx),

?};
?
?static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {

security/security.c +0 -0
index 89d43c65630c..b9346db8a2d4 100644
@@ -91,6 +91,7 @@ int __init security_init(void)

?
?#ifdef CONFIG_SECURITY_LSM_DEBUG
? pr_info("LSM: cred blob size???????= %d\n",
blob_sizes.lbs_cred);

security/security.c +1 -0
+	pr_info("LSM: file blob size???????= %d\n",

blob_sizes.lbs_file);
?#endif
?
? return 0;

security/security.c +24 -0
@@ -267,6 +268,30 @@ static void __init lsm_set_size(int *need, int
 *lbs)
 ?void __init security_add_blobs(struct lsm_blob_sizes *needed)
 ?{
 ?	lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
+	lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
+}
+
+/**
+ * lsm_file_alloc - allocate a composite file blob
+ * @file: the file that needs a blob
+ *
+ * Allocate the file blob for all the modules
+ *
+ * Returns 0, or -ENOMEM if memory can't be allocated.
+ */
+int lsm_file_alloc(struct file *file)
+{
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	if (file->f_security)
+		pr_info("%s: Inbound file blob is not NULL.\n",
 __func__);
+#endif
+	if (blob_sizes.lbs_file == 0)
+		return 0;
+
+	file->f_security = kzalloc(blob_sizes.lbs_file, GFP_KERNEL);
+	if (file->f_security == NULL)
+		return -ENOMEM;
+	return 0;
 ?}

?
?/*

security/security.c +0 -0
@@ -957,12 +982,19 @@ int security_file_permission(struct file *file,
 int mask)

?
?int security_file_alloc(struct file *file)
?{

security/security.c +4 -0
+	int rc = lsm_file_alloc(file);
+
+	if (rc)
+		return rc;

? return call_int_hook(file_alloc_security, 0, file);
?}
?
?void security_file_free(struct file *file)
?{
? call_void_hook(file_free_security, file);

security/security.c +3 -0
+
+	kfree(file->f_security);
+	file->f_security = NULL;

?}
?
?int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

security/selinux/hooks.c +0 -0
index 3b2f028f1e86..b7909d710368 100644
@@ -129,7 +129,6 @@ int selinux_enabled = 1;

?#endif
?
?static struct kmem_cache *sel_inode_cache;
-static struct kmem_cache *file_security_cache;
?
?/**
? * selinux_secmark_enabled - Check to see if SECMARK is currently enabled

security/selinux/hooks.c +0 -0
@@ -359,27 +358,15 @@ static void inode_free_security(struct inode
 *inode)

?
?static int file_alloc_security(struct file *file) ?{

security/selinux/hooks.c +1 -1
-	struct file_security_struct *fsec;
+	struct file_security_struct *fsec = selinux_file(file);

? u32 sid = current_sid();
?

security/selinux/hooks.c static void inode_free_security(struct inode *inode) · line 368

Quoted 4 lines
security/selinux/hooks.c +0 -4
-	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
-	if (!fsec)
-		return -ENOMEM;
-

NAK. See commit 63205654c0e05e5ffa1c6eef2fbef21dcabd2185 for why this was changed from a simple kzalloc() to using its own cache; we don't want to regress in this regard.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#6 lore raw

PATCH 3/11 LSM: Manage file security blobs

Quoted 11 lines · Stephen Smalley
Quoted 10 lines · Casey Schaufler

Subject: [PATCH 03/11] LSM: Manage file security blobs

Move the management of file security blobs from the individual security modules to the security infrastructure. The security modules using file blobs have been updated accordingly. Modules are required to identify the space they need at module initialization. In some cases a module no longer needs to supply a blob management hook, in which case the hook has been removed.

<SNIP>

Quoted 11 lines
Quoted 11 lines

?/**
? * selinux_secmark_enabled - Check to see if SECMARK is currently enabled

(quoted diff) +0 -0
@@ -359,27 +358,15 @@ static void inode_free_security(struct inode
 *inode)

?
?static int file_alloc_security(struct file *file) ?{

security/selinux/hooks.c +1 -1
-	struct file_security_struct *fsec;
+	struct file_security_struct *fsec = selinux_file(file);

? u32 sid = current_sid();

Quoted 8 lines

security/selinux/hooks.c static void inode_free_security(struct inode *inode) · line 368

Quoted 5 lines

?

security/selinux/hooks.c +0 -4
-	fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
-	if (!fsec)
-		return -ENOMEM;
-

NAK. See commit 63205654c0e05e5ffa1c6eef2fbef21dcabd2185 for why this was changed from a simple kzalloc() to using its own cache; we don't want to regress in this regard.

I was somewhat expecting this. Is there any reason that using kmem_cache_zalloc() in the infrastructure would not address the issue? If this is a win for SELinux it should be a win for any module that uses file blobs.

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

PATCH 5/11 LSM: Infrastructure management of the remaining blobs

In reply to Casey Schaufler
Quoted 23 lines · Casey Schaufler

Subject: [PATCH 05/11] LSM: Infrastructure management of the remaining blobs

Move management of the inode, ipc, key, msg_msg, sock and superblock security blobs from the security modules to the infrastructure. Use of the blob pointers is abstracted in the security modules.

---

?include/linux/lsm_hooks.h?????????|???8 +
?security/security.c???????????????| 259 +++++++++++++++++++++++++++-
?security/selinux/hooks.c??????????| 333 ++++++++++++--------------
----------
?security/selinux/include/objsec.h |??65 +++++++-
?security/selinux/netlabel.c???????|??15 +-
?security/selinux/selinuxfs.c??????|???4 +-
?security/selinux/ss/services.c????|???3 +-
?security/smack/smack.h????????????|??61 ++++++-
?security/smack/smack_lsm.c????????| 343 +++++++++++-----------------

----------
?security/smack/smack_netfilter.c??|???8 +- ?10 files changed, 599 insertions(+), 500 deletions(-)

<snip>

Quoted 24 lines
(quoted diff) +0 -0
@@ -319,6 +341,166 @@ int lsm_task_alloc(struct task_struct *task)
 ?	return 0;
 ?}

?

security/security.c +13 -0
+/**
+ * lsm_inode_alloc - allocate a composite inode blob
+ * @inode: the inode that needs a blob
+ *
+ * Allocate the inode blob for all the modules
+ *
+ * Returns 0, or -ENOMEM if memory can't be allocated.
+ */
+int lsm_inode_alloc(struct inode *inode)
+{
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	if (inode->i_security)
+		pr_info("%s: Inbound inode blob is not NULL.\n",

__func__);

security/security.c +5 -0
+#endif
+	if (blob_sizes.lbs_inode == 0)
+		return 0;
+
+	inode->i_security = kzalloc(blob_sizes.lbs_inode,

GFP_KERNEL);

security/security.c int lsm_task_alloc(struct task_struct *task) · line 365

Quoted 4 lines
security/security.c +4 -0
+	if (inode->i_security == NULL)
+		return -ENOMEM;
+	return 0;
+}

NAK. See commit 7cae7e26f245151b9ccad868bf2edf8c8048d307 for why we moved to using our own cache for inode security blobs in SELinux; we don't want to regress here. Also, you need to use GFP_NOFS here, see commit a02fe13297af26c13d004b1d44f391c077094ea0.

<snip>

Quoted 27 lines
(quoted diff) +0 -0
@@ -570,14 +758,39 @@ EXPORT_SYMBOL(security_sb_parse_opts_str);

?
?int security_inode_alloc(struct inode *inode)
?{

security/security.c +4 -1
-	inode->i_security = NULL;
+	int rc = lsm_inode_alloc(inode);
+
+	if (rc)
+		return rc;

? return call_int_hook(inode_alloc_security, 0, inode);
?}
?

security/security.c +8 -0
+static void inode_free_by_rcu(struct rcu_head *head)
+{
+	/*
+	?* The rcu head is at the start of the inode blob
+	?*/
+	kfree(head);
+}
+

?void security_inode_free(struct inode *inode)
?{
? integrity_inode_free(inode);
? call_void_hook(inode_free_security, inode);

(quoted diff) +3 -0
+	/*
+	?* The inode may still be referenced in a path walk and
+	?* a call to security_inode_permission() can be made

Review

Quoted 10 lines
(quoted diff) +10 -0
+	?* after inode_free_security() is called. Ideally, the VFS
+	?* wouldn't do this, but fixing that is a much harder
+	?* job. For now, simply free the i_security via RCU, and
+	?* leave the current inode->i_security pointer intact.
+	?* The inode will be freed after the RCU grace period too.
+	?*/
+	if (inode->i_security != NULL) {
+		call_rcu((struct rcu_head *)inode->i_security,
+				inode_free_by_rcu);
+		inode->i_security = NULL;

You can't clear inode->i_security here; see the comment above that you copied from the original SELinux code.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw

PATCH 7/11 LSM: Shared secids by token

In reply to Casey Schaufler
Quoted 1,081 lines · Casey Schaufler

Subject: [PATCH 07/11] LSM: Shared secids by token

Introduces a mechanism for mapping a set of security module secids to and from a "token". The module interfaces are changed to generally hide the mechanism from both the security modules and the callers of the security hooks.

---

?include/linux/lsm_hooks.h????????|??54 ++++++++-
?security/Makefile????????????????|???1 +
?security/security.c??????????????| 248
++++++++++++++++++++++++++++++++++-----
?security/selinux/hooks.c?????????|??31 +++--
?security/selinux/include/xfrm.h??|???2 +-
?security/selinux/xfrm.c??????????|???6 +-
?security/smack/smack.h???????????|???1 +
?security/smack/smack_lsm.c???????|??19 ++-
?security/smack/smack_netfilter.c |??17 ++-
?security/stacking.c??????????????| 165 ++++++++++++++++++++++++++

?10 files changed, 497 insertions(+), 47 deletions(-) ?create mode 100644 security/stacking.c

include/linux/lsm_hooks.h +1 -1
index dfe4dab1ff8d..75d95854f2ed 100644
@@ -1627,7 +1627,7 @@ union security_list_options {
 ?	void (*secmark_refcount_inc)(void);
 ?	void (*secmark_refcount_dec)(void);
 ?	void (*req_classify_flow)(const struct request_sock *req,
-					struct flowi *fl);
+					u32 *fl_secid);
 ?	int (*tun_dev_alloc_security)(void **security);
 ?	void (*tun_dev_free_security)(void *security);
 ?	int (*tun_dev_create)(void);
@@ -1663,7 +1663,7 @@ union security_list_options {
 ?					u8 dir);
 ?	int (*xfrm_state_pol_flow_match)(struct xfrm_state *x,

? struct xfrm_policy
*xp,

include/linux/lsm_hooks.h +0 -1
-						const struct flowi

*fl);

include/linux/lsm_hooks.h +1 -0
+						u32 fl_secid);

? int (*xfrm_decode_session)(struct sk_buff *skb, u32 *secid,
int ckall);
?#endif /* CONFIG_SECURITY_NETWORK_XFRM */
?

include/linux/lsm_hooks.h +1 -0
@@ -1916,9 +1916,59 @@ struct security_hook_list {
 ?	struct list_head		*head;
 ?	union security_list_options	hook;
 ?	char				*lsm;
+	int				lsm_index;
 ?} __randomize_layout;

?
?/*
+ * The maximum number of major security modules.
+ * Used to avoid excessive memory management while
+ * mapping global and module specific secids.
+ *
+ * Currently SELinux, Smack, AppArmor, TOMOYO
+ * Oh, but Casey needs to come up with the right way
+ * to identify a "major" module, so use the total number
+ * of modules (including minor) for now.
+ * Minor: Capability, Yama, LoadPin
+ */

include/linux/lsm_hooks.h +9 -0
+#define	LSM_MAX_MAJOR	8
+
+#ifdef CONFIG_SECURITY_STACKING
+struct lsm_secids {
+	u32	secid[LSM_MAX_MAJOR];
+};
+
+extern u32 lsm_secids_to_token(const struct lsm_secids *secids);
+extern void lsm_token_to_secids(const u32 token, struct lsm_secids

*secids);

include/linux/lsm_hooks.h +8 -0
+extern u32 lsm_token_to_module_secid(const u32 token, int lsm);
+extern void lsm_secids_init(struct lsm_secids *secids);
+#else /* CONFIG_SECURITY_STACKING */
+struct lsm_secids {
+	u32	secid;
+};
+
+static inline u32 lsm_secids_to_token(const struct lsm_secids

*secids)

include/linux/lsm_hooks.h +11 -0
+{
+	return secids->secid;
+}
+
+static inline void lsm_token_to_secids(const u32 token,
+				???????struct lsm_secids *secids)
+{
+	secids->secid = token;
+}
+
+static inline u32 lsm_token_to_module_secid(const u32 token, int

lsm)

include/linux/lsm_hooks.h +11 -0
+{
+	return token;
+}
+
+static inline void lsm_secids_init(struct lsm_secids *secids)
+{
+	secids->secid = 0;
+}
+#endif /* CONFIG_SECURITY_STACKING */
+
+/*

? * Security blob size or offset data.
? */
?struct lsm_blob_sizes {

security/Makefile +0 -0
index f2d71cdb8e19..05e6d525b5a1 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_SECURITY_APPARMOR)		+=

apparmor/
?obj-$(CONFIG_SECURITY_YAMA) += yama/
?obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/
?obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o
+obj-$(CONFIG_SECURITY_STACKING) += stacking.o
?
?# Object integrity file lists
?subdir-$(CONFIG_INTEGRITY) += integrity

security/security.c +0 -0
index 6b979aa769ad..9d402d954cef 100644
@@ -199,6 +199,11 @@ bool __init security_module_enable(const char
 *lsm, const bool stacked)

?#endif
?}
?

security/security.c +5 -0
+/*
+ * Keep the order of major modules for mapping secids.
+ */
+static int lsm_next_major;
+

?/**
? * security_add_hooks - Add a modules hooks to the hook lists. ? * @hooks: the hooks to add

security/security.c +1 -0
@@ -211,9 +216,14 @@ void __init security_add_hooks(struct
 security_hook_list *hooks, int count,
 ?				char *lsm)
 ?{
 ?	int i;
+	int lsm_index = lsm_next_major++;

?

security/security.c +2 -0
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: Security module %s gets index %d\n", lsm,

lsm_index);
+#endif
? for (i = 0; i < count; i++) {
? hooks[i].lsm = lsm;

security/security.c +1 -0
+		hooks[i].lsm_index = lsm_index;

? list_add_tail_rcu(&hooks[i].list, hooks[i].head);
? }
? if (lsm_append(lsm, &lsm_names) < 0)

security/security.c +0 -0
@@ -1218,7 +1228,15 @@ EXPORT_SYMBOL(security_inode_listsecurity);

?
?void security_inode_getsecid(struct inode *inode, u32 *secid) ?{

security/security.c +6 -1
-	call_void_hook(inode_getsecid, inode, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.inode_getsecid,

list)

security/security.c +1 -0
+		hp->hook.inode_getsecid(inode, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_inode_copy_up(struct dentry *src, struct cred **new)

security/security.c +0 -0
@@ -1406,7 +1424,18 @@ void security_transfer_creds(struct cred *new,
 const struct cred *old)

?
?int security_kernel_act_as(struct cred *new, u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(kernel_act_as, 0, new, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp, &security_hook_heads.kernel_act_as,

list) {

security/security.c +1 -0
+		rc = hp->hook.kernel_act_as(new, secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_kernel_create_files_as(struct cred *new, struct inode *inode)

security/security.c +0 -0
@@ -1465,8 +1494,15 @@ int security_task_getsid(struct task_struct
 *p)

?
?void security_task_getsecid(struct task_struct *p, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(task_getsecid, p, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.task_getsecid,

list)

security/security.c +1 -0
+		hp->hook.task_getsecid(p, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_task_getsecid);
?

security/security.c +13 -1
@@ -1515,7 +1551,19 @@ int security_task_movememory(struct
 task_struct *p)
 ?int security_task_kill(struct task_struct *p, struct siginfo *info,
 ?			int sig, u32 secid)
 ?{
-	return call_int_hook(task_kill, 0, p, info, sig, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp, &security_hook_heads.task_kill,
 list) {
+		rc = hp->hook.task_kill(p, info, sig,
+					secids.secid[hp-
 >lsm_index]);
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,

security/security.c +0 -0
@@ -1548,8 +1596,15 @@ int security_ipc_permission(struct
 kern_ipc_perm *ipcp, short flag)

?
?void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(ipc_getsecid, ipcp, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.ipc_getsecid,

list)

security/security.c +1 -0
+		hp->hook.ipc_getsecid(ipcp, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_msg_msg_alloc(struct msg_msg *msg)

security/security.c +0 -0
@@ -1840,15 +1895,42 @@ EXPORT_SYMBOL(security_ismaclabel);

?
?int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) ?{

security/security.c +0 -1
-	return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid,

secdata,

security/security.c +10 -1
-				seclen);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -EOPNOTSUPP;
+
+	lsm_token_to_secids(secid, &secids);
+
+	/*
+	?* CBS - Return the first result regardless.
+	?*/
+	list_for_each_entry(hp,

&security_hook_heads.secid_to_secctx, list) {

security/security.c +1 -0
+		rc = hp->hook.secid_to_secctx(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +5 -0
+						secdata, seclen);
+		if (rc != -EOPNOTSUPP)
+			break;
+	}
+	return rc;

?}
?EXPORT_SYMBOL(security_secid_to_secctx);
?
?int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
?{

security/security.c +0 -2
-	*secid = 0;
-	return call_int_hook(secctx_to_secid, 0, secdata, seclen,

secid);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secctx_to_secid, list) {

security/security.c +2 -0
+		rc = hp->hook.secctx_to_secid(secdata, seclen,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+
+	*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?EXPORT_SYMBOL(security_secctx_to_secid);
?

security/security.c +0 -0
@@ -1977,10 +2059,26 @@ int security_socket_getpeersec_stream(struct
 socket *sock, char __user *optval,
 ?				optval, optlen, len);
 ?}

?
-int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
+int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb,

security/security.c +1 -0
+				?????u32 *secid)

?{

security/security.c +0 -1
-	return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT,

sock,

security/security.c +7 -1
-			?????skb, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -ENOPROTOOPT;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.socket_getpeersec_dgram,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.socket_getpeersec_dgram(sock, skb,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +7 -0
+		if (rc)
+			break;
+	}
+
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?} ?EXPORT_SYMBOL(security_socket_getpeersec_dgram); ?

security/security.c +0 -0
@@ -2008,13 +2106,30 @@ EXPORT_SYMBOL(security_sk_clone);

?
?void security_sk_classify_flow(struct sock *sk, struct flowi *fl) ?{

security/security.c +6 -1
-	call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.sk_getsecid,

list)

security/security.c +1 -0
+		hp->hook.sk_getsecid(sk, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_sk_classify_flow);
?
-void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)

security/security.c +2 -0
+void security_req_classify_flow(const struct request_sock *req,
+				struct flowi *fl)

?{

security/security.c +6 -1
-	call_void_hook(req_classify_flow, req, fl);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.req_classify_flow, list)

security/security.c +1 -0
+		hp->hook.req_classify_flow(req, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_req_classify_flow);
?

security/security.c +0 -0
@@ -2045,7 +2160,20 @@ void security_inet_conn_established(struct
 sock *sk,

?
?int security_secmark_relabel_packet(u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(secmark_relabel_packet, 0, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secmark_relabel_packet,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.secmark_relabel_packet(
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?} ?EXPORT_SYMBOL(security_secmark_relabel_packet); ?

security/security.c +8 -1
@@ -2163,7 +2291,20 @@ EXPORT_SYMBOL(security_xfrm_state_alloc);
 ?int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
 ?				??????struct xfrm_sec_ctx *polsec,
 u32 secid)
 ?{
-	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec,
 secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,
 &security_hook_heads.xfrm_state_alloc_acquire,
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_alloc_acquire(x, polsec,
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_delete(struct xfrm_state *x)

security/security.c +0 -0
@@ -2179,7 +2320,19 @@ void security_xfrm_state_free(struct
 xfrm_state *x)

?
?int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
?{

security/security.c +0 -1
-	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid,

dir);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(fl_secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_policy_lookup, list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_policy_lookup(ctx,
+					secids.secid[hp->lsm_index], 

dir);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_pol_flow_match(struct xfrm_state *x,

security/security.c +1 -0
@@ -2187,6 +2340,7 @@ int security_xfrm_state_pol_flow_match(struct
 xfrm_state *x,
 ?				???????const struct flowi *fl)
 ?{
 ?	struct security_hook_list *hp;
+	struct lsm_secids secids;
 ?	int rc = 1;

?
? /*

security/security.c +0 -0
@@ -2198,9 +2352,12 @@ int security_xfrm_state_pol_flow_match(struct
 xfrm_state *x,

? ?* For speed optimization, we explicitly break the loop
rather than
? ?* using the macro
? ?*/

security/security.c +2 -0
+	lsm_token_to_secids(fl->flowi_secid, &secids);
+

? list_for_each_entry(hp,
&security_hook_heads.xfrm_state_pol_flow_match,

security/security.c +1 -2
-				list) {
-		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_pol_flow_match(x, xp,
+				secids.secid[hp->lsm_index]);

? break;
? }
? return rc;

security/security.c +0 -0
@@ -2208,15 +2365,41 @@ int security_xfrm_state_pol_flow_match(struct
 xfrm_state *x,

?
?int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid) ?{

security/security.c +7 -1
-	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 1);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?
?void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
?{

security/security.c +0 -1
-	int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl-
Quoted 1 line

flowi_secid,

security/security.c +5 -1
-				0);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);

?

security/security.c +1 -0
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 0);

security/security.c +3 -0
+		if (rc)
+			break;
+	}

? BUG_ON(rc);

security/security.c +1 -0
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_skb_classify_flow);
?

security/security.c +0 -0
@@ -2275,7 +2458,18 @@ void security_audit_rule_free(void *lsmrule)

?int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
? ??????struct audit_context *actx)
?{

security/security.c +0 -1
-	return call_int_hook(audit_rule_match, 0, secid, field, op,

lsmrule,

security/security.c +7 -1
-				actx);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.audit_rule_match, list) {

security/security.c +1 -0
+		rc = hp->hook.audit_rule_match(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +1 -0
+						field, op, lsmrule,

actx);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?#endif /* CONFIG_AUDIT */

security/selinux/hooks.c +0 -0
index 84d533335924..389f09ebd374 100644
@@ -100,6 +100,9 @@

?/* SECMARK reference count */
?static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); ?

security/selinux/hooks.c +3 -0
+/* Index into lsm_secids */
+static int selinux_secids_index;
+

?#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
?int selinux_enforcing;
?

security/selinux/hooks.c +0 -0
@@ -4610,6 +4613,11 @@ static int selinux_inet_sys_rcv_skb(struct net
 *ns, int ifindex,
 ?			????SECCLASS_NODE, NODE__RECVFROM, ad);
 ?}

?

security/selinux/hooks.c +3 -0
+static u32 selinux_secmark_to_secid(u32 token)
+{
+	return lsm_token_to_module_secid(token,

selinux_secids_index);

security/selinux/hooks.c +2 -0
+}
+

?static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
? ???????u16 family)
?{

security/selinux/hooks.c +0 -0
@@ -4629,7 +4637,9 @@ static int selinux_sock_rcv_skb_compat(struct
 sock *sk, struct sk_buff *skb,
 ?		return err;

?
? if (selinux_secmark_enabled()) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4703,7 +4713,9 @@ static int selinux_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?	}

?
? if (secmark_active) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4902,9 +4914,9 @@ static void selinux_secmark_refcount_dec(void)
 ?}

?
?static void selinux_req_classify_flow(const struct request_sock *req,

security/selinux/hooks.c +1 -1
-				??????struct flowi *fl)
+				??????u32 *fl_secid)

?{

security/selinux/hooks.c +1 -1
-	fl->flowi_secid = req->secid;
+	*fl_secid = req->secid;

?}
?
?static int selinux_tun_dev_alloc_security(void **security)

security/selinux/hooks.c +0 -0
@@ -5066,7 +5078,8 @@ static unsigned int selinux_ip_forward(struct
 sk_buff *skb,
 ?	}

?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET,
PACKET__FORWARD_IN, &ad))
? return NF_DROP;
?

security/selinux/hooks.c +0 -0
@@ -5178,7 +5191,8 @@ static unsigned int
 selinux_ip_postroute_compat(struct sk_buff *skb,
 ?		return NF_DROP;

?
? if (selinux_secmark_enabled())

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(sksec->sid, skb->secmark,
+		if (avc_has_perm(sksec->sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, PACKET__SEND,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -5301,7 +5315,8 @@ static unsigned int selinux_ip_postroute(struct
 sk_buff *skb,
 ?		return NF_DROP;

?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, secmark_perm,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -6339,6 +6354,8 @@ static __init int selinux_init(void)

?
? security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks),
"selinux");
?

security/selinux/hooks.c +2 -0
+	selinux_secids_index = selinux_hooks[0].lsm_index;
+

? if (avc_add_callback(selinux_netcache_avc_callback,
AVC_CALLBACK_RESET))
? panic("SELinux: Unable to register AVC netcache
callback\n");
?

security/selinux/include/xfrm.h +0 -0
index 1450f85b946d..475a328248b3 100644
@@ -25,7 +25,7 @@ int selinux_xfrm_state_delete(struct xfrm_state
 *x);

?int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/include/xfrm.h +1 -1
-				??????const struct flowi *fl);
+				??????u32 fl_secid);

?
?#ifdef CONFIG_SECURITY_NETWORK_XFRM
?extern atomic_t selinux_xfrm_refcount;

security/selinux/xfrm.c +0 -0
index 789d07bd900f..d71e2c32b5da 100644
@@ -174,7 +174,7 @@ int selinux_xfrm_policy_lookup(struct
 xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)

? */
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/xfrm.c +1 -1
-				??????const struct flowi *fl)
+				??????u32 fl_secid)

?{
? u32 state_sid;
?

security/selinux/xfrm.c +0 -0
@@ -196,13 +196,13 @@ int selinux_xfrm_state_pol_flow_match(struct
 xfrm_state *x,

?
? state_sid = x->security->ctx_sid;
?

security/selinux/xfrm.c +1 -1
-	if (fl->flowi_secid != state_sid)
+	if (fl_secid != state_sid)

? return 0;
?
? /* We don't need a separate SA Vs. policy polmatch check
since the SA
? ?* is now of the same label as the flow and a flow Vs.
policy polmatch
? ?* check had already happened in
selinux_xfrm_policy_lookup() above. */

security/selinux/xfrm.c +1 -1
-	return (avc_has_perm(fl->flowi_secid, state_sid,
+	return (avc_has_perm(fl_secid, state_sid,

? ????SECCLASS_ASSOCIATION,
ASSOCIATION__SENDTO,
? ????NULL) ? 0 : 1);
?}

security/smack/smack.h +0 -0
index e7611de071f1..e9fd586e0ec1 100644
@@ -328,6 +328,7 @@ void smk_destroy_label_list(struct list_head
 *list);

? * Shared data.
? */
?extern int smack_enabled;
+extern int smack_secids_index;
?extern int smack_cipso_direct;
?extern int smack_cipso_mapped;
?extern struct smack_known *smack_net_ambient;

security/smack/smack_lsm.c +0 -0
index 1e9ab7bdaf55..51daf9b05f17 100644
@@ -57,6 +57,7 @@ static LIST_HEAD(smk_ipv6_port_list);

?#endif
?static struct kmem_cache *smack_inode_cache; ?int smack_enabled;
+int smack_secids_index;
?
?static const match_table_t smk_mount_tokens = {
? {Opt_fsdefault, SMK_FSDEFAULT "%s"},

security/smack/smack_lsm.c +0 -0
@@ -3788,6 +3789,13 @@ static int smk_skb_to_addr_ipv6(struct sk_buff
 *skb, struct sockaddr_in6 *sip)
 ?}

?#endif /* CONFIG_IPV6 */
?

security/smack/smack_lsm.c +4 -0
+#ifdef CONFIG_SECURITY_SMACK_NETFILTER
+static u32 smk_of_secmark(u32 secmark)
+{
+	return lsm_token_to_module_secid(secmark,

smack_secids_index);

security/smack/smack_lsm.c +3 -0
+}
+#endif
+

?/**
? * smack_socket_sock_rcv_skb - Smack packet delivery access check ? * @sk: socket

security/smack/smack_lsm.c +0 -0
@@ -3819,7 +3827,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)

? ?* The secmark is assumed to reflect policy better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -3864,7 +3872,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?			break;

?#ifdef SMACK_IPV6_SECMARK_LABELING
? if (skb && skb->secmark != 0)

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? else
? skp = smack_ipv6host_label(&sadd);
? if (skp == NULL)

security/smack/smack_lsm.c +0 -0
@@ -3962,7 +3970,7 @@ static int smack_socket_getpeersec_dgram(struct
 socket *sock,
 ?		break;

? case PF_INET:
?#ifdef CONFIG_SECURITY_SMACK_NETFILTER

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

? if (s != 0)
? break;
?#endif

security/smack/smack_lsm.c +0 -0
@@ -3981,7 +3989,7 @@ static int smack_socket_getpeersec_dgram(struct
 socket *sock,
 ?		break;

? case PF_INET6:
?#ifdef SMACK_IPV6_SECMARK_LABELING

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

?#endif
? break;
? }

security/smack/smack_lsm.c +0 -0
@@ -4060,7 +4068,7 @@ static int smack_inet_conn_request(struct sock
 *sk, struct sk_buff *skb,

? ?* The secmark is assumed to reflect policy better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-		skp = smack_from_secid(skb->secmark);
+		skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -4650,6 +4658,7 @@ static __init int smack_init(void)

? ?* Register with LSM
? ?*/
? security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks),
"smack");

security/smack/smack_lsm.c +1 -0
+	smack_secids_index = smack_hooks[0].lsm_index;

? smack_enabled = 1;
?
? pr_info("Smack:??Initializing.\n");

security/smack/smack_netfilter.c +0 -0
index a5155295551f..510661ba6c16 100644
@@ -23,6 +23,19 @@

?
?#if IS_ENABLED(CONFIG_IPV6)
?

security/smack/smack_netfilter.c +13 -0
+/*
+ * Reinvestigate this soon?
+ *
+ */
+static u32 smack_to_secmark(u32 secid)
+{
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+	secids.secid[smack_secids_index] = secid;
+	return lsm_secids_to_token(&secids);
+}
+

?static unsigned int smack_ipv6_output(void *priv,
? struct sk_buff *skb,
? const struct nf_hook_state
*state)

security/smack/smack_netfilter.c +1 -1
@@ -34,7 +47,7 @@ static unsigned int smack_ipv6_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/smack/smack_netfilter.c +1 -1
@@ -52,7 +65,7 @@ static unsigned int smack_ipv4_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/stacking.c new file +11 -0
new file mode 100644
index 000000000000..65276cd695de
@@ -0,0 +1,165 @@
+/*
+ *??Maintain a mapping between the secid used in networking
+ *??and the set of secids used by the security modules.
+ *
+ *??Author:
+ *	Casey Schaufler <[email protected]>
+ *
+ *??Copyright (C) 2017 Casey Schaufler <[email protected]>
+ *??Copyright (C) 2017 Intel Corporation.
+ *
+ *	This program is free software; you can redistribute it

and/or modify
+ * it under the terms of the GNU General Public License
version 2,
+ *??????as published by the Free Software Foundation.
+ */

security/stacking.c new file +5 -0
+
+#include <linux/lsm_hooks.h>
+
+struct token_entry {
+	int			used;	/* relative age of

the entry */

security/stacking.c new file +2 -0
+	u32			token;	/* token value */
+	struct lsm_secids	secids;	/* secids mapped to

this token */

security/stacking.c new file +19 -0
+};
+
+/*
+ * Add an entry to the table when asked for a mapping that
+ * isn't already present. If the table is full throw away the
+ * least recently used entry. If the entry is present undate
+ * when it was used.
+ */
+#define TOKEN_AGE_LIMIT (MAX_INT >> 2)
+#define TOKEN_LIMIT 0x20000000
+#define TOKEN_SET_SIZE 200
+#define TOKEN_BIT 0x80000000
+int token_used;
+u32 token_next;
+struct lsm_secids null_secids;
+struct token_entry token_set[TOKEN_SET_SIZE];
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+static void report_token(const char *msg, const struct token_entry

*te)

security/stacking.c new file +4 -0
+{
+	int i;
+
+	pr_info("LSM: %s token=%08x %u,%u,%u,%u,%u,%u,%u,%u\n", msg,

te->token,

security/stacking.c new file +1 -0
+		te->secids.secid[0], te->secids.secid[1], te-
Quoted 1 line

secids.secid[2],

security/stacking.c new file +1 -0
+		te->secids.secid[3], te->secids.secid[4], te-
Quoted 1 line

secids.secid[5],

security/stacking.c new file +4 -0
+		te->secids.secid[6], te->secids.secid[7]);
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (te->secids.secid[i] & TOKEN_BIT)
+			pr_info("LSM: module %d provided a

token.\n", i);

security/stacking.c new file +3 -0
+}
+#else
+static inline void report_token(const char *msg, const struct

token_entry *te)

security/stacking.c new file +8 -0
+{
+}
+#endif
+
+static int next_used(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token use overflow - safe

reset\n");

security/stacking.c new file +9 -0
+		token_used = 0;
+	}
+	return ++token_used;
+}
+
+static u32 next_token(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token overflow - safe

reset\n");

security/stacking.c new file +15 -0
+		token_next = 0;
+	}
+	return ++token_next | TOKEN_BIT;
+}
+
+u32 lsm_secids_to_token(const struct lsm_secids *secids)
+{
+	int i;
+	int j;
+	int old;
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (secids->secid[i] & TOKEN_BIT)
+			pr_info("LSM: %s secid[%d]=%08x has token

bit\n",

security/stacking.c new file +14 -0
+				__func__, i, secids->secid[i]);
+#endif
+
+	/*
+	?* If none of the secids are set whoever sent this here
+	?* was thinking "0".
+	?*/
+	if (!memcmp(secids, &null_secids, sizeof(*secids)))
+		return 0;
+
+	for (i = 0; i < TOKEN_SET_SIZE; i++) {
+		if (token_set[i].token == 0)
+			break;
+		if (!memcmp(secids, &token_set[i].secids,

sizeof(*secids))) {

security/stacking.c new file +31 -0
+			token_set[i].used = next_used();
+			return token_set[i].token;
+		}
+	}
+	if (i == TOKEN_SET_SIZE) {
+		old = token_used;
+		for (j = 0; j < TOKEN_SET_SIZE; j++) {
+			if (token_set[j].used < old) {
+				old = token_set[j].used;
+				i = j;
+			}
+		}
+	}
+	token_set[i].secids = *secids;
+	token_set[i].token = next_token();
+	token_set[i].used = next_used();
+
+	report_token("new", &token_set[i]);
+
+	return token_set[i].token;
+}
+
+void lsm_token_to_secids(const u32 token, struct lsm_secids *secids)
+{
+	int i;
+	struct lsm_secids fudge;
+
+	if (token) {
+		if (!(token & TOKEN_BIT)) {
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+			pr_info("LSM: %s token=%08x has no token

bit\n",

security/stacking.c new file +18 -0
+				__func__, token);
+#endif
+			for (i = 0; i < LSM_MAX_MAJOR; i++)
+				fudge.secid[i] = token;
+			*secids = fudge;
+			return;
+		}
+		for (i = 0; i < TOKEN_SET_SIZE; i++) {
+			if (token_set[i].token == 0)
+				break;
+			if (token_set[i].token == token) {
+				*secids = token_set[i].secids;
+				token_set[i].used = next_used();
+				return;
+			}
+		}
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: %s token=%u was not found\n", __func__,

token);

security/stacking.c new file +7 -0
+#endif
+	}
+	*secids = null_secids;
+}
+
+u32 lsm_token_to_module_secid(const u32 token, int lsm)
+{

security/stacking.c line 165

Quoted 10 lines
security/stacking.c new file +10 -0
+	struct lsm_secids secids;
+
+????????lsm_token_to_secids(token, &secids);
+	return secids.secid[lsm];
+}
+
+void lsm_secids_init(struct lsm_secids *secids)
+{
+	*secids = null_secids;
+}

ENOLOCKING
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw

PATCH 7/11 LSM: Shared secids by token

Quoted 1,142 lines · Stephen Smalley
Quoted 1,141 lines · Casey Schaufler

Subject: [PATCH 07/11] LSM: Shared secids by token

Introduces a mechanism for mapping a set of security module secids to and from a "token". The module interfaces are changed to generally hide the mechanism from both the security modules and the callers of the security hooks.

---

?include/linux/lsm_hooks.h????????|??54 ++++++++-
?security/Makefile????????????????|???1 +
?security/security.c??????????????| 248
++++++++++++++++++++++++++++++++++-----
?security/selinux/hooks.c?????????|??31 +++--
?security/selinux/include/xfrm.h??|???2 +-
?security/selinux/xfrm.c??????????|???6 +-
?security/smack/smack.h???????????|???1 +
?security/smack/smack_lsm.c???????|??19 ++-
?security/smack/smack_netfilter.c |??17 ++-
?security/stacking.c??????????????| 165 ++++++++++++++++++++++++++

?10 files changed, 497 insertions(+), 47 deletions(-) ?create mode 100644 security/stacking.c

include/linux/lsm_hooks.h +1 -1
index dfe4dab1ff8d..75d95854f2ed 100644
@@ -1627,7 +1627,7 @@ union security_list_options {
 ?	void (*secmark_refcount_inc)(void);
 ?	void (*secmark_refcount_dec)(void);
 ?	void (*req_classify_flow)(const struct request_sock *req,
-					struct flowi *fl);
+					u32 *fl_secid);
 ?	int (*tun_dev_alloc_security)(void **security);
 ?	void (*tun_dev_free_security)(void *security);
 ?	int (*tun_dev_create)(void);
@@ -1663,7 +1663,7 @@ union security_list_options {
 ?					u8 dir);
 ?	int (*xfrm_state_pol_flow_match)(struct xfrm_state *x,

? struct xfrm_policy
*xp,

include/linux/lsm_hooks.h +0 -1
-						const struct flowi

*fl);

include/linux/lsm_hooks.h +1 -0
+						u32 fl_secid);

? int (*xfrm_decode_session)(struct sk_buff *skb, u32
*secid,
int ckall);
?#endif /* CONFIG_SECURITY_NETWORK_XFRM */
?

include/linux/lsm_hooks.h +1 -0
@@ -1916,9 +1916,59 @@ struct security_hook_list {
 ?	struct list_head		*head;
 ?	union security_list_options	hook;
 ?	char				*lsm;
+	int				lsm_index;
 ?} __randomize_layout;

?
?/*
+ * The maximum number of major security modules.
+ * Used to avoid excessive memory management while
+ * mapping global and module specific secids.
+ *
+ * Currently SELinux, Smack, AppArmor, TOMOYO
+ * Oh, but Casey needs to come up with the right way
+ * to identify a "major" module, so use the total number
+ * of modules (including minor) for now.
+ * Minor: Capability, Yama, LoadPin
+ */

include/linux/lsm_hooks.h +9 -0
+#define	LSM_MAX_MAJOR	8
+
+#ifdef CONFIG_SECURITY_STACKING
+struct lsm_secids {
+	u32	secid[LSM_MAX_MAJOR];
+};
+
+extern u32 lsm_secids_to_token(const struct lsm_secids *secids);
+extern void lsm_token_to_secids(const u32 token, struct lsm_secids

*secids);

include/linux/lsm_hooks.h +8 -0
+extern u32 lsm_token_to_module_secid(const u32 token, int lsm);
+extern void lsm_secids_init(struct lsm_secids *secids);
+#else /* CONFIG_SECURITY_STACKING */
+struct lsm_secids {
+	u32	secid;
+};
+
+static inline u32 lsm_secids_to_token(const struct lsm_secids

*secids)

include/linux/lsm_hooks.h +11 -0
+{
+	return secids->secid;
+}
+
+static inline void lsm_token_to_secids(const u32 token,
+				???????struct lsm_secids *secids)
+{
+	secids->secid = token;
+}
+
+static inline u32 lsm_token_to_module_secid(const u32 token, int

lsm)

include/linux/lsm_hooks.h +11 -0
+{
+	return token;
+}
+
+static inline void lsm_secids_init(struct lsm_secids *secids)
+{
+	secids->secid = 0;
+}
+#endif /* CONFIG_SECURITY_STACKING */
+
+/*

? * Security blob size or offset data.
? */
?struct lsm_blob_sizes {

security/Makefile +0 -0
index f2d71cdb8e19..05e6d525b5a1 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_SECURITY_APPARMOR)		+=

apparmor/
?obj-$(CONFIG_SECURITY_YAMA) += yama/
?obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/
?obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o
+obj-$(CONFIG_SECURITY_STACKING) += stacking.o
?
?# Object integrity file lists
?subdir-$(CONFIG_INTEGRITY) += integrity

security/security.c +0 -0
index 6b979aa769ad..9d402d954cef 100644
@@ -199,6 +199,11 @@ bool __init security_module_enable(const char
 *lsm, const bool stacked)

?#endif
?}
?

security/security.c +5 -0
+/*
+ * Keep the order of major modules for mapping secids.
+ */
+static int lsm_next_major;
+

?/**
? * security_add_hooks - Add a modules hooks to the hook lists. ? * @hooks: the hooks to add

security/security.c +1 -0
@@ -211,9 +216,14 @@ void __init security_add_hooks(struct
 security_hook_list *hooks, int count,
 ?				char *lsm)
 ?{
 ?	int i;
+	int lsm_index = lsm_next_major++;

?

security/security.c +2 -0
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: Security module %s gets index %d\n", lsm,

lsm_index);
+#endif
? for (i = 0; i < count; i++) {
? hooks[i].lsm = lsm;

security/security.c +1 -0
+		hooks[i].lsm_index = lsm_index;

? list_add_tail_rcu(&hooks[i].list, hooks[i].head);
? }
? if (lsm_append(lsm, &lsm_names) < 0)

security/security.c +0 -0
@@ -1218,7 +1228,15 @@ EXPORT_SYMBOL(security_inode_listsecurity);

?
?void security_inode_getsecid(struct inode *inode, u32 *secid) ?{

security/security.c +6 -1
-	call_void_hook(inode_getsecid, inode, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.inode_getsecid,
list)

security/security.c +1 -0
+		hp->hook.inode_getsecid(inode, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_inode_copy_up(struct dentry *src, struct cred **new)

security/security.c +0 -0
@@ -1406,7 +1424,18 @@ void security_transfer_creds(struct cred
 *new,
 const struct cred *old)

?
?int security_kernel_act_as(struct cred *new, u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(kernel_act_as, 0, new, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.kernel_act_as,
list) {

security/security.c +1 -0
+		rc = hp->hook.kernel_act_as(new, secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_kernel_create_files_as(struct cred *new, struct inode *inode)

security/security.c +0 -0
@@ -1465,8 +1494,15 @@ int security_task_getsid(struct task_struct
 *p)

?
?void security_task_getsecid(struct task_struct *p, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(task_getsecid, p, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.task_getsecid,
list)

security/security.c +1 -0
+		hp->hook.task_getsecid(p, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_task_getsecid);
?

security/security.c +0 -0
@@ -1515,7 +1551,19 @@ int security_task_movememory(struct
 task_struct *p)

?int security_task_kill(struct task_struct *p, struct siginfo *info,
? int sig, u32 secid)
?{

security/security.c +7 -1
-	return call_int_hook(task_kill, 0, p, info, sig, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp, &security_hook_heads.task_kill,

list) {

security/security.c +2 -0
+		rc = hp->hook.task_kill(p, info, sig,
+					secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,

security/security.c +0 -0
@@ -1548,8 +1596,15 @@ int security_ipc_permission(struct
 kern_ipc_perm *ipcp, short flag)

?
?void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(ipc_getsecid, ipcp, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.ipc_getsecid,

list)

security/security.c +1 -0
+		hp->hook.ipc_getsecid(ipcp, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_msg_msg_alloc(struct msg_msg *msg)

security/security.c +0 -0
@@ -1840,15 +1895,42 @@ EXPORT_SYMBOL(security_ismaclabel);

?
?int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
?{

security/security.c +0 -1
-	return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid,

secdata,

security/security.c +10 -1
-				seclen);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -EOPNOTSUPP;
+
+	lsm_token_to_secids(secid, &secids);
+
+	/*
+	?* CBS - Return the first result regardless.
+	?*/
+	list_for_each_entry(hp,

&security_hook_heads.secid_to_secctx, list) {

security/security.c +1 -0
+		rc = hp->hook.secid_to_secctx(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +5 -0
+						secdata, seclen);
+		if (rc != -EOPNOTSUPP)
+			break;
+	}
+	return rc;

?}
?EXPORT_SYMBOL(security_secid_to_secctx);
?
?int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
?{

security/security.c +0 -2
-	*secid = 0;
-	return call_int_hook(secctx_to_secid, 0, secdata, seclen,

secid);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secctx_to_secid, list) {

security/security.c +2 -0
+		rc = hp->hook.secctx_to_secid(secdata, seclen,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+
+	*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?EXPORT_SYMBOL(security_secctx_to_secid);
?

security/security.c +0 -0
@@ -1977,10 +2059,26 @@ int

security_socket_getpeersec_stream(struct
socket *sock, char __user *optval,
? optval, optlen, len);
?}
?
-int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
+int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb,

security/security.c +1 -0
+				?????u32 *secid)

?{

security/security.c +0 -2
-	return call_int_hook(socket_getpeersec_dgram,
-ENOPROTOOPT,

sock,

security/security.c +7 -1
-			?????skb, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -ENOPROTOOPT;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.socket_getpeersec_dgram,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.socket_getpeersec_dgram(sock, skb,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +7 -0
+		if (rc)
+			break;
+	}
+
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?} ?EXPORT_SYMBOL(security_socket_getpeersec_dgram); ?

security/security.c +0 -0
@@ -2008,13 +2106,30 @@ EXPORT_SYMBOL(security_sk_clone);

?
?void security_sk_classify_flow(struct sock *sk, struct flowi *fl) ?{

security/security.c +6 -1
-	call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.sk_getsecid,

list)

security/security.c +1 -0
+		hp->hook.sk_getsecid(sk, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_sk_classify_flow);
?
-void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)

security/security.c +2 -0
+void security_req_classify_flow(const struct request_sock *req,
+				struct flowi *fl)

?{

security/security.c +6 -1
-	call_void_hook(req_classify_flow, req, fl);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.req_classify_flow, list)

security/security.c +1 -0
+		hp->hook.req_classify_flow(req, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_req_classify_flow);
?

security/security.c +0 -0
@@ -2045,7 +2160,20 @@ void security_inet_conn_established(struct
 sock *sk,

?
?int security_secmark_relabel_packet(u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(secmark_relabel_packet, 0, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secmark_relabel_packet,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.secmark_relabel_packet(
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?} ?EXPORT_SYMBOL(security_secmark_relabel_packet); ?

security/security.c +7 -1
@@ -2163,7 +2291,20 @@ EXPORT_SYMBOL(security_xfrm_state_alloc);
 ?int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
 ?				??????struct xfrm_sec_ctx *polsec,
 u32 secid)
 ?{
-	return call_int_hook(xfrm_state_alloc_acquire, 0, x,
 polsec,
 secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_state_alloc_acquire,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_alloc_acquire(x, polsec,
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_delete(struct xfrm_state *x)

security/security.c +0 -0
@@ -2179,7 +2320,19 @@ void security_xfrm_state_free(struct
 xfrm_state *x)

?
?int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
?{

security/security.c +0 -1
-	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid,

dir);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(fl_secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_policy_lookup, list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_policy_lookup(ctx,
+					secids.secid[hp-
Quoted 1 line

lsm_index],?

dir);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_pol_flow_match(struct xfrm_state *x,

security/security.c +1 -0
@@ -2187,6 +2340,7 @@ int security_xfrm_state_pol_flow_match(struct
 xfrm_state *x,
 ?				???????const struct flowi *fl)
 ?{
 ?	struct security_hook_list *hp;
+	struct lsm_secids secids;
 ?	int rc = 1;

?
? /*

security/security.c +0 -0
@@ -2198,9 +2352,12 @@ int

security_xfrm_state_pol_flow_match(struct
xfrm_state *x,
? ?* For speed optimization, we explicitly break the loop
rather than
? ?* using the macro
? ?*/

security/security.c +2 -0
+	lsm_token_to_secids(fl->flowi_secid, &secids);
+

? list_for_each_entry(hp,
&security_hook_heads.xfrm_state_pol_flow_match,

security/security.c +0 -2
-				list) {
-		rc = hp->hook.xfrm_state_pol_flow_match(x, xp,

fl);

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_pol_flow_match(x, xp,
+				secids.secid[hp->lsm_index]);

? break;
? }
? return rc;

security/security.c +0 -0
@@ -2208,15 +2365,41 @@ int

security_xfrm_state_pol_flow_match(struct
xfrm_state *x,
?
?int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid) ?{

security/security.c +0 -1
-	return call_int_hook(xfrm_decode_session, 0, skb, secid,

1);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 1);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?
?void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
?{

security/security.c +0 -1
-	int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl-
Quoted 1 line

flowi_secid,

security/security.c +5 -1
-				0);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);

?

security/security.c +1 -0
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 0);

security/security.c +3 -0
+		if (rc)
+			break;
+	}

? BUG_ON(rc);

security/security.c +1 -0
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_skb_classify_flow);
?

security/security.c +0 -0
@@ -2275,7 +2458,18 @@ void security_audit_rule_free(void *lsmrule)

?int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
? ??????struct audit_context *actx)
?{

security/security.c +0 -1
-	return call_int_hook(audit_rule_match, 0, secid, field,

op,
lsmrule,

security/security.c +7 -1
-				actx);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.audit_rule_match, list) {

security/security.c +1 -0
+		rc = hp->hook.audit_rule_match(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +1 -0
+						field, op,

lsmrule,
actx);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?#endif /* CONFIG_AUDIT */

security/selinux/hooks.c +0 -0
index 84d533335924..389f09ebd374 100644
@@ -100,6 +100,9 @@

?/* SECMARK reference count */
?static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); ?

security/selinux/hooks.c +3 -0
+/* Index into lsm_secids */
+static int selinux_secids_index;
+

?#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
?int selinux_enforcing;
?

security/selinux/hooks.c +0 -0
@@ -4610,6 +4613,11 @@ static int selinux_inet_sys_rcv_skb(struct

net
*ns, int ifindex,
? ????SECCLASS_NODE, NODE__RECVFROM, ad);
?}
?

security/selinux/hooks.c +3 -0
+static u32 selinux_secmark_to_secid(u32 token)
+{
+	return lsm_token_to_module_secid(token,

selinux_secids_index);

security/selinux/hooks.c +2 -0
+}
+

?static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
? ???????u16 family)
?{

security/selinux/hooks.c +0 -0
@@ -4629,7 +4637,9 @@ static int selinux_sock_rcv_skb_compat(struct
 sock *sk, struct sk_buff *skb,
 ?		return err;

?
? if (selinux_secmark_enabled()) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4703,7 +4713,9 @@ static int selinux_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?	}

?
? if (secmark_active) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4902,9 +4914,9 @@ static void
 selinux_secmark_refcount_dec(void)
 ?}

?
?static void selinux_req_classify_flow(const struct request_sock *req,

security/selinux/hooks.c +1 -1
-				??????struct flowi *fl)
+				??????u32 *fl_secid)

?{

security/selinux/hooks.c +1 -1
-	fl->flowi_secid = req->secid;
+	*fl_secid = req->secid;

?}
?
?static int selinux_tun_dev_alloc_security(void **security)

security/selinux/hooks.c +0 -0
@@ -5066,7 +5078,8 @@ static unsigned int selinux_ip_forward(struct
 sk_buff *skb,
 ?	}

?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET,
PACKET__FORWARD_IN, &ad))
? return NF_DROP;
?

security/selinux/hooks.c +0 -0
@@ -5178,7 +5191,8 @@ static unsigned int
 selinux_ip_postroute_compat(struct sk_buff *skb,
 ?		return NF_DROP;

?
? if (selinux_secmark_enabled())

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(sksec->sid, skb->secmark,
+		if (avc_has_perm(sksec->sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, PACKET__SEND,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -5301,7 +5315,8 @@ static unsigned int

selinux_ip_postroute(struct
sk_buff *skb,
? return NF_DROP;
?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, secmark_perm,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -6339,6 +6354,8 @@ static __init int selinux_init(void)

?
? security_add_hooks(selinux_hooks,
ARRAY_SIZE(selinux_hooks),
"selinux");
?

security/selinux/hooks.c +2 -0
+	selinux_secids_index = selinux_hooks[0].lsm_index;
+

? if (avc_add_callback(selinux_netcache_avc_callback,
AVC_CALLBACK_RESET))
? panic("SELinux: Unable to register AVC netcache
callback\n");
?

security/selinux/include/xfrm.h +0 -0
index 1450f85b946d..475a328248b3 100644
@@ -25,7 +25,7 @@ int selinux_xfrm_state_delete(struct xfrm_state
 *x);

?int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/include/xfrm.h +1 -1
-				??????const struct flowi *fl);
+				??????u32 fl_secid);

?
?#ifdef CONFIG_SECURITY_NETWORK_XFRM
?extern atomic_t selinux_xfrm_refcount;

security/selinux/xfrm.c +0 -0
index 789d07bd900f..d71e2c32b5da 100644
@@ -174,7 +174,7 @@ int selinux_xfrm_policy_lookup(struct
 xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)

? */
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/xfrm.c +1 -1
-				??????const struct flowi *fl)
+				??????u32 fl_secid)

?{
? u32 state_sid;
?

security/selinux/xfrm.c +0 -0
@@ -196,13 +196,13 @@ int selinux_xfrm_state_pol_flow_match(struct
 xfrm_state *x,

?
? state_sid = x->security->ctx_sid;
?

security/selinux/xfrm.c +1 -1
-	if (fl->flowi_secid != state_sid)
+	if (fl_secid != state_sid)

? return 0;
?
? /* We don't need a separate SA Vs. policy polmatch check
since the SA
? ?* is now of the same label as the flow and a flow Vs.
policy polmatch
? ?* check had already happened in
selinux_xfrm_policy_lookup() above. */

security/selinux/xfrm.c +1 -1
-	return (avc_has_perm(fl->flowi_secid, state_sid,
+	return (avc_has_perm(fl_secid, state_sid,

? ????SECCLASS_ASSOCIATION,
ASSOCIATION__SENDTO,
? ????NULL) ? 0 : 1);
?}

security/smack/smack.h +0 -0
index e7611de071f1..e9fd586e0ec1 100644
@@ -328,6 +328,7 @@ void smk_destroy_label_list(struct list_head
 *list);

? * Shared data.
? */
?extern int smack_enabled;
+extern int smack_secids_index;
?extern int smack_cipso_direct;
?extern int smack_cipso_mapped;
?extern struct smack_known *smack_net_ambient;

security/smack/smack_lsm.c +0 -0
index 1e9ab7bdaf55..51daf9b05f17 100644
@@ -57,6 +57,7 @@ static LIST_HEAD(smk_ipv6_port_list);

?#endif
?static struct kmem_cache *smack_inode_cache; ?int smack_enabled;
+int smack_secids_index;
?
?static const match_table_t smk_mount_tokens = {
? {Opt_fsdefault, SMK_FSDEFAULT "%s"},

security/smack/smack_lsm.c +0 -0
@@ -3788,6 +3789,13 @@ static int smk_skb_to_addr_ipv6(struct

sk_buff
*skb, struct sockaddr_in6 *sip)
?}
?#endif /* CONFIG_IPV6 */
?

security/smack/smack_lsm.c +4 -0
+#ifdef CONFIG_SECURITY_SMACK_NETFILTER
+static u32 smk_of_secmark(u32 secmark)
+{
+	return lsm_token_to_module_secid(secmark,

smack_secids_index);

security/smack/smack_lsm.c +3 -0
+}
+#endif
+

?/**
? * smack_socket_sock_rcv_skb - Smack packet delivery access check ? * @sk: socket

security/smack/smack_lsm.c +0 -0
@@ -3819,7 +3827,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)

? ?* The secmark is assumed to reflect policy
better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -3864,7 +3872,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?			break;

?#ifdef SMACK_IPV6_SECMARK_LABELING
? if (skb && skb->secmark != 0)

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? else
? skp = smack_ipv6host_label(&sadd);
? if (skp == NULL)

security/smack/smack_lsm.c +0 -0
@@ -3962,7 +3970,7 @@ static int

smack_socket_getpeersec_dgram(struct
socket *sock,
? break;
? case PF_INET:
?#ifdef CONFIG_SECURITY_SMACK_NETFILTER

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

? if (s != 0)
? break;
?#endif

security/smack/smack_lsm.c +0 -0
@@ -3981,7 +3989,7 @@ static int

smack_socket_getpeersec_dgram(struct
socket *sock,
? break;
? case PF_INET6:
?#ifdef SMACK_IPV6_SECMARK_LABELING

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

?#endif
? break;
? }

security/smack/smack_lsm.c +0 -0
@@ -4060,7 +4068,7 @@ static int smack_inet_conn_request(struct

sock
*sk, struct sk_buff *skb,
? ?* The secmark is assumed to reflect policy better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-		skp = smack_from_secid(skb->secmark);
+		skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -4650,6 +4658,7 @@ static __init int smack_init(void)

? ?* Register with LSM
? ?*/
? security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks),
"smack");

security/smack/smack_lsm.c +1 -0
+	smack_secids_index = smack_hooks[0].lsm_index;

? smack_enabled = 1;
?
? pr_info("Smack:??Initializing.\n");

security/smack/smack_netfilter.c +0 -0
index a5155295551f..510661ba6c16 100644
@@ -23,6 +23,19 @@

?
?#if IS_ENABLED(CONFIG_IPV6)
?

security/smack/smack_netfilter.c +13 -0
+/*
+ * Reinvestigate this soon?
+ *
+ */
+static u32 smack_to_secmark(u32 secid)
+{
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+	secids.secid[smack_secids_index] = secid;
+	return lsm_secids_to_token(&secids);
+}
+

?static unsigned int smack_ipv6_output(void *priv,
? struct sk_buff *skb,
? const struct nf_hook_state
*state)

security/smack/smack_netfilter.c +1 -1
@@ -34,7 +47,7 @@ static unsigned int smack_ipv6_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/smack/smack_netfilter.c +1 -1
@@ -52,7 +65,7 @@ static unsigned int smack_ipv4_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/stacking.c new file +11 -0
new file mode 100644
index 000000000000..65276cd695de
@@ -0,0 +1,165 @@
+/*
+ *??Maintain a mapping between the secid used in networking
+ *??and the set of secids used by the security modules.
+ *
+ *??Author:
+ *	Casey Schaufler <[email protected]>
+ *
+ *??Copyright (C) 2017 Casey Schaufler <[email protected]>
+ *??Copyright (C) 2017 Intel Corporation.
+ *
+ *	This program is free software; you can redistribute it

and/or modify
+ * it under the terms of the GNU General Public License
version 2,
+ *??????as published by the Free Software Foundation.
+ */

security/stacking.c new file +5 -0
+
+#include <linux/lsm_hooks.h>
+
+struct token_entry {
+	int			used;	/* relative age of

the entry */

security/stacking.c new file +2 -0
+	u32			token;	/* token value */
+	struct lsm_secids	secids;	/* secids mapped

to
this token */

security/stacking.c new file +19 -0
+};
+
+/*
+ * Add an entry to the table when asked for a mapping that
+ * isn't already present. If the table is full throw away the
+ * least recently used entry. If the entry is present undate
+ * when it was used.
+ */
+#define TOKEN_AGE_LIMIT (MAX_INT >> 2)
+#define TOKEN_LIMIT 0x20000000
+#define TOKEN_SET_SIZE 200
+#define TOKEN_BIT 0x80000000
+int token_used;
+u32 token_next;
+struct lsm_secids null_secids;
+struct token_entry token_set[TOKEN_SET_SIZE];
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+static void report_token(const char *msg, const struct token_entry

*te)

security/stacking.c new file +4 -0
+{
+	int i;
+
+	pr_info("LSM: %s token=%08x %u,%u,%u,%u,%u,%u,%u,%u\n",

msg,
te->token,

security/stacking.c new file +1 -0
+		te->secids.secid[0], te->secids.secid[1], te-
Quoted 1 line

secids.secid[2],

security/stacking.c new file +1 -0
+		te->secids.secid[3], te->secids.secid[4], te-
Quoted 1 line

secids.secid[5],

security/stacking.c new file +4 -0
+		te->secids.secid[6], te->secids.secid[7]);
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (te->secids.secid[i] & TOKEN_BIT)
+			pr_info("LSM: module %d provided a

token.\n", i);

security/stacking.c new file +3 -0
+}
+#else
+static inline void report_token(const char *msg, const struct

token_entry *te)

security/stacking.c new file +8 -0
+{
+}
+#endif
+
+static int next_used(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token use overflow - safe

reset\n");

security/stacking.c new file +9 -0
+		token_used = 0;
+	}
+	return ++token_used;
+}
+
+static u32 next_token(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token overflow - safe

reset\n");

security/stacking.c new file +15 -0
+		token_next = 0;
+	}
+	return ++token_next | TOKEN_BIT;
+}
+
+u32 lsm_secids_to_token(const struct lsm_secids *secids)
+{
+	int i;
+	int j;
+	int old;
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (secids->secid[i] & TOKEN_BIT)
+			pr_info("LSM: %s secid[%d]=%08x has token

bit\n",

security/stacking.c new file +14 -0
+				__func__, i, secids->secid[i]);
+#endif
+
+	/*
+	?* If none of the secids are set whoever sent this here
+	?* was thinking "0".
+	?*/
+	if (!memcmp(secids, &null_secids, sizeof(*secids)))
+		return 0;
+
+	for (i = 0; i < TOKEN_SET_SIZE; i++) {
+		if (token_set[i].token == 0)
+			break;
+		if (!memcmp(secids, &token_set[i].secids,

sizeof(*secids))) {

security/stacking.c new file +23 -0
+			token_set[i].used = next_used();
+			return token_set[i].token;
+		}
+	}
+	if (i == TOKEN_SET_SIZE) {
+		old = token_used;
+		for (j = 0; j < TOKEN_SET_SIZE; j++) {
+			if (token_set[j].used < old) {
+				old = token_set[j].used;
+				i = j;
+			}
+		}
+	}
+	token_set[i].secids = *secids;
+	token_set[i].token = next_token();
+	token_set[i].used = next_used();
+
+	report_token("new", &token_set[i]);
+
+	return token_set[i].token;
+}
+
+void lsm_token_to_secids(const u32 token, struct lsm_secids

*secids)

security/stacking.c new file +8 -0
+{
+	int i;
+	struct lsm_secids fudge;
+
+	if (token) {
+		if (!(token & TOKEN_BIT)) {
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+			pr_info("LSM: %s token=%08x has no token

bit\n",

security/stacking.c new file +18 -0
+				__func__, token);
+#endif
+			for (i = 0; i < LSM_MAX_MAJOR; i++)
+				fudge.secid[i] = token;
+			*secids = fudge;
+			return;
+		}
+		for (i = 0; i < TOKEN_SET_SIZE; i++) {
+			if (token_set[i].token == 0)
+				break;
+			if (token_set[i].token == token) {
+				*secids = token_set[i].secids;
+				token_set[i].used = next_used();
+				return;
+			}
+		}
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: %s token=%u was not found\n", __func__,

token);

security/stacking.c new file +11 -0
+#endif
+	}
+	*secids = null_secids;
+}
+
+u32 lsm_token_to_module_secid(const u32 token, int lsm)
+{
+	struct lsm_secids secids;
+
+????????lsm_token_to_secids(token, &secids);
+	return secids.secid[lsm];
Quoted 8 lines

security/stacking.c line 165

Quoted 6 lines
security/stacking.c new file +6 -0
+}
+
+void lsm_secids_init(struct lsm_secids *secids)
+{
+	*secids = null_secids;
+}

ENOLOCKING

Also, how do you know that it is safe to reclaim the least recently used entry? Previously issued tokens/secids may have been cached in other kernel data structures, and you have no guarantees that they are no longer in use there.

A simple find / -ls on a Fedora system will roll over your token set size many times.

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

PATCH 3/11 LSM: Manage file security blobs

In reply to Casey Schaufler
Quoted 44 lines · Casey Schaufler
Quoted 14 lines · Stephen Smalley
Quoted 13 lines · Casey Schaufler

Subject: [PATCH 03/11] LSM: Manage file security blobs

Move the management of file security blobs from the individual security modules to the security infrastructure. The security modules
using file blobs have been updated accordingly. Modules are required
to identify the space they need at module initialization. In some cases a module no longer needs to supply a blob management hook, in
which case the hook has been removed.

<SNIP>

Quoted 17 lines
Quoted 17 lines

?/**
? * selinux_secmark_enabled - Check to see if SECMARK is currently
enabled

(quoted diff) +0 -0
@@ -359,27 +358,15 @@ static void inode_free_security(struct

inode
*inode)
?
?static int file_alloc_security(struct file *file) ?{

security/selinux/hooks.c +1 -1
-	struct file_security_struct *fsec;
+	struct file_security_struct *fsec = selinux_file(file);

? u32 sid = current_sid();
?

(quoted diff) +0 -1
-	fsec = kmem_cache_zalloc(file_security_cache,

GFP_KERNEL);

security/selinux/hooks.c +0 -1
-	if (!fsec)
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +0 -2
-		return -ENOMEM;
-

NAK. See commit 63205654c0e05e5ffa1c6eef2fbef21dcabd2185 for why this
was changed from a simple kzalloc() to using its own cache; we don't
want to regress in this regard.

Quoted 4 lines

I was somewhat expecting this. Is there any reason that using kmem_cache_zalloc() in the infrastructure would not address the issue? If this is a win for SELinux it should be a win for any module that uses file blobs.

Whether or not it will truly be a win will depend on the size of the file security blob for that module (or module stack). But yes, you should be able to do the same in the infrastructure.

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#16 lore raw

PATCH 7/11 LSM: Shared secids by token

In reply to Stephen Smalley
Quoted 1,116 lines · Stephen Smalley
Quoted 1,115 lines · Stephen Smalley
Quoted 1,114 lines · Casey Schaufler

Subject: [PATCH 07/11] LSM: Shared secids by token

Introduces a mechanism for mapping a set of security module secids to and from a "token". The module interfaces are changed to generally hide the mechanism from both the security modules and the callers of the security hooks.

---

?include/linux/lsm_hooks.h????????|??54 ++++++++-
?security/Makefile????????????????|???1 +
?security/security.c??????????????| 248
++++++++++++++++++++++++++++++++++-----
?security/selinux/hooks.c?????????|??31 +++--
?security/selinux/include/xfrm.h??|???2 +-
?security/selinux/xfrm.c??????????|???6 +-
?security/smack/smack.h???????????|???1 +
?security/smack/smack_lsm.c???????|??19 ++-
?security/smack/smack_netfilter.c |??17 ++-
?security/stacking.c??????????????| 165 ++++++++++++++++++++++++++

?10 files changed, 497 insertions(+), 47 deletions(-) ?create mode 100644 security/stacking.c

include/linux/lsm_hooks.h +1 -1
index dfe4dab1ff8d..75d95854f2ed 100644
@@ -1627,7 +1627,7 @@ union security_list_options {
 ?	void (*secmark_refcount_inc)(void);
 ?	void (*secmark_refcount_dec)(void);
 ?	void (*req_classify_flow)(const struct request_sock *req,
-					struct flowi *fl);
+					u32 *fl_secid);
 ?	int (*tun_dev_alloc_security)(void **security);
 ?	void (*tun_dev_free_security)(void *security);
 ?	int (*tun_dev_create)(void);
@@ -1663,7 +1663,7 @@ union security_list_options {
 ?					u8 dir);
 ?	int (*xfrm_state_pol_flow_match)(struct xfrm_state *x,

? struct xfrm_policy
*xp,

include/linux/lsm_hooks.h +0 -1
-						const struct flowi

*fl);

include/linux/lsm_hooks.h +1 -0
+						u32 fl_secid);

? int (*xfrm_decode_session)(struct sk_buff *skb, u32
*secid,
int ckall);
?#endif /* CONFIG_SECURITY_NETWORK_XFRM */
?

include/linux/lsm_hooks.h +1 -0
@@ -1916,9 +1916,59 @@ struct security_hook_list {
 ?	struct list_head		*head;
 ?	union security_list_options	hook;
 ?	char				*lsm;
+	int				lsm_index;
 ?} __randomize_layout;

?
?/*
+ * The maximum number of major security modules.
+ * Used to avoid excessive memory management while
+ * mapping global and module specific secids.
+ *
+ * Currently SELinux, Smack, AppArmor, TOMOYO
+ * Oh, but Casey needs to come up with the right way
+ * to identify a "major" module, so use the total number
+ * of modules (including minor) for now.
+ * Minor: Capability, Yama, LoadPin
+ */

include/linux/lsm_hooks.h +9 -0
+#define	LSM_MAX_MAJOR	8
+
+#ifdef CONFIG_SECURITY_STACKING
+struct lsm_secids {
+	u32	secid[LSM_MAX_MAJOR];
+};
+
+extern u32 lsm_secids_to_token(const struct lsm_secids *secids);
+extern void lsm_token_to_secids(const u32 token, struct lsm_secids

*secids);

include/linux/lsm_hooks.h +8 -0
+extern u32 lsm_token_to_module_secid(const u32 token, int lsm);
+extern void lsm_secids_init(struct lsm_secids *secids);
+#else /* CONFIG_SECURITY_STACKING */
+struct lsm_secids {
+	u32	secid;
+};
+
+static inline u32 lsm_secids_to_token(const struct lsm_secids

*secids)

include/linux/lsm_hooks.h +11 -0
+{
+	return secids->secid;
+}
+
+static inline void lsm_token_to_secids(const u32 token,
+				???????struct lsm_secids *secids)
+{
+	secids->secid = token;
+}
+
+static inline u32 lsm_token_to_module_secid(const u32 token, int

lsm)

include/linux/lsm_hooks.h +11 -0
+{
+	return token;
+}
+
+static inline void lsm_secids_init(struct lsm_secids *secids)
+{
+	secids->secid = 0;
+}
+#endif /* CONFIG_SECURITY_STACKING */
+
+/*

? * Security blob size or offset data.
? */
?struct lsm_blob_sizes {

security/Makefile +0 -0
index f2d71cdb8e19..05e6d525b5a1 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_SECURITY_APPARMOR)		+=

apparmor/
?obj-$(CONFIG_SECURITY_YAMA) += yama/
?obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/
?obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o
+obj-$(CONFIG_SECURITY_STACKING) += stacking.o
?
?# Object integrity file lists
?subdir-$(CONFIG_INTEGRITY) += integrity

security/security.c +0 -0
index 6b979aa769ad..9d402d954cef 100644
@@ -199,6 +199,11 @@ bool __init security_module_enable(const char
 *lsm, const bool stacked)

?#endif
?}
?

security/security.c +5 -0
+/*
+ * Keep the order of major modules for mapping secids.
+ */
+static int lsm_next_major;
+

?/**
? * security_add_hooks - Add a modules hooks to the hook lists. ? * @hooks: the hooks to add

security/security.c +1 -0
@@ -211,9 +216,14 @@ void __init security_add_hooks(struct
 security_hook_list *hooks, int count,
 ?				char *lsm)
 ?{
 ?	int i;
+	int lsm_index = lsm_next_major++;

?

security/security.c +2 -0
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: Security module %s gets index %d\n", lsm,

lsm_index);
+#endif
? for (i = 0; i < count; i++) {
? hooks[i].lsm = lsm;

security/security.c +1 -0
+		hooks[i].lsm_index = lsm_index;

? list_add_tail_rcu(&hooks[i].list, hooks[i].head);
? }
? if (lsm_append(lsm, &lsm_names) < 0)

security/security.c +0 -0
@@ -1218,7 +1228,15 @@ EXPORT_SYMBOL(security_inode_listsecurity);

?
?void security_inode_getsecid(struct inode *inode, u32 *secid) ?{

security/security.c +6 -1
-	call_void_hook(inode_getsecid, inode, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.inode_getsecid,
list)

security/security.c +1 -0
+		hp->hook.inode_getsecid(inode, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_inode_copy_up(struct dentry *src, struct cred **new)

security/security.c +0 -0
@@ -1406,7 +1424,18 @@ void security_transfer_creds(struct cred
 *new,
 const struct cred *old)

?
?int security_kernel_act_as(struct cred *new, u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(kernel_act_as, 0, new, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.kernel_act_as,
list) {

security/security.c +1 -0
+		rc = hp->hook.kernel_act_as(new, secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_kernel_create_files_as(struct cred *new, struct inode *inode)

security/security.c +0 -0
@@ -1465,8 +1494,15 @@ int security_task_getsid(struct task_struct
 *p)

?
?void security_task_getsecid(struct task_struct *p, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(task_getsecid, p, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.task_getsecid,
list)

security/security.c +1 -0
+		hp->hook.task_getsecid(p, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_task_getsecid);
?

security/security.c +0 -0
@@ -1515,7 +1551,19 @@ int security_task_movememory(struct
 task_struct *p)

?int security_task_kill(struct task_struct *p, struct siginfo *info,
? int sig, u32 secid)
?{

security/security.c +7 -1
-	return call_int_hook(task_kill, 0, p, info, sig, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp, &security_hook_heads.task_kill,

list) {

security/security.c +2 -0
+		rc = hp->hook.task_kill(p, info, sig,
+					secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,

security/security.c +0 -0
@@ -1548,8 +1596,15 @@ int security_ipc_permission(struct
 kern_ipc_perm *ipcp, short flag)

?
?void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) ?{

security/security.c +6 -2
-	*secid = 0;
-	call_void_hook(ipc_getsecid, ipcp, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.ipc_getsecid,

list)

security/security.c +1 -0
+		hp->hook.ipc_getsecid(ipcp, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	*secid = lsm_secids_to_token(&secids);

?}
?
?int security_msg_msg_alloc(struct msg_msg *msg)

security/security.c +0 -0
@@ -1840,15 +1895,42 @@ EXPORT_SYMBOL(security_ismaclabel);

?
?int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
?{

security/security.c +0 -1
-	return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid,

secdata,

security/security.c +10 -1
-				seclen);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -EOPNOTSUPP;
+
+	lsm_token_to_secids(secid, &secids);
+
+	/*
+	?* CBS - Return the first result regardless.
+	?*/
+	list_for_each_entry(hp,

&security_hook_heads.secid_to_secctx, list) {

security/security.c +1 -0
+		rc = hp->hook.secid_to_secctx(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +5 -0
+						secdata, seclen);
+		if (rc != -EOPNOTSUPP)
+			break;
+	}
+	return rc;

?}
?EXPORT_SYMBOL(security_secid_to_secctx);
?
?int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
?{

security/security.c +0 -2
-	*secid = 0;
-	return call_int_hook(secctx_to_secid, 0, secdata, seclen,

secid);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secctx_to_secid, list) {

security/security.c +2 -0
+		rc = hp->hook.secctx_to_secid(secdata, seclen,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+
+	*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?EXPORT_SYMBOL(security_secctx_to_secid);
?

security/security.c +0 -0
@@ -1977,10 +2059,26 @@ int

security_socket_getpeersec_stream(struct
socket *sock, char __user *optval,
? optval, optlen, len);
?}
?
-int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
+int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb,

security/security.c +1 -0
+				?????u32 *secid)

?{

security/security.c +0 -2
-	return call_int_hook(socket_getpeersec_dgram,
-ENOPROTOOPT,

sock,

security/security.c +7 -1
-			?????skb, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = -ENOPROTOOPT;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.socket_getpeersec_dgram,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.socket_getpeersec_dgram(sock, skb,
+						&secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +7 -0
+		if (rc)
+			break;
+	}
+
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?} ?EXPORT_SYMBOL(security_socket_getpeersec_dgram); ?

security/security.c +0 -0
@@ -2008,13 +2106,30 @@ EXPORT_SYMBOL(security_sk_clone);

?
?void security_sk_classify_flow(struct sock *sk, struct flowi *fl) ?{

security/security.c +6 -1
-	call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp, &security_hook_heads.sk_getsecid,

list)

security/security.c +1 -0
+		hp->hook.sk_getsecid(sk, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_sk_classify_flow);
?
-void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)

security/security.c +2 -0
+void security_req_classify_flow(const struct request_sock *req,
+				struct flowi *fl)

?{

security/security.c +6 -1
-	call_void_hook(req_classify_flow, req, fl);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.req_classify_flow, list)

security/security.c +1 -0
+		hp->hook.req_classify_flow(req, &secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +2 -0
+
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_req_classify_flow);
?

security/security.c +0 -0
@@ -2045,7 +2160,20 @@ void security_inet_conn_established(struct
 sock *sk,

?
?int security_secmark_relabel_packet(u32 secid) ?{

security/security.c +7 -1
-	return call_int_hook(secmark_relabel_packet, 0, secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.secmark_relabel_packet,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.secmark_relabel_packet(
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?} ?EXPORT_SYMBOL(security_secmark_relabel_packet); ?

security/security.c +7 -1
@@ -2163,7 +2291,20 @@ EXPORT_SYMBOL(security_xfrm_state_alloc);
 ?int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
 ?				??????struct xfrm_sec_ctx *polsec,
 u32 secid)
 ?{
-	return call_int_hook(xfrm_state_alloc_acquire, 0, x,
 polsec,
 secid);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_state_alloc_acquire,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_alloc_acquire(x, polsec,
+						secids.secid[hp-
Quoted 1 line

lsm_index]);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_delete(struct xfrm_state *x)

security/security.c +0 -0
@@ -2179,7 +2320,19 @@ void security_xfrm_state_free(struct
 xfrm_state *x)

?
?int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
?{

security/security.c +0 -1
-	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid,

dir);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(fl_secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_policy_lookup, list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_policy_lookup(ctx,
+					secids.secid[hp-
Quoted 1 line

lsm_index],?

dir);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?
?int security_xfrm_state_pol_flow_match(struct xfrm_state *x,

security/security.c +1 -0
@@ -2187,6 +2340,7 @@ int security_xfrm_state_pol_flow_match(struct
 xfrm_state *x,
 ?				???????const struct flowi *fl)
 ?{
 ?	struct security_hook_list *hp;
+	struct lsm_secids secids;
 ?	int rc = 1;

?
? /*

security/security.c +0 -0
@@ -2198,9 +2352,12 @@ int

security_xfrm_state_pol_flow_match(struct
xfrm_state *x,
? ?* For speed optimization, we explicitly break the loop
rather than
? ?* using the macro
? ?*/

security/security.c +2 -0
+	lsm_token_to_secids(fl->flowi_secid, &secids);
+

? list_for_each_entry(hp,
&security_hook_heads.xfrm_state_pol_flow_match,

security/security.c +0 -2
-				list) {
-		rc = hp->hook.xfrm_state_pol_flow_match(x, xp,

fl);

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_state_pol_flow_match(x, xp,
+				secids.secid[hp->lsm_index]);

? break;
? }
? return rc;

security/security.c +0 -0
@@ -2208,15 +2365,41 @@ int

security_xfrm_state_pol_flow_match(struct
xfrm_state *x,
?
?int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid) ?{

security/security.c +0 -1
-	return call_int_hook(xfrm_decode_session, 0, skb, secid,

1);

security/security.c +7 -0
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 1);

security/security.c +6 -0
+		if (rc)
+			break;
+	}
+	if (!rc)
+		*secid = lsm_secids_to_token(&secids);
+	return rc;

?}
?
?void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
?{

security/security.c +0 -1
-	int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl-
Quoted 1 line

flowi_secid,

security/security.c +5 -1
-				0);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_secids_init(&secids);

?

security/security.c +1 -0
+	list_for_each_entry(hp,

&security_hook_heads.xfrm_decode_session,

security/security.c +1 -0
+									

list) {

security/security.c +2 -0
+		rc = hp->hook.xfrm_decode_session(skb,
+					&secids.secid[hp-
Quoted 1 line

lsm_index], 0);

security/security.c +3 -0
+		if (rc)
+			break;
+	}

? BUG_ON(rc);

security/security.c +1 -0
+	fl->flowi_secid = lsm_secids_to_token(&secids);

?}
?EXPORT_SYMBOL(security_skb_classify_flow);
?

security/security.c +0 -0
@@ -2275,7 +2458,18 @@ void security_audit_rule_free(void *lsmrule)

?int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
? ??????struct audit_context *actx)
?{

security/security.c +0 -1
-	return call_int_hook(audit_rule_match, 0, secid, field,

op,
lsmrule,

security/security.c +7 -1
-				actx);
+	struct security_hook_list *hp;
+	struct lsm_secids secids;
+	int rc = 0;
+
+	lsm_token_to_secids(secid, &secids);
+
+	list_for_each_entry(hp,

&security_hook_heads.audit_rule_match, list) {

security/security.c +1 -0
+		rc = hp->hook.audit_rule_match(secids.secid[hp-
Quoted 1 line

lsm_index],

security/security.c +1 -0
+						field, op,

lsmrule,
actx);

security/security.c +4 -0
+		if (rc)
+			break;
+	}
+	return rc;

?}
?#endif /* CONFIG_AUDIT */

security/selinux/hooks.c +0 -0
index 84d533335924..389f09ebd374 100644
@@ -100,6 +100,9 @@

?/* SECMARK reference count */
?static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); ?

security/selinux/hooks.c +3 -0
+/* Index into lsm_secids */
+static int selinux_secids_index;
+

?#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
?int selinux_enforcing;
?

security/selinux/hooks.c +0 -0
@@ -4610,6 +4613,11 @@ static int selinux_inet_sys_rcv_skb(struct

net
*ns, int ifindex,
? ????SECCLASS_NODE, NODE__RECVFROM, ad);
?}
?

security/selinux/hooks.c +3 -0
+static u32 selinux_secmark_to_secid(u32 token)
+{
+	return lsm_token_to_module_secid(token,

selinux_secids_index);

security/selinux/hooks.c +2 -0
+}
+

?static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
? ???????u16 family)
?{

security/selinux/hooks.c +0 -0
@@ -4629,7 +4637,9 @@ static int selinux_sock_rcv_skb_compat(struct
 sock *sk, struct sk_buff *skb,
 ?		return err;

?
? if (selinux_secmark_enabled()) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4703,7 +4713,9 @@ static int selinux_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?	}

?
? if (secmark_active) {

security/selinux/hooks.c +0 -1
-		err = avc_has_perm(sk_sid, skb->secmark,

SECCLASS_PACKET,

security/selinux/hooks.c +2 -0
+		err = avc_has_perm(sk_sid,
+				???selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

security/selinux/hooks.c +1 -0
+				???SECCLASS_PACKET,

? ???PACKET__RECV, &ad);
? if (err)
? return err;

security/selinux/hooks.c +0 -0
@@ -4902,9 +4914,9 @@ static void
 selinux_secmark_refcount_dec(void)
 ?}

?
?static void selinux_req_classify_flow(const struct request_sock *req,

security/selinux/hooks.c +1 -1
-				??????struct flowi *fl)
+				??????u32 *fl_secid)

?{

security/selinux/hooks.c +1 -1
-	fl->flowi_secid = req->secid;
+	*fl_secid = req->secid;

?}
?
?static int selinux_tun_dev_alloc_security(void **security)

security/selinux/hooks.c +0 -0
@@ -5066,7 +5078,8 @@ static unsigned int selinux_ip_forward(struct
 sk_buff *skb,
 ?	}

?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET,
PACKET__FORWARD_IN, &ad))
? return NF_DROP;
?

security/selinux/hooks.c +0 -0
@@ -5178,7 +5191,8 @@ static unsigned int
 selinux_ip_postroute_compat(struct sk_buff *skb,
 ?		return NF_DROP;

?
? if (selinux_secmark_enabled())

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(sksec->sid, skb->secmark,
+		if (avc_has_perm(sksec->sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, PACKET__SEND,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -5301,7 +5315,8 @@ static unsigned int

selinux_ip_postroute(struct
sk_buff *skb,
? return NF_DROP;
?
? if (secmark_active)

security/selinux/hooks.c +2 -1
-		if (avc_has_perm(peer_sid, skb->secmark,
+		if (avc_has_perm(peer_sid,
+				?selinux_secmark_to_secid(skb-
Quoted 1 line

secmark),

? ?SECCLASS_PACKET, secmark_perm,
&ad))
? return NF_DROP_ERR(-ECONNREFUSED);
?

security/selinux/hooks.c +0 -0
@@ -6339,6 +6354,8 @@ static __init int selinux_init(void)

?
? security_add_hooks(selinux_hooks,
ARRAY_SIZE(selinux_hooks),
"selinux");
?

security/selinux/hooks.c +2 -0
+	selinux_secids_index = selinux_hooks[0].lsm_index;
+

? if (avc_add_callback(selinux_netcache_avc_callback,
AVC_CALLBACK_RESET))
? panic("SELinux: Unable to register AVC netcache
callback\n");
?

security/selinux/include/xfrm.h +0 -0
index 1450f85b946d..475a328248b3 100644
@@ -25,7 +25,7 @@ int selinux_xfrm_state_delete(struct xfrm_state
 *x);

?int selinux_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir);
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/include/xfrm.h +1 -1
-				??????const struct flowi *fl);
+				??????u32 fl_secid);

?
?#ifdef CONFIG_SECURITY_NETWORK_XFRM
?extern atomic_t selinux_xfrm_refcount;

security/selinux/xfrm.c +0 -0
index 789d07bd900f..d71e2c32b5da 100644
@@ -174,7 +174,7 @@ int selinux_xfrm_policy_lookup(struct
 xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)

? */
?int selinux_xfrm_state_pol_flow_match(struct xfrm_state *x,
? ??????struct xfrm_policy *xp,

security/selinux/xfrm.c +1 -1
-				??????const struct flowi *fl)
+				??????u32 fl_secid)

?{
? u32 state_sid;
?

security/selinux/xfrm.c +0 -0
@@ -196,13 +196,13 @@ int selinux_xfrm_state_pol_flow_match(struct
 xfrm_state *x,

?
? state_sid = x->security->ctx_sid;
?

security/selinux/xfrm.c +1 -1
-	if (fl->flowi_secid != state_sid)
+	if (fl_secid != state_sid)

? return 0;
?
? /* We don't need a separate SA Vs. policy polmatch check
since the SA
? ?* is now of the same label as the flow and a flow Vs.
policy polmatch
? ?* check had already happened in
selinux_xfrm_policy_lookup() above. */

security/selinux/xfrm.c +1 -1
-	return (avc_has_perm(fl->flowi_secid, state_sid,
+	return (avc_has_perm(fl_secid, state_sid,

? ????SECCLASS_ASSOCIATION,
ASSOCIATION__SENDTO,
? ????NULL) ? 0 : 1);
?}

security/smack/smack.h +0 -0
index e7611de071f1..e9fd586e0ec1 100644
@@ -328,6 +328,7 @@ void smk_destroy_label_list(struct list_head
 *list);

? * Shared data.
? */
?extern int smack_enabled;
+extern int smack_secids_index;
?extern int smack_cipso_direct;
?extern int smack_cipso_mapped;
?extern struct smack_known *smack_net_ambient;

security/smack/smack_lsm.c +0 -0
index 1e9ab7bdaf55..51daf9b05f17 100644
@@ -57,6 +57,7 @@ static LIST_HEAD(smk_ipv6_port_list);

?#endif
?static struct kmem_cache *smack_inode_cache; ?int smack_enabled;
+int smack_secids_index;
?
?static const match_table_t smk_mount_tokens = {
? {Opt_fsdefault, SMK_FSDEFAULT "%s"},

security/smack/smack_lsm.c +0 -0
@@ -3788,6 +3789,13 @@ static int smk_skb_to_addr_ipv6(struct

sk_buff
*skb, struct sockaddr_in6 *sip)
?}
?#endif /* CONFIG_IPV6 */
?

security/smack/smack_lsm.c +4 -0
+#ifdef CONFIG_SECURITY_SMACK_NETFILTER
+static u32 smk_of_secmark(u32 secmark)
+{
+	return lsm_token_to_module_secid(secmark,

smack_secids_index);

security/smack/smack_lsm.c +3 -0
+}
+#endif
+

?/**
? * smack_socket_sock_rcv_skb - Smack packet delivery access check ? * @sk: socket

security/smack/smack_lsm.c +0 -0
@@ -3819,7 +3827,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)

? ?* The secmark is assumed to reflect policy
better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -3864,7 +3872,7 @@ static int smack_socket_sock_rcv_skb(struct
 sock *sk, struct sk_buff *skb)
 ?			break;

?#ifdef SMACK_IPV6_SECMARK_LABELING
? if (skb && skb->secmark != 0)

security/smack/smack_lsm.c +1 -1
-			skp = smack_from_secid(skb->secmark);
+			skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? else
? skp = smack_ipv6host_label(&sadd);
? if (skp == NULL)

security/smack/smack_lsm.c +0 -0
@@ -3962,7 +3970,7 @@ static int

smack_socket_getpeersec_dgram(struct
socket *sock,
? break;
? case PF_INET:
?#ifdef CONFIG_SECURITY_SMACK_NETFILTER

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

? if (s != 0)
? break;
?#endif

security/smack/smack_lsm.c +0 -0
@@ -3981,7 +3989,7 @@ static int

smack_socket_getpeersec_dgram(struct
socket *sock,
? break;
? case PF_INET6:
?#ifdef SMACK_IPV6_SECMARK_LABELING

security/smack/smack_lsm.c +1 -1
-		s = skb->secmark;
+		s = smk_of_secmark(skb->secmark);

?#endif
? break;
? }

security/smack/smack_lsm.c +0 -0
@@ -4060,7 +4068,7 @@ static int smack_inet_conn_request(struct

sock
*sk, struct sk_buff *skb,
? ?* The secmark is assumed to reflect policy better.
? ?*/
? if (skb && skb->secmark != 0) {

security/smack/smack_lsm.c +1 -1
-		skp = smack_from_secid(skb->secmark);
+		skp = smack_from_secid(smk_of_secmark(skb-
Quoted 1 line

secmark));

? goto access_check;
? }
?#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

security/smack/smack_lsm.c +0 -0
@@ -4650,6 +4658,7 @@ static __init int smack_init(void)

? ?* Register with LSM
? ?*/
? security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks),
"smack");

security/smack/smack_lsm.c +1 -0
+	smack_secids_index = smack_hooks[0].lsm_index;

? smack_enabled = 1;
?
? pr_info("Smack:??Initializing.\n");

security/smack/smack_netfilter.c +0 -0
index a5155295551f..510661ba6c16 100644
@@ -23,6 +23,19 @@

?
?#if IS_ENABLED(CONFIG_IPV6)
?

security/smack/smack_netfilter.c +13 -0
+/*
+ * Reinvestigate this soon?
+ *
+ */
+static u32 smack_to_secmark(u32 secid)
+{
+	struct lsm_secids secids;
+
+	lsm_secids_init(&secids);
+	secids.secid[smack_secids_index] = secid;
+	return lsm_secids_to_token(&secids);
+}
+

?static unsigned int smack_ipv6_output(void *priv,
? struct sk_buff *skb,
? const struct nf_hook_state
*state)

security/smack/smack_netfilter.c +1 -1
@@ -34,7 +47,7 @@ static unsigned int smack_ipv6_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/smack/smack_netfilter.c +1 -1
@@ -52,7 +65,7 @@ static unsigned int smack_ipv4_output(void *priv,
 ?	if (sk && smack_sock(sk)) {
 ?		ssp = smack_sock(sk);
 ?		skp = ssp->smk_out;
-		skb->secmark = skp->smk_secid;
+		skb->secmark = smack_to_secmark(skp->smk_secid);
 ?	}

?
? return NF_ACCEPT;

security/stacking.c new file +11 -0
new file mode 100644
index 000000000000..65276cd695de
@@ -0,0 +1,165 @@
+/*
+ *??Maintain a mapping between the secid used in networking
+ *??and the set of secids used by the security modules.
+ *
+ *??Author:
+ *	Casey Schaufler <[email protected]>
+ *
+ *??Copyright (C) 2017 Casey Schaufler <[email protected]>
+ *??Copyright (C) 2017 Intel Corporation.
+ *
+ *	This program is free software; you can redistribute it

and/or modify
+ * it under the terms of the GNU General Public License
version 2,
+ *??????as published by the Free Software Foundation.
+ */

security/stacking.c new file +5 -0
+
+#include <linux/lsm_hooks.h>
+
+struct token_entry {
+	int			used;	/* relative age of

the entry */

security/stacking.c new file +2 -0
+	u32			token;	/* token value */
+	struct lsm_secids	secids;	/* secids mapped

to
this token */

security/stacking.c new file +19 -0
+};
+
+/*
+ * Add an entry to the table when asked for a mapping that
+ * isn't already present. If the table is full throw away the
+ * least recently used entry. If the entry is present undate
+ * when it was used.
+ */
+#define TOKEN_AGE_LIMIT (MAX_INT >> 2)
+#define TOKEN_LIMIT 0x20000000
+#define TOKEN_SET_SIZE 200
+#define TOKEN_BIT 0x80000000
+int token_used;
+u32 token_next;
+struct lsm_secids null_secids;
+struct token_entry token_set[TOKEN_SET_SIZE];
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+static void report_token(const char *msg, const struct token_entry

*te)

security/stacking.c new file +4 -0
+{
+	int i;
+
+	pr_info("LSM: %s token=%08x %u,%u,%u,%u,%u,%u,%u,%u\n",

msg,
te->token,

security/stacking.c new file +1 -0
+		te->secids.secid[0], te->secids.secid[1], te-
Quoted 1 line

secids.secid[2],

security/stacking.c new file +1 -0
+		te->secids.secid[3], te->secids.secid[4], te-
Quoted 1 line

secids.secid[5],

security/stacking.c new file +4 -0
+		te->secids.secid[6], te->secids.secid[7]);
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (te->secids.secid[i] & TOKEN_BIT)
+			pr_info("LSM: module %d provided a

token.\n", i);

security/stacking.c new file +3 -0
+}
+#else
+static inline void report_token(const char *msg, const struct

token_entry *te)

security/stacking.c new file +8 -0
+{
+}
+#endif
+
+static int next_used(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token use overflow - safe

reset\n");

security/stacking.c new file +9 -0
+		token_used = 0;
+	}
+	return ++token_used;
+}
+
+static u32 next_token(void)
+{
+	if (token_next >= TOKEN_LIMIT) {
+		pr_info("LSM: Security token overflow - safe

reset\n");

security/stacking.c new file +15 -0
+		token_next = 0;
+	}
+	return ++token_next | TOKEN_BIT;
+}
+
+u32 lsm_secids_to_token(const struct lsm_secids *secids)
+{
+	int i;
+	int j;
+	int old;
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	for (i = 0; i < LSM_MAX_MAJOR; i++)
+		if (secids->secid[i] & TOKEN_BIT)
+			pr_info("LSM: %s secid[%d]=%08x has token

bit\n",

security/stacking.c new file +14 -0
+				__func__, i, secids->secid[i]);
+#endif
+
+	/*
+	?* If none of the secids are set whoever sent this here
+	?* was thinking "0".
+	?*/
+	if (!memcmp(secids, &null_secids, sizeof(*secids)))
+		return 0;
+
+	for (i = 0; i < TOKEN_SET_SIZE; i++) {
+		if (token_set[i].token == 0)
+			break;
+		if (!memcmp(secids, &token_set[i].secids,

sizeof(*secids))) {

security/stacking.c new file +23 -0
+			token_set[i].used = next_used();
+			return token_set[i].token;
+		}
+	}
+	if (i == TOKEN_SET_SIZE) {
+		old = token_used;
+		for (j = 0; j < TOKEN_SET_SIZE; j++) {
+			if (token_set[j].used < old) {
+				old = token_set[j].used;
+				i = j;
+			}
+		}
+	}
+	token_set[i].secids = *secids;
+	token_set[i].token = next_token();
+	token_set[i].used = next_used();
+
+	report_token("new", &token_set[i]);
+
+	return token_set[i].token;
+}
+
+void lsm_token_to_secids(const u32 token, struct lsm_secids

*secids)

security/stacking.c new file +8 -0
+{
+	int i;
+	struct lsm_secids fudge;
+
+	if (token) {
+		if (!(token & TOKEN_BIT)) {
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+			pr_info("LSM: %s token=%08x has no token

bit\n",

security/stacking.c new file +18 -0
+				__func__, token);
+#endif
+			for (i = 0; i < LSM_MAX_MAJOR; i++)
+				fudge.secid[i] = token;
+			*secids = fudge;
+			return;
+		}
+		for (i = 0; i < TOKEN_SET_SIZE; i++) {
+			if (token_set[i].token == 0)
+				break;
+			if (token_set[i].token == token) {
+				*secids = token_set[i].secids;
+				token_set[i].used = next_used();
+				return;
+			}
+		}
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+	pr_info("LSM: %s token=%u was not found\n", __func__,

token);

security/stacking.c new file +10 -0
+#endif
+	}
+	*secids = null_secids;
+}
+
+u32 lsm_token_to_module_secid(const u32 token, int lsm)
+{
+	struct lsm_secids secids;
+
+????????lsm_token_to_secids(token, &secids);
Quoted 8 lines
Quoted 8 lines

security/stacking.c line 165

Quoted 7 lines
security/stacking.c new file +7 -0
+	return secids.secid[lsm];
+}
+
+void lsm_secids_init(struct lsm_secids *secids)
+{
+	*secids = null_secids;
+}

ENOLOCKING

Indeed. I felt it better to put this out with known issues than to hold back another release cycle. It also provides the opportunity for some eager individual to provide a proper implementation should they be inclined to show me how it's done properly. :)

Quoted 4 lines

Also, how do you know that it is safe to reclaim the least recently used entry? Previously issued tokens/secids may have been cached in other kernel data structures, and you have no guarantees that they are no longer in use there.

The reuse of secids is a problem the security modules need to address. Smack, for example, cannot overflow the 32 bit secid limit without running out of memory on any computer existing today or in the near future.

The reuse of tokens is another matter. But we know where they are used. They are used in audit and network operations, and the lifetime of the association is short. Audit gets secids or tokens from things, uses them to create strings, and is done. Local networking only needs a token for the time of transmission. Off-box networking is where there is likely to be a problem. There are still issues with getting tokens into netlabel, and it's going to be a full blown labeled network that's going to lead to trouble.

Quoted 2 lines

A simple find / -ls on a Fedora system will roll over your token set size many times.

Nope. I tried it, and as I expected, it introduced no new tokens. Tokens are only allocated when they are needed, and none of the operations involved in "find / -ls" or "find / -exec ls -Z" use secids. It is going to require audit and/or network operations to get more tokens created. The SELinux test suite, which changes network configuration, does create a few new tokens.

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html