keyctl.c 30 KB

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