keyctl.c 44 KB

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