keyctl.c 30 KB

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