keyctl.c 39 KB

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