keyctl.c 40 KB

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