arm64: expose FAR_EL1 tag bits in siginfo
Subsystems (5)
PATCH v18 1/1 arm64: expose FAR_EL1 tag bits in siginfo
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).
We should not stop clearing these bits in the existing fault address fields, because there may be existing userspace applications that are expecting the tag bits to be cleared. Instead, introduce a flag in sigaction.sa_flags, SA_EXPOSE_TAGBITS, and only expose the tag bits there if the signal handler has this flag set.
[1] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
---
To be applied on top of:
https://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace.git signal-for-v5.11
v18:
- move the SA_EXPOSE_TAGBITS handling to get_signal
v16:
- add missing file
v15:
- switched to the SA_EXPOSE_TAGBITS approach
proposed by Eric
- rebased to 5.10-rc3
v14:
- fix an inaccuracy in the commit message
- add some comments to arch_addr_tag_bits_mask
v13:
- renamed si_xflags to si_faultflags
- rebased to 5.10-rc2
v12:
- add new fields to signal_compat.c test cases
- rebased to 5.10-rc1
- mask out bits 63:60 for tag check faults
v11:
- add a comment explaining what the arch hook should do
- rename ignored bits to tag bits
v10:
- rename the flag to SIFAULTFLAG_ADDR_IGNORED_BITS
- use an arch hook to specify which bits are ignored, instead of passing them explicitly
- while refactoring for the arch hook, noticed that my previous patches missed a case involving cache maintenance instructions, so expose the tag bits for that signal as well
v9:
- make the ignored bits fields generic
- add some new dependent patches that prepare us to store the field in such a way that userspace can detect their presence
v8:
- rebase onto 5.8rc2
v7:
- switch to a new siginfo field instead of using sigcontext
- merge the patch back into one since the other patches are now unnecessary
v6:
- move fault address and fault code into the kernel_siginfo data structure
- split the patch in three since it was getting large and now has generic and arch-specific parts
v5:
- add padding to fault_addr_top_byte_context in order to ensure the correct size and preserve sp alignment
v4:
- expose only the tag bits in the context instead of the entire FAR_EL1
- remove mention of the new context from the sigcontext.__reserved[] note
v3:
- add documentation to tagged-pointers.rst
- update comments in sigcontext.h
v2:
- revert changes to hw_breakpoint.c
- rename set_thread_esr to set_thread_far_esr
Documentation/arm64/tagged-pointers.rst | 25 ++++++--- arch/arm64/include/asm/exception.h | 2 +- arch/arm64/include/asm/signal.h | 25 +++++++++ arch/arm64/include/asm/system_misc.h | 2 +- arch/arm64/include/asm/traps.h | 6 +-- arch/arm64/kernel/debug-monitors.c | 5 +- arch/arm64/kernel/entry-common.c | 2 - arch/arm64/kernel/ptrace.c | 7 +-- arch/arm64/kernel/sys_compat.c | 5 +- arch/arm64/kernel/traps.c | 29 ++++++----- arch/arm64/mm/fault.c | 68 ++++++++++++++----------- include/linux/signal.h | 14 +++++ include/linux/signal_types.h | 2 +- include/uapi/asm-generic/signal-defs.h | 3 ++ kernel/signal.c | 20 ++++++++ 15 files changed, 145 insertions(+), 70 deletions(-) create mode 100644 arch/arm64/include/asm/signal.h
Documentation/arm64/tagged-pointers.rst +19 -6
@@ -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.
arch/arm64/include/asm/exception.h +1 -1
@@ -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);
arch/arm64/include/asm/signal.h new file +25 -0
@@ -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
arch/arm64/include/asm/system_misc.h +1 -1
@@ -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,
arch/arm64/include/asm/traps.h +3 -3
@@ -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
arch/arm64/kernel/debug-monitors.c +2 -3
@@ -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,
arch/arm64/kernel/entry-common.c +0 -2
@@ -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);
@@ -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);
arch/arm64/kernel/ptrace.c +2 -5
@@ -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); }/*
arch/arm64/kernel/sys_compat.c +2 -3
@@ -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) {/*
@@ -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);
arch/arm64/kernel/traps.c +15 -14
@@ -170,32 +170,32 @@ 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)) {
@@ -203,7 +203,7 @@ void arm64_notify_die(const char *str, struct pt_regs *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); }
@@ -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); }/*
@@ -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 ;
@@ -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 */
@@ -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 ); }
@@ -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;
arch/arm64/mm/fault.c +37 -31
183 lines not on this page · raw message
include/linux/signal.h +14 -0
@@ -469,4 +469,18 @@ struct seq_file;extern void render_sigset_t (struct seq_file *,const char *,sigset_t *);#endif +#ifndef arch_untagged_si_addr +/* +* Given a fault address and a signal and si_code which correspond to the +* _sigfault union member, returns the address that must appear in si_addr if +* the signal handler does not have SA_EXPOSE_TAGBITS enabled in sa_flags. +*/ +static inline void __user *arch_untagged_si_addr (void __user *addr, +unsigned long sig, +unsigned long si_code) +{ +return addr; +} +#endif +#endif /* _LINUX_SIGNAL_H */
include/linux/signal_types.h +1 -1
@@ -78,6 +78,6 @@ struct ksignal {#define UAPI_SA_FLAGS \ (SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO | SA_ONSTACK | SA_RESTART | \ - SA_NODEFER | SA_RESETHAND | __ARCH_UAPI_SA_FLAGS) + SA_NODEFER | SA_RESETHAND | SA_EXPOSE_TAGBITS | __ARCH_UAPI_SA_FLAGS)#endif /* _LINUX_SIGNAL_TYPES_H */
include/uapi/asm-generic/signal-defs.h +3 -0
@@ -20,6 +20,8 @@* so this bit allows flag bit support to be detected from userspace while * allowing an old kernel to be distinguished from a kernel that supports every * flag bit. +* SA_EXPOSE_TAGBITS exposes an architecture-defined set of tag bits in +* siginfo.si_addr. * * SA_ONESHOT and SA_NOMASK are the historical Linux names for the Single * Unix names RESETHAND and NODEFER respectively.
@@ -41,6 +43,7 @@/* 0x00000100 used on sparc */ /* 0x00000200 used on sparc */ #define SA_UNSUPPORTED 0x00000400 +#define SA_EXPOSE_TAGBITS 0x00000800/* 0x00010000 used on mips */ /* 0x01000000 used on x86 */ /* 0x02000000 used on x86 */
kernel/signal.c +20 -0
@@ -2658,6 +2658,26 @@ bool get_signal(struct ksignal *ksig) ka = &sighand->action[signr-1 ]; +if (!(ka->sa.sa_flags &SA_EXPOSE_TAGBITS )) { +switch (siginfo_layout (signr, ksig->info.si_code)) { +case SIL_FAULT : +case SIL_FAULT_MCEERR : +case SIL_FAULT_BNDERR : +case SIL_FAULT_PKUERR : + ksig->info.si_addr =arch_untagged_si_addr ( + ksig->info.si_addr, signr, + ksig->info.si_code); +break ; +case SIL_KILL : +case SIL_TIMER : +case SIL_POLL : +case SIL_CHLD : +case SIL_RT : +case SIL_SYS : +break ; + } + } +/* Trace actually delivered signals. */ trace_signal_deliver (signr, &ksig->info, ka);
Signature · 1 line
2.29.2.299.gdc1121823c-goog
include/uapi/asm-generic/signal-defs.h line 24
Quoted 10 lines · Peter Collingbourne
include/uapi/asm-generic/signal-defs.h +2
-0@@ -20,6 +20,8 @@* so this bit allows flag bit support to be detected from userspace while * allowing an old kernel to be distinguished from a kernel that supports every * flag bit. +* SA_EXPOSE_TAGBITS exposes an architecture-defined set of tag bits in +* siginfo.si_addr.
Do we perhaps want to say the high bits of si_addr?
Quoted 31 lines
include/uapi/asm-generic/signal-defs.h +1
-0* * SA_ONESHOT and SA_NOMASK are the historical Linux names for the Single * Unix names RESETHAND and NODEFER respectively. @@ -41,6 +43,7 @@/* 0x00000100 used on sparc */ /* 0x00000200 used on sparc */ #define SA_UNSUPPORTED 0x00000400 +#define SA_EXPOSE_TAGBITS 0x00000800/* 0x00010000 used on mips */ /* 0x01000000 used on x86 */ /* 0x02000000 used on x86 */ kernel/signal.c +12
-0@@ -2658,6 +2658,26 @@ bool get_signal(struct ksignal *ksig) ka = &sighand->action[signr-1 ]; +if (!(ka->sa.sa_flags &SA_EXPOSE_TAGBITS )) { +switch (siginfo_layout (signr, ksig->info.si_code)) { +case SIL_FAULT : +case SIL_FAULT_MCEERR : +case SIL_FAULT_BNDERR : +case SIL_FAULT_PKUERR : + ksig->info.si_addr =arch_untagged_si_addr ( + ksig->info.si_addr, signr, + ksig->info.si_code); +break ; +case SIL_KILL : +case SIL_TIMER :
kernel/signal.c bool get_signal(struct ksignal *ksig) · line 2682
Quoted 10 lines
kernel/signal.c +8
-0+case SIL_POLL : +case SIL_CHLD : +case SIL_RT : +case SIL_SYS : +break ; + } + } +/* Trace actually delivered signals. */ trace_signal_deliver (signr, &ksig->info, ka);
Overall this looks good.
It is a common path so I wonder about the generated code, and how close to a noop this becomes on x86 and other architetures without tag bits.
Can you put this blob of code in it's own function (perhaps called hide_si_addr_tag_bits) and call it right after "ksig->sig = signr" line?
Effectively that is the same place in the code but it gets it outside of the sighand lock. The tracing code does not care as it does not look at si_addr.
I am concerned with the complexity of reading get_signal and using a well named inline function should make it unnecessary to read that code unless you care about what is going on.
Further having the code outside of the lock at the end of get_signal with nothing else going on seems much easier to reason about. The code is get_signal is something that needs reading and reasoning about.
Eric
Quoted 15 lines · Eric W. Biederman
include/uapi/asm-generic/signal-defs.h line 24
Quoted 10 lines · Peter Collingbourne
include/uapi/asm-generic/signal-defs.h +2
-0@@ -20,6 +20,8 @@* so this bit allows flag bit support to be detected from userspace while * allowing an old kernel to be distinguished from a kernel that supports every * flag bit. +* SA_EXPOSE_TAGBITS exposes an architecture-defined set of tag bits in +* siginfo.si_addr. Do we perhaps want to say the high bits of
si_addr?
I wouldn't want to make any specific claims about which bits of si_addr are involved here since they would not necessarily be the high bits. For example, imagine a software implementation of tagged addresses that works by constructing a fake top-level page table such that it contains N identical references to what from the kernel's perspective would be the true top-level page table. Assuming that your architecture does not give you the full 64 bits of address space to use with page tables your tag bits would be somewhere in the middle.
Quoted 44 lines
Quoted 31 lines
include/uapi/asm-generic/signal-defs.h +1
-0* * SA_ONESHOT and SA_NOMASK are the historical Linux names for the Single * Unix names RESETHAND and NODEFER respectively. @@ -41,6 +43,7 @@/* 0x00000100 used on sparc */ /* 0x00000200 used on sparc */ #define SA_UNSUPPORTED 0x00000400 +#define SA_EXPOSE_TAGBITS 0x00000800/* 0x00010000 used on mips */ /* 0x01000000 used on x86 */ /* 0x02000000 used on x86 */ kernel/signal.c +12
-0@@ -2658,6 +2658,26 @@ bool get_signal(struct ksignal *ksig) ka = &sighand->action[signr-1 ]; +if (!(ka->sa.sa_flags &SA_EXPOSE_TAGBITS )) { +switch (siginfo_layout (signr, ksig->info.si_code)) { +case SIL_FAULT : +case SIL_FAULT_MCEERR : +case SIL_FAULT_BNDERR : +case SIL_FAULT_PKUERR : + ksig->info.si_addr =arch_untagged_si_addr ( + ksig->info.si_addr, signr, + ksig->info.si_code); +break ; +case SIL_KILL : +case SIL_TIMER :kernel/signal.c bool get_signal(struct ksignal *ksig) · line 2682
Quoted 10 lines
kernel/signal.c +8
-0+case SIL_POLL : +case SIL_CHLD : +case SIL_RT : +case SIL_SYS : +break ; + } + } +/* Trace actually delivered signals. */ trace_signal_deliver (signr, &ksig->info, ka);Overall this looks good.
Quoted 2 lines
It is a common path so I wonder about the generated code, and how close to a noop this becomes on x86 and other architetures without tag bits.
I would in general expect compilers to be able to optimize all of this away on non-arm64 architectures because the compiler has enough information to know that the code isn't doing anything. Indeed I compiled the kernel for x86_64 with and without this change and the size of kernel/signal.o was the same.
Quoted 7 lines
Can you put this blob of code in it's own function (perhaps called
hide_si_addr_tag_bits) and call it right after "ksig->sig= signr" line?Effectively that is the same place in the code but it gets it outside of the sighand lock. The tracing code does not care as it does not look at
si_addr.
Quoted 7 lines
I am concerned with the complexity of reading
get_signaland using a well named inline function should make it unnecessary to read that code unless you care about what is going on.Further having the code outside of the lock at the end of
get_signalwith nothing else going on seems much easier to reason about. The code isget_signalis something that needs reading and reasoning about.
Okay, done in v19.
Peter