Commit 3e30148c authored by David Howells's avatar David Howells Committed by Linus Torvalds

[PATCH] Keys: Make request-key create an authorisation key

The attached patch makes the following changes:

 (1) There's a new special key type called ".request_key_auth".

     This is an authorisation key for when one process requests a key and
     another process is started to construct it. This type of key cannot be
     created by the user; nor can it be requested by kernel services.

     Authorisation keys hold two references:

     (a) Each refers to a key being constructed. When the key being
     	 constructed is instantiated the authorisation key is revoked,
     	 rendering it of no further use.

     (b) The "authorising process". This is either:

     	 (i) the process that called request_key(), or:

     	 (ii) if the process that called request_key() itself had an
     	      authorisation key in its session keyring, then the authorising
     	      process referred to by that authorisation key will also be
     	      referred to by the new authorisation key.

	 This means that the process that initiated a chain of key requests
	 will authorise the lot of them, and will, by default, wind up with
	 the keys obtained from them in its keyrings.

 (2) request_key() creates an authorisation key which is then passed to
     /sbin/request-key in as part of a new session keyring.

 (3) When request_key() is searching for a key to hand back to the caller, if
     it comes across an authorisation key in the session keyring of the
     calling process, it will also search the keyrings of the process
     specified therein and it will use the specified process's credentials
     (fsuid, fsgid, groups) to do that rather than the calling process's
     credentials.

     This allows a process started by /sbin/request-key to find keys belonging
     to the authorising process.

 (4) A key can be read, even if the process executing KEYCTL_READ doesn't have
     direct read or search permission if that key is contained within the
     keyrings of a process specified by an authorisation key found within the
     calling process's session keyring, and is searchable using the
     credentials of the authorising process.

     This allows a process started by /sbin/request-key to read keys belonging
     to the authorising process.

 (5) The magic KEY_SPEC_*_KEYRING key IDs when passed to KEYCTL_INSTANTIATE or
     KEYCTL_NEGATE will specify a keyring of the authorising process, rather
     than the process doing the instantiation.

 (6) One of the process keyrings can be nominated as the default to which
     request_key() should attach new keys if not otherwise specified. This is
     done with KEYCTL_SET_REQKEY_KEYRING and one of the KEY_REQKEY_DEFL_*
     constants. The current setting can also be read using this call.

 (7) request_key() is partially interruptible. If it is waiting for another
     process to finish constructing a key, it can be interrupted. This permits
     a request-key cycle to be broken without recourse to rebooting.
