linux-user: copy nanosleep's remaining time on EINTR
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.
--- linux-user/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
@@ -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
Quoted 14 lines · Brian Cain
do_syscall1()copies the host's "rem" timespec back to the guest wheneversafe_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_syscallreturns -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()callsnanosleep(&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")Signed-off-by Brian Cain<[email protected]>--- linux-user/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~