Skip to content

Commit b353a1f

Browse files
author
Al Viro
committed
switch keyctl_instantiate_key_common() to iov_iter
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 0504c07 commit b353a1f

3 files changed

Lines changed: 40 additions & 72 deletions

File tree

‎security/keys/compat.c‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,21 @@ static long compat_keyctl_instantiate_key_iov(
3131
key_serial_t ringid)
3232
{
3333
struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
34+
struct iov_iter from;
3435
long ret;
3536

36-
if (!_payload_iov || !ioc)
37-
goto no_payload;
37+
if (!_payload_iov)
38+
ioc = 0;
3839

39-
ret = compat_rw_copy_check_uvector(WRITE, _payload_iov, ioc,
40-
ARRAY_SIZE(iovstack),
41-
iovstack, &iov);
40+
ret = compat_import_iovec(WRITE, _payload_iov, ioc,
41+
ARRAY_SIZE(iovstack), &iov,
42+
&from);
4243
if (ret < 0)
43-
goto err;
44-
if (ret == 0)
45-
goto no_payload_free;
46-
47-
ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
48-
err:
49-
if (iov != iovstack)
50-
kfree(iov);
51-
return ret;
44+
return ret;
5245

53-
no_payload_free:
54-
if (iov != iovstack)
55-
kfree(iov);
56-
no_payload:
57-
return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
46+
ret = keyctl_instantiate_key_common(id, &from, ringid);
47+
kfree(iov);
48+
return ret;
5849
}
5950

6051
/*

‎security/keys/internal.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,10 @@ extern long keyctl_instantiate_key_iov(key_serial_t,
243243
unsigned, key_serial_t);
244244
extern long keyctl_invalidate_key(key_serial_t);
245245

246+
struct iov_iter;
246247
extern long keyctl_instantiate_key_common(key_serial_t,
247-
const struct iovec *,
248-
unsigned, size_t, key_serial_t);
248+
struct iov_iter *,
249+
key_serial_t);
249250
#ifdef CONFIG_PERSISTENT_KEYRINGS
250251
extern long keyctl_get_persistent(uid_t, key_serial_t);
251252
extern unsigned persistent_keyring_expiry;

‎security/keys/keyctl.c‎

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -997,21 +997,6 @@ static int keyctl_change_reqkey_auth(struct key *key)
997997
return commit_creds(new);
998998
}
999999

1000-
/*
1001-
* Copy the iovec data from userspace
1002-
*/
1003-
static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
1004-
unsigned ioc)
1005-
{
1006-
for (; ioc > 0; ioc--) {
1007-
if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1008-
return -EFAULT;
1009-
buffer += iov->iov_len;
1010-
iov++;
1011-
}
1012-
return 0;
1013-
}
1014-
10151000
/*
10161001
* Instantiate a key with the specified payload and link the key into the
10171002
* destination keyring if one is given.
@@ -1022,20 +1007,21 @@ static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
10221007
* If successful, 0 will be returned.
10231008
*/
10241009
long keyctl_instantiate_key_common(key_serial_t id,
1025-
const struct iovec *payload_iov,
1026-
unsigned ioc,
1027-
size_t plen,
1010+
struct iov_iter *from,
10281011
key_serial_t ringid)
10291012
{
10301013
const struct cred *cred = current_cred();
10311014
struct request_key_auth *rka;
10321015
struct key *instkey, *dest_keyring;
1016+
size_t plen = from ? iov_iter_count(from) : 0;
10331017
void *payload;
10341018
long ret;
1035-
bool vm = false;
10361019

10371020
kenter("%d,,%zu,%d", id, plen, ringid);
10381021

1022+
if (!plen)
1023+
from = NULL;
1024+
10391025
ret = -EINVAL;
10401026
if (plen > 1024 * 1024 - 1)
10411027
goto error;
@@ -1054,20 +1040,19 @@ long keyctl_instantiate_key_common(key_serial_t id,
10541040
/* pull the payload in if one was supplied */
10551041
payload = NULL;
10561042

1057-
if (payload_iov) {
1043+
if (from) {
10581044
ret = -ENOMEM;
10591045
payload = kmalloc(plen, GFP_KERNEL);
10601046
if (!payload) {
10611047
if (plen <= PAGE_SIZE)
10621048
goto error;
1063-
vm = true;
10641049
payload = vmalloc(plen);
10651050
if (!payload)
10661051
goto error;
10671052
}
10681053

1069-
ret = copy_from_user_iovec(payload, payload_iov, ioc);
1070-
if (ret < 0)
1054+
ret = -EFAULT;
1055+
if (copy_from_iter(payload, plen, from) != plen)
10711056
goto error2;
10721057
}
10731058

@@ -1089,10 +1074,7 @@ long keyctl_instantiate_key_common(key_serial_t id,
10891074
keyctl_change_reqkey_auth(NULL);
10901075

10911076
error2:
1092-
if (!vm)
1093-
kfree(payload);
1094-
else
1095-
vfree(payload);
1077+
kvfree(payload);
10961078
error:
10971079
return ret;
10981080
}
@@ -1112,15 +1094,19 @@ long keyctl_instantiate_key(key_serial_t id,
11121094
key_serial_t ringid)
11131095
{
11141096
if (_payload && plen) {
1115-
struct iovec iov[1] = {
1116-
[0].iov_base = (void __user *)_payload,
1117-
[0].iov_len = plen
1118-
};
1097+
struct iovec iov;
1098+
struct iov_iter from;
1099+
int ret;
11191100

1120-
return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1101+
ret = import_single_range(WRITE, (void __user *)_payload, plen,
1102+
&iov, &from);
1103+
if (unlikely(ret))
1104+
return ret;
1105+
1106+
return keyctl_instantiate_key_common(id, &from, ringid);
11211107
}
11221108

1123-
return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1109+
return keyctl_instantiate_key_common(id, NULL, ringid);
11241110
}
11251111

11261112
/*
@@ -1138,29 +1124,19 @@ long keyctl_instantiate_key_iov(key_serial_t id,
11381124
key_serial_t ringid)
11391125
{
11401126
struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1127+
struct iov_iter from;
11411128
long ret;
11421129

1143-
if (!_payload_iov || !ioc)
1144-
goto no_payload;
1130+
if (!_payload_iov)
1131+
ioc = 0;
11451132

1146-
ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1147-
ARRAY_SIZE(iovstack), iovstack, &iov);
1133+
ret = import_iovec(WRITE, _payload_iov, ioc,
1134+
ARRAY_SIZE(iovstack), &iov, &from);
11481135
if (ret < 0)
1149-
goto err;
1150-
if (ret == 0)
1151-
goto no_payload_free;
1152-
1153-
ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1154-
err:
1155-
if (iov != iovstack)
1156-
kfree(iov);
1136+
return ret;
1137+
ret = keyctl_instantiate_key_common(id, &from, ringid);
1138+
kfree(iov);
11571139
return ret;
1158-
1159-
no_payload_free:
1160-
if (iov != iovstack)
1161-
kfree(iov);
1162-
no_payload:
1163-
return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
11641140
}
11651141

11661142
/*

0 commit comments

Comments
 (0)