key.c 24 KB

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