keyctl.c 40 KB

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