All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-05-25 16:50 Michael LeMay
  2006-05-26  9:37 ` David Howells
                   ` (3 more replies)
  0 siblings, 4 replies; 41+ messages in thread
From: Michael LeMay @ 2006-05-25 16:50 UTC (permalink / raw)
  To: jmorris, selinux, sds, dhowells

The patch below provides SELinux support for the access key retention
subsystem of the kernel.

This revision incorporates the changes previously suggested on this
list, and also includes an extension to the key_alloc security hook,
to allow its callers to pass in a task representing the proper context
for the key being allocated (previously, the "current" pointer was
used).  This is important in a number of functions where the key
framework is generating keypairs for specific threads and processes,
primarily in process_keys.c.  Actually, I was a bit concerned that
no locking is performed on those task structures while the keyring
is being allocated.  David, are you confident that no locking is
required, and that those task structures will persist across the
calls to keyring_alloc?

I also added two lines in the SELinux initialization routine to
add security information to the two default keyrings associated
with root.  I would have liked to put this code in the
initialization routine for the key subsystem, but it is actually
initialized before SELinux, making this impossible.  One interesting
result of initializing root's keyrings during kernel initialization
is that they are assigned the type "kernel_t", rather than "xdm_t"
as is the case with all ordinary users.  I hope this is an acceptable
labeling...

One other notable change in this patch is the addition of the "lookup"
permission to the "key" security class.  This permission is passed to
SELinux whenever the key handling code performs a null permission check.
This will allow the SELinux policy to control existence checks on keys,
perhaps eliminating some covert channels.  However, many other channels
still exist in the system, since global serial number and type namespaces
are used.  These channels may need to be addressed to make the key
subsystem compliant with MLS, but that's another project...

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

---

 include/linux/key.h                          |   18 +++++--
 include/linux/security.h                     |   10 ++-
 kernel/user.c                                |    2
 security/dummy.c                             |    2
 security/keys/key.c                          |    8 +--
 security/keys/keyring.c                      |    5 +
 security/keys/process_keys.c                 |   15 +++--
 security/keys/request_key.c                  |    6 +-
 security/keys/request_key_auth.c             |    2
 security/selinux/hooks.c                     |   69 +++++++++++++++++++++++++++
 security/selinux/include/av_perm_to_string.h |    7 ++
 security/selinux/include/av_permissions.h    |    9 +++
 security/selinux/include/avc.h               |    1
 security/selinux/include/class_to_string.h   |    1
 security/selinux/include/flask.h             |    1
 security/selinux/include/objsec.h            |    5 +
 16 files changed, 134 insertions(+), 27 deletions(-)

diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/include/linux/key.h linux-2.6.17-rc4-mm3/include/linux/key.h
--- linux-2.6.17-rc4-mm3.orig/include/linux/key.h	2006-03-20 00:53:29.000000000 -0500
+++ linux-2.6.17-rc4-mm3/include/linux/key.h	2006-05-25 12:14:11.000000000 -0400
@@ -241,8 +241,9 @@ extern void unregister_key_type(struct k
 
 extern struct key *key_alloc(struct key_type *type,
 			     const char *desc,
-			     uid_t uid, gid_t gid, key_perm_t perm,
-			     int not_in_quota);
+			     uid_t uid, gid_t gid,
+			     struct task_struct *ctx,
+			     key_perm_t perm, int not_in_quota);
 extern int key_payload_reserve(struct key *key, size_t datalen);
 extern int key_instantiate_and_link(struct key *key,
 				    const void *data,
@@ -292,7 +293,9 @@ extern int key_unlink(struct key *keyrin
 		      struct key *key);
 
 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-				 int not_in_quota, struct key *dest);
+				 struct task_struct *ctx,
+				 int not_in_quota,
+				 struct key *dest);
 
 extern int keyring_clear(struct key *keyring);
 
