keyctl.c 49 KB

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