Signed-Off-By: default avatarDavid Howells <dhowells@redhat.com>
Signed-Off-By: default avatarBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 8589b4e0
...@@ -591,6 +591,37 @@ The keyctl syscall functions are: ...@@ -591,6 +591,37 @@ The keyctl syscall functions are:
this case too. this case too.
(*) Set the default request-key destination keyring.
long keyctl(KEYCTL_SET_REQKEY_KEYRING, int reqkey_defl);
This sets the default keyring to which implicitly requested keys will be
attached for this thread. reqkey_defl should be one of these constants:
CONSTANT VALUE NEW DEFAULT KEYRING
====================================== ====== =======================
KEY_REQKEY_DEFL_NO_CHANGE -1 No change
KEY_REQKEY_DEFL_DEFAULT 0 Default[1]
KEY_REQKEY_DEFL_THREAD_KEYRING 1 Thread keyring
KEY_REQKEY_DEFL_PROCESS_KEYRING 2 Process keyring
KEY_REQKEY_DEFL_SESSION_KEYRING 3 Session keyring
KEY_REQKEY_DEFL_USER_KEYRING 4 User keyring
KEY_REQKEY_DEFL_USER_SESSION_KEYRING 5 User session keyring
KEY_REQKEY_DEFL_GROUP_KEYRING 6 Group keyring
The old default will be returned if successful and error EINVAL will be
returned if reqkey_defl is not one of the above values.
The default keyring can be overridden by the keyring indicated to the
request_key() system call.
Note that this setting is inherited across fork/exec.
[1] The default default is: the thread keyring if there is one, otherwise
the process keyring if there is one, otherwise the session keyring if
there is one, otherwise the user default session keyring.
=============== ===============
KERNEL SERVICES KERNEL SERVICES
=============== ===============
...@@ -626,6 +657,9 @@ payload contents" for more information. ...@@ -626,6 +657,9 @@ payload contents" for more information.
Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be
returned. returned.
If successful, the key will have been attached to the default keyring for
implicitly obtained request-key keys, as set by KEYCTL_SET_REQKEY_KEYRING.
(*) When it is no longer required, the key should be released using: (*) When it is no longer required, the key should be released using:
......
/* key-ui.h: key userspace interface stuff for use by keyfs /* key-ui.h: key userspace interface stuff
* *
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
...@@ -84,8 +84,45 @@ static inline int key_any_permission(const struct key *key, key_perm_t perm) ...@@ -84,8 +84,45 @@ static inline int key_any_permission(const struct key *key, key_perm_t perm)
return kperm != 0; return kperm != 0;
} }
static inline int key_task_groups_search(struct task_struct *tsk, gid_t gid)
{
int ret;
task_lock(tsk);
ret = groups_search(tsk->group_info, gid);
task_unlock(tsk);
return ret;
}
static inline int key_task_permission(const struct key *key,
struct task_struct *context,
key_perm_t perm)
{
key_perm_t kperm;
if (key->uid == context->fsuid) {
kperm = key->perm >> 16;
}
else if (key->gid != -1 &&
key->perm & KEY_GRP_ALL && (
key->gid == context->fsgid ||
key_task_groups_search(context, key->gid)
)
) {
kperm = key->perm >> 8;
}
else {
kperm = key->perm;
}
kperm = kperm & perm & KEY_ALL;
return kperm == perm;
}
extern struct key *lookup_user_key(key_serial_t id, int create, int part, extern struct key *lookup_user_key(struct task_struct *context,
key_serial_t id, int create, int partial,
key_perm_t perm); key_perm_t perm);
extern long join_session_keyring(const char *name); extern long join_session_keyring(const char *name);
......
...@@ -199,10 +199,12 @@ extern int key_payload_reserve(struct key *key, size_t datalen); ...@@ -199,10 +199,12 @@ extern int key_payload_reserve(struct key *key, size_t datalen);
extern int key_instantiate_and_link(struct key *key, extern int key_instantiate_and_link(struct key *key,
const void *data, const void *data,
size_t datalen, size_t datalen,
struct key *keyring); struct key *keyring,
struct key *instkey);
extern int key_negate_and_link(struct key *key, extern int key_negate_and_link(struct key *key,
unsigned timeout, unsigned timeout,
struct key *keyring); struct key *keyring,
struct key *instkey);
extern void key_revoke(struct key *key); extern void key_revoke(struct key *key);
extern void key_put(struct key *key); extern void key_put(struct key *key);
...@@ -245,9 +247,6 @@ extern struct key *keyring_search(struct key *keyring, ...@@ -245,9 +247,6 @@ extern struct key *keyring_search(struct key *keyring,
struct key_type *type, struct key_type *type,
const char *description); const char *description);
extern struct key *search_process_keyrings(struct key_type *type,
const char *description);
extern int keyring_add_key(struct key *keyring, extern int keyring_add_key(struct key *keyring,
struct key *key); struct key *key);
......
...@@ -20,6 +20,16 @@ ...@@ -20,6 +20,16 @@
#define KEY_SPEC_USER_SESSION_KEYRING -5 /* - key ID for UID-session keyring */ #define KEY_SPEC_USER_SESSION_KEYRING -5 /* - key ID for UID-session keyring */
#define KEY_SPEC_GROUP_KEYRING -6 /* - key ID for GID-specific keyring */ #define KEY_SPEC_GROUP_KEYRING -6 /* - key ID for GID-specific keyring */
/* request-key default keyrings */
#define KEY_REQKEY_DEFL_NO_CHANGE -1
#define KEY_REQKEY_DEFL_DEFAULT 0
#define KEY_REQKEY_DEFL_THREAD_KEYRING 1
#define KEY_REQKEY_DEFL_PROCESS_KEYRING 2
#define KEY_REQKEY_DEFL_SESSION_KEYRING 3
#define KEY_REQKEY_DEFL_USER_KEYRING 4
#define KEY_REQKEY_DEFL_USER_SESSION_KEYRING 5
#define KEY_REQKEY_DEFL_GROUP_KEYRING 6
/* keyctl commands */ /* keyctl commands */
#define KEYCTL_GET_KEYRING_ID 0 /* ask for a keyring's ID */ #define KEYCTL_GET_KEYRING_ID 0 /* ask for a keyring's ID */
#define KEYCTL_JOIN_SESSION_KEYRING 1 /* join or start named session keyring */ #define KEYCTL_JOIN_SESSION_KEYRING 1 /* join or start named session keyring */
...@@ -35,5 +45,6 @@ ...@@ -35,5 +45,6 @@
#define KEYCTL_READ 11 /* read a key or keyring's contents */ #define KEYCTL_READ 11 /* read a key or keyring's contents */
#define KEYCTL_INSTANTIATE 12 /* instantiate a partially constructed key */ #define KEYCTL_INSTANTIATE 12 /* instantiate a partially constructed key */
#define KEYCTL_NEGATE 13 /* negate a partially constructed key */ #define KEYCTL_NEGATE 13 /* negate a partially constructed key */
#define KEYCTL_SET_REQKEY_KEYRING 14 /* set default request-key keyring */
#endif /* _LINUX_KEYCTL_H */ #endif /* _LINUX_KEYCTL_H */
...@@ -561,9 +561,10 @@ struct group_info { ...@@ -561,9 +561,10 @@ struct group_info {
groups_free(group_info); \ groups_free(group_info); \
} while (0) } while (0)
struct group_info *groups_alloc(int gidsetsize); extern struct group_info *groups_alloc(int gidsetsize);
void groups_free(struct group_info *group_info); extern void groups_free(struct group_info *group_info);
int set_current_groups(struct group_info *group_info); extern int set_current_groups(struct group_info *group_info);
extern int groups_search(struct group_info *group_info, gid_t grp);
/* access the groups "array" with this macro */ /* access the groups "array" with this macro */
#define GROUP_AT(gi, i) \ #define GROUP_AT(gi, i) \
((gi)->blocks[(i)/NGROUPS_PER_BLOCK][(i)%NGROUPS_PER_BLOCK]) ((gi)->blocks[(i)/NGROUPS_PER_BLOCK][(i)%NGROUPS_PER_BLOCK])
...@@ -660,6 +661,7 @@ struct task_struct { ...@@ -660,6 +661,7 @@ struct task_struct {
struct user_struct *user; struct user_struct *user;
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
struct key *thread_keyring; /* keyring private to this thread */ struct key *thread_keyring; /* keyring private to this thread */
unsigned char jit_keyring; /* default keyring to attach requested keys to */
#endif #endif
int oomkilladj; /* OOM kill score adjustment (bit shift). */ int oomkilladj; /* OOM kill score adjustment (bit shift). */
char comm[TASK_COMM_LEN]; /* executable name excluding path char comm[TASK_COMM_LEN]; /* executable name excluding path
......
...@@ -1259,7 +1259,7 @@ static void groups_sort(struct group_info *group_info) ...@@ -1259,7 +1259,7 @@ static void groups_sort(struct group_info *group_info)
} }
/* a simple bsearch */ /* a simple bsearch */
static int groups_search(struct group_info *group_info, gid_t grp) int groups_search(struct group_info *group_info, gid_t grp)
{ {
int left, right; int left, right;
......
...@@ -7,8 +7,9 @@ obj-y := \ ...@@ -7,8 +7,9 @@ obj-y := \
keyring.o \ keyring.o \
keyctl.o \ keyctl.o \
process_keys.o \ process_keys.o \
user_defined.o \ request_key.o \
request_key.o request_key_auth.o \
user_defined.o
obj-$(CONFIG_KEYS_COMPAT) += compat.o obj-$(CONFIG_KEYS_COMPAT) += compat.o
obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_PROC_FS) += proc.o
/* compat.c: 32-bit compatibility syscall for 64-bit systems /* compat.c: 32-bit compatibility syscall for 64-bit systems
* *
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -71,6 +71,9 @@ asmlinkage long compat_sys_keyctl(u32 option, ...@@ -71,6 +71,9 @@ asmlinkage long compat_sys_keyctl(u32 option,
case KEYCTL_NEGATE: case KEYCTL_NEGATE:
return keyctl_negate_key(arg2, arg3, arg4); return keyctl_negate_key(arg2, arg3, arg4);
case KEYCTL_SET_REQKEY_KEYRING:
return keyctl_set_reqkey_keyring(arg2);
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
......
/* internal.h: authentication token and access key management internal defs /* internal.h: authentication token and access key management internal defs
* *
* Copyright (C) 2003 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -15,6 +15,16 @@ ...@@ -15,6 +15,16 @@
#include <linux/key.h> #include <linux/key.h>
#include <linux/key-ui.h> #include <linux/key-ui.h>
#if 0
#define kenter(FMT, a...) printk("==> %s("FMT")\n",__FUNCTION__ , ## a)
#define kleave(FMT, a...) printk("<== %s()"FMT"\n",__FUNCTION__ , ## a)
#define kdebug(FMT, a...) printk(FMT"\n" , ## a)
#else
#define kenter(FMT, a...) do {} while(0)
#define kleave(FMT, a...) do {} while(0)
#define kdebug(FMT, a...) do {} while(0)
#endif
extern struct key_type key_type_dead; extern struct key_type key_type_dead;
extern struct key_type key_type_user; extern struct key_type key_type_user;
...@@ -66,20 +76,46 @@ extern struct key *__keyring_search_one(struct key *keyring, ...@@ -66,20 +76,46 @@ extern struct key *__keyring_search_one(struct key *keyring,
const char *description, const char *description,
key_perm_t perm); key_perm_t perm);
extern struct key *keyring_search_instkey(struct key *keyring,
key_serial_t target_id);
typedef int (*key_match_func_t)(const struct key *, const void *); typedef int (*key_match_func_t)(const struct key *, const void *);
extern struct key *keyring_search_aux(struct key *keyring, extern struct key *keyring_search_aux(struct key *keyring,
struct task_struct *tsk,
struct key_type *type, struct key_type *type,
const void *description, const void *description,
key_match_func_t match); key_match_func_t match);
extern struct key *search_process_keyrings_aux(struct key_type *type, extern struct key *search_process_keyrings(struct key_type *type,
const void *description, const void *description,
key_match_func_t match); key_match_func_t match,
struct task_struct *tsk);
extern struct key *find_keyring_by_name(const char *name, key_serial_t bound); extern struct key *find_keyring_by_name(const char *name, key_serial_t bound);
extern int install_thread_keyring(struct task_struct *tsk); extern int install_thread_keyring(struct task_struct *tsk);
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,
struct key *dest_keyring);
/*
* request_key authorisation
*/
struct request_key_auth {
struct key *target_key;
struct task_struct *context;
pid_t pid;
};
extern struct key_type key_type_request_key_auth;
extern struct key *request_key_auth_new(struct key *target,
struct key **_rkakey);
extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
/* /*
* keyctl functions * keyctl functions
...@@ -100,6 +136,7 @@ extern long keyctl_setperm_key(key_serial_t, key_perm_t); ...@@ -100,6 +136,7 @@ extern long keyctl_setperm_key(key_serial_t, key_perm_t);
extern long keyctl_instantiate_key(key_serial_t, const void __user *, extern long keyctl_instantiate_key(key_serial_t, const void __user *,
size_t, key_serial_t); size_t, key_serial_t);
extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t); extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
extern long keyctl_set_reqkey_keyring(int);
/* /*
......
/* key.c: basic authentication token and access key management /* key.c: basic authentication token and access key management
* *
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -391,7 +391,8 @@ EXPORT_SYMBOL(key_payload_reserve); ...@@ -391,7 +391,8 @@ EXPORT_SYMBOL(key_payload_reserve);
static int __key_instantiate_and_link(struct key *key, static int __key_instantiate_and_link(struct key *key,
const void *data, const void *data,
size_t datalen, size_t datalen,
struct key *keyring) struct key *keyring,
struct key *instkey)
{ {
int ret, awaken; int ret, awaken;
...@@ -419,6 +420,10 @@ static int __key_instantiate_and_link(struct key *key, ...@@ -419,6 +420,10 @@ static int __key_instantiate_and_link(struct key *key,
/* and link it into the destination keyring */ /* and link it into the destination keyring */
if (keyring) if (keyring)
ret = __key_link(keyring, key); ret = __key_link(keyring, key);
/* disable the authorisation key */
if (instkey)
key_revoke(instkey);
} }
} }
...@@ -439,19 +444,21 @@ static int __key_instantiate_and_link(struct key *key, ...@@ -439,19 +444,21 @@ static int __key_instantiate_and_link(struct key *key,
int key_instantiate_and_link(struct key *key, int key_instantiate_and_link(struct key *key,
const void *data, const void *data,
size_t datalen, size_t datalen,
struct key *keyring) struct key *keyring,
struct key *instkey)
{ {
int ret; int ret;
if (keyring) if (keyring)
down_write(&keyring->sem); down_write(&keyring->sem);
ret = __key_instantiate_and_link(key, data, datalen, keyring); ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
if (keyring) if (keyring)
up_write(&keyring->sem); up_write(&keyring->sem);
return ret; return ret;
} /* end key_instantiate_and_link() */ } /* end key_instantiate_and_link() */
EXPORT_SYMBOL(key_instantiate_and_link); EXPORT_SYMBOL(key_instantiate_and_link);
...@@ -462,7 +469,8 @@ EXPORT_SYMBOL(key_instantiate_and_link); ...@@ -462,7 +469,8 @@ EXPORT_SYMBOL(key_instantiate_and_link);
*/ */
int key_negate_and_link(struct key *key, int key_negate_and_link(struct key *key,
unsigned timeout, unsigned timeout,
struct key *keyring) struct key *keyring,
struct key *instkey)
{ {
struct timespec now; struct timespec now;
int ret, awaken; int ret, awaken;
...@@ -495,6 +503,10 @@ int key_negate_and_link(struct key *key, ...@@ -495,6 +503,10 @@ int key_negate_and_link(struct key *key,
/* and link it into the destination keyring */ /* and link it into the destination keyring */
if (keyring) if (keyring)
ret = __key_link(keyring, key); ret = __key_link(keyring, key);
/* disable the authorisation key */
if (instkey)
key_revoke(instkey);
} }
up_write(&key_construction_sem); up_write(&key_construction_sem);
...@@ -781,7 +793,7 @@ struct key *key_create_or_update(struct key *keyring, ...@@ -781,7 +793,7 @@ struct key *key_create_or_update(struct key *keyring,
} }
/* instantiate it and link it into the target keyring */ /* instantiate it and link it into the target keyring */
ret = __key_instantiate_and_link(key, payload, plen, keyring); ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
if (ret < 0) { if (ret < 0) {
key_put(key); key_put(key);
key = ERR_PTR(ret); key = ERR_PTR(ret);
......
This diff is collapsed.
/* keyring.c: keyring handling /* keyring.c: keyring handling
* *
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -308,7 +308,7 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, ...@@ -308,7 +308,7 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
uid, gid, KEY_USR_ALL, not_in_quota); uid, gid, KEY_USR_ALL, not_in_quota);
if (!IS_ERR(keyring)) { if (!IS_ERR(keyring)) {
ret = key_instantiate_and_link(keyring, NULL, 0, dest); ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
if (ret < 0) { if (ret < 0) {
key_put(keyring); key_put(keyring);
keyring = ERR_PTR(ret); keyring = ERR_PTR(ret);
...@@ -326,11 +326,12 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, ...@@ -326,11 +326,12 @@ struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
* - we only find keys on which we have search permission * - we only find keys on which we have search permission
* - we use the supplied match function to see if the description (or other * - we use the supplied match function to see if the description (or other
* feature of interest) matches * feature of interest) matches
* - we readlock the keyrings as we search down the tree * - we rely on RCU to prevent the keyring lists from disappearing on us
* - we return -EAGAIN if we didn't find any matching key * - we return -EAGAIN if we didn't find any matching key
* - we return -ENOKEY if we only found negative matching keys * - we return -ENOKEY if we only found negative matching keys
*/ */
struct key *keyring_search_aux(struct key *keyring, struct key *keyring_search_aux(struct key *keyring,
struct task_struct *context,
struct key_type *type, struct key_type *type,
const void *description, const void *description,
key_match_func_t match) key_match_func_t match)
...@@ -352,7 +353,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -352,7 +353,7 @@ struct key *keyring_search_aux(struct key *keyring,
/* top keyring must have search permission to begin the search */ /* top keyring must have search permission to begin the search */
key = ERR_PTR(-EACCES); key = ERR_PTR(-EACCES);
if (!key_permission(keyring, KEY_SEARCH)) if (!key_task_permission(keyring, context, KEY_SEARCH))
goto error; goto error;
key = ERR_PTR(-ENOTDIR); key = ERR_PTR(-ENOTDIR);
...@@ -392,7 +393,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -392,7 +393,7 @@ struct key *keyring_search_aux(struct key *keyring,
continue; continue;
/* key must have search permissions */ /* key must have search permissions */
if (!key_permission(key, KEY_SEARCH)) if (!key_task_permission(key, context, KEY_SEARCH))
continue; continue;
/* we set a different error code if we find a negative key */ /* we set a different error code if we find a negative key */
...@@ -418,7 +419,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -418,7 +419,7 @@ struct key *keyring_search_aux(struct key *keyring,
if (sp >= KEYRING_SEARCH_MAX_DEPTH) if (sp >= KEYRING_SEARCH_MAX_DEPTH)
continue; continue;
if (!key_permission(key, KEY_SEARCH)) if (!key_task_permission(key, context, KEY_SEARCH))
continue; continue;
/* stack the current position */ /* stack the current position */
...@@ -468,7 +469,11 @@ struct key *keyring_search(struct key *keyring, ...@@ -468,7 +469,11 @@ struct key *keyring_search(struct key *keyring,
struct key_type *type, struct key_type *type,
const char *description) const char *description)
{ {
return keyring_search_aux(keyring, type, description, type->match); if (!type->match)
return ERR_PTR(-ENOKEY);
return keyring_search_aux(keyring, current,
type, description, type->match);
} /* end keyring_search() */ } /* end keyring_search() */
...@@ -496,7 +501,8 @@ struct key *__keyring_search_one(struct key *keyring, ...@@ -496,7 +501,8 @@ struct key *__keyring_search_one(struct key *keyring,
key = klist->keys[loop]; key = klist->keys[loop];
if (key->type == ktype && if (key->type == ktype &&
key->type->match(key, description) && (!key->type->match ||
key->type->match(key, description)) &&
key_permission(key, perm) && key_permission(key, perm) &&
!test_bit(KEY_FLAG_REVOKED, &key->flags) !test_bit(KEY_FLAG_REVOKED, &key->flags)
) )
...@@ -515,6 +521,51 @@ struct key *__keyring_search_one(struct key *keyring, ...@@ -515,6 +521,51 @@ struct key *__keyring_search_one(struct key *keyring,
} /* end __keyring_search_one() */ } /* end __keyring_search_one() */
/*****************************************************************************/
/*
* search for an instantiation authorisation key matching a target key
* - the RCU read lock must be held by the caller
* - a target_id of zero specifies any valid token
*/
struct key *keyring_search_instkey(struct key *keyring,
key_serial_t target_id)
{
struct request_key_auth *rka;
struct keyring_list *klist;
struct key *instkey;
int loop;
klist = rcu_dereference(keyring->payload.subscriptions);
if (klist) {
for (loop = 0; loop < klist->nkeys; loop++) {
instkey = klist->keys[loop];
if (instkey->type != &key_type_request_key_auth)
continue;
rka = instkey->payload.data;
if (target_id && rka->target_key->serial != target_id)
continue;
/* the auth key is revoked during instantiation */
if (!test_bit(KEY_FLAG_REVOKED, &instkey->flags))
goto found;
instkey = ERR_PTR(-EKEYREVOKED);
goto error;
}
}
instkey = ERR_PTR(-EACCES);
goto error;
found:
atomic_inc(&instkey->usage);
error:
return instkey;
} /* end keyring_search_instkey() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* find a keyring with the specified name * find a keyring with the specified name
......
...@@ -165,7 +165,7 @@ int install_thread_keyring(struct task_struct *tsk) ...@@ -165,7 +165,7 @@ int install_thread_keyring(struct task_struct *tsk)
/* /*
* make sure a process keyring is installed * make sure a process keyring is installed
*/ */
static int install_process_keyring(struct task_struct *tsk) int install_process_keyring(struct task_struct *tsk)
{ {
unsigned long flags; unsigned long flags;
struct key *keyring; struct key *keyring;
...@@ -376,12 +376,13 @@ void key_fsgid_changed(struct task_struct *tsk) ...@@ -376,12 +376,13 @@ void key_fsgid_changed(struct task_struct *tsk)
* - we return -EAGAIN if we didn't find any matching key * - we return -EAGAIN if we didn't find any matching key
* - we return -ENOKEY if we found only negative matching keys * - we return -ENOKEY if we found only negative matching keys
*/ */
struct key *search_process_keyrings_aux(struct key_type *type, struct key *search_process_keyrings(struct key_type *type,
const void *description, const void *description,
key_match_func_t match) key_match_func_t match,
struct task_struct *context)
{ {
struct task_struct *tsk = current; struct request_key_auth *rka;
struct key *key, *ret, *err; struct key *key, *ret, *err, *instkey;
/* we want to return -EAGAIN or -ENOKEY if any of the keyrings were /* 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; * searchable, but we failed to find a key or we found a negative key;
...@@ -395,9 +396,9 @@ struct key *search_process_keyrings_aux(struct key_type *type, ...@@ -395,9 +396,9 @@ struct key *search_process_keyrings_aux(struct key_type *type,
err = ERR_PTR(-EAGAIN); err = ERR_PTR(-EAGAIN);
/* search the thread keyring first */ /* search the thread keyring first */
if (tsk->thread_keyring) { if (context->thread_keyring) {
key = keyring_search_aux(tsk->thread_keyring, type, key = keyring_search_aux(context->thread_keyring,
description, match); context, type, description, match);
if (!IS_ERR(key)) if (!IS_ERR(key))
goto found; goto found;
...@@ -415,9 +416,9 @@ struct key *search_process_keyrings_aux(struct key_type *type, ...@@ -415,9 +416,9 @@ struct key *search_process_keyrings_aux(struct key_type *type,
} }
/* search the process keyring second */ /* search the process keyring second */
if (tsk->signal->process_keyring) { if (context->signal->process_keyring) {
key = keyring_search_aux(tsk->signal->process_keyring, key = keyring_search_aux(context->signal->process_keyring,
type, description, match); context, type, description, match);
if (!IS_ERR(key)) if (!IS_ERR(key))
goto found; goto found;
...@@ -434,19 +435,69 @@ struct key *search_process_keyrings_aux(struct key_type *type, ...@@ -434,19 +435,69 @@ struct key *search_process_keyrings_aux(struct key_type *type,
} }
} }
/* search the session keyring last */ /* search the session keyring */
if (tsk->signal->session_keyring) { if (context->signal->session_keyring) {
rcu_read_lock(); rcu_read_lock();
key = keyring_search_aux( key = keyring_search_aux(
rcu_dereference(tsk->signal->session_keyring), rcu_dereference(context->signal->session_keyring),
type, description, match); context, type, description, match);
rcu_read_unlock(); rcu_read_unlock();
if (!IS_ERR(key))
goto found;
switch (PTR_ERR(key)) {
case -EAGAIN: /* no key */
if (ret)
break;
case -ENOKEY: /* negative key */
ret = key;
break;
default:
err = key;
break;
} }
else {
key = keyring_search_aux(tsk->user->session_keyring,
type, description, match);
}
/* if this process has a session keyring and that has an
* instantiation authorisation key in the bottom level, then we
* also search the keyrings of the process mentioned there */
if (context != current)
goto no_key;
rcu_read_lock();
instkey = __keyring_search_one(
rcu_dereference(context->signal->session_keyring),
&key_type_request_key_auth, NULL, 0);
rcu_read_unlock();
if (IS_ERR(instkey))
goto no_key;
rka = instkey->payload.data;
key = search_process_keyrings(type, description, match,
rka->context);
key_put(instkey);
if (!IS_ERR(key))
goto found;
switch (PTR_ERR(key)) {
case -EAGAIN: /* no key */
if (ret)
break;
case -ENOKEY: /* negative key */
ret = key;
break;
default:
err = key;
break;
}
}
/* or search the user-session keyring */
else {
key = keyring_search_aux(context->user->session_keyring,
context, type, description, match);
if (!IS_ERR(key)) if (!IS_ERR(key))
goto found; goto found;
...@@ -461,26 +512,16 @@ struct key *search_process_keyrings_aux(struct key_type *type, ...@@ -461,26 +512,16 @@ struct key *search_process_keyrings_aux(struct key_type *type,
err = key; err = key;
break; break;
} }
}
no_key:
/* no key - decide on the error we're going to go for */ /* no key - decide on the error we're going to go for */
key = ret ? ret : err; key = ret ? ret : err;
found: found:
return key; return key;
} /* end search_process_keyrings_aux() */
/*****************************************************************************/
/*
* search the process keyrings for the first matching key
* - we return -EAGAIN if we didn't find any matching key
* - we return -ENOKEY if we found only negative matching keys
*/
struct key *search_process_keyrings(struct key_type *type,
const char *description)
{
return search_process_keyrings_aux(type, description, type->match);
} /* end search_process_keyrings() */ } /* end search_process_keyrings() */
/*****************************************************************************/ /*****************************************************************************/
...@@ -489,72 +530,73 @@ struct key *search_process_keyrings(struct key_type *type, ...@@ -489,72 +530,73 @@ struct key *search_process_keyrings(struct key_type *type,
* - don't create special keyrings unless so requested * - don't create special keyrings unless so requested
* - partially constructed keys aren't found unless requested * - partially constructed keys aren't found unless requested
*/ */
struct key *lookup_user_key(key_serial_t id, int create, int partial, struct key *lookup_user_key(struct task_struct *context, key_serial_t id,
key_perm_t perm) int create, int partial, key_perm_t perm)
{ {
struct task_struct *tsk = current;
unsigned long flags;
struct key *key; struct key *key;
int ret; int ret;
if (!context)
context = current;
key = ERR_PTR(-ENOKEY); key = ERR_PTR(-ENOKEY);
switch (id) { switch (id) {
case KEY_SPEC_THREAD_KEYRING: case KEY_SPEC_THREAD_KEYRING:
if (!tsk->thread_keyring) { if (!context->thread_keyring) {
if (!create) if (!create)
goto error; goto error;
ret = install_thread_keyring(tsk); ret = install_thread_keyring(context);
if (ret < 0) { if (ret < 0) {
key = ERR_PTR(ret); key = ERR_PTR(ret);
goto error; goto error;
} }
} }
key = tsk->thread_keyring; key = context->thread_keyring;
atomic_inc(&key->usage); atomic_inc(&key->usage);
break; break;
case KEY_SPEC_PROCESS_KEYRING: case KEY_SPEC_PROCESS_KEYRING:
if (!tsk->signal->process_keyring) { if (!context->signal->process_keyring) {
if (!create) if (!create)
goto error; goto error;
ret = install_process_keyring(tsk); ret = install_process_keyring(context);
if (ret < 0) { if (ret < 0) {
key = ERR_PTR(ret); key = ERR_PTR(ret);
goto error; goto error;
} }
} }
key = tsk->signal->process_keyring; key = context->signal->process_keyring;
atomic_inc(&key->usage); atomic_inc(&key->usage);
break; break;
case KEY_SPEC_SESSION_KEYRING: case KEY_SPEC_SESSION_KEYRING:
if (!tsk->signal->session_keyring) { if (!context->signal->session_keyring) {
/* always install a session keyring upon access if one /* always install a session keyring upon access if one
* doesn't exist yet */ * doesn't exist yet */
ret = install_session_keyring( ret = install_session_keyring(
tsk, tsk->user->session_keyring); context, context->user->session_keyring);
if (ret < 0) if (ret < 0)
goto error; goto error;
} }
spin_lock_irqsave(&tsk->sighand->siglock, flags); rcu_read_lock();
key = tsk->signal->session_keyring; key = rcu_dereference(context->signal->session_keyring);
atomic_inc(&key->usage); atomic_inc(&key->usage);
spin_unlock_irqrestore(&tsk->sighand->siglock, flags); rcu_read_unlock();
break; break;
case KEY_SPEC_USER_KEYRING: case KEY_SPEC_USER_KEYRING:
key = tsk->user->uid_keyring; key = context->user->uid_keyring;
atomic_inc(&key->usage); atomic_inc(&key->usage);
break; break;
case KEY_SPEC_USER_SESSION_KEYRING: case KEY_SPEC_USER_SESSION_KEYRING:
key = tsk->user->session_keyring; key = context->user->session_keyring;
atomic_inc(&key->usage); atomic_inc(&key->usage);
break; break;
...@@ -574,7 +616,7 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial, ...@@ -574,7 +616,7 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial,
break; break;
} }
/* check the status and permissions */ /* check the status */
if (perm) { if (perm) {
ret = key_validate(key); ret = key_validate(key);
if (ret < 0) if (ret < 0)
...@@ -585,8 +627,10 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial, ...@@ -585,8 +627,10 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial,
if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
goto invalid_key; goto invalid_key;
/* check the permissions */
ret = -EACCES; ret = -EACCES;
if (!key_permission(key, perm))
if (!key_task_permission(key, context, perm))
goto invalid_key; goto invalid_key;
error: error:
...@@ -609,7 +653,6 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial, ...@@ -609,7 +653,6 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial,
long join_session_keyring(const char *name) long join_session_keyring(const char *name)
{ {
struct task_struct *tsk = current; struct task_struct *tsk = current;
unsigned long flags;
struct key *keyring; struct key *keyring;
long ret; long ret;
...@@ -619,9 +662,9 @@ long join_session_keyring(const char *name) ...@@ -619,9 +662,9 @@ long join_session_keyring(const char *name)
if (ret < 0) if (ret < 0)
goto error; goto error;
spin_lock_irqsave(&tsk->sighand->siglock, flags); rcu_read_lock();
ret = tsk->signal->session_keyring->serial; ret = rcu_dereference(tsk->signal->session_keyring)->serial;
spin_unlock_irqrestore(&tsk->sighand->siglock, flags); rcu_read_unlock();
goto error; goto error;
} }
......
/* request_key.c: request a key from userspace /* request_key.c: request a key from userspace
* *
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com) * Written by David Howells (dhowells@redhat.com)
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/kmod.h> #include <linux/kmod.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/keyctl.h>
#include "internal.h" #include "internal.h"
struct key_construction { struct key_construction {
...@@ -27,18 +28,26 @@ DECLARE_WAIT_QUEUE_HEAD(request_key_conswq); ...@@ -27,18 +28,26 @@ DECLARE_WAIT_QUEUE_HEAD(request_key_conswq);
/* /*
* request userspace finish the construction of a key * request userspace finish the construction of a key
* - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring> <info>" * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring> <info>"
* - if callout_info is an empty string, it'll be rendered as a "-" instead
*/ */
static int call_request_key(struct key *key, static int call_request_key(struct key *key,
const char *op, const char *op,
const char *callout_info) const char *callout_info)
{ {
struct task_struct *tsk = current; struct task_struct *tsk = current;
unsigned long flags;
key_serial_t prkey, sskey; key_serial_t prkey, sskey;
struct key *session_keyring, *rkakey;
char *argv[10], *envp[3], uid_str[12], gid_str[12]; char *argv[10], *envp[3], uid_str[12], gid_str[12];
char key_str[12], keyring_str[3][12]; char key_str[12], keyring_str[3][12];
int i; int ret, i;
kenter("{%d},%s,%s", key->serial, op, callout_info);
/* generate a new session keyring with an auth key in it */
session_keyring = request_key_auth_new(key, &rkakey);
if (IS_ERR(session_keyring)) {
ret = PTR_ERR(session_keyring);
goto error;
}
/* record the UID and GID */ /* record the UID and GID */
sprintf(uid_str, "%d", current->fsuid); sprintf(uid_str, "%d", current->fsuid);
...@@ -55,17 +64,17 @@ static int call_request_key(struct key *key, ...@@ -55,17 +64,17 @@ static int call_request_key(struct key *key,
if (tsk->signal->process_keyring) if (tsk->signal->process_keyring)
prkey = tsk->signal->process_keyring->serial; prkey = tsk->signal->process_keyring->serial;
sskey = 0; sprintf(keyring_str[1], "%d", prkey);
spin_lock_irqsave(&tsk->sighand->siglock, flags);
if (tsk->signal->session_keyring)
sskey = tsk->signal->session_keyring->serial;
spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
if (!sskey) if (tsk->signal->session_keyring) {
rcu_read_lock();
sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
rcu_read_unlock();
}
else {
sskey = tsk->user->session_keyring->serial; sskey = tsk->user->session_keyring->serial;
}
sprintf(keyring_str[1], "%d", prkey);
sprintf(keyring_str[2], "%d", sskey); sprintf(keyring_str[2], "%d", sskey);
/* set up a minimal environment */ /* set up a minimal environment */
...@@ -84,11 +93,20 @@ static int call_request_key(struct key *key, ...@@ -84,11 +93,20 @@ static int call_request_key(struct key *key,
argv[i++] = keyring_str[0]; argv[i++] = keyring_str[0];
argv[i++] = keyring_str[1]; argv[i++] = keyring_str[1];
argv[i++] = keyring_str[2]; argv[i++] = keyring_str[2];
argv[i++] = callout_info[0] ? (char *) callout_info : "-"; argv[i++] = (char *) callout_info;
argv[i] = NULL; argv[i] = NULL;
/* do it */ /* do it */
return call_usermodehelper_keys(argv[0], argv, envp, NULL, 1); ret = call_usermodehelper_keys(argv[0], argv, envp, session_keyring, 1);
/* dispose of the special keys */
key_revoke(rkakey);
key_put(rkakey);
key_put(session_keyring);
error:
kleave(" = %d", ret);
return ret;
} /* end call_request_key() */ } /* end call_request_key() */
...@@ -107,6 +125,8 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -107,6 +125,8 @@ static struct key *__request_key_construction(struct key_type *type,
struct key *key; struct key *key;
int ret, negated; int ret, negated;
kenter("%s,%s,%s", type->name, description, callout_info);
/* create a key and add it to the queue */ /* create a key and add it to the queue */
key = key_alloc(type, description, key = key_alloc(type, description,
current->fsuid, current->fsgid, KEY_USR_ALL, 0); current->fsuid, current->fsgid, KEY_USR_ALL, 0);
...@@ -143,6 +163,7 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -143,6 +163,7 @@ static struct key *__request_key_construction(struct key_type *type,
} }
out: out:
kleave(" = %p", key);
return key; return key;
request_failed: request_failed:
...@@ -216,6 +237,9 @@ static struct key *request_key_construction(struct key_type *type, ...@@ -216,6 +237,9 @@ static struct key *request_key_construction(struct key_type *type,
DECLARE_WAITQUEUE(myself, current); DECLARE_WAITQUEUE(myself, current);
kenter("%s,%s,{%d},%s",
type->name, description, user->uid, callout_info);
/* see if there's such a key under construction already */ /* see if there's such a key under construction already */
down_write(&key_construction_sem); down_write(&key_construction_sem);
...@@ -232,6 +256,7 @@ static struct key *request_key_construction(struct key_type *type, ...@@ -232,6 +256,7 @@ static struct key *request_key_construction(struct key_type *type,
/* see about getting userspace to construct the key */ /* see about getting userspace to construct the key */
key = __request_key_construction(type, description, callout_info); key = __request_key_construction(type, description, callout_info);
error: error:
kleave(" = %p", key);
return key; return key;
/* someone else has the same key under construction /* someone else has the same key under construction
...@@ -245,9 +270,11 @@ static struct key *request_key_construction(struct key_type *type, ...@@ -245,9 +270,11 @@ static struct key *request_key_construction(struct key_type *type,
add_wait_queue(&request_key_conswq, &myself); add_wait_queue(&request_key_conswq, &myself);
for (;;) { for (;;) {
set_current_state(TASK_UNINTERRUPTIBLE); set_current_state(TASK_INTERRUPTIBLE);
if (!test_bit(KEY_FLAG_USER_CONSTRUCT, &ckey->flags)) if (!test_bit(KEY_FLAG_USER_CONSTRUCT, &ckey->flags))
break; break;
if (signal_pending(current))
break;
schedule(); schedule();
} }
...@@ -265,23 +292,85 @@ static struct key *request_key_construction(struct key_type *type, ...@@ -265,23 +292,85 @@ static struct key *request_key_construction(struct key_type *type,
} /* end request_key_construction() */ } /* end request_key_construction() */
/*****************************************************************************/
/*
* link a freshly minted key to an appropriate destination keyring
*/
static void request_key_link(struct key *key, struct key *dest_keyring)
{
struct task_struct *tsk = current;
struct key *drop = NULL;
kenter("{%d},%p", key->serial, dest_keyring);
/* find the appropriate keyring */
if (!dest_keyring) {
switch (tsk->jit_keyring) {
case KEY_REQKEY_DEFL_DEFAULT:
case KEY_REQKEY_DEFL_THREAD_KEYRING:
dest_keyring = tsk->thread_keyring;
if (dest_keyring)
break;
case KEY_REQKEY_DEFL_PROCESS_KEYRING:
dest_keyring = tsk->signal->process_keyring;
if (dest_keyring)
break;
case KEY_REQKEY_DEFL_SESSION_KEYRING:
rcu_read_lock();
dest_keyring = key_get(
rcu_dereference(tsk->signal->session_keyring));
rcu_read_unlock();
drop = dest_keyring;
if (dest_keyring)
break;
case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
dest_keyring = current->user->session_keyring;
break;
case KEY_REQKEY_DEFL_USER_KEYRING:
dest_keyring = current->user->uid_keyring;
break;
case KEY_REQKEY_DEFL_GROUP_KEYRING:
default:
BUG();
}
}
/* and attach the key to it */
key_link(dest_keyring, key);
key_put(drop);
kleave("");
} /* end request_key_link() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* request a key * request a key
* - search the process's keyrings * - search the process's keyrings
* - check the list of keys being created or updated * - check the list of keys being created or updated
* - call out to userspace for a key if requested (supplementary info can be * - call out to userspace for a key if supplementary info was provided
* passed) * - cache the key in an appropriate keyring
*/ */
struct key *request_key(struct key_type *type, struct key *request_key_and_link(struct key_type *type,
const char *description, const char *description,
const char *callout_info) const char *callout_info,
struct key *dest_keyring)
{ {
struct key_user *user; struct key_user *user;
struct key *key; struct key *key;
kenter("%s,%s,%s,%p",
type->name, description, callout_info, dest_keyring);
/* search all the process keyrings for a key */ /* search all the process keyrings for a key */
key = search_process_keyrings_aux(type, description, type->match); key = search_process_keyrings(type, description, type->match, current);
if (PTR_ERR(key) == -EAGAIN) { if (PTR_ERR(key) == -EAGAIN) {
/* the search failed, but the keyrings were searchable, so we /* the search failed, but the keyrings were searchable, so we
...@@ -292,12 +381,13 @@ struct key *request_key(struct key_type *type, ...@@ -292,12 +381,13 @@ struct key *request_key(struct key_type *type,
/* - get hold of the user's construction queue */ /* - get hold of the user's construction queue */
user = key_user_lookup(current->fsuid); user = key_user_lookup(current->fsuid);
if (!user) { if (!user)
key = ERR_PTR(-ENOMEM); goto nomem;
goto error;
} do {
if (signal_pending(current))
goto interrupted;
for (;;) {
/* ask userspace (returns NULL if it waited on a key /* ask userspace (returns NULL if it waited on a key
* being constructed) */ * being constructed) */
key = request_key_construction(type, description, key = request_key_construction(type, description,
...@@ -307,18 +397,46 @@ struct key *request_key(struct key_type *type, ...@@ -307,18 +397,46 @@ struct key *request_key(struct key_type *type,
/* someone else made the key we want, so we need to /* someone else made the key we want, so we need to
* search again as it might now be available to us */ * search again as it might now be available to us */
key = search_process_keyrings_aux(type, description, key = search_process_keyrings(type, description,
type->match); type->match, current);
if (PTR_ERR(key) != -EAGAIN)
break; } while (PTR_ERR(key) == -EAGAIN);
}
key_user_put(user); key_user_put(user);
/* link the new key into the appropriate keyring */
if (!PTR_ERR(key))
request_key_link(key, dest_keyring);
} }
error: error:
kleave(" = %p", key);
return key; return key;
nomem:
key = ERR_PTR(-ENOMEM);
goto error;
interrupted:
key_user_put(user);
key = ERR_PTR(-EINTR);
goto error;
} /* end request_key_and_link() */
/*****************************************************************************/
/*
* request a key
* - search the process's keyrings
* - check the list of keys being created or updated
* - call out to userspace for a key if supplementary info was provided
*/
struct key *request_key(struct key_type *type,
const char *description,
const char *callout_info)
{
return request_key_and_link(type, description, callout_info, NULL);
} /* end request_key() */ } /* end request_key() */
EXPORT_SYMBOL(request_key); EXPORT_SYMBOL(request_key);
......
/* request_key_auth.c: request key authorisation controlling key def
*
* Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/err.h>
#include <linux/seq_file.h>
#include "internal.h"
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_destroy(struct key *);
/*
* the request-key authorisation key type definition
*/
struct key_type key_type_request_key_auth = {
.name = ".request_key_auth",
.def_datalen = sizeof(struct request_key_auth),
.instantiate = request_key_auth_instantiate,
.describe = request_key_auth_describe,
.destroy = request_key_auth_destroy,
};
/*****************************************************************************/
/*
* instantiate a request-key authorisation record
*/
static int request_key_auth_instantiate(struct key *key,
const void *data,
size_t datalen)
{
struct request_key_auth *rka, *irka;
struct key *instkey;
int ret;
ret = -ENOMEM;
rka = kmalloc(sizeof(*rka), GFP_KERNEL);
if (rka) {
/* see if the calling process is already servicing the key
* request of another process */
instkey = key_get_instantiation_authkey(0);
if (!IS_ERR(instkey)) {
/* it is - use that instantiation context here too */
irka = instkey->payload.data;
rka->context = irka->context;
rka->pid = irka->pid;
key_put(instkey);
}
else {
/* it isn't - use this process as the context */
rka->context = current;
rka->pid = current->pid;
}
rka->target_key = key_get((struct key *) data);
key->payload.data = rka;
ret = 0;
}
return ret;
} /* end request_key_auth_instantiate() */
/*****************************************************************************/
/*
*
*/
static void request_key_auth_describe(const struct key *key,
struct seq_file *m)
{
struct request_key_auth *rka = key->payload.data;
seq_puts(m, "key:");
seq_puts(m, key->description);
seq_printf(m, " pid:%d", rka->pid);
} /* end request_key_auth_describe() */
/*****************************************************************************/
/*
* destroy an instantiation authorisation token key
*/
static void request_key_auth_destroy(struct key *key)
{
struct request_key_auth *rka = key->payload.data;
kenter("{%d}", key->serial);
key_put(rka->target_key);
} /* end request_key_auth_destroy() */
/*****************************************************************************/
/*
* create a session keyring to be for the invokation of /sbin/request-key and
* stick an authorisation token in it
*/
struct key *request_key_auth_new(struct key *target, struct key **_rkakey)
{
struct key *keyring, *rkakey = NULL;
char desc[20];
int ret;
kenter("%d,", target->serial);
/* allocate a new session keyring */
sprintf(desc, "_req.%u", target->serial);
keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
if (IS_ERR(keyring)) {
kleave("= %ld", PTR_ERR(keyring));
return keyring;
}
/* allocate the auth key */
sprintf(desc, "%x", target->serial);
rkakey = key_alloc(&key_type_request_key_auth, desc,
current->fsuid, current->fsgid,
KEY_USR_VIEW, 1);
if (IS_ERR(rkakey)) {
key_put(keyring);
kleave("= %ld", PTR_ERR(rkakey));
return rkakey;
}
/* construct and attach to the keyring */
ret = key_instantiate_and_link(rkakey, target, 0, keyring, NULL);
if (ret < 0) {
key_revoke(rkakey);
key_put(rkakey);
key_put(keyring);
kleave("= %d", ret);
return ERR_PTR(ret);
}
*_rkakey = rkakey;
kleave(" = {%d} ({%d})", keyring->serial, rkakey->serial);
return keyring;
} /* end request_key_auth_new() */
/*****************************************************************************/
/*
* get the authorisation key for instantiation of a specific key if attached to
* the current process's keyrings
* - this key is inserted into a keyring and that is set as /sbin/request-key's
* session keyring
* - a target_id of zero specifies any valid token
*/
struct key *key_get_instantiation_authkey(key_serial_t target_id)
{
struct task_struct *tsk = current;
struct key *instkey;
/* we must have our own personal session keyring */
if (!tsk->signal->session_keyring)
return ERR_PTR(-EACCES);
/* and it must contain a suitable request authorisation key
* - lock RCU against session keyring changing
*/
rcu_read_lock();
instkey = keyring_search_instkey(
rcu_dereference(tsk->signal->session_keyring), target_id);
rcu_read_unlock();
return instkey;
} /* end key_get_instantiation_authkey() */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment