keyctl.c 39 KB

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