key.c 25 KB

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