keyctl.c 43 KB

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