keyctl.c 39 KB

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