keyctl.c 43 KB

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