[PATCH] arm64: fix compat syscall return truncation

Catalin Marinas catalin.marinas at arm.com
Mon Aug 2 09:54:23 PDT 2021


On Mon, Aug 02, 2021 at 11:42:00AM +0100, Mark Rutland wrote:
> 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 exists.
> 
> 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().
> 
> Signed-off-by: Mark Rutland <mark.rutland at arm.com>
> Reported-by: He Zhe <zhe.he at windriver.com>
> Reported-by: weiyuchen <weiyuchen3 at huawei.com>
> Cc: Catalin Marinas <catalin.marinas at arm.com>
> Cc: Will Deacon <will at kernel.org>

Reviewed-by: Catalin Marinas <catalin.marinas at arm.com>

(to the best of my knowledge, I can't guarantee all cases have been
covered)



More information about the linux-arm-kernel mailing list