Skip to content

Commit 8ad2348

Browse files
compudjtorvalds
authored andcommitted
Fix: compat_rw_copy_check_uvector() misuse in aio, readv, writev, and security keys
Looking at mm/process_vm_access.c:process_vm_rw() and comparing it to compat_process_vm_rw() shows that the compatibility code requires an explicit "access_ok()" check before calling compat_rw_copy_check_uvector(). The same difference seems to appear when we compare fs/read_write.c:do_readv_writev() to fs/compat.c:compat_do_readv_writev(). This subtle difference between the compat and non-compat requirements should probably be debated, as it seems to be error-prone. In fact, there are two others sites that use this function in the Linux kernel, and they both seem to get it wrong: Now shifting our attention to fs/aio.c, we see that aio_setup_iocb() also ends up calling compat_rw_copy_check_uvector() through aio_setup_vectored_rw(). Unfortunately, the access_ok() check appears to be missing. Same situation for security/keys/compat.c:compat_keyctl_instantiate_key_iov(). I propose that we add the access_ok() check directly into compat_rw_copy_check_uvector(), so callers don't have to worry about it, and it therefore makes the compat call code similar to its non-compat counterpart. Place the access_ok() check in the same location where copy_from_user() can trigger a -EFAULT error in the non-compat code, so the ABI behaviors are alike on both compat and non-compat. While we are here, fix compat_do_readv_writev() so it checks for compat_rw_copy_check_uvector() negative return values. And also, fix a memory leak in compat_keyctl_instantiate_key_iov() error handling. Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Acked-by: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0f9bf26 commit 8ad2348

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

fs/compat.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ ssize_t compat_rw_copy_check_uvector(int type,
558558
}
559559
*ret_pointer = iov;
560560

561+
ret = -EFAULT;
562+
if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
563+
goto out;
564+
561565
/*
562566
* Single unix specification:
563567
* We should -EINVAL if an element length is not >= 0 and fitting an
@@ -1080,17 +1084,12 @@ static ssize_t compat_do_readv_writev(int type, struct file *file,
10801084
if (!file->f_op)
10811085
goto out;
10821086

1083-
ret = -EFAULT;
1084-
if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1085-
goto out;
1086-
1087-
tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1087+
ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
10881088
UIO_FASTIOV, iovstack, &iov);
1089-
if (tot_len == 0) {
1090-
ret = 0;
1089+
if (ret <= 0)
10911090
goto out;
1092-
}
10931091

1092+
tot_len = ret;
10941093
ret = rw_verify_area(type, file, pos, tot_len);
10951094
if (ret < 0)
10961095
goto out;

mm/process_vm_access.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,6 @@ compat_process_vm_rw(compat_pid_t pid,
429429
if (flags != 0)
430430
return -EINVAL;
431431

432-
if (!access_ok(VERIFY_READ, lvec, liovcnt * sizeof(*lvec)))
433-
goto out;
434-
435-
if (!access_ok(VERIFY_READ, rvec, riovcnt * sizeof(*rvec)))
436-
goto out;
437-
438432
if (vm_write)
439433
rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
440434
UIO_FASTIOV, iovstack_l,
@@ -459,8 +453,6 @@ compat_process_vm_rw(compat_pid_t pid,
459453
kfree(iov_r);
460454
if (iov_l != iovstack_l)
461455
kfree(iov_l);
462-
463-
out:
464456
return rc;
465457
}
466458

security/keys/compat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ static long compat_keyctl_instantiate_key_iov(
4040
ARRAY_SIZE(iovstack),
4141
iovstack, &iov);
4242
if (ret < 0)
43-
return ret;
43+
goto err;
4444
if (ret == 0)
4545
goto no_payload_free;
4646

4747
ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
48-
48+
err:
4949
if (iov != iovstack)
5050
kfree(iov);
5151
return ret;

0 commit comments

Comments
 (0)