Skip to content

Commit

Permalink
[PATCH] Keys: Make request-key create an authorisation key
Browse files Browse the repository at this point in the history
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: David Howells <[email protected]>
Signed-Off-By: Benoit Boissinot <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
dhowells authored and Linus Torvalds committed Jun 24, 2005
1 parent 8589b4e commit 3e30148
Show file tree
Hide file tree
Showing 15 changed files with 779 additions and 191 deletions.
34 changes: 34 additions & 0 deletions Documentation/keys.txt
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,37 @@ The keyctl syscall functions are:
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
===============
Expand Down Expand Up @@ -626,6 +657,9 @@ payload contents" for more information.
Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be
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:

Expand Down
41 changes: 39 additions & 2 deletions include/linux/key-ui.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* 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.
* Written by David Howells ([email protected])
Expand Down Expand Up @@ -84,8 +84,45 @@ static inline int key_any_permission(const struct key *key, key_perm_t perm)
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);

extern long join_session_keyring(const char *name);
Expand Down
9 changes: 4 additions & 5 deletions include/linux/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ extern int key_payload_reserve(struct key *key, size_t datalen);
extern int key_instantiate_and_link(struct key *key,
const void *data,
size_t datalen,
struct key *keyring);
struct key *keyring,
struct key *instkey);
extern int key_negate_and_link(struct key *key,
unsigned timeout,
struct key *keyring);
struct key *keyring,
struct key *instkey);
extern void key_revoke(struct key *key);
extern void key_put(struct key *key);

Expand Down Expand Up @@ -245,9 +247,6 @@ extern struct key *keyring_search(struct key *keyring,
struct key_type *type,
const char *description);

extern struct key *search_process_keyrings(struct key_type *type,
const char *description);

extern int keyring_add_key(struct key *keyring,
struct key *key);

Expand Down
11 changes: 11 additions & 0 deletions include/linux/keyctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#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 */

/* 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 */
#define KEYCTL_GET_KEYRING_ID 0 /* ask for a keyring's ID */
#define KEYCTL_JOIN_SESSION_KEYRING 1 /* join or start named session keyring */
Expand All @@ -35,5 +45,6 @@
#define KEYCTL_READ 11 /* read a key or keyring's contents */
#define KEYCTL_INSTANTIATE 12 /* instantiate 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 */
8 changes: 5 additions & 3 deletions include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,10 @@ struct group_info {
groups_free(group_info); \
} while (0)

struct group_info *groups_alloc(int gidsetsize);
void groups_free(struct group_info *group_info);
int set_current_groups(struct group_info *group_info);
extern struct group_info *groups_alloc(int gidsetsize);
extern void groups_free(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 */
#define GROUP_AT(gi, i) \
((gi)->blocks[(i)/NGROUPS_PER_BLOCK][(i)%NGROUPS_PER_BLOCK])
Expand Down Expand Up @@ -660,6 +661,7 @@ struct task_struct {
struct user_struct *user;
#ifdef CONFIG_KEYS
struct key *thread_keyring; /* keyring private to this thread */
unsigned char jit_keyring; /* default keyring to attach requested keys to */
#endif
int oomkilladj; /* OOM kill score adjustment (bit shift). */
char comm[TASK_COMM_LEN]; /* executable name excluding path
Expand Down
2 changes: 1 addition & 1 deletion kernel/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ static void groups_sort(struct group_info *group_info)
}

/* 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;

Expand Down
5 changes: 3 additions & 2 deletions security/keys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ obj-y := \
keyring.o \
keyctl.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_PROC_FS) += proc.o
7 changes: 5 additions & 2 deletions security/keys/compat.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* 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 ([email protected])
*
* This program is free software; you can redistribute it and/or
Expand All @@ -24,7 +24,7 @@
* - if you can, you should call sys_keyctl directly
*/
asmlinkage long compat_sys_keyctl(u32 option,
u32 arg2, u32 arg3, u32 arg4, u32 arg5)
u32 arg2, u32 arg3, u32 arg4, u32 arg5)
{
switch (option) {
case KEYCTL_GET_KEYRING_ID:
Expand Down Expand Up @@ -71,6 +71,9 @@ asmlinkage long compat_sys_keyctl(u32 option,
case KEYCTL_NEGATE:
return keyctl_negate_key(arg2, arg3, arg4);

case KEYCTL_SET_REQKEY_KEYRING:
return keyctl_set_reqkey_keyring(arg2);

default:
return -EOPNOTSUPP;
}
Expand Down
45 changes: 41 additions & 4 deletions security/keys/internal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* 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 ([email protected])
*
* This program is free software; you can redistribute it and/or
Expand All @@ -15,6 +15,16 @@
#include <linux/key.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_user;

Expand Down Expand Up @@ -66,20 +76,46 @@ extern struct key *__keyring_search_one(struct key *keyring,
const char *description,
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 *);

extern struct key *keyring_search_aux(struct key *keyring,
struct task_struct *tsk,
struct key_type *type,
const void *description,
key_match_func_t match);

extern struct key *search_process_keyrings_aux(struct key_type *type,
const void *description,
key_match_func_t match);
extern struct key *search_process_keyrings(struct key_type *type,
const void *description,
key_match_func_t match,
struct task_struct *tsk);

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_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
Expand All @@ -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 *,
size_t, key_serial_t);
extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
extern long keyctl_set_reqkey_keyring(int);


/*
Expand Down
24 changes: 18 additions & 6 deletions security/keys/key.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* 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 ([email protected])
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -391,7 +391,8 @@ EXPORT_SYMBOL(key_payload_reserve);
static int __key_instantiate_and_link(struct key *key,
const void *data,
size_t datalen,
struct key *keyring)
struct key *keyring,
struct key *instkey)
{
int ret, awaken;

Expand Down Expand Up @@ -419,6 +420,10 @@ static int __key_instantiate_and_link(struct key *key,
/* and link it into the destination keyring */
if (keyring)
ret = __key_link(keyring, key);

/* disable the authorisation key */
if (instkey)
key_revoke(instkey);
}
}

Expand All @@ -439,19 +444,21 @@ static int __key_instantiate_and_link(struct key *key,
int key_instantiate_and_link(struct key *key,
const void *data,
size_t datalen,
struct key *keyring)
struct key *keyring,
struct key *instkey)
{
int ret;

if (keyring)
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)
up_write(&keyring->sem);

return ret;

} /* end key_instantiate_and_link() */

EXPORT_SYMBOL(key_instantiate_and_link);
Expand All @@ -462,7 +469,8 @@ EXPORT_SYMBOL(key_instantiate_and_link);
*/
int key_negate_and_link(struct key *key,
unsigned timeout,
struct key *keyring)
struct key *keyring,
struct key *instkey)
{
struct timespec now;
int ret, awaken;
Expand Down Expand Up @@ -495,6 +503,10 @@ int key_negate_and_link(struct key *key,
/* and link it into the destination keyring */
if (keyring)
ret = __key_link(keyring, key);

/* disable the authorisation key */
if (instkey)
key_revoke(instkey);
}

up_write(&key_construction_sem);
Expand Down Expand Up @@ -781,7 +793,7 @@ struct key *key_create_or_update(struct key *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) {
key_put(key);
key = ERR_PTR(ret);
Expand Down
Loading

0 comments on commit 3e30148

Please sign in to comment.