Skip to contentSkip to conversation

selinux: Implement LSM notification system

PATCH In review Sebastien Buisson 5 participants 20 messages Apr 26, 2017 – Apr 28, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 selinux: Implement LSM notification system

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers can register a callback with the LSM and security modules can produce events.

Add a call to the notification mechanism from SELinux when the AVC cache changes.

---
 include/linux/security.h | 23 +++++++++++++++++++++++
 security/security.c      | 20 ++++++++++++++++++++
 security/selinux/hooks.c | 12 ++++++++++++
 3 files changed, 55 insertions(+)
include/linux/security.h +23 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
 struct user_namespace;
 struct timezone;
 
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+
 /* These functions are in security/commoncap.c */
 extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
 		       int cap, int audit);
@@ -163,6 +167,10 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 	opts->mnt_opts = NULL;
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 struct security_mnt_opts {
 };
 
+static inline int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 }
security/security.c +20 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
 /* Maximum number of letters for an LSM name string */
 #define SECURITY_NAME_MAX	10
 
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
 char *lsm_names;
 /* Boot-time LSM user choice */
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+
 /*
  * Hook list operation macros.
  *
security/selinux/hooks.c +12 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32 event)
 	return 0;
 }
 
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+
 /*
  * initialise the security for the init task
  */
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM notifier callback\n");
+
 	if (selinux_enforcing)
 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
 	else
Signature · 6 lines
1.8.3.1

--
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
#1 lore raw

PATCH 2/3 selinux: add checksum to policydb

Add policycksum field to struct policydb. It holds the sha256 checksum computed on the binary policy every time the notifier is called after a policy change.
Add security_policy_cksum hook to give access to policy checksum to the rest of the kernel. Lustre client makes use of this information.

---
 include/linux/lsm_hooks.h           |  2 +
 include/linux/security.h            |  7 +++
 security/security.c                 |  6 +++
 security/selinux/hooks.c            | 12 ++++-
 security/selinux/include/security.h |  2 +
 security/selinux/ss/policydb.h      |  4 ++
 security/selinux/ss/services.c      | 91 +++++++++++++++++++++++++++++++++++++
 7 files changed, 123 insertions(+), 1 deletion(-)
include/linux/lsm_hooks.h +2 -0
index 080f34e..7aada73 100644
@@ -1568,6 +1568,7 @@
 	int (*inode_setsecctx)(struct dentry *dentry, void *ctx, u32 ctxlen);
 	int (*inode_getsecctx)(struct inode *inode, void **ctx, u32 *ctxlen);
 
+	ssize_t (*policy_cksum)(char *cksum, size_t len);
 #ifdef CONFIG_SECURITY_NETWORK
 	int (*unix_stream_connect)(struct sock *sock, struct sock *other,
 					struct sock *newsk);
@@ -1813,6 +1814,7 @@ struct security_hook_heads {
 	struct list_head inode_notifysecctx;
 	struct list_head inode_setsecctx;
 	struct list_head inode_getsecctx;
+	struct list_head policy_cksum;
 #ifdef CONFIG_SECURITY_NETWORK
 	struct list_head unix_stream_connect;
 	struct list_head unix_may_send;
include/linux/security.h +7 -0
index 73a9c93..8461d31 100644
@@ -385,6 +385,8 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen);
 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen);
 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen);
+
+ssize_t security_policy_cksum(char *cksum, size_t len);
 #else /* CONFIG_SECURITY */
 struct security_mnt_opts {
 };
@@ -1189,6 +1191,11 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32
 {
 	return -EOPNOTSUPP;
 }
+
+static inline ssize_t security_policy_cksum(char *cksum, size_t len)
+{
+	return -EOPNOTSUPP;
+}
 #endif	/* CONFIG_SECURITY */
 
 #ifdef CONFIG_SECURITY_NETWORK
security/security.c +6 -0
index ef9d9e1..a82c08c 100644
@@ -1305,6 +1305,12 @@ int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
 }
 EXPORT_SYMBOL(security_inode_getsecctx);
 
+ssize_t security_policy_cksum(char *cksum, size_t len)
+{
+	return call_int_hook(policy_cksum, -EOPNOTSUPP, cksum, len);
+}
+EXPORT_SYMBOL(security_policy_cksum);
+
 #ifdef CONFIG_SECURITY_NETWORK
 
 int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
security/selinux/hooks.c +11 -1
index a4d36f8..3759198 100644
@@ -173,8 +173,11 @@ static int selinux_netcache_avc_callback(u32 event)
 
 static int selinux_lsm_notifier_avc_callback(u32 event)
 {
-	if (event == AVC_CALLBACK_RESET)
+	if (event == AVC_CALLBACK_RESET) {
+		if (security_policydb_compute_cksum() != 0)
+			printk(KERN_ERR "Failed to compute policydb cksum\n");
 		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+	}
 
 	return 0;
 }
@@ -6071,6 +6074,11 @@ static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
 	*ctxlen = len;
 	return 0;
 }
+
+static ssize_t selinux_policy_cksum(char *cksum, size_t len)
+{
+	return security_policydb_cksum(cksum, len);
+}
 #ifdef CONFIG_KEYS
 
 static int selinux_key_alloc(struct key *k, const struct cred *cred,
@@ -6285,6 +6293,8 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer)
 	LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
 	LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
 
+	LSM_HOOK_INIT(policy_cksum, selinux_policy_cksum),
+
 	LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
 	LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
 
security/selinux/include/security.h +2 -0
index f979c35..a329571 100644
@@ -97,6 +97,8 @@ enum {
 int security_load_policy(void *data, size_t len);
 int security_read_policy(void **data, size_t *len);
 size_t security_policydb_len(void);
+ssize_t security_policydb_cksum(char *cksum, size_t len);
+int security_policydb_compute_cksum(void);
 
 int security_policycap_supported(unsigned int req_cap);
 
security/selinux/ss/policydb.h +4 -0
index 725d594..dc29492 100644
@@ -25,6 +25,7 @@
 #define _SS_POLICYDB_H_
 
 #include <linux/flex_array.h>
+#include <crypto/sha.h>
 
 #include "symtab.h"
 #include "avtab.h"
@@ -293,6 +294,9 @@ struct policydb {
 	size_t len;
 
 	unsigned int policyvers;
+	/* checksum computed on the policy */
+	unsigned char policycksum[SHA256_DIGEST_SIZE*2 + 1];
+	size_t policycksum_len;
 
 	unsigned int reject_unknown : 1;
 	unsigned int allow_unknown : 1;
security/selinux/ss/services.c +91 -0

103 lines not on this page · raw message

Signature · 6 lines
1.8.3.1

--
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
#11 lore raw

PATCH 3/3 selinux: expose policy SHA256 checksum via selinuxfs

In reply to Sebastien Buisson

Expose policy SHA256 checksum via selinuxfs.

---
 security/selinux/selinuxfs.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
security/selinux/selinuxfs.c +20 -0
index ce71718..b2d5deb 100644
@@ -30,6 +30,7 @@
 #include <linux/uaccess.h>
 #include <linux/kobject.h>
 #include <linux/ctype.h>
+#include <crypto/sha.h>
 
 /* selinuxfs pseudo filesystem for exporting the security policy API.
    Based on the proc code and the fs/nfsd/nfsctl.c code. */
@@ -99,6 +100,7 @@ enum sel_inos {
 	SEL_STATUS,	/* export current status using mmap() */
 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
+	SEL_POLICYCKSUM,/* return policy SHA256 checkum */
 	SEL_INO_NEXT,	/* The next inode number to use */
 };
 
@@ -313,6 +315,22 @@ static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
 	.llseek		= generic_file_llseek,
 };
 
+static ssize_t sel_read_policycksum(struct file *filp, char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	size_t tmpbuflen = SHA256_DIGEST_SIZE*2 + 1;
+	char tmpbuf[tmpbuflen];
+	ssize_t length;
+
+	length = security_policydb_cksum(tmpbuf, tmpbuflen);
+	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
+}
+
+static const struct file_operations sel_policycksum_ops = {
+	.read		= sel_read_policycksum,
+	.llseek		= generic_file_llseek,
+};
+
 /* declaration for sel_write_load */
 static int sel_make_bools(void);
 static int sel_make_classes(void);
@@ -1825,6 +1843,8 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
 					S_IWUGO},
+		[SEL_POLICYCKSUM] = {"policycksum", &sel_policycksum_ops,
+				     S_IRUGO},
 		/* last one */ {""}
 	};
 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
Signature · 6 lines
1.8.3.1

--
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
In reply to Sebastien Buisson
Quoted 5 lines · Sebastien Buisson

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers can register a callback with the LSM and security modules can produce events.

Why is this a generic mechanism? Do you ever see anyone other than SELinux using it?

Quoted 2 lines

Add a call to the notification mechanism from SELinux when the AVC cache changes.

This seems like a whole lot of mechanism for something you could accomplish with a log message. What am I missing?

Quoted 115 lines
---
 include/linux/security.h | 23 +++++++++++++++++++++++
 security/security.c      | 20 ++++++++++++++++++++
 security/selinux/hooks.c | 12 ++++++++++++
 3 files changed, 55 insertions(+)
include/linux/security.h +23 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
 struct user_namespace;
 struct timezone;
 
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+
 /* These functions are in security/commoncap.c */
 extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
 		       int cap, int audit);
@@ -163,6 +167,10 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 	opts->mnt_opts = NULL;
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 struct security_mnt_opts {
 };
 
+static inline int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 }
security/security.c +20 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
 /* Maximum number of letters for an LSM name string */
 #define SECURITY_NAME_MAX	10
 
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
 char *lsm_names;
 /* Boot-time LSM user choice */
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+
 /*
  * Hook list operation macros.
  *
security/selinux/hooks.c +8 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32 event)
 	return 0;
 }
 
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+
 /*
  * initialise the security for the init task
  */

security/selinux/hooks.c static __init int selinux_init(void) · line 6396

Quoted 10 lines
security/selinux/hooks.c +4 -0
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM notifier callback\n");
+
 	if (selinux_enforcing)
 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
 	else

--
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
Quoted 8 lines · Casey Schaufler
Quoted 5 lines · Sebastien Buisson

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers can register a callback with the LSM and security modules can produce events.

Why is this a generic mechanism? Do you ever see anyone other than SELinux using it?

I had created an SELinux specific mechanism, Paul Moore requested I make it generic.

Quoted 5 lines
Quoted 2 lines

Add a call to the notification mechanism from SELinux when the AVC cache changes.

This seems like a whole lot of mechanism for something you could accomplish with a log message. What am I missing?

This was part of a larger patch set that hasn't been accepted yet. SELinux support for Inifiniband. Subsequent patches in that patch set will use it as well.

Quoted 117 lines
Quoted 117 lines
---
 include/linux/security.h | 23 +++++++++++++++++++++++
 security/security.c      | 20 ++++++++++++++++++++
 security/selinux/hooks.c | 12 ++++++++++++
 3 files changed, 55 insertions(+)
include/linux/security.h +23 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
 struct user_namespace;
 struct timezone;
 
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+
 /* These functions are in security/commoncap.c */
 extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
 		       int cap, int audit);
@@ -163,6 +167,10 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 	opts->mnt_opts = NULL;
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 struct security_mnt_opts {
 };
 
+static inline int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 }
security/security.c +20 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
 /* Maximum number of letters for an LSM name string */
 #define SECURITY_NAME_MAX	10
 
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
 char *lsm_names;
 /* Boot-time LSM user choice */
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+
 /*
  * Hook list operation macros.
  *
security/selinux/hooks.c +8 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32 event)
 	return 0;
 }
 
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+
 /*
  * initialise the security for the init task
  */
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
Quoted 8 lines

security/selinux/hooks.c static __init int selinux_init(void) · line 6396

Quoted 7 lines
security/selinux/hooks.c +4 -0
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM notifier callback\n");
+
 	if (selinux_enforcing)
 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
 	else

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

#16 lore raw

2017-04-26 17:48 GMT+02:00 Daniel Jurgens <[email protected]>:

Quoted 1 line

This was part of a larger patch set that hasn't been accepted yet. SELinux support for Inifiniband. Subsequent patches in that patch set will use it as well.

I revived this patch following Stephen Smalley's suggestion to base the policy checksum computation and change notification on the work you had done on LSM notification callbacks.
--
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

#17 lore raw
In reply to Daniel Jurgens
Quoted 9 lines · Daniel Jurgens
Quoted 8 lines · Casey Schaufler
Quoted 5 lines · Sebastien Buisson

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers can register a callback with the LSM and security modules can produce events.

