Skip to content

linux-user: validate the fd for a zero-length readv/writev

TAKANO Mitsuhiro qemu-devel 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest2 patches 1 replyIn review

Changes in v2 from the cover letter

- return a shared empty vector instead of allocating one per call
- explain in the comment why errno cannot be set here
- cover recvmsg(), which had the same bug, in the test

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2linux-user: validate the fd for a zero-length readv/writevlinux-user/syscall.c+7 -21 reply
1/2linux-user: validate the fd for a zero-length readv/writevlinux-user/syscall.c+17 -30 replies
2/2tests/tcg: check that a zero-length readv/writev validates the fdtests/tcg/multiarch/linux/linux-zero-len-iovec.c tests/tcg/multiarch/meson.build+42 -00 replies
2/2tests/tcg: check that a zero-length readv/writev validates the fdtests/tcg/multiarch/linux/linux-zero-len-iovec.c tests/tcg/multiarch/meson.build+57 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/2] linux-user: validate the fd for a zero-length readv/writev

Hi Alex,

thanks for the review.

Quoted 3 lines

I can't imagine why we would want to leak an iovec structure every time this is called. All the callers seem to have handling for NULL. Why are we not just setting the right errno?

It is not leaked: every caller hands the vector to unlock_iovec(), which g_free()s it, and with count == 0 the loop over the target vector does not run. But you are right that there is no reason to allocate at all, so v2 returns a shared, empty vector instead. It is never dereferenced, and unlock_iovec() now skips the free.

Setting errno in lock_iovec() does not work, though: the answer depends on the descriptor, which lock_iovec() does not have. readv(fd, NULL, 0) is a successful no-op on a usable descriptor and EBADF on a wrong one, so the syscall has to be issued for the host to answer.

Since "the kernel does X" is a strong claim, I checked both halves.

Kernel side, in this tree:

- fs/read_write.c: vfs_readv()/vfs_writev() return -EBADF from the FMODE_READ/FMODE_WRITE test before import_iovec() is reached, so an empty vector still validates the descriptor.
- net/socket.c: __sys_recvmsg() returns -EBADF from the fd lookup before the msghdr is parsed, so recvmsg() with iovlen 0 is affected too. do_sendrecvmsg() turned that zero count into a successful return for !send, which is why v2 covers recvmsg() in the test; sendmsg() already issued the syscall through the "allow sending packet without any iov" path.

Behaviour side. I wrote a harness that calls readv, writev, preadv, pwritev, preadv2, pwritev2, vmsplice, sendmsg and recvmsg with an empty vector, plus readv/writev with a bogus vector pointer, on eleven fd states (/dev/null opened O_RDONLY, O_WRONLY, O_RDWR and O_PATH, a directory, both ends of a pipe, both ends of a datagram socketpair, one end of a stream socketpair, and a closed fd). That is 132 lines of output per run.

- v2 matches the native kernel run line for line on aarch64, arm, i386,

    ppc (32-bit BE), s390x (64-bit BE) and x86_64.

- unpatched 10.2.1 differs from the kernel in 64 of those 132 lines.
- the only mismatch the harness still finds is readv(fd, NULL, -1), where qemu returns EINVAL and the kernel returns EBADF for a bad fd. That is the pre-existing "count > IOV_MAX is rejected before the fd is looked at" behaviour, which this series does not touch; doing it properly needs the fd in the caller, so it would be a separate change.

The regression comes from f287b2c2d4: before that commit lock_iovec() succeeded for count == 0 (page_check_range() returns 0 for a zero length) and the syscall was issued. Hence the Fixes: tag.

Tests:
- tests/tcg with the new test in the suite: aarch64 106/106, i386 78/78,

    arm 64/64, s390x 135/135, x86_64 73/73.

- ppc-linux-user has no tests/tcg directory upstream, so I built the same test by hand with powerpc-linux-gnu-gcc: it passes with the series and aborts on 10.2.1.
- the new test compiles warning-free with -Wall -Wextra -Werror for all six cross compilers.

Because the vector is shared now, I also checked memory and concurrency:
eight threads issuing 4.8 million empty readv/writev calls against valid and wrong-mode descriptors finished with no failures, and qemu under valgrind reports 0 errors and 0 bytes definitely lost.

Changes in v2:
- return a shared empty vector instead of allocating one per call
- explain in the comment why errno cannot be set here
- cover recvmsg(), which had the same bug, in the test

If you think this corner is not worth carrying, say so and I will drop it
- the divergence is real but small.

Thanks,
Takano

TAKANO Mitsuhiro (2):
linux-user: validate the fd for a zero-length readv/writev tests/tcg: check that a zero-length readv/writev validates the fd

 linux-user/syscall.c                          | 20 ++++++-
 .../multiarch/linux/linux-zero-len-iovec.c    | 56 +++++++++++++++++++
 tests/tcg/multiarch/meson.build               |  1 +
 3 files changed, 74 insertions(+), 3 deletions(-)
 create mode 100644 tests/tcg/multiarch/linux/linux-zero-len-iovec.c
Signature · 1 line
2.53.0