keyctl.c 40 KB

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