key.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. 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. 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. /* search for an existing key of the same type and description in the
  639. * destination keyring
  640. */
  641. key_ref = __keyring_search_one(keyring_ref, ktype, description, 0);
  642. if (!IS_ERR(key_ref))
  643. goto found_matching_key;
  644. /* decide on the permissions we want */
  645. perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
  646. perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
  647. if (ktype->read)
  648. perm |= KEY_POS_READ | KEY_USR_READ;
  649. if (ktype == &key_type_keyring || ktype->update)
  650. perm |= KEY_USR_WRITE;
  651. /* allocate a new key */
  652. key = key_alloc(ktype, description, current->fsuid, current->fsgid,
  653. perm, not_in_quota);
  654. if (IS_ERR(key)) {
  655. key_ref = ERR_PTR(PTR_ERR(key));
  656. goto error_3;
  657. }
  658. /* instantiate it and link it into the target keyring */
  659. ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL);
  660. if (ret < 0) {
  661. key_put(key);
  662. key_ref = ERR_PTR(ret);
  663. goto error_3;
  664. }
  665. key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
  666. error_3:
  667. up_write(&keyring->sem);
  668. error_2:
  669. key_type_put(ktype);
  670. error:
  671. return key_ref;
  672. found_matching_key:
  673. /* we found a matching key, so we're going to try to update it
  674. * - we can drop the locks first as we have the key pinned
  675. */
  676. up_write(&keyring->sem);
  677. key_type_put(ktype);
  678. key_ref = __key_update(key_ref, payload, plen);
  679. goto error;
  680. } /* end key_create_or_update() */
  681. EXPORT_SYMBOL(key_create_or_update);
  682. /*****************************************************************************/
  683. /*
  684. * update a key
  685. */
  686. int key_update(key_ref_t key_ref, const void *payload, size_t plen)
  687. {
  688. struct key *key = key_ref_to_ptr(key_ref);
  689. int ret;
  690. key_check(key);
  691. /* the key must be writable */
  692. ret = key_permission(key_ref, KEY_WRITE);
  693. if (ret < 0)
  694. goto error;
  695. /* attempt to update it if supported */
  696. ret = -EOPNOTSUPP;
  697. if (key->type->update) {
  698. down_write(&key->sem);
  699. ret = key->type->update(key, payload, plen);
  700. if (ret == 0)
  701. /* updating a negative key instantiates it */
  702. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  703. up_write(&key->sem);
  704. }
  705. error:
  706. return ret;
  707. } /* end key_update() */
  708. EXPORT_SYMBOL(key_update);
  709. /*****************************************************************************/
  710. /*
  711. * revoke a key
  712. */
  713. void key_revoke(struct key *key)
  714. {
  715. key_check(key);
  716. /* make sure no one's trying to change or use the key when we mark
  717. * it */
  718. down_write(&key->sem);
  719. set_bit(KEY_FLAG_REVOKED, &key->flags);
  720. up_write(&key->sem);
  721. } /* end key_revoke() */
  722. EXPORT_SYMBOL(key_revoke);
  723. /*****************************************************************************/
  724. /*
  725. * register a type of key
  726. */
  727. int register_key_type(struct key_type *ktype)
  728. {
  729. struct key_type *p;
  730. int ret;
  731. ret = -EEXIST;
  732. down_write(&key_types_sem);
  733. /* disallow key types with the same name */
  734. list_for_each_entry(p, &key_types_list, link) {
  735. if (strcmp(p->name, ktype->name) == 0)
  736. goto out;
  737. }
  738. /* store the type */
  739. list_add(&ktype->link, &key_types_list);
  740. ret = 0;
  741. out:
  742. up_write(&key_types_sem);
  743. return ret;
  744. } /* end register_key_type() */
  745. EXPORT_SYMBOL(register_key_type);
  746. /*****************************************************************************/
  747. /*
  748. * unregister a type of key
  749. */
  750. void unregister_key_type(struct key_type *ktype)
  751. {
  752. struct rb_node *_n;
  753. struct key *key;
  754. down_write(&key_types_sem);
  755. /* withdraw the key type */
  756. list_del_init(&ktype->link);
  757. /* mark all the keys of this type dead */
  758. spin_lock(&key_serial_lock);
  759. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  760. key = rb_entry(_n, struct key, serial_node);
  761. if (key->type == ktype)
  762. key->type = &key_type_dead;
  763. }
  764. spin_unlock(&key_serial_lock);
  765. /* make sure everyone revalidates their keys */
  766. synchronize_rcu();
  767. /* we should now be able to destroy the payloads of all the keys of
  768. * this type with impunity */
  769. spin_lock(&key_serial_lock);
  770. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  771. key = rb_entry(_n, struct key, serial_node);
  772. if (key->type == ktype) {
  773. if (ktype->destroy)
  774. ktype->destroy(key);
  775. memset(&key->payload, 0xbd, sizeof(key->payload));
  776. }
  777. }
  778. spin_unlock(&key_serial_lock);
  779. up_write(&key_types_sem);
  780. } /* end unregister_key_type() */
  781. EXPORT_SYMBOL(unregister_key_type);
  782. /*****************************************************************************/
  783. /*
  784. * initialise the key management stuff
  785. */
  786. void __init key_init(void)
  787. {
  788. /* allocate a slab in which we can store keys */
  789. key_jar = kmem_cache_create("key_jar", sizeof(struct key),
  790. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  791. /* add the special key types */
  792. list_add_tail(&key_type_keyring.link, &key_types_list);
  793. list_add_tail(&key_type_dead.link, &key_types_list);
  794. list_add_tail(&key_type_user.link, &key_types_list);
  795. /* record the root user tracking */
  796. rb_link_node(&root_key_user.node,
  797. NULL,
  798. &key_user_tree.rb_node);
  799. rb_insert_color(&root_key_user.node,
  800. &key_user_tree);
  801. /* record root's user standard keyrings */
  802. key_check(&root_user_keyring);
  803. key_check(&root_session_keyring);
  804. __key_insert_serial(&root_user_keyring);
  805. __key_insert_serial(&root_session_keyring);
  806. keyring_publish_name(&root_user_keyring);
  807. keyring_publish_name(&root_session_keyring);
  808. /* link the two root keyrings together */
  809. key_link(&root_session_keyring, &root_user_keyring);
  810. } /* end key_init() */