12 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
20 #include <linux/capability.h>
21 #include <linux/string.h>
25 #include <asm/uaccess.h>
28 static int key_get_type_from_user(
char *
type,
29 const char __user *_type,
37 if (ret == 0 || ret >= len)
58 const char __user *, _description,
59 const void __user *, _payload,
63 key_ref_t keyring_ref, key_ref;
70 if (
plen > 1024 * 1024 - 1)
74 ret = key_get_type_from_user(type, _type,
sizeof(type));
81 if (IS_ERR(description)) {
82 ret = PTR_ERR(description);
114 if (IS_ERR(keyring_ref)) {
115 ret = PTR_ERR(keyring_ref);
122 payload,
plen, KEY_PERM_UNDEF,
124 if (!IS_ERR(key_ref)) {
125 ret = key_ref_to_ptr(key_ref)->serial;
126 key_ref_put(key_ref);
129 ret = PTR_ERR(key_ref);
132 key_ref_put(keyring_ref);
158 const char __user *, _description,
159 const char __user *, _callout_info,
160 key_serial_t, destringid)
162 struct key_type *
ktype;
170 ret = key_get_type_from_user(type, _type,
sizeof(type));
176 if (IS_ERR(description)) {
177 ret = PTR_ERR(description);
186 if (IS_ERR(callout_info)) {
187 ret = PTR_ERR(callout_info);
190 callout_len =
strlen(callout_info);
198 if (IS_ERR(dest_ref)) {
199 ret = PTR_ERR(dest_ref);
207 ret = PTR_ERR(ktype);
213 callout_len,
NULL, key_ref_to_ptr(dest_ref),
232 key_ref_put(dest_ref);
251 unsigned long lflags;
256 if (IS_ERR(key_ref)) {
257 ret = PTR_ERR(key_ref);
261 ret = key_ref_to_ptr(key_ref)->serial;
262 key_ref_put(key_ref);
311 const void __user *_payload,
337 if (IS_ERR(key_ref)) {
338 ret = PTR_ERR(key_ref);
345 key_ref_put(key_ref);
368 if (IS_ERR(key_ref)) {
369 ret = PTR_ERR(key_ref);
373 if (IS_ERR(key_ref)) {
374 ret = PTR_ERR(key_ref);
382 key_ref_put(key_ref);
404 if (IS_ERR(key_ref)) {
405 ret = PTR_ERR(key_ref);
412 key_ref_put(key_ref);
427 key_ref_t keyring_ref;
431 if (IS_ERR(keyring_ref)) {
432 ret = PTR_ERR(keyring_ref);
437 if (IS_ERR(keyring_ref))
439 if (
test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
440 &key_ref_to_ptr(keyring_ref)->
flags))
451 key_ref_put(keyring_ref);
469 key_ref_t keyring_ref, key_ref;
473 if (IS_ERR(keyring_ref)) {
474 ret = PTR_ERR(keyring_ref);
479 if (IS_ERR(key_ref)) {
480 ret = PTR_ERR(key_ref);
484 ret =
key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
486 key_ref_put(key_ref);
488 key_ref_put(keyring_ref);
504 key_ref_t keyring_ref, key_ref;
508 if (IS_ERR(keyring_ref)) {
509 ret = PTR_ERR(keyring_ref);
514 if (IS_ERR(key_ref)) {
515 ret = PTR_ERR(key_ref);
519 ret =
key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
521 key_ref_put(key_ref);
523 key_ref_put(keyring_ref);
545 struct key *
key, *instkey;
551 if (IS_ERR(key_ref)) {
554 if (PTR_ERR(key_ref) == -
EACCES) {
556 if (!IS_ERR(instkey)) {
561 if (!IS_ERR(key_ref))
566 ret = PTR_ERR(key_ref);
577 key = key_ref_to_ptr(key_ref);
585 key->description ?:
"");
594 if (buffer && buflen > 0) {
604 key_ref_put(key_ref);
620 const char __user *_type,
621 const char __user *_description,
622 key_serial_t destringid)
624 struct key_type *
ktype;
625 key_ref_t keyring_ref, key_ref, dest_ref;
630 ret = key_get_type_from_user(type, _type,
sizeof(type));
635 if (IS_ERR(description)) {
636 ret = PTR_ERR(description);
642 if (IS_ERR(keyring_ref)) {
643 ret = PTR_ERR(keyring_ref);
652 if (IS_ERR(dest_ref)) {
653 ret = PTR_ERR(dest_ref);
661 ret = PTR_ERR(ktype);
667 if (IS_ERR(key_ref)) {
668 ret = PTR_ERR(key_ref);
678 ret = key_permission(key_ref,
KEY_LINK);
682 ret =
key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
687 ret = key_ref_to_ptr(key_ref)->serial;
690 key_ref_put(key_ref);
694 key_ref_put(dest_ref);
696 key_ref_put(keyring_ref);
721 if (IS_ERR(key_ref)) {
726 key = key_ref_to_ptr(key_ref);
729 ret = key_permission(key_ref,
KEY_READ);
739 if (!is_key_possessed(key_ref)) {
749 if (key->type->read) {
753 ret = key->type->read(key, buffer, buflen);
791 if ((user != (
uid_t) -1) && !uid_valid(uid))
793 if ((group != (
gid_t) -1) && !gid_valid(gid))
797 if (user == (
uid_t) -1 && group == (
gid_t) -1)
802 if (IS_ERR(key_ref)) {
803 ret = PTR_ERR(key_ref);
807 key = key_ref_to_ptr(key_ref);
815 if (user != (
uid_t) -1 && !uid_eq(key->uid, uid))
825 if (user != (
uid_t) -1 && !uid_eq(uid, key->uid)) {
832 if (
test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
838 spin_lock(&newowner->
lock);
839 if (newowner->
qnkeys + 1 >= maxkeys ||
840 newowner->
qnbytes + key->quotalen >= maxbytes ||
841 newowner->
qnbytes + key->quotalen <
846 newowner->
qnbytes += key->quotalen;
847 spin_unlock(&newowner->
lock);
849 spin_lock(&key->user->lock);
851 key->user->qnbytes -= key->quotalen;
852 spin_unlock(&key->user->lock);
858 if (
test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
863 zapowner = key->user;
864 key->user = newowner;
869 if (group != (
gid_t) -1)
883 spin_unlock(&newowner->
lock);
903 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
908 if (IS_ERR(key_ref)) {
909 ret = PTR_ERR(key_ref);
913 key = key_ref_to_ptr(key_ref);
935 static long get_instantiation_keyring(key_serial_t ringid,
937 struct key **_dest_keyring)
941 *_dest_keyring =
NULL;
951 return PTR_ERR(dkref);
952 *_dest_keyring = key_ref_to_ptr(dkref);
972 static int keyctl_change_reqkey_auth(
struct key *
key)
980 key_put(new->request_key_auth);
981 new->request_key_auth = key_get(key);
989 static long copy_from_user_iovec(
void *
buffer,
const struct iovec *iov,
992 for (; ioc > 0; ioc--) {
1011 const struct iovec *payload_iov,
1014 key_serial_t ringid)
1018 struct key *instkey, *dest_keyring;
1023 kenter(
"%d,,%zu,%d",
id, plen, ringid);
1026 if (plen > 1024 * 1024 - 1)
1032 instkey = cred->request_key_auth;
1036 rka = instkey->payload.data;
1055 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1062 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1068 dest_keyring, instkey);
1075 keyctl_change_reqkey_auth(
NULL);
1096 const void __user *_payload,
1098 key_serial_t ringid)
1100 if (_payload && plen) {
1101 struct iovec iov[1] = {
1122 const struct iovec __user *_payload_iov,
1124 key_serial_t ringid)
1126 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1129 if (!_payload_iov || !ioc)
1137 goto no_payload_free;
1141 if (iov != iovstack)
1146 if (iov != iovstack)
1188 key_serial_t ringid)
1192 struct key *instkey, *dest_keyring;
1195 kenter(
"%d,%u,%u,%d",
id, timeout, error, ringid);
1209 instkey = cred->request_key_auth;
1213 rka = instkey->payload.data;
1219 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1225 dest_keyring, instkey);
1232 keyctl_change_reqkey_auth(
NULL);
1248 int ret, old_setting;
1259 switch (reqkey_defl) {
1290 new->jit_keyring = reqkey_defl;
1312 struct key *
key, *instkey;
1318 if (IS_ERR(key_ref)) {
1321 if (PTR_ERR(key_ref) == -
EACCES) {
1323 if (!IS_ERR(instkey)) {
1328 if (!IS_ERR(key_ref))
1333 ret = PTR_ERR(key_ref);
1338 key = key_ref_to_ptr(key_ref);
1366 struct key *authkey;
1376 ret = keyctl_change_reqkey_auth(
NULL);
1386 if (IS_ERR(authkey)) {
1387 ret = PTR_ERR(authkey);
1391 ret = keyctl_change_reqkey_auth(authkey);
1396 ret = authkey->serial;
1412 char __user *buffer,
1415 struct key *
key, *instkey;
1421 if (IS_ERR(key_ref)) {
1422 if (PTR_ERR(key_ref) != -
EACCES)
1423 return PTR_ERR(key_ref);
1428 if (IS_ERR(instkey))
1429 return PTR_ERR(instkey);
1433 if (IS_ERR(key_ref))
1434 return PTR_ERR(key_ref);
1437 key = key_ref_to_ptr(key_ref);
1438 ret = security_key_getsecurity(key, &context);
1443 if (buffer && buflen > 0 &&
1446 }
else if (ret > 0) {
1448 if (buffer && buflen > 0) {
1459 key_ref_put(key_ref);
1478 const struct cred *mycred, *pcred;
1480 key_ref_t keyring_r;
1485 if (IS_ERR(keyring_r))
1486 return PTR_ERR(keyring_r);
1496 newwork = &cred->
rcu;
1498 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1510 if (parent->
pid <= 1 || !parent->
mm)
1514 if (!thread_group_empty(parent))
1521 if (mycred == pcred ||
1522 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) {
1529 if (!uid_eq(pcred->
uid, mycred->
euid) ||
1530 !uid_eq(pcred->
euid, mycred->
euid) ||
1531 !uid_eq(pcred->
suid, mycred->
euid) ||
1532 !gid_eq(pcred->
gid, mycred->
egid) ||
1533 !gid_eq(pcred->
egid, mycred->
egid) ||
1534 !gid_eq(pcred->
sgid, mycred->
egid))
1538 if ((pcred->tgcred->session_keyring &&
1539 !uid_eq(pcred->tgcred->session_keyring->
uid, mycred->
euid)) ||
1540 !uid_eq(mycred->tgcred->session_keyring->
uid, mycred->
euid))
1561 key_ref_put(keyring_r);
1569 unsigned long,
arg4,
unsigned long,
arg5)
1581 (
const void __user *) arg3,
1597 (key_serial_t) arg3);
1601 (key_serial_t) arg3);
1605 (
const char __user *) arg3,
1606 (
const char __user *) arg4,
1607 (key_serial_t)
arg5);
1625 (
const void __user *) arg3,
1627 (key_serial_t) arg5);
1632 (key_serial_t) arg4);
1656 (key_serial_t) arg5);
1660 (key_serial_t) arg2,
1663 (key_serial_t) arg5);