keyctl.c 40 KB

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