keyctl.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /* Userspace key control operations
  2. *
  3. * Copyright (C) 2004-5 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/syscalls.h>
  16. #include <linux/key.h>
  17. #include <linux/keyctl.h>
  18. #include <linux/fs.h>
  19. #include <linux/capability.h>
  20. #include <linux/string.h>
  21. #include <linux/err.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/security.h>
  24. #include <linux/uio.h>
  25. #include <asm/uaccess.h>
  26. #include "internal.h"
  27. #define KEY_MAX_DESC_SIZE 4096
  28. static int key_get_type_from_user(char *type,
  29. const char __user *_type,
  30. unsigned len)
  31. {
  32. int ret;
  33. ret = strncpy_from_user(type, _type, len);
  34. if (ret < 0)
  35. return ret;
  36. if (ret == 0 || ret >= len)
  37. return -EINVAL;
  38. if (type[0] == '.')
  39. return -EPERM;
  40. type[len - 1] = '\0';
  41. return 0;
  42. }
  43. /*
  44. * Extract the description of a new key from userspace and either add it as a
  45. * new key to the specified keyring or update a matching key in that keyring.
  46. *
  47. * If the description is NULL or an empty string, the key type is asked to
  48. * generate one from the payload.
  49. *
  50. * The keyring must be writable so that we can attach the key to it.
  51. *
  52. * If successful, the new key's serial number is returned, otherwise an error
  53. * code is returned.
  54. */
  55. SYSCALL_DEFINE5(add_key, const char __user *, _type,
  56. const char __user *, _description,
  57. const void __user *, _payload,
  58. size_t, plen,
  59. key_serial_t, ringid)
  60. {
  61. key_ref_t keyring_ref, key_ref;
  62. char type[32], *description;
  63. void *payload;
  64. long ret;
  65. ret = -EINVAL;
  66. if (plen > 1024 * 1024 - 1)
  67. goto error;
  68. /* draw all the data into kernel space */
  69. ret = key_get_type_from_user(type, _type, sizeof(type));
  70. if (ret < 0)
  71. goto error;
  72. description = NULL;
  73. if (_description) {
  74. description = strndup_user(_description, KEY_MAX_DESC_SIZE);
  75. if (IS_ERR(description)) {
  76. ret = PTR_ERR(description);
  77. goto error;
  78. }
  79. if (!*description) {
  80. kfree(description);
  81. description = NULL;
  82. } else if ((description[0] == '.') &&
  83. (strncmp(type, "keyring", 7) == 0)) {
  84. ret = -EPERM;
  85. goto error2;
  86. }
  87. }
  88. /* pull the payload in if one was supplied */
  89. payload = NULL;
  90. if (plen) {
  91. ret = -ENOMEM;
  92. payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
  93. if (!payload) {
  94. if (plen <= PAGE_SIZE)
  95. goto error2;
  96. payload = vmalloc(plen);
  97. if (!payload)
  98. goto error2;
  99. }
  100. ret = -EFAULT;
  101. if (copy_from_user(payload, _payload, plen) != 0)
  102. goto error3;
  103. }
  104. /* find the target keyring (which must be writable) */
  105. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
  106. if (IS_ERR(keyring_ref)) {
  107. ret = PTR_ERR(keyring_ref);
  108. goto error3;
  109. }
  110. /* create or update the requested key and add it to the target
  111. * keyring */
  112. key_ref = key_create_or_update(keyring_ref, type, description,
  113. payload, plen, KEY_PERM_UNDEF,
  114. KEY_ALLOC_IN_QUOTA);
  115. if (!IS_ERR(key_ref)) {
  116. ret = key_ref_to_ptr(key_ref)->serial;
  117. key_ref_put(key_ref);
  118. }
  119. else {
  120. ret = PTR_ERR(key_ref);
  121. }
  122. key_ref_put(keyring_ref);
  123. error3:
  124. kvfree(payload);
  125. error2:
  126. kfree(description);
  127. error:
  128. return ret;
  129. }
  130. /*
  131. * Search the process keyrings and keyring trees linked from those for a
  132. * matching key. Keyrings must have appropriate Search permission to be
  133. * searched.
  134. *
  135. * If a key is found, it will be attached to the destination keyring if there's
  136. * one specified and the serial number of the key will be returned.
  137. *
  138. * If no key is found, /sbin/request-key will be invoked if _callout_info is
  139. * non-NULL in an attempt to create a key. The _callout_info string will be
  140. * passed to /sbin/request-key to aid with completing the request. If the
  141. * _callout_info string is "" then it will be changed to "-".
  142. */
  143. SYSCALL_DEFINE4(request_key, const char __user *, _type,
  144. const char __user *, _description,
  145. const char __user *, _callout_info,
  146. key_serial_t, destringid)
  147. {
  148. struct key_type *ktype;
  149. struct key *key;
  150. key_ref_t dest_ref;
  151. size_t callout_len;
  152. char type[32], *description, *callout_info;
  153. long ret;
  154. /* pull the type into kernel space */
  155. ret = key_get_type_from_user(type, _type, sizeof(type));
  156. if (ret < 0)
  157. goto error;
  158. /* pull the description into kernel space */
  159. description = strndup_user(_description, KEY_MAX_DESC_SIZE);
  160. if (IS_ERR(description)) {
  161. ret = PTR_ERR(description);
  162. goto error;
  163. }
  164. /* pull the callout info into kernel space */
  165. callout_info = NULL;
  166. callout_len = 0;
  167. if (_callout_info) {
  168. callout_info = strndup_user(_callout_info, PAGE_SIZE);
  169. if (IS_ERR(callout_info)) {
  170. ret = PTR_ERR(callout_info);
  171. goto error2;
  172. }
  173. callout_len = strlen(callout_info);
  174. }
  175. /* get the destination keyring if specified */
  176. dest_ref = NULL;
  177. if (destringid) {
  178. dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
  179. KEY_NEED_WRITE);
  180. if (IS_ERR(dest_ref)) {
  181. ret = PTR_ERR(dest_ref);
  182. goto error3;
  183. }
  184. }
  185. /* find the key type */
  186. ktype = key_type_lookup(type);
  187. if (IS_ERR(ktype)) {
  188. ret = PTR_ERR(ktype);
  189. goto error4;
  190. }
  191. /* do the search */
  192. key = request_key_and_link(ktype, description, callout_info,
  193. callout_len, NULL, key_ref_to_ptr(dest_ref),
  194. KEY_ALLOC_IN_QUOTA);
  195. if (IS_ERR(key)) {
  196. ret = PTR_ERR(key);
  197. goto error5;
  198. }
  199. /* wait for the key to finish being constructed */
  200. ret = wait_for_key_construction(key, 1);
  201. if (ret < 0)
  202. goto error6;
  203. ret = key->serial;
  204. error6:
  205. key_put(key);
  206. error5:
  207. key_type_put(ktype);
  208. error4:
  209. key_ref_put(dest_ref);
  210. error3:
  211. kfree(callout_info);
  212. error2:
  213. kfree(description);
  214. error:
  215. return ret;
  216. }
  217. /*
  218. * Get the ID of the specified process keyring.
  219. *
  220. * The requested keyring must have search permission to be found.
  221. *
  222. * If successful, the ID of the requested keyring will be returned.
  223. */
  224. long keyctl_get_keyring_ID(key_serial_t id, int create)
  225. {
  226. key_ref_t key_ref;
  227. unsigned long lflags;
  228. long ret;
  229. lflags = create ? KEY_LOOKUP_CREATE : 0;
  230. key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
  231. if (IS_ERR(key_ref)) {
  232. ret = PTR_ERR(key_ref);
  233. goto error;
  234. }
  235. ret = key_ref_to_ptr(key_ref)->serial;
  236. key_ref_put(key_ref);
  237. error:
  238. return ret;
  239. }
  240. /*
  241. * Join a (named) session keyring.
  242. *
  243. * Create and join an anonymous session keyring or join a named session
  244. * keyring, creating it if necessary. A named session keyring must have Search
  245. * permission for it to be joined. Session keyrings without this permit will
  246. * be skipped over. It is not permitted for userspace to create or join
  247. * keyrings whose name begin with a dot.
  248. *
  249. * If successful, the ID of the joined session keyring will be returned.
  250. */
  251. long keyctl_join_session_keyring(const char __user *_name)
  252. {
  253. char *name;
  254. long ret;
  255. /* fetch the name from userspace */
  256. name = NULL;
  257. if (_name) {
  258. name = strndup_user(_name, KEY_MAX_DESC_SIZE);
  259. if (IS_ERR(name)) {
  260. ret = PTR_ERR(name);
  261. goto error;
  262. }
  263. ret = -EPERM;
  264. if (name[0] == '.')
  265. goto error_name;
  266. }
  267. /* join the session */
  268. ret = join_session_keyring(name);
  269. error_name:
  270. kfree(name);
  271. error:
  272. return ret;
  273. }
  274. /*
  275. * Update a key's data payload from the given data.
  276. *
  277. * The key must grant the caller Write permission and the key type must support
  278. * updating for this to work. A negative key can be positively instantiated
  279. * with this call.
  280. *
  281. * If successful, 0 will be returned. If the key type does not support
  282. * updating, then -EOPNOTSUPP will be returned.
  283. */
  284. long keyctl_update_key(key_serial_t id,
  285. const void __user *_payload,
  286. size_t plen)
  287. {
  288. key_ref_t key_ref;
  289. void *payload;
  290. long ret;
  291. ret = -EINVAL;
  292. if (plen > PAGE_SIZE)
  293. goto error;
  294. /* pull the payload in if one was supplied */
  295. payload = NULL;
  296. if (plen) {
  297. ret = -ENOMEM;
  298. payload = kmalloc(plen, GFP_KERNEL);
  299. if (!payload)
  300. goto error;
  301. ret = -EFAULT;
  302. if (copy_from_user(payload, _payload, plen) != 0)
  303. goto error2;
  304. }
  305. /* find the target key (which must be writable) */
  306. key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
  307. if (IS_ERR(key_ref)) {
  308. ret = PTR_ERR(key_ref);
  309. goto error2;
  310. }
  311. /* update the key */
  312. ret = key_update(key_ref, payload, plen);
  313. key_ref_put(key_ref);
  314. error2:
  315. kfree(payload);
  316. error:
  317. return ret;
  318. }
  319. /*
  320. * Revoke a key.
  321. *
  322. * The key must be grant the caller Write or Setattr permission for this to
  323. * work. The key type should give up its quota claim when revoked. The key
  324. * and any links to the key will be automatically garbage collected after a
  325. * certain amount of time (/proc/sys/kernel/keys/gc_delay).
  326. *
  327. * Keys with KEY_FLAG_KEEP set should not be revoked.
  328. *
  329. * If successful, 0 is returned.
  330. */
  331. long keyctl_revoke_key(key_serial_t id)
  332. {
  333. key_ref_t key_ref;
  334. struct key *key;
  335. long ret;
  336. key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
  337. if (IS_ERR(key_ref)) {
  338. ret = PTR_ERR(key_ref);
  339. if (ret != -EACCES)
  340. goto error;
  341. key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
  342. if (IS_ERR(key_ref)) {
  343. ret = PTR_ERR(key_ref);
  344. goto error;
  345. }
  346. }
  347. key = key_ref_to_ptr(key_ref);
  348. ret = 0;
  349. if (test_bit(KEY_FLAG_KEEP, &key->flags))
  350. ret = -EPERM;
  351. else
  352. key_revoke(key);
  353. key_ref_put(key_ref);
  354. error:
  355. return ret;
  356. }
  357. /*
  358. * Invalidate a key.
  359. *
  360. * The key must be grant the caller Invalidate permission for this to work.
  361. * The key and any links to the key will be automatically garbage collected
  362. * immediately.
  363. *
  364. * Keys with KEY_FLAG_KEEP set should not be invalidated.
  365. *
  366. * If successful, 0 is returned.
  367. */
  368. long keyctl_invalidate_key(key_serial_t id)
  369. {
  370. key_ref_t key_ref;
  371. struct key *key;
  372. long ret;
  373. kenter("%d", id);
  374. key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
  375. if (IS_ERR(key_ref)) {
  376. ret = PTR_ERR(key_ref);
  377. /* Root is permitted to invalidate certain special keys */
  378. if (capable(CAP_SYS_ADMIN)) {
  379. key_ref = lookup_user_key(id, 0, 0);
  380. if (IS_ERR(key_ref))
  381. goto error;
  382. if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
  383. &key_ref_to_ptr(key_ref)->flags))
  384. goto invalidate;
  385. goto error_put;
  386. }
  387. goto error;
  388. }
  389. invalidate:
  390. key = key_ref_to_ptr(key_ref);
  391. ret = 0;
  392. if (test_bit(KEY_FLAG_KEEP, &key->flags))
  393. ret = -EPERM;
  394. else
  395. key_invalidate(key);
  396. error_put:
  397. key_ref_put(key_ref);
  398. error:
  399. kleave(" = %ld", ret);
  400. return ret;
  401. }
  402. /*
  403. * Clear the specified keyring, creating an empty process keyring if one of the
  404. * special keyring IDs is used.
  405. *
  406. * The keyring must grant the caller Write permission and not have
  407. * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
  408. */
  409. long keyctl_keyring_clear(key_serial_t ringid)
  410. {
  411. key_ref_t keyring_ref;
  412. struct key *keyring;
  413. long ret;
  414. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
  415. if (IS_ERR(keyring_ref)) {
  416. ret = PTR_ERR(keyring_ref);
  417. /* Root is permitted to invalidate certain special keyrings */
  418. if (capable(CAP_SYS_ADMIN)) {
  419. keyring_ref = lookup_user_key(ringid, 0, 0);
  420. if (IS_ERR(keyring_ref))
  421. goto error;
  422. if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
  423. &key_ref_to_ptr(keyring_ref)->flags))
  424. goto clear;
  425. goto error_put;
  426. }
  427. goto error;
  428. }
  429. clear:
  430. keyring = key_ref_to_ptr(keyring_ref);
  431. if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
  432. ret = -EPERM;
  433. else
  434. ret = keyring_clear(keyring);
  435. error_put:
  436. key_ref_put(keyring_ref);
  437. error:
  438. return ret;
  439. }
  440. /*
  441. * Create a link from a keyring to a key if there's no matching key in the
  442. * keyring, otherwise replace the link to the matching key with a link to the
  443. * new key.
  444. *
  445. * The key must grant the caller Link permission and the the keyring must grant
  446. * the caller Write permission. Furthermore, if an additional link is created,
  447. * the keyring's quota will be extended.
  448. *
  449. * If successful, 0 will be returned.
  450. */
  451. long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
  452. {
  453. key_ref_t keyring_ref, key_ref;
  454. long ret;
  455. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
  456. if (IS_ERR(keyring_ref)) {
  457. ret = PTR_ERR(keyring_ref);
  458. goto error;
  459. }
  460. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
  461. if (IS_ERR(key_ref)) {
  462. ret = PTR_ERR(key_ref);
  463. goto error2;
  464. }
  465. ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  466. key_ref_put(key_ref);
  467. error2:
  468. key_ref_put(keyring_ref);
  469. error:
  470. return ret;
  471. }
  472. /*
  473. * Unlink a key from a keyring.
  474. *
  475. * The keyring must grant the caller Write permission for this to work; the key
  476. * itself need not grant the caller anything. If the last link to a key is
  477. * removed then that key will be scheduled for destruction.
  478. *
  479. * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
  480. *
  481. * If successful, 0 will be returned.
  482. */
  483. long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
  484. {
  485. key_ref_t keyring_ref, key_ref;
  486. struct key *keyring, *key;
  487. long ret;
  488. keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
  489. if (IS_ERR(keyring_ref)) {
  490. ret = PTR_ERR(keyring_ref);
  491. goto error;
  492. }
  493. key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
  494. if (IS_ERR(key_ref)) {
  495. ret = PTR_ERR(key_ref);
  496. goto error2;
  497. }
  498. keyring = key_ref_to_ptr(keyring_ref);
  499. key = key_ref_to_ptr(key_ref);
  500. if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
  501. test_bit(KEY_FLAG_KEEP, &key->flags))
  502. ret = -EPERM;
  503. else
  504. ret = key_unlink(keyring, key);
  505. key_ref_put(key_ref);
  506. error2:
  507. key_ref_put(keyring_ref);
  508. error:
  509. return ret;
  510. }
  511. /*
  512. * Return a description of a key to userspace.
  513. *
  514. * The key must grant the caller View permission for this to work.
  515. *
  516. * If there's a buffer, we place up to buflen bytes of data into it formatted
  517. * in the following way:
  518. *
  519. * type;uid;gid;perm;description<NUL>
  520. *
  521. * If successful, we return the amount of description available, irrespective
  522. * of how much we may have copied into the buffer.
  523. */
  524. long keyctl_describe_key(key_serial_t keyid,
  525. char __user *buffer,
  526. size_t buflen)
  527. {
  528. struct key *key, *instkey;
  529. key_ref_t key_ref;
  530. char *infobuf;
  531. long ret;
  532. int desclen, infolen;
  533. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
  534. if (IS_ERR(key_ref)) {
  535. /* viewing a key under construction is permitted if we have the
  536. * authorisation token handy */
  537. if (PTR_ERR(key_ref) == -EACCES) {
  538. instkey = key_get_instantiation_authkey(keyid);
  539. if (!IS_ERR(instkey)) {
  540. key_put(instkey);
  541. key_ref = lookup_user_key(keyid,
  542. KEY_LOOKUP_PARTIAL,
  543. 0);
  544. if (!IS_ERR(key_ref))
  545. goto okay;
  546. }
  547. }
  548. ret = PTR_ERR(key_ref);
  549. goto error;
  550. }
  551. okay:
  552. key = key_ref_to_ptr(key_ref);
  553. desclen = strlen(key->description);
  554. /* calculate how much information we're going to return */
  555. ret = -ENOMEM;
  556. infobuf = kasprintf(GFP_KERNEL,
  557. "%s;%d;%d;%08x;",
  558. key->type->name,
  559. from_kuid_munged(current_user_ns(), key->uid),
  560. from_kgid_munged(current_user_ns(), key->gid),
  561. key->perm);
  562. if (!infobuf)
  563. goto error2;
  564. infolen = strlen(infobuf);
  565. ret = infolen + desclen + 1;
  566. /* consider returning the data */
  567. if (buffer && buflen >= ret) {
  568. if (copy_to_user(buffer, infobuf, infolen) != 0 ||
  569. copy_to_user(buffer + infolen, key->description,
  570. desclen + 1) != 0)
  571. ret = -EFAULT;
  572. }
  573. kfree(infobuf);
  574. error2:
  575. key_ref_put(key_ref);
  576. error:
  577. return ret;
  578. }
  579. /*
  580. * Search the specified keyring and any keyrings it links to for a matching
  581. * key. Only keyrings that grant the caller Search permission will be searched
  582. * (this includes the starting keyring). Only keys with Search permission can
  583. * be found.
  584. *
  585. * If successful, the found key will be linked to the destination keyring if
  586. * supplied and the key has Link permission, and the found key ID will be
  587. * returned.
  588. */
  589. long keyctl_keyring_search(key_serial_t ringid,
  590. const char __user *_type,
  591. const char __user *_description,
  592. key_serial_t destringid)
  593. {
  594. struct key_type *ktype;
  595. key_ref_t keyring_ref, key_ref, dest_ref;
  596. char type[32], *description;
  597. long ret;
  598. /* pull the type and description into kernel space */
  599. ret = key_get_type_from_user(type, _type, sizeof(type));
  600. if (ret < 0)
  601. goto error;
  602. description = strndup_user(_description, KEY_MAX_DESC_SIZE);
  603. if (IS_ERR(description)) {
  604. ret = PTR_ERR(description);
  605. goto error;
  606. }
  607. /* get the keyring at which to begin the search */
  608. keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
  609. if (IS_ERR(keyring_ref)) {
  610. ret = PTR_ERR(keyring_ref);
  611. goto error2;
  612. }
  613. /* get the destination keyring if specified */
  614. dest_ref = NULL;
  615. if (destringid) {
  616. dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
  617. KEY_NEED_WRITE);
  618. if (IS_ERR(dest_ref)) {
  619. ret = PTR_ERR(dest_ref);
  620. goto error3;
  621. }
  622. }
  623. /* find the key type */
  624. ktype = key_type_lookup(type);
  625. if (IS_ERR(ktype)) {
  626. ret = PTR_ERR(ktype);
  627. goto error4;
  628. }
  629. /* do the search */
  630. key_ref = keyring_search(keyring_ref, ktype, description);
  631. if (IS_ERR(key_ref)) {
  632. ret = PTR_ERR(key_ref);
  633. /* treat lack or presence of a negative key the same */
  634. if (ret == -EAGAIN)
  635. ret = -ENOKEY;
  636. goto error5;
  637. }
  638. /* link the resulting key to the destination keyring if we can */
  639. if (dest_ref) {
  640. ret = key_permission(key_ref, KEY_NEED_LINK);
  641. if (ret < 0)
  642. goto error6;
  643. ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
  644. if (ret < 0)
  645. goto error6;
  646. }
  647. ret = key_ref_to_ptr(key_ref)->serial;
  648. error6:
  649. key_ref_put(key_ref);
  650. error5:
  651. key_type_put(ktype);
  652. error4:
  653. key_ref_put(dest_ref);
  654. error3:
  655. key_ref_put(keyring_ref);
  656. error2:
  657. kfree(description);
  658. error:
  659. return ret;
  660. }
  661. /*
  662. * Read a key's payload.
  663. *
  664. * The key must either grant the caller Read permission, or it must grant the
  665. * caller Search permission when searched for from the process keyrings.
  666. *
  667. * If successful, we place up to buflen bytes of data into the buffer, if one
  668. * is provided, and return the amount of data that is available in the key,
  669. * irrespective of how much we copied into the buffer.
  670. */
  671. long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
  672. {
  673. struct key *key;
  674. key_ref_t key_ref;
  675. long ret;
  676. /* find the key first */
  677. key_ref = lookup_user_key(keyid, 0, 0);
  678. if (IS_ERR(key_ref)) {
  679. ret = -ENOKEY;
  680. goto error;
  681. }
  682. key = key_ref_to_ptr(key_ref);
  683. ret = key_read_state(key);
  684. if (ret < 0)
  685. goto error2; /* Negatively instantiated */
  686. /* see if we can read it directly */
  687. ret = key_permission(key_ref, KEY_NEED_READ);
  688. if (ret == 0)
  689. goto can_read_key;
  690. if (ret != -EACCES)
  691. goto error;
  692. /* we can't; see if it's searchable from this process's keyrings
  693. * - we automatically take account of the fact that it may be
  694. * dangling off an instantiation key
  695. */
  696. if (!is_key_possessed(key_ref)) {
  697. ret = -EACCES;
  698. goto error2;
  699. }
  700. /* the key is probably readable - now try to read it */
  701. can_read_key:
  702. ret = -EOPNOTSUPP;
  703. if (key->type->read) {
  704. /* Read the data with the semaphore held (since we might sleep)
  705. * to protect against the key being updated or revoked.
  706. */
  707. down_read(&key->sem);
  708. ret = key_validate(key);
  709. if (ret == 0)
  710. ret = key->type->read(key, buffer, buflen);
  711. up_read(&key->sem);
  712. }
  713. error2:
  714. key_put(key);
  715. error:
  716. return ret;
  717. }
  718. /*
  719. * Change the ownership of a key
  720. *
  721. * The key must grant the caller Setattr permission for this to work, though
  722. * the key need not be fully instantiated yet. For the UID to be changed, or
  723. * for the GID to be changed to a group the caller is not a member of, the
  724. * caller must have sysadmin capability. If either uid or gid is -1 then that
  725. * attribute is not changed.
  726. *
  727. * If the UID is to be changed, the new user must have sufficient quota to
  728. * accept the key. The quota deduction will be removed from the old user to
  729. * the new user should the attribute be changed.
  730. *
  731. * If successful, 0 will be returned.
  732. */
  733. long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
  734. {
  735. struct key_user *newowner, *zapowner = NULL;
  736. struct key *key;
  737. key_ref_t key_ref;
  738. long ret;
  739. kuid_t uid;
  740. kgid_t gid;
  741. uid = make_kuid(current_user_ns(), user);
  742. gid = make_kgid(current_user_ns(), group);
  743. ret = -EINVAL;
  744. if ((user != (uid_t) -1) && !uid_valid(uid))
  745. goto error;
  746. if ((group != (gid_t) -1) && !gid_valid(gid))
  747. goto error;
  748. ret = 0;
  749. if (user == (uid_t) -1 && group == (gid_t) -1)
  750. goto error;
  751. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  752. KEY_NEED_SETATTR);
  753. if (IS_ERR(key_ref)) {
  754. ret = PTR_ERR(key_ref);
  755. goto error;
  756. }
  757. key = key_ref_to_ptr(key_ref);
  758. /* make the changes with the locks held to prevent chown/chown races */
  759. ret = -EACCES;
  760. down_write(&key->sem);
  761. if (!capable(CAP_SYS_ADMIN)) {
  762. /* only the sysadmin can chown a key to some other UID */
  763. if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
  764. goto error_put;
  765. /* only the sysadmin can set the key's GID to a group other
  766. * than one of those that the current process subscribes to */
  767. if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
  768. goto error_put;
  769. }
  770. /* change the UID */
  771. if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
  772. ret = -ENOMEM;
  773. newowner = key_user_lookup(uid);
  774. if (!newowner)
  775. goto error_put;
  776. /* transfer the quota burden to the new user */
  777. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  778. unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
  779. key_quota_root_maxkeys : key_quota_maxkeys;
  780. unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
  781. key_quota_root_maxbytes : key_quota_maxbytes;
  782. spin_lock(&newowner->lock);
  783. if (newowner->qnkeys + 1 >= maxkeys ||
  784. newowner->qnbytes + key->quotalen >= maxbytes ||
  785. newowner->qnbytes + key->quotalen <
  786. newowner->qnbytes)
  787. goto quota_overrun;
  788. newowner->qnkeys++;
  789. newowner->qnbytes += key->quotalen;
  790. spin_unlock(&newowner->lock);
  791. spin_lock(&key->user->lock);
  792. key->user->qnkeys--;
  793. key->user->qnbytes -= key->quotalen;
  794. spin_unlock(&key->user->lock);
  795. }
  796. atomic_dec(&key->user->nkeys);
  797. atomic_inc(&newowner->nkeys);
  798. if (key->state != KEY_IS_UNINSTANTIATED) {
  799. atomic_dec(&key->user->nikeys);
  800. atomic_inc(&newowner->nikeys);
  801. }
  802. zapowner = key->user;
  803. key->user = newowner;
  804. key->uid = uid;
  805. }
  806. /* change the GID */
  807. if (group != (gid_t) -1)
  808. key->gid = gid;
  809. ret = 0;
  810. error_put:
  811. up_write(&key->sem);
  812. key_put(key);
  813. if (zapowner)
  814. key_user_put(zapowner);
  815. error:
  816. return ret;
  817. quota_overrun:
  818. spin_unlock(&newowner->lock);
  819. zapowner = newowner;
  820. ret = -EDQUOT;
  821. goto error_put;
  822. }
  823. /*
  824. * Change the permission mask on a key.
  825. *
  826. * The key must grant the caller Setattr permission for this to work, though
  827. * the key need not be fully instantiated yet. If the caller does not have
  828. * sysadmin capability, it may only change the permission on keys that it owns.
  829. */
  830. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  831. {
  832. struct key *key;
  833. key_ref_t key_ref;
  834. long ret;
  835. ret = -EINVAL;
  836. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  837. goto error;
  838. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  839. KEY_NEED_SETATTR);
  840. if (IS_ERR(key_ref)) {
  841. ret = PTR_ERR(key_ref);
  842. goto error;
  843. }
  844. key = key_ref_to_ptr(key_ref);
  845. /* make the changes with the locks held to prevent chown/chmod races */
  846. ret = -EACCES;
  847. down_write(&key->sem);
  848. /* if we're not the sysadmin, we can only change a key that we own */
  849. if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
  850. key->perm = perm;
  851. ret = 0;
  852. }
  853. up_write(&key->sem);
  854. key_put(key);
  855. error:
  856. return ret;
  857. }
  858. /*
  859. * Get the destination keyring for instantiation and check that the caller has
  860. * Write permission on it.
  861. */
  862. static long get_instantiation_keyring(key_serial_t ringid,
  863. struct request_key_auth *rka,
  864. struct key **_dest_keyring)
  865. {
  866. key_ref_t dkref;
  867. *_dest_keyring = NULL;
  868. /* just return a NULL pointer if we weren't asked to make a link */
  869. if (ringid == 0)
  870. return 0;
  871. /* if a specific keyring is nominated by ID, then use that */
  872. if (ringid > 0) {
  873. dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
  874. if (IS_ERR(dkref))
  875. return PTR_ERR(dkref);
  876. *_dest_keyring = key_ref_to_ptr(dkref);
  877. return 0;
  878. }
  879. if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
  880. return -EINVAL;
  881. /* otherwise specify the destination keyring recorded in the
  882. * authorisation key (any KEY_SPEC_*_KEYRING) */
  883. if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
  884. *_dest_keyring = key_get(rka->dest_keyring);
  885. return 0;
  886. }
  887. return -ENOKEY;
  888. }
  889. /*
  890. * Change the request_key authorisation key on the current process.
  891. */
  892. static int keyctl_change_reqkey_auth(struct key *key)
  893. {
  894. struct cred *new;
  895. new = prepare_creds();
  896. if (!new)
  897. return -ENOMEM;
  898. key_put(new->request_key_auth);
  899. new->request_key_auth = key_get(key);
  900. return commit_creds(new);
  901. }
  902. /*
  903. * Instantiate a key with the specified payload and link the key into the
  904. * destination keyring if one is given.
  905. *
  906. * The caller must have the appropriate instantiation permit set for this to
  907. * work (see keyctl_assume_authority). No other permissions are required.
  908. *
  909. * If successful, 0 will be returned.
  910. */
  911. long keyctl_instantiate_key_common(key_serial_t id,
  912. struct iov_iter *from,
  913. key_serial_t ringid)
  914. {
  915. const struct cred *cred = current_cred();
  916. struct request_key_auth *rka;
  917. struct key *instkey, *dest_keyring;
  918. size_t plen = from ? iov_iter_count(from) : 0;
  919. void *payload;
  920. long ret;
  921. kenter("%d,,%zu,%d", id, plen, ringid);
  922. if (!plen)
  923. from = NULL;
  924. ret = -EINVAL;
  925. if (plen > 1024 * 1024 - 1)
  926. goto error;
  927. /* the appropriate instantiation authorisation key must have been
  928. * assumed before calling this */
  929. ret = -EPERM;
  930. instkey = cred->request_key_auth;
  931. if (!instkey)
  932. goto error;
  933. rka = instkey->payload.data[0];
  934. if (rka->target_key->serial != id)
  935. goto error;
  936. /* pull the payload in if one was supplied */
  937. payload = NULL;
  938. if (from) {
  939. ret = -ENOMEM;
  940. payload = kmalloc(plen, GFP_KERNEL);
  941. if (!payload) {
  942. if (plen <= PAGE_SIZE)
  943. goto error;
  944. payload = vmalloc(plen);
  945. if (!payload)
  946. goto error;
  947. }
  948. ret = -EFAULT;
  949. if (copy_from_iter(payload, plen, from) != plen)
  950. goto error2;
  951. }
  952. /* find the destination keyring amongst those belonging to the
  953. * requesting task */
  954. ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
  955. if (ret < 0)
  956. goto error2;
  957. /* instantiate the key and link it into a keyring */
  958. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  959. dest_keyring, instkey);
  960. key_put(dest_keyring);
  961. /* discard the assumed authority if it's just been disabled by
  962. * instantiation of the key */
  963. if (ret == 0)
  964. keyctl_change_reqkey_auth(NULL);
  965. error2:
  966. kvfree(payload);
  967. error:
  968. return ret;
  969. }
  970. /*
  971. * Instantiate a key with the specified payload and link the key into the
  972. * destination keyring if one is given.
  973. *
  974. * The caller must have the appropriate instantiation permit set for this to
  975. * work (see keyctl_assume_authority). No other permissions are required.
  976. *
  977. * If successful, 0 will be returned.
  978. */
  979. long keyctl_instantiate_key(key_serial_t id,
  980. const void __user *_payload,
  981. size_t plen,
  982. key_serial_t ringid)
  983. {
  984. if (_payload && plen) {
  985. struct iovec iov;
  986. struct iov_iter from;
  987. int ret;
  988. ret = import_single_range(WRITE, (void __user *)_payload, plen,
  989. &iov, &from);
  990. if (unlikely(ret))
  991. return ret;
  992. return keyctl_instantiate_key_common(id, &from, ringid);
  993. }
  994. return keyctl_instantiate_key_common(id, NULL, ringid);
  995. }
  996. /*
  997. * Instantiate a key with the specified multipart payload and link the key into
  998. * the destination keyring if one is given.
  999. *
  1000. * The caller must have the appropriate instantiation permit set for this to
  1001. * work (see keyctl_assume_authority). No other permissions are required.
  1002. *
  1003. * If successful, 0 will be returned.
  1004. */
  1005. long keyctl_instantiate_key_iov(key_serial_t id,
  1006. const struct iovec __user *_payload_iov,
  1007. unsigned ioc,
  1008. key_serial_t ringid)
  1009. {
  1010. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1011. struct iov_iter from;
  1012. long ret;
  1013. if (!_payload_iov)
  1014. ioc = 0;
  1015. ret = import_iovec(WRITE, _payload_iov, ioc,
  1016. ARRAY_SIZE(iovstack), &iov, &from);
  1017. if (ret < 0)
  1018. return ret;
  1019. ret = keyctl_instantiate_key_common(id, &from, ringid);
  1020. kfree(iov);
  1021. return ret;
  1022. }
  1023. /*
  1024. * Negatively instantiate the key with the given timeout (in seconds) and link
  1025. * the key into the destination keyring if one is given.
  1026. *
  1027. * The caller must have the appropriate instantiation permit set for this to
  1028. * work (see keyctl_assume_authority). No other permissions are required.
  1029. *
  1030. * The key and any links to the key will be automatically garbage collected
  1031. * after the timeout expires.
  1032. *
  1033. * Negative keys are used to rate limit repeated request_key() calls by causing
  1034. * them to return -ENOKEY until the negative key expires.
  1035. *
  1036. * If successful, 0 will be returned.
  1037. */
  1038. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  1039. {
  1040. return keyctl_reject_key(id, timeout, ENOKEY, ringid);
  1041. }
  1042. /*
  1043. * Negatively instantiate the key with the given timeout (in seconds) and error
  1044. * code and link the key into the destination keyring if one is given.
  1045. *
  1046. * The caller must have the appropriate instantiation permit set for this to
  1047. * work (see keyctl_assume_authority). No other permissions are required.
  1048. *
  1049. * The key and any links to the key will be automatically garbage collected
  1050. * after the timeout expires.
  1051. *
  1052. * Negative keys are used to rate limit repeated request_key() calls by causing
  1053. * them to return the specified error code until the negative key expires.
  1054. *
  1055. * If successful, 0 will be returned.
  1056. */
  1057. long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
  1058. key_serial_t ringid)
  1059. {
  1060. const struct cred *cred = current_cred();
  1061. struct request_key_auth *rka;
  1062. struct key *instkey, *dest_keyring;
  1063. long ret;
  1064. kenter("%d,%u,%u,%d", id, timeout, error, ringid);
  1065. /* must be a valid error code and mustn't be a kernel special */
  1066. if (error <= 0 ||
  1067. error >= MAX_ERRNO ||
  1068. error == ERESTARTSYS ||
  1069. error == ERESTARTNOINTR ||
  1070. error == ERESTARTNOHAND ||
  1071. error == ERESTART_RESTARTBLOCK)
  1072. return -EINVAL;
  1073. /* the appropriate instantiation authorisation key must have been
  1074. * assumed before calling this */
  1075. ret = -EPERM;
  1076. instkey = cred->request_key_auth;
  1077. if (!instkey)
  1078. goto error;
  1079. rka = instkey->payload.data[0];
  1080. if (rka->target_key->serial != id)
  1081. goto error;
  1082. /* find the destination keyring if present (which must also be
  1083. * writable) */
  1084. ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
  1085. if (ret < 0)
  1086. goto error;
  1087. /* instantiate the key and link it into a keyring */
  1088. ret = key_reject_and_link(rka->target_key, timeout, error,
  1089. dest_keyring, instkey);
  1090. key_put(dest_keyring);
  1091. /* discard the assumed authority if it's just been disabled by
  1092. * instantiation of the key */
  1093. if (ret == 0)
  1094. keyctl_change_reqkey_auth(NULL);
  1095. error:
  1096. return ret;
  1097. }
  1098. /*
  1099. * Read or set the default keyring in which request_key() will cache keys and
  1100. * return the old setting.
  1101. *
  1102. * If a thread or process keyring is specified then it will be created if it
  1103. * doesn't yet exist. The old setting will be returned if successful.
  1104. */
  1105. long keyctl_set_reqkey_keyring(int reqkey_defl)
  1106. {
  1107. struct cred *new;
  1108. int ret, old_setting;
  1109. old_setting = current_cred_xxx(jit_keyring);
  1110. if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
  1111. return old_setting;
  1112. new = prepare_creds();
  1113. if (!new)
  1114. return -ENOMEM;
  1115. switch (reqkey_defl) {
  1116. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  1117. ret = install_thread_keyring_to_cred(new);
  1118. if (ret < 0)
  1119. goto error;
  1120. goto set;
  1121. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  1122. ret = install_process_keyring_to_cred(new);
  1123. if (ret < 0)
  1124. goto error;
  1125. goto set;
  1126. case KEY_REQKEY_DEFL_DEFAULT:
  1127. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  1128. case KEY_REQKEY_DEFL_USER_KEYRING:
  1129. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  1130. case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
  1131. goto set;
  1132. case KEY_REQKEY_DEFL_NO_CHANGE:
  1133. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  1134. default:
  1135. ret = -EINVAL;
  1136. goto error;
  1137. }
  1138. set:
  1139. new->jit_keyring = reqkey_defl;
  1140. commit_creds(new);
  1141. return old_setting;
  1142. error:
  1143. abort_creds(new);
  1144. return ret;
  1145. }
  1146. /*
  1147. * Set or clear the timeout on a key.
  1148. *
  1149. * Either the key must grant the caller Setattr permission or else the caller
  1150. * must hold an instantiation authorisation token for the key.
  1151. *
  1152. * The timeout is either 0 to clear the timeout, or a number of seconds from
  1153. * the current time. The key and any links to the key will be automatically
  1154. * garbage collected after the timeout expires.
  1155. *
  1156. * Keys with KEY_FLAG_KEEP set should not be timed out.
  1157. *
  1158. * If successful, 0 is returned.
  1159. */
  1160. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  1161. {
  1162. struct key *key, *instkey;
  1163. key_ref_t key_ref;
  1164. long ret;
  1165. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  1166. KEY_NEED_SETATTR);
  1167. if (IS_ERR(key_ref)) {
  1168. /* setting the timeout on a key under construction is permitted
  1169. * if we have the authorisation token handy */
  1170. if (PTR_ERR(key_ref) == -EACCES) {
  1171. instkey = key_get_instantiation_authkey(id);
  1172. if (!IS_ERR(instkey)) {
  1173. key_put(instkey);
  1174. key_ref = lookup_user_key(id,
  1175. KEY_LOOKUP_PARTIAL,
  1176. 0);
  1177. if (!IS_ERR(key_ref))
  1178. goto okay;
  1179. }
  1180. }
  1181. ret = PTR_ERR(key_ref);
  1182. goto error;
  1183. }
  1184. okay:
  1185. key = key_ref_to_ptr(key_ref);
  1186. ret = 0;
  1187. if (test_bit(KEY_FLAG_KEEP, &key->flags))
  1188. ret = -EPERM;
  1189. else
  1190. key_set_timeout(key, timeout);
  1191. key_put(key);
  1192. error:
  1193. return ret;
  1194. }
  1195. /*
  1196. * Assume (or clear) the authority to instantiate the specified key.
  1197. *
  1198. * This sets the authoritative token currently in force for key instantiation.
  1199. * This must be done for a key to be instantiated. It has the effect of making
  1200. * available all the keys from the caller of the request_key() that created a
  1201. * key to request_key() calls made by the caller of this function.
  1202. *
  1203. * The caller must have the instantiation key in their process keyrings with a
  1204. * Search permission grant available to the caller.
  1205. *
  1206. * If the ID given is 0, then the setting will be cleared and 0 returned.
  1207. *
  1208. * If the ID given has a matching an authorisation key, then that key will be
  1209. * set and its ID will be returned. The authorisation key can be read to get
  1210. * the callout information passed to request_key().
  1211. */
  1212. long keyctl_assume_authority(key_serial_t id)
  1213. {
  1214. struct key *authkey;
  1215. long ret;
  1216. /* special key IDs aren't permitted */
  1217. ret = -EINVAL;
  1218. if (id < 0)
  1219. goto error;
  1220. /* we divest ourselves of authority if given an ID of 0 */
  1221. if (id == 0) {
  1222. ret = keyctl_change_reqkey_auth(NULL);
  1223. goto error;
  1224. }
  1225. /* attempt to assume the authority temporarily granted to us whilst we
  1226. * instantiate the specified key
  1227. * - the authorisation key must be in the current task's keyrings
  1228. * somewhere
  1229. */
  1230. authkey = key_get_instantiation_authkey(id);
  1231. if (IS_ERR(authkey)) {
  1232. ret = PTR_ERR(authkey);
  1233. goto error;
  1234. }
  1235. ret = keyctl_change_reqkey_auth(authkey);
  1236. if (ret < 0)
  1237. goto error;
  1238. key_put(authkey);
  1239. ret = authkey->serial;
  1240. error:
  1241. return ret;
  1242. }
  1243. /*
  1244. * Get a key's the LSM security label.
  1245. *
  1246. * The key must grant the caller View permission for this to work.
  1247. *
  1248. * If there's a buffer, then up to buflen bytes of data will be placed into it.
  1249. *
  1250. * If successful, the amount of information available will be returned,
  1251. * irrespective of how much was copied (including the terminal NUL).
  1252. */
  1253. long keyctl_get_security(key_serial_t keyid,
  1254. char __user *buffer,
  1255. size_t buflen)
  1256. {
  1257. struct key *key, *instkey;
  1258. key_ref_t key_ref;
  1259. char *context;
  1260. long ret;
  1261. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
  1262. if (IS_ERR(key_ref)) {
  1263. if (PTR_ERR(key_ref) != -EACCES)
  1264. return PTR_ERR(key_ref);
  1265. /* viewing a key under construction is also permitted if we
  1266. * have the authorisation token handy */
  1267. instkey = key_get_instantiation_authkey(keyid);
  1268. if (IS_ERR(instkey))
  1269. return PTR_ERR(instkey);
  1270. key_put(instkey);
  1271. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
  1272. if (IS_ERR(key_ref))
  1273. return PTR_ERR(key_ref);
  1274. }
  1275. key = key_ref_to_ptr(key_ref);
  1276. ret = security_key_getsecurity(key, &context);
  1277. if (ret == 0) {
  1278. /* if no information was returned, give userspace an empty
  1279. * string */
  1280. ret = 1;
  1281. if (buffer && buflen > 0 &&
  1282. copy_to_user(buffer, "", 1) != 0)
  1283. ret = -EFAULT;
  1284. } else if (ret > 0) {
  1285. /* return as much data as there's room for */
  1286. if (buffer && buflen > 0) {
  1287. if (buflen > ret)
  1288. buflen = ret;
  1289. if (copy_to_user(buffer, context, buflen) != 0)
  1290. ret = -EFAULT;
  1291. }
  1292. kfree(context);
  1293. }
  1294. key_ref_put(key_ref);
  1295. return ret;
  1296. }
  1297. /*
  1298. * Attempt to install the calling process's session keyring on the process's
  1299. * parent process.
  1300. *
  1301. * The keyring must exist and must grant the caller LINK permission, and the
  1302. * parent process must be single-threaded and must have the same effective
  1303. * ownership as this process and mustn't be SUID/SGID.
  1304. *
  1305. * The keyring will be emplaced on the parent when it next resumes userspace.
  1306. *
  1307. * If successful, 0 will be returned.
  1308. */
  1309. long keyctl_session_to_parent(void)
  1310. {
  1311. struct task_struct *me, *parent;
  1312. const struct cred *mycred, *pcred;
  1313. struct callback_head *newwork, *oldwork;
  1314. key_ref_t keyring_r;
  1315. struct cred *cred;
  1316. int ret;
  1317. keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
  1318. if (IS_ERR(keyring_r))
  1319. return PTR_ERR(keyring_r);
  1320. ret = -ENOMEM;
  1321. /* our parent is going to need a new cred struct, a new tgcred struct
  1322. * and new security data, so we allocate them here to prevent ENOMEM in
  1323. * our parent */
  1324. cred = cred_alloc_blank();
  1325. if (!cred)
  1326. goto error_keyring;
  1327. newwork = &cred->rcu;
  1328. cred->session_keyring = key_ref_to_ptr(keyring_r);
  1329. keyring_r = NULL;
  1330. init_task_work(newwork, key_change_session_keyring);
  1331. me = current;
  1332. rcu_read_lock();
  1333. write_lock_irq(&tasklist_lock);
  1334. ret = -EPERM;
  1335. oldwork = NULL;
  1336. parent = me->real_parent;
  1337. /* the parent mustn't be init and mustn't be a kernel thread */
  1338. if (parent->pid <= 1 || !parent->mm)
  1339. goto unlock;
  1340. /* the parent must be single threaded */
  1341. if (!thread_group_empty(parent))
  1342. goto unlock;
  1343. /* the parent and the child must have different session keyrings or
  1344. * there's no point */
  1345. mycred = current_cred();
  1346. pcred = __task_cred(parent);
  1347. if (mycred == pcred ||
  1348. mycred->session_keyring == pcred->session_keyring) {
  1349. ret = 0;
  1350. goto unlock;
  1351. }
  1352. /* the parent must have the same effective ownership and mustn't be
  1353. * SUID/SGID */
  1354. if (!uid_eq(pcred->uid, mycred->euid) ||
  1355. !uid_eq(pcred->euid, mycred->euid) ||
  1356. !uid_eq(pcred->suid, mycred->euid) ||
  1357. !gid_eq(pcred->gid, mycred->egid) ||
  1358. !gid_eq(pcred->egid, mycred->egid) ||
  1359. !gid_eq(pcred->sgid, mycred->egid))
  1360. goto unlock;
  1361. /* the keyrings must have the same UID */
  1362. if ((pcred->session_keyring &&
  1363. !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
  1364. !uid_eq(mycred->session_keyring->uid, mycred->euid))
  1365. goto unlock;
  1366. /* cancel an already pending keyring replacement */
  1367. oldwork = task_work_cancel(parent, key_change_session_keyring);
  1368. /* the replacement session keyring is applied just prior to userspace
  1369. * restarting */
  1370. ret = task_work_add(parent, newwork, true);
  1371. if (!ret)
  1372. newwork = NULL;
  1373. unlock:
  1374. write_unlock_irq(&tasklist_lock);
  1375. rcu_read_unlock();
  1376. if (oldwork)
  1377. put_cred(container_of(oldwork, struct cred, rcu));
  1378. if (newwork)
  1379. put_cred(cred);
  1380. return ret;
  1381. error_keyring:
  1382. key_ref_put(keyring_r);
  1383. return ret;
  1384. }
  1385. /*
  1386. * The key control system call
  1387. */
  1388. SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1389. unsigned long, arg4, unsigned long, arg5)
  1390. {
  1391. switch (option) {
  1392. case KEYCTL_GET_KEYRING_ID:
  1393. return keyctl_get_keyring_ID((key_serial_t) arg2,
  1394. (int) arg3);
  1395. case KEYCTL_JOIN_SESSION_KEYRING:
  1396. return keyctl_join_session_keyring((const char __user *) arg2);
  1397. case KEYCTL_UPDATE:
  1398. return keyctl_update_key((key_serial_t) arg2,
  1399. (const void __user *) arg3,
  1400. (size_t) arg4);
  1401. case KEYCTL_REVOKE:
  1402. return keyctl_revoke_key((key_serial_t) arg2);
  1403. case KEYCTL_DESCRIBE:
  1404. return keyctl_describe_key((key_serial_t) arg2,
  1405. (char __user *) arg3,
  1406. (unsigned) arg4);
  1407. case KEYCTL_CLEAR:
  1408. return keyctl_keyring_clear((key_serial_t) arg2);
  1409. case KEYCTL_LINK:
  1410. return keyctl_keyring_link((key_serial_t) arg2,
  1411. (key_serial_t) arg3);
  1412. case KEYCTL_UNLINK:
  1413. return keyctl_keyring_unlink((key_serial_t) arg2,
  1414. (key_serial_t) arg3);
  1415. case KEYCTL_SEARCH:
  1416. return keyctl_keyring_search((key_serial_t) arg2,
  1417. (const char __user *) arg3,
  1418. (const char __user *) arg4,
  1419. (key_serial_t) arg5);
  1420. case KEYCTL_READ:
  1421. return keyctl_read_key((key_serial_t) arg2,
  1422. (char __user *) arg3,
  1423. (size_t) arg4);
  1424. case KEYCTL_CHOWN:
  1425. return keyctl_chown_key((key_serial_t) arg2,
  1426. (uid_t) arg3,
  1427. (gid_t) arg4);
  1428. case KEYCTL_SETPERM:
  1429. return keyctl_setperm_key((key_serial_t) arg2,
  1430. (key_perm_t) arg3);
  1431. case KEYCTL_INSTANTIATE:
  1432. return keyctl_instantiate_key((key_serial_t) arg2,
  1433. (const void __user *) arg3,
  1434. (size_t) arg4,
  1435. (key_serial_t) arg5);
  1436. case KEYCTL_NEGATE:
  1437. return keyctl_negate_key((key_serial_t) arg2,
  1438. (unsigned) arg3,
  1439. (key_serial_t) arg4);
  1440. case KEYCTL_SET_REQKEY_KEYRING:
  1441. return keyctl_set_reqkey_keyring(arg2);
  1442. case KEYCTL_SET_TIMEOUT:
  1443. return keyctl_set_timeout((key_serial_t) arg2,
  1444. (unsigned) arg3);
  1445. case KEYCTL_ASSUME_AUTHORITY:
  1446. return keyctl_assume_authority((key_serial_t) arg2);
  1447. case KEYCTL_GET_SECURITY:
  1448. return keyctl_get_security((key_serial_t) arg2,
  1449. (char __user *) arg3,
  1450. (size_t) arg4);
  1451. case KEYCTL_SESSION_TO_PARENT:
  1452. return keyctl_session_to_parent();
  1453. case KEYCTL_REJECT:
  1454. return keyctl_reject_key((key_serial_t) arg2,
  1455. (unsigned) arg3,
  1456. (unsigned) arg4,
  1457. (key_serial_t) arg5);
  1458. case KEYCTL_INSTANTIATE_IOV:
  1459. return keyctl_instantiate_key_iov(
  1460. (key_serial_t) arg2,
  1461. (const struct iovec __user *) arg3,
  1462. (unsigned) arg4,
  1463. (key_serial_t) arg5);
  1464. case KEYCTL_INVALIDATE:
  1465. return keyctl_invalidate_key((key_serial_t) arg2);
  1466. case KEYCTL_GET_PERSISTENT:
  1467. return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
  1468. case KEYCTL_DH_COMPUTE:
  1469. return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
  1470. (char __user *) arg3, (size_t) arg4,
  1471. (void __user *) arg5);
  1472. default:
  1473. return -EOPNOTSUPP;
  1474. }
  1475. }