keyctl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /* Userspace key control 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/key.h>
  17. #include <linux/keyctl.h>
  18. #include <linux/fs.h>
  19. #include <linux/capability.h>
  20. #include <linux/string.h>
  21. #include <linux/err.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/security.h>
  24. #include <asm/uaccess.h>
  25. #include "internal.h"
  26. static int key_get_type_from_user(char *type,
  27. const char __user *_type,
  28. unsigned len)
  29. {
  30. int ret;
  31. ret = strncpy_from_user(type, _type, len);
  32. if (ret < 0)
  33. return ret;
  34. if (ret == 0 || ret >= len)
  35. return -EINVAL;
  36. if (type[0] == '.')
  37. return -EPERM;
  38. type[len - 1] = '\0';
  39. return 0;
  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. *
  45. * The keyring must be writable so that we can attach the key to it.
  46. *
  47. * If successful, the new key's serial number is returned, otherwise an error
  48. * code is returned.
  49. */
  50. SYSCALL_DEFINE5(add_key, const char __user *, _type,
  51. const char __user *, _description,
  52. const void __user *, _payload,
  53. size_t, plen,
  54. key_serial_t, ringid)
  55. {
  56. key_ref_t keyring_ref, key_ref;
  57. char type[32], *description;
  58. void *payload;
  59. long ret;
  60. bool vm;
  61. ret = -EINVAL;
  62. if (plen > 1024 * 1024 - 1)
  63. goto error;
  64. /* draw all the data into kernel space */
  65. ret = key_get_type_from_user(type, _type, sizeof(type));
  66. if (ret < 0)
  67. goto error;
  68. description = strndup_user(_description, PAGE_SIZE);
  69. if (IS_ERR(description)) {
  70. ret = PTR_ERR(description);
  71. goto error;
  72. }
  73. /* pull the payload in if one was supplied */
  74. payload = NULL;
  75. vm = false;
  76. if (_payload) {
  77. ret = -ENOMEM;
  78. payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
  79. if (!payload) {
  80. if (plen <= PAGE_SIZE)
  81. goto error2;
  82. vm = true;
  83. payload = vmalloc(plen);
  84. if (!payload)
  85. goto error2;
  86. }
  87. ret = -EFAULT;
  88. if (copy_from_user(payload, _payload, plen) != 0)
  89. goto error3;
  90. }
  91. /* find the target keyring (which must be writable) */
  92. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
  93. if (IS_ERR(keyring_ref)) {
  94. ret = PTR_ERR(keyring_ref);
  95. goto error3;
  96. }
  97. /* create or update the requested key and add it to the target
  98. * keyring */
  99. key_ref = key_create_or_update(keyring_ref, type, description,
  100. payload, plen, KEY_PERM_UNDEF,
  101. KEY_ALLOC_IN_QUOTA);
  102. if (!IS_ERR(key_ref)) {
  103. ret = key_ref_to_ptr(key_ref)->serial;
  104. key_ref_put(key_ref);
  105. }
  106. else {
  107. ret = PTR_ERR(key_ref);
  108. }
  109. key_ref_put(keyring_ref);
  110. error3:
  111. if (!vm)
  112. kfree(payload);
  113. else
  114. vfree(payload);
  115. error2:
  116. kfree(description);
  117. error:
  118. return ret;
  119. }
  120. /*
  121. * Search the process keyrings and keyring trees linked from those for a
  122. * matching key. Keyrings must have appropriate Search permission to be
  123. * searched.
  124. *
  125. * If a key is found, it will be attached to the destination keyring if there's
  126. * one specified and the serial number of the key will be returned.
  127. *
  128. * If no key is found, /sbin/request-key will be invoked if _callout_info is
  129. * non-NULL in an attempt to create a key. The _callout_info string will be
  130. * passed to /sbin/request-key to aid with completing the request. If the
  131. * _callout_info string is "" then it will be changed to "-".
  132. */
  133. SYSCALL_DEFINE4(request_key, const char __user *, _type,
  134. const char __user *, _description,
  135. const char __user *, _callout_info,
  136. key_serial_t, destringid)
  137. {
  138. struct key_type *ktype;
  139. struct key *key;
  140. key_ref_t dest_ref;
  141. size_t callout_len;
  142. char type[32], *description, *callout_info;
  143. long ret;
  144. /* pull the type into kernel space */
  145. ret = key_get_type_from_user(type, _type, sizeof(type));
  146. if (ret < 0)
  147. goto error;
  148. /* pull the description into kernel space */
  149. description = strndup_user(_description, PAGE_SIZE);
  150. if (IS_ERR(description)) {
  151. ret = PTR_ERR(description);
  152. goto error;
  153. }
  154. /* pull the callout info into kernel space */
  155. callout_info = NULL;
  156. callout_len = 0;
  157. if (_callout_info) {
  158. callout_info = strndup_user(_callout_info, PAGE_SIZE);
  159. if (IS_ERR(callout_info)) {
  160. ret = PTR_ERR(callout_info);
  161. goto error2;
  162. }
  163. callout_len = strlen(callout_info);
  164. }
  165. /* get the destination keyring if specified */
  166. dest_ref = NULL;
  167. if (destringid) {
  168. dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
  169. KEY_WRITE);
  170. if (IS_ERR(dest_ref)) {
  171. ret = PTR_ERR(dest_ref);
  172. goto error3;
  173. }
  174. }
  175. /* find the key type */
  176. ktype = key_type_lookup(type);
  177. if (IS_ERR(ktype)) {
  178. ret = PTR_ERR(ktype);
  179. goto error4;
  180. }
  181. /* do the search */
  182. key = request_key_and_link(ktype, description, callout_info,
  183. callout_len, NULL, key_ref_to_ptr(dest_ref),
  184. KEY_ALLOC_IN_QUOTA);
  185. if (IS_ERR(key)) {
  186. ret = PTR_ERR(key);
  187. goto error5;
  188. }
  189. /* wait for the key to finish being constructed */
  190. ret = wait_for_key_construction(key, 1);
  191. if (ret < 0)
  192. goto error6;
  193. ret = key->serial;
  194. error6:
  195. key_put(key);
  196. error5:
  197. key_type_put(ktype);
  198. error4:
  199. key_ref_put(dest_ref);
  200. error3:
  201. kfree(callout_info);
  202. error2:
  203. kfree(description);
  204. error:
  205. return ret;
  206. }
  207. /*
  208. * Get the ID of the specified process keyring.
  209. *
  210. * The requested keyring must have search permission to be found.
  211. *
  212. * If successful, the ID of the requested keyring will be returned.
  213. */
  214. long keyctl_get_keyring_ID(key_serial_t id, int create)
  215. {
  216. key_ref_t key_ref;
  217. unsigned long lflags;
  218. long ret;
  219. lflags = create ? KEY_LOOKUP_CREATE : 0;
  220. key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
  221. if (IS_ERR(key_ref)) {
  222. ret = PTR_ERR(key_ref);
  223. goto error;
  224. }
  225. ret = key_ref_to_ptr(key_ref)->serial;
  226. key_ref_put(key_ref);
  227. error:
  228. return ret;
  229. }
  230. /*
  231. * Join a (named) session keyring.
  232. *
  233. * Create and join an anonymous session keyring or join a named session
  234. * keyring, creating it if necessary. A named session keyring must have Search
  235. * permission for it to be joined. Session keyrings without this permit will
  236. * be skipped over.
  237. *
  238. * If successful, the ID of the joined session keyring will be returned.
  239. */
  240. long keyctl_join_session_keyring(const char __user *_name)
  241. {
  242. char *name;
  243. long ret;
  244. /* fetch the name from userspace */
  245. name = NULL;
  246. if (_name) {
  247. name = strndup_user(_name, PAGE_SIZE);
  248. if (IS_ERR(name)) {
  249. ret = PTR_ERR(name);
  250. goto error;
  251. }
  252. }
  253. /* join the session */
  254. ret = join_session_keyring(name);
  255. kfree(name);
  256. error:
  257. return ret;
  258. }
  259. /*
  260. * Update a key's data payload from the given data.
  261. *
  262. * The key must grant the caller Write permission and the key type must support
  263. * updating for this to work. A negative key can be positively instantiated
  264. * with this call.
  265. *
  266. * If successful, 0 will be returned. If the key type does not support
  267. * updating, then -EOPNOTSUPP will be returned.
  268. */
  269. long keyctl_update_key(key_serial_t id,
  270. const void __user *_payload,
  271. size_t plen)
  272. {
  273. key_ref_t key_ref;
  274. void *payload;
  275. long ret;
  276. ret = -EINVAL;
  277. if (plen > PAGE_SIZE)
  278. goto error;
  279. /* pull the payload in if one was supplied */
  280. payload = NULL;
  281. if (_payload) {
  282. ret = -ENOMEM;
  283. payload = kmalloc(plen, GFP_KERNEL);
  284. if (!payload)
  285. goto error;
  286. ret = -EFAULT;
  287. if (copy_from_user(payload, _payload, plen) != 0)
  288. goto error2;
  289. }
  290. /* find the target key (which must be writable) */
  291. key_ref = lookup_user_key(id, 0, KEY_WRITE);
  292. if (IS_ERR(key_ref)) {
  293. ret = PTR_ERR(key_ref);
  294. goto error2;
  295. }
  296. /* update the key */
  297. ret = key_update(key_ref, payload, plen);
  298. key_ref_put(key_ref);
  299. error2:
  300. kfree(payload);
  301. error:
  302. return ret;
  303. }
  304. /*
  305. * Revoke a key.
  306. *
  307. * The key must be grant the caller Write or Setattr permission for this to
  308. * work. The key type should give up its quota claim when revoked. The key
  309. * and any links to the key will be automatically garbage collected after a
  310. * certain amount of time (/proc/sys/kernel/keys/gc_delay).
  311. *
  312. * If successful, 0 is returned.
  313. */
  314. long keyctl_revoke_key(key_serial_t id)
  315. {
  316. key_ref_t key_ref;
  317. long ret;
  318. key_ref = lookup_user_key(id, 0, KEY_WRITE);
  319. if (IS_ERR(key_ref)) {
  320. ret = PTR_ERR(key_ref);
  321. if (ret != -EACCES)
  322. goto error;
  323. key_ref = lookup_user_key(id, 0, KEY_SETATTR);
  324. if (IS_ERR(key_ref)) {
  325. ret = PTR_ERR(key_ref);
  326. goto error;
  327. }
  328. }
  329. key_revoke(key_ref_to_ptr(key_ref));
  330. ret = 0;
  331. key_ref_put(key_ref);
  332. error:
  333. return ret;
  334. }
  335. /*
  336. * Invalidate a key.
  337. *
  338. * The key must be grant the caller Invalidate permission for this to work.
  339. * The key and any links to the key will be automatically garbage collected
  340. * immediately.
  341. *
  342. * If successful, 0 is returned.
  343. */
  344. long keyctl_invalidate_key(key_serial_t id)
  345. {
  346. key_ref_t key_ref;
  347. long ret;
  348. kenter("%d", id);
  349. key_ref = lookup_user_key(id, 0, KEY_SEARCH);
  350. if (IS_ERR(key_ref)) {
  351. ret = PTR_ERR(key_ref);
  352. goto error;
  353. }
  354. key_invalidate(key_ref_to_ptr(key_ref));
  355. ret = 0;
  356. key_ref_put(key_ref);
  357. error:
  358. kleave(" = %ld", ret);
  359. return ret;
  360. }
  361. /*
  362. * Clear the specified keyring, creating an empty process keyring if one of the
  363. * special keyring IDs is used.
  364. *
  365. * The keyring must grant the caller Write permission for this to work. If
  366. * successful, 0 will be returned.
  367. */
  368. long keyctl_keyring_clear(key_serial_t ringid)
  369. {
  370. key_ref_t keyring_ref;
  371. long ret;
  372. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
  373. if (IS_ERR(keyring_ref)) {
  374. ret = PTR_ERR(keyring_ref);
  375. /* Root is permitted to invalidate certain special keyrings */
  376. if (capable(CAP_SYS_ADMIN)) {
  377. keyring_ref = lookup_user_key(ringid, 0, 0);
  378. if (IS_ERR(keyring_ref))
  379. goto error;
  380. if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
  381. &key_ref_to_ptr(keyring_ref)->flags))
  382. goto clear;
  383. goto error_put;
  384. }
  385. goto error;
  386. }
  387. clear:
  388. ret = keyring_clear(key_ref_to_ptr(keyring_ref));
  389. error_put:
  390. key_ref_put(keyring_ref);
  391. error:
  392. return ret;
  393. }
  394. /*
  395. * Create a link from a keyring to a key if there's no matching key in the
  396. * keyring, otherwise replace the link to the matching key with a link to the
  397. * new key.
  398. *
  399. * The key must grant the caller Link permission and the the keyring must grant
  400. * the caller Write permission. Furthermore, if an additional link is created,
  401. * the keyring's quota will be extended.
  402. *
  403. * If successful, 0 will be returned.
  404. */
  405. long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
  406. {
  407. key_ref_t keyring_ref, key_ref;
  408. long ret;
  409. keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
  410. if (IS_ERR(keyring_ref)) {
  411. ret = PTR_ERR(keyring_ref);
  412. goto error;
  413. }
  414. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
  415. if (IS_ERR(key_ref)) {
  416. ret = PTR_ERR(key_ref);
  417. goto error2;
  418. }
  419. ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  420. key_ref_put(key_ref);
  421. error2:
  422. key_ref_put(keyring_ref);
  423. error:
  424. return ret;
  425. }
  426. /*
  427. * Unlink a key from a keyring.
  428. *
  429. * The keyring must grant the caller Write permission for this to work; the key
  430. * itself need not grant the caller anything. If the last link to a key is
  431. * removed then that key will be scheduled for destruction.
  432. *
  433. * If successful, 0 will be returned.
  434. */
  435. long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
  436. {
  437. key_ref_t keyring_ref, key_ref;
  438. long ret;
  439. keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
  440. if (IS_ERR(keyring_ref)) {
  441. ret = PTR_ERR(keyring_ref);
  442. goto error;
  443. }
  444. key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
  445. if (IS_ERR(key_ref)) {
  446. ret = PTR_ERR(key_ref);
  447. goto error2;
  448. }
  449. ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  450. key_ref_put(key_ref);
  451. error2:
  452. key_ref_put(keyring_ref);
  453. error:
  454. return ret;
  455. }
  456. /*
  457. * Return a description of a key to userspace.
  458. *
  459. * The key must grant the caller View permission for this to work.
  460. *
  461. * If there's a buffer, we place up to buflen bytes of data into it formatted
  462. * in the following way:
  463. *
  464. * type;uid;gid;perm;description<NUL>
  465. *
  466. * If successful, we return the amount of description available, irrespective
  467. * of how much we may have copied into the buffer.
  468. */
  469. long keyctl_describe_key(key_serial_t keyid,
  470. char __user *buffer,
  471. size_t buflen)
  472. {
  473. struct key *key, *instkey;
  474. key_ref_t key_ref;
  475. char *tmpbuf;
  476. long ret;
  477. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
  478. if (IS_ERR(key_ref)) {
  479. /* viewing a key under construction is permitted if we have the
  480. * authorisation token handy */
  481. if (PTR_ERR(key_ref) == -EACCES) {
  482. instkey = key_get_instantiation_authkey(keyid);
  483. if (!IS_ERR(instkey)) {
  484. key_put(instkey);
  485. key_ref = lookup_user_key(keyid,
  486. KEY_LOOKUP_PARTIAL,
  487. 0);
  488. if (!IS_ERR(key_ref))
  489. goto okay;
  490. }
  491. }
  492. ret = PTR_ERR(key_ref);
  493. goto error;
  494. }
  495. okay:
  496. /* calculate how much description we're going to return */
  497. ret = -ENOMEM;
  498. tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  499. if (!tmpbuf)
  500. goto error2;
  501. key = key_ref_to_ptr(key_ref);
  502. ret = snprintf(tmpbuf, PAGE_SIZE - 1,
  503. "%s;%d;%d;%08x;%s",
  504. key->type->name,
  505. key->uid,
  506. key->gid,
  507. key->perm,
  508. key->description ?: "");
  509. /* include a NUL char at the end of the data */
  510. if (ret > PAGE_SIZE - 1)
  511. ret = PAGE_SIZE - 1;
  512. tmpbuf[ret] = 0;
  513. ret++;
  514. /* consider returning the data */
  515. if (buffer && buflen > 0) {
  516. if (buflen > ret)
  517. buflen = ret;
  518. if (copy_to_user(buffer, tmpbuf, buflen) != 0)
  519. ret = -EFAULT;
  520. }
  521. kfree(tmpbuf);
  522. error2:
  523. key_ref_put(key_ref);
  524. error:
  525. return ret;
  526. }
  527. /*
  528. * Search the specified keyring and any keyrings it links to for a matching
  529. * key. Only keyrings that grant the caller Search permission will be searched
  530. * (this includes the starting keyring). Only keys with Search permission can
  531. * be found.
  532. *
  533. * If successful, the found key will be linked to the destination keyring if
  534. * supplied and the key has Link permission, and the found key ID will be
  535. * returned.
  536. */
  537. long keyctl_keyring_search(key_serial_t ringid,
  538. const char __user *_type,
  539. const char __user *_description,
  540. key_serial_t destringid)
  541. {
  542. struct key_type *ktype;
  543. key_ref_t keyring_ref, key_ref, dest_ref;
  544. char type[32], *description;
  545. long ret;
  546. /* pull the type and description into kernel space */
  547. ret = key_get_type_from_user(type, _type, sizeof(type));
  548. if (ret < 0)
  549. goto error;
  550. description = strndup_user(_description, PAGE_SIZE);
  551. if (IS_ERR(description)) {
  552. ret = PTR_ERR(description);
  553. goto error;
  554. }
  555. /* get the keyring at which to begin the search */
  556. keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
  557. if (IS_ERR(keyring_ref)) {
  558. ret = PTR_ERR(keyring_ref);
  559. goto error2;
  560. }
  561. /* get the destination keyring if specified */
  562. dest_ref = NULL;
  563. if (destringid) {
  564. dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
  565. KEY_WRITE);
  566. if (IS_ERR(dest_ref)) {
  567. ret = PTR_ERR(dest_ref);
  568. goto error3;
  569. }
  570. }
  571. /* find the key type */
  572. ktype = key_type_lookup(type);
  573. if (IS_ERR(ktype)) {
  574. ret = PTR_ERR(ktype);
  575. goto error4;
  576. }
  577. /* do the search */
  578. key_ref = keyring_search(keyring_ref, ktype, description);
  579. if (IS_ERR(key_ref)) {
  580. ret = PTR_ERR(key_ref);
  581. /* treat lack or presence of a negative key the same */
  582. if (ret == -EAGAIN)
  583. ret = -ENOKEY;
  584. goto error5;
  585. }
  586. /* link the resulting key to the destination keyring if we can */
  587. if (dest_ref) {
  588. ret = key_permission(key_ref, KEY_LINK);
  589. if (ret < 0)
  590. goto error6;
  591. ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
  592. if (ret < 0)
  593. goto error6;
  594. }
  595. ret = key_ref_to_ptr(key_ref)->serial;
  596. error6:
  597. key_ref_put(key_ref);
  598. error5:
  599. key_type_put(ktype);
  600. error4:
  601. key_ref_put(dest_ref);
  602. error3:
  603. key_ref_put(keyring_ref);
  604. error2:
  605. kfree(description);
  606. error:
  607. return ret;
  608. }
  609. /*
  610. * Read a key's payload.
  611. *
  612. * The key must either grant the caller Read permission, or it must grant the
  613. * caller Search permission when searched for from the process keyrings.
  614. *
  615. * If successful, we place up to buflen bytes of data into the buffer, if one
  616. * is provided, and return the amount of data that is available in the key,
  617. * irrespective of how much we copied into the buffer.
  618. */
  619. long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
  620. {
  621. struct key *key;
  622. key_ref_t key_ref;
  623. long ret;
  624. /* find the key first */
  625. key_ref = lookup_user_key(keyid, 0, 0);
  626. if (IS_ERR(key_ref)) {
  627. ret = -ENOKEY;
  628. goto error;
  629. }
  630. key = key_ref_to_ptr(key_ref);
  631. /* see if we can read it directly */
  632. ret = key_permission(key_ref, KEY_READ);
  633. if (ret == 0)
  634. goto can_read_key;
  635. if (ret != -EACCES)
  636. goto error;
  637. /* we can't; see if it's searchable from this process's keyrings
  638. * - we automatically take account of the fact that it may be
  639. * dangling off an instantiation key
  640. */
  641. if (!is_key_possessed(key_ref)) {
  642. ret = -EACCES;
  643. goto error2;
  644. }
  645. /* the key is probably readable - now try to read it */
  646. can_read_key:
  647. ret = key_validate(key);
  648. if (ret == 0) {
  649. ret = -EOPNOTSUPP;
  650. if (key->type->read) {
  651. /* read the data with the semaphore held (since we
  652. * might sleep) */
  653. down_read(&key->sem);
  654. ret = key->type->read(key, buffer, buflen);
  655. up_read(&key->sem);
  656. }
  657. }
  658. error2:
  659. key_put(key);
  660. error:
  661. return ret;
  662. }
  663. /*
  664. * Change the ownership of a key
  665. *
  666. * The key must grant the caller Setattr permission for this to work, though
  667. * the key need not be fully instantiated yet. For the UID to be changed, or
  668. * for the GID to be changed to a group the caller is not a member of, the
  669. * caller must have sysadmin capability. If either uid or gid is -1 then that
  670. * attribute is not changed.
  671. *
  672. * If the UID is to be changed, the new user must have sufficient quota to
  673. * accept the key. The quota deduction will be removed from the old user to
  674. * the new user should the attribute be changed.
  675. *
  676. * If successful, 0 will be returned.
  677. */
  678. long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
  679. {
  680. struct key_user *newowner, *zapowner = NULL;
  681. struct key *key;
  682. key_ref_t key_ref;
  683. long ret;
  684. ret = 0;
  685. if (uid == (uid_t) -1 && gid == (gid_t) -1)
  686. goto error;
  687. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  688. KEY_SETATTR);
  689. if (IS_ERR(key_ref)) {
  690. ret = PTR_ERR(key_ref);
  691. goto error;
  692. }
  693. key = key_ref_to_ptr(key_ref);
  694. /* make the changes with the locks held to prevent chown/chown races */
  695. ret = -EACCES;
  696. down_write(&key->sem);
  697. if (!capable(CAP_SYS_ADMIN)) {
  698. /* only the sysadmin can chown a key to some other UID */
  699. if (uid != (uid_t) -1 && key->uid != uid)
  700. goto error_put;
  701. /* only the sysadmin can set the key's GID to a group other
  702. * than one of those that the current process subscribes to */
  703. if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
  704. goto error_put;
  705. }
  706. /* change the UID */
  707. if (uid != (uid_t) -1 && uid != key->uid) {
  708. ret = -ENOMEM;
  709. newowner = key_user_lookup(uid, current_user_ns());
  710. if (!newowner)
  711. goto error_put;
  712. /* transfer the quota burden to the new user */
  713. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  714. unsigned maxkeys = (uid == 0) ?
  715. key_quota_root_maxkeys : key_quota_maxkeys;
  716. unsigned maxbytes = (uid == 0) ?
  717. key_quota_root_maxbytes : key_quota_maxbytes;
  718. spin_lock(&newowner->lock);
  719. if (newowner->qnkeys + 1 >= maxkeys ||
  720. newowner->qnbytes + key->quotalen >= maxbytes ||
  721. newowner->qnbytes + key->quotalen <
  722. newowner->qnbytes)
  723. goto quota_overrun;
  724. newowner->qnkeys++;
  725. newowner->qnbytes += key->quotalen;
  726. spin_unlock(&newowner->lock);
  727. spin_lock(&key->user->lock);
  728. key->user->qnkeys--;
  729. key->user->qnbytes -= key->quotalen;
  730. spin_unlock(&key->user->lock);
  731. }
  732. atomic_dec(&key->user->nkeys);
  733. atomic_inc(&newowner->nkeys);
  734. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  735. atomic_dec(&key->user->nikeys);
  736. atomic_inc(&newowner->nikeys);
  737. }
  738. zapowner = key->user;
  739. key->user = newowner;
  740. key->uid = uid;
  741. }
  742. /* change the GID */
  743. if (gid != (gid_t) -1)
  744. key->gid = gid;
  745. ret = 0;
  746. error_put:
  747. up_write(&key->sem);
  748. key_put(key);
  749. if (zapowner)
  750. key_user_put(zapowner);
  751. error:
  752. return ret;
  753. quota_overrun:
  754. spin_unlock(&newowner->lock);
  755. zapowner = newowner;
  756. ret = -EDQUOT;
  757. goto error_put;
  758. }
  759. /*
  760. * Change the permission mask on a key.
  761. *
  762. * The key must grant the caller Setattr permission for this to work, though
  763. * the key need not be fully instantiated yet. If the caller does not have
  764. * sysadmin capability, it may only change the permission on keys that it owns.
  765. */
  766. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  767. {
  768. struct key *key;
  769. key_ref_t key_ref;
  770. long ret;
  771. ret = -EINVAL;
  772. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  773. goto error;
  774. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  775. KEY_SETATTR);
  776. if (IS_ERR(key_ref)) {
  777. ret = PTR_ERR(key_ref);
  778. goto error;
  779. }
  780. key = key_ref_to_ptr(key_ref);
  781. /* make the changes with the locks held to prevent chown/chmod races */
  782. ret = -EACCES;
  783. down_write(&key->sem);
  784. /* if we're not the sysadmin, we can only change a key that we own */
  785. if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
  786. key->perm = perm;
  787. ret = 0;
  788. }
  789. up_write(&key->sem);
  790. key_put(key);
  791. error:
  792. return ret;
  793. }
  794. /*
  795. * Get the destination keyring for instantiation and check that the caller has
  796. * Write permission on it.
  797. */
  798. static long get_instantiation_keyring(key_serial_t ringid,
  799. struct request_key_auth *rka,
  800. struct key **_dest_keyring)
  801. {
  802. key_ref_t dkref;
  803. *_dest_keyring = NULL;
  804. /* just return a NULL pointer if we weren't asked to make a link */
  805. if (ringid == 0)
  806. return 0;
  807. /* if a specific keyring is nominated by ID, then use that */
  808. if (ringid > 0) {
  809. dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
  810. if (IS_ERR(dkref))
  811. return PTR_ERR(dkref);
  812. *_dest_keyring = key_ref_to_ptr(dkref);
  813. return 0;
  814. }
  815. if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
  816. return -EINVAL;
  817. /* otherwise specify the destination keyring recorded in the
  818. * authorisation key (any KEY_SPEC_*_KEYRING) */
  819. if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
  820. *_dest_keyring = key_get(rka->dest_keyring);
  821. return 0;
  822. }
  823. return -ENOKEY;
  824. }
  825. /*
  826. * Change the request_key authorisation key on the current process.
  827. */
  828. static int keyctl_change_reqkey_auth(struct key *key)
  829. {
  830. struct cred *new;
  831. new = prepare_creds();
  832. if (!new)
  833. return -ENOMEM;
  834. key_put(new->request_key_auth);
  835. new->request_key_auth = key_get(key);
  836. return commit_creds(new);
  837. }
  838. /*
  839. * Copy the iovec data from userspace
  840. */
  841. static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
  842. unsigned ioc)
  843. {
  844. for (; ioc > 0; ioc--) {
  845. if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
  846. return -EFAULT;
  847. buffer += iov->iov_len;
  848. iov++;
  849. }
  850. return 0;
  851. }
  852. /*
  853. * Instantiate a key with the specified payload and link the key into the
  854. * destination keyring if one is given.
  855. *
  856. * The caller must have the appropriate instantiation permit set for this to
  857. * work (see keyctl_assume_authority). No other permissions are required.
  858. *
  859. * If successful, 0 will be returned.
  860. */
  861. long keyctl_instantiate_key_common(key_serial_t id,
  862. const struct iovec *payload_iov,
  863. unsigned ioc,
  864. size_t plen,
  865. key_serial_t ringid)
  866. {
  867. const struct cred *cred = current_cred();
  868. struct request_key_auth *rka;
  869. struct key *instkey, *dest_keyring;
  870. void *payload;
  871. long ret;
  872. bool vm = false;
  873. kenter("%d,,%zu,%d", id, plen, ringid);
  874. ret = -EINVAL;
  875. if (plen > 1024 * 1024 - 1)
  876. goto error;
  877. /* the appropriate instantiation authorisation key must have been
  878. * assumed before calling this */
  879. ret = -EPERM;
  880. instkey = cred->request_key_auth;
  881. if (!instkey)
  882. goto error;
  883. rka = instkey->payload.data;
  884. if (rka->target_key->serial != id)
  885. goto error;
  886. /* pull the payload in if one was supplied */
  887. payload = NULL;
  888. if (payload_iov) {
  889. ret = -ENOMEM;
  890. payload = kmalloc(plen, GFP_KERNEL);
  891. if (!payload) {
  892. if (plen <= PAGE_SIZE)
  893. goto error;
  894. vm = true;
  895. payload = vmalloc(plen);
  896. if (!payload)
  897. goto error;
  898. }
  899. ret = copy_from_user_iovec(payload, payload_iov, ioc);
  900. if (ret < 0)
  901. goto error2;
  902. }
  903. /* find the destination keyring amongst those belonging to the
  904. * requesting task */
  905. ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
  906. if (ret < 0)
  907. goto error2;
  908. /* instantiate the key and link it into a keyring */
  909. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  910. dest_keyring, instkey);
  911. key_put(dest_keyring);
  912. /* discard the assumed authority if it's just been disabled by
  913. * instantiation of the key */
  914. if (ret == 0)
  915. keyctl_change_reqkey_auth(NULL);
  916. error2:
  917. if (!vm)
  918. kfree(payload);
  919. else
  920. vfree(payload);
  921. error:
  922. return ret;
  923. }
  924. /*
  925. * Instantiate a key with the specified payload and link the key into the
  926. * destination keyring if one is given.
  927. *
  928. * The caller must have the appropriate instantiation permit set for this to
  929. * work (see keyctl_assume_authority). No other permissions are required.
  930. *
  931. * If successful, 0 will be returned.
  932. */
  933. long keyctl_instantiate_key(key_serial_t id,
  934. const void __user *_payload,
  935. size_t plen,
  936. key_serial_t ringid)
  937. {
  938. if (_payload && plen) {
  939. struct iovec iov[1] = {
  940. [0].iov_base = (void __user *)_payload,
  941. [0].iov_len = plen
  942. };
  943. return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
  944. }
  945. return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
  946. }
  947. /*
  948. * Instantiate a key with the specified multipart payload and link the key into
  949. * the destination keyring if one is given.
  950. *
  951. * The caller must have the appropriate instantiation permit set for this to
  952. * work (see keyctl_assume_authority). No other permissions are required.
  953. *
  954. * If successful, 0 will be returned.
  955. */
  956. long keyctl_instantiate_key_iov(key_serial_t id,
  957. const struct iovec __user *_payload_iov,
  958. unsigned ioc,
  959. key_serial_t ringid)
  960. {
  961. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  962. long ret;
  963. if (!_payload_iov || !ioc)
  964. goto no_payload;
  965. ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
  966. ARRAY_SIZE(iovstack), iovstack, &iov);
  967. if (ret < 0)
  968. return ret;
  969. if (ret == 0)
  970. goto no_payload_free;
  971. ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
  972. if (iov != iovstack)
  973. kfree(iov);
  974. return ret;
  975. no_payload_free:
  976. if (iov != iovstack)
  977. kfree(iov);
  978. no_payload:
  979. return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
  980. }
  981. /*
  982. * Negatively instantiate the key with the given timeout (in seconds) and link
  983. * the key into the destination keyring if one is given.
  984. *
  985. * The caller must have the appropriate instantiation permit set for this to
  986. * work (see keyctl_assume_authority). No other permissions are required.
  987. *
  988. * The key and any links to the key will be automatically garbage collected
  989. * after the timeout expires.
  990. *
  991. * Negative keys are used to rate limit repeated request_key() calls by causing
  992. * them to return -ENOKEY until the negative key expires.
  993. *
  994. * If successful, 0 will be returned.
  995. */
  996. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  997. {
  998. return keyctl_reject_key(id, timeout, ENOKEY, ringid);
  999. }
  1000. /*
  1001. * Negatively instantiate the key with the given timeout (in seconds) and error
  1002. * code and link the key into the destination keyring if one is given.
  1003. *
  1004. * The caller must have the appropriate instantiation permit set for this to
  1005. * work (see keyctl_assume_authority). No other permissions are required.
  1006. *
  1007. * The key and any links to the key will be automatically garbage collected
  1008. * after the timeout expires.
  1009. *
  1010. * Negative keys are used to rate limit repeated request_key() calls by causing
  1011. * them to return the specified error code until the negative key expires.
  1012. *
  1013. * If successful, 0 will be returned.
  1014. */
  1015. long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
  1016. key_serial_t ringid)
  1017. {
  1018. const struct cred *cred = current_cred();
  1019. struct request_key_auth *rka;
  1020. struct key *instkey, *dest_keyring;
  1021. long ret;
  1022. kenter("%d,%u,%u,%d", id, timeout, error, ringid);
  1023. /* must be a valid error code and mustn't be a kernel special */
  1024. if (error <= 0 ||
  1025. error >= MAX_ERRNO ||
  1026. error == ERESTARTSYS ||
  1027. error == ERESTARTNOINTR ||
  1028. error == ERESTARTNOHAND ||
  1029. error == ERESTART_RESTARTBLOCK)
  1030. return -EINVAL;
  1031. /* the appropriate instantiation authorisation key must have been
  1032. * assumed before calling this */
  1033. ret = -EPERM;
  1034. instkey = cred->request_key_auth;
  1035. if (!instkey)
  1036. goto error;
  1037. rka = instkey->payload.data;
  1038. if (rka->target_key->serial != id)
  1039. goto error;
  1040. /* find the destination keyring if present (which must also be
  1041. * writable) */
  1042. ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
  1043. if (ret < 0)
  1044. goto error;
  1045. /* instantiate the key and link it into a keyring */
  1046. ret = key_reject_and_link(rka->target_key, timeout, error,
  1047. dest_keyring, instkey);
  1048. key_put(dest_keyring);
  1049. /* discard the assumed authority if it's just been disabled by
  1050. * instantiation of the key */
  1051. if (ret == 0)
  1052. keyctl_change_reqkey_auth(NULL);
  1053. error:
  1054. return ret;
  1055. }
  1056. /*
  1057. * Read or set the default keyring in which request_key() will cache keys and
  1058. * return the old setting.
  1059. *
  1060. * If a process keyring is specified then this will be created if it doesn't
  1061. * yet exist. The old setting will be returned if successful.
  1062. */
  1063. long keyctl_set_reqkey_keyring(int reqkey_defl)
  1064. {
  1065. struct cred *new;
  1066. int ret, old_setting;
  1067. old_setting = current_cred_xxx(jit_keyring);
  1068. if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
  1069. return old_setting;
  1070. new = prepare_creds();
  1071. if (!new)
  1072. return -ENOMEM;
  1073. switch (reqkey_defl) {
  1074. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  1075. ret = install_thread_keyring_to_cred(new);
  1076. if (ret < 0)
  1077. goto error;
  1078. goto set;
  1079. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  1080. ret = install_process_keyring_to_cred(new);
  1081. if (ret < 0) {
  1082. if (ret != -EEXIST)
  1083. goto error;
  1084. ret = 0;
  1085. }
  1086. goto set;
  1087. case KEY_REQKEY_DEFL_DEFAULT:
  1088. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  1089. case KEY_REQKEY_DEFL_USER_KEYRING:
  1090. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  1091. case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
  1092. goto set;
  1093. case KEY_REQKEY_DEFL_NO_CHANGE:
  1094. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  1095. default:
  1096. ret = -EINVAL;
  1097. goto error;
  1098. }
  1099. set:
  1100. new->jit_keyring = reqkey_defl;
  1101. commit_creds(new);
  1102. return old_setting;
  1103. error:
  1104. abort_creds(new);
  1105. return ret;
  1106. }
  1107. /*
  1108. * Set or clear the timeout on a key.
  1109. *
  1110. * Either the key must grant the caller Setattr permission or else the caller
  1111. * must hold an instantiation authorisation token for the key.
  1112. *
  1113. * The timeout is either 0 to clear the timeout, or a number of seconds from
  1114. * the current time. The key and any links to the key will be automatically
  1115. * garbage collected after the timeout expires.
  1116. *
  1117. * If successful, 0 is returned.
  1118. */
  1119. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  1120. {
  1121. struct key *key, *instkey;
  1122. key_ref_t key_ref;
  1123. long ret;
  1124. key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
  1125. KEY_SETATTR);
  1126. if (IS_ERR(key_ref)) {
  1127. /* setting the timeout on a key under construction is permitted
  1128. * if we have the authorisation token handy */
  1129. if (PTR_ERR(key_ref) == -EACCES) {
  1130. instkey = key_get_instantiation_authkey(id);
  1131. if (!IS_ERR(instkey)) {
  1132. key_put(instkey);
  1133. key_ref = lookup_user_key(id,
  1134. KEY_LOOKUP_PARTIAL,
  1135. 0);
  1136. if (!IS_ERR(key_ref))
  1137. goto okay;
  1138. }
  1139. }
  1140. ret = PTR_ERR(key_ref);
  1141. goto error;
  1142. }
  1143. okay:
  1144. key = key_ref_to_ptr(key_ref);
  1145. key_set_timeout(key, timeout);
  1146. key_put(key);
  1147. ret = 0;
  1148. error:
  1149. return ret;
  1150. }
  1151. /*
  1152. * Assume (or clear) the authority to instantiate the specified key.
  1153. *
  1154. * This sets the authoritative token currently in force for key instantiation.
  1155. * This must be done for a key to be instantiated. It has the effect of making
  1156. * available all the keys from the caller of the request_key() that created a
  1157. * key to request_key() calls made by the caller of this function.
  1158. *
  1159. * The caller must have the instantiation key in their process keyrings with a
  1160. * Search permission grant available to the caller.
  1161. *
  1162. * If the ID given is 0, then the setting will be cleared and 0 returned.
  1163. *
  1164. * If the ID given has a matching an authorisation key, then that key will be
  1165. * set and its ID will be returned. The authorisation key can be read to get
  1166. * the callout information passed to request_key().
  1167. */
  1168. long keyctl_assume_authority(key_serial_t id)
  1169. {
  1170. struct key *authkey;
  1171. long ret;
  1172. /* special key IDs aren't permitted */
  1173. ret = -EINVAL;
  1174. if (id < 0)
  1175. goto error;
  1176. /* we divest ourselves of authority if given an ID of 0 */
  1177. if (id == 0) {
  1178. ret = keyctl_change_reqkey_auth(NULL);
  1179. goto error;
  1180. }
  1181. /* attempt to assume the authority temporarily granted to us whilst we
  1182. * instantiate the specified key
  1183. * - the authorisation key must be in the current task's keyrings
  1184. * somewhere
  1185. */
  1186. authkey = key_get_instantiation_authkey(id);
  1187. if (IS_ERR(authkey)) {
  1188. ret = PTR_ERR(authkey);
  1189. goto error;
  1190. }
  1191. ret = keyctl_change_reqkey_auth(authkey);
  1192. if (ret < 0)
  1193. goto error;
  1194. key_put(authkey);
  1195. ret = authkey->serial;
  1196. error:
  1197. return ret;
  1198. }
  1199. /*
  1200. * Get a key's the LSM security label.
  1201. *
  1202. * The key must grant the caller View permission for this to work.
  1203. *
  1204. * If there's a buffer, then up to buflen bytes of data will be placed into it.
  1205. *
  1206. * If successful, the amount of information available will be returned,
  1207. * irrespective of how much was copied (including the terminal NUL).
  1208. */
  1209. long keyctl_get_security(key_serial_t keyid,
  1210. char __user *buffer,
  1211. size_t buflen)
  1212. {
  1213. struct key *key, *instkey;
  1214. key_ref_t key_ref;
  1215. char *context;
  1216. long ret;
  1217. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
  1218. if (IS_ERR(key_ref)) {
  1219. if (PTR_ERR(key_ref) != -EACCES)
  1220. return PTR_ERR(key_ref);
  1221. /* viewing a key under construction is also permitted if we
  1222. * have the authorisation token handy */
  1223. instkey = key_get_instantiation_authkey(keyid);
  1224. if (IS_ERR(instkey))
  1225. return PTR_ERR(instkey);
  1226. key_put(instkey);
  1227. key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
  1228. if (IS_ERR(key_ref))
  1229. return PTR_ERR(key_ref);
  1230. }
  1231. key = key_ref_to_ptr(key_ref);
  1232. ret = security_key_getsecurity(key, &context);
  1233. if (ret == 0) {
  1234. /* if no information was returned, give userspace an empty
  1235. * string */
  1236. ret = 1;
  1237. if (buffer && buflen > 0 &&
  1238. copy_to_user(buffer, "", 1) != 0)
  1239. ret = -EFAULT;
  1240. } else if (ret > 0) {
  1241. /* return as much data as there's room for */
  1242. if (buffer && buflen > 0) {
  1243. if (buflen > ret)
  1244. buflen = ret;
  1245. if (copy_to_user(buffer, context, buflen) != 0)
  1246. ret = -EFAULT;
  1247. }
  1248. kfree(context);
  1249. }
  1250. key_ref_put(key_ref);
  1251. return ret;
  1252. }
  1253. /*
  1254. * Attempt to install the calling process's session keyring on the process's
  1255. * parent process.
  1256. *
  1257. * The keyring must exist and must grant the caller LINK permission, and the
  1258. * parent process must be single-threaded and must have the same effective
  1259. * ownership as this process and mustn't be SUID/SGID.
  1260. *
  1261. * The keyring will be emplaced on the parent when it next resumes userspace.
  1262. *
  1263. * If successful, 0 will be returned.
  1264. */
  1265. long keyctl_session_to_parent(void)
  1266. {
  1267. struct task_struct *me, *parent;
  1268. const struct cred *mycred, *pcred;
  1269. struct callback_head *newwork, *oldwork;
  1270. key_ref_t keyring_r;
  1271. struct cred *cred;
  1272. int ret;
  1273. keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
  1274. if (IS_ERR(keyring_r))
  1275. return PTR_ERR(keyring_r);
  1276. ret = -ENOMEM;
  1277. /* our parent is going to need a new cred struct, a new tgcred struct
  1278. * and new security data, so we allocate them here to prevent ENOMEM in
  1279. * our parent */
  1280. cred = cred_alloc_blank();
  1281. if (!cred)
  1282. goto error_keyring;
  1283. newwork = &cred->rcu;
  1284. cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
  1285. init_task_work(newwork, key_change_session_keyring);
  1286. me = current;
  1287. rcu_read_lock();
  1288. write_lock_irq(&tasklist_lock);
  1289. ret = -EPERM;
  1290. oldwork = NULL;
  1291. parent = me->real_parent;
  1292. task_lock(parent);
  1293. /* the parent mustn't be init and mustn't be a kernel thread */
  1294. if (parent->pid <= 1 || !parent->mm)
  1295. goto unlock;
  1296. /* the parent must be single threaded */
  1297. if (!thread_group_empty(parent))
  1298. goto unlock;
  1299. /* the parent and the child must have different session keyrings or
  1300. * there's no point */
  1301. mycred = current_cred();
  1302. pcred = __task_cred(parent);
  1303. if (mycred == pcred ||
  1304. mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) {
  1305. ret = 0;
  1306. goto unlock;
  1307. }
  1308. /* the parent must have the same effective ownership and mustn't be
  1309. * SUID/SGID */
  1310. if (pcred->uid != mycred->euid ||
  1311. pcred->euid != mycred->euid ||
  1312. pcred->suid != mycred->euid ||
  1313. pcred->gid != mycred->egid ||
  1314. pcred->egid != mycred->egid ||
  1315. pcred->sgid != mycred->egid)
  1316. goto unlock;
  1317. /* the keyrings must have the same UID */
  1318. if ((pcred->tgcred->session_keyring &&
  1319. pcred->tgcred->session_keyring->uid != mycred->euid) ||
  1320. mycred->tgcred->session_keyring->uid != mycred->euid)
  1321. goto unlock;
  1322. /* cancel an already pending keyring replacement */
  1323. oldwork = task_work_cancel(parent, key_change_session_keyring);
  1324. /* the replacement session keyring is applied just prior to userspace
  1325. * restarting */
  1326. ret = task_work_add(parent, newwork, true);
  1327. if (!ret)
  1328. newwork = NULL;
  1329. unlock:
  1330. task_unlock(parent);
  1331. write_unlock_irq(&tasklist_lock);
  1332. rcu_read_unlock();
  1333. if (oldwork)
  1334. put_cred(container_of(oldwork, struct cred, rcu));
  1335. if (newwork)
  1336. put_cred(cred);
  1337. return ret;
  1338. error_keyring:
  1339. key_ref_put(keyring_r);
  1340. return ret;
  1341. }
  1342. /*
  1343. * The key control system call
  1344. */
  1345. SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1346. unsigned long, arg4, unsigned long, arg5)
  1347. {
  1348. switch (option) {
  1349. case KEYCTL_GET_KEYRING_ID:
  1350. return keyctl_get_keyring_ID((key_serial_t) arg2,
  1351. (int) arg3);
  1352. case KEYCTL_JOIN_SESSION_KEYRING:
  1353. return keyctl_join_session_keyring((const char __user *) arg2);
  1354. case KEYCTL_UPDATE:
  1355. return keyctl_update_key((key_serial_t) arg2,
  1356. (const void __user *) arg3,
  1357. (size_t) arg4);
  1358. case KEYCTL_REVOKE:
  1359. return keyctl_revoke_key((key_serial_t) arg2);
  1360. case KEYCTL_DESCRIBE:
  1361. return keyctl_describe_key((key_serial_t) arg2,
  1362. (char __user *) arg3,
  1363. (unsigned) arg4);
  1364. case KEYCTL_CLEAR:
  1365. return keyctl_keyring_clear((key_serial_t) arg2);
  1366. case KEYCTL_LINK:
  1367. return keyctl_keyring_link((key_serial_t) arg2,
  1368. (key_serial_t) arg3);
  1369. case KEYCTL_UNLINK:
  1370. return keyctl_keyring_unlink((key_serial_t) arg2,
  1371. (key_serial_t) arg3);
  1372. case KEYCTL_SEARCH:
  1373. return keyctl_keyring_search((key_serial_t) arg2,
  1374. (const char __user *) arg3,
  1375. (const char __user *) arg4,
  1376. (key_serial_t) arg5);
  1377. case KEYCTL_READ:
  1378. return keyctl_read_key((key_serial_t) arg2,
  1379. (char __user *) arg3,
  1380. (size_t) arg4);
  1381. case KEYCTL_CHOWN:
  1382. return keyctl_chown_key((key_serial_t) arg2,
  1383. (uid_t) arg3,
  1384. (gid_t) arg4);
  1385. case KEYCTL_SETPERM:
  1386. return keyctl_setperm_key((key_serial_t) arg2,
  1387. (key_perm_t) arg3);
  1388. case KEYCTL_INSTANTIATE:
  1389. return keyctl_instantiate_key((key_serial_t) arg2,
  1390. (const void __user *) arg3,
  1391. (size_t) arg4,
  1392. (key_serial_t) arg5);
  1393. case KEYCTL_NEGATE:
  1394. return keyctl_negate_key((key_serial_t) arg2,
  1395. (unsigned) arg3,
  1396. (key_serial_t) arg4);
  1397. case KEYCTL_SET_REQKEY_KEYRING:
  1398. return keyctl_set_reqkey_keyring(arg2);
  1399. case KEYCTL_SET_TIMEOUT:
  1400. return keyctl_set_timeout((key_serial_t) arg2,
  1401. (unsigned) arg3);
  1402. case KEYCTL_ASSUME_AUTHORITY:
  1403. return keyctl_assume_authority((key_serial_t) arg2);
  1404. case KEYCTL_GET_SECURITY:
  1405. return keyctl_get_security((key_serial_t) arg2,
  1406. (char __user *) arg3,
  1407. (size_t) arg4);
  1408. case KEYCTL_SESSION_TO_PARENT:
  1409. return keyctl_session_to_parent();
  1410. case KEYCTL_REJECT:
  1411. return keyctl_reject_key((key_serial_t) arg2,
  1412. (unsigned) arg3,
  1413. (unsigned) arg4,
  1414. (key_serial_t) arg5);
  1415. case KEYCTL_INSTANTIATE_IOV:
  1416. return keyctl_instantiate_key_iov(
  1417. (key_serial_t) arg2,
  1418. (const struct iovec __user *) arg3,
  1419. (unsigned) arg4,
  1420. (key_serial_t) arg5);
  1421. case KEYCTL_INVALIDATE:
  1422. return keyctl_invalidate_key((key_serial_t) arg2);
  1423. default:
  1424. return -EOPNOTSUPP;
  1425. }
  1426. }