keyctl.c 39 KB

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