key.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /* Basic authentication token and access key management
  2. *
  3. * Copyright (C) 2004-2008 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 <linux/user_namespace.h>
  21. #include "internal.h"
  22. struct kmem_cache *key_jar;
  23. struct rb_root key_serial_tree; /* tree of keys indexed by serial */
  24. DEFINE_SPINLOCK(key_serial_lock);
  25. struct rb_root key_user_tree; /* tree of quota records indexed by UID */
  26. DEFINE_SPINLOCK(key_user_lock);
  27. unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */
  28. unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */
  29. unsigned int key_quota_maxkeys = 200; /* general key count quota */
  30. unsigned int key_quota_maxbytes = 20000; /* general key space quota */
  31. static LIST_HEAD(key_types_list);
  32. static DECLARE_RWSEM(key_types_sem);
  33. /* We serialise key instantiation and link */
  34. DEFINE_MUTEX(key_construction_mutex);
  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. * Get the key quota record for a user, allocating a new record if one doesn't
  45. * already exist.
  46. */
  47. struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
  48. {
  49. struct key_user *candidate = NULL, *user;
  50. struct rb_node *parent = NULL;
  51. struct rb_node **p;
  52. try_again:
  53. p = &key_user_tree.rb_node;
  54. spin_lock(&key_user_lock);
  55. /* search the tree for a user record with a matching UID */
  56. while (*p) {
  57. parent = *p;
  58. user = rb_entry(parent, struct key_user, node);
  59. if (uid < user->uid)
  60. p = &(*p)->rb_left;
  61. else if (uid > user->uid)
  62. p = &(*p)->rb_right;
  63. else if (user_ns < user->user_ns)
  64. p = &(*p)->rb_left;
  65. else if (user_ns > user->user_ns)
  66. p = &(*p)->rb_right;
  67. else
  68. goto found;
  69. }
  70. /* if we get here, we failed to find a match in the tree */
  71. if (!candidate) {
  72. /* allocate a candidate user record if we don't already have
  73. * one */
  74. spin_unlock(&key_user_lock);
  75. user = NULL;
  76. candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
  77. if (unlikely(!candidate))
  78. goto out;
  79. /* the allocation may have scheduled, so we need to repeat the
  80. * search lest someone else added the record whilst we were
  81. * asleep */
  82. goto try_again;
  83. }
  84. /* if we get here, then the user record still hadn't appeared on the
  85. * second pass - so we use the candidate record */
  86. atomic_set(&candidate->usage, 1);
  87. atomic_set(&candidate->nkeys, 0);
  88. atomic_set(&candidate->nikeys, 0);
  89. candidate->uid = uid;
  90. candidate->user_ns = get_user_ns(user_ns);
  91. candidate->qnkeys = 0;
  92. candidate->qnbytes = 0;
  93. spin_lock_init(&candidate->lock);
  94. mutex_init(&candidate->cons_lock);
  95. rb_link_node(&candidate->node, parent, p);
  96. rb_insert_color(&candidate->node, &key_user_tree);
  97. spin_unlock(&key_user_lock);
  98. user = candidate;
  99. goto out;
  100. /* okay - we found a user record for this UID */
  101. found:
  102. atomic_inc(&user->usage);
  103. spin_unlock(&key_user_lock);
  104. kfree(candidate);
  105. out:
  106. return user;
  107. }
  108. /*
  109. * Dispose of a user structure
  110. */
  111. void key_user_put(struct key_user *user)
  112. {
  113. if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
  114. rb_erase(&user->node, &key_user_tree);
  115. spin_unlock(&key_user_lock);
  116. put_user_ns(user->user_ns);
  117. kfree(user);
  118. }
  119. }
  120. /*
  121. * Allocate a serial number for a key. These are assigned randomly to avoid
  122. * security issues through covert channel problems.
  123. */
  124. static inline void key_alloc_serial(struct key *key)
  125. {
  126. struct rb_node *parent, **p;
  127. struct key *xkey;
  128. /* propose a random serial number and look for a hole for it in the
  129. * serial number tree */
  130. do {
  131. get_random_bytes(&key->serial, sizeof(key->serial));
  132. key->serial >>= 1; /* negative numbers are not permitted */
  133. } while (key->serial < 3);
  134. spin_lock(&key_serial_lock);
  135. attempt_insertion:
  136. parent = NULL;
  137. p = &key_serial_tree.rb_node;
  138. while (*p) {
  139. parent = *p;
  140. xkey = rb_entry(parent, struct key, serial_node);
  141. if (key->serial < xkey->serial)
  142. p = &(*p)->rb_left;
  143. else if (key->serial > xkey->serial)
  144. p = &(*p)->rb_right;
  145. else
  146. goto serial_exists;
  147. }
  148. /* we've found a suitable hole - arrange for this key to occupy it */
  149. rb_link_node(&key->serial_node, parent, p);
  150. rb_insert_color(&key->serial_node, &key_serial_tree);
  151. spin_unlock(&key_serial_lock);
  152. return;
  153. /* we found a key with the proposed serial number - walk the tree from
  154. * that point looking for the next unused serial number */
  155. serial_exists:
  156. for (;;) {
  157. key->serial++;
  158. if (key->serial < 3) {
  159. key->serial = 3;
  160. goto attempt_insertion;
  161. }
  162. parent = rb_next(parent);
  163. if (!parent)
  164. goto attempt_insertion;
  165. xkey = rb_entry(parent, struct key, serial_node);
  166. if (key->serial < xkey->serial)
  167. goto attempt_insertion;
  168. }
  169. }
  170. /**
  171. * key_alloc - Allocate a key of the specified type.
  172. * @type: The type of key to allocate.
  173. * @desc: The key description to allow the key to be searched out.
  174. * @uid: The owner of the new key.
  175. * @gid: The group ID for the new key's group permissions.
  176. * @cred: The credentials specifying UID namespace.
  177. * @perm: The permissions mask of the new key.
  178. * @flags: Flags specifying quota properties.
  179. *
  180. * Allocate a key of the specified type with the attributes given. The key is
  181. * returned in an uninstantiated state and the caller needs to instantiate the
  182. * key before returning.
  183. *
  184. * The user's key count quota is updated to reflect the creation of the key and
  185. * the user's key data quota has the default for the key type reserved. The
  186. * instantiation function should amend this as necessary. If insufficient
  187. * quota is available, -EDQUOT will be returned.
  188. *
  189. * The LSM security modules can prevent a key being created, in which case
  190. * -EACCES will be returned.
  191. *
  192. * Returns a pointer to the new key if successful and an error code otherwise.
  193. *
  194. * Note that the caller needs to ensure the key type isn't uninstantiated.
  195. * Internally this can be done by locking key_types_sem. Externally, this can
  196. * be done by either never unregistering the key type, or making sure
  197. * key_alloc() calls don't race with module unloading.
  198. */
  199. struct key *key_alloc(struct key_type *type, const char *desc,
  200. uid_t uid, gid_t gid, const struct cred *cred,
  201. key_perm_t perm, unsigned long flags)
  202. {
  203. struct key_user *user = NULL;
  204. struct key *key;
  205. size_t desclen, quotalen;
  206. int ret;
  207. key = ERR_PTR(-EINVAL);
  208. if (!desc || !*desc)
  209. goto error;
  210. if (type->vet_description) {
  211. ret = type->vet_description(desc);
  212. if (ret < 0) {
  213. key = ERR_PTR(ret);
  214. goto error;
  215. }
  216. }
  217. desclen = strlen(desc) + 1;
  218. quotalen = desclen + type->def_datalen;
  219. /* get hold of the key tracking for this user */
  220. user = key_user_lookup(uid, cred->user_ns);
  221. if (!user)
  222. goto no_memory_1;
  223. /* check that the user's quota permits allocation of another key and
  224. * its description */
  225. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  226. unsigned maxkeys = (uid == 0) ?
  227. key_quota_root_maxkeys : key_quota_maxkeys;
  228. unsigned maxbytes = (uid == 0) ?
  229. key_quota_root_maxbytes : key_quota_maxbytes;
  230. spin_lock(&user->lock);
  231. if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
  232. if (user->qnkeys + 1 >= maxkeys ||
  233. user->qnbytes + quotalen >= maxbytes ||
  234. user->qnbytes + quotalen < user->qnbytes)
  235. goto no_quota;
  236. }
  237. user->qnkeys++;
  238. user->qnbytes += quotalen;
  239. spin_unlock(&user->lock);
  240. }
  241. /* allocate and initialise the key and its description */
  242. key = kmem_cache_alloc(key_jar, GFP_KERNEL);
  243. if (!key)
  244. goto no_memory_2;
  245. if (desc) {
  246. key->description = kmemdup(desc, desclen, GFP_KERNEL);
  247. if (!key->description)
  248. goto no_memory_3;
  249. }
  250. atomic_set(&key->usage, 1);
  251. init_rwsem(&key->sem);
  252. lockdep_set_class(&key->sem, &type->lock_class);
  253. key->type = type;
  254. key->user = user;
  255. key->quotalen = quotalen;
  256. key->datalen = type->def_datalen;
  257. key->uid = uid;
  258. key->gid = gid;
  259. key->perm = perm;
  260. key->flags = 0;
  261. key->expiry = 0;
  262. key->payload.data = NULL;
  263. key->security = NULL;
  264. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
  265. key->flags |= 1 << KEY_FLAG_IN_QUOTA;
  266. memset(&key->type_data, 0, sizeof(key->type_data));
  267. #ifdef KEY_DEBUGGING
  268. key->magic = KEY_DEBUG_MAGIC;
  269. #endif
  270. /* let the security module know about the key */
  271. ret = security_key_alloc(key, cred, flags);
  272. if (ret < 0)
  273. goto security_error;
  274. /* publish the key by giving it a serial number */
  275. atomic_inc(&user->nkeys);
  276. key_alloc_serial(key);
  277. error:
  278. return key;
  279. security_error:
  280. kfree(key->description);
  281. kmem_cache_free(key_jar, key);
  282. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  283. spin_lock(&user->lock);
  284. user->qnkeys--;
  285. user->qnbytes -= quotalen;
  286. spin_unlock(&user->lock);
  287. }
  288. key_user_put(user);
  289. key = ERR_PTR(ret);
  290. goto error;
  291. no_memory_3:
  292. kmem_cache_free(key_jar, key);
  293. no_memory_2:
  294. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  295. spin_lock(&user->lock);
  296. user->qnkeys--;
  297. user->qnbytes -= quotalen;
  298. spin_unlock(&user->lock);
  299. }
  300. key_user_put(user);
  301. no_memory_1:
  302. key = ERR_PTR(-ENOMEM);
  303. goto error;
  304. no_quota:
  305. spin_unlock(&user->lock);
  306. key_user_put(user);
  307. key = ERR_PTR(-EDQUOT);
  308. goto error;
  309. }
  310. EXPORT_SYMBOL(key_alloc);
  311. /**
  312. * key_payload_reserve - Adjust data quota reservation for the key's payload
  313. * @key: The key to make the reservation for.
  314. * @datalen: The amount of data payload the caller now wants.
  315. *
  316. * Adjust the amount of the owning user's key data quota that a key reserves.
  317. * If the amount is increased, then -EDQUOT may be returned if there isn't
  318. * enough free quota available.
  319. *
  320. * If successful, 0 is returned.
  321. */
  322. int key_payload_reserve(struct key *key, size_t datalen)
  323. {
  324. int delta = (int)datalen - key->datalen;
  325. int ret = 0;
  326. key_check(key);
  327. /* contemplate the quota adjustment */
  328. if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  329. unsigned maxbytes = (key->user->uid == 0) ?
  330. key_quota_root_maxbytes : key_quota_maxbytes;
  331. spin_lock(&key->user->lock);
  332. if (delta > 0 &&
  333. (key->user->qnbytes + delta > maxbytes ||
  334. key->user->qnbytes + delta < key->user->qnbytes)) {
  335. ret = -EDQUOT;
  336. }
  337. else {
  338. key->user->qnbytes += delta;
  339. key->quotalen += delta;
  340. }
  341. spin_unlock(&key->user->lock);
  342. }
  343. /* change the recorded data length if that didn't generate an error */
  344. if (ret == 0)
  345. key->datalen = datalen;
  346. return ret;
  347. }
  348. EXPORT_SYMBOL(key_payload_reserve);
  349. /*
  350. * Instantiate a key and link it into the target keyring atomically. Must be
  351. * called with the target keyring's semaphore writelocked. The target key's
  352. * semaphore need not be locked as instantiation is serialised by
  353. * key_construction_mutex.
  354. */
  355. static int __key_instantiate_and_link(struct key *key,
  356. const void *data,
  357. size_t datalen,
  358. struct key *keyring,
  359. struct key *authkey,
  360. unsigned long *_prealloc)
  361. {
  362. int ret, awaken;
  363. key_check(key);
  364. key_check(keyring);
  365. awaken = 0;
  366. ret = -EBUSY;
  367. mutex_lock(&key_construction_mutex);
  368. /* can't instantiate twice */
  369. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  370. /* instantiate the key */
  371. ret = key->type->instantiate(key, data, datalen);
  372. if (ret == 0) {
  373. /* mark the key as being instantiated */
  374. atomic_inc(&key->user->nikeys);
  375. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  376. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  377. awaken = 1;
  378. /* and link it into the destination keyring */
  379. if (keyring)
  380. __key_link(keyring, key, _prealloc);
  381. /* disable the authorisation key */
  382. if (authkey)
  383. key_revoke(authkey);
  384. }
  385. }
  386. mutex_unlock(&key_construction_mutex);
  387. /* wake up anyone waiting for a key to be constructed */
  388. if (awaken)
  389. wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
  390. return ret;
  391. }
  392. /**
  393. * key_instantiate_and_link - Instantiate a key and link it into the keyring.
  394. * @key: The key to instantiate.
  395. * @data: The data to use to instantiate the keyring.
  396. * @datalen: The length of @data.
  397. * @keyring: Keyring to create a link in on success (or NULL).
  398. * @authkey: The authorisation token permitting instantiation.
  399. *
  400. * Instantiate a key that's in the uninstantiated state using the provided data
  401. * and, if successful, link it in to the destination keyring if one is
  402. * supplied.
  403. *
  404. * If successful, 0 is returned, the authorisation token is revoked and anyone
  405. * waiting for the key is woken up. If the key was already instantiated,
  406. * -EBUSY will be returned.
  407. */
  408. int key_instantiate_and_link(struct key *key,
  409. const void *data,
  410. size_t datalen,
  411. struct key *keyring,
  412. struct key *authkey)
  413. {
  414. unsigned long prealloc;
  415. int ret;
  416. if (keyring) {
  417. ret = __key_link_begin(keyring, key->type, key->description,
  418. &prealloc);
  419. if (ret < 0)
  420. return ret;
  421. }
  422. ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey,
  423. &prealloc);
  424. if (keyring)
  425. __key_link_end(keyring, key->type, prealloc);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(key_instantiate_and_link);
  429. /**
  430. * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
  431. * @key: The key to instantiate.
  432. * @timeout: The timeout on the negative key.
  433. * @error: The error to return when the key is hit.
  434. * @keyring: Keyring to create a link in on success (or NULL).
  435. * @authkey: The authorisation token permitting instantiation.
  436. *
  437. * Negatively instantiate a key that's in the uninstantiated state and, if
  438. * successful, set its timeout and stored error and link it in to the
  439. * destination keyring if one is supplied. The key and any links to the key
  440. * will be automatically garbage collected after the timeout expires.
  441. *
  442. * Negative keys are used to rate limit repeated request_key() calls by causing
  443. * them to return the stored error code (typically ENOKEY) until the negative
  444. * key expires.
  445. *
  446. * If successful, 0 is returned, the authorisation token is revoked and anyone
  447. * waiting for the key is woken up. If the key was already instantiated,
  448. * -EBUSY will be returned.
  449. */
  450. int key_reject_and_link(struct key *key,
  451. unsigned timeout,
  452. unsigned error,
  453. struct key *keyring,
  454. struct key *authkey)
  455. {
  456. unsigned long prealloc;
  457. struct timespec now;
  458. int ret, awaken, link_ret = 0;
  459. key_check(key);
  460. key_check(keyring);
  461. awaken = 0;
  462. ret = -EBUSY;
  463. if (keyring)
  464. link_ret = __key_link_begin(keyring, key->type,
  465. key->description, &prealloc);
  466. mutex_lock(&key_construction_mutex);
  467. /* can't instantiate twice */
  468. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  469. /* mark the key as being negatively instantiated */
  470. atomic_inc(&key->user->nikeys);
  471. set_bit(KEY_FLAG_NEGATIVE, &key->flags);
  472. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  473. key->type_data.reject_error = -error;
  474. now = current_kernel_time();
  475. key->expiry = now.tv_sec + timeout;
  476. key_schedule_gc(key->expiry + key_gc_delay);
  477. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  478. awaken = 1;
  479. ret = 0;
  480. /* and link it into the destination keyring */
  481. if (keyring && link_ret == 0)
  482. __key_link(keyring, key, &prealloc);
  483. /* disable the authorisation key */
  484. if (authkey)
  485. key_revoke(authkey);
  486. }
  487. mutex_unlock(&key_construction_mutex);
  488. if (keyring && link_ret == 0)
  489. __key_link_end(keyring, key->type, prealloc);
  490. /* wake up anyone waiting for a key to be constructed */
  491. if (awaken)
  492. wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
  493. return ret == 0 ? link_ret : ret;
  494. }
  495. EXPORT_SYMBOL(key_reject_and_link);
  496. /**
  497. * key_put - Discard a reference to a key.
  498. * @key: The key to discard a reference from.
  499. *
  500. * Discard a reference to a key, and when all the references are gone, we
  501. * schedule the cleanup task to come and pull it out of the tree in process
  502. * context at some later time.
  503. */
  504. void key_put(struct key *key)
  505. {
  506. if (key) {
  507. key_check(key);
  508. if (atomic_dec_and_test(&key->usage))
  509. queue_work(system_nrt_wq, &key_gc_work);
  510. }
  511. }
  512. EXPORT_SYMBOL(key_put);
  513. /*
  514. * Find a key by its serial number.
  515. */
  516. struct key *key_lookup(key_serial_t id)
  517. {
  518. struct rb_node *n;
  519. struct key *key;
  520. spin_lock(&key_serial_lock);
  521. /* search the tree for the specified key */
  522. n = key_serial_tree.rb_node;
  523. while (n) {
  524. key = rb_entry(n, struct key, serial_node);
  525. if (id < key->serial)
  526. n = n->rb_left;
  527. else if (id > key->serial)
  528. n = n->rb_right;
  529. else
  530. goto found;
  531. }
  532. not_found:
  533. key = ERR_PTR(-ENOKEY);
  534. goto error;
  535. found:
  536. /* pretend it doesn't exist if it is awaiting deletion */
  537. if (atomic_read(&key->usage) == 0)
  538. goto not_found;
  539. /* this races with key_put(), but that doesn't matter since key_put()
  540. * doesn't actually change the key
  541. */
  542. atomic_inc(&key->usage);
  543. error:
  544. spin_unlock(&key_serial_lock);
  545. return key;
  546. }
  547. /*
  548. * Find and lock the specified key type against removal.
  549. *
  550. * We return with the sem read-locked if successful. If the type wasn't
  551. * available -ENOKEY is returned instead.
  552. */
  553. struct key_type *key_type_lookup(const char *type)
  554. {
  555. struct key_type *ktype;
  556. down_read(&key_types_sem);
  557. /* look up the key type to see if it's one of the registered kernel
  558. * types */
  559. list_for_each_entry(ktype, &key_types_list, link) {
  560. if (strcmp(ktype->name, type) == 0)
  561. goto found_kernel_type;
  562. }
  563. up_read(&key_types_sem);
  564. ktype = ERR_PTR(-ENOKEY);
  565. found_kernel_type:
  566. return ktype;
  567. }
  568. void key_set_timeout(struct key *key, unsigned timeout)
  569. {
  570. struct timespec now;
  571. time_t expiry = 0;
  572. /* make the changes with the locks held to prevent races */
  573. down_write(&key->sem);
  574. if (timeout > 0) {
  575. now = current_kernel_time();
  576. expiry = now.tv_sec + timeout;
  577. }
  578. key->expiry = expiry;
  579. key_schedule_gc(key->expiry + key_gc_delay);
  580. up_write(&key->sem);
  581. }
  582. EXPORT_SYMBOL_GPL(key_set_timeout);
  583. /*
  584. * Unlock a key type locked by key_type_lookup().
  585. */
  586. void key_type_put(struct key_type *ktype)
  587. {
  588. up_read(&key_types_sem);
  589. }
  590. /*
  591. * Attempt to update an existing key.
  592. *
  593. * The key is given to us with an incremented refcount that we need to discard
  594. * if we get an error.
  595. */
  596. static inline key_ref_t __key_update(key_ref_t key_ref,
  597. const void *payload, size_t plen)
  598. {
  599. struct key *key = key_ref_to_ptr(key_ref);
  600. int ret;
  601. /* need write permission on the key to update it */
  602. ret = key_permission(key_ref, KEY_WRITE);
  603. if (ret < 0)
  604. goto error;
  605. ret = -EEXIST;
  606. if (!key->type->update)
  607. goto error;
  608. down_write(&key->sem);
  609. ret = key->type->update(key, payload, plen);
  610. if (ret == 0)
  611. /* updating a negative key instantiates it */
  612. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  613. up_write(&key->sem);
  614. if (ret < 0)
  615. goto error;
  616. out:
  617. return key_ref;
  618. error:
  619. key_put(key);
  620. key_ref = ERR_PTR(ret);
  621. goto out;
  622. }
  623. /**
  624. * key_create_or_update - Update or create and instantiate a key.
  625. * @keyring_ref: A pointer to the destination keyring with possession flag.
  626. * @type: The type of key.
  627. * @description: The searchable description for the key.
  628. * @payload: The data to use to instantiate or update the key.
  629. * @plen: The length of @payload.
  630. * @perm: The permissions mask for a new key.
  631. * @flags: The quota flags for a new key.
  632. *
  633. * Search the destination keyring for a key of the same description and if one
  634. * is found, update it, otherwise create and instantiate a new one and create a
  635. * link to it from that keyring.
  636. *
  637. * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
  638. * concocted.
  639. *
  640. * Returns a pointer to the new key if successful, -ENODEV if the key type
  641. * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
  642. * caller isn't permitted to modify the keyring or the LSM did not permit
  643. * creation of the key.
  644. *
  645. * On success, the possession flag from the keyring ref will be tacked on to
  646. * the key ref before it is returned.
  647. */
  648. key_ref_t key_create_or_update(key_ref_t keyring_ref,
  649. const char *type,
  650. const char *description,
  651. const void *payload,
  652. size_t plen,
  653. key_perm_t perm,
  654. unsigned long flags)
  655. {
  656. unsigned long prealloc;
  657. const struct cred *cred = current_cred();
  658. struct key_type *ktype;
  659. struct key *keyring, *key = NULL;
  660. key_ref_t key_ref;
  661. int ret;
  662. /* look up the key type to see if it's one of the registered kernel
  663. * types */
  664. ktype = key_type_lookup(type);
  665. if (IS_ERR(ktype)) {
  666. key_ref = ERR_PTR(-ENODEV);
  667. goto error;
  668. }
  669. key_ref = ERR_PTR(-EINVAL);
  670. if (!ktype->match || !ktype->instantiate)
  671. goto error_2;
  672. keyring = key_ref_to_ptr(keyring_ref);
  673. key_check(keyring);
  674. key_ref = ERR_PTR(-ENOTDIR);
  675. if (keyring->type != &key_type_keyring)
  676. goto error_2;
  677. ret = __key_link_begin(keyring, ktype, description, &prealloc);
  678. if (ret < 0)
  679. goto error_2;
  680. /* if we're going to allocate a new key, we're going to have
  681. * to modify the keyring */
  682. ret = key_permission(keyring_ref, KEY_WRITE);
  683. if (ret < 0) {
  684. key_ref = ERR_PTR(ret);
  685. goto error_3;
  686. }
  687. /* if it's possible to update this type of key, search for an existing
  688. * key of the same type and description in the destination keyring and
  689. * update that instead if possible
  690. */
  691. if (ktype->update) {
  692. key_ref = __keyring_search_one(keyring_ref, ktype, description,
  693. 0);
  694. if (!IS_ERR(key_ref))
  695. goto found_matching_key;
  696. }
  697. /* if the client doesn't provide, decide on the permissions we want */
  698. if (perm == KEY_PERM_UNDEF) {
  699. perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
  700. perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
  701. if (ktype->read)
  702. perm |= KEY_POS_READ | KEY_USR_READ;
  703. if (ktype == &key_type_keyring || ktype->update)
  704. perm |= KEY_USR_WRITE;
  705. }
  706. /* allocate a new key */
  707. key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
  708. perm, flags);
  709. if (IS_ERR(key)) {
  710. key_ref = ERR_CAST(key);
  711. goto error_3;
  712. }
  713. /* instantiate it and link it into the target keyring */
  714. ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL,
  715. &prealloc);
  716. if (ret < 0) {
  717. key_put(key);
  718. key_ref = ERR_PTR(ret);
  719. goto error_3;
  720. }
  721. key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
  722. error_3:
  723. __key_link_end(keyring, ktype, prealloc);
  724. error_2:
  725. key_type_put(ktype);
  726. error:
  727. return key_ref;
  728. found_matching_key:
  729. /* we found a matching key, so we're going to try to update it
  730. * - we can drop the locks first as we have the key pinned
  731. */
  732. __key_link_end(keyring, ktype, prealloc);
  733. key_type_put(ktype);
  734. key_ref = __key_update(key_ref, payload, plen);
  735. goto error;
  736. }
  737. EXPORT_SYMBOL(key_create_or_update);
  738. /**
  739. * key_update - Update a key's contents.
  740. * @key_ref: The pointer (plus possession flag) to the key.
  741. * @payload: The data to be used to update the key.
  742. * @plen: The length of @payload.
  743. *
  744. * Attempt to update the contents of a key with the given payload data. The
  745. * caller must be granted Write permission on the key. Negative keys can be
  746. * instantiated by this method.
  747. *
  748. * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
  749. * type does not support updating. The key type may return other errors.
  750. */
  751. int key_update(key_ref_t key_ref, const void *payload, size_t plen)
  752. {
  753. struct key *key = key_ref_to_ptr(key_ref);
  754. int ret;
  755. key_check(key);
  756. /* the key must be writable */
  757. ret = key_permission(key_ref, KEY_WRITE);
  758. if (ret < 0)
  759. goto error;
  760. /* attempt to update it if supported */
  761. ret = -EOPNOTSUPP;
  762. if (key->type->update) {
  763. down_write(&key->sem);
  764. ret = key->type->update(key, payload, plen);
  765. if (ret == 0)
  766. /* updating a negative key instantiates it */
  767. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  768. up_write(&key->sem);
  769. }
  770. error:
  771. return ret;
  772. }
  773. EXPORT_SYMBOL(key_update);
  774. /**
  775. * key_revoke - Revoke a key.
  776. * @key: The key to be revoked.
  777. *
  778. * Mark a key as being revoked and ask the type to free up its resources. The
  779. * revocation timeout is set and the key and all its links will be
  780. * automatically garbage collected after key_gc_delay amount of time if they
  781. * are not manually dealt with first.
  782. */
  783. void key_revoke(struct key *key)
  784. {
  785. struct timespec now;
  786. time_t time;
  787. key_check(key);
  788. /* make sure no one's trying to change or use the key when we mark it
  789. * - we tell lockdep that we might nest because we might be revoking an
  790. * authorisation key whilst holding the sem on a key we've just
  791. * instantiated
  792. */
  793. down_write_nested(&key->sem, 1);
  794. if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) &&
  795. key->type->revoke)
  796. key->type->revoke(key);
  797. /* set the death time to no more than the expiry time */
  798. now = current_kernel_time();
  799. time = now.tv_sec;
  800. if (key->revoked_at == 0 || key->revoked_at > time) {
  801. key->revoked_at = time;
  802. key_schedule_gc(key->revoked_at + key_gc_delay);
  803. }
  804. up_write(&key->sem);
  805. }
  806. EXPORT_SYMBOL(key_revoke);
  807. /**
  808. * register_key_type - Register a type of key.
  809. * @ktype: The new key type.
  810. *
  811. * Register a new key type.
  812. *
  813. * Returns 0 on success or -EEXIST if a type of this name already exists.
  814. */
  815. int register_key_type(struct key_type *ktype)
  816. {
  817. struct key_type *p;
  818. int ret;
  819. memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
  820. ret = -EEXIST;
  821. down_write(&key_types_sem);
  822. /* disallow key types with the same name */
  823. list_for_each_entry(p, &key_types_list, link) {
  824. if (strcmp(p->name, ktype->name) == 0)
  825. goto out;
  826. }
  827. /* store the type */
  828. list_add(&ktype->link, &key_types_list);
  829. ret = 0;
  830. out:
  831. up_write(&key_types_sem);
  832. return ret;
  833. }
  834. EXPORT_SYMBOL(register_key_type);
  835. /**
  836. * unregister_key_type - Unregister a type of key.
  837. * @ktype: The key type.
  838. *
  839. * Unregister a key type and mark all the extant keys of this type as dead.
  840. * Those keys of this type are then destroyed to get rid of their payloads and
  841. * they and their links will be garbage collected as soon as possible.
  842. */
  843. void unregister_key_type(struct key_type *ktype)
  844. {
  845. down_write(&key_types_sem);
  846. list_del_init(&ktype->link);
  847. downgrade_write(&key_types_sem);
  848. key_gc_keytype(ktype);
  849. up_read(&key_types_sem);
  850. }
  851. EXPORT_SYMBOL(unregister_key_type);
  852. /*
  853. * Initialise the key management state.
  854. */
  855. void __init key_init(void)
  856. {
  857. /* allocate a slab in which we can store keys */
  858. key_jar = kmem_cache_create("key_jar", sizeof(struct key),
  859. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  860. /* add the special key types */
  861. list_add_tail(&key_type_keyring.link, &key_types_list);
  862. list_add_tail(&key_type_dead.link, &key_types_list);
  863. list_add_tail(&key_type_user.link, &key_types_list);
  864. list_add_tail(&key_type_logon.link, &key_types_list);
  865. /* record the root user tracking */
  866. rb_link_node(&root_key_user.node,
  867. NULL,
  868. &key_user_tree.rb_node);
  869. rb_insert_color(&root_key_user.node,
  870. &key_user_tree);
  871. }