keyctl.c 39 KB

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