Skip to content

Commit

Permalink
KEYS: Add payload preparsing opportunity prior to key instantiate or …
Browse files Browse the repository at this point in the history
…update

Give the key type the opportunity to preparse the payload prior to the
instantiation and update routines being called.  This is done with the
provision of two new key type operations:

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

If the first operation is present, then it is called before key creation (in
the add/update case) or before the key semaphore is taken (in the update and
instantiate cases).  The second operation is called to clean up if the first
was called.

preparse() is given the opportunity to fill in the following structure:

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

Before the preparser is called, the first three fields will have been cleared,
the payload pointer and size will be stored in data and datalen and the default
quota size from the key_type struct will be stored into quotalen.

The preparser may parse the payload in any way it likes and may store data in
the type_data[] and payload fields for use by the instantiate() and update()
ops.

The preparser may also propose a description for the key by attaching it as a
string to the description field.  This can be used by passing a NULL or ""
description to the add_key() system call or the key_create_or_update()
function.  This cannot work with request_key() as that required the description
to tell the upcall about the key to be created.

This, for example permits keys that store PGP public keys to generate their own
name from the user ID and public key fingerprint in the key.

The instantiate() and update() operations are then modified to look like this:

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

and the new payload data is passed in *prep, whether or not it was preparsed.

Signed-off-by: David Howells <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
dhowells authored and rustyrussell committed Oct 8, 2012
1 parent 9bb9c3b commit cf7f601
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 102 deletions.
50 changes: 49 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 @@ -1114,12 +1118,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 +1180,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
8 changes: 4 additions & 4 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
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
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
9 changes: 5 additions & 4 deletions net/ceph/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,15 @@ int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
}
}

int ceph_key_instantiate(struct key *key, const void *data, size_t datalen)
int ceph_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
struct ceph_crypto_key *ckey;
size_t datalen = prep->datalen;
int ret;
void *p;

ret = -EINVAL;
if (datalen <= 0 || datalen > 32767 || !data)
if (datalen <= 0 || datalen > 32767 || !prep->data)
goto err;

ret = key_payload_reserve(key, datalen);
Expand All @@ -443,8 +444,8 @@ int ceph_key_instantiate(struct key *key, const void *data, size_t datalen)
goto err;

/* TODO ceph_crypto_key_decode should really take const input */
p = (void *)data;
ret = ceph_crypto_key_decode(ckey, &p, (char*)data+datalen);
p = (void *)prep->data;
ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
if (ret < 0)
goto err_ckey;

Expand Down
6 changes: 3 additions & 3 deletions net/dns_resolver/dns_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ const struct cred *dns_resolver_cache;
* "ip1,ip2,...#foo=bar"
*/
static int
dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
dns_resolver_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
struct user_key_payload *upayload;
unsigned long derrno;
int ret;
size_t result_len = 0;
const char *data = _data, *end, *opt;
size_t datalen = prep->datalen, result_len = 0;
const char *data = prep->data, *end, *opt;

