keyctl.c 27 KB

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