__sys_recvmmsg() replaces a positive message count with -EFAULT when it cannot copy the remaining timeout back to userspace. The datagrams have already been dequeued and copied to userspace at that point, so the error hides completed receives from the caller. Keep the received count when the optional timeout copyout fails. Tested on x86_64 with QEMU TCG. A read-only timeout mapping receives two datagrams and returns 2; writable, PROT_NONE, empty-queue, NULL-timeout, and pselect controls pass. Link: https://gist.github.com/shaos26/d697e44ff0f9bd801f61f2ce00a2dd51 Fixes: a2e2725541fa ("net: Introduce recvmmsg socket syscall") Signed-off-by: Shawn Shao --- net/socket.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/net/socket.c b/net/socket.c index 08658a9b8065d..02b7e813c902c 100644 --- a/net/socket.c +++ b/net/socket.c @@ -3151,11 +3151,11 @@ int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, if (datagrams <= 0) return datagrams; - if (timeout && put_timespec64(&timeout_sys, timeout)) - datagrams = -EFAULT; - - if (timeout32 && put_old_timespec32(&timeout_sys, timeout32)) - datagrams = -EFAULT; + /* The datagrams are already consumed; preserve their count on copyout. */ + if (timeout) + put_timespec64(&timeout_sys, timeout); + if (timeout32) + put_old_timespec32(&timeout_sys, timeout32); return datagrams; } -- 2.34.1