Skip to content

Commit d4f65b5

Browse files
committed
KEYS: Add payload preparsing opportunity prior to key instantiate or 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 <dhowells@redhat.com>
1 parent eeea3ac commit d4f65b5

15 files changed

Lines changed: 250 additions & 102 deletions

File tree

‎Documentation/security/keys.txt‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ The main syscalls are:
412412
to the keyring. In this case, an error will be generated if the process
413413
does not have permission to write to the keyring.
414414

415+
If the key type supports it, if the description is NULL or an empty
416+
string, the key type will try and generate a description from the content
417+
of the payload.
418+
415419
The payload is optional, and the pointer can be NULL if not required by
416420
the type. The payload is plen in size, and plen can be zero for an empty
417421
payload.
@@ -1114,12 +1118,53 @@ The structure has a number of fields, some of which are mandatory:
11141118
it should return 0.
11151119

11161120

1117-
(*) int (*instantiate)(struct key *key, const void *data, size_t datalen);
1121+
(*) int (*preparse)(struct key_preparsed_payload *prep);
1122+
1123+
This optional method permits the key type to attempt to parse payload
1124+
before a key is created (add key) or the key semaphore is taken (update or
1125+
instantiate key). The structure pointed to by prep looks like:
1126+
1127+
struct key_preparsed_payload {
1128+
char *description;
1129+
void *type_data[2];
1130+
void *payload;
1131+
const void *data;
1132+
size_t datalen;
1133+
size_t quotalen;
1134+
};
1135+
1136+
Before calling the method, the caller will fill in data and datalen with
1137+
the payload blob parameters; quotalen will be filled in with the default
1138+
quota size from the key type and the rest will be cleared.
1139+
1140+
If a description can be proposed from the payload contents, that should be
1141+
attached as a string to the description field. This will be used for the
1142+
key description if the caller of add_key() passes NULL or "".
1143+
1144+
The method can attach anything it likes to type_data[] and payload. These
1145+
are merely passed along to the instantiate() or update() operations.
1146+
1147+
The method should return 0 if success ful or a negative error code
1148+
otherwise.
1149+
1150+
1151+
(*) void (*free_preparse)(struct key_preparsed_payload *prep);
1152+
1153+
This method is only required if the preparse() method is provided,
1154+
otherwise it is unused. It cleans up anything attached to the
1155+
description, type_data and payload fields of the key_preparsed_payload
1156+
struct as filled in by the preparse() method.
1157+
1158+
1159+
(*) int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
11181160

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

1165+
The prep->data and prep->datalen fields will define the original payload
1166+
blob. If preparse() was supplied then other fields may be filled in also.
1167+
11231168
If the amount of data attached to the key differs from the size in
11241169
keytype->def_datalen, then key_payload_reserve() should be called.
11251170

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

1183+
The prep->data and prep->datalen fields will define the original payload
1184+
blob. If preparse() was supplied then other fields may be filled in also.
1185+
11381186
key_payload_reserve() should be called if the data length might change
11391187
before any changes are actually made. Note that if this succeeds, the type
11401188
is committed to changing the key because it's already been altered, so all

‎fs/cifs/cifs_spnego.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131

