keyctl.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /* keyctl.c: userspace keyctl 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 -EFAULT;
  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. /*
  42. * extract the description of a new key from userspace and either add it as a
  43. * new key to the specified keyring or update a matching key in that keyring
  44. * - the keyring must be writable
  45. * - returns the new key's serial number
  46. * - implements add_key()
  47. */
  48. asmlinkage long sys_add_key(const char __user *_type,
  49. const char __user *_description,
  50. const void __user *_payload,
  51. size_t plen,
  52. key_serial_t ringid)
  53. {
  54. key_ref_t keyring_ref, key_ref;
  55. char type[32], *description;
  56. void *payload;
  57. long ret;
  58. bool vm;
  59. ret = -EINVAL;
  60. if (plen > 1024 * 1024 - 1)
  61. goto error;
  62. /* draw all the data into kernel space */
  63. ret = key_get_type_from_user(type, _type, sizeof(type));
  64. if (ret < 0)
  65. goto error;
  66. description = strndup_user(_description, PAGE_SIZE);
  67. if (IS_ERR(description)) {
  68. ret = PTR_ERR(description);
  69. goto error;
  70. }
  71. /* pull the payload in if one was supplied */
  72. payload = NULL;
  73. vm = false;
  74. if (_payload) {
  75. ret = -ENOMEM;
  76. payload = kmalloc(plen, GFP_KERNEL);
  77. if (!payload) {
  78. if (plen <= PAGE_SIZE)
  79. goto error2;
  80. vm = true;
  81. payload = vmalloc(plen);
  82. if (!payload)
  83. goto error2;
  84. }
  85. ret = -EFAULT;
  86. if (copy_from_user(payload, _payload, plen) != 0)
  87. goto error3;
  88. }
  89. /* find the target keyring (which must be writable) */
  90. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  91. if (IS_ERR(keyring_ref)) {
  92. ret = PTR_ERR(keyring_ref);
  93. goto error3;
  94. }
  95. /* create or update the requested key and add it to the target
  96. * keyring */
  97. key_ref = key_create_or_update(keyring_ref, type, description,
  98. payload, plen, KEY_PERM_UNDEF,
  99. KEY_ALLOC_IN_QUOTA);
  100. if (!IS_ERR(key_ref)) {
  101. ret = key_ref_to_ptr(key_ref)->serial;
  102. key_ref_put(key_ref);
  103. }
  104. else {
  105. ret = PTR_ERR(key_ref);
  106. }
  107. key_ref_put(keyring_ref);
  108. error3:
  109. if (!vm)
  110. kfree(payload);
  111. else
  112. vfree(payload);
  113. error2:
  114. kfree(description);
  115. error:
  116. return ret;
  117. } /* end sys_add_key() */
  118. /*****************************************************************************/
  119. /*
  120. * search the process keyrings for a matching key
  121. * - nested keyrings may also be searched if they have Search permission
  122. * - if a key is found, it will be attached to the destination keyring if
  123. * there's one specified
  124. * - /sbin/request-key will be invoked if _callout_info is non-NULL
  125. * - the _callout_info string will be passed to /sbin/request-key
  126. * - if the _callout_info string is empty, it will be rendered as "-"
  127. * - implements request_key()
  128. */
  129. asmlinkage long sys_request_key(const char __user *_type,
  130. const char __user *_description,
  131. const char __user *_callout_info,
  132. key_serial_t destringid)
  133. {
  134. struct key_type *ktype;
  135. struct key *key;
  136. key_ref_t dest_ref;
  137. size_t callout_len;
  138. char type[32], *description, *callout_info;
  139. long ret;
  140. /* pull the type into kernel space */
  141. ret = key_get_type_from_user(type, _type, sizeof(type));
  142. if (ret < 0)
  143. goto error;
  144. /* pull the description into kernel space */
  145. description = strndup_user(_description, PAGE_SIZE);
  146. if (IS_ERR(description)) {
  147. ret = PTR_ERR(description);
  148. goto error;
  149. }
  150. /* pull the callout info into kernel space */
  151. callout_info = NULL;
  152. callout_len = 0;
  153. if (_callout_info) {
  154. callout_info = strndup_user(_callout_info, PAGE_SIZE);
  155. if (IS_ERR(callout_info)) {
  156. ret = PTR_ERR(callout_info);
  157. goto error2;
  158. }
  159. callout_len = strlen(callout_info);
  160. }
  161. /* get the destination keyring if specified */
  162. dest_ref = NULL;
  163. if (destringid) {
  164. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  165. if (IS_ERR(dest_ref)) {
  166. ret = PTR_ERR(dest_ref);
  167. goto error3;
  168. }
  169. }
  170. /* find the key type */
  171. ktype = key_type_lookup(type);
  172. if (IS_ERR(ktype)) {
  173. ret = PTR_ERR(ktype);
  174. goto error4;
  175. }
  176. /* do the search */
  177. key = request_key_and_link(ktype, description, callout_info,
  178. callout_len, NULL, key_ref_to_ptr(dest_ref),
  179. KEY_ALLOC_IN_QUOTA);
  180. if (IS_ERR(key)) {
  181. ret = PTR_ERR(key);
  182. goto error5;
  183. }
  184. ret = key->serial;
  185. key_put(key);
  186. error5:
  187. key_type_put(ktype);
  188. error4:
  189. key_ref_put(dest_ref);
  190. error3:
  191. kfree(callout_info);
  192. error2:
  193. kfree(description);
  194. error:
  195. return ret;
  196. } /* end sys_request_key() */
  197. /*****************************************************************************/
  198. /*
  199. * get the ID of the specified process keyring
  200. * - the keyring must have search permission to be found
  201. * - implements keyctl(KEYCTL_GET_KEYRING_ID)
  202. */
  203. long keyctl_get_keyring_ID(key_serial_t id, int create)
  204. {
  205. key_ref_t key_ref;
  206. long ret;
  207. key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
  208. if (IS_ERR(key_ref)) {
  209. ret = PTR_ERR(key_ref);
  210. goto error;
  211. }
  212. ret = key_ref_to_ptr(key_ref)->serial;
  213. key_ref_put(key_ref);
  214. error:
  215. return ret;
  216. } /* end keyctl_get_keyring_ID() */
  217. /*****************************************************************************/
  218. /*
  219. * join the session keyring
  220. * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
  221. */
  222. long keyctl_join_session_keyring(const char __user *_name)
  223. {
  224. char *name;
  225. long ret;
  226. /* fetch the name from userspace */
  227. name = NULL;
  228. if (_name) {
  229. name = strndup_user(_name, PAGE_SIZE);
  230. if (IS_ERR(name)) {
  231. ret = PTR_ERR(name);
  232. goto error;
  233. }
  234. }
  235. /* join the session */
  236. ret = join_session_keyring(name);
  237. error:
  238. return ret;
  239. } /* end keyctl_join_session_keyring() */
  240. /*****************************************************************************/
  241. /*
  242. * update a key's data payload
  243. * - the key must be writable
  244. * - implements keyctl(KEYCTL_UPDATE)
  245. */
  246. long keyctl_update_key(key_serial_t id,
  247. const void __user *_payload,
  248. size_t plen)
  249. {
  250. key_ref_t key_ref;
  251. void *payload;
  252. long ret;
  253. ret = -EINVAL;
  254. if (plen > PAGE_SIZE)
  255. goto error;
  256. /* pull the payload in if one was supplied */
  257. payload = NULL;
  258. if (_payload) {
  259. ret = -ENOMEM;
  260. payload = kmalloc(plen, GFP_KERNEL);
  261. if (!payload)
  262. goto error;
  263. ret = -EFAULT;
  264. if (copy_from_user(payload, _payload, plen) != 0)
  265. goto error2;
  266. }
  267. /* find the target key (which must be writable) */
  268. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  269. if (IS_ERR(key_ref)) {
  270. ret = PTR_ERR(key_ref);
  271. goto error2;
  272. }
  273. /* update the key */
  274. ret = key_update(key_ref, payload, plen);
  275. key_ref_put(key_ref);
  276. error2:
  277. kfree(payload);
  278. error:
  279. return ret;
  280. } /* end keyctl_update_key() */
  281. /*****************************************************************************/
  282. /*
  283. * revoke a key
  284. * - the key must be writable
  285. * - implements keyctl(KEYCTL_REVOKE)
  286. */
  287. long keyctl_revoke_key(key_serial_t id)
  288. {
  289. key_ref_t key_ref;
  290. long ret;
  291. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  292. if (IS_ERR(key_ref)) {
  293. ret = PTR_ERR(key_ref);
  294. goto error;
  295. }
  296. key_revoke(key_ref_to_ptr(key_ref));
  297. ret = 0;
  298. key_ref_put(key_ref);
  299. error:
  300. return ret;
  301. } /* end keyctl_revoke_key() */
  302. /*****************************************************************************/
  303. /*
  304. * clear the specified process keyring
  305. * - the keyring must be writable
  306. * - implements keyctl(KEYCTL_CLEAR)
  307. */
  308. long keyctl_keyring_clear(key_serial_t ringid)
  309. {
  310. key_ref_t keyring_ref;
  311. long ret;
  312. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  313. if (IS_ERR(keyring_ref)) {
  314. ret = PTR_ERR(keyring_ref);
  315. goto error;
  316. }
  317. ret = keyring_clear(key_ref_to_ptr(keyring_ref));
  318. key_ref_put(keyring_ref);
  319. error:
  320. return ret;
  321. } /* end keyctl_keyring_clear() */
  322. /*****************************************************************************/
  323. /*
  324. * link a key into a keyring
  325. * - the keyring must be writable
  326. * - the key must be linkable
  327. * - implements keyctl(KEYCTL_LINK)
  328. */
  329. long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
  330. {
  331. key_ref_t keyring_ref, key_ref;
  332. long ret;
  333. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  334. if (IS_ERR(keyring_ref)) {
  335. ret = PTR_ERR(keyring_ref);
  336. goto error;
  337. }
  338. key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
  339. if (IS_ERR(key_ref)) {
  340. ret = PTR_ERR(key_ref);
  341. goto error2;
  342. }
  343. ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  344. key_ref_put(key_ref);
  345. error2:
  346. key_ref_put(keyring_ref);
  347. error:
  348. return ret;
  349. } /* end keyctl_keyring_link() */
  350. /*****************************************************************************/
  351. /*
  352. * unlink the first attachment of a key from a keyring
  353. * - the keyring must be writable
  354. * - we don't need any permissions on the key
  355. * - implements keyctl(KEYCTL_UNLINK)
  356. */
  357. long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
  358. {
  359. key_ref_t keyring_ref, key_ref;
  360. long ret;
  361. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
  362. if (IS_ERR(keyring_ref)) {
  363. ret = PTR_ERR(keyring_ref);
  364. goto error;
  365. }
  366. key_ref = lookup_user_key(NULL, id, 0, 0, 0);
  367. if (IS_ERR(key_ref)) {
  368. ret = PTR_ERR(key_ref);
  369. goto error2;
  370. }
  371. ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  372. key_ref_put(key_ref);
  373. error2:
  374. key_ref_put(keyring_ref);
  375. error:
  376. return ret;
  377. } /* end keyctl_keyring_unlink() */
  378. /*****************************************************************************/
  379. /*
  380. * describe a user key
  381. * - the key must have view permission
  382. * - if there's a buffer, we place up to buflen bytes of data into it
  383. * - unless there's an error, we return the amount of description available,
  384. * irrespective of how much we may have copied
  385. * - the description is formatted thus:
  386. * type;uid;gid;perm;description<NUL>
  387. * - implements keyctl(KEYCTL_DESCRIBE)
  388. */
  389. long keyctl_describe_key(key_serial_t keyid,
  390. char __user *buffer,
  391. size_t buflen)
  392. {
  393. struct key *key, *instkey;
  394. key_ref_t key_ref;
  395. char *tmpbuf;
  396. long ret;
  397. key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
  398. if (IS_ERR(key_ref)) {
  399. /* viewing a key under construction is permitted if we have the
  400. * authorisation token handy */
  401. if (PTR_ERR(key_ref) == -EACCES) {
  402. instkey = key_get_instantiation_authkey(keyid);
  403. if (!IS_ERR(instkey)) {
  404. key_put(instkey);
  405. key_ref = lookup_user_key(NULL, keyid,
  406. 0, 1, 0);
  407. if (!IS_ERR(key_ref))
  408. goto okay;
  409. }
  410. }
  411. ret = PTR_ERR(key_ref);
  412. goto error;
  413. }
  414. okay:
  415. /* calculate how much description we're going to return */
  416. ret = -ENOMEM;
  417. tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  418. if (!tmpbuf)
  419. goto error2;
  420. key = key_ref_to_ptr(key_ref);
  421. ret = snprintf(tmpbuf, PAGE_SIZE - 1,
  422. "%s;%d;%d;%08x;%s",
  423. key_ref_to_ptr(key_ref)->type->name,
  424. key_ref_to_ptr(key_ref)->uid,
  425. key_ref_to_ptr(key_ref)->gid,
  426. key_ref_to_ptr(key_ref)->perm,
  427. key_ref_to_ptr(key_ref)->description ?
  428. key_ref_to_ptr(key_ref)->description : ""
  429. );
  430. /* include a NUL char at the end of the data */
  431. if (ret > PAGE_SIZE - 1)
  432. ret = PAGE_SIZE - 1;
  433. tmpbuf[ret] = 0;
  434. ret++;
  435. /* consider returning the data */
  436. if (buffer && buflen > 0) {
  437. if (buflen > ret)
  438. buflen = ret;
  439. if (copy_to_user(buffer, tmpbuf, buflen) != 0)
  440. ret = -EFAULT;
  441. }
  442. kfree(tmpbuf);
  443. error2:
  444. key_ref_put(key_ref);
  445. error:
  446. return ret;
  447. } /* end keyctl_describe_key() */
  448. /*****************************************************************************/
  449. /*
  450. * search the specified keyring for a matching key
  451. * - the start keyring must be searchable
  452. * - nested keyrings may also be searched if they are searchable
  453. * - only keys with search permission may be found
  454. * - if a key is found, it will be attached to the destination keyring if
  455. * there's one specified
  456. * - implements keyctl(KEYCTL_SEARCH)
  457. */
  458. long keyctl_keyring_search(key_serial_t ringid,
  459. const char __user *_type,
  460. const char __user *_description,
  461. key_serial_t destringid)
  462. {
  463. struct key_type *ktype;
  464. key_ref_t keyring_ref, key_ref, dest_ref;
  465. char type[32], *description;
  466. long ret;
  467. /* pull the type and description into kernel space */
  468. ret = key_get_type_from_user(type, _type, sizeof(type));
  469. if (ret < 0)
  470. goto error;
  471. description = strndup_user(_description, PAGE_SIZE);
  472. if (IS_ERR(description)) {
  473. ret = PTR_ERR(description);
  474. goto error;
  475. }
  476. /* get the keyring at which to begin the search */
  477. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
  478. if (IS_ERR(keyring_ref)) {
  479. ret = PTR_ERR(keyring_ref);
  480. goto error2;
  481. }
  482. /* get the destination keyring if specified */
  483. dest_ref = NULL;
  484. if (destringid) {
  485. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  486. if (IS_ERR(dest_ref)) {
  487. ret = PTR_ERR(dest_ref);
  488. goto error3;
  489. }
  490. }
  491. /* find the key type */
  492. ktype = key_type_lookup(type);
  493. if (IS_ERR(ktype)) {
  494. ret = PTR_ERR(ktype);
  495. goto error4;
  496. }
  497. /* do the search */
  498. key_ref = keyring_search(keyring_ref, ktype, description);
  499. if (IS_ERR(key_ref)) {
  500. ret = PTR_ERR(key_ref);
  501. /* treat lack or presence of a negative key the same */
  502. if (ret == -EAGAIN)
  503. ret = -ENOKEY;
  504. goto error5;
  505. }
  506. /* link the resulting key to the destination keyring if we can */
  507. if (dest_ref) {
  508. ret = key_permission(key_ref, KEY_LINK);
  509. if (ret < 0)
  510. goto error6;
  511. ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
  512. if (ret < 0)
  513. goto error6;
  514. }
  515. ret = key_ref_to_ptr(key_ref)->serial;
  516. error6:
  517. key_ref_put(key_ref);
  518. error5:
  519. key_type_put(ktype);
  520. error4:
  521. key_ref_put(dest_ref);
  522. error3:
  523. key_ref_put(keyring_ref);
  524. error2:
  525. kfree(description);
  526. error:
  527. return ret;
  528. } /* end keyctl_keyring_search() */
  529. /*****************************************************************************/
  530. /*
  531. * read a user key's payload
  532. * - the keyring must be readable or the key must be searchable from the
  533. * process's keyrings
  534. * - if there's a buffer, we place up to buflen bytes of data into it
  535. * - unless there's an error, we return the amount of data in the key,
  536. * irrespective of how much we may have copied
  537. * - implements keyctl(KEYCTL_READ)
  538. */
  539. long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
  540. {
  541. struct key *key;
  542. key_ref_t key_ref;
  543. long ret;
  544. /* find the key first */
  545. key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
  546. if (IS_ERR(key_ref)) {
  547. ret = -ENOKEY;
  548. goto error;
  549. }
  550. key = key_ref_to_ptr(key_ref);
  551. /* see if we can read it directly */
  552. ret = key_permission(key_ref, KEY_READ);
  553. if (ret == 0)
  554. goto can_read_key;
  555. if (ret != -EACCES)
  556. goto error;
  557. /* we can't; see if it's searchable from this process's keyrings
  558. * - we automatically take account of the fact that it may be
  559. * dangling off an instantiation key
  560. */
  561. if (!is_key_possessed(key_ref)) {
  562. ret = -EACCES;
  563. goto error2;
  564. }
  565. /* the key is probably readable - now try to read it */
  566. can_read_key:
  567. ret = key_validate(key);
  568. if (ret == 0) {
  569. ret = -EOPNOTSUPP;
  570. if (key->type->read) {
  571. /* read the data with the semaphore held (since we
  572. * might sleep) */
  573. down_read(&key->sem);
  574. ret = key->type->read(key, buffer, buflen);
  575. up_read(&key->sem);
  576. }
  577. }
  578. error2:
  579. key_put(key);
  580. error:
  581. return ret;
  582. } /* end keyctl_read_key() */
  583. /*****************************************************************************/
  584. /*
  585. * change the ownership of a key
  586. * - the keyring owned by the changer
  587. * - if the uid or gid is -1, then that parameter is not changed
  588. * - implements keyctl(KEYCTL_CHOWN)
  589. */
  590. long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
  591. {
  592. struct key_user *newowner, *zapowner = NULL;
  593. struct key *key;
  594. key_ref_t key_ref;
  595. long ret;
  596. ret = 0;
  597. if (uid == (uid_t) -1 && gid == (gid_t) -1)
  598. goto error;
  599. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  600. if (IS_ERR(key_ref)) {
  601. ret = PTR_ERR(key_ref);
  602. goto error;
  603. }
  604. key = key_ref_to_ptr(key_ref);
  605. /* make the changes with the locks held to prevent chown/chown races */
  606. ret = -EACCES;
  607. down_write(&key->sem);
  608. if (!capable(CAP_SYS_ADMIN)) {
  609. /* only the sysadmin can chown a key to some other UID */
  610. if (uid != (uid_t) -1 && key->uid != uid)
  611. goto error_put;
  612. /* only the sysadmin can set the key's GID to a group other
  613. * than one of those that the current process subscribes to */
  614. if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
  615. goto error_put;
  616. }
  617. /* change the UID */
  618. if (uid != (uid_t) -1 && uid != key->uid) {
  619. ret = -ENOMEM;
  620. newowner = key_user_lookup(uid);
  621. if (!newowner)
  622. goto error_put;
  623. /* transfer the quota burden to the new user */
  624. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  625. unsigned maxkeys = (uid == 0) ?
  626. key_quota_root_maxkeys : key_quota_maxkeys;
  627. unsigned maxbytes = (uid == 0) ?
  628. key_quota_root_maxbytes : key_quota_maxbytes;
  629. spin_lock(&newowner->lock);
  630. if (newowner->qnkeys + 1 >= maxkeys ||
  631. newowner->qnbytes + key->quotalen >= maxbytes ||
  632. newowner->qnbytes + key->quotalen <
  633. newowner->qnbytes)
  634. goto quota_overrun;
  635. newowner->qnkeys++;
  636. newowner->qnbytes += key->quotalen;
  637. spin_unlock(&newowner->lock);
  638. spin_lock(&key->user->lock);
  639. key->user->qnkeys--;
  640. key->user->qnbytes -= key->quotalen;
  641. spin_unlock(&key->user->lock);
  642. }
  643. atomic_dec(&key->user->nkeys);
  644. atomic_inc(&newowner->nkeys);
  645. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  646. atomic_dec(&key->user->nikeys);
  647. atomic_inc(&newowner->nikeys);
  648. }
  649. zapowner = key->user;
  650. key->user = newowner;
  651. key->uid = uid;
  652. }
  653. /* change the GID */
  654. if (gid != (gid_t) -1)
  655. key->gid = gid;
  656. ret = 0;
  657. error_put:
  658. up_write(&key->sem);
  659. key_put(key);
  660. if (zapowner)
  661. key_user_put(zapowner);
  662. error:
  663. return ret;
  664. quota_overrun:
  665. spin_unlock(&newowner->lock);
  666. zapowner = newowner;
  667. ret = -EDQUOT;
  668. goto error_put;
  669. } /* end keyctl_chown_key() */
  670. /*****************************************************************************/
  671. /*
  672. * change the permission mask on a key
  673. * - the keyring owned by the changer
  674. * - implements keyctl(KEYCTL_SETPERM)
  675. */
  676. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  677. {
  678. struct key *key;
  679. key_ref_t key_ref;
  680. long ret;
  681. ret = -EINVAL;
  682. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  683. goto error;
  684. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  685. if (IS_ERR(key_ref)) {
  686. ret = PTR_ERR(key_ref);
  687. goto error;
  688. }
  689. key = key_ref_to_ptr(key_ref);
  690. /* make the changes with the locks held to prevent chown/chmod races */
  691. ret = -EACCES;
  692. down_write(&key->sem);
  693. /* if we're not the sysadmin, we can only change a key that we own */
  694. if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
  695. key->perm = perm;
  696. ret = 0;
  697. }
  698. up_write(&key->sem);
  699. key_put(key);
  700. error:
  701. return ret;
  702. } /* end keyctl_setperm_key() */
  703. /*****************************************************************************/
  704. /*
  705. * instantiate the key with the specified payload, and, if one is given, link
  706. * the key into the keyring
  707. */
  708. long keyctl_instantiate_key(key_serial_t id,
  709. const void __user *_payload,
  710. size_t plen,
  711. key_serial_t ringid)
  712. {
  713. struct request_key_auth *rka;
  714. struct key *instkey;
  715. key_ref_t keyring_ref;
  716. void *payload;
  717. long ret;
  718. bool vm = false;
  719. ret = -EINVAL;
  720. if (plen > 1024 * 1024 - 1)
  721. goto error;
  722. /* the appropriate instantiation authorisation key must have been
  723. * assumed before calling this */
  724. ret = -EPERM;
  725. instkey = current->request_key_auth;
  726. if (!instkey)
  727. goto error;
  728. rka = instkey->payload.data;
  729. if (rka->target_key->serial != id)
  730. goto error;
  731. /* pull the payload in if one was supplied */
  732. payload = NULL;
  733. if (_payload) {
  734. ret = -ENOMEM;
  735. payload = kmalloc(plen, GFP_KERNEL);
  736. if (!payload) {
  737. if (plen <= PAGE_SIZE)
  738. goto error;
  739. vm = true;
  740. payload = vmalloc(plen);
  741. if (!payload)
  742. goto error;
  743. }
  744. ret = -EFAULT;
  745. if (copy_from_user(payload, _payload, plen) != 0)
  746. goto error2;
  747. }
  748. /* find the destination keyring amongst those belonging to the
  749. * requesting task */
  750. keyring_ref = NULL;
  751. if (ringid) {
  752. keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
  753. KEY_WRITE);
  754. if (IS_ERR(keyring_ref)) {
  755. ret = PTR_ERR(keyring_ref);
  756. goto error2;
  757. }
  758. }
  759. /* instantiate the key and link it into a keyring */
  760. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  761. key_ref_to_ptr(keyring_ref), instkey);
  762. key_ref_put(keyring_ref);
  763. /* discard the assumed authority if it's just been disabled by
  764. * instantiation of the key */
  765. if (ret == 0) {
  766. key_put(current->request_key_auth);
  767. current->request_key_auth = NULL;
  768. }
  769. error2:
  770. if (!vm)
  771. kfree(payload);
  772. else
  773. vfree(payload);
  774. error:
  775. return ret;
  776. } /* end keyctl_instantiate_key() */
  777. /*****************************************************************************/
  778. /*
  779. * negatively instantiate the key with the given timeout (in seconds), and, if
  780. * one is given, link the key into the keyring
  781. */
  782. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  783. {
  784. struct request_key_auth *rka;
  785. struct key *instkey;
  786. key_ref_t keyring_ref;
  787. long ret;
  788. /* the appropriate instantiation authorisation key must have been
  789. * assumed before calling this */
  790. ret = -EPERM;
  791. instkey = current->request_key_auth;
  792. if (!instkey)
  793. goto error;
  794. rka = instkey->payload.data;
  795. if (rka->target_key->serial != id)
  796. goto error;
  797. /* find the destination keyring if present (which must also be
  798. * writable) */
  799. keyring_ref = NULL;
  800. if (ringid) {
  801. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  802. if (IS_ERR(keyring_ref)) {
  803. ret = PTR_ERR(keyring_ref);
  804. goto error;
  805. }
  806. }
  807. /* instantiate the key and link it into a keyring */
  808. ret = key_negate_and_link(rka->target_key, timeout,
  809. key_ref_to_ptr(keyring_ref), instkey);
  810. key_ref_put(keyring_ref);
  811. /* discard the assumed authority if it's just been disabled by
  812. * instantiation of the key */
  813. if (ret == 0) {
  814. key_put(current->request_key_auth);
  815. current->request_key_auth = NULL;
  816. }
  817. error:
  818. return ret;
  819. } /* end keyctl_negate_key() */
  820. /*****************************************************************************/
  821. /*
  822. * set the default keyring in which request_key() will cache keys
  823. * - return the old setting
  824. */
  825. long keyctl_set_reqkey_keyring(int reqkey_defl)
  826. {
  827. int ret;
  828. switch (reqkey_defl) {
  829. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  830. ret = install_thread_keyring(current);
  831. if (ret < 0)
  832. return ret;
  833. goto set;
  834. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  835. ret = install_process_keyring(current);
  836. if (ret < 0)
  837. return ret;
  838. case KEY_REQKEY_DEFL_DEFAULT:
  839. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  840. case KEY_REQKEY_DEFL_USER_KEYRING:
  841. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  842. set:
  843. current->jit_keyring = reqkey_defl;
  844. case KEY_REQKEY_DEFL_NO_CHANGE:
  845. return current->jit_keyring;
  846. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  847. default:
  848. return -EINVAL;
  849. }
  850. } /* end keyctl_set_reqkey_keyring() */
  851. /*****************************************************************************/
  852. /*
  853. * set or clear the timeout for a key
  854. */
  855. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  856. {
  857. struct timespec now;
  858. struct key *key;
  859. key_ref_t key_ref;
  860. time_t expiry;
  861. long ret;
  862. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  863. if (IS_ERR(key_ref)) {
  864. ret = PTR_ERR(key_ref);
  865. goto error;
  866. }
  867. key = key_ref_to_ptr(key_ref);
  868. /* make the changes with the locks held to prevent races */
  869. down_write(&key->sem);
  870. expiry = 0;
  871. if (timeout > 0) {
  872. now = current_kernel_time();
  873. expiry = now.tv_sec + timeout;
  874. }
  875. key->expiry = expiry;
  876. up_write(&key->sem);
  877. key_put(key);
  878. ret = 0;
  879. error:
  880. return ret;
  881. } /* end keyctl_set_timeout() */
  882. /*****************************************************************************/
  883. /*
  884. * assume the authority to instantiate the specified key
  885. */
  886. long keyctl_assume_authority(key_serial_t id)
  887. {
  888. struct key *authkey;
  889. long ret;
  890. /* special key IDs aren't permitted */
  891. ret = -EINVAL;
  892. if (id < 0)
  893. goto error;
  894. /* we divest ourselves of authority if given an ID of 0 */
  895. if (id == 0) {
  896. key_put(current->request_key_auth);
  897. current->request_key_auth = NULL;
  898. ret = 0;
  899. goto error;
  900. }
  901. /* attempt to assume the authority temporarily granted to us whilst we
  902. * instantiate the specified key
  903. * - the authorisation key must be in the current task's keyrings
  904. * somewhere
  905. */
  906. authkey = key_get_instantiation_authkey(id);
  907. if (IS_ERR(authkey)) {
  908. ret = PTR_ERR(authkey);
  909. goto error;
  910. }
  911. key_put(current->request_key_auth);
  912. current->request_key_auth = authkey;
  913. ret = authkey->serial;
  914. error:
  915. return ret;
  916. } /* end keyctl_assume_authority() */
  917. /*
  918. * get the security label of a key
  919. * - the key must grant us view permission
  920. * - if there's a buffer, we place up to buflen bytes of data into it
  921. * - unless there's an error, we return the amount of information available,
  922. * irrespective of how much we may have copied (including the terminal NUL)
  923. * - implements keyctl(KEYCTL_GET_SECURITY)
  924. */
  925. long keyctl_get_security(key_serial_t keyid,
  926. char __user *buffer,
  927. size_t buflen)
  928. {
  929. struct key *key, *instkey;
  930. key_ref_t key_ref;
  931. char *context;
  932. long ret;
  933. key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
  934. if (IS_ERR(key_ref)) {
  935. if (PTR_ERR(key_ref) != -EACCES)
  936. return PTR_ERR(key_ref);
  937. /* viewing a key under construction is also permitted if we
  938. * have the authorisation token handy */
  939. instkey = key_get_instantiation_authkey(keyid);
  940. if (IS_ERR(instkey))
  941. return PTR_ERR(key_ref);
  942. key_put(instkey);
  943. key_ref = lookup_user_key(NULL, keyid, 0, 1, 0);
  944. if (IS_ERR(key_ref))
  945. return PTR_ERR(key_ref);
  946. }
  947. key = key_ref_to_ptr(key_ref);
  948. ret = security_key_getsecurity(key, &context);
  949. if (ret == 0) {
  950. /* if no information was returned, give userspace an empty
  951. * string */
  952. ret = 1;
  953. if (buffer && buflen > 0 &&
  954. copy_to_user(buffer, "", 1) != 0)
  955. ret = -EFAULT;
  956. } else if (ret > 0) {
  957. /* return as much data as there's room for */
  958. if (buffer && buflen > 0) {
  959. if (buflen > ret)
  960. buflen = ret;
  961. if (copy_to_user(buffer, context, buflen) != 0)
  962. ret = -EFAULT;
  963. }
  964. kfree(context);
  965. }
  966. key_ref_put(key_ref);
  967. return ret;
  968. }
  969. /*****************************************************************************/
  970. /*
  971. * the key control system call
  972. */
  973. asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
  974. unsigned long arg4, unsigned long arg5)
  975. {
  976. switch (option) {
  977. case KEYCTL_GET_KEYRING_ID:
  978. return keyctl_get_keyring_ID((key_serial_t) arg2,
  979. (int) arg3);
  980. case KEYCTL_JOIN_SESSION_KEYRING:
  981. return keyctl_join_session_keyring((const char __user *) arg2);
  982. case KEYCTL_UPDATE:
  983. return keyctl_update_key((key_serial_t) arg2,
  984. (const void __user *) arg3,
  985. (size_t) arg4);
  986. case KEYCTL_REVOKE:
  987. return keyctl_revoke_key((key_serial_t) arg2);
  988. case KEYCTL_DESCRIBE:
  989. return keyctl_describe_key((key_serial_t) arg2,
  990. (char __user *) arg3,
  991. (unsigned) arg4);
  992. case KEYCTL_CLEAR:
  993. return keyctl_keyring_clear((key_serial_t) arg2);
  994. case KEYCTL_LINK:
  995. return keyctl_keyring_link((key_serial_t) arg2,
  996. (key_serial_t) arg3);
  997. case KEYCTL_UNLINK:
  998. return keyctl_keyring_unlink((key_serial_t) arg2,
  999. (key_serial_t) arg3);
  1000. case KEYCTL_SEARCH:
  1001. return keyctl_keyring_search((key_serial_t) arg2,
  1002. (const char __user *) arg3,
  1003. (const char __user *) arg4,
  1004. (key_serial_t) arg5);
  1005. case KEYCTL_READ:
  1006. return keyctl_read_key((key_serial_t) arg2,
  1007. (char __user *) arg3,
  1008. (size_t) arg4);
  1009. case KEYCTL_CHOWN:
  1010. return keyctl_chown_key((key_serial_t) arg2,
  1011. (uid_t) arg3,
  1012. (gid_t) arg4);
  1013. case KEYCTL_SETPERM:
  1014. return keyctl_setperm_key((key_serial_t) arg2,
  1015. (key_perm_t) arg3);
  1016. case KEYCTL_INSTANTIATE:
  1017. return keyctl_instantiate_key((key_serial_t) arg2,
  1018. (const void __user *) arg3,
  1019. (size_t) arg4,
  1020. (key_serial_t) arg5);
  1021. case KEYCTL_NEGATE:
  1022. return keyctl_negate_key((key_serial_t) arg2,
  1023. (unsigned) arg3,
  1024. (key_serial_t) arg4);
  1025. case KEYCTL_SET_REQKEY_KEYRING:
  1026. return keyctl_set_reqkey_keyring(arg2);
  1027. case KEYCTL_SET_TIMEOUT:
  1028. return keyctl_set_timeout((key_serial_t) arg2,
  1029. (unsigned) arg3);
  1030. case KEYCTL_ASSUME_AUTHORITY:
  1031. return keyctl_assume_authority((key_serial_t) arg2);
  1032. case KEYCTL_GET_SECURITY:
  1033. return keyctl_get_security((key_serial_t) arg2,
  1034. (char *) arg3,
  1035. (size_t) arg4);
  1036. default:
  1037. return -EOPNOTSUPP;
  1038. }
  1039. } /* end sys_keyctl() */