Skip to content

Commit

Permalink
arm64: expose FAR_EL1 tag bits in siginfo
Browse files Browse the repository at this point in the history
The kernel currently clears the tag bits (i.e. bits 56-63) in the fault
address exposed via siginfo.si_addr and sigcontext.fault_address. However,
the tag bits may be needed by tools in order to accurately diagnose
memory errors, such as HWASan [1] or future tools based on the Memory
Tagging Extension (MTE).

Expose these bits via the arch_untagged_si_addr mechanism, so that
they are only exposed to signal handlers with the SA_EXPOSE_TAGBITS
flag set.

[1] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html

Signed-off-by: Peter Collingbourne <pcc@google.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://linux-review.googlesource.com/id/Ia8876bad8c798e0a32df7c2ce1256c4771c81446
Link: https://lore.kernel.org/r/0010296597784267472fa13b39f8238d87a72cf8.1605904350.git.pcc@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
  • Loading branch information
Peter Collingbourne authored and Catalin Marinas committed Nov 23, 2020
1 parent 6ac05e8 commit dceec3f
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 71 deletions.
25 changes: 19 additions & 6 deletions Documentation/arm64/tagged-pointers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,25 @@ visibility.
Preserving tags
---------------

Non-zero tags are not preserved when delivering signals. This means that
signal handlers in applications making use of tags cannot rely on the
tag information for user virtual addresses being maintained for fields
inside siginfo_t. One exception to this rule is for signals raised in
response to watchpoint debug exceptions, where the tag information will
be preserved.
When delivering signals, non-zero tags are not preserved in
siginfo.si_addr unless the flag SA_EXPOSE_TAGBITS was set in
sigaction.sa_flags when the signal handler was installed. This means
that signal handlers in applications making use of tags cannot rely
on the tag information for user virtual addresses being maintained
in these fields unless the flag was set.

Due to architecture limitations, bits 63:60 of the fault address
are not preserved in response to synchronous tag check faults
(SEGV_MTESERR) even if SA_EXPOSE_TAGBITS was set. Applications should
treat the values of these bits as undefined in order to accommodate
future architecture revisions which may preserve the bits.

For signals raised in response to watchpoint debug exceptions, the
tag information will be preserved regardless of the SA_EXPOSE_TAGBITS
flag setting.

Non-zero tags are never preserved in sigcontext.fault_address
regardless of the SA_EXPOSE_TAGBITS flag setting.

The architecture prevents the use of a tagged PC, so the upper byte will
be set to a sign-extension of bit 55 on exception return.
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/include/asm/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static inline u32 disr_to_esr(u64 disr)
}

asmlinkage void enter_from_user_mode(void);
void do_mem_abort(unsigned long addr, unsigned int esr, struct pt_regs *regs);
void do_mem_abort(unsigned long far, unsigned int esr, struct pt_regs *regs);
void do_undefinstr(struct pt_regs *regs);
void do_bti(struct pt_regs *regs);
asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr);
Expand Down
25 changes: 25 additions & 0 deletions arch/arm64/include/asm/signal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ARM64_ASM_SIGNAL_H
#define __ARM64_ASM_SIGNAL_H

#include <asm/memory.h>
#include <uapi/asm/signal.h>
#include <uapi/asm/siginfo.h>

static inline void __user *arch_untagged_si_addr(void __user *addr,
unsigned long sig,
unsigned long si_code)
{
/*
* For historical reasons, all bits of the fault address are exposed as
* address bits for watchpoint exceptions. New architectures should
* handle the tag bits consistently.
*/
if (sig == SIGTRAP && si_code == TRAP_BRKPT)
return addr;

return untagged_addr(addr);
}
#define arch_untagged_si_addr arch_untagged_si_addr

#endif
2 changes: 1 addition & 1 deletion arch/arm64/include/asm/system_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void die(const char *msg, struct pt_regs *regs, int err);

struct siginfo;
void arm64_notify_die(const char *str, struct pt_regs *regs,
int signo, int sicode, void __user *addr,
int signo, int sicode, unsigned long far,
int err);

void hook_debug_fault_code(int nr, int (*fn)(unsigned long, unsigned int,
Expand Down
6 changes: 3 additions & 3 deletions arch/arm64/include/asm/traps.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void register_undef_hook(struct undef_hook *hook);
void unregister_undef_hook(struct undef_hook *hook);
void force_signal_inject(int signal, int code, unsigned long address, unsigned int err);
void arm64_notify_segfault(unsigned long addr);
void arm64_force_sig_fault(int signo, int code, void __user *addr, const char *str);
void arm64_force_sig_mceerr(int code, void __user *addr, short lsb, const char *str);
void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr, const char *str);
void arm64_force_sig_fault(int signo, int code, unsigned long far, const char *str);
void arm64_force_sig_mceerr(int code, unsigned long far, short lsb, const char *str);
void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far, const char *str);