kenter("%%%d,%s,'%*.*s',%zu",
key->serial, key->description,
Expand Down
40 changes: 20 additions & 20 deletions net/rxrpc/ar-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include "ar-internal.h"

static int rxrpc_vet_description_s(const char *);
static int rxrpc_instantiate(struct key *, const void *, size_t);
static int rxrpc_instantiate_s(struct key *, const void *, size_t);
static int rxrpc_instantiate(struct key *, struct key_preparsed_payload *);
static int rxrpc_instantiate_s(struct key *, struct key_preparsed_payload *);
static void rxrpc_destroy(struct key *);
static void rxrpc_destroy_s(struct key *);
static void rxrpc_describe(const struct key *, struct seq_file *);
Expand Down Expand Up @@ -678,34 +678,34 @@ static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datal
*
* if no data is provided, then a no-security key is made
*/
static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
static int rxrpc_instantiate(struct key *key, struct key_preparsed_payload *prep)
{
const struct rxrpc_key_data_v1 *v1;
struct rxrpc_key_token *token, **pp;
size_t plen;
u32 kver;
int ret;

_enter("{%x},,%zu", key_serial(key), datalen);
_enter("{%x},,%zu", key_serial(key), prep->datalen);

/* handle a no-security key */
if (!data && datalen == 0)
if (!prep->data && prep->datalen == 0)
return 0;

/* determine if the XDR payload format is being used */
if (datalen > 7 * 4) {
ret = rxrpc_instantiate_xdr(key, data, datalen);
if (prep->datalen > 7 * 4) {
ret = rxrpc_instantiate_xdr(key, prep->data, prep->datalen);
if (ret != -EPROTO)
return ret;
}

/* get the key interface version number */
ret = -EINVAL;
if (datalen <= 4 || !data)
if (prep->datalen <= 4 || !prep->data)
goto error;
memcpy(&kver, data, sizeof(kver));
data += sizeof(kver);
datalen -= sizeof(kver);
memcpy(&kver, prep->data, sizeof(kver));
prep->data += sizeof(kver);
prep->datalen -= sizeof(kver);

_debug("KEY I/F VERSION: %u", kver);

Expand All @@ -715,11 +715,11 @@ static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)

/* deal with a version 1 key */
ret = -EINVAL;
if (datalen < sizeof(*v1))
if (prep->datalen < sizeof(*v1))
goto error;

v1 = data;
if (datalen != sizeof(*v1) + v1->ticket_length)
v1 = prep->data;
if (prep->datalen != sizeof(*v1) + v1->ticket_length)
goto error;

_debug("SCIX: %u", v1->security_index);
Expand Down Expand Up @@ -784,25 +784,25 @@ static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
* instantiate a server secret key
* data should be a pointer to the 8-byte secret key
*/
static int rxrpc_instantiate_s(struct key *key, const void *data,
size_t datalen)
static int rxrpc_instantiate_s(struct key *key,
struct key_preparsed_payload *prep)
{
struct crypto_blkcipher *ci;

_enter("{%x},,%zu", key_serial(key), datalen);
_enter("{%x},,%zu", key_serial(key), prep->datalen);

if (datalen != 8)
if (prep->datalen != 8)
return -EINVAL;

memcpy(&key->type_data, data, 8);
memcpy(&key->type_data, prep->data, 8);

ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(ci)) {
_leave(" = %ld", PTR_ERR(ci));
return PTR_ERR(ci);
}

if (crypto_blkcipher_setkey(ci, data, 8) < 0)
if (crypto_blkcipher_setkey(ci, prep->data, 8) < 0)
BUG();

key->payload.data = ci;
Expand Down
16 changes: 9 additions & 7 deletions security/keys/encrypted-keys/encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,25 +773,26 @@ static int encrypted_init(struct encrypted_key_payload *epayload,
*
* On success, return 0. Otherwise return errno.
*/
static int encrypted_instantiate(struct key *key, const void *data,
size_t datalen)
static int encrypted_instantiate(struct key *key,
struct key_preparsed_payload *prep)
{
struct encrypted_key_payload *epayload = NULL;
char *datablob = NULL;
const char *format = NULL;
char *master_desc = NULL;
char *decrypted_datalen = NULL;
char *hex_encoded_iv = NULL;
size_t datalen = prep->datalen;
int ret;

if (datalen <= 0 || datalen > 32767 || !data)
if (datalen <= 0 || datalen > 32767 || !prep->data)
return -EINVAL;

datablob = kmalloc(datalen + 1, GFP_KERNEL);
if (!datablob)
return -ENOMEM;
datablob[datalen] = 0;
memcpy(datablob, data, datalen);
memcpy(datablob, prep->data, datalen);
ret = datablob_parse(datablob, &format, &master_desc,
&decrypted_datalen, &hex_encoded_iv);
if (ret < 0)
Expand Down Expand Up @@ -834,24 +835,25 @@ static void encrypted_rcu_free(struct rcu_head *rcu)
*
* On success, return 0. Otherwise return errno.
*/
static int encrypted_update(struct key *key, const void *data, size_t datalen)
static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
{
struct encrypted_key_payload *epayload = key->payload.data;
struct encrypted_key_payload *new_epayload;
char *buf;
char *new_master_desc = NULL;
const char *format = NULL;
size_t datalen = prep->datalen;
int ret = 0;

if (datalen <= 0 || datalen > 32767 || !data)
if (datalen <= 0 || datalen > 32767 || !prep->data)
return -EINVAL;

buf = kmalloc(datalen + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;

buf[datalen] = 0;
memcpy(buf, data, datalen);
memcpy(buf, prep->data, datalen);
ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
if (ret < 0)
goto out;
Expand Down
Loading

0 comments on commit cf7f601

Please sign in to comment.