keyctl.c 26 KB

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