3232
/* create a new cifs key */
3333
static int
34-
cifs_spnego_key_instantiate(struct key *key, const void *data, size_t datalen)
34+
cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
3535
{
3636
char *payload;
3737
int ret;
3838

3939
ret = -ENOMEM;
40-
payload = kmalloc(datalen, GFP_KERNEL);
40+
payload = kmalloc(prep->datalen, GFP_KERNEL);
4141
if (!payload)
4242
goto error;
4343

4444
/* attach the data */
45-
memcpy(payload, data, datalen);
45+
memcpy(payload, prep->data, prep->datalen);
4646
key->payload.data = payload;
4747
ret = 0;
4848

‎fs/cifs/cifsacl.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ static struct shrinker cifs_shrinker = {
167167
};
168168

169169
static int
170-
cifs_idmap_key_instantiate(struct key *key, const void *data, size_t datalen)
170+
cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
171171
{
172172
char *payload;
173173

174-
payload = kmalloc(datalen, GFP_KERNEL);
174+
payload = kmalloc(prep->datalen, GFP_KERNEL);
175175
if (!payload)
176176
return -ENOMEM;
177177

178-
memcpy(payload, data, datalen);
178+
memcpy(payload, prep->data, prep->datalen);
179179
key->payload.data = payload;
180-
key->datalen = datalen;
180+
key->datalen = prep->datalen;
181181
return 0;
182182
}
183183

‎include/keys/user-type.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ struct user_key_payload {
3535
extern struct key_type key_type_user;
3636
extern struct key_type key_type_logon;
3737

38-
extern int user_instantiate(struct key *key, const void *data, size_t datalen);
39-
extern int user_update(struct key *key, const void *data, size_t datalen);
38+
struct key_preparsed_payload;
39+
40+
extern int user_instantiate(struct key *key, struct key_preparsed_payload *prep);
41+
extern int user_update(struct key *key, struct key_preparsed_payload *prep);
4042
extern int user_match(const struct key *key, const void *criterion);
4143
extern void user_revoke(struct key *key);
4244
extern void user_destroy(struct key *key);

‎include/linux/key-type.h‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ struct key_construction {
2626
struct key *authkey;/* authorisation for key being constructed */
2727
};
2828

29+
/*
30+
* Pre-parsed payload, used by key add, update and instantiate.
31+
*
32+
* This struct will be cleared and data and datalen will be set with the data
33+
* and length parameters from the caller and quotalen will be set from
34+
* def_datalen from the key type. Then if the preparse() op is provided by the
35+
* key type, that will be called. Then the struct will be passed to the
36+
* instantiate() or the update() op.
37+
*
38+
* If the preparse() op is given, the free_preparse() op will be called to
39+
* clear the contents.
40+
*/
41+
struct key_preparsed_payload {
42+
char *description; /* Proposed key description (or NULL) */
43+
void *type_data[2]; /* Private key-type data */
44+
void *payload; /* Proposed payload */
45+
const void *data; /* Raw data */
46+
size_t datalen; /* Raw datalen */
47+
size_t quotalen; /* Quota length for proposed payload */
48+
};
49+
2950
typedef int (*request_key_actor_t)(struct key_construction *key,
3051
const char *op, void *aux);
3152

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

69+
/* Preparse the data blob from userspace that is to be the payload,
70+
* generating a proposed description and payload that will be handed to
71+
* the instantiate() and update() ops.
72+
*/
73+
int (*preparse)(struct key_preparsed_payload *prep);
74+
75+
/* Free a preparse data structure.
76+
*/
77+
void (*free_preparse)(struct key_preparsed_payload *prep);
78+
4879
/* instantiate a key of this type
4980
* - this method should call key_payload_reserve() to determine if the
5081
* user's quota will hold the payload
5182
*/
52-
int (*instantiate)(struct key *key, const void *data, size_t datalen);
83+
int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);
5384

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

6192
/* match a key against a description */
6293
int (*match)(const struct key *key, const void *desc);

‎net/ceph/crypto.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,15 @@ int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len,
423423
}
424424
}
425425

