linux-user: Fix unaligned memory access in prlimit64 syscall

target_rlimit64 contains uint64_t fields, so it's 8-byte aligned on
some hosts, while some guests may align their respective type on a
4-byte boundary. This may lead to an unaligned access, which is an UB.

Fix by defining the fields as abi_ullong. This makes the host alignment
match that of the guest, and lets the compiler know that it should emit
code that can deal with the guest alignment.

While at it, also use __get_user() and __put_user() instead of
tswap64().

Fixes: 163a05a839 ("linux-user: Implement prlimit64 syscall")
Reported-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Laurent Vivier <laurent@vivier.eu>
Message-Id: <20230224003907.263914-2-iii@linux.ibm.com>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
(cherry picked from commit 9c1da8b5ee7f6e80e6b683e7fb73df1029a7cbbe)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Ilya Leoshkevich 2023-02-24 01:39:06 +01:00 committed by Michael Tokarev
parent b57641e907
commit b6abbe6250
2 changed files with 6 additions and 6 deletions

View File

@ -12,8 +12,8 @@ struct target_rlimit {
}; };
struct target_rlimit64 { struct target_rlimit64 {
uint64_t rlim_cur; abi_ullong rlim_cur;
uint64_t rlim_max; abi_ullong rlim_max;
}; };
#define TARGET_RLIM_INFINITY ((abi_ulong)-1) #define TARGET_RLIM_INFINITY ((abi_ulong)-1)

View File

@ -12888,8 +12888,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) { if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
return -TARGET_EFAULT; return -TARGET_EFAULT;
} }
rnew.rlim_cur = tswap64(target_rnew->rlim_cur); __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
rnew.rlim_max = tswap64(target_rnew->rlim_max); __get_user(rnew.rlim_max, &target_rnew->rlim_max);
unlock_user_struct(target_rnew, arg3, 0); unlock_user_struct(target_rnew, arg3, 0);
rnewp = &rnew; rnewp = &rnew;
} }
@ -12899,8 +12899,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) { if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
return -TARGET_EFAULT; return -TARGET_EFAULT;
} }
target_rold->rlim_cur = tswap64(rold.rlim_cur); __put_user(rold.rlim_cur, &target_rold->rlim_cur);
target_rold->rlim_max = tswap64(rold.rlim_max); __put_user(rold.rlim_max, &target_rold->rlim_max);
unlock_user_struct(target_rold, arg4, 1); unlock_user_struct(target_rold, arg4, 1);
} }
return ret; return ret;