Skip to contentSkip to conversation

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

PATCH In review Brian Cain 2 participants 2 messages Nov 18

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

PATCH 1/1 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.

Fixes 9e518226f431 ("linux-user: Use safe_syscall wrapper for sleep syscalls")
---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index 2fc61b3eabc..350e69e2d00 100644
@@ -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;
                 }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Brian Cain

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.

Quoted 6 lines
Fixes 9e518226f431 ("linux-user: Use safe_syscall wrapper for sleep syscalls")
---
  linux-user/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~