Skip to content

Commit e30e8d4

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: fix compat syscall return truncation
Due to inconsistencies in the way we manipulate compat GPRs, we have a few issues today: * For audit and tracing, where error codes are handled as a (native) long, negative error codes are expected to be sign-extended to the native 64-bits, or they may fail to be matched correctly. Thus a syscall which fails with an error may erroneously be identified as failing. * For ptrace, *all* compat return values should be sign-extended for consistency with 32-bit arm, but we currently only do this for negative return codes. * As we may transiently set the upper 32 bits of some compat GPRs while in the kernel, these can be sampled by perf, which is somewhat confusing. This means that where a syscall returns a pointer above 2G, this will be sign-extended, but will not be mistaken for an error as error codes are constrained to the inclusive range [-4096, -1] where no user pointer can exist. To fix all of these, we must consistently use helpers to get/set the compat GPRs, ensuring that we never write the upper 32 bits of the return code, and always sign-extend when reading the return code. This patch does so, with the following changes: * We re-organise syscall_get_return_value() to always sign-extend for compat tasks, and reimplement syscall_get_error() atop. We update syscall_trace_exit() to use syscall_get_return_value(). * We consistently use syscall_set_return_value() to set the return value, ensureing the upper 32 bits are never set unexpectedly. * As the core audit code currently uses regs_return_value() rather than syscall_get_return_value(), we special-case this for compat_user_mode(regs) such that this will do the right thing. Going forward, we should try to move the core audit code over to syscall_get_return_value(). Cc: <[email protected]> Reported-by: He Zhe <[email protected]> Reported-by: weiyuchen <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent d8a7190 commit e30e8d4

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

arch/arm64/include/asm/ptrace.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,17 @@ static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
320320

321321
static inline unsigned long regs_return_value(struct pt_regs *regs)
322322
{
323-
return regs->regs[0];
323+
unsigned long val = regs->regs[0];
324+
325+
/*
326+
* Audit currently uses regs_return_value() instead of
327+
* syscall_get_return_value(). Apply the same sign-extension here until
328+
* audit is updated to use syscall_get_return_value().
329+
*/
330+
if (compat_user_mode(regs))
331+
val = sign_extend64(val, 31);
332+
333+
return val;
324334
}
325335

326336
static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)

arch/arm64/include/asm/syscall.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ static inline void syscall_rollback(struct task_struct *task,
2929
regs->regs[0] = regs->orig_x0;
3030
}
3131

32-
33-
static inline long syscall_get_error(struct task_struct *task,
34-
struct pt_regs *regs)
32+
static inline long syscall_get_return_value(struct task_struct *task,
33+
struct pt_regs *regs)
3534
{
36-
unsigned long error = regs->regs[0];
35+
unsigned long val = regs->regs[0];
3736

3837
if (is_compat_thread(task_thread_info(task)))
39-
error = sign_extend64(error, 31);
38+
val = sign_extend64(val, 31);
4039

41-
return IS_ERR_VALUE(error) ? error : 0;
40+
return val;
4241
}
4342

44-
static inline long syscall_get_return_value(struct task_struct *task,
45-
struct pt_regs *regs)
43+
static inline long syscall_get_error(struct task_struct *task,
44+
struct pt_regs *regs)
4645
{
47-
return regs->regs[0];
46+
unsigned long error = syscall_get_return_value(task, regs);
47+
48+
return IS_ERR_VALUE(error) ? error : 0;
4849
}
4950

5051
static inline void syscall_set_return_value(struct task_struct *task,

arch/arm64/kernel/ptrace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ void syscall_trace_exit(struct pt_regs *regs)
18621862
audit_syscall_exit(regs);
18631863

18641864
if (flags & _TIF_SYSCALL_TRACEPOINT)
1865-
trace_sys_exit(regs, regs_return_value(regs));
1865+
trace_sys_exit(regs, syscall_get_return_value(current, regs));
18661866

18671867
if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
18681868
tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);

arch/arm64/kernel/signal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <asm/unistd.h>
3030
#include <asm/fpsimd.h>
3131
#include <asm/ptrace.h>
32+
#include <asm/syscall.h>
3233
#include <asm/signal32.h>
3334
#include <asm/traps.h>
3435
#include <asm/vdso.h>
@@ -890,7 +891,7 @@ static void do_signal(struct pt_regs *regs)
890891
retval == -ERESTART_RESTARTBLOCK ||
891892
(retval == -ERESTARTSYS &&
892893
!(ksig.ka.sa.sa_flags & SA_RESTART)))) {
893-
regs->regs[0] = -EINTR;
894+
syscall_set_return_value(current, regs, -EINTR, 0);
894895
regs->pc = continue_addr;
895896
}
896897

arch/arm64/kernel/syscall.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
5454
ret = do_ni_syscall(regs, scno);
5555
}
5656

57-
if (is_compat_task())
58-
ret = lower_32_bits(ret);
59-
60-
regs->regs[0] = ret;
57+
syscall_set_return_value(current, regs, 0, ret);
6158

6259
/*
6360
* Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
@@ -115,7 +112,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
115112
* syscall. do_notify_resume() will send a signal to userspace
116113
* before the syscall is restarted.
117114
*/
118-
regs->regs[0] = -ERESTARTNOINTR;
115+
syscall_set_return_value(current, regs, -ERESTARTNOINTR, 0);
119116
return;
120117
}
121118

@@ -136,7 +133,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
136133
* anyway.
137134
*/
138135
if (scno == NO_SYSCALL)
139-
regs->regs[0] = -ENOSYS;
136+
syscall_set_return_value(current, regs, -ENOSYS, 0);
140137
scno = syscall_trace_enter(regs);
141138
if (scno == NO_SYSCALL)
142139
goto trace_exit;

0 commit comments

Comments
 (0)