Why is this a generic mechanism? Do you ever see anyone other than SELinux using it?

Quoted 7 lines

I had created an SELinux specific mechanism, Paul Moore requested I make it generic.

Quoted 5 lines
Quoted 2 lines

Add a call to the notification mechanism from SELinux when the AVC cache changes.

This seems like a whole lot of mechanism for something you could accomplish with a log message. What am I missing?

This was part of a larger patch set that hasn't been accepted yet. SELinux support for Inifiniband. Subsequent patches in that patch set will use it as well.

You should include this information in a 0/3 message. Without the context the patch set is curious, but not compelling.

Quoted 117 lines
Quoted 117 lines
Quoted 117 lines
---
 include/linux/security.h | 23 +++++++++++++++++++++++
 security/security.c      | 20 ++++++++++++++++++++
 security/selinux/hooks.c | 12 ++++++++++++
 3 files changed, 55 insertions(+)
include/linux/security.h +23 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
 struct user_namespace;
 struct timezone;
 
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+
 /* These functions are in security/commoncap.c */
 extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
 		       int cap, int audit);
@@ -163,6 +167,10 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 	opts->mnt_opts = NULL;
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
 struct security_mnt_opts {
 };
 
+static inline int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 }
security/security.c +20 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
 /* Maximum number of letters for an LSM name string */
 #define SECURITY_NAME_MAX	10
 
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+
 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
 char *lsm_names;
 /* Boot-time LSM user choice */
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n", __func__);
 }
 
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+
 /*
  * Hook list operation macros.
  *
security/selinux/hooks.c +8 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32 event)
 	return 0;
 }
 
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+
 /*
  * initialise the security for the init task
  */
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
 	if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache callback\n");
 
Quoted 8 lines
Quoted 7 lines

security/selinux/hooks.c static __init int selinux_init(void) · line 6396

Quoted 7 lines
security/selinux/hooks.c +4 -0
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM notifier callback\n");
+
 	if (selinux_enforcing)
 		printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
 	else

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

#18 lore raw
In reply to Casey Schaufler
Quoted 11 lines · Casey Schaufler
Quoted 7 lines · Sebastien Buisson

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers
can register a callback with the LSM and security modules can produce
events.

Why is this a generic mechanism? Do you ever see anyone other than SELinux using it?

I do - any security module that wants to support access control over Lustre filesystems or Infiniband. Seems ironic for you to be arguing for a SELinux-specific interface rather than a LSM interface.

Quoted 7 lines
Quoted 3 lines

Add a call to the notification mechanism from SELinux when the AVC cache changes.

This seems like a whole lot of mechanism for something you could accomplish with a log message. What am I missing?

It's a notification to a kernel subsystem that policy has changed so that the subsystem can update any cached state.

Quoted 143 lines
Quoted 143 lines

---
 include/linux/security.h | 23 +++++++++++++++++++++++ security/security.c      | 20 ++++++++++++++++++++ security/selinux/hooks.c | 12 ++++++++++++

 3 files changed, 55 insertions(+)
include/linux/security.h +0 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
  struct user_namespace;
  struct timezone;

include/linux/security.h +4 -0
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+

 /* These functions are in security/commoncap.c */ extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
         int cap, int audit);

include/linux/security.h +0 -0
@@ -163,6 +167,10 @@ struct security_mnt_opts {
  	int num_mnt_opts;
  };

include/linux/security.h +4 -0
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+

 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
  opts->mnt_opts = NULL;

include/linux/security.h +0 -0
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma,
 struct sembuf *sops,
  struct security_mnt_opts {
  };


+static inline int call_lsm_notifier(enum lsm_event event, void *data)

include/linux/security.h +10 -0
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block

*nb)

include/linux/security.h +4 -0
+{
+	return 0;
+}
+

 static inline void security_init_mnt_opts(struct security_mnt_opts *opts)
 {
 }

security/security.c +0 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
  /* Maximum number of letters for an LSM name string */
  #define SECURITY_NAME_MAX	10

security/security.c +2 -0
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+

 struct security_hook_heads security_hook_heads
__lsm_ro_after_init;
 char *lsm_names;
 /* Boot-time LSM user choice */

security/security.c +0 -0
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct
 security_hook_list *hooks, int count,
  		panic("%s - Cannot get early memory.\n",
 __func__);
  }

security/security.c +3 -0
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain,

event, data);

security/security.c +6 -0
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain,

nb);

security/security.c +6 -0
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return

atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);

security/security.c +3 -0
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+

 /*
  * Hook list operation macros.
  *

security/selinux/hooks.c +0 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32
 event)
  	return 0;
  }

security/selinux/hooks.c +8 -0
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+

 /*
  * initialise the security for the init task
  */

security/selinux/hooks.c +0 -0
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
  	if (avc_add_callback(selinux_netcache_avc_callback,
 AVC_CALLBACK_RESET))

  panic("SELinux: Unable to register AVC netcache callback\n");

security/selinux/hooks.c +3 -0
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM

notifier callback\n");
+
  if (selinux_enforcing)
  printk(KERN_DEBUG "SELinux:  Starting in enforcing mode\n");
  else

#19 lore raw
Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Casey Schaufler
Quoted 7 lines · Sebastien Buisson

From: Daniel Jurgens <[email protected]>

Add a generic notification mechanism in the LSM. Interested consumers
can register a callback with the LSM and security modules can produce
events.

Quoted 5 lines
Quoted 2 lines

Why is this a generic mechanism? Do you ever see anyone other than SELinux using it?

I do - any security module that wants to support access control over Lustre filesystems or Infiniband. Seems ironic for you to be arguing for a SELinux-specific interface rather than a LSM interface.

I lost the connection between this mechanism and Lustre and Infiniband, neither of which are mentioned in the patchset. And I *did* pose it as a question. I am all in favor of general mechanisms where they are generally useful. I am also adverse to special purpose clutter.

Quoted 141 lines
Quoted 6 lines
Quoted 3 lines

Add a call to the notification mechanism from SELinux when the AVC cache changes.

This seems like a whole lot of mechanism for something you could accomplish with a log message. What am I missing?

It's a notification to a kernel subsystem that policy has changed so that the subsystem can update any cached state.

