Skip to content

Commit

Permalink
Merge branch 'security-next-keys' of git://git.kernel.org/pub/scm/lin…
Browse files Browse the repository at this point in the history
…ux/kernel/git/dhowells/security-keys into next-queue

As requested by David.
  • Loading branch information
James Morris committed Oct 3, 2012
2 parents 87b526d + 4442d77 commit 61d335d
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 330 deletions.
67 changes: 66 additions & 1 deletion Documentation/security/keys.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ The main syscalls are:
to the keyring. In this case, an error will be generated if the process
does not have permission to write to the keyring.

If the key type supports it, if the description is NULL or an empty
string, the key type will try and generate a description from the content
of the payload.

The payload is optional, and the pointer can be NULL if not required by
the type. The payload is plen in size, and plen can be zero for an empty
payload.
Expand Down Expand Up @@ -990,6 +994,23 @@ payload contents" for more information.
reference pointer if successful.


(*) A keyring can be created by:

struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
const struct cred *cred,
key_perm_t perm,
unsigned long flags,
struct key *dest);

This creates a keyring with the given attributes and returns it. If dest
is not NULL, the new keyring will be linked into the keyring to which it
points. No permission checks are made upon the destination keyring.

Error EDQUOT can be returned if the keyring would overload the quota (pass
KEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accounted
towards the user's quota). Error ENOMEM can also be returned.


(*) To check the validity of a key, this function can be called:

int validate_key(struct key *key);
Expand Down Expand Up @@ -1114,12 +1135,53 @@ The structure has a number of fields, some of which are mandatory:
it should return 0.


(*) int (*instantiate)(struct key *key, const void *data, size_t datalen);
(*) int (*preparse)(struct key_preparsed_payload *prep);

This optional method permits the key type to attempt to parse payload
before a key is created (add key) or the key semaphore is taken (update or
instantiate key). The structure pointed to by prep looks like:

struct key_preparsed_payload {
char *description;
void *type_data[2];
void *payload;
const void *data;
size_t datalen;
size_t quotalen;
};

Before calling the method, the caller will fill in data and datalen with
the payload blob parameters; quotalen will be filled in with the default
quota size from the key type and the rest will be cleared.

If a description can be proposed from the payload contents, that should be
attached as a string to the description field. This will be used for the
key description if the caller of add_key() passes NULL or "".

The method can attach anything it likes to type_data[] and payload. These
are merely passed along to the instantiate() or update() operations.

The method should return 0 if success ful or a negative error code
otherwise.


(*) void (*free_preparse)(struct key_preparsed_payload *prep);

This method is only required if the preparse() method is provided,
otherwise it is unused. It cleans up anything attached to the
description, type_data and payload fields of the key_preparsed_payload
struct as filled in by the preparse() method.


(*) int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);

This method is called to attach a payload to a key during construction.
The payload attached need not bear any relation to the data passed to this
function.

The prep->data and prep->datalen fields will define the original payload
blob. If preparse() was supplied then other fields may be filled in also.

If the amount of data attached to the key differs from the size in
keytype->def_datalen, then key_payload_reserve() should be called.

Expand All @@ -1135,6 +1197,9 @@ The structure has a number of fields, some of which are mandatory:
If this type of key can be updated, then this method should be provided.
It is called to update a key's payload from the blob of data provided.

The prep->data and prep->datalen fields will define the original payload
blob. If preparse() was supplied then other fields may be filled in also.

key_payload_reserve() should be called if the data length might change
before any changes are actually made. Note that if this succeeds, the type
is committed to changing the key because it's already been altered, so all
Expand Down
6 changes: 3 additions & 3 deletions fs/cifs/cifs_spnego.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@

/* create a new cifs key */
static int
cifs_spnego_key_instantiate(struct key *key, const void *data, size_t datalen)
cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
char *payload;
int ret;

ret = -ENOMEM;
payload = kmalloc(datalen, GFP_KERNEL);
payload = kmalloc(prep->datalen, GFP_KERNEL);
if (!payload)
goto error;

/* attach the data */
memcpy(payload, data, datalen);
memcpy(payload, prep->data, prep->datalen);
key->payload.data = payload;
ret = 0;

Expand Down
20 changes: 8 additions & 12 deletions fs/cifs/cifsacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ static struct shrinker cifs_shrinker = {
};

static int
cifs_idmap_key_instantiate(struct key *key, const void *data, size_t datalen)
cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
char *payload;

payload = kmalloc(datalen, GFP_KERNEL);
payload = kmalloc(prep->datalen, GFP_KERNEL);
if (!payload)
return -ENOMEM;

memcpy(payload, data, datalen);
memcpy(payload, prep->data, prep->datalen);
key->payload.data = payload;
key->datalen = datalen;
key->datalen = prep->datalen;
return 0;
}

Expand Down Expand Up @@ -537,19 +537,15 @@ init_cifs_idmap(void)
if (!cred)
return -ENOMEM;

keyring = key_alloc(&key_type_keyring, ".cifs_idmap", 0, 0, cred,
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
KEY_USR_VIEW | KEY_USR_READ,
KEY_ALLOC_NOT_IN_QUOTA);
keyring = keyring_alloc(".cifs_idmap", 0, 0, cred,
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
KEY_USR_VIEW | KEY_USR_READ,
KEY_ALLOC_NOT_IN_QUOTA, NULL);
if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring);
goto failed_put_cred;
}

ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
if (ret < 0)
goto failed_put_key;

