keyctl.c 41 KB

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