Quoted 132 lines
Quoted 132 lines
---
 include/linux/security.h | 23 +++++++++++++++++++++++
 security/security.c      | 20 ++++++++++++++++++++
 security/selinux/hooks.c | 12 ++++++++++++
 3 files changed, 55 insertions(+)
include/linux/security.h +4 -0
index af675b5..73a9c93 100644
@@ -68,6 +68,10 @@
 struct user_namespace;
 struct timezone;
 
+enum lsm_event {
+	LSM_POLICY_CHANGE,
+};
+
 /* These functions are in security/commoncap.c */
 extern int cap_capable(const struct cred *cred, struct
 user_namespace *ns,
include/linux/security.h +4 -0
 		       int cap, int audit);
@@ -163,6 +167,10 @@ struct security_mnt_opts {
 	int num_mnt_opts;
 };
 
+int call_lsm_notifier(enum lsm_event event, void *data);
+int register_lsm_notifier(struct notifier_block *nb);
+int unregister_lsm_notifier(struct notifier_block *nb);
+
 static inline void security_init_mnt_opts(struct security_mnt_opts
 *opts)
 {
include/linux/security.h +15 -0
 	opts->mnt_opts = NULL;
@@ -381,6 +389,21 @@ int security_sem_semop(struct sem_array *sma,
 struct sembuf *sops,
 struct security_mnt_opts {
 };
 
+static inline int call_lsm_notifier(enum lsm_event event, void
 *data)
+{
+	return 0;
+}
+
+static inline int register_lsm_notifier(struct notifier_block *nb)
+{
+	return 0;
+}
+
+static inline  int unregister_lsm_notifier(struct notifier_block
 *nb)
+{
+	return 0;
+}
+

static inline void security_init_mnt_opts(struct security_mnt_opts
*opts)
{
}

security/security.c +2 -0
index b9fea39..ef9d9e1 100644
@@ -32,6 +32,8 @@
 /* Maximum number of letters for an LSM name string */
 #define SECURITY_NAME_MAX	10
 
+static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
+
 struct security_hook_heads security_hook_heads
 __lsm_ro_after_init;
 char *lsm_names;

/* Boot-time LSM user choice */

security/security.c +9 -0
@@ -146,6 +148,24 @@ void __init security_add_hooks(struct
 security_hook_list *hooks, int count,
 		panic("%s - Cannot get early memory.\n",
 __func__);
 }
 
+int call_lsm_notifier(enum lsm_event event, void *data)
+{
+	return atomic_notifier_call_chain(&lsm_notifier_chain,
 event, data);
+}
+EXPORT_SYMBOL(call_lsm_notifier);
+
+int register_lsm_notifier(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&lsm_notifier_chain,

nb);

security/security.c +6 -0
+}
+EXPORT_SYMBOL(register_lsm_notifier);
+
+int unregister_lsm_notifier(struct notifier_block *nb)
+{
+	return

atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);

security/security.c +3 -0
+}
+EXPORT_SYMBOL(unregister_lsm_notifier);
+
 /*
  * Hook list operation macros.
  *
security/selinux/hooks.c +8 -0
index e67a526..a4d36f8 100644
@@ -171,6 +171,14 @@ static int selinux_netcache_avc_callback(u32
 event)
 	return 0;
 }
 
+static int selinux_lsm_notifier_avc_callback(u32 event)
+{
+	if (event == AVC_CALLBACK_RESET)
+		call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
+
+	return 0;
+}
+
 /*
  * initialise the security for the init task

*/