ret = register_key_type(&cifs_idmap_key_type);
if (ret < 0)
goto failed_put_key;
Expand Down
12 changes: 4 additions & 8 deletions fs/nfs/idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,15 @@ static int nfs_idmap_init_keyring(void)
if (!cred)
return -ENOMEM;

keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
KEY_USR_VIEW | KEY_USR_READ,
KEY_ALLOC_NOT_IN_QUOTA);
keyring = keyring_alloc(".id_resolver", 0, 0, cred,
(KEY_POS_ALL & ~KEY_POS_SETATTR) |
KEY_USR_VIEW | KEY_USR_READ,
KEY_ALLOC_NOT_IN_QUOTA, NULL);
if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring);
goto failed_put_cred;
}

ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
if (ret < 0)
goto failed_put_key;

ret = register_key_type(&key_type_id_resolver);
if (ret < 0)
goto failed_put_key;
Expand Down
6 changes: 4 additions & 2 deletions include/keys/user-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ struct user_key_payload {
extern struct key_type key_type_user;
extern struct key_type key_type_logon;

extern int user_instantiate(struct key *key, const void *data, size_t datalen);
extern int user_update(struct key *key, const void *data, size_t datalen);
struct key_preparsed_payload;

extern int user_instantiate(struct key *key, struct key_preparsed_payload *prep);
extern int user_update(struct key *key, struct key_preparsed_payload *prep);
extern int user_match(const struct key *key, const void *criterion);
extern void user_revoke(struct key *key);
extern void user_destroy(struct key *key);
Expand Down
17 changes: 2 additions & 15 deletions include/linux/cred.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,6 @@ extern int groups_search(const struct group_info *, kgid_t);
extern int in_group_p(kgid_t);
extern int in_egroup_p(kgid_t);

/*
* The common credentials for a thread group
* - shared by CLONE_THREAD
*/
#ifdef CONFIG_KEYS
struct thread_group_cred {
atomic_t usage;
pid_t tgid; /* thread group process ID */
spinlock_t lock;
struct key __rcu *session_keyring; /* keyring inherited over fork */
struct key *process_keyring; /* keyring private to this process */
struct rcu_head rcu; /* RCU deletion hook */
};
#endif

/*
* The security context of a task
*
Expand Down Expand Up @@ -139,6 +124,8 @@ struct cred {
#ifdef CONFIG_KEYS
unsigned char jit_keyring; /* default keyring to attach requested
* keys to */
struct key __rcu *session_keyring; /* keyring inherited over fork */
struct key *process_keyring; /* keyring private to this process */
struct key *thread_keyring; /* keyring private to this thread */
struct key *request_key_auth; /* assumed request_key authority */
struct thread_group_cred *tgcred; /* thread-group shared credentials */
Expand Down
35 changes: 33 additions & 2 deletions include/linux/key-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ struct key_construction {
struct key *authkey;/* authorisation for key being constructed */
};

/*
* Pre-parsed payload, used by key add, update and instantiate.
*
* This struct will be cleared and data and datalen will be set with the data
* and length parameters from the caller and quotalen will be set from
* def_datalen from the key type. Then if the preparse() op is provided by the
* key type, that will be called. Then the struct will be passed to the
* instantiate() or the update() op.
*
* If the preparse() op is given, the free_preparse() op will be called to
* clear the contents.
*/
struct key_preparsed_payload {
char *description; /* Proposed key description (or NULL) */
void *type_data[2]; /* Private key-type data */
void *payload; /* Proposed payload */
const void *data; /* Raw data */
size_t datalen; /* Raw datalen */
size_t quotalen; /* Quota length for proposed payload */
};

typedef int (*request_key_actor_t)(struct key_construction *key,
const char *op, void *aux);

Expand All @@ -45,18 +66,28 @@ struct key_type {
/* vet a description */
int (*vet_description)(const char *description);

/* Preparse the data blob from userspace that is to be the payload,
* generating a proposed description and payload that will be handed to
* the instantiate() and update() ops.
*/
int (*preparse)(struct key_preparsed_payload *prep);

/* Free a preparse data structure.
*/
void (*free_preparse)(struct key_preparsed_payload *prep);

/* instantiate a key of this type
* - this method should call key_payload_reserve() to determine if the
* user's quota will hold the payload
*/
int (*instantiate)(struct key *key, const void *data, size_t datalen);
int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);

/* update a key of this type (optional)
* - this method should call key_payload_reserve() to recalculate the
* quota consumption
* - the key must be locked against read when modifying
*/
int (*update)(struct key *key, const void *data, size_t datalen);
int (*update)(struct key *key, struct key_preparsed_payload *prep);

/* match a key against a description */
int (*match)(const struct key *key, const void *desc);
Expand Down
1 change: 1 addition & 0 deletions include/linux/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ extern int key_unlink(struct key *keyring,

extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
const struct cred *cred,
key_perm_t perm,
unsigned long flags,
struct key *dest);

Expand Down
Loading

0 comments on commit 61d335d

Please sign in to comment.