keyctl.c 36 KB

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