keyctl.c 41 KB

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