key.c 24 KB

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