Commit 2bf4854e79 for qemu.org

commit 2bf4854e7924883223fccfa139dbc87427c7f758
Author: Brian Cain <brian.cain@oss.qualcomm.com>
Date:   Wed Nov 18 09:49:33 2026 -0800

    linux-user: copy nanosleep's remaining time on EINTR

    do_syscall1() copies the host's "rem" timespec back to the guest
    whenever safe_nanosleep() fails.  But the kernel only fills in rem when
    the sleep was interrupted (-EINTR).  When a signal arrives just before
    the host call, safe_syscall returns -QEMU_ERESTARTSYS without running
    the syscall at all, so rem is uninitialized stack.

    That is harmless for a guest that passes distinct request and remainder
    buffers, but musl's usleep() calls nanosleep(&ts, &ts).  The garbage
    then overwrites the guest's request before the syscall is restarted, so
    the restarted sleep can last arbitrarily long.

    Match clock_nanosleep(), which already only copies the remainder back
    on -TARGET_EINTR.

    Cc: qemu-stable@nongnu.org
    Fixes: 9e518226f431 ("linux-user: Use safe_syscall wrapper for sleep syscalls")
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
    Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 2fc61b3eab..350e69e2d0 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -12249,7 +12249,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
                 return -TARGET_EFAULT;
             }
             ret = get_errno(safe_nanosleep(&req, &rem));
-            if (is_error(ret) && arg2) {
+            if (ret == -TARGET_EINTR && arg2) {
                 if (host_to_target_timespec(arg2, &rem)) {
                     return -TARGET_EFAULT;
                 }