security/selinux/hooks.c +0 -0
@@ -6379,6 +6387,10 @@ static __init int selinux_init(void)
 	if (avc_add_callback(selinux_netcache_avc_callback,
 AVC_CALLBACK_RESET))
 		panic("SELinux: Unable to register AVC netcache
 callback\n");
 
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
security/selinux/hooks.c +4 -0
+	if (avc_add_callback(selinux_lsm_notifier_avc_callback,
+			     AVC_CALLBACK_RESET))
+		panic("SELinux: Unable to register AVC LSM
 notifier callback\n");
+
security/selinux/hooks.c +0 -0
 	if (selinux_enforcing)
 		printk(KERN_DEBUG "SELinux:  Starting in enforcing

mode\n");
else

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

#2 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

In reply to Sebastien Buisson
Quoted 32 lines · Sebastien Buisson

Add policycksum field to struct policydb. It holds the sha256 checksum computed on the binary policy every time the notifier is called after a policy change.
Add security_policy_cksum hook to give access to policy checksum to the rest of the kernel. Lustre client makes use of this information.

---
 include/linux/lsm_hooks.h           |  2 +
 include/linux/security.h            |  7 +++
 security/security.c                 |  6 +++
 security/selinux/hooks.c            | 12 ++++-
 security/selinux/include/security.h |  2 +
 security/selinux/ss/policydb.h      |  4 ++
 security/selinux/ss/services.c      | 91
+++++++++++++++++++++++++++++++++++++

 7 files changed, 123 insertions(+), 1 deletion(-)
security/selinux/hooks.c +0 -0
index a4d36f8..3759198 100644
@@ -173,8 +173,11 @@ static int selinux_netcache_avc_callback(u32
 event)


 static int selinux_lsm_notifier_avc_callback(u32 event) {

security/selinux/hooks.c +3 -1
-	if (event == AVC_CALLBACK_RESET)
+	if (event == AVC_CALLBACK_RESET) {
+		if (security_policydb_compute_cksum() != 0)
+			printk(KERN_ERR "Failed to compute policydb
Quoted 1 line

cksum\n");

This seems like an odd place to trigger the computation. Why aren't you just computing it when the policy is loaded directly in security_load_policy()? You already have the (data, len) on entry to that function. Just compute it at load time, save it, and be done. No need for a notifier then for your use case unless I am missing something.

I suppose the question is which checksum do you want - the hash of the policy file that was written to /sys/fs/selinux/load by userspace, or the hash of the policy file that the kernel generates on demand if you open /sys/fs/selinux/policy. Those can differ in non-semantic ways due to ordering differences, for example. I think the former is more likely to be of interest to userspace (e.g. to compare the hash value against the hash of the policy file), and is cheaper since you already have a (data, len) pair on entry to security_load_policy() that you can hash immediately rather than requiring the kernel to regenerate the image from the policydb.

Quoted 29 lines

  call_lsm_notifier(LSM_POLICY_CHANGE, NULL);

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


  return 0;
 }

security/selinux/ss/services.c +2 -0
index 60d9b02..a35d294 100644
@@ -53,6 +53,8 @@
  #include <linux/flex_array.h>
  #include <linux/vmalloc.h>
  #include <net/netlabel.h>
+#include <crypto/hash.h>
+#include <crypto/sha.h>


 #include "flask.h"
 #include "avc.h"

security/selinux/ss/services.c +0 -0
@@ -2170,6 +2172,95 @@ size_t security_policydb_len(void)
  }


 /**
+ * security_policydb_cksum - Get policydb checksum.
+ * @cksum: string to store checksum to
+ * @len: length of checksum
+ */

security/selinux/ss/services.c size_t security_policydb_len(void) · line 2186

Quoted 8 lines
security/selinux/ss/services.c +8 -0
+ssize_t security_policydb_cksum(char *cksum, size_t len)
+{
+	int rc;
+
+	read_lock(&policy_rwlock);
+	if (strlcpy(cksum, policydb.policycksum, len) >= len)
+		rc = -ENAMETOOLONG;
+	rc = policydb.policycksum_len;

Obviously you'll clobber -ENAMETOOLONG here.

security/selinux/ss/services.c size_t security_policydb_len(void) · line 2190

Quoted 4 lines
security/selinux/ss/services.c +4 -0
+	read_unlock(&policy_rwlock);
+
+	return rc;
+}

You are requiring all callers to know that they are dealing with a sha256 hash string in order to provide an adequately sized buffer. So we either ought to make that evident in the interface, or make the interface more flexible/general. The latter is imho preferable. We could simply allocate a buffer of the right length and return it, like selinux_inode_getsecurity() does.

Quoted 12 lines
security/selinux/ss/services.c +3 -0
+
+/**
+ * security_policydb_compute_cksum - Compute checksum of a policy

database.
+ */

security/selinux/ss/services.c +7 -0
+int security_policydb_compute_cksum(void)
+{
+	struct crypto_ahash *tfm;
+	struct ahash_request *req;
+	struct scatterlist sl;
+	char hashval[SHA256_DIGEST_SIZE];
+	int idx;

security/selinux/ss/services.c size_t security_policydb_len(void) · line 2211

Quoted 10 lines
security/selinux/ss/services.c +10 -0
+	unsigned char *p;
+	size_t len;
+	void *data;
+	int rc;
+
+	rc = security_read_policy(&data, &len);
+	if (rc) {
+		printk(KERN_ERR "Failed to read security policy\n");
+		return rc;
+	}

This requires regenerating the policy image from the policydb; simpler if we can just hash what we were given in security_load_policy() and save it at that time.

security/selinux/ss/services.c

Quoted 2 lines
security/selinux/ss/services.c +2 -0
+
+	tfm = crypto_alloc_ahash("sha256", 0, CRYPTO_ALG_ASYNC);

Why are you using the async interface?

Quoted 57 lines
security/selinux/ss/services.c +2 -0
+	if (IS_ERR(tfm)) {
+		printk(KERN_ERR "Failed to alloc crypto hash

sha256\n");

security/selinux/ss/services.c +8 -0
+		vfree(data);
+		rc = PTR_ERR(tfm);
+		return rc;
+	}
+
+	req = ahash_request_alloc(tfm, GFP_KERNEL);
+	if (!req) {
+		printk(KERN_ERR "Failed to alloc ahash_request for

sha256\n");

security/selinux/ss/services.c +32 -0
+		crypto_free_ahash(tfm);
+		vfree(data);
+		rc = -ENOMEM;
+		return rc;
+	}
+
+	ahash_request_set_callback(req, 0, NULL, NULL);
+
+	rc = crypto_ahash_init(req);
+	if (rc) {
+		printk(KERN_ERR "Failed to init ahash\n");
+		ahash_request_free(req);
+		crypto_free_ahash(tfm);
+		vfree(data);
+		return rc;
+	}
+
+	sg_init_one(&sl, (void *)data, len);
+	ahash_request_set_crypt(req, &sl, hashval, sl.length);
+	rc = crypto_ahash_digest(req);
+
+	crypto_free_ahash(tfm);
+	ahash_request_free(req);
+	vfree(data);
+	if (rc) {
+		printk(KERN_ERR "Failed to compute digest\n");
+		return rc;
+	}
+
+	p = policydb.policycksum;
+	for (idx = 0; idx < SHA256_DIGEST_SIZE; idx++) {
+		snprintf(p, 3, "%02x", (unsigned

char)(hashval[idx]));

security/selinux/ss/services.c +3 -0
+		p += 2;
+	}
+	policydb.policycksum_len = (size_t)(p -

policydb.policycksum);

security/selinux/ss/services.c +5 -0
+
+	return 0;
+}
+
+/**

  * security_port_sid - Obtain the SID for a port.
  * @protocol: protocol number
  * @port: port number

#12 lore raw

Re: [PATCH 3/3] selinux: expose policy SHA256 checksum via selinuxfs

In reply to Sebastien Buisson
Quoted 45 lines · Sebastien Buisson

Expose policy SHA256 checksum via selinuxfs.

---
 security/selinux/selinuxfs.c | 20 ++++++++++++++++++++

 1 file changed, 20 insertions(+)
security/selinux/selinuxfs.c +1 -0
index ce71718..b2d5deb 100644
@@ -30,6 +30,7 @@
  #include <linux/uaccess.h>
  #include <linux/kobject.h>
  #include <linux/ctype.h>
+#include <crypto/sha.h>


 /* selinuxfs pseudo filesystem for exporting the security policy API.
    Based on the proc code and the fs/nfsd/nfsctl.c code. */

security/selinux/selinuxfs.c +0 -0
@@ -99,6 +100,7 @@ enum sel_inos {

  SEL_STATUS, /* export current status using mmap() */
  SEL_POLICY, /* allow userspace to read the in kernel
policy */
  SEL_VALIDATE_TRANS, /* compute validatetrans decision */

security/selinux/selinuxfs.c +1 -0
+	SEL_POLICYCKSUM,/* return policy SHA256 checkum */

  SEL_INO_NEXT, /* The next inode number to use */
 };

security/selinux/selinuxfs.c +0 -0
@@ -313,6 +315,22 @@ static ssize_t sel_read_policyvers(struct file
 *filp, char __user *buf,
  	.llseek		= generic_file_llseek,
  };


+static ssize_t sel_read_policycksum(struct file *filp, char __user *buf,

security/selinux/selinuxfs.c +8 -0
+				    size_t count, loff_t *ppos)
+{
+	size_t tmpbuflen = SHA256_DIGEST_SIZE*2 + 1;
+	char tmpbuf[tmpbuflen];
+	ssize_t length;
+
+	length = security_policydb_cksum(tmpbuf, tmpbuflen);
+	return simple_read_from_buffer(buf, count, ppos, tmpbuf,
Quoted 2 lines

length);
+}

Should we also include information about the hash used, in case it changes in the future?

Quoted 21 lines
security/selinux/selinuxfs.c +6 -0
+
+static const struct file_operations sel_policycksum_ops = {
+	.read		= sel_read_policycksum,
+	.llseek		= generic_file_llseek,
+};
+

 /* declaration for sel_write_load */
 static int sel_make_bools(void);
 static int sel_make_classes(void);

security/selinux/selinuxfs.c +2 -0
@@ -1825,6 +1843,8 @@ static int sel_fill_super(struct super_block
 *sb, void *data, int silent)
  		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
  		[SEL_VALIDATE_TRANS] = {"validatetrans",
 &sel_transition_ops,
  					S_IWUGO},
+		[SEL_POLICYCKSUM] = {"policycksum",
 &sel_policycksum_ops,
+				     S_IRUGO},

  /* last one */ {""}
  };
  ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);

maintainer
#13 lore raw

Re: [PATCH 3/3] selinux: expose policy SHA256 checksum via selinuxfs

Quoted 7 lines · Stephen Smalley
Quoted 3 lines
(quoted diff) +1 -0
+	return simple_read_from_buffer(buf, count, ppos, tmpbuf,

length);
+}

Should we also include information about the hash used, in case it changes in the future?

Good idea, yes.

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
#3 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

In reply to Stephen Smalley

2017-04-26 20:30 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 1 line

This seems like an odd place to trigger the computation.

I noticed that the policy as exposed via /sys/fs/selinux/policy can also be modified in security_set_bools(). So in order to limit the places from where to compute the policy checksum, I moved the call to checksum computation to selinux_lsm_notifier_avc_callback(). That being said, maybe the hash of /sys/fs/selinux/policy is not the checksum we want. See your comments and my answers below.

Quoted 6 lines

Why aren't you
just computing it when the policy is loaded directly in security_load_policy()? You already have the (data, len) on entry to that function. Just compute it at load time, save it, and be done. No need for a notifier then for your use case unless I am missing something.

You are right. Getting from the Lustre client code the SELinux internally computed checksum is cheap, so no need to be notified every time the policy changes, and no need to store the checksum in Lustre at that time.
I will drop the "Implement LSM notification system" patch from this series, as I cannot justify its usefulness from a Lustre client standpoint anymore.

Quoted 10 lines

I suppose the question is which checksum do you want - the hash of the policy file that was written to /sys/fs/selinux/load by userspace, or the hash of the policy file that the kernel generates on demand if you open /sys/fs/selinux/policy. Those can differ in non-semantic ways due to ordering differences, for example. I think the former is more likely to be of interest to userspace (e.g. to compare the hash value against the hash of the policy file), and is cheaper since you already have a (data, len) pair on entry to security_load_policy() that you can hash immediately rather than requiring the kernel to regenerate the image from the policydb.

OK, I understand now why I was seeing differences between the checksum computed on a (data, len) pair on entry to security_load_policy(), and the checksum computed on a (data, len) pair got from security_read_policy().
I thought it was a problem to have a difference between the internally computed checksum and the one a user can get by calling sha256sum on /sys/fs/selinux/policy. But now I see it makes sense to reflect what was loaded by userspace. So I will simplify this patch accordingly.
--
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

#4 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

Quoted 5 lines · Sebastien Buisson

2017-04-26 20:30 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 1 line

This seems like an odd place to trigger the computation.

I noticed that the policy as exposed via /sys/fs/selinux/policy can also be modified in security_set_bools().

That's true, but does that matter for your use case? Do you care about non-persistent boolean changes? What is the property you want to ensure?

Quoted 42 lines

So in order to limit the
places from where to compute the policy checksum, I moved the call to checksum computation to selinux_lsm_notifier_avc_callback(). That being said, maybe the hash of /sys/fs/selinux/policy is not the checksum we want. See your comments and my answers below.

Quoted 8 lines

Why aren't you
just computing it when the policy is loaded directly in security_load_policy()?  You already have the (data, len) on entry to
that function.  Just compute it at load time, save it, and be done.  No
need for a notifier then for your use case unless I am missing something.

You are right. Getting from the Lustre client code the SELinux internally computed checksum is cheap, so no need to be notified every
time the policy changes, and no need to store the checksum in Lustre at that time.
I will drop the "Implement LSM notification system" patch from this series, as I cannot justify its usefulness from a Lustre client standpoint anymore.

Quoted 9 lines

I suppose the question is which checksum do you want - the hash of the
policy file that was written to /sys/fs/selinux/load by userspace, or
the hash of the policy file that the kernel generates on demand if you
open /sys/fs/selinux/policy.  Those can differ in non-semantic ways due
to ordering differences, for example.  I think the former is more

Quoted 8 lines

likely to be of interest to userspace (e.g. to compare the hash value
against the hash of the policy file), and is cheaper since you already
have a (data, len) pair on entry to security_load_policy() that you can
hash immediately rather than requiring the kernel to regenerate the image from the policydb.

Quoted 11 lines

OK, I understand now why I was seeing differences between the checksum
computed on a (data, len) pair on entry to security_load_policy(), and
the checksum computed on a (data, len) pair got from security_read_policy().
I thought it was a problem to have a difference between the internally
computed checksum and the one a user can get by calling sha256sum on /sys/fs/selinux/policy. But now I see it makes sense to reflect what was loaded by userspace. So I will simplify this patch accordingly.

Ok, that should work as long as you just want to validate that all the clients loaded the same policy file, and aren't concerned about non- persistent policy boolean changes.

You needed to get (global) enforcing mode too, didn't you? That's separate from the policy.

Make sure you make the hash algorithm explicit in both what is returned by the hook to lustre and by what is exported via selinuxfs. Can likely just encode the hash algorithm name in the string when you generate it.

#5 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

2017-04-27 17:18 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 3 lines

Ok, that should work as long as you just want to validate that all the clients loaded the same policy file, and aren't concerned about non- persistent policy boolean changes.

As far as I understand, non-persistent policy boolean changes can affect the way the policy is enforced. So that is a problem if the checksum does not reflect it. We want to protect against someone tampering the policy locally on a Lustre client, even if it does not survive a reboot.
I just checked, with the method of computing the checksum on a (data, len) pair on entry to security_load_policy() the checksum does not change after using setsebool. So it seems I would need to call security_read_policy() to retrieve the binary representation of the policy as currently enforced by the kernel. Unless you can see another way?

Quoted 2 lines

You needed to get (global) enforcing mode too, didn't you? That's separate from the policy.

Exactly, I also need to rework the patch I proposed about this, in light of the comments I received.

Quoted 4 lines

Make sure you make the hash algorithm explicit in both what is returned by the hook to lustre and by what is exported via selinuxfs. Can likely just encode the hash algorithm name in the string when you generate it.

Sure, I will add "sha256:" at the beginning of the string.
--
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

#6 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

Quoted 7 lines · Sebastien Buisson

2017-04-27 17:18 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 5 lines

Ok, that should work as long as you just want to validate that all the
clients loaded the same policy file, and aren't concerned about non-
persistent policy boolean changes.

Quoted 5 lines

As far as I understand, non-persistent policy boolean changes can affect the way the policy is enforced. So that is a problem if the checksum does not reflect it. We want to protect against someone tampering the policy locally on a Lustre client, even if it does not survive a reboot.

A boolean change can affect which TE rules in the policy are enabled/disabled, but only in ways that are defined by the original policy. You can't add arbitrary TE rules that way, just enable/disable blocks that were defined conditionally in the policy. It also has no effect on MLS enforcement, for example. So it depends on your goals.

Quoted 7 lines

I just checked, with the method of computing the checksum on a (data, len) pair on entry to security_load_policy() the checksum does not change after using setsebool. So it seems I would need to call security_read_policy() to retrieve the binary representation of the policy as currently enforced by the kernel. Unless you can see another
way?

I don't think that's a viable option, since security_read_policy() is going to be expensive in order to generate a full policy image, while security_set_bools() is supposed to be substantially cheaper than a full policy load.

Also, the advantage of taking the hash of the original input file is that you can independently compute a reference hash offline or on the server from the same policy file and compare them and you can identify which policy file was loaded based on the hash.

If you care about the active boolean state, then I'd suggest hashing the active boolean state separately and storing that after the policy hash. You can do that in both security_load_policy() and security_set_bools(). Just iterate through the bools like security_set_bools() does, write the ->state of each boolean into a buffer, and then hash that buffer.

Quoted 5 lines
Quoted 2 lines

You needed to get (global) enforcing mode too, didn't you?  That's separate from the policy.

Exactly, I also need to rework the patch I proposed about this, in light of the comments I received.

So perhaps what you really want is a hook interface and a selinuxfs interface that returns a single string that encodes all of the policy properties that you care about? Rather than separate hooks and interfaces? You could embed the enforcing status in the string too. Should probably include checkreqprot as well since that affects enforcement of mmap/mprotect checks.

Quoted 7 lines
Quoted 5 lines

Make sure you make the hash algorithm explicit in both what is returned
by the hook to lustre and by what is exported via selinuxfs.  Can likely just encode the hash algorithm name in the string when you generate it.

Sure, I will add "sha256:" at the beginning of the string.

#7 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

2017-04-27 20:47 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 18 lines
Quoted 7 lines

I just checked, with the method of computing the checksum on a (data, len) pair on entry to security_load_policy() the checksum does not change after using setsebool. So it seems I would need to call security_read_policy() to retrieve the binary representation of the policy as currently enforced by the kernel. Unless you can see another
way?

I don't think that's a viable option, since security_read_policy() is going to be expensive in order to generate a full policy image, while security_set_bools() is supposed to be substantially cheaper than a full policy load.

Also, the advantage of taking the hash of the original input file is that you can independently compute a reference hash offline or on the server from the same policy file and compare them and you can identify which policy file was loaded based on the hash.

Quoted 6 lines

If you care about the active boolean state, then I'd suggest hashing the active boolean state separately and storing that after the policy hash. You can do that in both security_load_policy() and security_set_bools(). Just iterate through the bools like security_set_bools() does, write the ->state of each boolean into a buffer, and then hash that buffer.

I just noticed another issue: with the method of computing the checksum on a (data, len) pair on entry to security_load_policy(), the checksum does not change after inserting a new module with semodule. It is a problem as a module can allow actions by certain users on some file contexts. So not detecting that kind of policy tampering defeats the purpose of the checksum as I imagine it.

To address this I propose to come back to the idea of the notifier. The checksum would not be stored inside the struct policydb. The checksum would be computed on a (data, len) pair got from security_read_policy() every time someone is asking for it through the security_policy_cksum() hook. The ones that would potentially call security_policy_cksum() are those that would register a callback on lsm_notifier, and the userspace processes reading /sys/fs/selinux/policycksum. So no matter if computing the checksum gets expensive, that would be the caller's responsibility to use it with care. Just like with /sys/fs/selinux/policy today in fact.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

You needed to get (global) enforcing mode too, didn't you? That's separate from the policy.

Exactly, I also need to rework the patch I proposed about this, in light of the comments I received.

Quoted 6 lines

So perhaps what you really want is a hook interface and a selinuxfs interface that returns a single string that encodes all of the policy properties that you care about? Rather than separate hooks and interfaces? You could embed the enforcing status in the string too. Should probably include checkreqprot as well since that affects enforcement of mmap/mprotect checks.

True, I should build a string of the form:
<0 or 1 for enforce>:<0 or 1 for checkreqprot>:<hashalg>=<global checksum> I should probably rename it 'policybrief' instead of 'policycksum'.

I realize that the 'SELinux user to UNIX user' assignments are important as well. If for instance a regular user on a given cluster node is mapped to unconfined_u instead of user_u, this user would erroneously have major privileges. I do not know where I should look for this information, and possibly compute another checksum.
--
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

#8 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

Quoted 36 lines · Sebastien Buisson

2017-04-27 20:47 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 26 lines
Quoted 10 lines

I just checked, with the method of computing the checksum on a (data,
len) pair on entry to security_load_policy() the checksum does not
change after using setsebool. So it seems I would need to call security_read_policy() to retrieve the binary representation of the
policy as currently enforced by the kernel. Unless you can see another
way?

I don't think that's a viable option, since security_read_policy() is
going to be expensive in order to generate a full policy image, while
security_set_bools() is supposed to be substantially cheaper than a full policy load.

Also, the advantage of taking the hash of the original input file is
that you can independently compute a reference hash offline or on the
server from the same policy file and compare them and you can identify
which policy file was loaded based on the hash.

Quoted 8 lines

If you care about the active boolean state, then I'd suggest hashing
the active boolean state separately and storing that after the policy
hash.  You can do that in both security_load_policy() and security_set_bools().  Just iterate through the bools like security_set_bools() does, write the ->state of each boolean into a buffer, and then hash that buffer.

Quoted 8 lines

I just noticed another issue: with the method of computing the checksum on a (data, len) pair on entry to security_load_policy(), the
checksum does not change after inserting a new module with semodule. It is a problem as a module can allow actions by certain users on some
file contexts. So not detecting that kind of policy tampering defeats the purpose of the checksum as I imagine it.

You seem to be conflating kernel policy with userspace policy. security_load_policy() is provided with the kernel policy image, which is the result of linking the kernel-relevant portions of all policy modules together. A hash of that image will change if you insert a policy module that affects the kernel policy in any way. But a change that only affects userspace policy isn't ever going to be reflected in the kernel. It doesn't matter where or when you compute your checksum within the kernel; it isn't ever going to reflect those userspace policy changes.

Quoted 11 lines

To address this I propose to come back to the idea of the notifier. The checksum would not be stored inside the struct policydb. The checksum would be computed on a (data, len) pair got from security_read_policy() every time someone is asking for it through the
security_policy_cksum() hook. The ones that would potentially call security_policy_cksum() are those that would register a callback on lsm_notifier, and the userspace processes reading /sys/fs/selinux/policycksum. So no matter if computing the checksum gets expensive, that would be the caller's responsibility to use it with care. Just like with /sys/fs/selinux/policy today in fact.

This won't detect changes to userspace policy configurations either, and it is less efficient than just computing/updating the checksum in security_load_policy() and security_set_bools(). Also, if all you want is a hash of /sys/fs/selinux/policy, then userspace can already read and hash that itself at any time. You aren't really providing any additional information that way. In contrast, saving and providing a hash of the policy image that was loaded is not something that is currently available, and could be useful in checking against a reference hash of the policy file or in identifying which policy file was loaded.

Quoted 22 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

You needed to get (global) enforcing mode too, didn't you?  That's
separate from the policy.

Exactly, I also need to rework the patch I proposed about this, in
light of the comments I received.

Quoted 8 lines

So perhaps what you really want is a hook interface and a selinuxfs interface that returns a single string that encodes all of the policy
properties that you care about?  Rather than separate hooks and interfaces?  You could embed the enforcing status in the string too.
Should probably include checkreqprot as well since that affects enforcement of mmap/mprotect checks.

True, I should build a string of the form:
<0 or 1 for enforce>:<0 or 1 for checkreqprot>:<hashalg>=<global checksum>
I should probably rename it 'policybrief' instead of 'policycksum'.

Quoted 5 lines

I realize that the 'SELinux user to UNIX user' assignments are important as well. If for instance a regular user on a given cluster node is mapped to unconfined_u instead of user_u, this user would erroneously have major privileges. I do not know where I should look for this information, and possibly compute another checksum.

As above, that's userspace policy configuration, and not something that kernel can or should deal with.

#9 lore raw

Re: [PATCH 2/3] selinux: add checksum to policydb

2017-04-28 17:50 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 9 lines

You seem to be conflating kernel policy with userspace policy. security_load_policy() is provided with the kernel policy image, which is the result of linking the kernel-relevant portions of all policy modules together. A hash of that image will change if you insert a policy module that affects the kernel policy in any way. But a change that only affects userspace policy isn't ever going to be reflected in the kernel. It doesn't matter where or when you compute your checksum within the kernel; it isn't ever going to reflect those userspace policy changes.

Here is the content of the module is used for my tests:

#============= user_t ==============
allow user_t mnt_t:dir { write add_name };
allow user_t mnt_t:file { write create };

After loading the .pp corresponding to it, I can see that with the method of computing the checksum on the (data, len) pair on entry to security_load_policy(), the checksum does not change. However, when using the (data, len) pair got from
security_read_policy(), the checksum changes. And when I remove the module, the checksum is back to its previous value.
So this is what makes me think there is a difference. Am I missing something? --
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

Re: [PATCH 2/3] selinux: add checksum to policydb

Quoted 21 lines · Sebastien Buisson

2017-04-28 17:50 GMT+02:00 Stephen Smalley <[email protected]>:

Quoted 5 lines

You seem to be conflating kernel policy with userspace policy. security_load_policy() is provided with the kernel policy image, which
is the result of linking the kernel-relevant portions of all policy modules together. A hash of that image will change if you insert a

Quoted 8 lines

policy module that affects the kernel policy in any way.  But a change
that only affects userspace policy isn't ever going to be reflected in
the kernel.  It doesn't matter where or when you compute your checksum
within the kernel; it isn't ever going to reflect those userspace policy changes.

Here is the content of the module is used for my tests:

#============= user_t ==============
allow user_t mnt_t:dir { write add_name };
allow user_t mnt_t:file { write create };

Quoted 8 lines

After loading the .pp corresponding to it, I can see that with the method of computing the checksum on the (data, len) pair on entry to security_load_policy(), the checksum does not change. However, when using the (data, len) pair got from security_read_policy(), the checksum changes. And when I remove the module, the checksum is back to its previous value.
So this is what makes me think there is a difference. Am I missing something?

Policy is loaded via security_load_policy(), so the policy image has to go through it in the first place to be loaded (ignoring kernel exploits or direct /dev/mem access). You couldn't have loaded the modified policy with your new rules without the modified policy getting processed by security_load_policy(). So I'm assuming there is a bug in your code or your testing.