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