mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
entry, treewide: Make syscall_enter_from_user_mode[_work]() indicate syscall execution
The return values of syscall_enter_from_user_mode[_work]() are
non-intuitive. Both functions return the syscall number which should be
invoked by the architecture specific syscall entry code. The returned
number can be:
- the unmodified syscall number which was handed in by the caller
- a modified syscall number (ptrace, seccomp, trace/probe/bpf)
That has an additional twist. If the return value is -1L then the caller is
not allowed to modify the return value as that indicates that the modifying
entity requests to abort the syscall and set the return value already. That
can obviously not be differentiated from a syscall which handed in -1 as
syscall number.
The most trivial way to deal with that is:
set_return_value(regs, -ENOSYS);
nr = syscall_enter_from_user_mode(regs, nr);
if (valid(nr))
handle_syscall(regs, nr);
That's what LOONGARCH, RISCV, and X86 do. But PowerPC and S390 do not
preset the return value, so when user space hands in -1 and there is
nothing setting the return value in the entry work code, then the syscall
is skipped but the return value is whatever random data has been in the
return value register.
Change the return values of syscall_enter_from_user_mode[_work]() to
boolean and return false, when either ptrace or seccomp request to skip the
syscall. If they return true, update the syscall number as it might have
been changed.
That results in slightly different behaviour of the architectures versus
tracing.
If the syscall tracepoint has probe/BPF attached, those might set the
syscall number to -1 and also set the return value. PowerPC and S390 will
then overwrite that value with -ENOSYS. The other architectures will just
ignore it like any other invalid syscall and use the modified one.
Originally-by: Michal Suchánek <msuchanek@suse.de>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Tested-by: Michal Suchánek <msuchanek@suse.de>
Link: https://patch.msgid.link/20260712141346.772209074@kernel.org
This commit is contained in:
@@ -58,26 +58,51 @@ state transitions must run with interrupts disabled.
|
|||||||
Syscalls
|
Syscalls
|
||||||
--------
|
--------
|
||||||
|
|
||||||
Syscall-entry code starts in assembly code and calls out into low-level C code
|
Syscall-entry code starts in assembly code and calls out into low-level C
|
||||||
after establishing low-level architecture-specific state and stack frames. This
|
code after establishing low-level architecture-specific state and stack
|
||||||
low-level C code must not be instrumented. A typical syscall handling function
|
frames. This low-level C code must not be instrumented. The recommended
|
||||||
invoked from low-level assembly code looks like this:
|
syscall handling function invoked from low-level assembly code looks like
|
||||||
|
this:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
noinstr void syscall(struct pt_regs *regs, int nr)
|
noinstr void syscall(struct pt_regs *regs, long nr)
|
||||||
{
|
{
|
||||||
arch_syscall_enter(regs);
|
arch_syscall_enter(regs);
|
||||||
nr = syscall_enter_from_user_mode_randomize_stack(regs, nr);
|
result_reg(regs) = -ENOSYS;
|
||||||
|
if (syscall_enter_from_user_mode_randomize_stack(regs, &nr)) {
|
||||||
instrumentation_begin();
|
instrumentation_begin();
|
||||||
if (!invoke_syscall(regs, nr) && nr != -1)
|
if (valid(nr)
|
||||||
result_reg(regs) = __sys_ni_syscall(regs);
|
result_reg(regs) = invoke_syscall(regs, nr);
|
||||||
instrumentation_end();
|
instrumentation_end();
|
||||||
|
}
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
This is the most resilent variant as it has always a guaranteed valid
|
||||||
|
return code. The alternative variant is:
|
||||||
|
|
||||||
|
.. code-block:: c
|
||||||
|
|
||||||
|
noinstr void syscall(struct pt_regs *regs, long nr)
|
||||||
|
{
|
||||||
|
arch_syscall_enter(regs);
|
||||||
|
if (syscall_enter_from_user_mode_randomize_stack(regs, &nr)) {
|
||||||
|
instrumentation_begin();
|
||||||
|
if (valid(nr)
|
||||||
|
result_reg(regs) = invoke_syscall(regs, nr);
|
||||||
|
else
|
||||||
|
result_reg(regs) = -ENOSYS;
|
||||||
|
instrumentation_end();
|
||||||
|
}
|
||||||
|
syscall_exit_to_user_mode(regs);
|
||||||
|
}
|
||||||
|
|
||||||
|
That works for most situations except when a probe/BPF attached to the
|
||||||
|
syscall tracepoint sets an invalid syscall number e.g. -1 and also modifies
|
||||||
|
the result register. So this variant will obviously overwrite the modified
|
||||||
|
result with -ENOSYS.
|
||||||
|
|
||||||
syscall_enter_from_user_mode_randomize_stack() first invokes
|
syscall_enter_from_user_mode_randomize_stack() first invokes
|
||||||
enter_from_user_mode_randomize_stack() which establishes state in the
|
enter_from_user_mode_randomize_stack() which establishes state in the
|
||||||
following order:
|
following order:
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ typedef long (*sys_call_fn)(unsigned long, unsigned long,
|
|||||||
|
|
||||||
void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
|
void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
|
||||||
{
|
{
|
||||||
unsigned long nr;
|
|
||||||
sys_call_fn syscall_fn;
|
sys_call_fn syscall_fn;
|
||||||
|
unsigned long nr;
|
||||||
|
|
||||||
nr = regs->regs[11];
|
nr = regs->regs[11];
|
||||||
/* Set for syscall restarting */
|
/* Set for syscall restarting */
|
||||||
@@ -69,12 +69,12 @@ void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
|
|||||||
regs->orig_a0 = regs->regs[4];
|
regs->orig_a0 = regs->regs[4];
|
||||||
regs->regs[4] = -ENOSYS;
|
regs->regs[4] = -ENOSYS;
|
||||||
|
|
||||||
nr = syscall_enter_from_user_mode_randomize_stack(regs, nr);
|
if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
|
||||||
|
if (nr < NR_syscalls) {
|
||||||
if (nr < NR_syscalls) {
|
syscall_fn = sys_call_table[array_index_nospec(nr, NR_syscalls)];
|
||||||
syscall_fn = sys_call_table[array_index_nospec(nr, NR_syscalls)];
|
regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
|
||||||
regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
|
regs->regs[7], regs->regs[8], regs->regs[9]);
|
||||||
regs->regs[7], regs->regs[8], regs->regs[9]);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
|
|||||||
long ret;
|
long ret;
|
||||||
syscall_fn f;
|
syscall_fn f;
|
||||||
|
|
||||||
r0 = syscall_enter_from_user_mode_randomize_stack(regs, r0);
|
if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0)))
|
||||||
|
return syscall_get_error(current, regs);
|
||||||
|
|
||||||
if (unlikely(r0 >= NR_syscalls)) {
|
if (unlikely(r0 >= NR_syscalls)) {
|
||||||
if (unlikely(trap_is_unsupported_scv(regs))) {
|
if (unlikely(trap_is_unsupported_scv(regs))) {
|
||||||
|
|||||||
@@ -332,13 +332,12 @@ void do_trap_ecall_u(struct pt_regs *regs)
|
|||||||
|
|
||||||
riscv_v_vstate_discard(regs);
|
riscv_v_vstate_discard(regs);
|
||||||
|
|
||||||
syscall = syscall_enter_from_user_mode_randomize_stack(regs, syscall);
|
if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &syscall))) {
|
||||||
|
if (syscall >= 0 && syscall < NR_syscalls) {
|
||||||
if (syscall >= 0 && syscall < NR_syscalls) {
|
syscall = array_index_nospec(syscall, NR_syscalls);
|
||||||
syscall = array_index_nospec(syscall, NR_syscalls);
|
syscall_handler(regs, syscall);
|
||||||
syscall_handler(regs, syscall);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
} else {
|
} else {
|
||||||
irqentry_state_t state = irqentry_nmi_enter(regs);
|
irqentry_state_t state = irqentry_nmi_enter(regs);
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ SYSCALL_DEFINE0(ni_syscall)
|
|||||||
void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
|
void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
|
||||||
{
|
{
|
||||||
unsigned long nr;
|
unsigned long nr;
|
||||||
|
bool permit;
|
||||||
|
|
||||||
enter_from_user_mode_randomize_stack(regs);
|
enter_from_user_mode_randomize_stack(regs);
|
||||||
|
|
||||||
@@ -121,7 +122,9 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
|
|||||||
regs->psw.addr = current->restart_block.arch_data;
|
regs->psw.addr = current->restart_block.arch_data;
|
||||||
current->restart_block.arch_data = 1;
|
current->restart_block.arch_data = 1;
|
||||||
}
|
}
|
||||||
nr = syscall_enter_from_user_mode_work(regs, nr);
|
|
||||||
|
permit = syscall_enter_from_user_mode_work(regs, &nr);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In the s390 ptrace ABI, both the syscall number and the return value
|
* In the s390 ptrace ABI, both the syscall number and the return value
|
||||||
* use gpr2. However, userspace puts the syscall number either in the
|
* use gpr2. However, userspace puts the syscall number either in the
|
||||||
@@ -129,7 +132,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
|
|||||||
* work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
|
* work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
|
||||||
* and if set, the syscall will be skipped.
|
* and if set, the syscall will be skipped.
|
||||||
*/
|
*/
|
||||||
if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)))
|
if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET) || !permit))
|
||||||
goto out;
|
goto out;
|
||||||
regs->gprs[2] = -ENOSYS;
|
regs->gprs[2] = -ENOSYS;
|
||||||
if (likely(nr < NR_syscalls)) {
|
if (likely(nr < NR_syscalls)) {
|
||||||
|
|||||||
+12
-13
@@ -161,8 +161,9 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
|
|||||||
nr = syscall_32_enter(regs);
|
nr = syscall_32_enter(regs);
|
||||||
|
|
||||||
local_irq_enable();
|
local_irq_enable();
|
||||||
nr = syscall_enter_from_user_mode_work(regs, nr);
|
|
||||||
do_syscall_32_irqs_on(regs, nr);
|
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
|
||||||
|
do_syscall_32_irqs_on(regs, nr);
|
||||||
|
|
||||||
instrumentation_end();
|
instrumentation_end();
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
@@ -223,8 +224,8 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
|
|||||||
nr = syscall_32_enter(regs);
|
nr = syscall_32_enter(regs);
|
||||||
|
|
||||||
local_irq_enable();
|
local_irq_enable();
|
||||||
nr = syscall_enter_from_user_mode_work(regs, nr);
|
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
|
||||||
do_syscall_32_irqs_on(regs, nr);
|
do_syscall_32_irqs_on(regs, nr);
|
||||||
|
|
||||||
instrumentation_end();
|
instrumentation_end();
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
@@ -243,13 +244,13 @@ __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
|
|||||||
* orig_ax, the int return value truncates it. This matches
|
* orig_ax, the int return value truncates it. This matches
|
||||||
* the semantics of syscall_get_nr().
|
* the semantics of syscall_get_nr().
|
||||||
*/
|
*/
|
||||||
nr = syscall_enter_from_user_mode_randomize_stack(regs, nr);
|
if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
|
||||||
|
instrumentation_begin();
|
||||||
|
|
||||||
instrumentation_begin();
|
do_syscall_32_irqs_on(regs, nr);
|
||||||
|
|
||||||
do_syscall_32_irqs_on(regs, nr);
|
instrumentation_end();
|
||||||
|
}
|
||||||
instrumentation_end();
|
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
}
|
}
|
||||||
#endif /* !CONFIG_IA32_EMULATION */
|
#endif /* !CONFIG_IA32_EMULATION */
|
||||||
@@ -286,10 +287,8 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
nr = syscall_enter_from_user_mode_work(regs, nr);
|
if (likely(syscall_enter_from_user_mode_work(regs, &nr)))
|
||||||
|
do_syscall_32_irqs_on(regs, nr);
|
||||||
/* Now this is just like a normal syscall. */
|
|
||||||
do_syscall_32_irqs_on(regs, nr);
|
|
||||||
|
|
||||||
instrumentation_end();
|
instrumentation_end();
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
|
|||||||
@@ -78,14 +78,14 @@ static __always_inline void do_syscall_x32(struct pt_regs *regs, unsigned long n
|
|||||||
/* Returns true to return using SYSRET, or false to use IRET */
|
/* Returns true to return using SYSRET, or false to use IRET */
|
||||||
__visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)
|
__visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)
|
||||||
{
|
{
|
||||||
nr = syscall_enter_from_user_mode_randomize_stack(regs, nr);
|
if (likely(syscall_enter_from_user_mode_randomize_stack(regs, &nr))) {
|
||||||
|
instrumentation_begin();
|
||||||
|
|
||||||
instrumentation_begin();
|
if (!do_syscall_x64(regs, nr))
|
||||||
|
do_syscall_x32(regs, nr);
|
||||||
|
|
||||||
if (!do_syscall_x64(regs, nr))
|
instrumentation_end();
|
||||||
do_syscall_x32(regs, nr);
|
}
|
||||||
|
|
||||||
instrumentation_end();
|
|
||||||
syscall_exit_to_user_mode(regs);
|
syscall_exit_to_user_mode(regs);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -114,16 +114,15 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
|
|||||||
* @regs: Pointer to currents pt_regs
|
* @regs: Pointer to currents pt_regs
|
||||||
* @syscall: The syscall number
|
* @syscall: The syscall number
|
||||||
*
|
*
|
||||||
* Invoked from architecture specific syscall entry code with interrupts
|
* Invoked from architecture specific syscall entry code with interrupts enabled
|
||||||
* enabled after invoking enter_from_user_mode(), enabling interrupts and
|
* after invoking enter_from_user_mode(), enabling interrupts and extra
|
||||||
* extra architecture specific work.
|
* architecture specific work with the syscall return value preset to -ENOSYS.
|
||||||
*
|
*
|
||||||
* Returns: The original or a modified syscall number
|
* Returns: True if the syscall should be invoked, False otherwise.
|
||||||
*
|
*
|
||||||
* If the returned syscall number is -1 then the syscall should be
|
* If the return value is false, the caller must skip the syscall and leave the
|
||||||
* skipped. In this case the caller may invoke syscall_set_error() or
|
* syscall return value unmodified as it might have been set by one of the entry
|
||||||
* syscall_set_return_value() first. If neither of those are called and -1
|
* work functions.
|
||||||
* is returned, then the syscall will fail with ENOSYS.
|
|
||||||
*
|
*
|
||||||
* It handles the following work items:
|
* It handles the following work items:
|
||||||
*
|
*
|
||||||
@@ -131,19 +130,20 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
|
|||||||
* ptrace_report_syscall_permit_entry(), __seccomp_permit_syscall(), trace_sys_enter()
|
* ptrace_report_syscall_permit_entry(), __seccomp_permit_syscall(), trace_sys_enter()
|
||||||
* 2) Invocation of audit_syscall_entry()
|
* 2) Invocation of audit_syscall_entry()
|
||||||
*/
|
*/
|
||||||
static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
|
static __always_inline bool syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
|
||||||
{
|
{
|
||||||
unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
|
unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
|
||||||
|
|
||||||
if (work & SYSCALL_WORK_ENTER) {
|
if (!(work & SYSCALL_WORK_ENTER))
|
||||||
if (!syscall_trace_enter(regs, work, syscall))
|
return true;
|
||||||
return -1L;
|
|
||||||
|
|
||||||
/* Reread the syscall number as it might have been modified */
|
if (unlikely(!syscall_trace_enter(regs, work, *syscall)))
|
||||||
syscall = syscall_get_nr(current, regs);
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return syscall;
|
/* Reread the syscall number as it might have been modified */
|
||||||
|
*syscall = syscall_get_nr(current, regs);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user