Skip to content

Commit 3d7d1b0

Browse files
mrutland-armgregkh
authored andcommitted
arm64: fix compat syscall return truncation
commit e30e8d4 upstream. 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]> [Mark: trivial conflict resolution for v5.10.y] Signed-off-by: Mark Rutland <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bb65051 commit 3d7d1b0

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
@@ -316,7 +316,17 @@ static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
316316

317317
static inline unsigned long regs_return_value(struct pt_regs *regs)
318318
{
319-
return regs->regs[0];
319+
unsigned long val = regs->regs[0];
320+
321+
/*
322+
* Audit currently uses regs_return_value() instead of
323+
* syscall_get_return_value(). Apply the same sign-extension here until
324+
* audit is updated to use syscall_get_return_value().
325+
*/
326+
if (compat_user_mode(regs))
327+
val = sign_extend64(val, 31);
328+
329+
return val;
320330
}
321331

322332
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
@@ -1823,7 +1823,7 @@ void syscall_trace_exit(struct pt_regs *regs)
18231823
audit_syscall_exit(regs);
18241824

18251825
if (flags & _TIF_SYSCALL_TRACEPOINT)
1826-
trace_sys_exit(regs, regs_return_value(regs));
1826+
trace_sys_exit(regs, syscall_get_return_value(current, regs));
18271827

18281828
if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
18291829
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
@@ -50,10 +50,7 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno,
5050
ret = do_ni_syscall(regs, scno);
5151
}
5252

53-
if (is_compat_task())
54-
ret = lower_32_bits(ret);
55-
56-
regs->regs[0] = ret;
53+
syscall_set_return_value(current, regs, 0, ret);
5754
}
5855

5956
static inline bool has_syscall_work(unsigned long flags)
@@ -128,7 +125,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
128125
* syscall. do_notify_resume() will send a signal to userspace
129126
* before the syscall is restarted.
130127
*/
131-
regs->regs[0] = -ERESTARTNOINTR;
128+
syscall_set_return_value(current, regs, -ERESTARTNOINTR, 0);
132129
return;
133130
}
134131

@@ -149,7 +146,7 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,
149146
* anyway.
150147
*/
151148
if (scno == NO_SYSCALL)
152-
regs->regs[0] = -ENOSYS;
149+
syscall_set_return_value(current, regs, -ENOSYS, 0);
153150
scno = syscall_trace_enter(regs);
154151
if (scno == NO_SYSCALL)
155152
goto trace_exit;

0 commit comments

Comments
 (0)