426-
int ceph_key_instantiate(struct key *key, const void *data, size_t datalen)
426+
int ceph_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
427427
{
428428
struct ceph_crypto_key *ckey;
429+
size_t datalen = prep->datalen;
429430
int ret;
430431
void *p;
431432

432433
ret = -EINVAL;
433-
if (datalen <= 0 || datalen > 32767 || !data)
434+
if (datalen <= 0 || datalen > 32767 || !prep->data)
434435
goto err;
435436

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

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

‎net/dns_resolver/dns_key.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ const struct cred *dns_resolver_cache;
5959
* "ip1,ip2,...#foo=bar"
6060
*/
6161
static int
62-
dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen)
62+
dns_resolver_instantiate(struct key *key, struct key_preparsed_payload *prep)
6363
{
6464
struct user_key_payload *upayload;
6565
unsigned long derrno;
6666
int ret;
67-
size_t result_len = 0;
68-
const char *data = _data, *end, *opt;
67+
size_t datalen = prep->datalen, result_len = 0;
68+
const char *data = prep->data, *end, *opt;
6969

7070
kenter("%%%d,%s,'%*.*s',%zu",
7171
key->serial, key->description,

‎net/rxrpc/ar-key.c‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include "ar-internal.h"
2727

2828
static int rxrpc_vet_description_s(const char *);
29-
static int rxrpc_instantiate(struct key *, const void *, size_t);
30-
static int rxrpc_instantiate_s(struct key *, const void *, size_t);
29+
static int rxrpc_instantiate(struct key *, struct key_preparsed_payload *);
30+
static int rxrpc_instantiate_s(struct key *, struct key_preparsed_payload *);
3131
static void rxrpc_destroy(struct key *);
3232
static void rxrpc_destroy_s(struct key *);
3333
static void rxrpc_describe(const struct key *, struct seq_file *);
@@ -678,34 +678,34 @@ static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datal
678678
*
679679
* if no data is provided, then a no-security key is made
680680
*/
681-
static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
681+
static int rxrpc_instantiate(struct key *key, struct key_preparsed_payload *prep)
682682
{
683683
const struct rxrpc_key_data_v1 *v1;
684684
struct rxrpc_key_token *token, **pp;
685685
size_t plen;
686686
u32 kver;
687687
int ret;
688688

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

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

695695
/* determine if the XDR payload format is being used */
696-
if (datalen > 7 * 4) {
697-
ret = rxrpc_instantiate_xdr(key, data, datalen);
696+
if (prep->datalen > 7 * 4) {
697+
ret = rxrpc_instantiate_xdr(key, prep->data, prep->datalen);
698698
if (ret != -EPROTO)
699699
return ret;
700700
}
701701

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

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

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

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

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

725725
_debug("SCIX: %u", v1->security_index);
@@ -784,25 +784,25 @@ static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
784784
* instantiate a server secret key
785785
* data should be a pointer to the 8-byte secret key
786786
*/
787-
static int rxrpc_instantiate_s(struct key *key, const void *data,
788-
size_t datalen)
787+
static int rxrpc_instantiate_s(struct key *key,
788+
struct key_preparsed_payload *prep)
789789
{
790790
struct crypto_blkcipher *ci;
791791

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

794-
if (datalen != 8)
794+
if (prep->datalen != 8)
795795
return -EINVAL;
796796

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

799799
ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
800800
if (IS_ERR(ci)) {
801801
_leave(" = %ld", PTR_ERR(ci));
802802
return PTR_ERR(ci);
803803
}
804804

805-
if (crypto_blkcipher_setkey(ci, data, 8) < 0)
805+
if (crypto_blkcipher_setkey(ci, prep->data, 8) < 0)
806806
BUG();
807807

808808
key->payload.data = ci;

‎security/keys/encrypted-keys/encrypted.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -773,25 +773,26 @@ static int encrypted_init(struct encrypted_key_payload *epayload,
773773
*
774774
* On success, return 0. Otherwise return errno.
775775
*/
776-
static int encrypted_instantiate(struct key *key, const void *data,
777-
size_t datalen)
776+
static int encrypted_instantiate(struct key *key,
777+
struct key_preparsed_payload *prep)
778778
{
779779
struct encrypted_key_payload *epayload = NULL;
780780
char *datablob = NULL;
781781
const char *format = NULL;
782782
char *master_desc = NULL;
783783
char *decrypted_datalen = NULL;
784784
char *hex_encoded_iv = NULL;
785+
size_t datalen = prep->datalen;
785786
int ret;
786787

787-
if (datalen <= 0 || datalen > 32767 || !data)
788+
if (datalen <= 0 || datalen > 32767 || !prep->data)
788789
return -EINVAL;
789790

790791
datablob = kmalloc(datalen + 1, GFP_KERNEL);
791792
if (!datablob)
792793
return -ENOMEM;
793794
datablob[datalen] = 0;
794-
memcpy(datablob, data, datalen);
795+
memcpy(datablob, prep->data, datalen);
795796
ret = datablob_parse(datablob, &format, &master_desc,
796797
&decrypted_datalen, &hex_encoded_iv);
797798
if (ret < 0)
@@ -834,24 +835,25 @@ static void encrypted_rcu_free(struct rcu_head *rcu)
834835
*
835836
* On success, return 0. Otherwise return errno.
836837
*/
837-
static int encrypted_update(struct key *key, const void *data, size_t datalen)
838+
static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
838839
{
839840
struct encrypted_key_payload *epayload = key->payload.data;
840841
struct encrypted_key_payload *new_epayload;
841842
char *buf;
842843
char *new_master_desc = NULL;
843844
const char *format = NULL;
845+
size_t datalen = prep->datalen;
844846
int ret = 0;
845847

846-
if (datalen <= 0 || datalen > 32767 || !data)
848+
if (datalen <= 0 || datalen > 32767 || !prep->data)
847849
return -EINVAL;
848850

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

853855
buf[datalen] = 0;
854-
memcpy(buf, data, datalen);
856+
memcpy(buf, prep->data, datalen);
855857
ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
856858
if (ret < 0)
857859
goto out;

0 commit comments

Comments
 (0)