keyctl.c 41 KB

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