keyctl.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. spin_lock(&newowner->lock);
  626. if (newowner->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
  627. newowner->qnbytes + key->quotalen >=
  628. KEYQUOTA_MAX_BYTES)
  629. goto quota_overrun;
  630. newowner->qnkeys++;
  631. newowner->qnbytes += key->quotalen;
  632. spin_unlock(&newowner->lock);
  633. spin_lock(&key->user->lock);
  634. key->user->qnkeys--;
  635. key->user->qnbytes -= key->quotalen;
  636. spin_unlock(&key->user->lock);
  637. }
  638. atomic_dec(&key->user->nkeys);
  639. atomic_inc(&newowner->nkeys);
  640. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  641. atomic_dec(&key->user->nikeys);
  642. atomic_inc(&newowner->nikeys);
  643. }
  644. zapowner = key->user;
  645. key->user = newowner;
  646. key->uid = uid;
  647. }
  648. /* change the GID */
  649. if (gid != (gid_t) -1)
  650. key->gid = gid;
  651. ret = 0;
  652. error_put:
  653. up_write(&key->sem);
  654. key_put(key);
  655. if (zapowner)
  656. key_user_put(zapowner);
  657. error:
  658. return ret;
  659. quota_overrun:
  660. spin_unlock(&newowner->lock);
  661. zapowner = newowner;
  662. ret = -EDQUOT;
  663. goto error_put;
  664. } /* end keyctl_chown_key() */
  665. /*****************************************************************************/
  666. /*
  667. * change the permission mask on a key
  668. * - the keyring owned by the changer
  669. * - implements keyctl(KEYCTL_SETPERM)
  670. */
  671. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  672. {
  673. struct key *key;
  674. key_ref_t key_ref;
  675. long ret;
  676. ret = -EINVAL;
  677. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  678. goto error;
  679. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  680. if (IS_ERR(key_ref)) {
  681. ret = PTR_ERR(key_ref);
  682. goto error;
  683. }
  684. key = key_ref_to_ptr(key_ref);
  685. /* make the changes with the locks held to prevent chown/chmod races */
  686. ret = -EACCES;
  687. down_write(&key->sem);
  688. /* if we're not the sysadmin, we can only change a key that we own */
  689. if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
  690. key->perm = perm;
  691. ret = 0;
  692. }
  693. up_write(&key->sem);
  694. key_put(key);
  695. error:
  696. return ret;
  697. } /* end keyctl_setperm_key() */
  698. /*****************************************************************************/
  699. /*
  700. * instantiate the key with the specified payload, and, if one is given, link
  701. * the key into the keyring
  702. */
  703. long keyctl_instantiate_key(key_serial_t id,
  704. const void __user *_payload,
  705. size_t plen,
  706. key_serial_t ringid)
  707. {
  708. struct request_key_auth *rka;
  709. struct key *instkey;
  710. key_ref_t keyring_ref;
  711. void *payload;
  712. long ret;
  713. bool vm = false;
  714. ret = -EINVAL;
  715. if (plen > 1024 * 1024 - 1)
  716. goto error;
  717. /* the appropriate instantiation authorisation key must have been
  718. * assumed before calling this */
  719. ret = -EPERM;
  720. instkey = current->request_key_auth;
  721. if (!instkey)
  722. goto error;
  723. rka = instkey->payload.data;
  724. if (rka->target_key->serial != id)
  725. goto error;
  726. /* pull the payload in if one was supplied */
  727. payload = NULL;
  728. if (_payload) {
  729. ret = -ENOMEM;
  730. payload = kmalloc(plen, GFP_KERNEL);
  731. if (!payload) {
  732. if (plen <= PAGE_SIZE)
  733. goto error;
  734. vm = true;
  735. payload = vmalloc(plen);
  736. if (!payload)
  737. goto error;
  738. }
  739. ret = -EFAULT;
  740. if (copy_from_user(payload, _payload, plen) != 0)
  741. goto error2;
  742. }
  743. /* find the destination keyring amongst those belonging to the
  744. * requesting task */
  745. keyring_ref = NULL;
  746. if (ringid) {
  747. keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
  748. KEY_WRITE);
  749. if (IS_ERR(keyring_ref)) {
  750. ret = PTR_ERR(keyring_ref);
  751. goto error2;
  752. }
  753. }
  754. /* instantiate the key and link it into a keyring */
  755. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  756. key_ref_to_ptr(keyring_ref), instkey);
  757. key_ref_put(keyring_ref);
  758. /* discard the assumed authority if it's just been disabled by
  759. * instantiation of the key */
  760. if (ret == 0) {
  761. key_put(current->request_key_auth);
  762. current->request_key_auth = NULL;
  763. }
  764. error2:
  765. if (!vm)
  766. kfree(payload);
  767. else
  768. vfree(payload);
  769. error:
  770. return ret;
  771. } /* end keyctl_instantiate_key() */
  772. /*****************************************************************************/
  773. /*
  774. * negatively instantiate the key with the given timeout (in seconds), and, if
  775. * one is given, link the key into the keyring
  776. */
  777. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  778. {
  779. struct request_key_auth *rka;
  780. struct key *instkey;
  781. key_ref_t keyring_ref;
  782. long ret;
  783. /* the appropriate instantiation authorisation key must have been
  784. * assumed before calling this */
  785. ret = -EPERM;
  786. instkey = current->request_key_auth;
  787. if (!instkey)
  788. goto error;
  789. rka = instkey->payload.data;
  790. if (rka->target_key->serial != id)
  791. goto error;
  792. /* find the destination keyring if present (which must also be
  793. * writable) */
  794. keyring_ref = NULL;
  795. if (ringid) {
  796. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  797. if (IS_ERR(keyring_ref)) {
  798. ret = PTR_ERR(keyring_ref);
  799. goto error;
  800. }
  801. }
  802. /* instantiate the key and link it into a keyring */
  803. ret = key_negate_and_link(rka->target_key, timeout,
  804. key_ref_to_ptr(keyring_ref), instkey);
  805. key_ref_put(keyring_ref);
  806. /* discard the assumed authority if it's just been disabled by
  807. * instantiation of the key */
  808. if (ret == 0) {
  809. key_put(current->request_key_auth);
  810. current->request_key_auth = NULL;
  811. }
  812. error:
  813. return ret;
  814. } /* end keyctl_negate_key() */
  815. /*****************************************************************************/
  816. /*
  817. * set the default keyring in which request_key() will cache keys
  818. * - return the old setting
  819. */
  820. long keyctl_set_reqkey_keyring(int reqkey_defl)
  821. {
  822. int ret;
  823. switch (reqkey_defl) {
  824. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  825. ret = install_thread_keyring(current);
  826. if (ret < 0)
  827. return ret;
  828. goto set;
  829. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  830. ret = install_process_keyring(current);
  831. if (ret < 0)
  832. return ret;
  833. case KEY_REQKEY_DEFL_DEFAULT:
  834. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  835. case KEY_REQKEY_DEFL_USER_KEYRING:
  836. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  837. set:
  838. current->jit_keyring = reqkey_defl;
  839. case KEY_REQKEY_DEFL_NO_CHANGE:
  840. return current->jit_keyring;
  841. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  842. default:
  843. return -EINVAL;
  844. }
  845. } /* end keyctl_set_reqkey_keyring() */
  846. /*****************************************************************************/
  847. /*
  848. * set or clear the timeout for a key
  849. */
  850. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  851. {
  852. struct timespec now;
  853. struct key *key;
  854. key_ref_t key_ref;
  855. time_t expiry;
  856. long ret;
  857. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  858. if (IS_ERR(key_ref)) {
  859. ret = PTR_ERR(key_ref);
  860. goto error;
  861. }
  862. key = key_ref_to_ptr(key_ref);
  863. /* make the changes with the locks held to prevent races */
  864. down_write(&key->sem);
  865. expiry = 0;
  866. if (timeout > 0) {
  867. now = current_kernel_time();
  868. expiry = now.tv_sec + timeout;
  869. }
  870. key->expiry = expiry;
  871. up_write(&key->sem);
  872. key_put(key);
  873. ret = 0;
  874. error:
  875. return ret;
  876. } /* end keyctl_set_timeout() */
  877. /*****************************************************************************/
  878. /*
  879. * assume the authority to instantiate the specified key
  880. */
  881. long keyctl_assume_authority(key_serial_t id)
  882. {
  883. struct key *authkey;
  884. long ret;
  885. /* special key IDs aren't permitted */
  886. ret = -EINVAL;
  887. if (id < 0)
  888. goto error;
  889. /* we divest ourselves of authority if given an ID of 0 */
  890. if (id == 0) {
  891. key_put(current->request_key_auth);
  892. current->request_key_auth = NULL;
  893. ret = 0;
  894. goto error;
  895. }
  896. /* attempt to assume the authority temporarily granted to us whilst we
  897. * instantiate the specified key
  898. * - the authorisation key must be in the current task's keyrings
  899. * somewhere
  900. */
  901. authkey = key_get_instantiation_authkey(id);
  902. if (IS_ERR(authkey)) {
  903. ret = PTR_ERR(authkey);
  904. goto error;
  905. }
  906. key_put(current->request_key_auth);
  907. current->request_key_auth = authkey;
  908. ret = authkey->serial;
  909. error:
  910. return ret;
  911. } /* end keyctl_assume_authority() */
  912. /*
  913. * get the security label of a key
  914. * - the key must grant us view permission
  915. * - if there's a buffer, we place up to buflen bytes of data into it
  916. * - unless there's an error, we return the amount of information available,
  917. * irrespective of how much we may have copied (including the terminal NUL)
  918. * - implements keyctl(KEYCTL_GET_SECURITY)
  919. */
  920. long keyctl_get_security(key_serial_t keyid,
  921. char __user *buffer,
  922. size_t buflen)
  923. {
  924. struct key *key, *instkey;
  925. key_ref_t key_ref;
  926. char *context;
  927. long ret;
  928. key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
  929. if (IS_ERR(key_ref)) {
  930. if (PTR_ERR(key_ref) != -EACCES)
  931. return PTR_ERR(key_ref);
  932. /* viewing a key under construction is also permitted if we
  933. * have the authorisation token handy */
  934. instkey = key_get_instantiation_authkey(keyid);
  935. if (IS_ERR(instkey))
  936. return PTR_ERR(key_ref);
  937. key_put(instkey);
  938. key_ref = lookup_user_key(NULL, keyid, 0, 1, 0);
  939. if (IS_ERR(key_ref))
  940. return PTR_ERR(key_ref);
  941. }
  942. key = key_ref_to_ptr(key_ref);
  943. ret = security_key_getsecurity(key, &context);
  944. if (ret == 0) {
  945. /* if no information was returned, give userspace an empty
  946. * string */
  947. ret = 1;
  948. if (buffer && buflen > 0 &&
  949. copy_to_user(buffer, "", 1) != 0)
  950. ret = -EFAULT;
  951. } else if (ret > 0) {
  952. /* return as much data as there's room for */
  953. if (buffer && buflen > 0) {
  954. if (buflen > ret)
  955. buflen = ret;
  956. if (copy_to_user(buffer, context, buflen) != 0)
  957. ret = -EFAULT;
  958. }
  959. kfree(context);
  960. }
  961. key_ref_put(key_ref);
  962. return ret;
  963. }
  964. /*****************************************************************************/
  965. /*
  966. * the key control system call
  967. */
  968. asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
  969. unsigned long arg4, unsigned long arg5)
  970. {
  971. switch (option) {
  972. case KEYCTL_GET_KEYRING_ID:
  973. return keyctl_get_keyring_ID((key_serial_t) arg2,
  974. (int) arg3);
  975. case KEYCTL_JOIN_SESSION_KEYRING:
  976. return keyctl_join_session_keyring((const char __user *) arg2);
  977. case KEYCTL_UPDATE:
  978. return keyctl_update_key((key_serial_t) arg2,
  979. (const void __user *) arg3,
  980. (size_t) arg4);
  981. case KEYCTL_REVOKE:
  982. return keyctl_revoke_key((key_serial_t) arg2);
  983. case KEYCTL_DESCRIBE:
  984. return keyctl_describe_key((key_serial_t) arg2,
  985. (char __user *) arg3,
  986. (unsigned) arg4);
  987. case KEYCTL_CLEAR:
  988. return keyctl_keyring_clear((key_serial_t) arg2);
  989. case KEYCTL_LINK:
  990. return keyctl_keyring_link((key_serial_t) arg2,
  991. (key_serial_t) arg3);
  992. case KEYCTL_UNLINK:
  993. return keyctl_keyring_unlink((key_serial_t) arg2,
  994. (key_serial_t) arg3);
  995. case KEYCTL_SEARCH:
  996. return keyctl_keyring_search((key_serial_t) arg2,
  997. (const char __user *) arg3,
  998. (const char __user *) arg4,
  999. (key_serial_t) arg5);
  1000. case KEYCTL_READ:
  1001. return keyctl_read_key((key_serial_t) arg2,
  1002. (char __user *) arg3,
  1003. (size_t) arg4);
  1004. case KEYCTL_CHOWN:
  1005. return keyctl_chown_key((key_serial_t) arg2,
  1006. (uid_t) arg3,
  1007. (gid_t) arg4);
  1008. case KEYCTL_SETPERM:
  1009. return keyctl_setperm_key((key_serial_t) arg2,
  1010. (key_perm_t) arg3);
  1011. case KEYCTL_INSTANTIATE:
  1012. return keyctl_instantiate_key((key_serial_t) arg2,
  1013. (const void __user *) arg3,
  1014. (size_t) arg4,
  1015. (key_serial_t) arg5);
  1016. case KEYCTL_NEGATE:
  1017. return keyctl_negate_key((key_serial_t) arg2,
  1018. (unsigned) arg3,
  1019. (key_serial_t) arg4);
  1020. case KEYCTL_SET_REQKEY_KEYRING:
  1021. return keyctl_set_reqkey_keyring(arg2);
  1022. case KEYCTL_SET_TIMEOUT:
  1023. return keyctl_set_timeout((key_serial_t) arg2,
  1024. (unsigned) arg3);
  1025. case KEYCTL_ASSUME_AUTHORITY:
  1026. return keyctl_assume_authority((key_serial_t) arg2);
  1027. case KEYCTL_GET_SECURITY:
  1028. return keyctl_get_security((key_serial_t) arg2,
  1029. (char *) arg3,
  1030. (size_t) arg4);
  1031. default:
  1032. return -EOPNOTSUPP;
  1033. }
  1034. } /* end sys_keyctl() */