/*
* Move regs->pc to next instruction and do necessary setup before it
Expand Down
5 changes: 2 additions & 3 deletions arch/arm64/kernel/debug-monitors.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,8 @@ static void send_user_sigtrap(int si_code)
if (interrupts_enabled(regs))
local_irq_enable();

arm64_force_sig_fault(SIGTRAP, si_code,
(void __user *)instruction_pointer(regs),
"User debug trap");
arm64_force_sig_fault(SIGTRAP, si_code, instruction_pointer(regs),
"User debug trap");
}

static int single_step_handler(unsigned long unused, unsigned int esr,
Expand Down
2 changes: 0 additions & 2 deletions arch/arm64/kernel/entry-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static void notrace el1_abort(struct pt_regs *regs, unsigned long esr)
unsigned long far = read_sysreg(far_el1);

local_daif_inherit(regs);
far = untagged_addr(far);
do_mem_abort(far, esr, regs);
}
NOKPROBE_SYMBOL(el1_abort);
Expand Down Expand Up @@ -114,7 +113,6 @@ static void notrace el0_da(struct pt_regs *regs, unsigned long esr)

user_exit_irqoff();
local_daif_restore(DAIF_PROCCTX);
far = untagged_addr(far);
do_mem_abort(far, esr, regs);
}
NOKPROBE_SYMBOL(el0_da);
Expand Down
7 changes: 2 additions & 5 deletions arch/arm64/kernel/ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,11 @@ static void ptrace_hbptriggered(struct perf_event *bp,
break;
}
}
arm64_force_sig_ptrace_errno_trap(si_errno,
(void __user *)bkpt->trigger,
arm64_force_sig_ptrace_errno_trap(si_errno, bkpt->trigger,
desc);
}
#endif
arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT,
(void __user *)(bkpt->trigger),
desc);
arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT, bkpt->trigger, desc);
}

/*
Expand Down
5 changes: 2 additions & 3 deletions arch/arm64/kernel/sys_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ do_compat_cache_op(unsigned long start, unsigned long end, int flags)
*/
long compat_arm_syscall(struct pt_regs *regs, int scno)
{
void __user *addr;
unsigned long addr;

switch (scno) {
/*
Expand Down Expand Up @@ -111,8 +111,7 @@ long compat_arm_syscall(struct pt_regs *regs, int scno)
break;
}

addr = (void __user *)instruction_pointer(regs) -
(compat_thumb_mode(regs) ? 2 : 4);
addr = instruction_pointer(regs) - (compat_thumb_mode(regs) ? 2 : 4);

arm64_notify_die("Oops - bad compat syscall(2)", regs,
SIGILL, ILL_ILLTRP, addr, scno);
Expand Down
29 changes: 15 additions & 14 deletions arch/arm64/kernel/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,40 +170,40 @@ static void arm64_show_signal(int signo, const char *str)
__show_regs(regs);
}

void arm64_force_sig_fault(int signo, int code, void __user *addr,
void arm64_force_sig_fault(int signo, int code, unsigned long far,
const char *str)
{
arm64_show_signal(signo, str);
if (signo == SIGKILL)
force_sig(SIGKILL);
else
force_sig_fault(signo, code, addr);
force_sig_fault(signo, code, (void __user *)far);
}

void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
void arm64_force_sig_mceerr(int code, unsigned long far, short lsb,
const char *str)
{
arm64_show_signal(SIGBUS, str);
force_sig_mceerr(code, addr, lsb);
force_sig_mceerr(code, (void __user *)far, lsb);
}

void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far,
const char *str)
{
arm64_show_signal(SIGTRAP, str);
force_sig_ptrace_errno_trap(errno, addr);
force_sig_ptrace_errno_trap(errno, (void __user *)far);
}

void arm64_notify_die(const char *str, struct pt_regs *regs,
int signo, int sicode, void __user *addr,
int signo, int sicode, unsigned long far,
int err)
{
if (user_mode(regs)) {
WARN_ON(regs != current_pt_regs());
current->thread.fault_address = 0;
current->thread.fault_code = err;

arm64_force_sig_fault(signo, sicode, addr, str);
arm64_force_sig_fault(signo, sicode, far, str);
} else {
die(str, regs, err);
}
Expand Down Expand Up @@ -374,7 +374,7 @@ void force_signal_inject(int signal, int code, unsigned long address, unsigned i
signal = SIGKILL;
}

arm64_notify_die(desc, regs, signal, code, (void __user *)address, err);
arm64_notify_die(desc, regs, signal, code, address, err);
}

/*
Expand All @@ -385,7 +385,7 @@ void arm64_notify_segfault(unsigned long addr)
int code;

mmap_read_lock(current->mm);
if (find_vma(current->mm, addr) == NULL)
if (find_vma(current->mm, untagged_addr(addr)) == NULL)
code = SEGV_MAPERR;
else
code = SEGV_ACCERR;
Expand Down Expand Up @@ -448,12 +448,13 @@ NOKPROBE_SYMBOL(do_ptrauth_fault);

static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
{
unsigned long address;
unsigned long tagged_address, address;
int rt = ESR_ELx_SYS64_ISS_RT(esr);
int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
int ret = 0;

address = untagged_addr(pt_regs_read_reg(regs, rt));
tagged_address = pt_regs_read_reg(regs, rt);
address = untagged_addr(tagged_address);

switch (crm) {
case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
Expand All @@ -480,7 +481,7 @@ static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
}

if (ret)
arm64_notify_segfault(address);
arm64_notify_segfault(tagged_address);
else
arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
}
Expand Down Expand Up @@ -772,7 +773,7 @@ asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
*/
void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
{
void __user *pc = (void __user *)instruction_pointer(regs);
unsigned long pc = instruction_pointer(regs);

current->thread.fault_address = 0;
current->thread.fault_code = esr;
Expand Down
Loading

0 comments on commit dceec3f

Please sign in to comment.