keyctl.c 26 KB

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