KEYS: Increase the payload size when instantiating a key
Subsystems (4)
David Howells
maintainerPATCH 1/4 KEYS: Increase the payload size when instantiating a key
Increase the size of a payload that can be used to instantiate a key in add_key() and keyctl_instantiate_key(). This permits huge CIFS SPNEGO blobs to be passed around. The limit is raised to 1MB. If kmalloc() can't allocate a buffer of sufficient size, vmalloc() will be tried instead.
---
security/keys/keyctl.c | 38 ++++++++++++++++++++++++++++++-------- 1 files changed, 30 insertions(+), 8 deletions(-)
security/keys/keyctl.c +30 -8
@@ -19,6 +19,7 @@#include <linux/capability.h> #include <linux/string.h> #include <linux/err.h> +#include <linux/vmalloc.h> #include <asm/uaccess.h> #include "internal.h"
@@ -62,9 +63,10 @@ asmlinkage long sys_add_key(const char __user *_type,char type[32 ], *description;void *payload;long ret; +bool vm; ret = -EINVAL ; -if (plen >32767 ) +if (plen >1024 *1024 -1 ) goto error;/* draw all the data into kernel space */
@@ -81,11 +83,18 @@ asmlinkage long sys_add_key(const char __user *_type,/* pull the payload in if one was supplied */ payload =NULL ; + vm = false;if (_payload) { ret = -ENOMEM ; payload =kmalloc (plen,GFP_KERNEL ); -if (!payload) - goto error2; +if (!payload) { +if (plen <=PAGE_SIZE ) + goto error2; + vm = true; + payload =vmalloc (plen); +if (!payload) + goto error2; + } ret = -EFAULT ;if (copy_from_user (payload, _payload, plen) !=0 )
@@ -113,7 +122,10 @@ asmlinkage long sys_add_key(const char __user *_type,key_ref_put (keyring_ref); error3: -kfree (payload); +if (!vm) +kfree (payload); +else +vfree (payload); error2:kfree (description); error:
@@ -821,9 +833,10 @@ long keyctl_instantiate_key(key_serial_t id,key_ref_t keyring_ref;void *payload;long ret; +bool vm = false; ret = -EINVAL ; -if (plen >32767 ) +if (plen >1024 *1024 -1 ) goto error;/* the appropriate instantiation authorisation key must have been
@@ -843,8 +856,14 @@ long keyctl_instantiate_key(key_serial_t id,if (_payload) { ret = -ENOMEM ; payload =kmalloc (plen,GFP_KERNEL ); -if (!payload) - goto error; +if (!payload) { +if (plen <=PAGE_SIZE ) + goto error; + vm = true; + payload =vmalloc (plen); +if (!payload) + goto error; + } ret = -EFAULT ;if (copy_from_user (payload, _payload, plen) !=0 )
@@ -877,7 +896,10 @@ long keyctl_instantiate_key(key_serial_t id, } error2: -kfree (payload); +if (!vm) +kfree (payload); +else +vfree (payload); error:return ret;
David Howells
maintainerPATCH 2/4 KEYS: Check starting keyring as part of search
Check the starting keyring as part of the search to (a) see if that is what we're searching for, and (b) to check it is still valid for searching.
The scenario: User in process A does things that cause things to be created in its process session keyring. The user then does an su to another user and starts a new process, B. The two processes now share the same process session keyring.
Process B does an NFS access which results in an upcall to gssd. When gssd attempts to instantiate the context key (to be linked into the process session keyring), it is denied access even though it has an authorization key.
The order of calls is:
keyctl_instantiate_key()
lookup_user_key() (the default: case)
search_process_keyrings(current)
search_process_keyrings(rka->context) (recursive call)
keyring_search_aux()
keyring_search_aux() verifies the keys and keyrings underneath the top-level keyring it is given, but that top-level keyring is neither fully validated nor checked to see if it is the thing being searched for.
This patch changes keyring_search_aux() to:
1) do more validation on the top keyring it is given and
2) check whether that top-level keyring is the thing being searched for
---
security/keys/keyring.c | 35 +++++++++++++++++++++++++++++++---- 1 files changed, 31 insertions(+), 4 deletions(-)
security/keys/keyring.c +31 -4
@@ -292,7 +292,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,struct keyring_list *keylist;struct timespec now; -unsigned long possessed; +unsigned long possessed, kflags;struct key *keyring, *key;key_ref_t key_ref;long err;
@@ -318,6 +318,32 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref, now =current_kernel_time (); err = -EAGAIN ; sp =0 ; + +/* firstly we should check to see if this top-level keyring is what we +* are looking for */ + key_ref =ERR_PTR (-EAGAIN ); + kflags = keyring->flags; +if (keyring->type == type &&match (keyring, description)) { + key = keyring; + +/* check it isn't negative and hasn't expired or been +* revoked */ +if (kflags & (1 <<KEY_FLAG_REVOKED )) + goto error_2; +if (key->expiry && now.tv_sec >= key->expiry) + goto error_2; + key_ref =ERR_PTR (-ENOKEY ); +if (kflags & (1 <<KEY_FLAG_NEGATIVE )) + goto error_2; + goto found; + } + +/* otherwise, the top keyring must not be revoked, expired, or +* negatively instantiated if we are to search it */ + key_ref =ERR_PTR (-EAGAIN ); +if (kflags & ((1 <<KEY_FLAG_REVOKED ) | (1 <<KEY_FLAG_NEGATIVE )) || + (keyring->expiry && now.tv_sec >= keyring->expiry)) + goto error_2;/* start processing a new keyring */ descend:
@@ -331,13 +357,14 @@ descend:/* iterate through the keys in this keyring first */ for (kix =0 ; kix < keylist->nkeys; kix++) { key = keylist->keys[kix]; + kflags = key->flags;/* ignore keys not of this type */ if (key->type != type)continue ;/* skip revoked keys and expired keys */ -if (test_bit (KEY_FLAG_REVOKED , &key->flags)) +if (kflags & (1 <<KEY_FLAG_REVOKED ))continue ;if (key->expiry && now.tv_sec >= key->expiry)
@@ -352,8 +379,8 @@ descend: context, KEY_SEARCH) < 0)continue ; -/* we set a different error code if we find a negative key */ -if (test_bit (KEY_FLAG_NEGATIVE , &key->flags)) { +/* we set a different error code if we pass a negative key */ +if (kflags & (1 <<KEY_FLAG_NEGATIVE )) { err = -ENOKEY ;continue ; }
Re: [PATCH 2/4] KEYS: Check starting keyring as part of search
Quoted 31 lines · On Fri, 08 Feb 2008 16:04:29 +0000 David Howells <[email protected]> wrote:
Check the starting keyring as part of the search to (a) see if that is what we're searching for, and (b) to check it is still valid for searching.
The scenario: User in process A does things that cause things to be created in its process session keyring. The user then does an su to another user and starts a new process, B. The two processes now share the same process session keyring.
Process B does an NFS access which results in an upcall to gssd. When gssd attempts to instantiate the context key (to be linked into the process session keyring), it is denied access even though it has an authorization key.
The order of calls is:
keyctl_instantiate_key()
lookup_user_key()(the default: case)
search_process_keyrings(current)
search_process_keyrings(rka->context)(recursive call)
keyring_search_aux()
keyring_search_aux()verifies the keys and keyrings underneath the top-level keyring it is given, but that top-level keyring is neither fully validated nor checked to see if it is the thing being searched for.This patch changes
keyring_search_aux()to:
1) do more validation on the top keyring it is given and
2) check whether that top-level keyring is the thing being searched for...
security/keys/keyring.c key_ref_t keyring_search_aux(key_ref_t keyring_ref, · line 345
Quoted 1 line
security/keys/keyring.c +1
-0+ (keyring->expiry && now.tv_sec >= keyring->expiry))
time_after()?
David Howells
maintainerRe: [PATCH 2/4] KEYS: Check starting keyring as part of search
Quoted 3 lines · Andrew Morton
security/keys/keyring.c key_ref_t keyring_search_aux(key_ref_t keyring_ref, · line 345
Quoted 1 line
security/keys/keyring.c +1
-0+ (keyring->expiry && now.tv_sec >= keyring->expiry))
time_after()?
Possibly. tv_sec oughtn't to wrap, though. I thought time_after() is really for comparing jiffies which might wrap.
David
David Howells
maintainerPATCH 3/4 KEYS: Allow the callout data to be passed as a blob rather than a string
In reply to David HowellsAllow the callout data to be passed as a blob rather than a string for internal kernel services that call any request_key_*() interface other than request_key(). request_key() itself still takes a NUL-terminated string.
The functions that change are:
request_key_with_auxdata()
request_key_async()
request_key_async_with_auxdata()
---
Documentation/keys-request-key.txt | 11 +++++--- Documentation/keys.txt | 14 +++++++--- include/linux/key.h | 9 ++++--- security/keys/internal.h | 9 ++++--- security/keys/keyctl.c | 7 ++++- security/keys/request_key.c | 49 ++++++++++++++++++++++-------------- security/keys/request_key_auth.c | 12 +++++---- 7 files changed, 70 insertions(+), 41 deletions(-)
Documentation/keys-request-key.txt +7 -4
@@ -11,26 +11,29 @@ request_key*(): struct key *request_key(const struct key_type *type, const char *description, - const char *callout_string); + const char *callout_info); or: struct key *request_key_with_auxdata(const struct key_type *type, const char *description, - const char *callout_string, + const char *callout_info, + size_t callout_len, void *aux); or: struct key *request_key_async(const struct key_type *type, const char *description, - const char *callout_string); + const char *callout_info, + size_t callout_len); or: struct key *request_key_async_with_auxdata(const struct key_type *type, const char *description, - const char *callout_string, + const char *callout_info, + size_t callout_len, void *aux); Or by userspace invoking the request_key system call:
Documentation/keys.txt +9 -5
@@ -771,7 +771,7 @@ payload contents" for more information. struct key *request_key(const struct key_type *type, const char *description, - const char *callout_string); + const char *callout_info); This is used to request a key or keyring with a description that matches the description specified according to the key type's match function. This
@@ -793,24 +793,28 @@ payload contents" for more information. struct key *request_key_with_auxdata(const struct key_type *type, const char *description, - const char *callout_string, + const void *callout_info, + size_t callout_len, void *aux); This is identical to request_key(), except that the auxiliary data is - passed to the key_type->request_key() op if it exists. + passed to the key_type->request_key() op if it exists, and the callout_info + is a blob of length callout_len, if given (the length may be 0). (*) A key can be requested asynchronously by calling one of: struct key *request_key_async(const struct key_type *type, const char *description, - const char *callout_string); + const void *callout_info, + size_t callout_len); or: struct key *request_key_async_with_auxdata(const struct key_type *type, const char *description, - const char *callout_string, + const char *callout_info, + size_t callout_len, void *aux); which are asynchronous equivalents of request_key() and
include/linux/key.h +6 -3
@@ -208,16 +208,19 @@ extern struct key *request_key(struct key_type *type,extern struct key *request_key_with_auxdata (struct key_type *type,const char *description, -const char *callout_info, +const void *callout_info, +size_t callout_len,void *aux);extern struct key *request_key_async (struct key_type *type,const char *description, -const char *callout_info); +const void *callout_info, +size_t callout_len);extern struct key *request_key_async_with_auxdata (struct key_type *type,const char *description, -const char *callout_info, +const void *callout_info, +size_t callout_len,void *aux);extern int wait_for_key_construction (struct key *key,bool intr);
security/keys/internal.h +6 -3
@@ -109,7 +109,8 @@ extern int install_process_keyring(struct task_struct *tsk);extern struct key *request_key_and_link (struct key_type *type,const char *description, -const char *callout_info, +const void *callout_info, +size_t callout_len,void *aux,struct key *dest_keyring,unsigned long flags);
@@ -120,13 +121,15 @@ extern struct key *request_key_and_link(struct key_type *type,struct request_key_auth {struct key *target_key;struct task_struct *context; -char *callout_info; +void *callout_info; +size_t callout_len;pid_t pid; };extern struct key_type key_type_request_key_auth;extern struct key *request_key_auth_new (struct key *target, -const char *callout_info); +const void *callout_info, +size_t callout_len);extern struct key *key_get_instantiation_authkey (key_serial_t target_id);
security/keys/keyctl.c +5 -2
@@ -152,6 +152,7 @@ asmlinkage long sys_request_key(const char __user *_type,struct key_type *ktype;struct key *key;key_ref_t dest_ref; +size_t callout_len;char type[32 ], *description, *callout_info;long ret;
@@ -169,12 +170,14 @@ asmlinkage long sys_request_key(const char __user *_type,/* pull the callout info into kernel space */ callout_info =NULL ; + callout_len =0 ;if (_callout_info) { callout_info =strndup_user (_callout_info,PAGE_SIZE );if (IS_ERR (callout_info)) { ret =PTR_ERR (callout_info); goto error2; } + callout_len =strlen (callout_info); }/* get the destination keyring if specified */
@@ -195,8 +198,8 @@ asmlinkage long sys_request_key(const char __user *_type, }/* do the search */ - key =request_key_and_link (ktype, description, callout_info,NULL , -key_ref_to_ptr (dest_ref), + key =request_key_and_link (ktype, description, callout_info, + callout_len,NULL ,key_ref_to_ptr (dest_ref),KEY_ALLOC_IN_QUOTA );if (IS_ERR (key)) { ret =PTR_ERR (key);
security/keys/request_key.c +30 -19
130 lines not on this page · raw message
security/keys/request_key_auth.c +7 -5
@@ -61,7 +61,7 @@ static void request_key_auth_describe(const struct key *key,seq_puts (m,"key:" );seq_puts (m, key->description); -seq_printf (m," pid:%d ci:%zu" , rka->pid,strlen (rka->callout_info)); +seq_printf (m," pid:%d ci:%zu" , rka->pid, rka->callout_len); }/* end request_key_auth_describe() */
@@ -77,7 +77,7 @@ static long request_key_auth_read(const struct key *key,size_t datalen;long ret; - datalen =strlen (rka->callout_info); + datalen = rka->callout_len; ret = datalen;/* we can return the data as is */
@@ -137,7 +137,8 @@ static void request_key_auth_destroy(struct key *key)* create an authorisation token for /sbin/request-key or whoever to gain * access to the caller's security data */ -struct key *request_key_auth_new (struct key *target,const char *callout_info) +struct key *request_key_auth_new (struct key *target,const void *callout_info, +size_t callout_len) {struct request_key_auth *rka, *irka;struct key *authkey =NULL ;
@@ -152,7 +153,7 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info)kleave (" = -ENOMEM" );return ERR_PTR (-ENOMEM ); } - rka->callout_info =kmalloc (strlen (callout_info) +1 ,GFP_KERNEL ); + rka->callout_info =kmalloc (callout_len,GFP_KERNEL );if (!rka->callout_info) {kleave (" = -ENOMEM" );kfree (rka);
@@ -186,7 +187,8 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info) } rka->target_key =key_get (target); -strcpy (rka->callout_info, callout_info); +memcpy (rka->callout_info, callout_info, callout_len); + rka->callout_len = callout_len;/* allocate the auth key */ sprintf (desc,"%x" , target->serial);
David Howells
maintainerPATCH 4/4 KEYS: Add keyctl function to get a security label
In reply to David HowellsAdd a keyctl() function to get the security label of a key.
The following is added to Documentation/keys.txt:
(*) Get the LSM security context attached to a key.
long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer,
size_t buflen)
This function returns a string that represents the LSM security context attached to a key in the buffer provided.
Unless there's an error, it always returns the amount of data it could produce, even if that's too big for the buffer, but it won't copy more than requested to userspace. If the buffer pointer is NULL then no copy will take place.
A NUL character is included at the end of the string if the buffer is sufficiently big. This is included in the returned count. If no LSM is in force then an empty string will be returned.
A process must have view permission on the key for this function to be successful.
---
Documentation/keys.txt | 21 +++++++++++++++ include/linux/keyctl.h | 1 + include/linux/security.h | 20 +++++++++++++- security/dummy.c | 8 ++++++ security/keys/compat.c | 3 ++ security/keys/keyctl.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ security/security.c | 5 +++ security/selinux/hooks.c | 21 +++++++++++++-- 8 files changed, 141 insertions(+), 4 deletions(-)
Documentation/keys.txt +21 -0
@@ -711,6 +711,27 @@ The keyctl syscall functions are: The assumed authoritative key is inherited across fork and exec. + (*) Get the LSM security context attached to a key. + + long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer, + size_t buflen) + + This function returns a string that represents the LSM security context + attached to a key in the buffer provided. + + Unless there's an error, it always returns the amount of data it could + produce, even if that's too big for the buffer, but it won't copy more + than requested to userspace. If the buffer pointer is NULL then no copy + will take place. + + A NUL character is included at the end of the string if the buffer is + sufficiently big. This is included in the returned count. If no LSM is + in force then an empty string will be returned. + + A process must have view permission on the key for this function to be + successful. + + =============== KERNEL SERVICES ===============
include/linux/keyctl.h +1 -0
@@ -49,5 +49,6 @@#define KEYCTL_SET_REQKEY_KEYRING 14/* set default request-key keyring */ #define KEYCTL_SET_TIMEOUT 15/* set key timeout */ #define KEYCTL_ASSUME_AUTHORITY 16/* assume request_key() authorisation */ +#define KEYCTL_GET_SECURITY 17/* get key security label */ #endif /* _LINUX_KEYCTL_H */
include/linux/security.h +19 -1
@@ -970,6 +970,17 @@ struct request_sock;* @perm describes the combination of permissions required of this key. * Return 1 if permission granted, 0 if permission denied and -ve it the * normal permissions model should be effected. +* @key_getsecurity: +* Get a textual representation of the security context attached to a key +* for the purposes of honouring KEYCTL_GETSECURITY. This function +* allocates the storage for the NUL-terminated string and the caller +* should free it. +* @key points to the key to be queried. +* @_buffer points to a pointer that should be set to point to the +* resulting string (if no label or an error occurs). +* Return the length of the string (including terminating NUL) or -ve if +* an error. +* May also return 0 (and a NULL buffer pointer) if there is no label. * * Security hooks affecting all System V IPC operations. *
@@ -1459,7 +1470,7 @@ struct security_operations {int (*key_permission)(key_ref_t key_ref,struct task_struct *context,key_perm_t perm); - +int (*key_getsecurity)(struct key *key,char **_buffer);#endif /* CONFIG_KEYS */ };
@@ -2600,6 +2611,7 @@ int security_key_alloc(struct key *key, struct task_struct *tsk, unsigned long fvoid security_key_free (struct key *key);int security_key_permission (key_ref_t key_ref,struct task_struct *context,key_perm_t perm); +int security_key_getsecurity (struct key *key,char **_buffer);#else
@@ -2621,6 +2633,12 @@ static inline int security_key_permission(key_ref_t key_ref,return 0 ; } +static inline int security_key_getsecurity (struct key *key,char **_buffer) +{ + *_buffer =NULL ; +return 0 ; +} +#endif #endif /* CONFIG_KEYS */
security/dummy.c +8 -0
@@ -977,6 +977,13 @@ static inline int dummy_key_permission(key_ref_t key_ref, {return 0 ; } + +static int dummy_key_getsecurity (struct key *key,char **_buffer) +{ + *_buffer =NULL ; +return 0 ; +} +#endif /* CONFIG_KEYS */ struct security_operations dummy_security_ops;
@@ -1164,6 +1171,7 @@ void security_fixup_ops (struct security_operations *ops)set_to_dummy_if_null (ops, key_alloc);set_to_dummy_if_null (ops, key_free);set_to_dummy_if_null (ops, key_permission); +set_to_dummy_if_null (ops, key_getsecurity);#endif /* CONFIG_KEYS */ }
security/keys/compat.c +3 -0
@@ -79,6 +79,9 @@ asmlinkage long compat_sys_keyctl(u32 option,case KEYCTL_ASSUME_AUTHORITY :return keyctl_assume_authority (arg2); +case KEYCTL_GET_SECURITY : +return keyctl_get_security (arg2,compat_ptr (arg3), arg4); +default :return -EOPNOTSUPP ; }
security/keys/keyctl.c +66 -0
84 lines not on this page · raw message
security/security.c +5 -0
@@ -1102,4 +1102,9 @@ int security_key_permission(key_ref_t key_ref, return security_ops->key_permission(key_ref, context, perm); } +int security_key_getsecurity (struct key *key,char **_buffer) +{ +return security_ops->key_getsecurity (key, _buffer); +} +#endif /* CONFIG_KEYS */
security/selinux/hooks.c +18 -3
@@ -5203,6 +5203,20 @@ static int selinux_key_permission(key_ref_t key_ref, SECCLASS_KEY, perm, NULL); } +static int selinux_key_getsecurity (struct key *key,char **_buffer) +{ +struct key_security_struct *ksec = key->security; +char *context =NULL ; +unsigned len; +int rc; + + rc =security_sid_to_context (ksec->sid, &context, &len); +if (!rc) + rc = len; + *_buffer = context; +return rc; +} +#endif static struct security_operations selinux_ops = {
@@ -5382,9 +5396,10 @@ static struct security_operations selinux_ops = {#endif #ifdef CONFIG_KEYS - .key_alloc = selinux_key_alloc, - .key_free = selinux_key_free, - .key_permission = selinux_key_permission, + .key_alloc = selinux_key_alloc, + .key_free = selinux_key_free, + .key_permission = selinux_key_permission, + .key_getsecurity = selinux_key_getsecurity,#endif };
Re: [PATCH 4/4] KEYS: Add keyctl function to get a security label
security/keys/keyctl.c error: · line 1111
Quoted 11 lines · On Fri, 08 Feb 2008 16:04:40 +0000 David Howells <[email protected]> wrote:
security/keys/keyctl.c +11
-0+ key_ref =lookup_user_key (NULL , keyid,0 ,1 ,KEY_VIEW ); +if (IS_ERR (key_ref)) { +if (PTR_ERR (key_ref) != -EACCES ) +return PTR_ERR (key_ref); + +/* viewing a key under construction is also permitted if we +* have the authorisation token handy */ + instkey =key_get_instantiation_authkey (keyid); +if (IS_ERR (instkey)) +return PTR_ERR (key_ref); +key_put (instkey);
This check looks a wee bit racy?
David Howells
maintainerRe: [PATCH 4/4] KEYS: Add keyctl function to get a security label
Quoted 5 lines · Andrew Morton
Quoted 5 lines
security/keys/keyctl.c +5
-0+ key_ref =lookup_user_key (NULL , keyid,0 ,1 ,KEY_VIEW ); +if (IS_ERR (key_ref)) { +if (PTR_ERR (key_ref) != -EACCES ) +return PTR_ERR (key_ref); +
Quoted 8 lines
security/keys/keyctl.c error: · line 1111
Quoted 6 lines
security/keys/keyctl.c +6
-0+/* viewing a key under construction is also permitted if we +* have the authorisation token handy */ + instkey =key_get_instantiation_authkey (keyid); +if (IS_ERR (instkey)) +return PTR_ERR (key_ref); +key_put (instkey);This check looks a wee bit racy?
It's not particularly racy. Admittedly there's a tiny window in which we can retrieve the security label of a key for which we had an authorisation token that got revoked just as we completed our access checks on it. Does this matter?
David