keyctl.c 33 KB

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