keyctl.c 23 KB

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