@@ -313,7 +316,8 @@ extern void keyring_replace_payload(stru
  * the userspace interface
  */
 extern struct key root_user_keyring, root_session_keyring;
-extern int alloc_uid_keyring(struct user_struct *user);
+extern int alloc_uid_keyring(struct user_struct *user,
+			     struct task_struct *ctx);
 extern void switch_uid_keyring(struct user_struct *new_user);
 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
 extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -342,7 +346,7 @@ extern void key_init(void);
 #define make_key_ref(k)			({ NULL; })
 #define key_ref_to_ptr(k)		({ NULL; })
 #define is_key_possessed(k)		0
-#define alloc_uid_keyring(u)		0
+#define alloc_uid_keyring(u,c)		0
 #define switch_uid_keyring(u)		do { } while(0)
 #define __install_session_keyring(t, k)	({ NULL; })
 #define copy_keys(f,t)			0
@@ -355,6 +359,10 @@ extern void key_init(void);
 #define key_fsgid_changed(t)		do { } while(0)
 #define key_init()			do { } while(0)
 
+/* Initial keyrings */
+extern struct key root_user_keyring;
+extern struct key root_session_keyring;
+
 #endif /* CONFIG_KEYS */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_KEY_H */
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/include/linux/security.h linux-2.6.17-rc4-mm3/include/linux/security.h
--- linux-2.6.17-rc4-mm3.orig/include/linux/security.h	2006-05-24 09:01:42.000000000 -0400
+++ linux-2.6.17-rc4-mm3/include/linux/security.h	2006-05-25 12:16:49.000000000 -0400
@@ -1305,7 +1305,7 @@ struct security_operations {
 
 	/* key management security hooks */
 #ifdef CONFIG_KEYS
-	int (*key_alloc)(struct key *key);
+	int (*key_alloc)(struct key *key, struct task_struct *tsk);
 	void (*key_free)(struct key *key);
 	int (*key_permission)(key_ref_t key_ref,
 			      struct task_struct *context,
@@ -2980,9 +2980,10 @@ static inline int security_xfrm_policy_l
 
 #ifdef CONFIG_KEYS
 #ifdef CONFIG_SECURITY
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
-	return security_ops->key_alloc(key);
+	return security_ops->key_alloc(key, tsk);
 }
 
 static inline void security_key_free(struct key *key)
@@ -2999,7 +3000,8 @@ static inline int security_key_permissio
 
 #else
 
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/kernel/user.c linux-2.6.17-rc4-mm3/kernel/user.c
--- linux-2.6.17-rc4-mm3.orig/kernel/user.c	2006-05-24 09:01:43.000000000 -0400
+++ linux-2.6.17-rc4-mm3/kernel/user.c	2006-05-25 09:00:52.000000000 -0400
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid
 		new->mq_bytes = 0;
 		new->locked_shm = 0;
 
-		if (alloc_uid_keyring(new) < 0) {
+		if (alloc_uid_keyring(new, current) < 0) {
 			kmem_cache_free(uid_cachep, new);
 			return NULL;
 		}
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/dummy.c linux-2.6.17-rc4-mm3/security/dummy.c
--- linux-2.6.17-rc4-mm3.orig/security/dummy.c	2006-05-24 09:01:44.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/dummy.c	2006-05-25 09:37:20.000000000 -0400
@@ -850,7 +850,7 @@ static int dummy_setprocattr(struct task
 }
 
 #ifdef CONFIG_KEYS
-static inline int dummy_key_alloc(struct key *key)
+static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/keys/key.c linux-2.6.17-rc4-mm3/security/keys/key.c
--- linux-2.6.17-rc4-mm3.orig/security/keys/key.c	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/keys/key.c	2006-05-25 09:33:41.000000000 -0400
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(stru
  *   instantiate the key or discard it before returning
  */
 struct key *key_alloc(struct key_type *type, const char *desc,
-		      uid_t uid, gid_t gid, key_perm_t perm,
-		      int not_in_quota)
+		      uid_t uid, gid_t gid, struct task_struct *ctx,
+		      key_perm_t perm, int not_in_quota)
 {
 	struct key_user *user = NULL;
 	struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *t
 #endif
 
 	/* let the security module know about the key */
-	ret = security_key_alloc(key);
+	ret = security_key_alloc(key, ctx);
 	if (ret < 0)
 		goto security_error;
 
@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t
 
 	/* allocate a new key */
 	key = key_alloc(ktype, description, current->fsuid, current->fsgid,
-			perm, not_in_quota);
+			current, perm, not_in_quota);
 	if (IS_ERR(key)) {
 		key_ref = ERR_PTR(PTR_ERR(key));
 		goto error_3;
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/keys/keyring.c linux-2.6.17-rc4-mm3/security/keys/keyring.c
--- linux-2.6.17-rc4-mm3.orig/security/keys/keyring.c	2006-05-24 09:01:44.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/keys/keyring.c	2006-05-25 08:56:08.000000000 -0400
@@ -240,13 +240,14 @@ static long keyring_read(const struct ke
  * allocate a keyring and link into the destination keyring
  */
 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-			  int not_in_quota, struct key *dest)
+			  struct task_struct *ctx, int not_in_quota,
+			  struct key *dest)
 {
 	struct key *keyring;
 	int ret;
 
 	keyring = key_alloc(&key_type_keyring, description,
-			    uid, gid,
+			    uid, gid, ctx,
 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
 			    not_in_quota);
 
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/keys/process_keys.c linux-2.6.17-rc4-mm3/security/keys/process_keys.c
--- linux-2.6.17-rc4-mm3.orig/security/keys/process_keys.c	2006-05-24 09:01:44.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/keys/process_keys.c	2006-05-25 12:18:25.000000000 -0400
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
 /*
  * allocate the keyrings to be associated with a UID
  */
-int alloc_uid_keyring(struct user_struct *user)
+int alloc_uid_keyring(struct user_struct *user,
+		      struct task_struct *ctx)
 {
 	struct key *uid_keyring, *session_keyring;
 	char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct
 	/* concoct a default session keyring */
 	sprintf(buf, "_uid_ses.%u", user->uid);
 
-	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
+	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
 	if (IS_ERR(session_keyring)) {
 		ret = PTR_ERR(session_keyring);
 		goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct
 	 * keyring */
 	sprintf(buf, "_uid.%u", user->uid);
 
-	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
+	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
 				    session_keyring);
 	if (IS_ERR(uid_keyring)) {
 		key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_s
 
 	sprintf(buf, "_tid.%u", tsk->pid);
 
-	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_
 	if (!tsk->signal->process_keyring) {
 		sprintf(buf, "_pid.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struc
 	if (!keyring) {
 		sprintf(buf, "_ses.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring))
 			return PTR_ERR(keyring);
 	}
@@ -717,7 +718,7 @@ long join_session_keyring(const char *na
 	keyring = find_keyring_by_name(name, 0);
 	if (PTR_ERR(keyring) == -ENOKEY) {
 		/* not found - try and create a new one */
-		keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
+		keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error2;
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/keys/request_key_auth.c linux-2.6.17-rc4-mm3/security/keys/request_key_auth.c
--- linux-2.6.17-rc4-mm3.orig/security/keys/request_key_auth.c	2006-03-20 00:53:29.000000000 -0500
+++ linux-2.6.17-rc4-mm3/security/keys/request_key_auth.c	2006-05-25 09:35:50.000000000 -0400
@@ -148,7 +148,7 @@ struct key *request_key_auth_new(struct 
 	sprintf(desc, "%x", target->serial);
 
 	authkey = key_alloc(&key_type_request_key_auth, desc,
-			    current->fsuid, current->fsgid,
+			    current->fsuid, current->fsgid, current,
 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
 			    KEY_USR_VIEW, 1);
 	if (IS_ERR(authkey)) {
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/keys/request_key.c linux-2.6.17-rc4-mm3/security/keys/request_key.c
--- linux-2.6.17-rc4-mm3.orig/security/keys/request_key.c	2006-03-20 00:53:29.000000000 -0500
+++ linux-2.6.17-rc4-mm3/security/keys/request_key.c	2006-05-25 09:35:19.000000000 -0400
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct 
 	/* allocate a new session keyring */
 	sprintf(desc, "_req.%u", key->serial);
 
-	keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
+	keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
+				current, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_constru
 
 	/* create a key and add it to the queue */
 	key = key_alloc(type, description,
-			current->fsuid, current->fsgid, KEY_POS_ALL, 0);
+			current->fsuid, current->fsgid,
+			current, KEY_POS_ALL, 0);
 	if (IS_ERR(key))
 		goto alloc_failed;
 
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/hooks.c linux-2.6.17-rc4-mm3/security/selinux/hooks.c
--- linux-2.6.17-rc4-mm3.orig/security/selinux/hooks.c	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/selinux/hooks.c	2006-05-25 09:36:32.000000000 -0400
@@ -4252,6 +4252,62 @@ static int selinux_setprocattr(struct ta
 	return size;
 }
 
+#ifdef CONFIG_KEYS
+
+static int selinux_key_alloc (struct key *k, struct task_struct *tsk)
+{
+	struct task_security_struct *tsec = tsk->security;
+	struct key_security_struct *ksec;
+
+	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
+	if (!ksec)
+		return -ENOMEM;
+
+	ksec->obj = k;
+	ksec->sid = tsec->sid;
+	k->security = ksec;
+
+	return 0;
+}
+
+static void selinux_key_free (struct key *k)
+{
+	struct key_security_struct *ksec = k->security;
+
+	k->security = NULL;
+	kfree(ksec);
+}
+
+static int selinux_key_permission (key_ref_t key_ref,
+			    struct task_struct *ctx,
+			    key_perm_t perm)
+{
+	struct key * key;
+	struct task_security_struct *tsec;
+	struct key_security_struct *ksec;
+	struct avc_audit_data ad;
+
+	key = key_ref_to_ptr(key_ref);
+
+	tsec = ctx->security;
+	ksec = key->security;
+
+	AVC_AUDIT_DATA_INIT(&ad, KEY);
+	ad.tsk = ctx;
+	
+	/* even if no specific permissions are requested,
+	   ensure that it is permissible to return information
+	   about the existence of a key */
+	if (perm == 0) {
+		perm = KEY__LOOKUP;
+	}
+
+	return avc_has_perm(tsec->sid, ksec->sid,
+			    SECCLASS_KEY, perm, &ad);
+}
+
+#endif
+
 static struct security_operations selinux_ops = {
 	.ptrace =			selinux_ptrace,
 	.capget =			selinux_capget,
@@ -4404,6 +4460,12 @@ static struct security_operations selinu
 	.xfrm_state_free_security =	selinux_xfrm_state_free,
 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
 #endif
+
+#ifdef CONFIG_KEYS
+	.key_alloc = selinux_key_alloc,
+	.key_free = selinux_key_free,
+	.key_permission = selinux_key_permission,
+#endif
 };
 
 static __init int selinux_init(void)
@@ -4439,6 +4501,13 @@ static __init int selinux_init(void)
 	} else {
 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
 	}
+
+#ifdef CONFIG_KEYS
+	/* Add security information to initial keyrings */
+	security_key_alloc(&root_user_keyring, current);
+	security_key_alloc(&root_session_keyring, current);
+#endif
+
 	return 0;
 }
 
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/avc.h linux-2.6.17-rc4-mm3/security/selinux/include/avc.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/avc.h	2006-03-20 00:53:29.000000000 -0500
+++ linux-2.6.17-rc4-mm3/security/selinux/include/avc.h	2006-05-24 11:13:15.000000000 -0400
@@ -43,6 +43,7 @@ struct avc_audit_data {
 #define AVC_AUDIT_DATA_NET  2
 #define AVC_AUDIT_DATA_CAP  3
 #define AVC_AUDIT_DATA_IPC  4
+#define AVC_AUDIT_DATA_KEY  5
 	struct task_struct *tsk;
 	union 	{
 		struct {
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc4-mm3/security/selinux/include/av_permissions.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_permissions.h	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/selinux/include/av_permissions.h	2006-05-24 11:00:08.000000000 -0400
@@ -959,3 +959,12 @@
 #define PACKET__SEND                              0x00000001UL
 #define PACKET__RECV                              0x00000002UL
 #define PACKET__RELABELTO                         0x00000004UL
+
+#define KEY__VIEW                                 0x00000001UL
+#define KEY__READ                                 0x00000002UL
+#define KEY__WRITE                                0x00000004UL
+#define KEY__SEARCH                               0x00000008UL
+#define KEY__LINK                                 0x00000010UL
+#define KEY__SETATTR                              0x00000020UL
+#define KEY__LOOKUP                               0x00000040UL
+
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_perm_to_string.h linux-2.6.17-rc4-mm3/security/selinux/include/av_perm_to_string.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_perm_to_string.h	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/selinux/include/av_perm_to_string.h	2006-05-24 11:00:08.000000000 -0400
@@ -242,3 +242,10 @@
    S_(SECCLASS_PACKET, PACKET__SEND, "send")
    S_(SECCLASS_PACKET, PACKET__RECV, "recv")
    S_(SECCLASS_PACKET, PACKET__RELABELTO, "relabelto")
+   S_(SECCLASS_KEY, KEY__VIEW, "view")
+   S_(SECCLASS_KEY, KEY__READ, "read")
+   S_(SECCLASS_KEY, KEY__WRITE, "write")
+   S_(SECCLASS_KEY, KEY__SEARCH, "search")
+   S_(SECCLASS_KEY, KEY__LINK, "link")
+   S_(SECCLASS_KEY, KEY__SETATTR, "setattr")
+   S_(SECCLASS_KEY, KEY__LOOKUP, "lookup")
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/class_to_string.h linux-2.6.17-rc4-mm3/security/selinux/include/class_to_string.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/class_to_string.h	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/selinux/include/class_to_string.h	2006-05-24 11:00:08.000000000 -0400
@@ -60,3 +60,4 @@
     S_("netlink_kobject_uevent_socket")
     S_("appletalk_socket")
     S_("packet")
+    S_("key")
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/flask.h linux-2.6.17-rc4-mm3/security/selinux/include/flask.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/flask.h	2006-05-24 09:01:58.000000000 -0400
+++ linux-2.6.17-rc4-mm3/security/selinux/include/flask.h	2006-05-24 11:00:08.000000000 -0400
@@ -62,6 +62,7 @@
 #define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET           55
 #define SECCLASS_APPLETALK_SOCKET                        56
 #define SECCLASS_PACKET                                  57
+#define SECCLASS_KEY                                     58
 
 /*
  * Security identifier indices for initial entities
diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/objsec.h linux-2.6.17-rc4-mm3/security/selinux/include/objsec.h
--- linux-2.6.17-rc4-mm3.orig/security/selinux/include/objsec.h	2006-03-20 00:53:29.000000000 -0500
+++ linux-2.6.17-rc4-mm3/security/selinux/include/objsec.h	2006-05-24 09:09:24.000000000 -0400
@@ -99,6 +99,11 @@ struct sk_security_struct {
 	u32 peer_sid;			/* SID of peer */
 };
 
+struct key_security_struct {
+	struct key *obj; /* back pointer */
+	u32 sid;         /* SID of key */
+};
+
 extern unsigned int selinux_checkreqprot;
 
 #endif /* _SELINUX_OBJSEC_H_ */



--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-25 16:50 [PATCH] selinux: Kernel access key retention support for SELinux Michael LeMay
@ 2006-05-26  9:37 ` David Howells
  2006-05-26 13:54   ` David Howells
  2006-05-26 12:30 ` Stephen Smalley
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 41+ messages in thread
From: David Howells @ 2006-05-26  9:37 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells


Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> This revision incorporates the changes previously suggested on this
> list, and also includes an extension to the key_alloc security hook,
> to allow its callers to pass in a task representing the proper context
> for the key being allocated (previously, the "current" pointer was
> used).  This is important in a number of functions where the key
> framework is generating keypairs for specific threads and processes,
> primarily in process_keys.c.

Looks okay.

> Actually, I was a bit concerned that no locking is performed on those task
> structures while the keyring is being allocated.  David, are you confident
> that no locking is required, and that those task structures will persist
> across the calls to keyring_alloc?

The task struct should be pinned by the caller of key_alloc(), either by the
context being the current process, or by the caller of request_key() being
asleep until the requested key is instantiated.

However, I've now a horrible feeling that I can see a race between two
processes both trying to instantiate the same key: what if one checks the
authorisation key preparatory to searching it, then the second instantiates
and revokes the authorisation key, and then the first searches the context
process's keyrings?

I've a feeling there's a race there; and I've a feeling that I need to make a
revocation key op so that the any ref on the task struct can be discarded
properly when the key is revoked:-/


Anyway, that's the subject for a separate patch.  I don't think it would
change yours:

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>
Signed-Off-By: David Howells <dhowells@redhat.com>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-25 16:50 [PATCH] selinux: Kernel access key retention support for SELinux Michael LeMay
  2006-05-26  9:37 ` David Howells
@ 2006-05-26 12:30 ` Stephen Smalley
  2006-05-26 12:56   ` Stephen Smalley
  2006-05-26 12:31 ` Stephen Smalley
  2006-05-26 13:00 ` Stephen Smalley
  3 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-05-26 12:30 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, dhowells

On Thu, 2006-05-25 at 12:50 -0400, Michael LeMay wrote:
> One interesting
> result of initializing root's keyrings during kernel initialization
> is that they are assigned the type "kernel_t", rather than "xdm_t"
> as is the case with all ordinary users.  I hope this is an acceptable
> labeling...

xdm_t (i.e. the security context of the display manager) on the user
keys doesn't seem right; I would think it should be the context of the
user session itself, so that user sessions with different security
contexts would have distinct key labels (both for role/domain separation
and for level separation).  Anyone know how keys are created by gdm et
al, e.g. is it done from some pam module?  gdm is already instrumented
to call setexeccon() to set the user session security context before
executing the session, but if it is directly creating the key within the
main gdm process (without executing a helper) or if it executes a helper
before performing that setup, the key creation will happen in gdm's
context.  

> One other notable change in this patch is the addition of the "lookup"
> permission to the "key" security class.  This permission is passed to
> SELinux whenever the key handling code performs a null permission check.

Are these null permission checks done as internal tests by the kernel
itself, or do they originate from userspace queries?  If the former,
then failing the lookup based on the user's context could lead to
internal confusion for the kernel (e.g. it decides that the key doesn't
exist, so it creates a duplicate).  

Compatibility question:  If you build a kernel with these checks and try
running it on an existing system today without corresponding policy
updates, does anything critical immediately break?  You mentioned that
gdm is creating keys - what happens if that fails?  Just want to know
how closely we need to coordinate the kernel update with any policy
update.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-25 16:50 [PATCH] selinux: Kernel access key retention support for SELinux Michael LeMay
  2006-05-26  9:37 ` David Howells
  2006-05-26 12:30 ` Stephen Smalley
@ 2006-05-26 12:31 ` Stephen Smalley
  2006-05-26 13:00 ` Stephen Smalley
  3 siblings, 0 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-05-26 12:31 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, dhowells

On Thu, 2006-05-25 at 12:50 -0400, Michael LeMay wrote:
> diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/avc.h linux-2.6.17-rc4-mm3/security/selinux/include/avc.h
> --- linux-2.6.17-rc4-mm3.orig/security/selinux/include/avc.h	2006-03-20 00:53:29.000000000 -0500
> +++ linux-2.6.17-rc4-mm3/security/selinux/include/avc.h	2006-05-24 11:13:15.000000000 -0400
> @@ -43,6 +43,7 @@ struct avc_audit_data {
>  #define AVC_AUDIT_DATA_NET  2
>  #define AVC_AUDIT_DATA_CAP  3
>  #define AVC_AUDIT_DATA_IPC  4
> +#define AVC_AUDIT_DATA_KEY  5
>  	struct task_struct *tsk;
>  	union 	{
>  		struct {

This looks like a leftover from the prior version of the patch.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-26 12:30 ` Stephen Smalley
@ 2006-05-26 12:56   ` Stephen Smalley
  0 siblings, 0 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-05-26 12:56 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, dhowells

On Fri, 2006-05-26 at 08:30 -0400, Stephen Smalley wrote:
> On Thu, 2006-05-25 at 12:50 -0400, Michael LeMay wrote:
> > One interesting
> > result of initializing root's keyrings during kernel initialization
> > is that they are assigned the type "kernel_t", rather than "xdm_t"
> > as is the case with all ordinary users.  I hope this is an acceptable
> > labeling...
> 
> xdm_t (i.e. the security context of the display manager) on the user
> keys doesn't seem right; I would think it should be the context of the
> user session itself, so that user sessions with different security
> contexts would have distinct key labels (both for role/domain separation
> and for level separation).  Anyone know how keys are created by gdm et
> al, e.g. is it done from some pam module?  gdm is already instrumented
> to call setexeccon() to set the user session security context before
> executing the session, but if it is directly creating the key within the
> main gdm process (without executing a helper) or if it executes a helper
> before performing that setup, the key creation will happen in gdm's
> context.  

Ah, wait - is this just the keyring allocated upon set*uid via
alloc_uid_keyring()?  In that case, then those keyrings will always be
labeled with the security label of the login-like program rather than
the user session itself.  So keyrings created via a gdm login will have
one label, while keyrings created by local login or by sshd will have
another label.  But multiple user logins via the same mechanism will all
get the same label on that keyring.

Now, if we introduced a /proc/pid/attr/keycreate node for specifying a
desired label for a new key/keyring (controlled via a separate create
permission in the key class, and modified the login-like programs to
call an interface to set that to match the user session security context
(e.g. setkeycreatecon()), then we could put those keyrings into the user
session security context.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-25 16:50 [PATCH] selinux: Kernel access key retention support for SELinux Michael LeMay
                   ` (2 preceding siblings ...)
  2006-05-26 12:31 ` Stephen Smalley
@ 2006-05-26 13:00 ` Stephen Smalley
  3 siblings, 0 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-05-26 13:00 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, dhowells

On Thu, 2006-05-25 at 12:50 -0400, Michael LeMay wrote:
> diff -uprN -X linux-2.6.17-rc4-mm3/Documentation/dontdiff linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc4-mm3/security/selinux/include/av_permissions.h
> --- linux-2.6.17-rc4-mm3.orig/security/selinux/include/av_permissions.h	2006-05-24 09:01:58.000000000 -0400
> +++ linux-2.6.17-rc4-mm3/security/selinux/include/av_permissions.h	2006-05-24 11:00:08.000000000 -0400
> @@ -959,3 +959,12 @@
>  #define PACKET__SEND                              0x00000001UL
>  #define PACKET__RECV                              0x00000002UL
>  #define PACKET__RELABELTO                         0x00000004UL
> +
> +#define KEY__VIEW                                 0x00000001UL
> +#define KEY__READ                                 0x00000002UL
> +#define KEY__WRITE                                0x00000004UL
> +#define KEY__SEARCH                               0x00000008UL
> +#define KEY__LINK                                 0x00000010UL
> +#define KEY__SETATTR                              0x00000020UL
> +#define KEY__LOOKUP                               0x00000040UL
> +

I know that these symbols are auto-generated and just following our
existing convention (class__perm), but in this case, we are rather close
to a collision with the key subsystem's own definitions of its
permissions (only differing in the extra underscore).  Of course, they
are supposed to match in value with the key subsystem's own definitions
anyway so that we don't have to do any translation at permission check
time, but it seems a little worrisome.  Possibly we need to fix the
namespace issues here (via separate patch) by adding a SE_ prefix or
similar to all of these symbols.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-26  9:37 ` David Howells
@ 2006-05-26 13:54   ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-05-26 13:54 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux, sds

David Howells <dhowells@redhat.com> wrote:

> However, I've now a horrible feeling that I can see a race between two
> processes both trying to instantiate the same key: what if one checks the
> authorisation key preparatory to searching it, then the second instantiates
> and revokes the authorisation key, and then the first searches the context
> process's keyrings?

How about the attached patch?

David

---
The attached patch adds a revocation notification method to the key type and
calls it whilst the key's semaphore is still write-locked after setting the
revocation flag.

The patch then uses this to maintain a reference on the task_struct of the
process that calls request_key() for as long as the authorisation key remains
unrevoked.

Access to the "context" pointer in the auth key must thenceforth be done with
the auth key semaphore held.  The revocation method is called with the target
key semaphore held write-locked and the search of the context process's
keyrings is done with the auth key semaphore read-locked.

The check for the revocation state of the auth key just prior to searching it
is done after the auth key is read-locked for the search.  This ensures that
the auth key can't be revoked between the check and the search.

The revocation notification method is added so that the context task_struct
can be released as soon as instantiation happens rather than waiting for the
auth key to be destroyed, thus avoiding the unnecessary pinning of the
requesting process.

Signed-Off-By: David Howells <dhowells@redhat.com>
---
warthog>diffstat -p1 keys-revoke-2617rc5.diff
 Documentation/keys.txt           |   10 ++++++++
 include/linux/key.h              |    5 ++++
 security/keys/key.c              |    4 +++
 security/keys/process_keys.c     |   43 +++++++++++++++++++++++--------------
 security/keys/request_key_auth.c |   45 ++++++++++++++++++++++++++++++++++++++-
 5 files changed, 90 insertions(+), 17 deletions(-)

diff -uNrp linux-2.6.17-rc5/Documentation/keys.txt linux-2.6.17-rc5-keys/Documentation/keys.txt
--- linux-2.6.17-rc5/Documentation/keys.txt	2006-05-26 10:54:09.000000000 +0100
+++ linux-2.6.17-rc5-keys/Documentation/keys.txt	2006-05-26 14:41:47.000000000 +0100
@@ -935,6 +935,16 @@ The structure has a number of fields, so
      It is not safe to sleep in this method; the caller may hold spinlocks.
 
 
+ (*) void (*revoke)(struct key *key);
+
+     This method is optional.  It is called to discard part of the payload
+     data upon a key being revoked.  The caller will have the key semaphore
+     write-locked.
+
+     It is safe to sleep in this method, though care should be taken to avoid
+     a deadlock against the key semaphore.
+
+
  (*) void (*destroy)(struct key *key);
 
      This method is optional. It is called to discard the payload data on a key
diff -uNrp linux-2.6.17-rc5/include/linux/key.h linux-2.6.17-rc5-keys/include/linux/key.h
--- linux-2.6.17-rc5/include/linux/key.h	2006-05-26 10:54:33.000000000 +0100
+++ linux-2.6.17-rc5-keys/include/linux/key.h	2006-05-26 13:41:30.000000000 +0100
@@ -205,6 +205,11 @@ struct key_type {
 	/* match a key against a description */
 	int (*match)(const struct key *key, const void *desc);
 
+	/* clear some of the data from a key on revokation (optional)
+	 * - the key's semaphore will be write-locked by the caller
+	 */
+	void (*revoke)(struct key *key);
+
 	/* clear the data from a key (optional) */
 	void (*destroy)(struct key *key);
 
diff -uNrp linux-2.6.17-rc5/security/keys/key.c linux-2.6.17-rc5-keys/security/keys/key.c
--- linux-2.6.17-rc5/security/keys/key.c	2006-05-26 10:55:05.000000000 +0100
+++ linux-2.6.17-rc5-keys/security/keys/key.c	2006-05-26 10:59:47.000000000 +0100
@@ -907,6 +907,10 @@ void key_revoke(struct key *key)
 	 * it */
 	down_write(&key->sem);
 	set_bit(KEY_FLAG_REVOKED, &key->flags);
+
+	if (key->type->revoke)
+		key->type->revoke(key);
+
 	up_write(&key->sem);
 
 } /* end key_revoke() */
diff -uNrp linux-2.6.17-rc5/security/keys/process_keys.c linux-2.6.17-rc5-keys/security/keys/process_keys.c
--- linux-2.6.17-rc5/security/keys/process_keys.c	2006-05-26 10:55:05.000000000 +0100
+++ linux-2.6.17-rc5-keys/security/keys/process_keys.c	2006-05-26 11:38:53.000000000 +0100
@@ -390,6 +390,8 @@ key_ref_t search_process_keyrings(struct
 	struct request_key_auth *rka;
 	key_ref_t key_ref, ret, err;
 
+	might_sleep();
+
 	/* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
 	 * searchable, but we failed to find a key or we found a negative key;
 	 * otherwise we want to return a sample error (probably -EACCES) if
@@ -495,27 +497,36 @@ key_ref_t search_process_keyrings(struct
 	 */
 	if (context->request_key_auth &&
 	    context == current &&
-	    type != &key_type_request_key_auth &&
-	    key_validate(context->request_key_auth) == 0
+	    type != &key_type_request_key_auth
 	    ) {
-		rka = context->request_key_auth->payload.data;
+		/* defend against the auth key being revoked */
+		down_read(&context->request_key_auth->sem);
 
-		key_ref = search_process_keyrings(type, description, match,
-						  rka->context);
+		if (key_validate(context->request_key_auth) == 0) {
+			rka = context->request_key_auth->payload.data;
 
-		if (!IS_ERR(key_ref))
-			goto found;
+			key_ref = search_process_keyrings(type, description,
+							  match, rka->context);
 
-		switch (PTR_ERR(key_ref)) {
-		case -EAGAIN: /* no key */
-			if (ret)
+			up_read(&context->request_key_auth->sem);
+
+			if (!IS_ERR(key_ref))
+				goto found;
+
+			switch (PTR_ERR(key_ref)) {
+			case -EAGAIN: /* no key */
+				if (ret)
+					break;
+			case -ENOKEY: /* negative key */
+				ret = key_ref;
 				break;
-		case -ENOKEY: /* negative key */
-			ret = key_ref;
-			break;
-		default:
-			err = key_ref;
-			break;
+			default:
+				err = key_ref;
+				break;
+			}
+		}
+		else {
+			up_read(&context->request_key_auth->sem);
 		}
 	}
 
diff -uNrp linux-2.6.17-rc5/security/keys/request_key_auth.c linux-2.6.17-rc5-keys/security/keys/request_key_auth.c
--- linux-2.6.17-rc5/security/keys/request_key_auth.c	2006-05-26 10:54:40.000000000 +0100
+++ linux-2.6.17-rc5-keys/security/keys/request_key_auth.c	2006-05-26 11:39:59.000000000 +0100
@@ -20,6 +20,7 @@
 
 static int request_key_auth_instantiate(struct key *, const void *, size_t);
 static void request_key_auth_describe(const struct key *, struct seq_file *);
+static void request_key_auth_revoke(struct key *);
 static void request_key_auth_destroy(struct key *);
 static long request_key_auth_read(const struct key *, char __user *, size_t);
 
@@ -31,6 +32,7 @@ struct key_type key_type_request_key_aut
 	.def_datalen	= sizeof(struct request_key_auth),
 	.instantiate	= request_key_auth_instantiate,
 	.describe	= request_key_auth_describe,
+	.revoke		= request_key_auth_revoke,
 	.destroy	= request_key_auth_destroy,
 	.read		= request_key_auth_read,
 };
@@ -93,6 +95,24 @@ static long request_key_auth_read(const 
 
 /*****************************************************************************/
 /*
+ * handle revocation of an authorisation token key
+ * - called with the key sem write-locked
+ */
+static void request_key_auth_revoke(struct key *key)
+{
+	struct request_key_auth *rka = key->payload.data;
+
+	kenter("{%d}", key->serial);
+
+	if (rka->context) {
+		put_task_struct(rka->context);
+		rka->context = NULL;
+	}
+	
+} /* end request_key_auth_revoke() */
+
+/*****************************************************************************/
+/*
  * destroy an instantiation authorisation token key
  */
 static void request_key_auth_destroy(struct key *key)
@@ -101,6 +121,11 @@ static void request_key_auth_destroy(str
 
 	kenter("{%d}", key->serial);
 
+	if (rka->context) {
+		put_task_struct(rka->context);
+		rka->context = NULL;
+	}
+
 	key_put(rka->target_key);
 	kfree(rka);
 
@@ -131,14 +156,26 @@ struct key *request_key_auth_new(struct 
 	 * another process */
 	if (current->request_key_auth) {
 		/* it is - use that instantiation context here too */
+		down_read(&current->request_key_auth->sem);
+
+		/* if the auth key has been revoked, then the key we're
+		 * servicing is already instantiated */
+		if (test_bit(KEY_FLAG_REVOKED,
+			     &current->request_key_auth->flags))
+			goto auth_key_revoked;
+
 		irka = current->request_key_auth->payload.data;
 		rka->context = irka->context;
 		rka->pid = irka->pid;
+		get_task_struct(rka->context);
+
+		up_read(&current->request_key_auth->sem);
 	}
 	else {
 		/* it isn't - use this process as the context */
 		rka->context = current;
 		rka->pid = current->pid;
+		get_task_struct(rka->context);
 	}
 
 	rka->target_key = key_get(target);
@@ -161,9 +198,15 @@ struct key *request_key_auth_new(struct 
 	if (ret < 0)
 		goto error_inst;
 
-	kleave(" = {%d})", authkey->serial);
+	kleave(" = {%d}", authkey->serial);
 	return authkey;
 
+auth_key_revoked:
+	up_read(&current->request_key_auth->sem);
+	kfree(rka);
+	kleave("= -EKEYREVOKED");
+	return ERR_PTR(-EKEYREVOKED);
+
 error_inst:
 	key_revoke(authkey);
 	key_put(authkey);

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-05-31 17:50 Michael LeMay
  2006-05-31 18:29 ` Stephen Smalley
                   ` (3 more replies)
  0 siblings, 4 replies; 41+ messages in thread
From: Michael LeMay @ 2006-05-31 17:50 UTC (permalink / raw)
  To: jmorris, selinux, sds, dhowells

This revision of my patch incorporates a few more changes suggested
by Stephen.  I eliminated the "lookup" permission, due to the possibility
for kernel confusion, and because of its strange semantics (e.g. it was
possible to write to a key without performing a lookup on it).  I also added
an unlink permission to the keys subsystem and SELinux, which is checked
before a key is unlinked from a keyring.  I felt this change was fairly
trivial and within the overall spirit of the patch, which is why I
included it here.  If any of you decide it should be split off into
some other patch, I'd be glad to remove it.

I've tested this patch using all four combinations of SELinux
enabled/disabled and keys enabled/disabled, so I'm fairly
confident that it won't cause any breakage.

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.  Add an "unlink" permission to the key
subsystem.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

---

 include/linux/key-ui.h                       |    5 +-
 include/linux/key.h                          |   18 +++++--
 include/linux/security.h                     |   10 ++--
 kernel/user.c                                |    2
 security/dummy.c                             |    2
 security/keys/key.c                          |    8 +--
 security/keys/keyctl.c                       |    4 -
 security/keys/keyring.c                      |    5 +-
 security/keys/process_keys.c                 |   15 +++---
 security/keys/request_key.c                  |    6 +-
 security/keys/request_key_auth.c             |    2
 security/selinux/hooks.c                     |   66 +++++++++++++++++++++++++++
 security/selinux/include/av_perm_to_string.h |    7 ++
 security/selinux/include/av_permissions.h    |    9 +++
 security/selinux/include/class_to_string.h   |    1
 security/selinux/include/flask.h             |    1
 security/selinux/include/objsec.h            |    5 ++
 17 files changed, 135 insertions(+), 31 deletions(-)

diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/key.h linux-2.6.17-rc5-mm1/include/linux/key.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/key.h	2006-05-31 09:48:38.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/key.h	2006-05-31 10:03:00.000000000 -0400
@@ -246,8 +246,9 @@ extern void unregister_key_type(struct k
 
 extern struct key *key_alloc(struct key_type *type,
 			     const char *desc,
-			     uid_t uid, gid_t gid, key_perm_t perm,
-			     int not_in_quota);
+			     uid_t uid, gid_t gid,
+			     struct task_struct *ctx,
+			     key_perm_t perm, int not_in_quota);
 extern int key_payload_reserve(struct key *key, size_t datalen);
 extern int key_instantiate_and_link(struct key *key,
 				    const void *data,
@@ -297,7 +298,9 @@ extern int key_unlink(struct key *keyrin
 		      struct key *key);
 
 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-				 int not_in_quota, struct key *dest);
+				 struct task_struct *ctx,
+				 int not_in_quota,
+				 struct key *dest);
 
 extern int keyring_clear(struct key *keyring);
 
@@ -318,7 +321,8 @@ extern void keyring_replace_payload(stru
  * the userspace interface
  */
 extern struct key root_user_keyring, root_session_keyring;
-extern int alloc_uid_keyring(struct user_struct *user);
+extern int alloc_uid_keyring(struct user_struct *user,
+			     struct task_struct *ctx);
 extern void switch_uid_keyring(struct user_struct *new_user);
 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
 extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -347,7 +351,7 @@ extern void key_init(void);
 #define make_key_ref(k)			({ NULL; })
 #define key_ref_to_ptr(k)		({ NULL; })
 #define is_key_possessed(k)		0
-#define alloc_uid_keyring(u)		0
+#define alloc_uid_keyring(u,c)		0
 #define switch_uid_keyring(u)		do { } while(0)
 #define __install_session_keyring(t, k)	({ NULL; })
 #define copy_keys(f,t)			0
@@ -360,6 +364,10 @@ extern void key_init(void);
 #define key_fsgid_changed(t)		do { } while(0)
 #define key_init()			do { } while(0)
 
+/* Initial keyrings */
+extern struct key root_user_keyring;
+extern struct key root_session_keyring;
+
 #endif /* CONFIG_KEYS */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_KEY_H */
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/key-ui.h linux-2.6.17-rc5-mm1/include/linux/key-ui.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/key-ui.h	2006-05-31 09:48:38.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/key-ui.h	2006-05-31 10:18:19.000000000 -0400
@@ -24,8 +24,9 @@ extern spinlock_t key_serial_lock;
 #define	KEY_WRITE	0x04	/* require permission to update / modify */
 #define	KEY_SEARCH	0x08	/* require permission to search (keyring) or find (key) */
 #define	KEY_LINK	0x10	/* require permission to link */
-#define	KEY_SETATTR	0x20	/* require permission to change attributes */
-#define	KEY_ALL		0x3f	/* all the above permissions */
+#define	KEY_UNLINK	0x20	/* require permission to link */
+#define	KEY_SETATTR	0x40	/* require permission to change attributes */
+#define	KEY_ALL		0x7f	/* all the above permissions */
 
 /*
  * the keyring payload contains a list of the keys to which the keyring is
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/security.h linux-2.6.17-rc5-mm1/include/linux/security.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/security.h	2006-05-31 09:48:39.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/security.h	2006-05-31 10:03:00.000000000 -0400
@@ -1305,7 +1305,7 @@ struct security_operations {
 
 	/* key management security hooks */
 #ifdef CONFIG_KEYS
-	int (*key_alloc)(struct key *key);
+	int (*key_alloc)(struct key *key, struct task_struct *tsk);
 	void (*key_free)(struct key *key);
 	int (*key_permission)(key_ref_t key_ref,
 			      struct task_struct *context,
@@ -2980,9 +2980,10 @@ static inline int security_xfrm_policy_l
 
 #ifdef CONFIG_KEYS
 #ifdef CONFIG_SECURITY
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
-	return security_ops->key_alloc(key);
+	return security_ops->key_alloc(key, tsk);
 }
 
 static inline void security_key_free(struct key *key)
@@ -2999,7 +3000,8 @@ static inline int security_key_permissio
 
 #else
 
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/kernel/user.c linux-2.6.17-rc5-mm1/kernel/user.c
--- linux-2.6.17-rc5-mm1.orig/kernel/user.c	2006-05-31 09:48:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/kernel/user.c	2006-05-31 10:03:00.000000000 -0400
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid
 		new->mq_bytes = 0;
 		new->locked_shm = 0;
 
-		if (alloc_uid_keyring(new) < 0) {
+		if (alloc_uid_keyring(new, current) < 0) {
 			kmem_cache_free(uid_cachep, new);
 			return NULL;
 		}
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/dummy.c linux-2.6.17-rc5-mm1/security/dummy.c
--- linux-2.6.17-rc5-mm1.orig/security/dummy.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/dummy.c	2006-05-31 10:03:00.000000000 -0400
@@ -850,7 +850,7 @@ static int dummy_setprocattr(struct task
 }
 
 #ifdef CONFIG_KEYS
-static inline int dummy_key_alloc(struct key *key)
+static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/key.c linux-2.6.17-rc5-mm1/security/keys/key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/key.c	2006-05-31 10:03:00.000000000 -0400
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(stru
  *   instantiate the key or discard it before returning
  */
 struct key *key_alloc(struct key_type *type, const char *desc,
-		      uid_t uid, gid_t gid, key_perm_t perm,
-		      int not_in_quota)
+		      uid_t uid, gid_t gid, struct task_struct *ctx,
+		      key_perm_t perm, int not_in_quota)
 {
 	struct key_user *user = NULL;
 	struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *t
 #endif
 
 	/* let the security module know about the key */
-	ret = security_key_alloc(key);
+	ret = security_key_alloc(key, ctx);
 	if (ret < 0)
 		goto security_error;
 
@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t
 
 	/* allocate a new key */
 	key = key_alloc(ktype, description, current->fsuid, current->fsgid,
-			perm, not_in_quota);
+			current, perm, not_in_quota);
 	if (IS_ERR(key)) {
 		key_ref = ERR_PTR(PTR_ERR(key));
 		goto error_3;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/keyctl.c linux-2.6.17-rc5-mm1/security/keys/keyctl.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/keyctl.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/keyctl.c	2006-05-31 10:16:06.000000000 -0400
@@ -396,7 +396,7 @@ long keyctl_keyring_link(key_serial_t id
 /*
  * unlink the first attachment of a key from a keyring
  * - the keyring must be writable
- * - we don't need any permissions on the key
+ * - we need unlink permissions on the key
  * - implements keyctl(KEYCTL_UNLINK)
  */
 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
@@ -410,7 +410,7 @@ long keyctl_keyring_unlink(key_serial_t 
 		goto error;
 	}
 
-	key_ref = lookup_user_key(NULL, id, 0, 0, 0);
+	key_ref = lookup_user_key(NULL, id, 0, 0, KEY_UNLINK);
 	if (IS_ERR(key_ref)) {
 		ret = PTR_ERR(key_ref);
 		goto error2;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c linux-2.6.17-rc5-mm1/security/keys/keyring.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/keyring.c	2006-05-31 10:03:00.000000000 -0400
@@ -240,13 +240,14 @@ static long keyring_read(const struct ke
  * allocate a keyring and link into the destination keyring
  */
 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-			  int not_in_quota, struct key *dest)
+			  struct task_struct *ctx, int not_in_quota,
+			  struct key *dest)
 {
 	struct key *keyring;
 	int ret;
 
 	keyring = key_alloc(&key_type_keyring, description,
-			    uid, gid,
+			    uid, gid, ctx,
 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
 			    not_in_quota);
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c linux-2.6.17-rc5-mm1/security/keys/process_keys.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/process_keys.c	2006-05-31 10:03:00.000000000 -0400
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
 /*
  * allocate the keyrings to be associated with a UID
  */
-int alloc_uid_keyring(struct user_struct *user)
+int alloc_uid_keyring(struct user_struct *user,
+		      struct task_struct *ctx)
 {
 	struct key *uid_keyring, *session_keyring;
 	char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct
 	/* concoct a default session keyring */
 	sprintf(buf, "_uid_ses.%u", user->uid);
 
-	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
+	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
 	if (IS_ERR(session_keyring)) {
 		ret = PTR_ERR(session_keyring);
 		goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct
 	 * keyring */
 	sprintf(buf, "_uid.%u", user->uid);
 
-	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
+	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
 				    session_keyring);
 	if (IS_ERR(uid_keyring)) {
 		key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_s
 
 	sprintf(buf, "_tid.%u", tsk->pid);
 
-	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_
 	if (!tsk->signal->process_keyring) {
 		sprintf(buf, "_pid.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struc
 	if (!keyring) {
 		sprintf(buf, "_ses.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring))
 			return PTR_ERR(keyring);
 	}
@@ -727,7 +728,7 @@ long join_session_keyring(const char *na
 	keyring = find_keyring_by_name(name, 0);
 	if (PTR_ERR(keyring) == -ENOKEY) {
 		/* not found - try and create a new one */
-		keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
+		keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error2;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c	2006-05-31 10:03:00.000000000 -0400
@@ -185,7 +185,7 @@ struct key *request_key_auth_new(struct 
 	sprintf(desc, "%x", target->serial);
 
 	authkey = key_alloc(&key_type_request_key_auth, desc,
-			    current->fsuid, current->fsgid,
+			    current->fsuid, current->fsgid, current,
 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
 			    KEY_USR_VIEW, 1);
 	if (IS_ERR(authkey)) {
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c linux-2.6.17-rc5-mm1/security/keys/request_key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key.c	2006-05-31 10:03:00.000000000 -0400
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct 
 	/* allocate a new session keyring */
 	sprintf(desc, "_req.%u", key->serial);
 
-	keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
+	keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
+				current, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_constru
 
 	/* create a key and add it to the queue */
 	key = key_alloc(type, description,
-			current->fsuid, current->fsgid, KEY_POS_ALL, 0);
+			current->fsuid, current->fsgid,
+			current, KEY_POS_ALL, 0);
 	if (IS_ERR(key))
 		goto alloc_failed;
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c linux-2.6.17-rc5-mm1/security/selinux/hooks.c
--- linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/hooks.c	2006-05-31 10:21:30.000000000 -0400
@@ -4252,6 +4252,59 @@ static int selinux_setprocattr(struct ta
 	return size;
 }
 
+#ifdef CONFIG_KEYS
+
+static int selinux_key_alloc (struct key *k, struct task_struct *tsk)
+{
+	struct task_security_struct *tsec = tsk->security;
+	struct key_security_struct *ksec;
+
+	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
+	if (!ksec)
+		return -ENOMEM;
+
+	ksec->obj = k;
+	ksec->sid = tsec->sid;
+	k->security = ksec;
+
+	return 0;
+}
+
+static void selinux_key_free (struct key *k)
+{
+	struct key_security_struct *ksec = k->security;
+
+	k->security = NULL;
+	kfree(ksec);
+}
+
+static int selinux_key_permission (key_ref_t key_ref,
+			    struct task_struct *ctx,
+			    key_perm_t perm)
+{
+	struct key * key;
+	struct task_security_struct *tsec;
+	struct key_security_struct *ksec;
+	struct avc_audit_data ad;
+
+	key = key_ref_to_ptr(key_ref);
+
+	tsec = ctx->security;
+	ksec = key->security;
+
+	/* if no specific permissions are requested, we skip the
+	   permission check. No serious, additional covert channels
+	   appear to be created. */
+	if (perm == 0) {
+		return 0;
+	}
+
+	return avc_has_perm(tsec->sid, ksec->sid,
+			    SECCLASS_KEY, perm, NULL);
+}
+
+#endif
+
 static struct security_operations selinux_ops = {
 	.ptrace =			selinux_ptrace,
 	.capget =			selinux_capget,
@@ -4404,6 +4457,12 @@ static struct security_operations selinu
 	.xfrm_state_free_security =	selinux_xfrm_state_free,
 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
 #endif
+
+#ifdef CONFIG_KEYS
+	.key_alloc = selinux_key_alloc,
+	.key_free = selinux_key_free,
+	.key_permission = selinux_key_permission,
+#endif
 };
 
 static __init int selinux_init(void)
@@ -4439,6 +4498,13 @@ static __init int selinux_init(void)
 	} else {
 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
 	}
+
+#ifdef CONFIG_KEYS
+	/* Add security information to initial keyrings */
+	security_key_alloc(&root_user_keyring, current);
+	security_key_alloc(&root_session_keyring, current);
+#endif
+
 	return 0;
 }
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h	2006-05-31 10:39:56.000000000 -0400
@@ -959,3 +959,12 @@
 #define PACKET__SEND                              0x00000001UL
 #define PACKET__RECV                              0x00000002UL
 #define PACKET__RELABELTO                         0x00000004UL
+
+#define KEY__VIEW                                 0x00000001UL
+#define KEY__READ                                 0x00000002UL
+#define KEY__WRITE                                0x00000004UL
+#define KEY__SEARCH                               0x00000008UL
+#define KEY__LINK                                 0x00000010UL
+#define KEY__UNLINK                               0x00000020UL
+#define KEY__SETATTR                              0x00000040UL
+
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h	2006-05-31 10:39:56.000000000 -0400
@@ -242,3 +242,10 @@
    S_(SECCLASS_PACKET, PACKET__SEND, "send")
    S_(SECCLASS_PACKET, PACKET__RECV, "recv")
    S_(SECCLASS_PACKET, PACKET__RELABELTO, "relabelto")
+   S_(SECCLASS_KEY, KEY__VIEW, "view")
+   S_(SECCLASS_KEY, KEY__READ, "read")
+   S_(SECCLASS_KEY, KEY__WRITE, "write")
+   S_(SECCLASS_KEY, KEY__SEARCH, "search")
+   S_(SECCLASS_KEY, KEY__LINK, "link")
+   S_(SECCLASS_KEY, KEY__UNLINK, "unlink")
+   S_(SECCLASS_KEY, KEY__SETATTR, "setattr")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h	2006-05-31 10:39:56.000000000 -0400
@@ -60,3 +60,4 @@
     S_("netlink_kobject_uevent_socket")
     S_("appletalk_socket")
     S_("packet")
+    S_("key")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h linux-2.6.17-rc5-mm1/security/selinux/include/flask.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/flask.h	2006-05-31 10:39:56.000000000 -0400
@@ -62,6 +62,7 @@
 #define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET           55
 #define SECCLASS_APPLETALK_SOCKET                        56
 #define SECCLASS_PACKET                                  57
+#define SECCLASS_KEY                                     58
 
 /*
  * Security identifier indices for initial entities
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h	2006-05-31 10:03:00.000000000 -0400
@@ -99,6 +99,11 @@ struct sk_security_struct {
 	u32 peer_sid;			/* SID of peer */
 };
 
+struct key_security_struct {
+	struct key *obj; /* back pointer */
+	u32 sid;         /* SID of key */
+};
+
 extern unsigned int selinux_checkreqprot;
 
 #endif /* _SELINUX_OBJSEC_H_ */



--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 17:50 Michael LeMay
@ 2006-05-31 18:29 ` Stephen Smalley
  2006-06-01 10:13   ` David Howells
  2006-05-31 20:42 ` James Morris
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-05-31 18:29 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, dhowells

On Wed, 2006-05-31 at 13:50 -0400, Michael LeMay wrote:
> This revision of my patch incorporates a few more changes suggested
> by Stephen.  I eliminated the "lookup" permission, due to the possibility
> for kernel confusion, and because of its strange semantics (e.g. it was
> possible to write to a key without performing a lookup on it).  I also added
> an unlink permission to the keys subsystem and SELinux, which is checked
> before a key is unlinked from a keyring.  I felt this change was fairly
> trivial and within the overall spirit of the patch, which is why I
> included it here.  If any of you decide it should be split off into
> some other patch, I'd be glad to remove it.

[Capturing an off-list discussion for the list] From a quick look, this
appears to break compatibility as it affects the key user interface.  We
could follow a similar approach to what was done in
fs/namei.c:permission() and open_namei(), where we introduced a new
MAY_APPEND flag, but mask it out before calling the normal DAC
permission checking logic, only passing it to the LSM hook.  We would
just need to reserve a permission bit for KEY_UNLINK for in-kernel use,
possibly the high bit.  No change to the existing key permission bits,
and plenty of room for them to grow still.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 17:50 Michael LeMay
  2006-05-31 18:29 ` Stephen Smalley
@ 2006-05-31 20:42 ` James Morris
  2006-06-01  9:32   ` David Howells
  2006-06-01 10:09 ` David Howells
  2006-06-01 14:40 ` David Howells
  3 siblings, 1 reply; 41+ messages in thread
From: James Morris @ 2006-05-31 20:42 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, Stephen Smalley, dhowells

On Wed, 31 May 2006, Michael LeMay wrote:


> +static void selinux_key_free (struct key *k)

Please don't leave a space between the function name and the parentheses.  
I know this is probably confusing, because unfortunately the LSM code 
does, but it's not consistent with the SELinux code nor most of the rest 
of the kernel.

> +	if (perm == 0) {
> +		return 0;
> +	}

No need for the braces.

>  static struct security_operations selinux_ops = {
>  	.ptrace =			selinux_ptrace,
>  	.capget =			selinux_capget,
> @@ -4404,6 +4457,12 @@ static struct security_operations selinu
>  	.xfrm_state_free_security =	selinux_xfrm_state_free,
>  	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
>  #endif
> +
> +#ifdef CONFIG_KEYS
> +	.key_alloc = selinux_key_alloc,
> +	.key_free = selinux_key_free,
> +	.key_permission = selinux_key_permission,
> +#endif

Inconsistent indenting of the functions with existing code.


> +   S_(SECCLASS_KEY, KEY__VIEW, "view")
> +   S_(SECCLASS_KEY, KEY__READ, "read")

What's the difference between reading and viewing a key?



- James
-- 
James Morris
<jmorris@namei.org>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 20:42 ` James Morris
@ 2006-06-01  9:32   ` David Howells
  2006-06-01 14:26     ` James Morris
  0 siblings, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-01  9:32 UTC (permalink / raw)
  To: James Morris; +Cc: Michael LeMay, jmorris, selinux, Stephen Smalley, dhowells

James Morris <jmorris@namei.org> wrote:

> What's the difference between reading and viewing a key?

"Viewing" is looking at the key's attributes.  "Reading" is reading the
payload of the key.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 17:50 Michael LeMay
  2006-05-31 18:29 ` Stephen Smalley
  2006-05-31 20:42 ` James Morris
@ 2006-06-01 10:09 ` David Howells
  2006-06-01 11:39   ` Stephen Smalley
  2006-06-01 14:40 ` David Howells
  3 siblings, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-01 10:09 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells

Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> This revision of my patch incorporates a few more changes suggested
> by Stephen.  I eliminated the "lookup" permission, due to the possibility
> for kernel confusion, and because of its strange semantics (e.g. it was
> possible to write to a key without performing a lookup on it).

Remember: keys aren't files and they don't exactly follow UNIX filesystem
semantics.

I think the default permissions for a key must be only the possessor is allowed
to do anything to it or with it, and that the user should be granted some
things (setattr, for instance), and that no-one else should be granted
anything.

The default permissions probably need a good overhaul.

> I also added an unlink permission to the keys subsystem and SELinux, which
> is checked before a key is unlinked from a keyring.  I felt this change was
> fairly trivial and within the overall spirit of the patch, which is why I
> included it here.

It isn't as trivial as you think.  Unlink is implied by several operations you
haven't altered: link, add key, request key, clear keyring and search keyring.
This is because you can displace a key link from a keyring by adding a link to
another key of matching type and description.

> If any of you decide it should be split off into some other patch, I'd be
> glad to remove it.

It certainly needs be split off into its own patch as it's a fairly major
change.  Your changes in this area are also incomplete.

I also don't really want unlinkable keys for a number of reasons:

 (1) You can't get rid of them when they're dead (unless you allow a specific
     bypass).

 (2) If they're your keys, they eat your quota and but you may not be able to
     ditch them.

 (3) If someone accidentally zeroes the mask on the key, they're left with a
     key that they can't get rid of.

 (4) What are the semantics of dealing with a keyring that has a link to an
     unlinkable key within it?  Does the keyring become automatically
     unlinkable too?

 (5) How do we deal with an unlinkable session keyring?  Should this prevent
     someone joining a new session?  And should this affect all their heirs
     also?

I feel that unlinkability really should apply to the link to a key, not to the
key itself if we have it at all.

Why do you want unlinkable keys anyway?

> -#define	KEY_SETATTR	0x20	/* require permission to change attributes */
> +#define	KEY_SETATTR	0x40	/* require permission to change attributes */

NAK!  You may not change this as this will invalidate RHEL4's kernel and the
keyutils package.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 18:29 ` Stephen Smalley
@ 2006-06-01 10:13   ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-01 10:13 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: Michael LeMay, jmorris, selinux, dhowells

Stephen Smalley <sds@tycho.nsa.gov> wrote:

> No change to the existing key permission bits, and plenty of room for them
> to grow still.

There's not all that much room.  There're only two bits, and one of them is
tentatively reserved for key cloning as requested by Kyle Moffett.

Key cloning involves breaking the key into two bits: a handle with security
and permissions info on it and the payload.  A key could then be "cloned" to
get a new handle upon which new permissions could be enscribed.  There are a
few problems with this: how do you manage quota and how do you deal with
joining session keyrings?

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 10:09 ` David Howells
@ 2006-06-01 11:39   ` Stephen Smalley
  2006-06-01 12:27     ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-06-01 11:39 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Thu, 2006-06-01 at 11:09 +0100, David Howells wrote:
> It isn't as trivial as you think.  Unlink is implied by several operations you
> haven't altered: link, add key, request key, clear keyring and search keyring.
> This is because you can displace a key link from a keyring by adding a link to
> another key of matching type and description.
> 
> > If any of you decide it should be split off into some other patch, I'd be
> > glad to remove it.
> 
> It certainly needs be split off into its own patch as it's a fairly major
> change.  Your changes in this area are also incomplete.
> 
> I also don't really want unlinkable keys for a number of reasons:
> 
>  (1) You can't get rid of them when they're dead (unless you allow a specific
>      bypass).
> 
>  (2) If they're your keys, they eat your quota and but you may not be able to
>      ditch them.
> 
>  (3) If someone accidentally zeroes the mask on the key, they're left with a
>      key that they can't get rid of.
> 
>  (4) What are the semantics of dealing with a keyring that has a link to an
>      unlinkable key within it?  Does the keyring become automatically
>      unlinkable too?
> 
>  (5) How do we deal with an unlinkable session keyring?  Should this prevent
>      someone joining a new session?  And should this affect all their heirs
>      also?
> 
> I feel that unlinkability really should apply to the link to a key, not to the
> key itself if we have it at all.
> 
> Why do you want unlinkable keys anyway?

The concern is that any process that requires write access to the
keyring for any reason would otherwise be able to unlink all keys from
it, irrespective of what may have created the link in the first place.
The individual links in a single keyring could have been created by
processes in different security contexts, as a single user session can
have processes running in multiple security contexts.  Having a per-key
unlink check parallels the existing checking on the link side.  And 
unlinking a key can lead to its destruction (if it is the last link),
right, so it has a potentially modifying impact on the key itself?
  
-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 11:39   ` Stephen Smalley
@ 2006-06-01 12:27     ` David Howells
  2006-06-01 14:21       ` Stephen Smalley
  0 siblings, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-01 12:27 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: David Howells, Michael LeMay, jmorris, selinux


Stephen Smalley <sds@tycho.nsa.gov> wrote:

> > Why do you want unlinkable keys anyway?
> 
> The concern is that any process that requires write access to the
> keyring for any reason would otherwise be able to unlink all keys from
> it, irrespective of what may have created the link in the first place.

Yes.

> The individual links in a single keyring could have been created by
> processes in different security contexts, as a single user session can
> have processes running in multiple security contexts.

Hmmm... I think it's incorrect to have an unlink permission bit on the key as
it causes too many potential problems, and would apply to all the links on a
key, irrespective of the keyrings holding them.

Perhaps what's required is to have unlink permission controlled from the
keyring's perspective.  How about adding another security op that takes the
keyring, the key and the requesting process, and asks if that process may
detach that key from that keyring.  Clearing a keyring would then be a filter
operation.

There are still a couple of problems to deal with:

 (1) Key displacement.  How do you deal with a new link that's being made
     displacing an old link that would otherwise be refused unlink permission?
     It might be sufficient to do the check in advance and assume that things
     won't then change whilst we create a new key, except that there's a race
     between two processes both trying to displace the same link in the same
     keyring.

     The alternative for new key addition is to do the check at the point of
     link creation, and just accept that we'll not create a link if we can't
     displace the old key.

     It would also be possible to store a bit of information with each pointer
     in the keyring if that would be useful.  The problem with caching can
     you/can't you data in the keyring is that it very much depends on who's
     asking.

 (2) Permission percolation over keyrings.  What do you do about a keyring
     that contains a key that you're not permitted to unlink?  Does the
     keyring itself become unlinkable?  What about session keyrings?

> Having a per-key unlink check parallels the existing checking on the link
> side.

I'm not sure they are exactly parallel: having a link check doesn't cause all
the problems that having an unlink check does.

> And unlinking a key can lead to its destruction (if it is the last link),
> right, so it has a potentially modifying impact on the key itself?

Yes, but you have to be able to live with that, otherwise the kernel might
fill up with obsolete keys.  If a filesystem or some other kernel service is
using a key, it should keep its own link to that key.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 12:27     ` David Howells
@ 2006-06-01 14:21       ` Stephen Smalley
  2006-06-01 14:52         ` Valdis.Kletnieks
  0 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-06-01 14:21 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Thu, 2006-06-01 at 13:27 +0100, David Howells wrote:
> Stephen Smalley <sds@tycho.nsa.gov> wrote:
> 
> > > Why do you want unlinkable keys anyway?
> > 
> > The concern is that any process that requires write access to the
> > keyring for any reason would otherwise be able to unlink all keys from
> > it, irrespective of what may have created the link in the first place.
> 
> Yes.
> 
> > The individual links in a single keyring could have been created by
> > processes in different security contexts, as a single user session can
> > have processes running in multiple security contexts.
> 
> Hmmm... I think it's incorrect to have an unlink permission bit on the key as
> it causes too many potential problems, and would apply to all the links on a
> key, irrespective of the keyrings holding them.
> 
> Perhaps what's required is to have unlink permission controlled from the
> keyring's perspective.  How about adding another security op that takes the
> keyring, the key and the requesting process, and asks if that process may
> detach that key from that keyring.  Clearing a keyring would then be a filter
> operation.
> 
> There are still a couple of problems to deal with:
> 
>  (1) Key displacement.  How do you deal with a new link that's being made
>      displacing an old link that would otherwise be refused unlink permission?
>      It might be sufficient to do the check in advance and assume that things
>      won't then change whilst we create a new key, except that there's a race
>      between two processes both trying to displace the same link in the same
>      keyring.
> 
>      The alternative for new key addition is to do the check at the point of
>      link creation, and just accept that we'll not create a link if we can't
>      displace the old key.
> 
>      It would also be possible to store a bit of information with each pointer
>      in the keyring if that would be useful.  The problem with caching can
>      you/can't you data in the keyring is that it very much depends on who's
>      asking.
> 
>  (2) Permission percolation over keyrings.  What do you do about a keyring
>      that contains a key that you're not permitted to unlink?  Does the
>      keyring itself become unlinkable?  What about session keyrings?
> 
> > Having a per-key unlink check parallels the existing checking on the link
> > side.
> 
> I'm not sure they are exactly parallel: having a link check doesn't cause all
> the problems that having an unlink check does.
> 
> > And unlinking a key can lead to its destruction (if it is the last link),
> > right, so it has a potentially modifying impact on the key itself?
> 
> Yes, but you have to be able to live with that, otherwise the kernel might
> fill up with obsolete keys.  If a filesystem or some other kernel service is
> using a key, it should keep its own link to that key.

Ok, given these considerations, I think we should just drop the unlink
check for now, and proceed with the rest of the patch (modulo
CodingStyle issues).

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01  9:32   ` David Howells
@ 2006-06-01 14:26     ` James Morris
  2006-06-01 14:36       ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: James Morris @ 2006-06-01 14:26 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux, Stephen Smalley

On Thu, 1 Jun 2006, David Howells wrote:

> James Morris <jmorris@namei.org> wrote:
> 
> > What's the difference between reading and viewing a key?
> 
> "Viewing" is looking at the key's attributes.

Why not be consistent with the rest of Linux and use something like 
"getattr" ?




-- 
James Morris
<jmorris@namei.org>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 14:26     ` James Morris
@ 2006-06-01 14:36       ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-01 14:36 UTC (permalink / raw)
  To: James Morris
  Cc: David Howells, Michael LeMay, jmorris, selinux, Stephen Smalley

James Morris <jmorris@namei.org> wrote:

> > > What's the difference between reading and viewing a key?
> > 
> > "Viewing" is looking at the key's attributes.
> 
> Why not be consistent with the rest of Linux and use something like 
> "getattr" ?

That would be reasonable.  The only problem I have with that is that the
"view" name is part of the userspace ABI and documentation, and besides
'getattr' didn't really exist when I first did my key stuff.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-05-31 17:50 Michael LeMay
                   ` (2 preceding siblings ...)
  2006-06-01 10:09 ` David Howells
@ 2006-06-01 14:40 ` David Howells
  3 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-01 14:40 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells

Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> I've tested this patch using all four combinations of SELinux
> enabled/disabled and keys enabled/disabled, so I'm fairly
> confident that it won't cause any breakage.

Whilst that may be true, you haven't updated the documentation, and so the
patch is incomplete on those grounds too.  See:

	Documentation/keys.txt

You would certainly need to document the Unlink permission, should that be
added in your patch.  You should probably add a section detailing how SE Linux
governs keys management also.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 14:21       ` Stephen Smalley
@ 2006-06-01 14:52         ` Valdis.Kletnieks
       [not found]           ` <447F0093.3090601@lemaymd.com>
  0 siblings, 1 reply; 41+ messages in thread
From: Valdis.Kletnieks @ 2006-06-01 14:52 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: David Howells, Michael LeMay, jmorris, selinux

[-- Attachment #1: Type: text/plain, Size: 282 bytes --]

On Thu, 01 Jun 2006 10:21:35 EDT, Stephen Smalley said:

> Ok, given these considerations, I think we should just drop the unlink
> check for now, and proceed with the rest of the patch (modulo
> CodingStyle issues).

Should we put a note in Documentation/keys.txt to that effect?


[-- Attachment #2: Type: application/pgp-signature, Size: 226 bytes --]

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
       [not found]           ` <447F0093.3090601@lemaymd.com>
@ 2006-06-01 15:17             ` Stephen Smalley
  2006-06-01 15:25               ` Stephen Smalley
  0 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-06-01 15:17 UTC (permalink / raw)
  To: Michael LeMay
  Cc: Valdis.Kletnieks, David Howells, Michael LeMay, jmorris, selinux

On Thu, 2006-06-01 at 10:58 -0400, Michael LeMay wrote:
> Valdis.Kletnieks@vt.edu wrote:
> > On Thu, 01 Jun 2006 10:21:35 EDT, Stephen Smalley said:
> >
> >   
> >> Ok, given these considerations, I think we should just drop the unlink
> >> check for now, and proceed with the rest of the patch (modulo
> >> CodingStyle issues).
> >>     
> >
> > Should we put a note in Documentation/keys.txt to that effect?
> >
> >   
> I wonder if it would be best to leave that out of the documentation for 
> now, since it isn't mentioned anywhere else and hasn't been discussed 
> until now...

Yes, I don't think we need to document it unless/until we implement such
a check.  We can add a small section to keys.txt that explains that
SELinux labels keys based on the allocating task security context (and
then amend that to note /proc/self/attr/keycreate when it is supported)
and applies a parallel permission check for each normal key permission
check between the acting task security context and the key security
context.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 15:17             ` Stephen Smalley
@ 2006-06-01 15:25               ` Stephen Smalley
  0 siblings, 0 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-06-01 15:25 UTC (permalink / raw)
  To: Michael LeMay
  Cc: Valdis.Kletnieks, David Howells, Michael LeMay, jmorris, selinux

On Thu, 2006-06-01 at 11:17 -0400, Stephen Smalley wrote:
> On Thu, 2006-06-01 at 10:58 -0400, Michael LeMay wrote:
> > Valdis.Kletnieks@vt.edu wrote:
> > > On Thu, 01 Jun 2006 10:21:35 EDT, Stephen Smalley said:
> > >
> > >   
> > >> Ok, given these considerations, I think we should just drop the unlink
> > >> check for now, and proceed with the rest of the patch (modulo
> > >> CodingStyle issues).
> > >>     
> > >
> > > Should we put a note in Documentation/keys.txt to that effect?
> > >
> > >   
> > I wonder if it would be best to leave that out of the documentation for 
> > now, since it isn't mentioned anywhere else and hasn't been discussed 
> > until now...
> 
> Yes, I don't think we need to document it unless/until we implement such
> a check.  We can add a small section to keys.txt that explains that
> SELinux labels keys based on the allocating task security context (and
> then amend that to note /proc/self/attr/keycreate when it is supported)
> and applies a parallel permission check for each normal key permission
> check between the acting task security context and the key security
> context.

BTW, you also need to submit an updated policy patch once you've
finalized the kernel permission checks so that the permission
definitions will be included in the refpolicy.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-06-01 18:26 Michael LeMay
  2006-06-01 23:40 ` James Morris
  2006-06-02  9:21 ` David Howells
  0 siblings, 2 replies; 41+ messages in thread
From: Michael LeMay @ 2006-06-01 18:26 UTC (permalink / raw)
  To: jmorris, selinux, sds, dhowells

Maybe this will be the one...

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

---

 Documentation/keys.txt                       |   23 +++++++++
 include/linux/key.h                          |   18 +++++--
 include/linux/security.h                     |   10 ++--
 kernel/user.c                                |    2
 security/dummy.c                             |    2
 security/keys/key.c                          |    8 +--
 security/keys/keyring.c                      |    5 +-
 security/keys/process_keys.c                 |   15 +++---
 security/keys/request_key.c                  |    6 +-
 security/keys/request_key_auth.c             |    2
 security/selinux/hooks.c                     |   64 +++++++++++++++++++++++++++
 security/selinux/include/av_perm_to_string.h |    6 ++
 security/selinux/include/av_permissions.h    |    8 +++
 security/selinux/include/class_to_string.h   |    1
 security/selinux/include/flask.h             |    1
 security/selinux/include/objsec.h            |    5 ++
 16 files changed, 149 insertions(+), 27 deletions(-)

diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt linux-2.6.17-rc5-mm1/Documentation/keys.txt
--- linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt	2006-05-31 09:47:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/Documentation/keys.txt	2006-06-01 11:24:53.000000000 -0400
@@ -19,6 +19,7 @@ This document has the following sections
 	- Key overview
 	- Key service overview
 	- Key access permissions
+	- SELinux support
 	- New procfs files
 	- Userspace system call interface
 	- Kernel services
@@ -232,6 +233,28 @@ For changing the ownership, group ID or 
 the key or having the sysadmin capability is sufficient.
 
 
+===============
+SELINUX SUPPORT
+===============
+
+The security class "key" has been added to SELinux so that mandatory access
+controls can be applied to keys created within various contexts.  This support
+is preliminary, and is likely to change quite significantly in the near
+future.  Currently, all of the basic permissions explained above are provided
+in SELinux as well, and are simply checked after all discretionary permission
+checks have been performed.
+
+Each key is labeled with the same context as the task to which it belongs.
+Typically, this is the same task that was running when the key was created.
+The default keyrings are handled differently, but in a way that is very intuitive.
+The user and user session keyrings that are created when the user logs in are
+currently labeled with the context of the login manager, but will soon be labeled
+with the user's initial context.  The keyrings associated with new threads are
+each labeled with the context of their associated thread, and both session and
+process keyrings are handled similarly.  However, the default keyrings associated
+with the root user are labeled with the default kernel context, since they are
+created early in the boot process, before root has even logged in.
+
 ================
 NEW PROCFS FILES
 ================
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/key.h linux-2.6.17-rc5-mm1/include/linux/key.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/key.h	2006-05-31 09:48:38.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/key.h	2006-05-31 10:03:00.000000000 -0400
@@ -246,8 +246,9 @@ extern void unregister_key_type(struct k
 
 extern struct key *key_alloc(struct key_type *type,
 			     const char *desc,
-			     uid_t uid, gid_t gid, key_perm_t perm,
-			     int not_in_quota);
+			     uid_t uid, gid_t gid,
+			     struct task_struct *ctx,
+			     key_perm_t perm, int not_in_quota);
 extern int key_payload_reserve(struct key *key, size_t datalen);
 extern int key_instantiate_and_link(struct key *key,
 				    const void *data,
@@ -297,7 +298,9 @@ extern int key_unlink(struct key *keyrin
 		      struct key *key);
 
 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-				 int not_in_quota, struct key *dest);
+				 struct task_struct *ctx,
+				 int not_in_quota,
+				 struct key *dest);
 
 extern int keyring_clear(struct key *keyring);
 
@@ -318,7 +321,8 @@ extern void keyring_replace_payload(stru
  * the userspace interface
  */
 extern struct key root_user_keyring, root_session_keyring;
-extern int alloc_uid_keyring(struct user_struct *user);
+extern int alloc_uid_keyring(struct user_struct *user,
+			     struct task_struct *ctx);
 extern void switch_uid_keyring(struct user_struct *new_user);
 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
 extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -347,7 +351,7 @@ extern void key_init(void);
 #define make_key_ref(k)			({ NULL; })
 #define key_ref_to_ptr(k)		({ NULL; })
 #define is_key_possessed(k)		0
-#define alloc_uid_keyring(u)		0
+#define alloc_uid_keyring(u,c)		0
 #define switch_uid_keyring(u)		do { } while(0)
 #define __install_session_keyring(t, k)	({ NULL; })
 #define copy_keys(f,t)			0
@@ -360,6 +364,10 @@ extern void key_init(void);
 #define key_fsgid_changed(t)		do { } while(0)
 #define key_init()			do { } while(0)
 
+/* Initial keyrings */
+extern struct key root_user_keyring;
+extern struct key root_session_keyring;
+
 #endif /* CONFIG_KEYS */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_KEY_H */
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/security.h linux-2.6.17-rc5-mm1/include/linux/security.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/security.h	2006-05-31 09:48:39.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/security.h	2006-05-31 10:03:00.000000000 -0400
@@ -1305,7 +1305,7 @@ struct security_operations {
 
 	/* key management security hooks */
 #ifdef CONFIG_KEYS
-	int (*key_alloc)(struct key *key);
+	int (*key_alloc)(struct key *key, struct task_struct *tsk);
 	void (*key_free)(struct key *key);
 	int (*key_permission)(key_ref_t key_ref,
 			      struct task_struct *context,
@@ -2980,9 +2980,10 @@ static inline int security_xfrm_policy_l
 
 #ifdef CONFIG_KEYS
 #ifdef CONFIG_SECURITY
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
-	return security_ops->key_alloc(key);
+	return security_ops->key_alloc(key, tsk);
 }
 
 static inline void security_key_free(struct key *key)
@@ -2999,7 +3000,8 @@ static inline int security_key_permissio
 
 #else
 
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/kernel/user.c linux-2.6.17-rc5-mm1/kernel/user.c
--- linux-2.6.17-rc5-mm1.orig/kernel/user.c	2006-05-31 09:48:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/kernel/user.c	2006-05-31 10:03:00.000000000 -0400
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid
 		new->mq_bytes = 0;
 		new->locked_shm = 0;
 
-		if (alloc_uid_keyring(new) < 0) {
+		if (alloc_uid_keyring(new, current) < 0) {
 			kmem_cache_free(uid_cachep, new);
 			return NULL;
 		}
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/dummy.c linux-2.6.17-rc5-mm1/security/dummy.c
--- linux-2.6.17-rc5-mm1.orig/security/dummy.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/dummy.c	2006-05-31 10:03:00.000000000 -0400
@@ -850,7 +850,7 @@ static int dummy_setprocattr(struct task
 }
 
 #ifdef CONFIG_KEYS
-static inline int dummy_key_alloc(struct key *key)
+static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/key.c linux-2.6.17-rc5-mm1/security/keys/key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/key.c	2006-05-31 10:03:00.000000000 -0400
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(stru
  *   instantiate the key or discard it before returning
  */
 struct key *key_alloc(struct key_type *type, const char *desc,
-		      uid_t uid, gid_t gid, key_perm_t perm,
-		      int not_in_quota)
+		      uid_t uid, gid_t gid, struct task_struct *ctx,
+		      key_perm_t perm, int not_in_quota)
 {
 	struct key_user *user = NULL;
 	struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *t
 #endif
 
 	/* let the security module know about the key */
-	ret = security_key_alloc(key);
+	ret = security_key_alloc(key, ctx);
 	if (ret < 0)
 		goto security_error;
 
@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t
 
 	/* allocate a new key */
 	key = key_alloc(ktype, description, current->fsuid, current->fsgid,
-			perm, not_in_quota);
+			current, perm, not_in_quota);
 	if (IS_ERR(key)) {
 		key_ref = ERR_PTR(PTR_ERR(key));
 		goto error_3;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c linux-2.6.17-rc5-mm1/security/keys/keyring.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/keyring.c	2006-05-31 10:03:00.000000000 -0400
@@ -240,13 +240,14 @@ static long keyring_read(const struct ke
  * allocate a keyring and link into the destination keyring
  */
 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-			  int not_in_quota, struct key *dest)
+			  struct task_struct *ctx, int not_in_quota,
+			  struct key *dest)
 {
 	struct key *keyring;
 	int ret;
 
 	keyring = key_alloc(&key_type_keyring, description,
-			    uid, gid,
+			    uid, gid, ctx,
 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
 			    not_in_quota);
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c linux-2.6.17-rc5-mm1/security/keys/process_keys.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/process_keys.c	2006-05-31 10:03:00.000000000 -0400
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
 /*
  * allocate the keyrings to be associated with a UID
  */
-int alloc_uid_keyring(struct user_struct *user)
+int alloc_uid_keyring(struct user_struct *user,
+		      struct task_struct *ctx)
 {
 	struct key *uid_keyring, *session_keyring;
 	char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct
 	/* concoct a default session keyring */
 	sprintf(buf, "_uid_ses.%u", user->uid);
 
-	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
+	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
 	if (IS_ERR(session_keyring)) {
 		ret = PTR_ERR(session_keyring);
 		goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct
 	 * keyring */
 	sprintf(buf, "_uid.%u", user->uid);
 
-	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
+	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
 				    session_keyring);
 	if (IS_ERR(uid_keyring)) {
 		key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_s
 
 	sprintf(buf, "_tid.%u", tsk->pid);
 
-	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_
 	if (!tsk->signal->process_keyring) {
 		sprintf(buf, "_pid.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struc
 	if (!keyring) {
 		sprintf(buf, "_ses.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring))
 			return PTR_ERR(keyring);
 	}
@@ -727,7 +728,7 @@ long join_session_keyring(const char *na
 	keyring = find_keyring_by_name(name, 0);
 	if (PTR_ERR(keyring) == -ENOKEY) {
 		/* not found - try and create a new one */
-		keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
+		keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error2;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c	2006-05-31 10:03:00.000000000 -0400
@@ -185,7 +185,7 @@ struct key *request_key_auth_new(struct 
 	sprintf(desc, "%x", target->serial);
 
 	authkey = key_alloc(&key_type_request_key_auth, desc,
-			    current->fsuid, current->fsgid,
+			    current->fsuid, current->fsgid, current,
 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
 			    KEY_USR_VIEW, 1);
 	if (IS_ERR(authkey)) {
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c linux-2.6.17-rc5-mm1/security/keys/request_key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key.c	2006-05-31 10:03:00.000000000 -0400
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct 
 	/* allocate a new session keyring */
 	sprintf(desc, "_req.%u", key->serial);
 
-	keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
+	keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
+				current, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_constru
 
 	/* create a key and add it to the queue */
 	key = key_alloc(type, description,
-			current->fsuid, current->fsgid, KEY_POS_ALL, 0);
+			current->fsuid, current->fsgid,
+			current, KEY_POS_ALL, 0);
 	if (IS_ERR(key))
 		goto alloc_failed;
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c linux-2.6.17-rc5-mm1/security/selinux/hooks.c
--- linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/hooks.c	2006-06-01 11:32:19.000000000 -0400
@@ -4252,6 +4252,57 @@ static int selinux_setprocattr(struct ta
 	return size;
 }
 
+#ifdef CONFIG_KEYS
+
+static int selinux_key_alloc(struct key *k, struct task_struct *tsk)
+{
+	struct task_security_struct *tsec = tsk->security;
+	struct key_security_struct *ksec;
+
+	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
+	if (!ksec)
+		return -ENOMEM;
+
+	ksec->obj = k;
+	ksec->sid = tsec->sid;
+	k->security = ksec;
+
+	return 0;
+}
+
+static void selinux_key_free(struct key *k)
+{
+	struct key_security_struct *ksec = k->security;
+
+	k->security = NULL;
+	kfree(ksec);
+}
+
+static int selinux_key_permission(key_ref_t key_ref,
+			    struct task_struct *ctx,
+			    key_perm_t perm)
+{
+	struct key *key;
+	struct task_security_struct *tsec;
+	struct key_security_struct *ksec;
+
+	key = key_ref_to_ptr(key_ref);
+
+	tsec = ctx->security;
+	ksec = key->security;
+
+	/* if no specific permissions are requested, we skip the
+	   permission check. No serious, additional covert channels
+	   appear to be created. */
+	if (perm == 0)
+		return 0;
+
+	return avc_has_perm(tsec->sid, ksec->sid,
+			    SECCLASS_KEY, perm, NULL);
+}
+
+#endif
+
 static struct security_operations selinux_ops = {
 	.ptrace =			selinux_ptrace,
 	.capget =			selinux_capget,
@@ -4404,6 +4455,12 @@ static struct security_operations selinu
 	.xfrm_state_free_security =	selinux_xfrm_state_free,
 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
 #endif
+
+#ifdef CONFIG_KEYS
+	.key_alloc =                    selinux_key_alloc,
+	.key_free =                     selinux_key_free,
+	.key_permission =               selinux_key_permission,
+#endif
 };
 
 static __init int selinux_init(void)
@@ -4439,6 +4496,13 @@ static __init int selinux_init(void)
 	} else {
 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
 	}
+
+#ifdef CONFIG_KEYS
+	/* Add security information to initial keyrings */
+	security_key_alloc(&root_user_keyring, current);
+	security_key_alloc(&root_session_keyring, current);
+#endif
+
 	return 0;
 }
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h	2006-06-01 10:42:18.000000000 -0400
@@ -959,3 +959,11 @@
 #define PACKET__SEND                              0x00000001UL
 #define PACKET__RECV                              0x00000002UL
 #define PACKET__RELABELTO                         0x00000004UL
+
+#define KEY__VIEW                                 0x00000001UL
+#define KEY__READ                                 0x00000002UL
+#define KEY__WRITE                                0x00000004UL
+#define KEY__SEARCH                               0x00000008UL
+#define KEY__LINK                                 0x00000010UL
+#define KEY__SETATTR                              0x00000020UL
+
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -242,3 +242,9 @@
    S_(SECCLASS_PACKET, PACKET__SEND, "send")
    S_(SECCLASS_PACKET, PACKET__RECV, "recv")
    S_(SECCLASS_PACKET, PACKET__RELABELTO, "relabelto")
+   S_(SECCLASS_KEY, KEY__VIEW, "view")
+   S_(SECCLASS_KEY, KEY__READ, "read")
+   S_(SECCLASS_KEY, KEY__WRITE, "write")
+   S_(SECCLASS_KEY, KEY__SEARCH, "search")
+   S_(SECCLASS_KEY, KEY__LINK, "link")
+   S_(SECCLASS_KEY, KEY__SETATTR, "setattr")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -60,3 +60,4 @@
     S_("netlink_kobject_uevent_socket")
     S_("appletalk_socket")
     S_("packet")
+    S_("key")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h linux-2.6.17-rc5-mm1/security/selinux/include/flask.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/flask.h	2006-06-01 10:42:18.000000000 -0400
@@ -62,6 +62,7 @@
 #define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET           55
 #define SECCLASS_APPLETALK_SOCKET                        56
 #define SECCLASS_PACKET                                  57
+#define SECCLASS_KEY                                     58
 
 /*
  * Security identifier indices for initial entities
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h	2006-05-31 10:03:00.000000000 -0400
@@ -99,6 +99,11 @@ struct sk_security_struct {
 	u32 peer_sid;			/* SID of peer */
 };
 
+struct key_security_struct {
+	struct key *obj; /* back pointer */
+	u32 sid;         /* SID of key */
+};
+
 extern unsigned int selinux_checkreqprot;
 
 #endif /* _SELINUX_OBJSEC_H_ */



--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 18:26 Michael LeMay
@ 2006-06-01 23:40 ` James Morris
  2006-06-02  9:21 ` David Howells
  1 sibling, 0 replies; 41+ messages in thread
From: James Morris @ 2006-06-01 23:40 UTC (permalink / raw)
  To: Michael LeMay; +Cc: selinux, Stephen Smalley, dhowells

On Thu, 1 Jun 2006, Michael LeMay wrote:

> Maybe this will be the one...

Looks ok to me.  I'll forward it upstream once David Howells acks it.

-- 
James Morris
<jmorris@namei.org>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-01 18:26 Michael LeMay
  2006-06-01 23:40 ` James Morris
@ 2006-06-02  9:21 ` David Howells
  2006-06-02 12:44   ` Michael LeMay
  1 sibling, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-02  9:21 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells


Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> Introduce SELinux hooks to support the access key retention subsystem
> within the kernel.  Incorporate new flask headers from a modified
> version of the SELinux reference policy, with support for the new
> security class representing retained keys.  Extend the "key_alloc"
> security hook with a task parameter representing the intended
> ownership context for the key being allocated.  Attach security
> information to root's default keyrings within the SELinux
> initialization routine.

The code looks okay, but I've a couple of comments on the documentation:

> +future.  Currently, all of the basic permissions explained above are provided
> +in SELinux as well, and are simply checked after all discretionary permission
> +checks have been performed.

Aren't the standard checks done first, then the security module checks?

> +The user and user session keyrings that are created when the user logs in are
> +currently labeled with the context of the login manager, but will soon be labeled
> +with the user's initial context.

What do you mean by "soon"?  Do you mean that we don't do this at the moment,
but will do some time in the future, or do you mean that the keyrings start
off labelled one way, but get relabelled later on in the login process?

I think I'd also split the second paragraph a bit.

Can you also wrap the text at 80 characters please?  I won't mention that
"labeled" should be "labelled":-)

How about this:

---
The security class "key" has been added to SELinux so that mandatory access
controls can be applied to keys created within various contexts.  This support
is preliminary, and is likely to change quite significantly in the near future.
Currently, all of the basic permissions explained above are provided in SELinux
as well, and are always checked before any discretionary permission checks are
performed.

Each key is labeled with the same context as the task to which it belongs.
Typically, this is the same task that was running when the key was created.
The default keyrings are handled differently, but in a way that is very
intuitive:

 (*) The user and user session keyrings that are created when the user logs in
     are currently labeled with the context of the login manager, but will soon
     be labeled with the user's initial context.

 (*) The keyrings associated with new threads are each labeled with the context
     of their associated thread, and both session and process keyrings are
     handled similarly.

Note, however, that the default keyrings associated with the root user are
labeled with the default kernel context, since they are created early in the
boot process, before root has a chance to log in.
---

Remember, if you're using emacs, do: "ESC 7 9 C-x f" to set the fill column,
then "M-q" on each paragraph to fill it appropriately.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02  9:21 ` David Howells
@ 2006-06-02 12:44   ` Michael LeMay
  2006-06-02 13:02     ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: Michael LeMay @ 2006-06-02 12:44 UTC (permalink / raw)
  To: David Howells; +Cc: jmorris, selinux, sds

On Fri, 2006-06-02 at 10:21 +0100, David Howells wrote:
> Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:
> 

> What do you mean by "soon"?  Do you mean that we don't do this at the moment,
> but will do some time in the future, or do you mean that the keyrings start
> off labelled one way, but get relabelled later on in the login process?
> 

I guess that is quite ambiguous, and there's not really any reason to
discuss our future patches in the documentation anyway.

> I think I'd also split the second paragraph a bit.
> 
> Can you also wrap the text at 80 characters please?  I won't mention that
> "labeled" should be "labelled":-)

I'm a proud American; I don't use British spelling. ;-)  Are you
serious, though?  Is there some convention about American vs. British
spelling in our section of the kernel?  I see both in the documentation.

> 
> How about this:
> 
> ---
> The security class "key" has been added to SELinux so that mandatory access
> controls can be applied to keys created within various contexts.  This support
> is preliminary, and is likely to change quite significantly in the near future.
> Currently, all of the basic permissions explained above are provided in SELinux
> as well, and are always checked before any discretionary permission checks are
> performed.

I think you meant to reverse the last sentence, but correct me if I'm
wrong...

> 
> --
> This message was distributed to subscribers of the selinux mailing list.
> If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
> the words "unsubscribe selinux" without quotes as the message.


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 12:44   ` Michael LeMay
@ 2006-06-02 13:02     ` David Howells
  2006-06-02 13:08       ` Stephen Smalley
  0 siblings, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-02 13:02 UTC (permalink / raw)
  To: Michael LeMay; +Cc: David Howells, jmorris, selinux, sds

Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> > I think I'd also split the second paragraph a bit.
> > 
> > Can you also wrap the text at 80 characters please?  I won't mention that
> > "labeled" should be "labelled":-)
> 
> I'm a proud American; I don't use British spelling. ;-)  Are you
> serious, though?  Is there some convention about American vs. British
> spelling in our section of the kernel?  I see both in the documentation.

I'm thinking people may see it in the document I wrote and think I'm a US-ian.
Brrr:-)

There isn't any convention on this that I know of.

> > Currently, all of the basic permissions explained above are provided in
> > SELinux as well, and are always checked before any discretionary
> > permission checks are performed.
> 
> I think you meant to reverse the last sentence, but correct me if I'm
> wrong...

Yes.  Assuming I understand what you're saying, and that "discretionary
permissions checks" are what SE Linux does, then yes, key_task_permission()
does those last.  If not, then it needs rewording rather than reversing to
make it clearer.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:02     ` David Howells
@ 2006-06-02 13:08       ` Stephen Smalley
  2006-06-02 13:25         ` David Howells
                           ` (2 more replies)
  0 siblings, 3 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-06-02 13:08 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Fri, 2006-06-02 at 14:02 +0100, David Howells wrote:
> Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:
> 
> > > I think I'd also split the second paragraph a bit.
> > > 
> > > Can you also wrap the text at 80 characters please?  I won't mention that
> > > "labeled" should be "labelled":-)
> > 
> > I'm a proud American; I don't use British spelling. ;-)  Are you
> > serious, though?  Is there some convention about American vs. British
> > spelling in our section of the kernel?  I see both in the documentation.
> 
> I'm thinking people may see it in the document I wrote and think I'm a US-ian.
> Brrr:-)
> 
> There isn't any convention on this that I know of.
> 
> > > Currently, all of the basic permissions explained above are provided in
> > > SELinux as well, and are always checked before any discretionary
> > > permission checks are performed.
> > 
> > I think you meant to reverse the last sentence, but correct me if I'm
> > wrong...
> 
> Yes.  Assuming I understand what you're saying, and that "discretionary
> permissions checks" are what SE Linux does, then yes, key_task_permission()
> does those last.  If not, then it needs rewording rather than reversing to
> make it clearer.

Discretionary access control is what the basic Linux access control
model is.  Mandatory access control is what SELinux provides.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:08       ` Stephen Smalley
@ 2006-06-02 13:25         ` David Howells
  2006-06-02 13:28         ` Stephen Smalley
  2006-06-02 15:54         ` Casey Schaufler
  2 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-02 13:25 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: David Howells, Michael LeMay, jmorris, selinux


Stephen Smalley <sds@tycho.nsa.gov> wrote:

> > > > Currently, all of the basic permissions explained above are provided in
> > > > SELinux as well, and are always checked before any discretionary
> > > > permission checks are performed.
> > > 
> > > I think you meant to reverse the last sentence, but correct me if I'm
> > > wrong...
> > 
> > Yes.  Assuming I understand what you're saying, and that "discretionary
> > permissions checks" are what SE Linux does, then yes, key_task_permission()
> > does those last.  If not, then it needs rewording rather than reversing to
> > make it clearer.
> 
> Discretionary access control is what the basic Linux access control
> model is.  Mandatory access control is what SELinux provides.

Ah.  In that case a bit of rewording is required rather than reversal:

How about this:

---
The security class "key" has been added to SELinux so that mandatory access
controls can be applied to keys created within various contexts.  This support
is preliminary, and is likely to change quite significantly in the near future.
Currently, all of the basic permissions explained above are provided in SELinux
as well; SE Linux is simply invoked after all basic permission checks have been
performed.

Each key is labeled with the same context as the task to which it belongs.
Typically, this is the same task that was running when the key was created.
The default keyrings are handled differently, but in a way that is very
intuitive:

 (*) The user and user session keyrings that are created when the user logs in
     are currently labeled with the context of the login manager, but will soon
     be labeled with the user's initial context.

 (*) The keyrings associated with new threads are each labeled with the context
     of their associated thread, and both session and process keyrings are
     handled similarly.

Note, however, that the default keyrings associated with the root user are
labeled with the default kernel context, since they are created early in the
boot process, before root has a chance to log in.
---

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-06-02 13:27 Michael LeMay
  2006-06-02 13:38 ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: Michael LeMay @ 2006-06-02 13:27 UTC (permalink / raw)
  To: jmorris, selinux, sds, dhowells

Let me know if you see anything else that needs to be changed in this version...

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

---

 Documentation/keys.txt                       |   29 ++++++++++++
 include/linux/key.h                          |   18 +++++--
 include/linux/security.h                     |   10 ++--
 kernel/user.c                                |    2
 security/dummy.c                             |    2
 security/keys/key.c                          |    8 +--
 security/keys/keyring.c                      |    5 +-
 security/keys/process_keys.c                 |   15 +++---
 security/keys/request_key.c                  |    6 +-
 security/keys/request_key_auth.c             |    2
 security/selinux/hooks.c                     |   64 +++++++++++++++++++++++++++
 security/selinux/include/av_perm_to_string.h |    6 ++
 security/selinux/include/av_permissions.h    |    8 +++
 security/selinux/include/class_to_string.h   |    1
 security/selinux/include/flask.h             |    1
 security/selinux/include/objsec.h            |    5 ++
 16 files changed, 155 insertions(+), 27 deletions(-)

diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt linux-2.6.17-rc5-mm1/Documentation/keys.txt
--- linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt	2006-05-31 09:47:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/Documentation/keys.txt	2006-06-02 09:13:48.000000000 -0400
@@ -19,6 +19,7 @@ This document has the following sections
 	- Key overview
 	- Key service overview
 	- Key access permissions
+	- SELinux support
 	- New procfs files
 	- Userspace system call interface
 	- Kernel services
@@ -232,6 +233,34 @@ For changing the ownership, group ID or 
 the key or having the sysadmin capability is sufficient.
 
 
+===============
+SELINUX SUPPORT
+===============
+
+The security class "key" has been added to SELinux so that mandatory access
+controls can be applied to keys created within various contexts.  This support
+is preliminary, and is likely to change quite significantly in the near future.
+Currently, all of the basic permissions explained above are provided in SELinux
+as well, and are always checked immediately after any discretionary permission
+checks are performed.
+
+Each key is labeled with the same context as the task to which it belongs.
+Typically, this is the same task that was running when the key was created.
+The default keyrings are handled differently, but in a way that is very
+intuitive:
+
+ (*) The user and user session keyrings that are created when the user logs in
+     are currently labeled with the context of the login manager.
+
+ (*) The keyrings associated with new threads are each labeled with the context
+     of their associated thread, and both session and process keyrings are
+     handled similarly.
+
+Note, however, that the default keyrings associated with the root user are
+labeled with the default kernel context, since they are created early in the
+boot process, before root has a chance to log in.
+
+
 ================
 NEW PROCFS FILES
 ================
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/key.h linux-2.6.17-rc5-mm1/include/linux/key.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/key.h	2006-05-31 09:48:38.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/key.h	2006-05-31 10:03:00.000000000 -0400
@@ -246,8 +246,9 @@ extern void unregister_key_type(struct k
 
 extern struct key *key_alloc(struct key_type *type,
 			     const char *desc,
-			     uid_t uid, gid_t gid, key_perm_t perm,
-			     int not_in_quota);
+			     uid_t uid, gid_t gid,
+			     struct task_struct *ctx,
+			     key_perm_t perm, int not_in_quota);
 extern int key_payload_reserve(struct key *key, size_t datalen);
 extern int key_instantiate_and_link(struct key *key,
 				    const void *data,
@@ -297,7 +298,9 @@ extern int key_unlink(struct key *keyrin
 		      struct key *key);
 
 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-				 int not_in_quota, struct key *dest);
+				 struct task_struct *ctx,
+				 int not_in_quota,
+				 struct key *dest);
 
 extern int keyring_clear(struct key *keyring);
 
@@ -318,7 +321,8 @@ extern void keyring_replace_payload(stru
  * the userspace interface
  */
 extern struct key root_user_keyring, root_session_keyring;
-extern int alloc_uid_keyring(struct user_struct *user);
+extern int alloc_uid_keyring(struct user_struct *user,
+			     struct task_struct *ctx);
 extern void switch_uid_keyring(struct user_struct *new_user);
 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
 extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -347,7 +351,7 @@ extern void key_init(void);
 #define make_key_ref(k)			({ NULL; })
 #define key_ref_to_ptr(k)		({ NULL; })
 #define is_key_possessed(k)		0
-#define alloc_uid_keyring(u)		0
+#define alloc_uid_keyring(u,c)		0
 #define switch_uid_keyring(u)		do { } while(0)
 #define __install_session_keyring(t, k)	({ NULL; })
 #define copy_keys(f,t)			0
@@ -360,6 +364,10 @@ extern void key_init(void);
 #define key_fsgid_changed(t)		do { } while(0)
 #define key_init()			do { } while(0)
 
+/* Initial keyrings */
+extern struct key root_user_keyring;
+extern struct key root_session_keyring;
+
 #endif /* CONFIG_KEYS */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_KEY_H */
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/security.h linux-2.6.17-rc5-mm1/include/linux/security.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/security.h	2006-05-31 09:48:39.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/security.h	2006-05-31 10:03:00.000000000 -0400
@@ -1305,7 +1305,7 @@ struct security_operations {
 
 	/* key management security hooks */
 #ifdef CONFIG_KEYS
-	int (*key_alloc)(struct key *key);
+	int (*key_alloc)(struct key *key, struct task_struct *tsk);
 	void (*key_free)(struct key *key);
 	int (*key_permission)(key_ref_t key_ref,
 			      struct task_struct *context,
@@ -2980,9 +2980,10 @@ static inline int security_xfrm_policy_l
 
 #ifdef CONFIG_KEYS
 #ifdef CONFIG_SECURITY
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
-	return security_ops->key_alloc(key);
+	return security_ops->key_alloc(key, tsk);
 }
 
 static inline void security_key_free(struct key *key)
@@ -2999,7 +3000,8 @@ static inline int security_key_permissio
 
 #else
 
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/kernel/user.c linux-2.6.17-rc5-mm1/kernel/user.c
--- linux-2.6.17-rc5-mm1.orig/kernel/user.c	2006-05-31 09:48:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/kernel/user.c	2006-05-31 10:03:00.000000000 -0400
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid
 		new->mq_bytes = 0;
 		new->locked_shm = 0;
 
-		if (alloc_uid_keyring(new) < 0) {
+		if (alloc_uid_keyring(new, current) < 0) {
 			kmem_cache_free(uid_cachep, new);
 			return NULL;
 		}
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/dummy.c linux-2.6.17-rc5-mm1/security/dummy.c
--- linux-2.6.17-rc5-mm1.orig/security/dummy.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/dummy.c	2006-05-31 10:03:00.000000000 -0400
@@ -850,7 +850,7 @@ static int dummy_setprocattr(struct task
 }
 
 #ifdef CONFIG_KEYS
-static inline int dummy_key_alloc(struct key *key)
+static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/key.c linux-2.6.17-rc5-mm1/security/keys/key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/key.c	2006-05-31 10:03:00.000000000 -0400
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(stru
  *   instantiate the key or discard it before returning
  */
 struct key *key_alloc(struct key_type *type, const char *desc,
-		      uid_t uid, gid_t gid, key_perm_t perm,
-		      int not_in_quota)
+		      uid_t uid, gid_t gid, struct task_struct *ctx,
+		      key_perm_t perm, int not_in_quota)
 {
 	struct key_user *user = NULL;
 	struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *t
 #endif
 
 	/* let the security module know about the key */
-	ret = security_key_alloc(key);
+	ret = security_key_alloc(key, ctx);
 	if (ret < 0)
 		goto security_error;
 
@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t
 
 	/* allocate a new key */
 	key = key_alloc(ktype, description, current->fsuid, current->fsgid,
-			perm, not_in_quota);
+			current, perm, not_in_quota);
 	if (IS_ERR(key)) {
 		key_ref = ERR_PTR(PTR_ERR(key));
 		goto error_3;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c linux-2.6.17-rc5-mm1/security/keys/keyring.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/keyring.c	2006-05-31 10:03:00.000000000 -0400
@@ -240,13 +240,14 @@ static long keyring_read(const struct ke
  * allocate a keyring and link into the destination keyring
  */
 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-			  int not_in_quota, struct key *dest)
+			  struct task_struct *ctx, int not_in_quota,
+			  struct key *dest)
 {
 	struct key *keyring;
 	int ret;
 
 	keyring = key_alloc(&key_type_keyring, description,
-			    uid, gid,
+			    uid, gid, ctx,
 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
 			    not_in_quota);
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c linux-2.6.17-rc5-mm1/security/keys/process_keys.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/process_keys.c	2006-05-31 10:03:00.000000000 -0400
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
 /*
  * allocate the keyrings to be associated with a UID
  */
-int alloc_uid_keyring(struct user_struct *user)
+int alloc_uid_keyring(struct user_struct *user,
+		      struct task_struct *ctx)
 {
 	struct key *uid_keyring, *session_keyring;
 	char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct
 	/* concoct a default session keyring */
 	sprintf(buf, "_uid_ses.%u", user->uid);
 
-	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
+	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
 	if (IS_ERR(session_keyring)) {
 		ret = PTR_ERR(session_keyring);
 		goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct
 	 * keyring */
 	sprintf(buf, "_uid.%u", user->uid);
 
-	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
+	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
 				    session_keyring);
 	if (IS_ERR(uid_keyring)) {
 		key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_s
 
 	sprintf(buf, "_tid.%u", tsk->pid);
 
-	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_
 	if (!tsk->signal->process_keyring) {
 		sprintf(buf, "_pid.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struc
 	if (!keyring) {
 		sprintf(buf, "_ses.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring))
 			return PTR_ERR(keyring);
 	}
@@ -727,7 +728,7 @@ long join_session_keyring(const char *na
 	keyring = find_keyring_by_name(name, 0);
 	if (PTR_ERR(keyring) == -ENOKEY) {
 		/* not found - try and create a new one */
-		keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
+		keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error2;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c	2006-05-31 10:03:00.000000000 -0400
@@ -185,7 +185,7 @@ struct key *request_key_auth_new(struct 
 	sprintf(desc, "%x", target->serial);
 
 	authkey = key_alloc(&key_type_request_key_auth, desc,
-			    current->fsuid, current->fsgid,
+			    current->fsuid, current->fsgid, current,
 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
 			    KEY_USR_VIEW, 1);
 	if (IS_ERR(authkey)) {
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c linux-2.6.17-rc5-mm1/security/keys/request_key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key.c	2006-05-31 10:03:00.000000000 -0400
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct 
 	/* allocate a new session keyring */
 	sprintf(desc, "_req.%u", key->serial);
 
-	keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
+	keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
+				current, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_constru
 
 	/* create a key and add it to the queue */
 	key = key_alloc(type, description,
-			current->fsuid, current->fsgid, KEY_POS_ALL, 0);
+			current->fsuid, current->fsgid,
+			current, KEY_POS_ALL, 0);
 	if (IS_ERR(key))
 		goto alloc_failed;
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c linux-2.6.17-rc5-mm1/security/selinux/hooks.c
--- linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/hooks.c	2006-06-01 11:32:19.000000000 -0400
@@ -4252,6 +4252,57 @@ static int selinux_setprocattr(struct ta
 	return size;
 }
 
+#ifdef CONFIG_KEYS
+
+static int selinux_key_alloc(struct key *k, struct task_struct *tsk)
+{
+	struct task_security_struct *tsec = tsk->security;
+	struct key_security_struct *ksec;
+
+	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
+	if (!ksec)
+		return -ENOMEM;
+
+	ksec->obj = k;
+	ksec->sid = tsec->sid;
+	k->security = ksec;
+
+	return 0;
+}
+
+static void selinux_key_free(struct key *k)
+{
+	struct key_security_struct *ksec = k->security;
+
+	k->security = NULL;
+	kfree(ksec);
+}
+
+static int selinux_key_permission(key_ref_t key_ref,
+			    struct task_struct *ctx,
+			    key_perm_t perm)
+{
+	struct key *key;
+	struct task_security_struct *tsec;
+	struct key_security_struct *ksec;
+
+	key = key_ref_to_ptr(key_ref);
+
+	tsec = ctx->security;
+	ksec = key->security;
+
+	/* if no specific permissions are requested, we skip the
+	   permission check. No serious, additional covert channels
+	   appear to be created. */
+	if (perm == 0)
+		return 0;
+
+	return avc_has_perm(tsec->sid, ksec->sid,
+			    SECCLASS_KEY, perm, NULL);
+}
+
+#endif
+
 static struct security_operations selinux_ops = {
 	.ptrace =			selinux_ptrace,
 	.capget =			selinux_capget,
@@ -4404,6 +4455,12 @@ static struct security_operations selinu
 	.xfrm_state_free_security =	selinux_xfrm_state_free,
 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
 #endif
+
+#ifdef CONFIG_KEYS
+	.key_alloc =                    selinux_key_alloc,
+	.key_free =                     selinux_key_free,
+	.key_permission =               selinux_key_permission,
+#endif
 };
 
 static __init int selinux_init(void)
@@ -4439,6 +4496,13 @@ static __init int selinux_init(void)
 	} else {
 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
 	}
+
+#ifdef CONFIG_KEYS
+	/* Add security information to initial keyrings */
+	security_key_alloc(&root_user_keyring, current);
+	security_key_alloc(&root_session_keyring, current);
+#endif
+
 	return 0;
 }
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h	2006-06-01 10:42:18.000000000 -0400
@@ -959,3 +959,11 @@
 #define PACKET__SEND                              0x00000001UL
 #define PACKET__RECV                              0x00000002UL
 #define PACKET__RELABELTO                         0x00000004UL
+
+#define KEY__VIEW                                 0x00000001UL
+#define KEY__READ                                 0x00000002UL
+#define KEY__WRITE                                0x00000004UL
+#define KEY__SEARCH                               0x00000008UL
+#define KEY__LINK                                 0x00000010UL
+#define KEY__SETATTR                              0x00000020UL
+
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -242,3 +242,9 @@
    S_(SECCLASS_PACKET, PACKET__SEND, "send")
    S_(SECCLASS_PACKET, PACKET__RECV, "recv")
    S_(SECCLASS_PACKET, PACKET__RELABELTO, "relabelto")
+   S_(SECCLASS_KEY, KEY__VIEW, "view")
+   S_(SECCLASS_KEY, KEY__READ, "read")
+   S_(SECCLASS_KEY, KEY__WRITE, "write")
+   S_(SECCLASS_KEY, KEY__SEARCH, "search")
+   S_(SECCLASS_KEY, KEY__LINK, "link")
+   S_(SECCLASS_KEY, KEY__SETATTR, "setattr")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -60,3 +60,4 @@
     S_("netlink_kobject_uevent_socket")
     S_("appletalk_socket")
     S_("packet")
+    S_("key")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h linux-2.6.17-rc5-mm1/security/selinux/include/flask.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/flask.h	2006-06-01 10:42:18.000000000 -0400
@@ -62,6 +62,7 @@
 #define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET           55
 #define SECCLASS_APPLETALK_SOCKET                        56
 #define SECCLASS_PACKET                                  57
+#define SECCLASS_KEY                                     58
 
 /*
  * Security identifier indices for initial entities
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h	2006-05-31 10:03:00.000000000 -0400
@@ -99,6 +99,11 @@ struct sk_security_struct {
 	u32 peer_sid;			/* SID of peer */
 };
 
+struct key_security_struct {
+	struct key *obj; /* back pointer */
+	u32 sid;         /* SID of key */
+};
+
 extern unsigned int selinux_checkreqprot;
 
 #endif /* _SELINUX_OBJSEC_H_ */



--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:08       ` Stephen Smalley
  2006-06-02 13:25         ` David Howells
@ 2006-06-02 13:28         ` Stephen Smalley
  2006-06-02 13:30           ` Stephen Smalley
  2006-06-02 13:30           ` David Howells
  2006-06-02 15:54         ` Casey Schaufler
  2 siblings, 2 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-06-02 13:28 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Fri, 2006-06-02 at 09:08 -0400, Stephen Smalley wrote:
> On Fri, 2006-06-02 at 14:02 +0100, David Howells wrote:
> > Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:
> > 
> > > > I think I'd also split the second paragraph a bit.
> > > > 
> > > > Can you also wrap the text at 80 characters please?  I won't mention that
> > > > "labeled" should be "labelled":-)
> > > 
> > > I'm a proud American; I don't use British spelling. ;-)  Are you
> > > serious, though?  Is there some convention about American vs. British
> > > spelling in our section of the kernel?  I see both in the documentation.
> > 
> > I'm thinking people may see it in the document I wrote and think I'm a US-ian.
> > Brrr:-)
> > 
> > There isn't any convention on this that I know of.
> > 
> > > > Currently, all of the basic permissions explained above are provided in
> > > > SELinux as well, and are always checked before any discretionary
> > > > permission checks are performed.
> > > 
> > > I think you meant to reverse the last sentence, but correct me if I'm
> > > wrong...
> > 
> > Yes.  Assuming I understand what you're saying, and that "discretionary
> > permissions checks" are what SE Linux does, then yes, key_task_permission()
> > does those last.  If not, then it needs rewording rather than reversing to
> > make it clearer.
> 
> Discretionary access control is what the basic Linux access control
> model is.  Mandatory access control is what SELinux provides.

So to clarify:  SELinux is presently performing a parallel MAC
permission check for each existing DAC permission check, and the MAC
check occurs after the DAC check (and only occurs if the DAC check
succeeds).  

Also, to clarify re label(l)ing, each key is labeled based on the
creating task's security context, which doesn't necessarily have
anything to do with "belonging" or "possesssion", and the label remains
invariant for the lifetime of the key (non-tranquility).  An extended
interface will be added via subsequent patch to permit keys to be
created with a specified security context (using the existing model
of /proc/self/attr) so that the login managers can create keys based on
the session security context rather than based on their own security
context.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:28         ` Stephen Smalley
@ 2006-06-02 13:30           ` Stephen Smalley
  2006-06-02 13:30           ` David Howells
  1 sibling, 0 replies; 41+ messages in thread
From: Stephen Smalley @ 2006-06-02 13:30 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Fri, 2006-06-02 at 09:28 -0400, Stephen Smalley wrote:
> Also, to clarify re label(l)ing, each key is labeled based on the
> creating task's security context, which doesn't necessarily have
> anything to do with "belonging" or "possesssion", and the label remains
> invariant for the lifetime of the key (non-tranquility).

Oops, I meant "tranquility" above, naturally.  And "possession".

>   An extended
> interface will be added via subsequent patch to permit keys to be
> created with a specified security context (using the existing model
> of /proc/self/attr) so that the login managers can create keys based on
> the session security context rather than based on their own security
> context.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:28         ` Stephen Smalley
  2006-06-02 13:30           ` Stephen Smalley
@ 2006-06-02 13:30           ` David Howells
  2006-06-02 13:52             ` Stephen Smalley
  1 sibling, 1 reply; 41+ messages in thread
From: David Howells @ 2006-06-02 13:30 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: David Howells, Michael LeMay, jmorris, selinux

Stephen Smalley <sds@tycho.nsa.gov> wrote:

> > Discretionary access control is what the basic Linux access control
> > model is.  Mandatory access control is what SELinux provides.
> 
> So to clarify:  SELinux is presently performing a parallel MAC
> permission check for each existing DAC permission check, and the MAC
> check occurs after the DAC check (and only occurs if the DAC check
> succeeds).  

That makes things clearer, thanks; though why is it called a 'DAC' when it is
in fact mandatory?  And why 'MAC' when it's discretionary in the fact that
it's _not_ checked if you don't have a security context?

> An extended interface will be added via subsequent patch to permit keys to
> be created with a specified security context (using the existing model of
> /proc/self/attr) so that the login managers can create keys based on the
> session security context rather than based on their own security context.

You could also add an extra keyctl function for that.  I added one at one
time, but it was decided not to include it at that point.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:27 Michael LeMay
@ 2006-06-02 13:38 ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-02 13:38 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells

Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> Let me know if you see anything else that needs to be changed in this
> version...

I'd like:

> +Currently, all of the basic permissions explained above are provided in SELinux
> +as well, and are always checked immediately after any discretionary permission
> +checks are performed.

changing to:

+Currently, all of the basic permissions explained above are provided in SELinux
+as well; SE Linux is simply invoked after all basic permission checks have been
+performed.


I think that in what you said "and are always checked" refers grammatically to
"all of the basic permissions" - at least, that's the way I understood it.

Documentation takes as much skill, debugging and bloody-mindedness as the code
to which it refers:-).

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-06-02 13:45 Michael LeMay
  2006-06-02 14:10 ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: Michael LeMay @ 2006-06-02 13:45 UTC (permalink / raw)
  To: jmorris, selinux, sds, dhowells

Sorry about that, I didn't get your message before I cut the last patch.

---<snip>---
From: Michael LeMay <mdlemay@epoch.ncsc.mil>

Introduce SELinux hooks to support the access key retention subsystem
within the kernel.  Incorporate new flask headers from a modified
version of the SELinux reference policy, with support for the new
security class representing retained keys.  Extend the "key_alloc"
security hook with a task parameter representing the intended
ownership context for the key being allocated.  Attach security
information to root's default keyrings within the SELinux
initialization routine.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

---

 Documentation/keys.txt                       |   29 ++++++++++++
 include/linux/key.h                          |   18 +++++--
 include/linux/security.h                     |   10 ++--
 kernel/user.c                                |    2
 security/dummy.c                             |    2
 security/keys/key.c                          |    8 +--
 security/keys/keyring.c                      |    5 +-
 security/keys/process_keys.c                 |   15 +++---
 security/keys/request_key.c                  |    6 +-
 security/keys/request_key_auth.c             |    2
 security/selinux/hooks.c                     |   64 +++++++++++++++++++++++++++
 security/selinux/include/av_perm_to_string.h |    6 ++
 security/selinux/include/av_permissions.h    |    8 +++
 security/selinux/include/class_to_string.h   |    1
 security/selinux/include/flask.h             |    1
 security/selinux/include/objsec.h            |    5 ++
 16 files changed, 155 insertions(+), 27 deletions(-)

diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt linux-2.6.17-rc5-mm1/Documentation/keys.txt
--- linux-2.6.17-rc5-mm1.orig/Documentation/keys.txt	2006-05-31 09:47:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/Documentation/keys.txt	2006-06-02 09:42:07.000000000 -0400
@@ -19,6 +19,7 @@ This document has the following sections
 	- Key overview
 	- Key service overview
 	- Key access permissions
+	- SELinux support
 	- New procfs files
 	- Userspace system call interface
 	- Kernel services
@@ -232,6 +233,34 @@ For changing the ownership, group ID or 
 the key or having the sysadmin capability is sufficient.
 
 
+===============
+SELINUX SUPPORT
+===============
+
+The security class "key" has been added to SELinux so that mandatory access
+controls can be applied to keys created within various contexts.  This support
+is preliminary, and is likely to change quite significantly in the near future.
+Currently, all of the basic permissions explained above are provided in SELinux
+as well; SE Linux is simply invoked after all basic permission checks have been
+performed.
+
+Each key is labeled with the same context as the task to which it belongs.
+Typically, this is the same task that was running when the key was created.
+The default keyrings are handled differently, but in a way that is very
+intuitive:
+
+ (*) The user and user session keyrings that are created when the user logs in
+     are currently labeled with the context of the login manager.
+
+ (*) The keyrings associated with new threads are each labeled with the context
+     of their associated thread, and both session and process keyrings are
+     handled similarly.
+
+Note, however, that the default keyrings associated with the root user are
+labeled with the default kernel context, since they are created early in the
+boot process, before root has a chance to log in.
+
+
 ================
 NEW PROCFS FILES
 ================
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/key.h linux-2.6.17-rc5-mm1/include/linux/key.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/key.h	2006-05-31 09:48:38.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/key.h	2006-05-31 10:03:00.000000000 -0400
@@ -246,8 +246,9 @@ extern void unregister_key_type(struct k
 
 extern struct key *key_alloc(struct key_type *type,
 			     const char *desc,
-			     uid_t uid, gid_t gid, key_perm_t perm,
-			     int not_in_quota);
+			     uid_t uid, gid_t gid,
+			     struct task_struct *ctx,
+			     key_perm_t perm, int not_in_quota);
 extern int key_payload_reserve(struct key *key, size_t datalen);
 extern int key_instantiate_and_link(struct key *key,
 				    const void *data,
@@ -297,7 +298,9 @@ extern int key_unlink(struct key *keyrin
 		      struct key *key);
 
 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-				 int not_in_quota, struct key *dest);
+				 struct task_struct *ctx,
+				 int not_in_quota,
+				 struct key *dest);
 
 extern int keyring_clear(struct key *keyring);
 
@@ -318,7 +321,8 @@ extern void keyring_replace_payload(stru
  * the userspace interface
  */
 extern struct key root_user_keyring, root_session_keyring;
-extern int alloc_uid_keyring(struct user_struct *user);
+extern int alloc_uid_keyring(struct user_struct *user,
+			     struct task_struct *ctx);
 extern void switch_uid_keyring(struct user_struct *new_user);
 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
 extern int copy_thread_group_keys(struct task_struct *tsk);
@@ -347,7 +351,7 @@ extern void key_init(void);
 #define make_key_ref(k)			({ NULL; })
 #define key_ref_to_ptr(k)		({ NULL; })
 #define is_key_possessed(k)		0
-#define alloc_uid_keyring(u)		0
+#define alloc_uid_keyring(u,c)		0
 #define switch_uid_keyring(u)		do { } while(0)
 #define __install_session_keyring(t, k)	({ NULL; })
 #define copy_keys(f,t)			0
@@ -360,6 +364,10 @@ extern void key_init(void);
 #define key_fsgid_changed(t)		do { } while(0)
 #define key_init()			do { } while(0)
 
+/* Initial keyrings */
+extern struct key root_user_keyring;
+extern struct key root_session_keyring;
+
 #endif /* CONFIG_KEYS */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_KEY_H */
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/include/linux/security.h linux-2.6.17-rc5-mm1/include/linux/security.h
--- linux-2.6.17-rc5-mm1.orig/include/linux/security.h	2006-05-31 09:48:39.000000000 -0400
+++ linux-2.6.17-rc5-mm1/include/linux/security.h	2006-05-31 10:03:00.000000000 -0400
@@ -1305,7 +1305,7 @@ struct security_operations {
 
 	/* key management security hooks */
 #ifdef CONFIG_KEYS
-	int (*key_alloc)(struct key *key);
+	int (*key_alloc)(struct key *key, struct task_struct *tsk);
 	void (*key_free)(struct key *key);
 	int (*key_permission)(key_ref_t key_ref,
 			      struct task_struct *context,
@@ -2980,9 +2980,10 @@ static inline int security_xfrm_policy_l
 
 #ifdef CONFIG_KEYS
 #ifdef CONFIG_SECURITY
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
-	return security_ops->key_alloc(key);
+	return security_ops->key_alloc(key, tsk);
 }
 
 static inline void security_key_free(struct key *key)
@@ -2999,7 +3000,8 @@ static inline int security_key_permissio
 
 #else
 
-static inline int security_key_alloc(struct key *key)
+static inline int security_key_alloc(struct key *key,
+				     struct task_struct *tsk)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/kernel/user.c linux-2.6.17-rc5-mm1/kernel/user.c
--- linux-2.6.17-rc5-mm1.orig/kernel/user.c	2006-05-31 09:48:43.000000000 -0400
+++ linux-2.6.17-rc5-mm1/kernel/user.c	2006-05-31 10:03:00.000000000 -0400
@@ -148,7 +148,7 @@ struct user_struct * alloc_uid(uid_t uid
 		new->mq_bytes = 0;
 		new->locked_shm = 0;
 
-		if (alloc_uid_keyring(new) < 0) {
+		if (alloc_uid_keyring(new, current) < 0) {
 			kmem_cache_free(uid_cachep, new);
 			return NULL;
 		}
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/dummy.c linux-2.6.17-rc5-mm1/security/dummy.c
--- linux-2.6.17-rc5-mm1.orig/security/dummy.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/dummy.c	2006-05-31 10:03:00.000000000 -0400
@@ -850,7 +850,7 @@ static int dummy_setprocattr(struct task
 }
 
 #ifdef CONFIG_KEYS
-static inline int dummy_key_alloc(struct key *key)
+static inline int dummy_key_alloc(struct key *key, struct task_struct *ctx)
 {
 	return 0;
 }
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/key.c linux-2.6.17-rc5-mm1/security/keys/key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/key.c	2006-05-31 10:03:00.000000000 -0400
@@ -247,8 +247,8 @@ static inline void key_alloc_serial(stru
  *   instantiate the key or discard it before returning
  */
 struct key *key_alloc(struct key_type *type, const char *desc,
-		      uid_t uid, gid_t gid, key_perm_t perm,
-		      int not_in_quota)
+		      uid_t uid, gid_t gid, struct task_struct *ctx,
+		      key_perm_t perm, int not_in_quota)
 {
 	struct key_user *user = NULL;
 	struct key *key;
@@ -318,7 +318,7 @@ struct key *key_alloc(struct key_type *t
 #endif
 
 	/* let the security module know about the key */
-	ret = security_key_alloc(key);
+	ret = security_key_alloc(key, ctx);
 	if (ret < 0)
 		goto security_error;
 
@@ -822,7 +822,7 @@ key_ref_t key_create_or_update(key_ref_t
 
 	/* allocate a new key */
 	key = key_alloc(ktype, description, current->fsuid, current->fsgid,
-			perm, not_in_quota);
+			current, perm, not_in_quota);
 	if (IS_ERR(key)) {
 		key_ref = ERR_PTR(PTR_ERR(key));
 		goto error_3;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c linux-2.6.17-rc5-mm1/security/keys/keyring.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/keyring.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/keyring.c	2006-05-31 10:03:00.000000000 -0400
@@ -240,13 +240,14 @@ static long keyring_read(const struct ke
  * allocate a keyring and link into the destination keyring
  */
 struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
-			  int not_in_quota, struct key *dest)
+			  struct task_struct *ctx, int not_in_quota,
+			  struct key *dest)
 {
 	struct key *keyring;
 	int ret;
 
 	keyring = key_alloc(&key_type_keyring, description,
-			    uid, gid,
+			    uid, gid, ctx,
 			    (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
 			    not_in_quota);
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c linux-2.6.17-rc5-mm1/security/keys/process_keys.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/process_keys.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/process_keys.c	2006-05-31 10:03:00.000000000 -0400
@@ -67,7 +67,8 @@ struct key root_session_keyring = {
 /*
  * allocate the keyrings to be associated with a UID
  */
-int alloc_uid_keyring(struct user_struct *user)
+int alloc_uid_keyring(struct user_struct *user,
+		      struct task_struct *ctx)
 {
 	struct key *uid_keyring, *session_keyring;
 	char buf[20];
@@ -76,7 +77,7 @@ int alloc_uid_keyring(struct user_struct
 	/* concoct a default session keyring */
 	sprintf(buf, "_uid_ses.%u", user->uid);
 
-	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
+	session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0, NULL);
 	if (IS_ERR(session_keyring)) {
 		ret = PTR_ERR(session_keyring);
 		goto error;
@@ -86,7 +87,7 @@ int alloc_uid_keyring(struct user_struct
 	 * keyring */
 	sprintf(buf, "_uid.%u", user->uid);
 
-	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
+	uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx, 0,
 				    session_keyring);
 	if (IS_ERR(uid_keyring)) {
 		key_put(session_keyring);
@@ -143,7 +144,7 @@ int install_thread_keyring(struct task_s
 
 	sprintf(buf, "_tid.%u", tsk->pid);
 
-	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+	keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error;
@@ -177,7 +178,7 @@ int install_process_keyring(struct task_
 	if (!tsk->signal->process_keyring) {
 		sprintf(buf, "_pid.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error;
@@ -217,7 +218,7 @@ static int install_session_keyring(struc
 	if (!keyring) {
 		sprintf(buf, "_ses.%u", tsk->tgid);
 
-		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
+		keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk, 1, NULL);
 		if (IS_ERR(keyring))
 			return PTR_ERR(keyring);
 	}
@@ -727,7 +728,7 @@ long join_session_keyring(const char *na
 	keyring = find_keyring_by_name(name, 0);
 	if (PTR_ERR(keyring) == -ENOKEY) {
 		/* not found - try and create a new one */
-		keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
+		keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk, 0, NULL);
 		if (IS_ERR(keyring)) {
 			ret = PTR_ERR(keyring);
 			goto error2;
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key_auth.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key_auth.c	2006-05-31 10:03:00.000000000 -0400
@@ -185,7 +185,7 @@ struct key *request_key_auth_new(struct 
 	sprintf(desc, "%x", target->serial);
 
 	authkey = key_alloc(&key_type_request_key_auth, desc,
-			    current->fsuid, current->fsgid,
+			    current->fsuid, current->fsgid, current,
 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
 			    KEY_USR_VIEW, 1);
 	if (IS_ERR(authkey)) {
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c linux-2.6.17-rc5-mm1/security/keys/request_key.c
--- linux-2.6.17-rc5-mm1.orig/security/keys/request_key.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/keys/request_key.c	2006-05-31 10:03:00.000000000 -0400
@@ -48,7 +48,8 @@ static int call_sbin_request_key(struct 
 	/* allocate a new session keyring */
 	sprintf(desc, "_req.%u", key->serial);
 
-	keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
+	keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
+				current, 1, NULL);
 	if (IS_ERR(keyring)) {
 		ret = PTR_ERR(keyring);
 		goto error_alloc;
@@ -137,7 +138,8 @@ static struct key *__request_key_constru
 
 	/* create a key and add it to the queue */
 	key = key_alloc(type, description,
-			current->fsuid, current->fsgid, KEY_POS_ALL, 0);
+			current->fsuid, current->fsgid,
+			current, KEY_POS_ALL, 0);
 	if (IS_ERR(key))
 		goto alloc_failed;
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c linux-2.6.17-rc5-mm1/security/selinux/hooks.c
--- linux-2.6.17-rc5-mm1.orig/security/selinux/hooks.c	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/hooks.c	2006-06-01 11:32:19.000000000 -0400
@@ -4252,6 +4252,57 @@ static int selinux_setprocattr(struct ta
 	return size;
 }
 
+#ifdef CONFIG_KEYS
+
+static int selinux_key_alloc(struct key *k, struct task_struct *tsk)
+{
+	struct task_security_struct *tsec = tsk->security;
+	struct key_security_struct *ksec;
+
+	ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
+	if (!ksec)
+		return -ENOMEM;
+
+	ksec->obj = k;
+	ksec->sid = tsec->sid;
+	k->security = ksec;
+
+	return 0;
+}
+
+static void selinux_key_free(struct key *k)
+{
+	struct key_security_struct *ksec = k->security;
+
+	k->security = NULL;
+	kfree(ksec);
+}
+
+static int selinux_key_permission(key_ref_t key_ref,
+			    struct task_struct *ctx,
+			    key_perm_t perm)
+{
+	struct key *key;
+	struct task_security_struct *tsec;
+	struct key_security_struct *ksec;
+
+	key = key_ref_to_ptr(key_ref);
+
+	tsec = ctx->security;
+	ksec = key->security;
+
+	/* if no specific permissions are requested, we skip the
+	   permission check. No serious, additional covert channels
+	   appear to be created. */
+	if (perm == 0)
+		return 0;
+
+	return avc_has_perm(tsec->sid, ksec->sid,
+			    SECCLASS_KEY, perm, NULL);
+}
+
+#endif
+
 static struct security_operations selinux_ops = {
 	.ptrace =			selinux_ptrace,
 	.capget =			selinux_capget,
@@ -4404,6 +4455,12 @@ static struct security_operations selinu
 	.xfrm_state_free_security =	selinux_xfrm_state_free,
 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
 #endif
+
+#ifdef CONFIG_KEYS
+	.key_alloc =                    selinux_key_alloc,
+	.key_free =                     selinux_key_free,
+	.key_permission =               selinux_key_permission,
+#endif
 };
 
 static __init int selinux_init(void)
@@ -4439,6 +4496,13 @@ static __init int selinux_init(void)
 	} else {
 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
 	}
+
+#ifdef CONFIG_KEYS
+	/* Add security information to initial keyrings */
+	security_key_alloc(&root_user_keyring, current);
+	security_key_alloc(&root_session_keyring, current);
+#endif
+
 	return 0;
 }
 
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_permissions.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_permissions.h	2006-06-01 10:42:18.000000000 -0400
@@ -959,3 +959,11 @@
 #define PACKET__SEND                              0x00000001UL
 #define PACKET__RECV                              0x00000002UL
 #define PACKET__RELABELTO                         0x00000004UL
+
+#define KEY__VIEW                                 0x00000001UL
+#define KEY__READ                                 0x00000002UL
+#define KEY__WRITE                                0x00000004UL
+#define KEY__SEARCH                               0x00000008UL
+#define KEY__LINK                                 0x00000010UL
+#define KEY__SETATTR                              0x00000020UL
+
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/av_perm_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/av_perm_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -242,3 +242,9 @@
    S_(SECCLASS_PACKET, PACKET__SEND, "send")
    S_(SECCLASS_PACKET, PACKET__RECV, "recv")
    S_(SECCLASS_PACKET, PACKET__RELABELTO, "relabelto")
+   S_(SECCLASS_KEY, KEY__VIEW, "view")
+   S_(SECCLASS_KEY, KEY__READ, "read")
+   S_(SECCLASS_KEY, KEY__WRITE, "write")
+   S_(SECCLASS_KEY, KEY__SEARCH, "search")
+   S_(SECCLASS_KEY, KEY__LINK, "link")
+   S_(SECCLASS_KEY, KEY__SETATTR, "setattr")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/class_to_string.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/class_to_string.h	2006-06-01 10:42:18.000000000 -0400
@@ -60,3 +60,4 @@
     S_("netlink_kobject_uevent_socket")
     S_("appletalk_socket")
     S_("packet")
+    S_("key")
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h linux-2.6.17-rc5-mm1/security/selinux/include/flask.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/flask.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/flask.h	2006-06-01 10:42:18.000000000 -0400
@@ -62,6 +62,7 @@
 #define SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET           55
 #define SECCLASS_APPLETALK_SOCKET                        56
 #define SECCLASS_PACKET                                  57
+#define SECCLASS_KEY                                     58
 
 /*
  * Security identifier indices for initial entities
diff -uprN -X linux-2.6.17-rc5-mm1/Documentation/dontdiff linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h
--- linux-2.6.17-rc5-mm1.orig/security/selinux/include/objsec.h	2006-05-31 09:48:49.000000000 -0400
+++ linux-2.6.17-rc5-mm1/security/selinux/include/objsec.h	2006-05-31 10:03:00.000000000 -0400
@@ -99,6 +99,11 @@ struct sk_security_struct {
 	u32 peer_sid;			/* SID of peer */
 };
 
+struct key_security_struct {
+	struct key *obj; /* back pointer */
+	u32 sid;         /* SID of key */
+};
+
 extern unsigned int selinux_checkreqprot;
 
 #endif /* _SELINUX_OBJSEC_H_ */



--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:30           ` David Howells
@ 2006-06-02 13:52             ` Stephen Smalley
  2006-06-02 13:53               ` David Howells
  0 siblings, 1 reply; 41+ messages in thread
From: Stephen Smalley @ 2006-06-02 13:52 UTC (permalink / raw)
  To: David Howells; +Cc: Michael LeMay, jmorris, selinux

On Fri, 2006-06-02 at 14:30 +0100, David Howells wrote:
> Stephen Smalley <sds@tycho.nsa.gov> wrote:
> 
> > > Discretionary access control is what the basic Linux access control
> > > model is.  Mandatory access control is what SELinux provides.
> > 
> > So to clarify:  SELinux is presently performing a parallel MAC
> > permission check for each existing DAC permission check, and the MAC
> > check occurs after the DAC check (and only occurs if the DAC check
> > succeeds).  
> 
> That makes things clearer, thanks; though why is it called a 'DAC' when it is
> in fact mandatory?  And why 'MAC' when it's discretionary in the fact that
> it's _not_ checked if you don't have a security context?

Discretionary means "at the discretion of the user or program", i.e. the
standard Linux permission checks are based on permission bits set by the
user/program, and the user/program is free to give away access
arbitrarily, making it vulnerable to flawed and malicious applications
and to careless users.  Mandatory means administratively-defined policy
along with control over all processes and objects and decisions based on
all security-relevant information (more than just user identity).

Everything has a security context assigned to it (whether via default
inheritance or explicit labeling), and the SELinux MAC check is always
applied before any operation is permitted.  But the SELinux MAC check
won't be reached if some earlier error condition occurs, whether that is
a validation error on some input argument that precedes permission
checks entirely or an error from the DAC permission checks.  That is ok,
as the operation won't proceed in that situation, and there is no
information leak since both DAC and MAC denials yield EACCES.

-- 
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:52             ` Stephen Smalley
@ 2006-06-02 13:53               ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-02 13:53 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: David Howells, Michael LeMay, jmorris, selinux

Stephen Smalley <sds@tycho.nsa.gov> wrote:

> Discretionary means "at the discretion of the user or program", i.e. the
> standard Linux permission checks are based on permission bits set by the
> user/program, and the user/program is free to give away access
> arbitrarily, making it vulnerable to flawed and malicious applications
> and to careless users.  Mandatory means administratively-defined policy
> along with control over all processes and objects and decisions based on
> all security-relevant information (more than just user identity).

Aha! Gotcha! Thanks.

David

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:45 Michael LeMay
@ 2006-06-02 14:10 ` David Howells
  0 siblings, 0 replies; 41+ messages in thread
From: David Howells @ 2006-06-02 14:10 UTC (permalink / raw)
  To: Michael LeMay; +Cc: jmorris, selinux, sds, dhowells

Michael LeMay <mdlemay@epoch.ncsc.mil> wrote:

> Introduce SELinux hooks to support the access key retention subsystem
> within the kernel.  Incorporate new flask headers from a modified
> version of the SELinux reference policy, with support for the new
> security class representing retained keys.  Extend the "key_alloc"
> security hook with a task parameter representing the intended
> ownership context for the key being allocated.  Attach security
> information to root's default keyrings within the SELinux
> initialization routine.
> 
> Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>

I've thrown my testsuite at it and that seems to work still:

	http://people.redhat.com/~dhowells/keyutils/keyutils-tests.tar.bz2


Signed-Off-By: David Howells <dhowells@redhat.com>


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 13:08       ` Stephen Smalley
  2006-06-02 13:25         ` David Howells
  2006-06-02 13:28         ` Stephen Smalley
@ 2006-06-02 15:54         ` Casey Schaufler
  2 siblings, 0 replies; 41+ messages in thread
From: Casey Schaufler @ 2006-06-02 15:54 UTC (permalink / raw)
  To: Stephen Smalley, David Howells; +Cc: Michael LeMay, jmorris, selinux



--- Stephen Smalley <sds@tycho.nsa.gov> wrote:


> Discretionary access control is what the basic Linux
> access control
> model is.  Mandatory access control is what SELinux
> provides.

With user settable MCS labels SELinux provides a
Discretionary Access Control mechanism.
 

Casey Schaufler
casey@schaufler-ca.com

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* RE: [PATCH] selinux: Kernel access key retention support for SELinux
@ 2006-06-02 17:09 Frank L. Mayer
  2006-06-02 17:53 ` Casey Schaufler
  0 siblings, 1 reply; 41+ messages in thread
From: Frank L. Mayer @ 2006-06-02 17:09 UTC (permalink / raw)
  To: casey, Stephen Smalley, David Howells; +Cc: Michael LeMay, jmorris, selinux

> > Discretionary access control is what the basic Linux access control 
> > model is.  Mandatory access control is what SELinux provides.
> 
> With user settable MCS labels SELinux provides a 
> Discretionary Access Control mechanism.

And with non-user settable MLS and/or Type labels SELinux provides MAC.
Hence Steve's point.  Frank


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* RE: [PATCH] selinux: Kernel access key retention support for SELinux
  2006-06-02 17:09 Frank L. Mayer
@ 2006-06-02 17:53 ` Casey Schaufler
  0 siblings, 0 replies; 41+ messages in thread
From: Casey Schaufler @ 2006-06-02 17:53 UTC (permalink / raw)
  To: Frank L. Mayer, Stephen Smalley, David Howells
  Cc: Michael LeMay, jmorris, selinux



--- "Frank L. Mayer" <mayerf@tresys.com> wrote:

> > > Discretionary access control is what the basic
> Linux access control 
> > > model is.  Mandatory access control is what
> SELinux provides.
> > 
> > With user settable MCS labels SELinux provides a 
> > Discretionary Access Control mechanism.
> 
> And with non-user settable MLS and/or Type labels
> SELinux provides MAC.
> Hence Steve's point.  Frank

My point is that SELinux is not ONLY a MAC mechanism.
SELinux does indeed provide MAC. It also provides DAC.
SELinux has more than MAC now. Thus, holding SELinux
up as an example of MAC is no longer correct, because
SELinux includes a DAC mechanism in MCS.

It's easy enough to confuse people with our
rich set of trusted systems jargon when it is
used correctly (my favorite is the difference
between a Bell & LePadula label and a level)
without improperly identifying a popular and
visible product (SELinux) as a particular thing
(A MAC system) when that identification is not
correct (because SELinux also supports DAC).


Casey Schaufler
casey@schaufler-ca.com

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 41+ messages in thread

end of thread, other threads:[~2006-06-02 17:54 UTC | newest]

Thread overview: 41+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-25 16:50 [PATCH] selinux: Kernel access key retention support for SELinux Michael LeMay
2006-05-26  9:37 ` David Howells
2006-05-26 13:54   ` David Howells
2006-05-26 12:30 ` Stephen Smalley
2006-05-26 12:56   ` Stephen Smalley
2006-05-26 12:31 ` Stephen Smalley
2006-05-26 13:00 ` Stephen Smalley
  -- strict thread matches above, loose matches on Subject: below --
2006-05-31 17:50 Michael LeMay
2006-05-31 18:29 ` Stephen Smalley
2006-06-01 10:13   ` David Howells
2006-05-31 20:42 ` James Morris
2006-06-01  9:32   ` David Howells
2006-06-01 14:26     ` James Morris
2006-06-01 14:36       ` David Howells
2006-06-01 10:09 ` David Howells
2006-06-01 11:39   ` Stephen Smalley
2006-06-01 12:27     ` David Howells
2006-06-01 14:21       ` Stephen Smalley
2006-06-01 14:52         ` Valdis.Kletnieks
     [not found]           ` <447F0093.3090601@lemaymd.com>
2006-06-01 15:17             ` Stephen Smalley
2006-06-01 15:25               ` Stephen Smalley
2006-06-01 14:40 ` David Howells
2006-06-01 18:26 Michael LeMay
2006-06-01 23:40 ` James Morris
2006-06-02  9:21 ` David Howells
2006-06-02 12:44   ` Michael LeMay
2006-06-02 13:02     ` David Howells
2006-06-02 13:08       ` Stephen Smalley
2006-06-02 13:25         ` David Howells
2006-06-02 13:28         ` Stephen Smalley
2006-06-02 13:30           ` Stephen Smalley
2006-06-02 13:30           ` David Howells
2006-06-02 13:52             ` Stephen Smalley
2006-06-02 13:53               ` David Howells
2006-06-02 15:54         ` Casey Schaufler
2006-06-02 13:27 Michael LeMay
2006-06-02 13:38 ` David Howells
2006-06-02 13:45 Michael LeMay
2006-06-02 14:10 ` David Howells
2006-06-02 17:09 Frank L. Mayer
2006-06-02 17:53 ` Casey Schaufler

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.