keyctl.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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 = key_permission(key_ref, KEY_LINK);
  530. if (ret < 0)
  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. ret = key_permission(key_ref, KEY_READ);
  574. if (ret == 0)
  575. goto can_read_key;
  576. if (ret != -EACCES)
  577. goto error;
  578. /* we can't; see if it's searchable from this process's keyrings
  579. * - we automatically take account of the fact that it may be
  580. * dangling off an instantiation key
  581. */
  582. if (!is_key_possessed(key_ref)) {
  583. ret = -EACCES;
  584. goto error2;
  585. }
  586. /* the key is probably readable - now try to read it */
  587. can_read_key:
  588. ret = key_validate(key);
  589. if (ret == 0) {
  590. ret = -EOPNOTSUPP;
  591. if (key->type->read) {
  592. /* read the data with the semaphore held (since we
  593. * might sleep) */
  594. down_read(&key->sem);
  595. ret = key->type->read(key, buffer, buflen);
  596. up_read(&key->sem);
  597. }
  598. }
  599. error2:
  600. key_put(key);
  601. error:
  602. return ret;
  603. } /* end keyctl_read_key() */
  604. /*****************************************************************************/
  605. /*
  606. * change the ownership of a key
  607. * - the keyring owned by the changer
  608. * - if the uid or gid is -1, then that parameter is not changed
  609. * - implements keyctl(KEYCTL_CHOWN)
  610. */
  611. long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
  612. {
  613. struct key *key;
  614. key_ref_t key_ref;
  615. long ret;
  616. ret = 0;
  617. if (uid == (uid_t) -1 && gid == (gid_t) -1)
  618. goto error;
  619. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  620. if (IS_ERR(key_ref)) {
  621. ret = PTR_ERR(key_ref);
  622. goto error;
  623. }
  624. key = key_ref_to_ptr(key_ref);
  625. /* make the changes with the locks held to prevent chown/chown races */
  626. ret = -EACCES;
  627. down_write(&key->sem);
  628. if (!capable(CAP_SYS_ADMIN)) {
  629. /* only the sysadmin can chown a key to some other UID */
  630. if (uid != (uid_t) -1 && key->uid != uid)
  631. goto no_access;
  632. /* only the sysadmin can set the key's GID to a group other
  633. * than one of those that the current process subscribes to */
  634. if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
  635. goto no_access;
  636. }
  637. /* change the UID (have to update the quotas) */
  638. if (uid != (uid_t) -1 && uid != key->uid) {
  639. /* don't support UID changing yet */
  640. ret = -EOPNOTSUPP;
  641. goto no_access;
  642. }
  643. /* change the GID */
  644. if (gid != (gid_t) -1)
  645. key->gid = gid;
  646. ret = 0;
  647. no_access:
  648. up_write(&key->sem);
  649. key_put(key);
  650. error:
  651. return ret;
  652. } /* end keyctl_chown_key() */
  653. /*****************************************************************************/
  654. /*
  655. * change the permission mask on a key
  656. * - the keyring owned by the changer
  657. * - implements keyctl(KEYCTL_SETPERM)
  658. */
  659. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  660. {
  661. struct key *key;
  662. key_ref_t key_ref;
  663. long ret;
  664. ret = -EINVAL;
  665. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  666. goto error;
  667. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  668. if (IS_ERR(key_ref)) {
  669. ret = PTR_ERR(key_ref);
  670. goto error;
  671. }
  672. key = key_ref_to_ptr(key_ref);
  673. /* make the changes with the locks held to prevent chown/chmod races */
  674. ret = -EACCES;
  675. down_write(&key->sem);
  676. /* if we're not the sysadmin, we can only change a key that we own */
  677. if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
  678. key->perm = perm;
  679. ret = 0;
  680. }
  681. up_write(&key->sem);
  682. key_put(key);
  683. error:
  684. return ret;
  685. } /* end keyctl_setperm_key() */
  686. /*****************************************************************************/
  687. /*
  688. * instantiate the key with the specified payload, and, if one is given, link
  689. * the key into the keyring
  690. */
  691. long keyctl_instantiate_key(key_serial_t id,
  692. const void __user *_payload,
  693. size_t plen,
  694. key_serial_t ringid)
  695. {
  696. struct request_key_auth *rka;
  697. struct key *instkey;
  698. key_ref_t keyring_ref;
  699. void *payload;
  700. long ret;
  701. ret = -EINVAL;
  702. if (plen > 32767)
  703. goto error;
  704. /* the appropriate instantiation authorisation key must have been
  705. * assumed before calling this */
  706. ret = -EPERM;
  707. instkey = current->request_key_auth;
  708. if (!instkey)
  709. goto error;
  710. rka = instkey->payload.data;
  711. if (rka->target_key->serial != id)
  712. goto error;
  713. /* pull the payload in if one was supplied */
  714. payload = NULL;
  715. if (_payload) {
  716. ret = -ENOMEM;
  717. payload = kmalloc(plen, GFP_KERNEL);
  718. if (!payload)
  719. goto error;
  720. ret = -EFAULT;
  721. if (copy_from_user(payload, _payload, plen) != 0)
  722. goto error2;
  723. }
  724. /* find the destination keyring amongst those belonging to the
  725. * requesting task */
  726. keyring_ref = NULL;
  727. if (ringid) {
  728. keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
  729. KEY_WRITE);
  730. if (IS_ERR(keyring_ref)) {
  731. ret = PTR_ERR(keyring_ref);
  732. goto error2;
  733. }
  734. }
  735. /* instantiate the key and link it into a keyring */
  736. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  737. key_ref_to_ptr(keyring_ref), instkey);
  738. key_ref_put(keyring_ref);
  739. /* discard the assumed authority if it's just been disabled by
  740. * instantiation of the key */
  741. if (ret == 0) {
  742. key_put(current->request_key_auth);
  743. current->request_key_auth = NULL;
  744. }
  745. error2:
  746. kfree(payload);
  747. error:
  748. return ret;
  749. } /* end keyctl_instantiate_key() */
  750. /*****************************************************************************/
  751. /*
  752. * negatively instantiate the key with the given timeout (in seconds), and, if
  753. * one is given, link the key into the keyring
  754. */
  755. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  756. {
  757. struct request_key_auth *rka;
  758. struct key *instkey;
  759. key_ref_t keyring_ref;
  760. long ret;
  761. /* the appropriate instantiation authorisation key must have been
  762. * assumed before calling this */
  763. ret = -EPERM;
  764. instkey = current->request_key_auth;
  765. if (!instkey)
  766. goto error;
  767. rka = instkey->payload.data;
  768. if (rka->target_key->serial != id)
  769. goto error;
  770. /* find the destination keyring if present (which must also be
  771. * writable) */
  772. keyring_ref = NULL;
  773. if (ringid) {
  774. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  775. if (IS_ERR(keyring_ref)) {
  776. ret = PTR_ERR(keyring_ref);
  777. goto error;
  778. }
  779. }
  780. /* instantiate the key and link it into a keyring */
  781. ret = key_negate_and_link(rka->target_key, timeout,
  782. key_ref_to_ptr(keyring_ref), instkey);
  783. key_ref_put(keyring_ref);
  784. /* discard the assumed authority if it's just been disabled by
  785. * instantiation of the key */
  786. if (ret == 0) {
  787. key_put(current->request_key_auth);
  788. current->request_key_auth = NULL;
  789. }
  790. error:
  791. return ret;
  792. } /* end keyctl_negate_key() */
  793. /*****************************************************************************/
  794. /*
  795. * set the default keyring in which request_key() will cache keys
  796. * - return the old setting
  797. */
  798. long keyctl_set_reqkey_keyring(int reqkey_defl)
  799. {
  800. int ret;
  801. switch (reqkey_defl) {
  802. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  803. ret = install_thread_keyring(current);
  804. if (ret < 0)
  805. return ret;
  806. goto set;
  807. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  808. ret = install_process_keyring(current);
  809. if (ret < 0)
  810. return ret;
  811. case KEY_REQKEY_DEFL_DEFAULT:
  812. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  813. case KEY_REQKEY_DEFL_USER_KEYRING:
  814. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  815. set:
  816. current->jit_keyring = reqkey_defl;
  817. case KEY_REQKEY_DEFL_NO_CHANGE:
  818. return current->jit_keyring;
  819. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  820. default:
  821. return -EINVAL;
  822. }
  823. } /* end keyctl_set_reqkey_keyring() */
  824. /*****************************************************************************/
  825. /*
  826. * set or clear the timeout for a key
  827. */
  828. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  829. {
  830. struct timespec now;
  831. struct key *key;
  832. key_ref_t key_ref;
  833. time_t expiry;
  834. long ret;
  835. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  836. if (IS_ERR(key_ref)) {
  837. ret = PTR_ERR(key_ref);
  838. goto error;
  839. }
  840. key = key_ref_to_ptr(key_ref);
  841. /* make the changes with the locks held to prevent races */
  842. down_write(&key->sem);
  843. expiry = 0;
  844. if (timeout > 0) {
  845. now = current_kernel_time();
  846. expiry = now.tv_sec + timeout;
  847. }
  848. key->expiry = expiry;
  849. up_write(&key->sem);
  850. key_put(key);
  851. ret = 0;
  852. error:
  853. return ret;
  854. } /* end keyctl_set_timeout() */
  855. /*****************************************************************************/
  856. /*
  857. * assume the authority to instantiate the specified key
  858. */
  859. long keyctl_assume_authority(key_serial_t id)
  860. {
  861. struct key *authkey;
  862. long ret;
  863. /* special key IDs aren't permitted */
  864. ret = -EINVAL;
  865. if (id < 0)
  866. goto error;
  867. /* we divest ourselves of authority if given an ID of 0 */
  868. if (id == 0) {
  869. key_put(current->request_key_auth);
  870. current->request_key_auth = NULL;
  871. ret = 0;
  872. goto error;
  873. }
  874. /* attempt to assume the authority temporarily granted to us whilst we
  875. * instantiate the specified key
  876. * - the authorisation key must be in the current task's keyrings
  877. * somewhere
  878. */
  879. authkey = key_get_instantiation_authkey(id);
  880. if (IS_ERR(authkey)) {
  881. ret = PTR_ERR(authkey);
  882. goto error;
  883. }
  884. key_put(current->request_key_auth);
  885. current->request_key_auth = authkey;
  886. ret = authkey->serial;
  887. error:
  888. return ret;
  889. } /* end keyctl_assume_authority() */
  890. /*****************************************************************************/
  891. /*
  892. * the key control system call
  893. */
  894. asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
  895. unsigned long arg4, unsigned long arg5)
  896. {
  897. switch (option) {
  898. case KEYCTL_GET_KEYRING_ID:
  899. return keyctl_get_keyring_ID((key_serial_t) arg2,
  900. (int) arg3);
  901. case KEYCTL_JOIN_SESSION_KEYRING:
  902. return keyctl_join_session_keyring((const char __user *) arg2);
  903. case KEYCTL_UPDATE:
  904. return keyctl_update_key((key_serial_t) arg2,
  905. (const void __user *) arg3,
  906. (size_t) arg4);
  907. case KEYCTL_REVOKE:
  908. return keyctl_revoke_key((key_serial_t) arg2);
  909. case KEYCTL_DESCRIBE:
  910. return keyctl_describe_key((key_serial_t) arg2,
  911. (char __user *) arg3,
  912. (unsigned) arg4);
  913. case KEYCTL_CLEAR:
  914. return keyctl_keyring_clear((key_serial_t) arg2);
  915. case KEYCTL_LINK:
  916. return keyctl_keyring_link((key_serial_t) arg2,
  917. (key_serial_t) arg3);
  918. case KEYCTL_UNLINK:
  919. return keyctl_keyring_unlink((key_serial_t) arg2,
  920. (key_serial_t) arg3);
  921. case KEYCTL_SEARCH:
  922. return keyctl_keyring_search((key_serial_t) arg2,
  923. (const char __user *) arg3,
  924. (const char __user *) arg4,
  925. (key_serial_t) arg5);
  926. case KEYCTL_READ:
  927. return keyctl_read_key((key_serial_t) arg2,
  928. (char __user *) arg3,
  929. (size_t) arg4);
  930. case KEYCTL_CHOWN:
  931. return keyctl_chown_key((key_serial_t) arg2,
  932. (uid_t) arg3,
  933. (gid_t) arg4);
  934. case KEYCTL_SETPERM:
  935. return keyctl_setperm_key((key_serial_t) arg2,
  936. (key_perm_t) arg3);
  937. case KEYCTL_INSTANTIATE:
  938. return keyctl_instantiate_key((key_serial_t) arg2,
  939. (const void __user *) arg3,
  940. (size_t) arg4,
  941. (key_serial_t) arg5);
  942. case KEYCTL_NEGATE:
  943. return keyctl_negate_key((key_serial_t) arg2,
  944. (unsigned) arg3,
  945. (key_serial_t) arg4);
  946. case KEYCTL_SET_REQKEY_KEYRING:
  947. return keyctl_set_reqkey_keyring(arg2);
  948. case KEYCTL_SET_TIMEOUT:
  949. return keyctl_set_timeout((key_serial_t) arg2,
  950. (unsigned) arg3);
  951. case KEYCTL_ASSUME_AUTHORITY:
  952. return keyctl_assume_authority((key_serial_t) arg2);
  953. default:
  954. return -EOPNOTSUPP;
  955. }
  956. } /* end sys_keyctl() */