keyctl.c 43 KB

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