key.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /* key.c: basic authentication token and access key management
  2. *
  3. * Copyright (C) 2004-6 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/security.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/err.h>
  18. #include "internal.h"
  19. static kmem_cache_t *key_jar;
  20. static key_serial_t key_serial_next = 3;
  21. struct rb_root key_serial_tree; /* tree of keys indexed by serial */
  22. DEFINE_SPINLOCK(key_serial_lock);
  23. struct rb_root key_user_tree; /* tree of quota records indexed by UID */
  24. DEFINE_SPINLOCK(key_user_lock);
  25. static LIST_HEAD(key_types_list);
  26. static DECLARE_RWSEM(key_types_sem);
  27. static void key_cleanup(void *data);
  28. static DECLARE_WORK(key_cleanup_task, key_cleanup, NULL);
  29. /* we serialise key instantiation and link */
  30. DECLARE_RWSEM(key_construction_sem);
  31. /* any key who's type gets unegistered will be re-typed to this */
  32. static struct key_type key_type_dead = {
  33. .name = "dead",
  34. };
  35. #ifdef KEY_DEBUGGING
  36. void __key_check(const struct key *key)
  37. {
  38. printk("__key_check: key %p {%08x} should be {%08x}\n",
  39. key, key->magic, KEY_DEBUG_MAGIC);
  40. BUG();
  41. }
  42. #endif
  43. /*****************************************************************************/
  44. /*
  45. * get the key quota record for a user, allocating a new record if one doesn't
  46. * already exist
  47. */
  48. struct key_user *key_user_lookup(uid_t uid)
  49. {
  50. struct key_user *candidate = NULL, *user;
  51. struct rb_node *parent = NULL;
  52. struct rb_node **p;
  53. try_again:
  54. p = &key_user_tree.rb_node;
  55. spin_lock(&key_user_lock);
  56. /* search the tree for a user record with a matching UID */
  57. while (*p) {
  58. parent = *p;
  59. user = rb_entry(parent, struct key_user, node);
  60. if (uid < user->uid)
  61. p = &(*p)->rb_left;
  62. else if (uid > user->uid)
  63. p = &(*p)->rb_right;
  64. else
  65. goto found;
  66. }
  67. /* if we get here, we failed to find a match in the tree */
  68. if (!candidate) {
  69. /* allocate a candidate user record if we don't already have
  70. * one */
  71. spin_unlock(&key_user_lock);
  72. user = NULL;
  73. candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
  74. if (unlikely(!candidate))
  75. goto out;
  76. /* the allocation may have scheduled, so we need to repeat the
  77. * search lest someone else added the record whilst we were
  78. * asleep */
  79. goto try_again;
  80. }
  81. /* if we get here, then the user record still hadn't appeared on the
  82. * second pass - so we use the candidate record */
  83. atomic_set(&candidate->usage, 1);
  84. atomic_set(&candidate->nkeys, 0);
  85. atomic_set(&candidate->nikeys, 0);
  86. candidate->uid = uid;
  87. candidate->qnkeys = 0;
  88. candidate->qnbytes = 0;
  89. spin_lock_init(&candidate->lock);
  90. INIT_LIST_HEAD(&candidate->consq);
  91. rb_link_node(&candidate->node, parent, p);
  92. rb_insert_color(&candidate->node, &key_user_tree);
  93. spin_unlock(&key_user_lock);
  94. user = candidate;
  95. goto out;
  96. /* okay - we found a user record for this UID */
  97. found:
  98. atomic_inc(&user->usage);
  99. spin_unlock(&key_user_lock);
  100. kfree(candidate);
  101. out:
  102. return user;
  103. } /* end key_user_lookup() */
  104. /*****************************************************************************/
  105. /*
  106. * dispose of a user structure
  107. */
  108. void key_user_put(struct key_user *user)
  109. {
  110. if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
  111. rb_erase(&user->node, &key_user_tree);
  112. spin_unlock(&key_user_lock);
  113. kfree(user);
  114. }
  115. } /* end key_user_put() */
  116. /*****************************************************************************/
  117. /*
  118. * insert a key with a fixed serial number
  119. */
  120. static void __init __key_insert_serial(struct key *key)
  121. {
  122. struct rb_node *parent, **p;
  123. struct key *xkey;
  124. parent = NULL;
  125. p = &key_serial_tree.rb_node;
  126. while (*p) {
  127. parent = *p;
  128. xkey = rb_entry(parent, struct key, serial_node);
  129. if (key->serial < xkey->serial)
  130. p = &(*p)->rb_left;
  131. else if (key->serial > xkey->serial)
  132. p = &(*p)->rb_right;
  133. else
  134. BUG();
  135. }
  136. /* we've found a suitable hole - arrange for this key to occupy it */
  137. rb_link_node(&key->serial_node, parent, p);
  138. rb_insert_color(&key->serial_node, &key_serial_tree);
  139. } /* end __key_insert_serial() */
  140. /*****************************************************************************/
  141. /*
  142. * assign a key the next unique serial number
  143. * - we work through all the serial numbers between 2 and 2^31-1 in turn and
  144. * then wrap
  145. */
  146. static inline void key_alloc_serial(struct key *key)
  147. {
  148. struct rb_node *parent, **p;
  149. struct key *xkey;
  150. spin_lock(&key_serial_lock);
  151. /* propose a likely serial number and look for a hole for it in the
  152. * serial number tree */
  153. key->serial = key_serial_next;
  154. if (key->serial < 3)
  155. key->serial = 3;
  156. key_serial_next = key->serial + 1;
  157. parent = NULL;
  158. p = &key_serial_tree.rb_node;
  159. while (*p) {
  160. parent = *p;
  161. xkey = rb_entry(parent, struct key, serial_node);
  162. if (key->serial < xkey->serial)
  163. p = &(*p)->rb_left;
  164. else if (key->serial > xkey->serial)
  165. p = &(*p)->rb_right;
  166. else
  167. goto serial_exists;
  168. }
  169. goto insert_here;
  170. /* we found a key with the proposed serial number - walk the tree from
  171. * that point looking for the next unused serial number */
  172. serial_exists:
  173. for (;;) {
  174. key->serial = key_serial_next;
  175. if (key->serial < 2)
  176. key->serial = 2;
  177. key_serial_next = key->serial + 1;
  178. if (!parent->rb_parent)
  179. p = &key_serial_tree.rb_node;
  180. else if (parent->rb_parent->rb_left == parent)
  181. p = &parent->rb_parent->rb_left;
  182. else
  183. p = &parent->rb_parent->rb_right;
  184. parent = rb_next(parent);
  185. if (!parent)
  186. break;
  187. xkey = rb_entry(parent, struct key, serial_node);
  188. if (key->serial < xkey->serial)
  189. goto insert_here;
  190. }
  191. /* we've found a suitable hole - arrange for this key to occupy it */
  192. insert_here:
  193. rb_link_node(&key->serial_node, parent, p);
  194. rb_insert_color(&key->serial_node, &key_serial_tree);
  195. spin_unlock(&key_serial_lock);
  196. } /* end key_alloc_serial() */
  197. /*****************************************************************************/
  198. /*
  199. * allocate a key of the specified type
  200. * - update the user's quota to reflect the existence of the key
  201. * - called from a key-type operation with key_types_sem read-locked by
  202. * key_create_or_update()
  203. * - this prevents unregistration of the key type
  204. * - upon return the key is as yet uninstantiated; the caller needs to either
  205. * instantiate the key or discard it before returning
  206. */
  207. struct key *key_alloc(struct key_type *type, const char *desc,
  208. uid_t uid, gid_t gid, key_perm_t perm,
  209. int not_in_quota)
  210. {
  211. struct key_user *user = NULL;
  212. struct key *key;
  213. size_t desclen, quotalen;
  214. int ret;
  215. key = ERR_PTR(-EINVAL);
  216. if (!desc || !*desc)
  217. goto error;
  218. desclen = strlen(desc) + 1;
  219. quotalen = desclen + type->def_datalen;
  220. /* get hold of the key tracking for this user */
  221. user = key_user_lookup(uid);
  222. if (!user)
  223. goto no_memory_1;
  224. /* check that the user's quota permits allocation of another key and
  225. * its description */
  226. if (!not_in_quota) {
  227. spin_lock(&user->lock);
  228. if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
  229. user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES
  230. )
  231. goto no_quota;
  232. user->qnkeys++;
  233. user->qnbytes += quotalen;
  234. spin_unlock(&user->lock);
  235. }
  236. /* allocate and initialise the key and its description */
  237. key = kmem_cache_alloc(key_jar, SLAB_KERNEL);
  238. if (!key)
  239. goto no_memory_2;
  240. if (desc) {
  241. key->description = kmalloc(desclen, GFP_KERNEL);
  242. if (!key->description)
  243. goto no_memory_3;
  244. memcpy(key->description, desc, desclen);
  245. }
  246. atomic_set(&key->usage, 1);
  247. init_rwsem(&key->sem);
  248. key->type = type;
  249. key->user = user;
  250. key->quotalen = quotalen;
  251. key->datalen = type->def_datalen;
  252. key->uid = uid;
  253. key->gid = gid;
  254. key->perm = perm;
  255. key->flags = 0;
  256. key->expiry = 0;
  257. key->payload.data = NULL;
  258. key->security = NULL;
  259. if (!not_in_quota)
  260. key->flags |= 1 << KEY_FLAG_IN_QUOTA;
  261. memset(&key->type_data, 0, sizeof(key->type_data));
  262. #ifdef KEY_DEBUGGING
  263. key->magic = KEY_DEBUG_MAGIC;
  264. #endif
  265. /* let the security module know about the key */
  266. ret = security_key_alloc(key);
  267. if (ret < 0)
  268. goto security_error;
  269. /* publish the key by giving it a serial number */
  270. atomic_inc(&user->nkeys);
  271. key_alloc_serial(key);
  272. error:
  273. return key;
  274. security_error:
  275. kfree(key->description);
  276. kmem_cache_free(key_jar, key);
  277. if (!not_in_quota) {
  278. spin_lock(&user->lock);
  279. user->qnkeys--;
  280. user->qnbytes -= quotalen;
  281. spin_unlock(&user->lock);
  282. }
  283. key_user_put(user);
  284. key = ERR_PTR(ret);
  285. goto error;
  286. no_memory_3:
  287. kmem_cache_free(key_jar, key);
  288. no_memory_2:
  289. if (!not_in_quota) {
  290. spin_lock(&user->lock);
  291. user->qnkeys--;
  292. user->qnbytes -= quotalen;
  293. spin_unlock(&user->lock);
  294. }
  295. key_user_put(user);
  296. no_memory_1:
  297. key = ERR_PTR(-ENOMEM);
  298. goto error;
  299. no_quota:
  300. spin_unlock(&user->lock);
  301. key_user_put(user);
  302. key = ERR_PTR(-EDQUOT);
  303. goto error;
  304. } /* end key_alloc() */
  305. EXPORT_SYMBOL(key_alloc);
  306. /*****************************************************************************/
  307. /*
  308. * reserve an amount of quota for the key's payload
  309. */
  310. int key_payload_reserve(struct key *key, size_t datalen)
  311. {
  312. int delta = (int) datalen - key->datalen;
  313. int ret = 0;
  314. key_check(key);
  315. /* contemplate the quota adjustment */
  316. if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  317. spin_lock(&key->user->lock);
  318. if (delta > 0 &&
  319. key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES
  320. ) {
  321. ret = -EDQUOT;
  322. }
  323. else {
  324. key->user->qnbytes += delta;
  325. key->quotalen += delta;
  326. }
  327. spin_unlock(&key->user->lock);
  328. }
  329. /* change the recorded data length if that didn't generate an error */
  330. if (ret == 0)
  331. key->datalen = datalen;
  332. return ret;
  333. } /* end key_payload_reserve() */
  334. EXPORT_SYMBOL(key_payload_reserve);
  335. /*****************************************************************************/
  336. /*
  337. * instantiate a key and link it into the target keyring atomically
  338. * - called with the target keyring's semaphore writelocked
  339. */
  340. static int __key_instantiate_and_link(struct key *key,
  341. const void *data,
  342. size_t datalen,
  343. struct key *keyring,
  344. struct key *instkey)
  345. {
  346. int ret, awaken;
  347. key_check(key);
  348. key_check(keyring);
  349. awaken = 0;
  350. ret = -EBUSY;
  351. down_write(&key_construction_sem);
  352. /* can't instantiate twice */
  353. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  354. /* instantiate the key */
  355. ret = key->type->instantiate(key, data, datalen);
  356. if (ret == 0) {
  357. /* mark the key as being instantiated */
  358. atomic_inc(&key->user->nikeys);
  359. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  360. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  361. awaken = 1;
  362. /* and link it into the destination keyring */
  363. if (keyring)
  364. ret = __key_link(keyring, key);
  365. /* disable the authorisation key */
  366. if (instkey)
  367. key_revoke(instkey);
  368. }
  369. }
  370. up_write(&key_construction_sem);
  371. /* wake up anyone waiting for a key to be constructed */
  372. if (awaken)
  373. wake_up_all(&request_key_conswq);
  374. return ret;
  375. } /* end __key_instantiate_and_link() */
  376. /*****************************************************************************/
  377. /*
  378. * instantiate a key and link it into the target keyring atomically
  379. */
  380. int key_instantiate_and_link(struct key *key,
  381. const void *data,
  382. size_t datalen,
  383. struct key *keyring,
  384. struct key *instkey)
  385. {
  386. int ret;
  387. if (keyring)
  388. down_write(&keyring->sem);
  389. ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey);
  390. if (keyring)
  391. up_write(&keyring->sem);
  392. return ret;
  393. } /* end key_instantiate_and_link() */
  394. EXPORT_SYMBOL(key_instantiate_and_link);
  395. /*****************************************************************************/
  396. /*
  397. * negatively instantiate a key and link it into the target keyring atomically
  398. */
  399. int key_negate_and_link(struct key *key,
  400. unsigned timeout,
  401. struct key *keyring,
  402. struct key *instkey)
  403. {
  404. struct timespec now;
  405. int ret, awaken;
  406. key_check(key);
  407. key_check(keyring);
  408. awaken = 0;
  409. ret = -EBUSY;
  410. if (keyring)
  411. down_write(&keyring->sem);
  412. down_write(&key_construction_sem);
  413. /* can't instantiate twice */
  414. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  415. /* mark the key as being negatively instantiated */
  416. atomic_inc(&key->user->nikeys);
  417. set_bit(KEY_FLAG_NEGATIVE, &key->flags);
  418. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  419. now = current_kernel_time();
  420. key->expiry = now.tv_sec + timeout;
  421. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  422. awaken = 1;
  423. ret = 0;
  424. /* and link it into the destination keyring */
  425. if (keyring)
  426. ret = __key_link(keyring, key);
  427. /* disable the authorisation key */
  428. if (instkey)
  429. key_revoke(instkey);
  430. }
  431. up_write(&key_construction_sem);
  432. if (keyring)
  433. up_write(&keyring->sem);
  434. /* wake up anyone waiting for a key to be constructed */
  435. if (awaken)
  436. wake_up_all(&request_key_conswq);
  437. return ret;
  438. } /* end key_negate_and_link() */
  439. EXPORT_SYMBOL(key_negate_and_link);
  440. /*****************************************************************************/
  441. /*
  442. * do cleaning up in process context so that we don't have to disable
  443. * interrupts all over the place
  444. */
  445. static void key_cleanup(void *data)
  446. {
  447. struct rb_node *_n;
  448. struct key *key;
  449. go_again:
  450. /* look for a dead key in the tree */
  451. spin_lock(&key_serial_lock);
  452. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  453. key = rb_entry(_n, struct key, serial_node);
  454. if (atomic_read(&key->usage) == 0)
  455. goto found_dead_key;
  456. }
  457. spin_unlock(&key_serial_lock);
  458. return;
  459. found_dead_key:
  460. /* we found a dead key - once we've removed it from the tree, we can
  461. * drop the lock */
  462. rb_erase(&key->serial_node, &key_serial_tree);
  463. spin_unlock(&key_serial_lock);
  464. key_check(key);
  465. security_key_free(key);
  466. /* deal with the user's key tracking and quota */
  467. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  468. spin_lock(&key->user->lock);
  469. key->user->qnkeys--;
  470. key->user->qnbytes -= key->quotalen;
  471. spin_unlock(&key->user->lock);
  472. }
  473. atomic_dec(&key->user->nkeys);
  474. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  475. atomic_dec(&key->user->nikeys);
  476. key_user_put(key->user);
  477. /* now throw away the key memory */
  478. if (key->type->destroy)
  479. key->type->destroy(key);
  480. kfree(key->description);
  481. #ifdef KEY_DEBUGGING
  482. key->magic = KEY_DEBUG_MAGIC_X;
  483. #endif
  484. kmem_cache_free(key_jar, key);
  485. /* there may, of course, be more than one key to destroy */
  486. goto go_again;
  487. } /* end key_cleanup() */
  488. /*****************************************************************************/
  489. /*
  490. * dispose of a reference to a key
  491. * - when all the references are gone, we schedule the cleanup task to come and
  492. * pull it out of the tree in definite process context
  493. */
  494. void key_put(struct key *key)
  495. {
  496. if (key) {
  497. key_check(key);
  498. if (atomic_dec_and_test(&key->usage))
  499. schedule_work(&key_cleanup_task);
  500. }
  501. } /* end key_put() */
  502. EXPORT_SYMBOL(key_put);
  503. /*****************************************************************************/
  504. /*
  505. * find a key by its serial number
  506. */
  507. struct key *key_lookup(key_serial_t id)
  508. {
  509. struct rb_node *n;
  510. struct key *key;
  511. spin_lock(&key_serial_lock);
  512. /* search the tree for the specified key */
  513. n = key_serial_tree.rb_node;
  514. while (n) {
  515. key = rb_entry(n, struct key, serial_node);
  516. if (id < key->serial)
  517. n = n->rb_left;
  518. else if (id > key->serial)
  519. n = n->rb_right;
  520. else
  521. goto found;
  522. }
  523. not_found:
  524. key = ERR_PTR(-ENOKEY);
  525. goto error;
  526. found:
  527. /* pretend it doesn't exist if it's dead */
  528. if (atomic_read(&key->usage) == 0 ||
  529. test_bit(KEY_FLAG_DEAD, &key->flags) ||
  530. key->type == &key_type_dead)
  531. goto not_found;
  532. /* this races with key_put(), but that doesn't matter since key_put()
  533. * doesn't actually change the key
  534. */
  535. atomic_inc(&key->usage);
  536. error:
  537. spin_unlock(&key_serial_lock);
  538. return key;
  539. } /* end key_lookup() */
  540. /*****************************************************************************/
  541. /*
  542. * find and lock the specified key type against removal
  543. * - we return with the sem readlocked
  544. */
  545. struct key_type *key_type_lookup(const char *type)
  546. {
  547. struct key_type *ktype;
  548. down_read(&key_types_sem);
  549. /* look up the key type to see if it's one of the registered kernel
  550. * types */
  551. list_for_each_entry(ktype, &key_types_list, link) {
  552. if (strcmp(ktype->name, type) == 0)
  553. goto found_kernel_type;
  554. }
  555. up_read(&key_types_sem);
  556. ktype = ERR_PTR(-ENOKEY);
  557. found_kernel_type:
  558. return ktype;
  559. } /* end key_type_lookup() */
  560. /*****************************************************************************/
  561. /*
  562. * unlock a key type
  563. */
  564. void key_type_put(struct key_type *ktype)
  565. {
  566. up_read(&key_types_sem);
  567. } /* end key_type_put() */
  568. /*****************************************************************************/
  569. /*
  570. * attempt to update an existing key
  571. * - the key has an incremented refcount
  572. * - we need to put the key if we get an error
  573. */
  574. static inline key_ref_t __key_update(key_ref_t key_ref,
  575. const void *payload, size_t plen)
  576. {
  577. struct key *key = key_ref_to_ptr(key_ref);
  578. int ret;
  579. /* need write permission on the key to update it */
  580. ret = key_permission(key_ref, KEY_WRITE);
  581. if (ret < 0)
  582. goto error;
  583. ret = -EEXIST;
  584. if (!key->type->update)
  585. goto error;
  586. down_write(&key->sem);
  587. ret = key->type->update(key, payload, plen);
  588. if (ret == 0)
  589. /* updating a negative key instantiates it */
  590. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  591. up_write(&key->sem);
  592. if (ret < 0)
  593. goto error;
  594. out:
  595. return key_ref;
  596. error:
  597. key_put(key);
  598. key_ref = ERR_PTR(ret);
  599. goto out;
  600. } /* end __key_update() */
  601. /*****************************************************************************/
  602. /*
  603. * search the specified keyring for a key of the same description; if one is
  604. * found, update it, otherwise add a new one
  605. */
  606. key_ref_t key_create_or_update(key_ref_t keyring_ref,
  607. const char *type,
  608. const char *description,
  609. const void *payload,
  610. size_t plen,
  611. int not_in_quota)
  612. {
  613. struct key_type *ktype;
  614. struct key *keyring, *key = NULL;
  615. key_perm_t perm;
  616. key_ref_t key_ref;
  617. int ret;
  618. /* look up the key type to see if it's one of the registered kernel
  619. * types */
  620. ktype = key_type_lookup(type);
  621. if (IS_ERR(ktype)) {
  622. key_ref = ERR_PTR(-ENODEV);
  623. goto error;
  624. }
  625. key_ref = ERR_PTR(-EINVAL);
  626. if (!ktype->match || !ktype->instantiate)
  627. goto error_2;
  628. keyring = key_ref_to_ptr(keyring_ref);
  629. key_check(keyring);
  630. key_ref = ERR_PTR(-ENOTDIR);
  631. if (keyring->type != &key_type_keyring)
  632. goto error_2;
  633. down_write(&keyring->sem);
  634. /* if we're going to allocate a new key, we're going to have
  635. * to modify the keyring */
  636. ret = key_permission(keyring_ref, KEY_WRITE);
  637. if (ret < 0) {
  638. key_ref = ERR_PTR(ret);
  639. goto error_3;
  640. }
  641. /* if it's possible to update this type of key, search for an existing
  642. * key of the same type and description in the destination keyring and
  643. * update that instead if possible
  644. */
  645. if (ktype->update) {
  646. key_ref = __keyring_search_one(keyring_ref, ktype, description,
  647. 0);
  648. if (!IS_ERR(key_ref))
  649. goto found_matching_key;
  650. }
  651. /* decide on the permissions we want */
  652. perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
  653. perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
  654. if (ktype->read)
  655. perm |= KEY_POS_READ | KEY_USR_READ;
  656. if (ktype == &key_type_keyring || ktype->update)
  657. perm |= KEY_USR_WRITE;
  658. /* allocate a new key */
  659. key = key_alloc(ktype, description, current->fsuid, current->fsgid,
  660. perm, not_in_quota);
  661. if (IS_ERR(key)) {
  662. key_ref = ERR_PTR(PTR_ERR(key));
  663. goto error_3;
  664. }
  665. /* instantiate it and link it into the target keyring */
  666. ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
  667. if (ret < 0) {
  668. key_put(key);
  669. key_ref = ERR_PTR(ret);
  670. goto error_3;
  671. }
  672. key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
  673. error_3:
  674. up_write(&keyring->sem);
  675. error_2:
  676. key_type_put(ktype);
  677. error:
  678. return key_ref;
  679. found_matching_key:
  680. /* we found a matching key, so we're going to try to update it
  681. * - we can drop the locks first as we have the key pinned
  682. */
  683. up_write(&keyring->sem);
  684. key_type_put(ktype);
  685. key_ref = __key_update(key_ref, payload, plen);
  686. goto error;
  687. } /* end key_create_or_update() */
  688. EXPORT_SYMBOL(key_create_or_update);
  689. /*****************************************************************************/
  690. /*
  691. * update a key
  692. */
  693. int key_update(key_ref_t key_ref, const void *payload, size_t plen)
  694. {
  695. struct key *key = key_ref_to_ptr(key_ref);
  696. int ret;
  697. key_check(key);
  698. /* the key must be writable */
  699. ret = key_permission(key_ref, KEY_WRITE);
  700. if (ret < 0)
  701. goto error;
  702. /* attempt to update it if supported */
  703. ret = -EOPNOTSUPP;
  704. if (key->type->update) {
  705. down_write(&key->sem);
  706. ret = key->type->update(key, payload, plen);
  707. if (ret == 0)
  708. /* updating a negative key instantiates it */
  709. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  710. up_write(&key->sem);
  711. }
  712. error:
  713. return ret;
  714. } /* end key_update() */
  715. EXPORT_SYMBOL(key_update);
  716. /*****************************************************************************/
  717. /*
  718. * revoke a key
  719. */
  720. void key_revoke(struct key *key)
  721. {
  722. key_check(key);
  723. /* make sure no one's trying to change or use the key when we mark
  724. * it */
  725. down_write(&key->sem);
  726. set_bit(KEY_FLAG_REVOKED, &key->flags);
  727. up_write(&key->sem);
  728. } /* end key_revoke() */
  729. EXPORT_SYMBOL(key_revoke);
  730. /*****************************************************************************/
  731. /*
  732. * register a type of key
  733. */
  734. int register_key_type(struct key_type *ktype)
  735. {
  736. struct key_type *p;
  737. int ret;
  738. ret = -EEXIST;
  739. down_write(&key_types_sem);
  740. /* disallow key types with the same name */
  741. list_for_each_entry(p, &key_types_list, link) {
  742. if (strcmp(p->name, ktype->name) == 0)
  743. goto out;
  744. }
  745. /* store the type */
  746. list_add(&ktype->link, &key_types_list);
  747. ret = 0;
  748. out:
  749. up_write(&key_types_sem);
  750. return ret;
  751. } /* end register_key_type() */
  752. EXPORT_SYMBOL(register_key_type);
  753. /*****************************************************************************/
  754. /*
  755. * unregister a type of key
  756. */
  757. void unregister_key_type(struct key_type *ktype)
  758. {
  759. struct rb_node *_n;
  760. struct key *key;
  761. down_write(&key_types_sem);
  762. /* withdraw the key type */
  763. list_del_init(&ktype->link);
  764. /* mark all the keys of this type dead */
  765. spin_lock(&key_serial_lock);
  766. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  767. key = rb_entry(_n, struct key, serial_node);
  768. if (key->type == ktype)
  769. key->type = &key_type_dead;
  770. }
  771. spin_unlock(&key_serial_lock);
  772. /* make sure everyone revalidates their keys */
  773. synchronize_rcu();
  774. /* we should now be able to destroy the payloads of all the keys of
  775. * this type with impunity */
  776. spin_lock(&key_serial_lock);
  777. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  778. key = rb_entry(_n, struct key, serial_node);
  779. if (key->type == ktype) {
  780. if (ktype->destroy)
  781. ktype->destroy(key);
  782. memset(&key->payload, 0xbd, sizeof(key->payload));
  783. }
  784. }
  785. spin_unlock(&key_serial_lock);
  786. up_write(&key_types_sem);
  787. } /* end unregister_key_type() */
  788. EXPORT_SYMBOL(unregister_key_type);
  789. /*****************************************************************************/
  790. /*
  791. * initialise the key management stuff
  792. */
  793. void __init key_init(void)
  794. {
  795. /* allocate a slab in which we can store keys */
  796. key_jar = kmem_cache_create("key_jar", sizeof(struct key),
  797. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  798. /* add the special key types */
  799. list_add_tail(&key_type_keyring.link, &key_types_list);
  800. list_add_tail(&key_type_dead.link, &key_types_list);
  801. list_add_tail(&key_type_user.link, &key_types_list);
  802. /* record the root user tracking */
  803. rb_link_node(&root_key_user.node,
  804. NULL,
  805. &key_user_tree.rb_node);
  806. rb_insert_color(&root_key_user.node,
  807. &key_user_tree);
  808. /* record root's user standard keyrings */
  809. key_check(&root_user_keyring);
  810. key_check(&root_session_keyring);
  811. __key_insert_serial(&root_user_keyring);
  812. __key_insert_serial(&root_session_keyring);
  813. keyring_publish_name(&root_user_keyring);
  814. keyring_publish_name(&root_session_keyring);
  815. /* link the two root keyrings together */
  816. key_link(&root_session_keyring, &root_user_keyring);
  817. } /* end key_init() */