keyctl.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /* keyctl.c: userspace keyctl 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 <asm/uaccess.h>
  22. #include "internal.h"
  23. static int key_get_type_from_user(char *type,
  24. const char __user *_type,
  25. unsigned len)
  26. {
  27. int ret;
  28. ret = strncpy_from_user(type, _type, len);
  29. if (ret < 0)
  30. return -EFAULT;
  31. if (ret == 0 || ret >= len)
  32. return -EINVAL;
  33. if (type[0] == '.')
  34. return -EPERM;
  35. type[len - 1] = '\0';
  36. return 0;
  37. }
  38. /*****************************************************************************/
  39. /*
  40. * extract the description of a new key from userspace and either add it as a
  41. * new key to the specified keyring or update a matching key in that keyring
  42. * - the keyring must be writable
  43. * - returns the new key's serial number
  44. * - implements add_key()
  45. */
  46. asmlinkage long sys_add_key(const char __user *_type,
  47. const char __user *_description,
  48. const void __user *_payload,
  49. size_t plen,
  50. key_serial_t ringid)
  51. {
  52. key_ref_t keyring_ref, key_ref;
  53. char type[32], *description;
  54. void *payload;
  55. long ret;
  56. ret = -EINVAL;
  57. if (plen > 32767)
  58. goto error;
  59. /* draw all the data into kernel space */
  60. ret = key_get_type_from_user(type, _type, sizeof(type));
  61. if (ret < 0)
  62. goto error;
  63. description = strndup_user(_description, PAGE_SIZE);
  64. if (IS_ERR(description)) {
  65. ret = PTR_ERR(description);
  66. goto error;
  67. }
  68. /* pull the payload in if one was supplied */
  69. payload = NULL;
  70. if (_payload) {
  71. ret = -ENOMEM;
  72. payload = kmalloc(plen, GFP_KERNEL);
  73. if (!payload)
  74. goto error2;
  75. ret = -EFAULT;
  76. if (copy_from_user(payload, _payload, plen) != 0)
  77. goto error3;
  78. }
  79. /* find the target keyring (which must be writable) */
  80. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  81. if (IS_ERR(keyring_ref)) {
  82. ret = PTR_ERR(keyring_ref);
  83. goto error3;
  84. }
  85. /* create or update the requested key and add it to the target
  86. * keyring */
  87. key_ref = key_create_or_update(keyring_ref, type, description,
  88. payload, plen, KEY_ALLOC_IN_QUOTA);
  89. if (!IS_ERR(key_ref)) {
  90. ret = key_ref_to_ptr(key_ref)->serial;
  91. key_ref_put(key_ref);
  92. }
  93. else {
  94. ret = PTR_ERR(key_ref);
  95. }
  96. key_ref_put(keyring_ref);
  97. error3:
  98. kfree(payload);
  99. error2:
  100. kfree(description);
  101. error:
  102. return ret;
  103. } /* end sys_add_key() */
  104. /*****************************************************************************/
  105. /*
  106. * search the process keyrings for a matching key
  107. * - nested keyrings may also be searched if they have Search permission
  108. * - if a key is found, it will be attached to the destination keyring if
  109. * there's one specified
  110. * - /sbin/request-key will be invoked if _callout_info is non-NULL
  111. * - the _callout_info string will be passed to /sbin/request-key
  112. * - if the _callout_info string is empty, it will be rendered as "-"
  113. * - implements request_key()
  114. */
  115. asmlinkage long sys_request_key(const char __user *_type,
  116. const char __user *_description,
  117. const char __user *_callout_info,
  118. key_serial_t destringid)
  119. {
  120. struct key_type *ktype;
  121. struct key *key;
  122. key_ref_t dest_ref;
  123. char type[32], *description, *callout_info;
  124. long ret;
  125. /* pull the type into kernel space */
  126. ret = key_get_type_from_user(type, _type, sizeof(type));
  127. if (ret < 0)
  128. goto error;
  129. /* pull the description into kernel space */
  130. description = strndup_user(_description, PAGE_SIZE);
  131. if (IS_ERR(description)) {
  132. ret = PTR_ERR(description);
  133. goto error;
  134. }
  135. /* pull the callout info into kernel space */
  136. callout_info = NULL;
  137. if (_callout_info) {
  138. callout_info = strndup_user(_callout_info, PAGE_SIZE);
  139. if (IS_ERR(callout_info)) {
  140. ret = PTR_ERR(callout_info);
  141. goto error2;
  142. }
  143. }
  144. /* get the destination keyring if specified */
  145. dest_ref = NULL;
  146. if (destringid) {
  147. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  148. if (IS_ERR(dest_ref)) {
  149. ret = PTR_ERR(dest_ref);
  150. goto error3;
  151. }
  152. }
  153. /* find the key type */
  154. ktype = key_type_lookup(type);
  155. if (IS_ERR(ktype)) {
  156. ret = PTR_ERR(ktype);
  157. goto error4;
  158. }
  159. /* do the search */
  160. key = request_key_and_link(ktype, description, callout_info,
  161. key_ref_to_ptr(dest_ref),
  162. KEY_ALLOC_IN_QUOTA);
  163. if (IS_ERR(key)) {
  164. ret = PTR_ERR(key);
  165. goto error5;
  166. }
  167. ret = key->serial;
  168. key_put(key);
  169. error5:
  170. key_type_put(ktype);
  171. error4:
  172. key_ref_put(dest_ref);
  173. error3:
  174. kfree(callout_info);
  175. error2:
  176. kfree(description);
  177. error:
  178. return ret;
  179. } /* end sys_request_key() */
  180. /*****************************************************************************/
  181. /*
  182. * get the ID of the specified process keyring
  183. * - the keyring must have search permission to be found
  184. * - implements keyctl(KEYCTL_GET_KEYRING_ID)
  185. */
  186. long keyctl_get_keyring_ID(key_serial_t id, int create)
  187. {
  188. key_ref_t key_ref;
  189. long ret;
  190. key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
  191. if (IS_ERR(key_ref)) {
  192. ret = PTR_ERR(key_ref);
  193. goto error;
  194. }
  195. ret = key_ref_to_ptr(key_ref)->serial;
  196. key_ref_put(key_ref);
  197. error:
  198. return ret;
  199. } /* end keyctl_get_keyring_ID() */
  200. /*****************************************************************************/
  201. /*
  202. * join the session keyring
  203. * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
  204. */
  205. long keyctl_join_session_keyring(const char __user *_name)
  206. {
  207. char *name;
  208. long ret;
  209. /* fetch the name from userspace */
  210. name = NULL;
  211. if (_name) {
  212. name = strndup_user(_name, PAGE_SIZE);
  213. if (IS_ERR(name)) {
  214. ret = PTR_ERR(name);
  215. goto error;
  216. }
  217. }
  218. /* join the session */
  219. ret = join_session_keyring(name);
  220. error:
  221. return ret;
  222. } /* end keyctl_join_session_keyring() */
  223. /*****************************************************************************/
  224. /*
  225. * update a key's data payload
  226. * - the key must be writable
  227. * - implements keyctl(KEYCTL_UPDATE)
  228. */
  229. long keyctl_update_key(key_serial_t id,
  230. const void __user *_payload,
  231. size_t plen)
  232. {
  233. key_ref_t key_ref;
  234. void *payload;
  235. long ret;
  236. ret = -EINVAL;
  237. if (plen > PAGE_SIZE)
  238. goto error;
  239. /* pull the payload in if one was supplied */
  240. payload = NULL;
  241. if (_payload) {
  242. ret = -ENOMEM;
  243. payload = kmalloc(plen, GFP_KERNEL);
  244. if (!payload)
  245. goto error;
  246. ret = -EFAULT;
  247. if (copy_from_user(payload, _payload, plen) != 0)
  248. goto error2;
  249. }
  250. /* find the target key (which must be writable) */
  251. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  252. if (IS_ERR(key_ref)) {
  253. ret = PTR_ERR(key_ref);
  254. goto error2;
  255. }
  256. /* update the key */
  257. ret = key_update(key_ref, payload, plen);
  258. key_ref_put(key_ref);
  259. error2:
  260. kfree(payload);
  261. error:
  262. return ret;
  263. } /* end keyctl_update_key() */
  264. /*****************************************************************************/
  265. /*
  266. * revoke a key
  267. * - the key must be writable
  268. * - implements keyctl(KEYCTL_REVOKE)
  269. */
  270. long keyctl_revoke_key(key_serial_t id)
  271. {
  272. key_ref_t key_ref;
  273. long ret;
  274. key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
  275. if (IS_ERR(key_ref)) {
  276. ret = PTR_ERR(key_ref);
  277. goto error;
  278. }
  279. key_revoke(key_ref_to_ptr(key_ref));
  280. ret = 0;
  281. key_ref_put(key_ref);
  282. error:
  283. return ret;
  284. } /* end keyctl_revoke_key() */
  285. /*****************************************************************************/
  286. /*
  287. * clear the specified process keyring
  288. * - the keyring must be writable
  289. * - implements keyctl(KEYCTL_CLEAR)
  290. */
  291. long keyctl_keyring_clear(key_serial_t ringid)
  292. {
  293. key_ref_t keyring_ref;
  294. long ret;
  295. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  296. if (IS_ERR(keyring_ref)) {
  297. ret = PTR_ERR(keyring_ref);
  298. goto error;
  299. }
  300. ret = keyring_clear(key_ref_to_ptr(keyring_ref));
  301. key_ref_put(keyring_ref);
  302. error:
  303. return ret;
  304. } /* end keyctl_keyring_clear() */
  305. /*****************************************************************************/
  306. /*
  307. * link a key into a keyring
  308. * - the keyring must be writable
  309. * - the key must be linkable
  310. * - implements keyctl(KEYCTL_LINK)
  311. */
  312. long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
  313. {
  314. key_ref_t keyring_ref, key_ref;
  315. long ret;
  316. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  317. if (IS_ERR(keyring_ref)) {
  318. ret = PTR_ERR(keyring_ref);
  319. goto error;
  320. }
  321. key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
  322. if (IS_ERR(key_ref)) {
  323. ret = PTR_ERR(key_ref);
  324. goto error2;
  325. }
  326. ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  327. key_ref_put(key_ref);
  328. error2:
  329. key_ref_put(keyring_ref);
  330. error:
  331. return ret;
  332. } /* end keyctl_keyring_link() */
  333. /*****************************************************************************/
  334. /*
  335. * unlink the first attachment of a key from a keyring
  336. * - the keyring must be writable
  337. * - we don't need any permissions on the key
  338. * - implements keyctl(KEYCTL_UNLINK)
  339. */
  340. long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
  341. {
  342. key_ref_t keyring_ref, key_ref;
  343. long ret;
  344. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
  345. if (IS_ERR(keyring_ref)) {
  346. ret = PTR_ERR(keyring_ref);
  347. goto error;
  348. }
  349. key_ref = lookup_user_key(NULL, id, 0, 0, 0);
  350. if (IS_ERR(key_ref)) {
  351. ret = PTR_ERR(key_ref);
  352. goto error2;
  353. }
  354. ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
  355. key_ref_put(key_ref);
  356. error2:
  357. key_ref_put(keyring_ref);
  358. error:
  359. return ret;
  360. } /* end keyctl_keyring_unlink() */
  361. /*****************************************************************************/
  362. /*
  363. * describe a user key
  364. * - the key must have view permission
  365. * - if there's a buffer, we place up to buflen bytes of data into it
  366. * - unless there's an error, we return the amount of description available,
  367. * irrespective of how much we may have copied
  368. * - the description is formatted thus:
  369. * type;uid;gid;perm;description<NUL>
  370. * - implements keyctl(KEYCTL_DESCRIBE)
  371. */
  372. long keyctl_describe_key(key_serial_t keyid,
  373. char __user *buffer,
  374. size_t buflen)
  375. {
  376. struct key *key, *instkey;
  377. key_ref_t key_ref;
  378. char *tmpbuf;
  379. long ret;
  380. key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
  381. if (IS_ERR(key_ref)) {
  382. /* viewing a key under construction is permitted if we have the
  383. * authorisation token handy */
  384. if (PTR_ERR(key_ref) == -EACCES) {
  385. instkey = key_get_instantiation_authkey(keyid);
  386. if (!IS_ERR(instkey)) {
  387. key_put(instkey);
  388. key_ref = lookup_user_key(NULL, keyid,
  389. 0, 1, 0);
  390. if (!IS_ERR(key_ref))
  391. goto okay;
  392. }
  393. }
  394. ret = PTR_ERR(key_ref);
  395. goto error;
  396. }
  397. okay:
  398. /* calculate how much description we're going to return */
  399. ret = -ENOMEM;
  400. tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  401. if (!tmpbuf)
  402. goto error2;
  403. key = key_ref_to_ptr(key_ref);
  404. ret = snprintf(tmpbuf, PAGE_SIZE - 1,
  405. "%s;%d;%d;%08x;%s",
  406. key_ref_to_ptr(key_ref)->type->name,
  407. key_ref_to_ptr(key_ref)->uid,
  408. key_ref_to_ptr(key_ref)->gid,
  409. key_ref_to_ptr(key_ref)->perm,
  410. key_ref_to_ptr(key_ref)->description ?
  411. key_ref_to_ptr(key_ref)->description : ""
  412. );
  413. /* include a NUL char at the end of the data */
  414. if (ret > PAGE_SIZE - 1)
  415. ret = PAGE_SIZE - 1;
  416. tmpbuf[ret] = 0;
  417. ret++;
  418. /* consider returning the data */
  419. if (buffer && buflen > 0) {
  420. if (buflen > ret)
  421. buflen = ret;
  422. if (copy_to_user(buffer, tmpbuf, buflen) != 0)
  423. ret = -EFAULT;
  424. }
  425. kfree(tmpbuf);
  426. error2:
  427. key_ref_put(key_ref);
  428. error:
  429. return ret;
  430. } /* end keyctl_describe_key() */
  431. /*****************************************************************************/
  432. /*
  433. * search the specified keyring for a matching key
  434. * - the start keyring must be searchable
  435. * - nested keyrings may also be searched if they are searchable
  436. * - only keys with search permission may be found
  437. * - if a key is found, it will be attached to the destination keyring if
  438. * there's one specified
  439. * - implements keyctl(KEYCTL_SEARCH)
  440. */
  441. long keyctl_keyring_search(key_serial_t ringid,
  442. const char __user *_type,
  443. const char __user *_description,
  444. key_serial_t destringid)
  445. {
  446. struct key_type *ktype;
  447. key_ref_t keyring_ref, key_ref, dest_ref;
  448. char type[32], *description;
  449. long ret;
  450. /* pull the type and description into kernel space */
  451. ret = key_get_type_from_user(type, _type, sizeof(type));
  452. if (ret < 0)
  453. goto error;
  454. description = strndup_user(_description, PAGE_SIZE);
  455. if (IS_ERR(description)) {
  456. ret = PTR_ERR(description);
  457. goto error;
  458. }
  459. /* get the keyring at which to begin the search */
  460. keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
  461. if (IS_ERR(keyring_ref)) {
  462. ret = PTR_ERR(keyring_ref);
  463. goto error2;
  464. }
  465. /* get the destination keyring if specified */
  466. dest_ref = NULL;
  467. if (destringid) {
  468. dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
  469. if (IS_ERR(dest_ref)) {
  470. ret = PTR_ERR(dest_ref);
  471. goto error3;
  472. }
  473. }
  474. /* find the key type */
  475. ktype = key_type_lookup(type);
  476. if (IS_ERR(ktype)) {
  477. ret = PTR_ERR(ktype);
  478. goto error4;
  479. }
  480. /* do the search */
  481. key_ref = keyring_search(keyring_ref, ktype, description);
  482. if (IS_ERR(key_ref)) {
  483. ret = PTR_ERR(key_ref);
  484. /* treat lack or presence of a negative key the same */
  485. if (ret == -EAGAIN)
  486. ret = -ENOKEY;
  487. goto error5;
  488. }
  489. /* link the resulting key to the destination keyring if we can */
  490. if (dest_ref) {
  491. ret = key_permission(key_ref, KEY_LINK);
  492. if (ret < 0)
  493. goto error6;
  494. ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
  495. if (ret < 0)
  496. goto error6;
  497. }
  498. ret = key_ref_to_ptr(key_ref)->serial;
  499. error6:
  500. key_ref_put(key_ref);
  501. error5:
  502. key_type_put(ktype);
  503. error4:
  504. key_ref_put(dest_ref);
  505. error3:
  506. key_ref_put(keyring_ref);
  507. error2:
  508. kfree(description);
  509. error:
  510. return ret;
  511. } /* end keyctl_keyring_search() */
  512. /*****************************************************************************/
  513. /*
  514. * read a user key's payload
  515. * - the keyring must be readable or the key must be searchable from the
  516. * process's keyrings
  517. * - if there's a buffer, we place up to buflen bytes of data into it
  518. * - unless there's an error, we return the amount of data in the key,
  519. * irrespective of how much we may have copied
  520. * - implements keyctl(KEYCTL_READ)
  521. */
  522. long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
  523. {
  524. struct key *key;
  525. key_ref_t key_ref;
  526. long ret;
  527. /* find the key first */
  528. key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
  529. if (IS_ERR(key_ref)) {
  530. ret = -ENOKEY;
  531. goto error;
  532. }
  533. key = key_ref_to_ptr(key_ref);
  534. /* see if we can read it directly */
  535. ret = key_permission(key_ref, KEY_READ);
  536. if (ret == 0)
  537. goto can_read_key;
  538. if (ret != -EACCES)
  539. goto error;
  540. /* we can't; see if it's searchable from this process's keyrings
  541. * - we automatically take account of the fact that it may be
  542. * dangling off an instantiation key
  543. */
  544. if (!is_key_possessed(key_ref)) {
  545. ret = -EACCES;
  546. goto error2;
  547. }
  548. /* the key is probably readable - now try to read it */
  549. can_read_key:
  550. ret = key_validate(key);
  551. if (ret == 0) {
  552. ret = -EOPNOTSUPP;
  553. if (key->type->read) {
  554. /* read the data with the semaphore held (since we
  555. * might sleep) */
  556. down_read(&key->sem);
  557. ret = key->type->read(key, buffer, buflen);
  558. up_read(&key->sem);
  559. }
  560. }
  561. error2:
  562. key_put(key);
  563. error:
  564. return ret;
  565. } /* end keyctl_read_key() */
  566. /*****************************************************************************/
  567. /*
  568. * change the ownership of a key
  569. * - the keyring owned by the changer
  570. * - if the uid or gid is -1, then that parameter is not changed
  571. * - implements keyctl(KEYCTL_CHOWN)
  572. */
  573. long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
  574. {
  575. struct key *key;
  576. key_ref_t key_ref;
  577. long ret;
  578. ret = 0;
  579. if (uid == (uid_t) -1 && gid == (gid_t) -1)
  580. goto error;
  581. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  582. if (IS_ERR(key_ref)) {
  583. ret = PTR_ERR(key_ref);
  584. goto error;
  585. }
  586. key = key_ref_to_ptr(key_ref);
  587. /* make the changes with the locks held to prevent chown/chown races */
  588. ret = -EACCES;
  589. down_write(&key->sem);
  590. if (!capable(CAP_SYS_ADMIN)) {
  591. /* only the sysadmin can chown a key to some other UID */
  592. if (uid != (uid_t) -1 && key->uid != uid)
  593. goto no_access;
  594. /* only the sysadmin can set the key's GID to a group other
  595. * than one of those that the current process subscribes to */
  596. if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
  597. goto no_access;
  598. }
  599. /* change the UID (have to update the quotas) */
  600. if (uid != (uid_t) -1 && uid != key->uid) {
  601. /* don't support UID changing yet */
  602. ret = -EOPNOTSUPP;
  603. goto no_access;
  604. }
  605. /* change the GID */
  606. if (gid != (gid_t) -1)
  607. key->gid = gid;
  608. ret = 0;
  609. no_access:
  610. up_write(&key->sem);
  611. key_put(key);
  612. error:
  613. return ret;
  614. } /* end keyctl_chown_key() */
  615. /*****************************************************************************/
  616. /*
  617. * change the permission mask on a key
  618. * - the keyring owned by the changer
  619. * - implements keyctl(KEYCTL_SETPERM)
  620. */
  621. long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
  622. {
  623. struct key *key;
  624. key_ref_t key_ref;
  625. long ret;
  626. ret = -EINVAL;
  627. if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
  628. goto error;
  629. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  630. if (IS_ERR(key_ref)) {
  631. ret = PTR_ERR(key_ref);
  632. goto error;
  633. }
  634. key = key_ref_to_ptr(key_ref);
  635. /* make the changes with the locks held to prevent chown/chmod races */
  636. ret = -EACCES;
  637. down_write(&key->sem);
  638. /* if we're not the sysadmin, we can only change a key that we own */
  639. if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
  640. key->perm = perm;
  641. ret = 0;
  642. }
  643. up_write(&key->sem);
  644. key_put(key);
  645. error:
  646. return ret;
  647. } /* end keyctl_setperm_key() */
  648. /*****************************************************************************/
  649. /*
  650. * instantiate the key with the specified payload, and, if one is given, link
  651. * the key into the keyring
  652. */
  653. long keyctl_instantiate_key(key_serial_t id,
  654. const void __user *_payload,
  655. size_t plen,
  656. key_serial_t ringid)
  657. {
  658. struct request_key_auth *rka;
  659. struct key *instkey;
  660. key_ref_t keyring_ref;
  661. void *payload;
  662. long ret;
  663. ret = -EINVAL;
  664. if (plen > 32767)
  665. goto error;
  666. /* the appropriate instantiation authorisation key must have been
  667. * assumed before calling this */
  668. ret = -EPERM;
  669. instkey = current->request_key_auth;
  670. if (!instkey)
  671. goto error;
  672. rka = instkey->payload.data;
  673. if (rka->target_key->serial != id)
  674. goto error;
  675. /* pull the payload in if one was supplied */
  676. payload = NULL;
  677. if (_payload) {
  678. ret = -ENOMEM;
  679. payload = kmalloc(plen, GFP_KERNEL);
  680. if (!payload)
  681. goto error;
  682. ret = -EFAULT;
  683. if (copy_from_user(payload, _payload, plen) != 0)
  684. goto error2;
  685. }
  686. /* find the destination keyring amongst those belonging to the
  687. * requesting task */
  688. keyring_ref = NULL;
  689. if (ringid) {
  690. keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
  691. KEY_WRITE);
  692. if (IS_ERR(keyring_ref)) {
  693. ret = PTR_ERR(keyring_ref);
  694. goto error2;
  695. }
  696. }
  697. /* instantiate the key and link it into a keyring */
  698. ret = key_instantiate_and_link(rka->target_key, payload, plen,
  699. key_ref_to_ptr(keyring_ref), instkey);
  700. key_ref_put(keyring_ref);
  701. /* discard the assumed authority if it's just been disabled by
  702. * instantiation of the key */
  703. if (ret == 0) {
  704. key_put(current->request_key_auth);
  705. current->request_key_auth = NULL;
  706. }
  707. error2:
  708. kfree(payload);
  709. error:
  710. return ret;
  711. } /* end keyctl_instantiate_key() */
  712. /*****************************************************************************/
  713. /*
  714. * negatively instantiate the key with the given timeout (in seconds), and, if
  715. * one is given, link the key into the keyring
  716. */
  717. long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
  718. {
  719. struct request_key_auth *rka;
  720. struct key *instkey;
  721. key_ref_t keyring_ref;
  722. long ret;
  723. /* the appropriate instantiation authorisation key must have been
  724. * assumed before calling this */
  725. ret = -EPERM;
  726. instkey = current->request_key_auth;
  727. if (!instkey)
  728. goto error;
  729. rka = instkey->payload.data;
  730. if (rka->target_key->serial != id)
  731. goto error;
  732. /* find the destination keyring if present (which must also be
  733. * writable) */
  734. keyring_ref = NULL;
  735. if (ringid) {
  736. keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
  737. if (IS_ERR(keyring_ref)) {
  738. ret = PTR_ERR(keyring_ref);
  739. goto error;
  740. }
  741. }
  742. /* instantiate the key and link it into a keyring */
  743. ret = key_negate_and_link(rka->target_key, timeout,
  744. key_ref_to_ptr(keyring_ref), instkey);
  745. key_ref_put(keyring_ref);
  746. /* discard the assumed authority if it's just been disabled by
  747. * instantiation of the key */
  748. if (ret == 0) {
  749. key_put(current->request_key_auth);
  750. current->request_key_auth = NULL;
  751. }
  752. error:
  753. return ret;
  754. } /* end keyctl_negate_key() */
  755. /*****************************************************************************/
  756. /*
  757. * set the default keyring in which request_key() will cache keys
  758. * - return the old setting
  759. */
  760. long keyctl_set_reqkey_keyring(int reqkey_defl)
  761. {
  762. int ret;
  763. switch (reqkey_defl) {
  764. case KEY_REQKEY_DEFL_THREAD_KEYRING:
  765. ret = install_thread_keyring(current);
  766. if (ret < 0)
  767. return ret;
  768. goto set;
  769. case KEY_REQKEY_DEFL_PROCESS_KEYRING:
  770. ret = install_process_keyring(current);
  771. if (ret < 0)
  772. return ret;
  773. case KEY_REQKEY_DEFL_DEFAULT:
  774. case KEY_REQKEY_DEFL_SESSION_KEYRING:
  775. case KEY_REQKEY_DEFL_USER_KEYRING:
  776. case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
  777. set:
  778. current->jit_keyring = reqkey_defl;
  779. case KEY_REQKEY_DEFL_NO_CHANGE:
  780. return current->jit_keyring;
  781. case KEY_REQKEY_DEFL_GROUP_KEYRING:
  782. default:
  783. return -EINVAL;
  784. }
  785. } /* end keyctl_set_reqkey_keyring() */
  786. /*****************************************************************************/
  787. /*
  788. * set or clear the timeout for a key
  789. */
  790. long keyctl_set_timeout(key_serial_t id, unsigned timeout)
  791. {
  792. struct timespec now;
  793. struct key *key;
  794. key_ref_t key_ref;
  795. time_t expiry;
  796. long ret;
  797. key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
  798. if (IS_ERR(key_ref)) {
  799. ret = PTR_ERR(key_ref);
  800. goto error;
  801. }
  802. key = key_ref_to_ptr(key_ref);
  803. /* make the changes with the locks held to prevent races */
  804. down_write(&key->sem);
  805. expiry = 0;
  806. if (timeout > 0) {
  807. now = current_kernel_time();
  808. expiry = now.tv_sec + timeout;
  809. }
  810. key->expiry = expiry;
  811. up_write(&key->sem);
  812. key_put(key);
  813. ret = 0;
  814. error:
  815. return ret;
  816. } /* end keyctl_set_timeout() */
  817. /*****************************************************************************/
  818. /*
  819. * assume the authority to instantiate the specified key
  820. */
  821. long keyctl_assume_authority(key_serial_t id)
  822. {
  823. struct key *authkey;
  824. long ret;
  825. /* special key IDs aren't permitted */
  826. ret = -EINVAL;
  827. if (id < 0)
  828. goto error;
  829. /* we divest ourselves of authority if given an ID of 0 */
  830. if (id == 0) {
  831. key_put(current->request_key_auth);
  832. current->request_key_auth = NULL;
  833. ret = 0;
  834. goto error;
  835. }
  836. /* attempt to assume the authority temporarily granted to us whilst we
  837. * instantiate the specified key
  838. * - the authorisation key must be in the current task's keyrings
  839. * somewhere
  840. */
  841. authkey = key_get_instantiation_authkey(id);
  842. if (IS_ERR(authkey)) {
  843. ret = PTR_ERR(authkey);
  844. goto error;
  845. }
  846. key_put(current->request_key_auth);
  847. current->request_key_auth = authkey;
  848. ret = authkey->serial;
  849. error:
  850. return ret;
  851. } /* end keyctl_assume_authority() */
  852. /*****************************************************************************/
  853. /*
  854. * the key control system call
  855. */
  856. asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
  857. unsigned long arg4, unsigned long arg5)
  858. {
  859. switch (option) {
  860. case KEYCTL_GET_KEYRING_ID:
  861. return keyctl_get_keyring_ID((key_serial_t) arg2,
  862. (int) arg3);
  863. case KEYCTL_JOIN_SESSION_KEYRING:
  864. return keyctl_join_session_keyring((const char __user *) arg2);
  865. case KEYCTL_UPDATE:
  866. return keyctl_update_key((key_serial_t) arg2,
  867. (const void __user *) arg3,
  868. (size_t) arg4);
  869. case KEYCTL_REVOKE:
  870. return keyctl_revoke_key((key_serial_t) arg2);
  871. case KEYCTL_DESCRIBE:
  872. return keyctl_describe_key((key_serial_t) arg2,
  873. (char __user *) arg3,
  874. (unsigned) arg4);
  875. case KEYCTL_CLEAR:
  876. return keyctl_keyring_clear((key_serial_t) arg2);
  877. case KEYCTL_LINK:
  878. return keyctl_keyring_link((key_serial_t) arg2,
  879. (key_serial_t) arg3);
  880. case KEYCTL_UNLINK:
  881. return keyctl_keyring_unlink((key_serial_t) arg2,
  882. (key_serial_t) arg3);
  883. case KEYCTL_SEARCH:
  884. return keyctl_keyring_search((key_serial_t) arg2,
  885. (const char __user *) arg3,
  886. (const char __user *) arg4,
  887. (key_serial_t) arg5);
  888. case KEYCTL_READ:
  889. return keyctl_read_key((key_serial_t) arg2,
  890. (char __user *) arg3,
  891. (size_t) arg4);
  892. case KEYCTL_CHOWN:
  893. return keyctl_chown_key((key_serial_t) arg2,
  894. (uid_t) arg3,
  895. (gid_t) arg4);
  896. case KEYCTL_SETPERM:
  897. return keyctl_setperm_key((key_serial_t) arg2,
  898. (key_perm_t) arg3);
  899. case KEYCTL_INSTANTIATE:
  900. return keyctl_instantiate_key((key_serial_t) arg2,
  901. (const void __user *) arg3,
  902. (size_t) arg4,
  903. (key_serial_t) arg5);
  904. case KEYCTL_NEGATE:
  905. return keyctl_negate_key((key_serial_t) arg2,
  906. (unsigned) arg3,
  907. (key_serial_t) arg4);
  908. case KEYCTL_SET_REQKEY_KEYRING:
  909. return keyctl_set_reqkey_keyring(arg2);
  910. case KEYCTL_SET_TIMEOUT:
  911. return keyctl_set_timeout((key_serial_t) arg2,
  912. (unsigned) arg3);
  913. case KEYCTL_ASSUME_AUTHORITY:
  914. return keyctl_assume_authority((key_serial_t) arg2);
  915. default:
  916. return -EOPNOTSUPP;
  917. }
  918. } /* end sys_keyctl() */