12 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
23 #define rcu_dereference_locked_keyring(keyring) \
24 (rcu_dereference_protected( \
25 (keyring)->payload.subscriptions, \
26 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
28 #define rcu_deref_link_locked(klist, index, keyring) \
29 (rcu_dereference_protected( \
30 (klist)->keys[index], \
31 rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
33 #define MAX_KEYRING_LINKS \
34 min_t(size_t, USHRT_MAX - 1, \
35 ((PAGE_SIZE - sizeof(struct keyring_list)) / sizeof(struct key *)))
37 #define KEY_LINK_FIXQUOTA 1UL
43 #define KEYRING_SEARCH_MAX_DEPTH 6
48 #define KEYRING_NAME_HASH_SIZE (1 << 5)
53 static inline unsigned keyring_hash(
const char *
desc)
58 bucket += (
unsigned char)*desc;
68 static int keyring_instantiate(
struct key *keyring,
69 struct key_preparsed_payload *prep);
70 static int keyring_match(
const struct key *keyring,
const void *criterion);
71 static void keyring_revoke(
struct key *keyring);
72 static void keyring_destroy(
struct key *keyring);
73 static void keyring_describe(
const struct key *keyring,
struct seq_file *
m);
74 static long keyring_read(
const struct key *keyring,
80 .instantiate = keyring_instantiate,
81 .match = keyring_match,
82 .revoke = keyring_revoke,
83 .destroy = keyring_destroy,
84 .describe = keyring_describe,
99 static void keyring_publish_name(
struct key *keyring)
103 if (keyring->description) {
104 bucket = keyring_hash(keyring->description);
108 if (!keyring_name_hash[bucket].
next)
109 INIT_LIST_HEAD(&keyring_name_hash[bucket]);
112 &keyring_name_hash[bucket]);
123 static int keyring_instantiate(
struct key *keyring,
124 struct key_preparsed_payload *prep)
129 if (prep->datalen == 0) {
131 keyring_publish_name(keyring);
141 static int keyring_match(
const struct key *keyring,
const void *
description)
143 return keyring->description &&
144 strcmp(keyring->description, description) == 0;
156 static void keyring_destroy(
struct key *keyring)
161 if (keyring->description) {
164 if (keyring->type_data.link.next !=
NULL &&
165 !list_empty(&keyring->type_data.link))
173 for (loop = klist->
nkeys - 1; loop >= 0; loop--)
182 static void keyring_describe(
const struct key *keyring,
struct seq_file *
m)
186 if (keyring->description)
191 if (key_is_instantiated(keyring)) {
207 static long keyring_read(
const struct key *keyring,
219 qty = klist->
nkeys *
sizeof(key_serial_t);
221 if (buffer && buflen > 0) {
229 for (loop = 0; loop < klist->
nkeys; loop++) {
233 tmp =
sizeof(key_serial_t);
266 keyring =
key_alloc(&key_type_keyring, description,
268 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
271 if (!IS_ERR(keyring)) {
275 keyring = ERR_PTR(ret);
320 struct key_type *
type,
334 unsigned long possessed, kflags;
335 struct key *keyring, *
key;
340 keyring = key_ref_to_ptr(keyring_ref);
341 possessed = is_key_possessed(keyring_ref);
347 key_ref = ERR_PTR(err);
352 if (keyring->type != &key_type_keyring)
363 key_ref = ERR_PTR(-
EAGAIN);
364 kflags = keyring->flags;
365 if (keyring->type == type && match(keyring, description)) {
372 if (kflags & (1 << KEY_FLAG_REVOKED))
374 if (key->expiry && now.
tv_sec >= key->expiry)
376 key_ref = ERR_PTR(key->type_data.reject_error);
377 if (kflags & (1 << KEY_FLAG_NEGATIVE))
384 key_ref = ERR_PTR(-
EAGAIN);
385 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
386 (1 << KEY_FLAG_REVOKED) |
387 (1 << KEY_FLAG_NEGATIVE)) ||
388 (keyring->expiry && now.
tv_sec >= keyring->expiry))
393 kflags = keyring->flags;
394 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
395 (1 << KEY_FLAG_REVOKED)))
396 goto not_this_keyring;
400 goto not_this_keyring;
403 nkeys = keylist->
nkeys;
405 for (kix = 0; kix < nkeys; kix++) {
410 if (key->type != type)
414 if (!no_state_check) {
415 if (kflags & ((1 << KEY_FLAG_INVALIDATED) |
416 (1 << KEY_FLAG_REVOKED)))
419 if (key->expiry && now.
tv_sec >= key->expiry)
424 if (!
match(key, description))
436 if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
437 err = key->type_data.reject_error;
447 nkeys = keylist->
nkeys;
449 for (; kix < nkeys; kix++) {
451 if (key->type != &key_type_keyring)
487 key_ref = ERR_PTR(err);
493 key->last_used_at = now.
tv_sec;
494 keyring->last_used_at = now.
tv_sec;
498 key_ref = make_key_ref(key, possessed);
515 struct key_type *
type,
522 type, description, type->match,
false);
542 const struct key_type *
ktype,
547 unsigned long possessed;
548 struct key *keyring, *
key;
551 keyring = key_ref_to_ptr(keyring_ref);
552 possessed = is_key_possessed(keyring_ref);
558 nkeys = klist->
nkeys;
560 for (loop = 0; loop < nkeys ; loop++) {
562 if (key->type == ktype &&
563 (!key->type->match ||
564 key->type->match(key, description)) &&
565 key_permission(make_key_ref(key, possessed),
567 !(key->flags & ((1 << KEY_FLAG_INVALIDATED) |
568 (1 << KEY_FLAG_REVOKED)))
579 keyring->last_used_at = key->last_used_at =
582 return make_key_ref(key, possessed);
604 bucket = keyring_hash(name);
608 if (keyring_name_hash[bucket].
next) {
612 &keyring_name_hash[bucket],
618 if (
test_bit(KEY_FLAG_REVOKED, &keyring->flags))
621 if (
strcmp(keyring->description, name) != 0)
624 if (!skip_perm_check &&
625 key_permission(make_key_ref(keyring, 0),
639 keyring = ERR_PTR(-
ENOKEY);
652 static int keyring_detect_cycle(
struct key *
A,
struct key *
B)
660 struct key *subtree, *
key;
661 int sp, nkeys, kix,
ret;
674 if (
test_bit(KEY_FLAG_REVOKED, &subtree->flags))
675 goto not_this_keyring;
679 goto not_this_keyring;
684 nkeys = keylist->
nkeys;
686 for (; kix < nkeys; kix++) {
693 if (key->type == &key_type_keyring) {
738 static void keyring_unlink_rcu_disposal(
struct rcu_head *rcu)
757 unsigned long prealloc;
765 if (keyring->type != &key_type_keyring)
771 if (
test_bit(KEY_FLAG_REVOKED, &keyring->flags))
776 if (
type == &key_type_keyring)
783 if (klist && klist->
nkeys > 0) {
785 for (loop = klist->
nkeys - 1; loop >= 0; loop--) {
788 if (key->type ==
type &&
798 if (key->last_used_at < lowest_lru) {
799 lowest_lru = key->last_used_at;
809 kdebug(
"LRU discard %d\n", lru);
832 BUG_ON(max <= klist->maxkeys);
835 size =
sizeof(*klist) +
sizeof(
struct key *) * max;
845 sizeof(
struct key *) * klist->
nkeys);
860 *_prealloc = prealloc;
869 if (
type == &key_type_keyring)
870 up_write(&keyring_serialise_link_sem);
885 if (key->type == &key_type_keyring)
888 return keyring_detect_cycle(keyring, key);
901 unsigned long *_prealloc)
909 kenter(
"%d,%d,%p", keyring->serial, key->serial, nklist);
914 keyring->last_used_at = key->last_used_at =
920 kdebug(
"reissue %hu/%hu/%hu",
930 kdebug(
"dispose %hu/%hu/%hu",
932 call_rcu(&klist->
rcu, keyring_unlink_rcu_disposal);
935 kdebug(
"replace %hu/%hu/%hu",
947 kdebug(
"append %hu/%hu/%hu",
962 unsigned long prealloc)
970 if (
type == &key_type_keyring)
971 up_write(&keyring_serialise_link_sem);
1005 unsigned long prealloc;
1049 if (keyring->type != &key_type_keyring)
1057 for (loop = 0; loop < klist->
nkeys; loop++)
1059 goto key_is_present;
1068 nklist =
kmalloc(
sizeof(*klist) +
1069 sizeof(
struct key *) * klist->
maxkeys,
1079 loop *
sizeof(
struct key *));
1081 if (loop < nklist->nkeys)
1083 &klist->
keys[loop + 1],
1084 (nklist->
nkeys - loop) *
sizeof(
struct key *));
1096 call_rcu(&klist->
rcu, keyring_unlink_rcu_disposal);
1113 static void keyring_clear_rcu_disposal(
struct rcu_head *rcu)
1120 for (loop = klist->
nkeys - 1; loop >= 0; loop--)
1140 if (keyring->type == &key_type_keyring) {
1158 call_rcu(&klist->
rcu, keyring_clear_rcu_disposal);
1172 static void keyring_revoke(
struct key *keyring)
1183 call_rcu(&klist->
rcu, keyring_clear_rcu_disposal);
1199 int loop, keep,
max;
1201 kenter(
"{%x,%s}", key_serial(keyring), keyring->description);
1211 for (loop = klist->
nkeys - 1; loop >= 0; loop--)
1216 if (keep == klist->
nkeys)
1233 for (loop = klist->
nkeys - 1; loop >= 0; loop--) {
1235 if (!key_is_dead(key, limit)) {
1257 call_rcu(&klist->
rcu, keyring_clear_rcu_disposal);
1263 keyring_clear_rcu_disposal(&new->rcu);