keyctl.c 28 KB

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