keyctl.c 25 KB

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