keyctl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. goto err;
  988. if (ret == 0)
  989. goto no_payload_free;
  990. ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
  991. err:
  992. if (iov != iovstack)
  993. kfree(iov);
  994. return ret;
  995. no_payload_free:
  996. if (iov != iovstack)
  997. kfree(iov);
  998. no_payload:
  999. return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
  1000. }
  1001. /*
  1002. * Negatively instantiate the key with the given timeout (in seconds) and link
  1003. * the key into the destination keyring if one is given.
  1004. *
  1005. * The caller must have the appropriate instantiation permit set for this to
  1006. * work (see keyctl_assume_authority). No other permissions are required.
  1007. *
  1008. * The key and any links to the key will be automatically garbage collected
  1009. * after the timeout expires.
  1010. *
  1011. * Negative keys are used to rate limit repeated request_key() calls by causing
  1012. * them to return -ENOKEY until the negative key expires.
  1013. *
  1014. * If successful, 0 will be returned.
  1015. */
  1016. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  1017. {
  1018. return keyctl_reject_key(id, timeout, ENOKEY, ringid);
  1019. }
  1020. /*
  1021. * Negatively instantiate the key with the given timeout (in seconds) and error
  1022. * code and link the key into the destination keyring if one is given.
  1023. *
  1024. * The caller must have the appropriate instantiation permit set for this to
  1025. * work (see keyctl_assume_authority). No other permissions are required.
  1026. *
  1027. * The key and any links to the key will be automatically garbage collected
  1028. * after the timeout expires.
  1029. *
  1030. * Negative keys are used to rate limit repeated request_key() calls by causing
  1031. * them to return the specified error code until the negative key expires.
  1032. *
  1033. * If successful, 0 will be returned.
  1034. */
  1035. long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
  1036. key_serial_t ringid)
  1037. {
  1038. const struct cred *cred = current_cred();
  1039. struct request_key_auth *rka;
  1040. struct key *instkey, *dest_keyring;
  1041. long ret;
  1042. kenter("%d,%u,%u,%d", id, timeout, error, ringid);
  1043. /* must be a valid error code and mustn't be a kernel special */
  1044. if (error <= 0 ||
  1045. error >= MAX_ERRNO ||
  1046. error == ERESTARTSYS ||
  1047. error == ERESTARTNOINTR ||
  1048. error == ERESTARTNOHAND ||
  1049. error == ERESTART_RESTARTBLOCK)
  1050. return -EINVAL;
  1051. /* the appropriate instantiation authorisation key must have been
  1052. * assumed before calling this */
  1053. ret = -EPERM;
  1054. instkey = cred->request_key_auth;
  1055. if (!instkey)
  1056. goto error;
  1057. rka = instkey->payload.data;
  1058. if (rka->target_key->serial != id)
  1059. goto error;
  1060. /* find the destination keyring if present (which must also be
  1061. * writable) */
  1062. ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
  1063. if (ret < 0)
  1064. goto error;
  1065. /* instantiate the key and link it into a keyring */
  1066. ret = key_reject_and_link(rka->target_key, timeout, error,
  1067. dest_keyring, instkey);
  1068. key_put(dest_keyring);
  1069. /* discard the assumed authority if it's just been disabled by
  1070. * instantiation of the key */
  1071. if (ret == 0)
  1072. keyctl_change_reqkey_auth(NULL);
  1073. error:
  1074. return ret;
  1075. }
  1076. /*
  1077. * Read or set the default keyring in which request_key() will cache keys and
  1078. * return the old setting.
  1079. *
  1080. * If a process keyring is specified then this will be created if it doesn't
  1081. * yet exist. The old setting will be returned if successful.
  1082. */
  1083. long keyctl_set_reqkey_keyring(int reqkey_defl)
  1084. {
  1085. struct cred *new;
  1086. int ret, old_setting;
  1087. old_setting = current_cred_xxx(jit_keyring);
  1088. if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
  1089. return old_setting;
  1090. new = prepare_creds();
  1091. if (!new)
  1092. return -ENOMEM;
  1093. switch (reqkey_defl) {
  1094. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  1095. ret = install_thread_keyring_to_cred(new);
  1096. if (ret < 0)
  1097. goto error;
  1098. goto set;
  1099. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  1100. ret = install_process_keyring_to_cred(new);
  1101. if (ret < 0) {
  1102. if (ret != -EEXIST)
  1103. goto error;
  1104. ret = 0;
  1105. }
  1106. goto set;
  1107. case KEY_REQKEY_DEFL_DEFAULT:
  1108. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  1109. case KEY_REQKEY_DEFL_USER_KEYRING:
  1110. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  1111. case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
  1112. goto set;
  1113. case KEY_REQKEY_DEFL_NO_CHANGE:
  1114. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  1115. default:
  1116. ret = -EINVAL;
  1117. goto error;
  1118. }
  1119. set:
  1120. new->jit_keyring = reqkey_defl;
  1121. commit_creds(new);
  1122. return old_setting;
  1123. error:
  1124. abort_creds(new);
  1125. return ret;
  1126. }
  1127. /*
  1128. * Set or clear the timeout on a key.
  1129. *
  1130. * Either the key must grant the caller Setattr permission or else the caller
  1131. * must hold an instantiation authorisation token for the key.
  1132. *
  1133. * The timeout is either 0 to clear the timeout, or a number of seconds from
  1134. * the current time. The key and any links to the key will be automatically
  1135. * garbage collected after the timeout expires.
  1136. *
  1137. * If successful, 0 is returned.
  1138. */
  1139. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  1140. {
  1141. struct key *key, *instkey;
  1142. key_ref_t key_ref;
  1143. long ret;
  1144. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  1145. KEY_SETATTR);
  1146. if (IS_ERR(key_ref)) {
  1147. /* setting the timeout on a key under construction is permitted
  1148. * if we have the authorisation token handy */
  1149. if (PTR_ERR(key_ref) == -EACCES) {
  1150. instkey = key_get_instantiation_authkey(id);
  1151. if (!IS_ERR(instkey)) {
  1152. key_put(instkey);
  1153. key_ref = lookup_user_key(id,
  1154. KEY_LOOKUP_PARTIAL,
  1155. 0);
  1156. if (!IS_ERR(key_ref))
  1157. goto okay;
  1158. }
  1159. }
  1160. ret = PTR_ERR(key_ref);
  1161. goto error;
  1162. }
  1163. okay:
  1164. key = key_ref_to_ptr(key_ref);
  1165. key_set_timeout(key, timeout);
  1166. key_put(key);
  1167. ret = 0;
  1168. error:
  1169. return ret;
  1170. }
  1171. /*
  1172. * Assume (or clear) the authority to instantiate the specified key.
  1173. *
  1174. * This sets the authoritative token currently in force for key instantiation.
  1175. * This must be done for a key to be instantiated. It has the effect of making
  1176. * available all the keys from the caller of the request_key() that created a
  1177. * key to request_key() calls made by the caller of this function.
  1178. *
  1179. * The caller must have the instantiation key in their process keyrings with a
  1180. * Search permission grant available to the caller.
  1181. *
  1182. * If the ID given is 0, then the setting will be cleared and 0 returned.
  1183. *
  1184. * If the ID given has a matching an authorisation key, then that key will be
  1185. * set and its ID will be returned. The authorisation key can be read to get
  1186. * the callout information passed to request_key().
  1187. */
  1188. long keyctl_assume_authority(key_serial_t id)
  1189. {
  1190. struct key *authkey;
  1191. long ret;
  1192. /* special key IDs aren't permitted */
  1193. ret = -EINVAL;
  1194. if (id < 0)
  1195. goto error;
  1196. /* we divest ourselves of authority if given an ID of 0 */
  1197. if (id == 0) {
  1198. ret = keyctl_change_reqkey_auth(NULL);
  1199. goto error;
  1200. }
  1201. /* attempt to assume the authority temporarily granted to us whilst we
  1202. * instantiate the specified key
  1203. * - the authorisation key must be in the current task's keyrings
  1204. * somewhere
  1205. */
  1206. authkey = key_get_instantiation_authkey(id);
  1207. if (IS_ERR(authkey)) {
  1208. ret = PTR_ERR(authkey);
  1209. goto error;
  1210. }
  1211. ret = keyctl_change_reqkey_auth(authkey);
  1212. if (ret < 0)
  1213. goto error;
  1214. key_put(authkey);
  1215. ret = authkey->serial;
  1216. error:
  1217. return ret;
  1218. }
  1219. /*
  1220. * Get a key's the LSM security label.
  1221. *
  1222. * The key must grant the caller View permission for this to work.
  1223. *
  1224. * If there's a buffer, then up to buflen bytes of data will be placed into it.
  1225. *
  1226. * If successful, the amount of information available will be returned,
  1227. * irrespective of how much was copied (including the terminal NUL).
  1228. */
  1229. long keyctl_get_security(key_serial_t keyid,
  1230. char __user *buffer,
  1231. size_t buflen)
  1232. {
  1233. struct key *key, *instkey;
  1234. key_ref_t key_ref;
  1235. char *context;
  1236. long ret;
  1237. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
  1238. if (IS_ERR(key_ref)) {
  1239. if (PTR_ERR(key_ref) != -EACCES)
  1240. return PTR_ERR(key_ref);
  1241. /* viewing a key under construction is also permitted if we
  1242. * have the authorisation token handy */
  1243. instkey = key_get_instantiation_authkey(keyid);
  1244. if (IS_ERR(instkey))
  1245. return PTR_ERR(instkey);
  1246. key_put(instkey);
  1247. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
  1248. if (IS_ERR(key_ref))
  1249. return PTR_ERR(key_ref);
  1250. }
  1251. key = key_ref_to_ptr(key_ref);
  1252. ret = security_key_getsecurity(key, &context);
  1253. if (ret == 0) {
  1254. /* if no information was returned, give userspace an empty
  1255. * string */
  1256. ret = 1;
  1257. if (buffer && buflen > 0 &&
  1258. copy_to_user(buffer, "", 1) != 0)
  1259. ret = -EFAULT;
  1260. } else if (ret > 0) {
  1261. /* return as much data as there's room for */
  1262. if (buffer && buflen > 0) {
  1263. if (buflen > ret)
  1264. buflen = ret;
  1265. if (copy_to_user(buffer, context, buflen) != 0)
  1266. ret = -EFAULT;
  1267. }
  1268. kfree(context);
  1269. }
  1270. key_ref_put(key_ref);
  1271. return ret;
  1272. }
  1273. /*
  1274. * Attempt to install the calling process's session keyring on the process's
  1275. * parent process.
  1276. *
  1277. * The keyring must exist and must grant the caller LINK permission, and the
  1278. * parent process must be single-threaded and must have the same effective
  1279. * ownership as this process and mustn't be SUID/SGID.
  1280. *
  1281. * The keyring will be emplaced on the parent when it next resumes userspace.
  1282. *
  1283. * If successful, 0 will be returned.
  1284. */
  1285. long keyctl_session_to_parent(void)
  1286. {
  1287. struct task_struct *me, *parent;
  1288. const struct cred *mycred, *pcred;
  1289. struct callback_head *newwork, *oldwork;
  1290. key_ref_t keyring_r;
  1291. struct cred *cred;
  1292. int ret;
  1293. keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
  1294. if (IS_ERR(keyring_r))
  1295. return PTR_ERR(keyring_r);
  1296. ret = -ENOMEM;
  1297. /* our parent is going to need a new cred struct, a new tgcred struct
  1298. * and new security data, so we allocate them here to prevent ENOMEM in
  1299. * our parent */
  1300. cred = cred_alloc_blank();
  1301. if (!cred)
  1302. goto error_keyring;
  1303. newwork = &cred->rcu;
  1304. cred->session_keyring = key_ref_to_ptr(keyring_r);
  1305. keyring_r = NULL;
  1306. init_task_work(newwork, key_change_session_keyring);
  1307. me = current;
  1308. rcu_read_lock();
  1309. write_lock_irq(&tasklist_lock);
  1310. ret = -EPERM;
  1311. oldwork = NULL;
  1312. parent = me->real_parent;
  1313. /* the parent mustn't be init and mustn't be a kernel thread */
  1314. if (parent->pid <= 1 || !parent->mm)
  1315. goto unlock;
  1316. /* the parent must be single threaded */
  1317. if (!thread_group_empty(parent))
  1318. goto unlock;
  1319. /* the parent and the child must have different session keyrings or
  1320. * there's no point */
  1321. mycred = current_cred();
  1322. pcred = __task_cred(parent);
  1323. if (mycred == pcred ||
  1324. mycred->session_keyring == pcred->session_keyring) {
  1325. ret = 0;
  1326. goto unlock;
  1327. }
  1328. /* the parent must have the same effective ownership and mustn't be
  1329. * SUID/SGID */
  1330. if (!uid_eq(pcred->uid, mycred->euid) ||
  1331. !uid_eq(pcred->euid, mycred->euid) ||
  1332. !uid_eq(pcred->suid, mycred->euid) ||
  1333. !gid_eq(pcred->gid, mycred->egid) ||
  1334. !gid_eq(pcred->egid, mycred->egid) ||
  1335. !gid_eq(pcred->sgid, mycred->egid))
  1336. goto unlock;
  1337. /* the keyrings must have the same UID */
  1338. if ((pcred->session_keyring &&
  1339. !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
  1340. !uid_eq(mycred->session_keyring->uid, mycred->euid))
  1341. goto unlock;
  1342. /* cancel an already pending keyring replacement */
  1343. oldwork = task_work_cancel(parent, key_change_session_keyring);
  1344. /* the replacement session keyring is applied just prior to userspace
  1345. * restarting */
  1346. ret = task_work_add(parent, newwork, true);
  1347. if (!ret)
  1348. newwork = NULL;
  1349. unlock:
  1350. write_unlock_irq(&tasklist_lock);
  1351. rcu_read_unlock();
  1352. if (oldwork)
  1353. put_cred(container_of(oldwork, struct cred, rcu));
  1354. if (newwork)
  1355. put_cred(cred);
  1356. return ret;
  1357. error_keyring:
  1358. key_ref_put(keyring_r);
  1359. return ret;
  1360. }
  1361. /*
  1362. * The key control system call
  1363. */
  1364. SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1365. unsigned long, arg4, unsigned long, arg5)
  1366. {
  1367. switch (option) {
  1368. case KEYCTL_GET_KEYRING_ID:
  1369. return keyctl_get_keyring_ID((key_serial_t) arg2,
  1370. (int) arg3);
  1371. case KEYCTL_JOIN_SESSION_KEYRING:
  1372. return keyctl_join_session_keyring((const char __user *) arg2);
  1373. case KEYCTL_UPDATE:
  1374. return keyctl_update_key((key_serial_t) arg2,
  1375. (const void __user *) arg3,
  1376. (size_t) arg4);
  1377. case KEYCTL_REVOKE:
  1378. return keyctl_revoke_key((key_serial_t) arg2);
  1379. case KEYCTL_DESCRIBE:
  1380. return keyctl_describe_key((key_serial_t) arg2,
  1381. (char __user *) arg3,
  1382. (unsigned) arg4);
  1383. case KEYCTL_CLEAR:
  1384. return keyctl_keyring_clear((key_serial_t) arg2);
  1385. case KEYCTL_LINK:
  1386. return keyctl_keyring_link((key_serial_t) arg2,
  1387. (key_serial_t) arg3);
  1388. case KEYCTL_UNLINK:
  1389. return keyctl_keyring_unlink((key_serial_t) arg2,
  1390. (key_serial_t) arg3);
  1391. case KEYCTL_SEARCH:
  1392. return keyctl_keyring_search((key_serial_t) arg2,
  1393. (const char __user *) arg3,
  1394. (const char __user *) arg4,
  1395. (key_serial_t) arg5);
  1396. case KEYCTL_READ:
  1397. return keyctl_read_key((key_serial_t) arg2,
  1398. (char __user *) arg3,
  1399. (size_t) arg4);
  1400. case KEYCTL_CHOWN:
  1401. return keyctl_chown_key((key_serial_t) arg2,
  1402. (uid_t) arg3,
  1403. (gid_t) arg4);
  1404. case KEYCTL_SETPERM:
  1405. return keyctl_setperm_key((key_serial_t) arg2,
  1406. (key_perm_t) arg3);
  1407. case KEYCTL_INSTANTIATE:
  1408. return keyctl_instantiate_key((key_serial_t) arg2,
  1409. (const void __user *) arg3,
  1410. (size_t) arg4,
  1411. (key_serial_t) arg5);
  1412. case KEYCTL_NEGATE:
  1413. return keyctl_negate_key((key_serial_t) arg2,
  1414. (unsigned) arg3,
  1415. (key_serial_t) arg4);
  1416. case KEYCTL_SET_REQKEY_KEYRING:
  1417. return keyctl_set_reqkey_keyring(arg2);
  1418. case KEYCTL_SET_TIMEOUT:
  1419. return keyctl_set_timeout((key_serial_t) arg2,
  1420. (unsigned) arg3);
  1421. case KEYCTL_ASSUME_AUTHORITY:
  1422. return keyctl_assume_authority((key_serial_t) arg2);
  1423. case KEYCTL_GET_SECURITY:
  1424. return keyctl_get_security((key_serial_t) arg2,
  1425. (char __user *) arg3,
  1426. (size_t) arg4);
  1427. case KEYCTL_SESSION_TO_PARENT:
  1428. return keyctl_session_to_parent();
  1429. case KEYCTL_REJECT:
  1430. return keyctl_reject_key((key_serial_t) arg2,
  1431. (unsigned) arg3,
  1432. (unsigned) arg4,
  1433. (key_serial_t) arg5);
  1434. case KEYCTL_INSTANTIATE_IOV:
  1435. return keyctl_instantiate_key_iov(
  1436. (key_serial_t) arg2,
  1437. (const struct iovec __user *) arg3,
  1438. (unsigned) arg4,
  1439. (key_serial_t) arg5);
  1440. case KEYCTL_INVALIDATE:
  1441. return keyctl_invalidate_key((key_serial_t) arg2);
  1442. default:
  1443. return -EOPNOTSUPP;
  1444. }
  1445. }