keyctl.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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/err.h>
  19. #include <asm/uaccess.h>
  20. #include "internal.h"
  21. /*****************************************************************************/
  22. /*
  23. * extract the description of a new key from userspace and either add it as a
  24. * new key to the specified keyring or update a matching key in that keyring
  25. * - the keyring must be writable
  26. * - returns the new key's serial number
  27. * - implements add_key()
  28. */
  29. asmlinkage long sys_add_key(const char __user *_type,
  30. const char __user *_description,
  31. const void __user *_payload,
  32. size_t plen,
  33. key_serial_t ringid)
  34. {
  35. key_ref_t keyring_ref, key_ref;
  36. char type[32], *description;
  37. void *payload;
  38. long dlen, ret;
  39. ret = -EINVAL;
  40. if (plen > 32767)
  41. goto error;
  42. /* draw all the data into kernel space */
  43. ret = strncpy_from_user(type, _type, sizeof(type) - 1);
  44. if (ret < 0)
  45. goto error;
  46. type[31] = '\0';
  47. ret = -EPERM;
  48. if (type[0] == '.')
  49. goto error;
  50. ret = -EFAULT;
  51. dlen = strnlen_user(_description, PAGE_SIZE - 1);
  52. if (dlen <= 0)
  53. goto error;
  54. ret = -EINVAL;
  55. if (dlen > PAGE_SIZE - 1)
  56. goto error;
  57. ret = -ENOMEM;
  58. description = kmalloc(dlen + 1, GFP_KERNEL);
  59. if (!description)
  60. goto error;
  61. ret = -EFAULT;
  62. if (copy_from_user(description, _description, dlen + 1) != 0)
  63. goto error2;
  64. /* pull the payload in if one was supplied */
  65. payload = NULL;
  66. if (_payload) {
  67. ret = -ENOMEM;
  68. payload = kmalloc(plen, GFP_KERNEL);
  69. if (!payload)
  70. goto error2;
  71. ret = -EFAULT;
  72. if (copy_from_user(payload, _payload, plen) != 0)
  73. goto error3;
  74. }
  75. /* find the target keyring (which must be writable) */
  76. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  77. if (IS_ERR(keyring_ref)) {
  78. ret = PTR_ERR(keyring_ref);
  79. goto error3;
  80. }
  81. /* create or update the requested key and add it to the target
  82. * keyring */
  83. key_ref = key_create_or_update(keyring_ref, type, description,
  84. payload, plen, 0);
  85. if (!IS_ERR(key_ref)) {
  86. ret = key_ref_to_ptr(key_ref)->serial;
  87. key_ref_put(key_ref);
  88. }
  89. else {
  90. ret = PTR_ERR(key_ref);
  91. }
  92. key_ref_put(keyring_ref);
  93. error3:
  94. kfree(payload);
  95. error2:
  96. kfree(description);
  97. error:
  98. return ret;
  99. } /* end sys_add_key() */
  100. /*****************************************************************************/
  101. /*
  102. * search the process keyrings for a matching key
  103. * - nested keyrings may also be searched if they have Search permission
  104. * - if a key is found, it will be attached to the destination keyring if
  105. * there's one specified
  106. * - /sbin/request-key will be invoked if _callout_info is non-NULL
  107. * - the _callout_info string will be passed to /sbin/request-key
  108. * - if the _callout_info string is empty, it will be rendered as "-"
  109. * - implements request_key()
  110. */
  111. asmlinkage long sys_request_key(const char __user *_type,
  112. const char __user *_description,
  113. const char __user *_callout_info,
  114. key_serial_t destringid)
  115. {
  116. struct key_type *ktype;
  117. struct key *key;
  118. key_ref_t dest_ref;
  119. char type[32], *description, *callout_info;
  120. long dlen, ret;
  121. /* pull the type into kernel space */
  122. ret = strncpy_from_user(type, _type, sizeof(type) - 1);
  123. if (ret < 0)
  124. goto error;
  125. type[31] = '\0';
  126. ret = -EPERM;
  127. if (type[0] == '.')
  128. goto error;
  129. /* pull the description into kernel space */
  130. ret = -EFAULT;
  131. dlen = strnlen_user(_description, PAGE_SIZE - 1);
  132. if (dlen <= 0)
  133. goto error;
  134. ret = -EINVAL;
  135. if (dlen > PAGE_SIZE - 1)
  136. goto error;
  137. ret = -ENOMEM;
  138. description = kmalloc(dlen + 1, GFP_KERNEL);
  139. if (!description)
  140. goto error;
  141. ret = -EFAULT;
  142. if (copy_from_user(description, _description, dlen + 1) != 0)
  143. goto error2;
  144. /* pull the callout info into kernel space */
  145. callout_info = NULL;
  146. if (_callout_info) {
  147. ret = -EFAULT;
  148. dlen = strnlen_user(_callout_info, PAGE_SIZE - 1);
  149. if (dlen <= 0)
  150. goto error2;
  151. ret = -EINVAL;
  152. if (dlen > PAGE_SIZE - 1)
  153. goto error2;
  154. ret = -ENOMEM;
  155. callout_info = kmalloc(dlen + 1, GFP_KERNEL);
  156. if (!callout_info)
  157. goto error2;
  158. ret = -EFAULT;
  159. if (copy_from_user(callout_info, _callout_info, dlen + 1) != 0)
  160. goto error3;
  161. }
  162. /* get the destination keyring if specified */
  163. dest_ref = NULL;
  164. if (destringid) {
  165. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  166. if (IS_ERR(dest_ref)) {
  167. ret = PTR_ERR(dest_ref);
  168. goto error3;
  169. }
  170. }
  171. /* find the key type */
  172. ktype = key_type_lookup(type);
  173. if (IS_ERR(ktype)) {
  174. ret = PTR_ERR(ktype);
  175. goto error4;
  176. }
  177. /* do the search */
  178. key = request_key_and_link(ktype, description, callout_info,
  179. key_ref_to_ptr(dest_ref));
  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 nlen, ret;
  226. /* fetch the name from userspace */
  227. name = NULL;
  228. if (_name) {
  229. ret = -EFAULT;
  230. nlen = strnlen_user(_name, PAGE_SIZE - 1);
  231. if (nlen <= 0)
  232. goto error;
  233. ret = -EINVAL;
  234. if (nlen > PAGE_SIZE - 1)
  235. goto error;
  236. ret = -ENOMEM;
  237. name = kmalloc(nlen + 1, GFP_KERNEL);
  238. if (!name)
  239. goto error;
  240. ret = -EFAULT;
  241. if (copy_from_user(name, _name, nlen + 1) != 0)
  242. goto error2;
  243. }
  244. /* join the session */
  245. ret = join_session_keyring(name);
  246. error2:
  247. kfree(name);
  248. error:
  249. return ret;
  250. } /* end keyctl_join_session_keyring() */
  251. /*****************************************************************************/
  252. /*
  253. * update a key's data payload
  254. * - the key must be writable
  255. * - implements keyctl(KEYCTL_UPDATE)
  256. */
  257. long keyctl_update_key(key_serial_t id,
  258. const void __user *_payload,
  259. size_t plen)
  260. {
  261. key_ref_t key_ref;
  262. void *payload;
  263. long ret;
  264. ret = -EINVAL;
  265. if (plen > PAGE_SIZE)
  266. goto error;
  267. /* pull the payload in if one was supplied */
  268. payload = NULL;
  269. if (_payload) {
  270. ret = -ENOMEM;
  271. payload = kmalloc(plen, GFP_KERNEL);
  272. if (!payload)
  273. goto error;
  274. ret = -EFAULT;
  275. if (copy_from_user(payload, _payload, plen) != 0)
  276. goto error2;
  277. }
  278. /* find the target key (which must be writable) */
  279. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  280. if (IS_ERR(key_ref)) {
  281. ret = PTR_ERR(key_ref);
  282. goto error2;
  283. }
  284. /* update the key */
  285. ret = key_update(key_ref, payload, plen);
  286. key_ref_put(key_ref);
  287. error2:
  288. kfree(payload);
  289. error:
  290. return ret;
  291. } /* end keyctl_update_key() */
  292. /*****************************************************************************/
  293. /*
  294. * revoke a key
  295. * - the key must be writable
  296. * - implements keyctl(KEYCTL_REVOKE)
  297. */
  298. long keyctl_revoke_key(key_serial_t id)
  299. {
  300. key_ref_t key_ref;
  301. long ret;
  302. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  303. if (IS_ERR(key_ref)) {
  304. ret = PTR_ERR(key_ref);
  305. goto error;
  306. }
  307. key_revoke(key_ref_to_ptr(key_ref));
  308. ret = 0;
  309. key_ref_put(key_ref);
  310. error:
  311. return ret;
  312. } /* end keyctl_revoke_key() */
  313. /*****************************************************************************/
  314. /*
  315. * clear the specified process keyring
  316. * - the keyring must be writable
  317. * - implements keyctl(KEYCTL_CLEAR)
  318. */
  319. long keyctl_keyring_clear(key_serial_t ringid)
  320. {
  321. key_ref_t keyring_ref;
  322. long ret;
  323. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  324. if (IS_ERR(keyring_ref)) {
  325. ret = PTR_ERR(keyring_ref);
  326. goto error;
  327. }
  328. ret = keyring_clear(key_ref_to_ptr(keyring_ref));
  329. key_ref_put(keyring_ref);
  330. error:
  331. return ret;
  332. } /* end keyctl_keyring_clear() */
  333. /*****************************************************************************/
  334. /*
  335. * link a key into a keyring
  336. * - the keyring must be writable
  337. * - the key must be linkable
  338. * - implements keyctl(KEYCTL_LINK)
  339. */
  340. long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
  341. {
  342. key_ref_t keyring_ref, key_ref;
  343. long ret;
  344. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  345. if (IS_ERR(keyring_ref)) {
  346. ret = PTR_ERR(keyring_ref);
  347. goto error;
  348. }
  349. key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
  350. if (IS_ERR(key_ref)) {
  351. ret = PTR_ERR(key_ref);
  352. goto error2;
  353. }
  354. ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  355. key_ref_put(key_ref);
  356. error2:
  357. key_ref_put(keyring_ref);
  358. error:
  359. return ret;
  360. } /* end keyctl_keyring_link() */
  361. /*****************************************************************************/
  362. /*
  363. * unlink the first attachment of a key from a keyring
  364. * - the keyring must be writable
  365. * - we don't need any permissions on the key
  366. * - implements keyctl(KEYCTL_UNLINK)
  367. */
  368. long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
  369. {
  370. key_ref_t keyring_ref, key_ref;
  371. long ret;
  372. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
  373. if (IS_ERR(keyring_ref)) {
  374. ret = PTR_ERR(keyring_ref);
  375. goto error;
  376. }
  377. key_ref = lookup_user_key(NULL, id, 0, 0, 0);
  378. if (IS_ERR(key_ref)) {
  379. ret = PTR_ERR(key_ref);
  380. goto error2;
  381. }
  382. ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  383. key_ref_put(key_ref);
  384. error2:
  385. key_ref_put(keyring_ref);
  386. error:
  387. return ret;
  388. } /* end keyctl_keyring_unlink() */
  389. /*****************************************************************************/
  390. /*
  391. * describe a user key
  392. * - the key must have view permission
  393. * - if there's a buffer, we place up to buflen bytes of data into it
  394. * - unless there's an error, we return the amount of description available,
  395. * irrespective of how much we may have copied
  396. * - the description is formatted thus:
  397. * type;uid;gid;perm;description<NUL>
  398. * - implements keyctl(KEYCTL_DESCRIBE)
  399. */
  400. long keyctl_describe_key(key_serial_t keyid,
  401. char __user *buffer,
  402. size_t buflen)
  403. {
  404. struct key *key, *instkey;
  405. key_ref_t key_ref;
  406. char *tmpbuf;
  407. long ret;
  408. key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
  409. if (IS_ERR(key_ref)) {
  410. /* viewing a key under construction is permitted if we have the
  411. * authorisation token handy */
  412. if (PTR_ERR(key_ref) == -EACCES) {
  413. instkey = key_get_instantiation_authkey(keyid);
  414. if (!IS_ERR(instkey)) {
  415. key_put(instkey);
  416. key_ref = lookup_user_key(NULL, keyid,
  417. 0, 1, 0);
  418. if (!IS_ERR(key_ref))
  419. goto okay;
  420. }
  421. }
  422. ret = PTR_ERR(key_ref);
  423. goto error;
  424. }
  425. okay:
  426. /* calculate how much description we're going to return */
  427. ret = -ENOMEM;
  428. tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  429. if (!tmpbuf)
  430. goto error2;
  431. key = key_ref_to_ptr(key_ref);
  432. ret = snprintf(tmpbuf, PAGE_SIZE - 1,
  433. "%s;%d;%d;%08x;%s",
  434. key_ref_to_ptr(key_ref)->type->name,
  435. key_ref_to_ptr(key_ref)->uid,
  436. key_ref_to_ptr(key_ref)->gid,
  437. key_ref_to_ptr(key_ref)->perm,
  438. key_ref_to_ptr(key_ref)->description ?
  439. key_ref_to_ptr(key_ref)->description : ""
  440. );
  441. /* include a NUL char at the end of the data */
  442. if (ret > PAGE_SIZE - 1)
  443. ret = PAGE_SIZE - 1;
  444. tmpbuf[ret] = 0;
  445. ret++;
  446. /* consider returning the data */
  447. if (buffer && buflen > 0) {
  448. if (buflen > ret)
  449. buflen = ret;
  450. if (copy_to_user(buffer, tmpbuf, buflen) != 0)
  451. ret = -EFAULT;
  452. }
  453. kfree(tmpbuf);
  454. error2:
  455. key_ref_put(key_ref);
  456. error:
  457. return ret;
  458. } /* end keyctl_describe_key() */
  459. /*****************************************************************************/
  460. /*
  461. * search the specified keyring for a matching key
  462. * - the start keyring must be searchable
  463. * - nested keyrings may also be searched if they are searchable
  464. * - only keys with search permission may be found
  465. * - if a key is found, it will be attached to the destination keyring if
  466. * there's one specified
  467. * - implements keyctl(KEYCTL_SEARCH)
  468. */
  469. long keyctl_keyring_search(key_serial_t ringid,
  470. const char __user *_type,
  471. const char __user *_description,
  472. key_serial_t destringid)
  473. {
  474. struct key_type *ktype;
  475. key_ref_t keyring_ref, key_ref, dest_ref;
  476. char type[32], *description;
  477. long dlen, ret;
  478. /* pull the type and description into kernel space */
  479. ret = strncpy_from_user(type, _type, sizeof(type) - 1);
  480. if (ret < 0)
  481. goto error;
  482. type[31] = '\0';
  483. ret = -EFAULT;
  484. dlen = strnlen_user(_description, PAGE_SIZE - 1);
  485. if (dlen <= 0)
  486. goto error;
  487. ret = -EINVAL;
  488. if (dlen > PAGE_SIZE - 1)
  489. goto error;
  490. ret = -ENOMEM;
  491. description = kmalloc(dlen + 1, GFP_KERNEL);
  492. if (!description)
  493. goto error;
  494. ret = -EFAULT;
  495. if (copy_from_user(description, _description, dlen + 1) != 0)
  496. goto error2;
  497. /* get the keyring at which to begin the search */
  498. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
  499. if (IS_ERR(keyring_ref)) {
  500. ret = PTR_ERR(keyring_ref);
  501. goto error2;
  502. }
  503. /* get the destination keyring if specified */
  504. dest_ref = NULL;
  505. if (destringid) {
  506. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  507. if (IS_ERR(dest_ref)) {
  508. ret = PTR_ERR(dest_ref);
  509. goto error3;
  510. }
  511. }
  512. /* find the key type */
  513. ktype = key_type_lookup(type);
  514. if (IS_ERR(ktype)) {
  515. ret = PTR_ERR(ktype);
  516. goto error4;
  517. }
  518. /* do the search */
  519. key_ref = keyring_search(keyring_ref, ktype, description);
  520. if (IS_ERR(key_ref)) {
  521. ret = PTR_ERR(key_ref);
  522. /* treat lack or presence of a negative key the same */
  523. if (ret == -EAGAIN)
  524. ret = -ENOKEY;
  525. goto error5;
  526. }
  527. /* link the resulting key to the destination keyring if we can */
  528. if (dest_ref) {
  529. ret = -EACCES;
  530. if (!key_permission(key_ref, KEY_LINK))
  531. goto error6;
  532. ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
  533. if (ret < 0)
  534. goto error6;
  535. }
  536. ret = key_ref_to_ptr(key_ref)->serial;
  537. error6:
  538. key_ref_put(key_ref);
  539. error5:
  540. key_type_put(ktype);
  541. error4:
  542. key_ref_put(dest_ref);
  543. error3:
  544. key_ref_put(keyring_ref);
  545. error2:
  546. kfree(description);
  547. error:
  548. return ret;
  549. } /* end keyctl_keyring_search() */
  550. /*****************************************************************************/
  551. /*
  552. * read a user key's payload
  553. * - the keyring must be readable or the key must be searchable from the
  554. * process's keyrings
  555. * - if there's a buffer, we place up to buflen bytes of data into it
  556. * - unless there's an error, we return the amount of data in the key,
  557. * irrespective of how much we may have copied
  558. * - implements keyctl(KEYCTL_READ)
  559. */
  560. long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
  561. {
  562. struct key *key;
  563. key_ref_t key_ref;
  564. long ret;
  565. /* find the key first */
  566. key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
  567. if (IS_ERR(key_ref)) {
  568. ret = -ENOKEY;
  569. goto error;
  570. }
  571. key = key_ref_to_ptr(key_ref);
  572. /* see if we can read it directly */
  573. if (key_permission(key_ref, KEY_READ))
  574. goto can_read_key;
  575. /* we can't; see if it's searchable from this process's keyrings
  576. * - we automatically take account of the fact that it may be
  577. * dangling off an instantiation key
  578. */
  579. if (!is_key_possessed(key_ref)) {
  580. ret = -EACCES;
  581. goto error2;
  582. }
  583. /* the key is probably readable - now try to read it */
  584. can_read_key:
  585. ret = key_validate(key);
  586. if (ret == 0) {
  587. ret = -EOPNOTSUPP;
  588. if (key->type->read) {
  589. /* read the data with the semaphore held (since we
  590. * might sleep) */
  591. down_read(&key->sem);
  592. ret = key->type->read(key, buffer, buflen);
  593. up_read(&key->sem);
  594. }
  595. }
  596. error2:
  597. key_put(key);
  598. error:
  599. return ret;
  600. } /* end keyctl_read_key() */
  601. /*****************************************************************************/
  602. /*
  603. * change the ownership of a key
  604. * - the keyring owned by the changer
  605. * - if the uid or gid is -1, then that parameter is not changed
  606. * - implements keyctl(KEYCTL_CHOWN)
  607. */
  608. long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
  609. {
  610. struct key *key;
  611. key_ref_t key_ref;
  612. long ret;
  613. ret = 0;
  614. if (uid == (uid_t) -1 && gid == (gid_t) -1)
  615. goto error;
  616. key_ref = lookup_user_key(NULL, id, 1, 1, 0);
  617. if (IS_ERR(key_ref)) {
  618. ret = PTR_ERR(key_ref);
  619. goto error;
  620. }
  621. key = key_ref_to_ptr(key_ref);
  622. /* make the changes with the locks held to prevent chown/chown races */
  623. ret = -EACCES;
  624. down_write(&key->sem);
  625. if (!capable(CAP_SYS_ADMIN)) {
  626. /* only the sysadmin can chown a key to some other UID */
  627. if (uid != (uid_t) -1 && key->uid != uid)
  628. goto no_access;
  629. /* only the sysadmin can set the key's GID to a group other
  630. * than one of those that the current process subscribes to */
  631. if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
  632. goto no_access;
  633. }
  634. /* change the UID (have to update the quotas) */
  635. if (uid != (uid_t) -1 && uid != key->uid) {
  636. /* don't support UID changing yet */
  637. ret = -EOPNOTSUPP;
  638. goto no_access;
  639. }
  640. /* change the GID */
  641. if (gid != (gid_t) -1)
  642. key->gid = gid;
  643. ret = 0;
  644. no_access:
  645. up_write(&key->sem);
  646. key_put(key);
  647. error:
  648. return ret;
  649. } /* end keyctl_chown_key() */
  650. /*****************************************************************************/
  651. /*
  652. * change the permission mask on a key
  653. * - the keyring owned by the changer
  654. * - implements keyctl(KEYCTL_SETPERM)
  655. */
  656. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  657. {
  658. struct key *key;
  659. key_ref_t key_ref;
  660. long ret;
  661. ret = -EINVAL;
  662. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  663. goto error;
  664. key_ref = lookup_user_key(NULL, id, 1, 1, 0);
  665. if (IS_ERR(key_ref)) {
  666. ret = PTR_ERR(key_ref);
  667. goto error;
  668. }
  669. key = key_ref_to_ptr(key_ref);
  670. /* make the changes with the locks held to prevent chown/chmod races */
  671. ret = -EACCES;
  672. down_write(&key->sem);
  673. /* if we're not the sysadmin, we can only change a key that we own */
  674. if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
  675. key->perm = perm;
  676. ret = 0;
  677. }
  678. up_write(&key->sem);
  679. key_put(key);
  680. error:
  681. return ret;
  682. } /* end keyctl_setperm_key() */
  683. /*****************************************************************************/
  684. /*
  685. * instantiate the key with the specified payload, and, if one is given, link
  686. * the key into the keyring
  687. */
  688. long keyctl_instantiate_key(key_serial_t id,
  689. const void __user *_payload,
  690. size_t plen,
  691. key_serial_t ringid)
  692. {
  693. struct request_key_auth *rka;
  694. struct key *instkey;
  695. key_ref_t keyring_ref;
  696. void *payload;
  697. long ret;
  698. ret = -EINVAL;
  699. if (plen > 32767)
  700. goto error;
  701. /* pull the payload in if one was supplied */
  702. payload = NULL;
  703. if (_payload) {
  704. ret = -ENOMEM;
  705. payload = kmalloc(plen, GFP_KERNEL);
  706. if (!payload)
  707. goto error;
  708. ret = -EFAULT;
  709. if (copy_from_user(payload, _payload, plen) != 0)
  710. goto error2;
  711. }
  712. /* find the instantiation authorisation key */
  713. instkey = key_get_instantiation_authkey(id);
  714. if (IS_ERR(instkey)) {
  715. ret = PTR_ERR(instkey);
  716. goto error2;
  717. }
  718. rka = instkey->payload.data;
  719. /* find the destination keyring amongst those belonging to the
  720. * requesting task */
  721. keyring_ref = NULL;
  722. if (ringid) {
  723. keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
  724. KEY_WRITE);
  725. if (IS_ERR(keyring_ref)) {
  726. ret = PTR_ERR(keyring_ref);
  727. goto error3;
  728. }
  729. }
  730. /* instantiate the key and link it into a keyring */
  731. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  732. key_ref_to_ptr(keyring_ref), instkey);
  733. key_ref_put(keyring_ref);
  734. error3:
  735. key_put(instkey);
  736. error2:
  737. kfree(payload);
  738. error:
  739. return ret;
  740. } /* end keyctl_instantiate_key() */
  741. /*****************************************************************************/
  742. /*
  743. * negatively instantiate the key with the given timeout (in seconds), and, if
  744. * one is given, link the key into the keyring
  745. */
  746. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  747. {
  748. struct request_key_auth *rka;
  749. struct key *instkey;
  750. key_ref_t keyring_ref;
  751. long ret;
  752. /* find the instantiation authorisation key */
  753. instkey = key_get_instantiation_authkey(id);
  754. if (IS_ERR(instkey)) {
  755. ret = PTR_ERR(instkey);
  756. goto error;
  757. }
  758. rka = instkey->payload.data;
  759. /* find the destination keyring if present (which must also be
  760. * writable) */
  761. keyring_ref = NULL;
  762. if (ringid) {
  763. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  764. if (IS_ERR(keyring_ref)) {
  765. ret = PTR_ERR(keyring_ref);
  766. goto error2;
  767. }
  768. }
  769. /* instantiate the key and link it into a keyring */
  770. ret = key_negate_and_link(rka->target_key, timeout,
  771. key_ref_to_ptr(keyring_ref), instkey);
  772. key_ref_put(keyring_ref);
  773. error2:
  774. key_put(instkey);
  775. error:
  776. return ret;
  777. } /* end keyctl_negate_key() */
  778. /*****************************************************************************/
  779. /*
  780. * set the default keyring in which request_key() will cache keys
  781. * - return the old setting
  782. */
  783. long keyctl_set_reqkey_keyring(int reqkey_defl)
  784. {
  785. int ret;
  786. switch (reqkey_defl) {
  787. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  788. ret = install_thread_keyring(current);
  789. if (ret < 0)
  790. return ret;
  791. goto set;
  792. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  793. ret = install_process_keyring(current);
  794. if (ret < 0)
  795. return ret;
  796. case KEY_REQKEY_DEFL_DEFAULT:
  797. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  798. case KEY_REQKEY_DEFL_USER_KEYRING:
  799. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  800. set:
  801. current->jit_keyring = reqkey_defl;
  802. case KEY_REQKEY_DEFL_NO_CHANGE:
  803. return current->jit_keyring;
  804. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  805. default:
  806. return -EINVAL;
  807. }
  808. } /* end keyctl_set_reqkey_keyring() */
  809. /*****************************************************************************/
  810. /*
  811. * the key control system call
  812. */
  813. asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
  814. unsigned long arg4, unsigned long arg5)
  815. {
  816. switch (option) {
  817. case KEYCTL_GET_KEYRING_ID:
  818. return keyctl_get_keyring_ID((key_serial_t) arg2,
  819. (int) arg3);
  820. case KEYCTL_JOIN_SESSION_KEYRING:
  821. return keyctl_join_session_keyring((const char __user *) arg2);
  822. case KEYCTL_UPDATE:
  823. return keyctl_update_key((key_serial_t) arg2,
  824. (const void __user *) arg3,
  825. (size_t) arg4);
  826. case KEYCTL_REVOKE:
  827. return keyctl_revoke_key((key_serial_t) arg2);
  828. case KEYCTL_DESCRIBE:
  829. return keyctl_describe_key((key_serial_t) arg2,
  830. (char __user *) arg3,
  831. (unsigned) arg4);
  832. case KEYCTL_CLEAR:
  833. return keyctl_keyring_clear((key_serial_t) arg2);
  834. case KEYCTL_LINK:
  835. return keyctl_keyring_link((key_serial_t) arg2,
  836. (key_serial_t) arg3);
  837. case KEYCTL_UNLINK:
  838. return keyctl_keyring_unlink((key_serial_t) arg2,
  839. (key_serial_t) arg3);
  840. case KEYCTL_SEARCH:
  841. return keyctl_keyring_search((key_serial_t) arg2,
  842. (const char __user *) arg3,
  843. (const char __user *) arg4,
  844. (key_serial_t) arg5);
  845. case KEYCTL_READ:
  846. return keyctl_read_key((key_serial_t) arg2,
  847. (char __user *) arg3,
  848. (size_t) arg4);
  849. case KEYCTL_CHOWN:
  850. return keyctl_chown_key((key_serial_t) arg2,
  851. (uid_t) arg3,
  852. (gid_t) arg4);
  853. case KEYCTL_SETPERM:
  854. return keyctl_setperm_key((key_serial_t) arg2,
  855. (key_perm_t) arg3);
  856. case KEYCTL_INSTANTIATE:
  857. return keyctl_instantiate_key((key_serial_t) arg2,
  858. (const void __user *) arg3,
  859. (size_t) arg4,
  860. (key_serial_t) arg5);
  861. case KEYCTL_NEGATE:
  862. return keyctl_negate_key((key_serial_t) arg2,
  863. (unsigned) arg3,
  864. (key_serial_t) arg4);
  865. case KEYCTL_SET_REQKEY_KEYRING:
  866. return keyctl_set_reqkey_keyring(arg2);
  867. default:
  868. return -EOPNOTSUPP;
  869. }
  870. } /* end sys_keyctl() */