keyctl.c 39 KB

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