keyctl.c 23 KB

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