Skip to content

Commit b9fffa3

Browse files
dhowellsxjamesmorris
authored andcommitted
KEYS: Add a key type op to permit the key description to be vetted
Add a key type operation to permit the key type to vet the description of a new key that key_alloc() is about to allocate. The operation may reject the description if it wishes with an error of its choosing. If it does this, the key will not be allocated. Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Mimi Zohar <zohar@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 633e804 commit b9fffa3

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎Documentation/keys.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,13 @@ The structure has a number of fields, some of which are mandatory:
10621062
viable.
10631063

10641064

1065+
(*) int (*vet_description)(const char *description);
1066+
1067+
This optional method is called to vet a key description. If the key type
1068+
doesn't approve of the key description, it may return an error, otherwise
1069+
it should return 0.
1070+
1071+
10651072
(*) int (*instantiate)(struct key *key, const void *data, size_t datalen);
10661073

10671074
This method is called to attach a payload to a key during construction.

‎include/linux/key-type.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ struct key_type {
4141
*/
4242
size_t def_datalen;
4343

44+
/* vet a description */
45+
int (*vet_description)(const char *description);
46+
4447
/* instantiate a key of this type
4548
* - this method should call key_payload_reserve() to determine if the
4649
* user's quota will hold the payload

‎net/rxrpc/ar-key.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <keys/user-type.h>
2626
#include "ar-internal.h"
2727

28+
static int rxrpc_vet_description_s(const char *);
2829
static int rxrpc_instantiate(struct key *, const void *, size_t);
2930
static int rxrpc_instantiate_s(struct key *, const void *, size_t);
3031
static void rxrpc_destroy(struct key *);
@@ -52,12 +53,30 @@ EXPORT_SYMBOL(key_type_rxrpc);
5253
*/
5354
struct key_type key_type_rxrpc_s = {
5455
.name = "rxrpc_s",
56+
.vet_description = rxrpc_vet_description_s,
5557
.instantiate = rxrpc_instantiate_s,
5658
.match = user_match,
5759
.destroy = rxrpc_destroy_s,
5860
.describe = rxrpc_describe,
5961
};
6062

63+
/*
64+
* Vet the description for an RxRPC server key
65+
*/
66+
static int rxrpc_vet_description_s(const char *desc)
67+
{
68+
unsigned long num;
69+
char *p;
70+
71+
num = simple_strtoul(desc, &p, 10);
72+
if (*p != ':' || num > 65535)
73+
return -EINVAL;
74+
num = simple_strtoul(p + 1, &p, 10);
75+
if (*p || num < 1 || num > 255)
76+
return -EINVAL;
77+
return 0;
78+
}
79+
6180
/*
6281
* parse an RxKAD type XDR format token
6382
* - the caller guarantees we have at least 4 words

‎security/keys/key.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ struct key *key_alloc(struct key_type *type, const char *desc,
249249
if (!desc || !*desc)
250250
goto error;
251251

252+
if (type->vet_description) {
253+
ret = type->vet_description(desc);
254+
if (ret < 0) {
255+
key = ERR_PTR(ret);
256+
goto error;
257+
}
258+
}
259+
252260
desclen = strlen(desc) + 1;
253261
quotalen = desclen + type->def_datalen;
254262

0 commit comments

Comments
 (0)