linux-user: User support for AArch64 BTI
v10 cover linux-user: User support for AArch64 BTI
The kernel abi for this was merged in v5.8, just as the qemu 5.1 merge window was closing, so this slipped to the next dev cycle.
Changes from v9:
* Split what is now patch 7 into 3 more (pmm).
* All prerequisites are now upstream.
r~
Richard Henderson (8):
linux-user/aarch64: Reset btype for signals
linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI include/elf: Add defines related to GNU property notes for AArch64 linux-user/elfload: Fix coding style in load_elf_image linux-user/elfload: Adjust iteration over phdr linux-user/elfload: Move PT_INTERP detection to first loop linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes tests/tcg/aarch64: Add bti smoke test
include/elf.h | 22 +++++ include/exec/cpu-all.h | 2 + linux-user/qemu.h | 4 + linux-user/syscall_defs.h | 4 + target/arm/cpu.h | 5 + linux-user/aarch64/signal.c | 10 +- linux-user/elfload.c | 147 ++++++++++++++++++++++-------- linux-user/mmap.c | 16 ++++ target/arm/translate-a64.c | 6 +- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++ tests/tcg/configure.sh | 4 + 13 files changed, 298 insertions(+), 42 deletions(-) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c
Signature · 1 line
2.25.1
PATCH v10 1/8 linux-user/aarch64: Reset btype for signals
The kernel sets btype for the signal handler as if for a call.
--- linux-user/aarch64/signal.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
linux-user/aarch64/signal.c +8 -2
@@ -506,10 +506,16 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, + offsetof(struct target_rt_frame_record , tramp); } env->xregs[0 ] = usig; - env->xregs[31 ] = frame_addr; env->xregs[29 ] = frame_addr + fr_ofs; - env->pc = ka->_sa_handler; env->xregs[30 ] = return_addr; + env->xregs[31 ] = frame_addr; + env->pc = ka->_sa_handler; + +/* Invoke the signal handler as if by indirect call. */ +if (cpu_isar_feature (aa64_bti,env_archcpu (env))) { + env->btype =2 ; + } +if (info) {tswap_siginfo (&frame->info, info); env->xregs[1 ] = frame_addr + offsetof(struct target_rt_sigframe , info);
Signature · 1 line
2.25.1
PATCH v10 2/8 linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI
In reply to Richard HendersonTransform the prot bit to a qemu internal page bit, and save it in the page tables.
---
v10: Add PAGE_BTI define (pmm).
--- include/exec/cpu-all.h | 2 ++ linux-user/syscall_defs.h | 4 ++++ target/arm/cpu.h | 5 +++++ linux-user/mmap.c | 16 ++++++++++++++++ target/arm/translate-a64.c | 6 +++--- 5 files changed, 30 insertions(+), 3 deletions(-)
include/exec/cpu-all.h +2 -0
@@ -274,6 +274,8 @@ extern intptr_t qemu_host_page_mask;/* FIXME: Code that sets/uses this is broken and needs to go away. */ #define PAGE_RESERVED 0x0020#endif +/* Target-specific bits that will be used via page_get_flags(). */ +#define PAGE_TARGET_1 0x0080#if defined(CONFIG_USER_ONLY )void page_dump (FILE *f);
linux-user/syscall_defs.h +4 -0
@@ -1277,6 +1277,10 @@ struct target_winsize {#define TARGET_PROT_SEM 0x08#endif +#ifdef TARGET_AARCH64 +#define TARGET_PROT_BTI 0x10 +#endif +/* Common */ #define TARGET_MAP_SHARED 0x01/* Share changes */ #define TARGET_MAP_PRIVATE 0x02/* Changes are private */
target/arm/cpu.h +5 -0
@@ -3439,6 +3439,11 @@ static inline MemTxAttrs *typecheck_memtxattrs(MemTxAttrs *x)#define arm_tlb_bti_gp (x) (typecheck_memtxattrs(x)->target_tlb_bit0)#define arm_tlb_mte_tagged (x) (typecheck_memtxattrs(x)->target_tlb_bit1) +/* +* AArch64 usage of the PAGE_TARGET_* bits for linux-user. +*/ +#define PAGE_BTI PAGE_TARGET_1 +/* * Naming convention for isar_feature functions: * Functions which test 32-bit ID registers should have _aa32_ in
linux-user/mmap.c +16 -0
@@ -83,6 +83,22 @@ static int validate_prot_to_pageflags(int *host_prot, int prot) *host_prot = (prot & (PROT_READ |PROT_WRITE )) | (prot &PROT_EXEC ?PROT_READ :0 ); +#ifdef TARGET_AARCH64 +/* +* The PROT_BTI bit is only accepted if the cpu supports the feature. +* Since this is the unusual case, don't bother checking unless +* the bit has been requested. If set and valid, record the bit +* within QEMU's page_flags. +*/ +if (prot &TARGET_PROT_BTI ) { +ARMCPU *cpu =ARM_CPU (thread_cpu); +if (cpu_isar_feature (aa64_bti, cpu)) { + valid |=TARGET_PROT_BTI ; + page_flags |=PAGE_BTI ; + } + } +#endif +return prot & ~valid ?0 : page_flags; }
target/arm/translate-a64.c +3 -3
@@ -14507,10 +14507,10 @@ static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)*/ static bool is_guarded_page (CPUARMState *env,DisasContext *s) { -#ifdef CONFIG_USER_ONLY -return false;/* FIXME */ -#else uint64_t addr = s->base.pc_first; +#ifdef CONFIG_USER_ONLY +return page_get_flags (addr) &PAGE_BTI ; +#else int mmu_idx =arm_to_core_mmu_idx (s->mmu_idx);unsigned int index =tlb_index (env, mmu_idx, addr);CPUTLBEntry *entry =tlb_entry (env, mmu_idx, addr);
Signature · 1 line
2.25.1
PATCH v10 3/8 include/elf: Add defines related to GNU property notes for AArch64
In reply to Richard HendersonThese are all of the defines required to parse GNU_PROPERTY_AARCH64_FEATURE_1_AND, copied from binutils. Other missing defines related to other GNU program headers and notes are elided for now.
--- include/elf.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/elf.h +22 -0
@@ -26,9 +26,13 @@ typedef int64_t Elf64_Sxword;#define PT_NOTE 4#define PT_SHLIB 5#define PT_PHDR 6 +#define PT_LOOS 0x60000000 +#define PT_HIOS 0x6fffffff#define PT_LOPROC 0x70000000#define PT_HIPROC 0x7fffffff +#define PT_GNU_PROPERTY (PT_LOOS + 0x474e553) +#define PT_MIPS_REGINFO 0x70000000#define PT_MIPS_RTPROC 0x70000001#define PT_MIPS_OPTIONS 0x70000002
@@ -1657,6 +1661,24 @@ typedef struct elf64_shdr {#define NT_ARM_SYSTEM_CALL 0x404/* ARM system call number */ #define NT_ARM_SVE 0x405/* ARM Scalable Vector Extension regs */ +/* Defined note types for GNU systems. */ + +#define NT_GNU_PROPERTY_TYPE_0 5/* Program property */ + +/* Values used in GNU .note.gnu.property notes (NT_GNU_PROPERTY_TYPE_0). */ + +#define GNU_PROPERTY_STACK_SIZE 1 +#define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2 + +#define GNU_PROPERTY_LOPROC 0xc0000000 +#define GNU_PROPERTY_HIPROC 0xdfffffff +#define GNU_PROPERTY_LOUSER 0xe0000000 +#define GNU_PROPERTY_HIUSER 0xffffffff + +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000 +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1u << 0) +#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1u << 1) +/* * Physical entry point into the kernel. *
Signature · 1 line
2.25.1
PATCH v10 4/8 linux-user/elfload: Fix coding style in load_elf_image
In reply to Richard HendersonFixing this now will clarify following patches.
--- linux-user/elfload.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
linux-user/elfload.c +9 -3
@@ -2531,9 +2531,15 @@ static void load_elf_image(const char *image_name, int image_fd,abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;int elf_prot =0 ; -if (eppnt->p_flags &PF_R ) elf_prot =PROT_READ ; -if (eppnt->p_flags &PF_W ) elf_prot |=PROT_WRITE ; -if (eppnt->p_flags &PF_X ) elf_prot |=PROT_EXEC ; +if (eppnt->p_flags &PF_R ) { + elf_prot |=PROT_READ ; + } +if (eppnt->p_flags &PF_W ) { + elf_prot |=PROT_WRITE ; + } +if (eppnt->p_flags &PF_X ) { + elf_prot |=PROT_EXEC ; + } vaddr = load_bias + eppnt->p_vaddr; vaddr_po =TARGET_ELF_PAGEOFFSET (vaddr);
Signature · 1 line
2.25.1
Re: [PATCH v10 4/8] linux-user/elfload: Fix coding style in load_elf_image
Quoted 3 lines · Richard Henderson
Fixing this now will clarify following patches.
Signed-off-by Richard Henderson <[email protected]>
Quoted 28 lines
--- linux-user/elfload.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)linux-user/elfload.c +9
-3@@ -2531,9 +2531,15 @@ static void load_elf_image(const char *image_name, int image_fd,abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;int elf_prot =0 ; -if (eppnt->p_flags &PF_R ) elf_prot =PROT_READ ; -if (eppnt->p_flags &PF_W ) elf_prot |=PROT_WRITE ; -if (eppnt->p_flags &PF_X ) elf_prot |=PROT_EXEC ; +if (eppnt->p_flags &PF_R ) { + elf_prot |=PROT_READ ; + } +if (eppnt->p_flags &PF_W ) { + elf_prot |=PROT_WRITE ; + } +if (eppnt->p_flags &PF_X ) { + elf_prot |=PROT_EXEC ; + } vaddr = load_bias + eppnt->p_vaddr; vaddr_po =TARGET_ELF_PAGEOFFSET (vaddr);
Re: [PATCH v10 4/8] linux-user/elfload: Fix coding style in load_elf_image
In reply to Richard HendersonQuoted 7 lines · Richard Henderson
Fixing this now will clarify following patches.
Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
thanks
-- PMM
PATCH v10 5/8 linux-user/elfload: Adjust iteration over phdr
In reply to Richard HendersonThe second loop uses a loop induction variable, and the first does not. Transform the first to match the second, to simplify a following patch moving code between them.
--- linux-user/elfload.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
linux-user/elfload.c +5 -4
@@ -2426,17 +2426,18 @@ static void load_elf_image(const char *image_name, int image_fd, loaddr =-1 , hiaddr =0 ; info->alignment =0 ;for (i =0 ; i < ehdr->e_phnum; ++i) { -if (phdr[i].p_type ==PT_LOAD ) { -abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset; +struct elf_phdr *eppnt = phdr + i; +if (eppnt->p_type ==PT_LOAD ) { +abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;if (a < loaddr) { loaddr = a; } - a = phdr[i].p_vaddr + phdr[i].p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz;if (a > hiaddr) { hiaddr = a; } ++info->nsegs; - info->alignment |= phdr[i].p_align; + info->alignment |= eppnt->p_align; } }
Signature · 1 line
2.25.1
Re: [PATCH v10 5/8] linux-user/elfload: Adjust iteration over phdr
Quoted 5 lines · Richard Henderson
The second loop uses a loop induction variable, and the first does not. Transform the first to match the second, to simplify a following patch moving code between them.
Signed-off-by Richard Henderson <[email protected]>
Quoted 32 lines
--- linux-user/elfload.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)linux-user/elfload.c +5
-4@@ -2426,17 +2426,18 @@ static void load_elf_image(const char *image_name, int image_fd, loaddr =-1 , hiaddr =0 ; info->alignment =0 ;for (i =0 ; i < ehdr->e_phnum; ++i) { -if (phdr[i].p_type ==PT_LOAD ) { -abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset; +struct elf_phdr *eppnt = phdr + i; +if (eppnt->p_type ==PT_LOAD ) { +abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;if (a < loaddr) { loaddr = a; } - a = phdr[i].p_vaddr + phdr[i].p_memsz; + a = eppnt->p_vaddr + eppnt->p_memsz;if (a > hiaddr) { hiaddr = a; } ++info->nsegs; - info->alignment |= phdr[i].p_align; + info->alignment |= eppnt->p_align; } }
Re: [PATCH v10 5/8] linux-user/elfload: Adjust iteration over phdr
In reply to Richard HendersonQuoted 9 lines · Richard Henderson
The second loop uses a loop induction variable, and the first does not. Transform the first to match the second, to simplify a following patch moving code between them.
Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
thanks
-- PMM
PATCH v10 6/8 linux-user/elfload: Move PT_INTERP detection to first loop
In reply to Richard HendersonFor BTI, we need to know if the executable is static or dynamic, which means looking for PT_INTERP earlier.
--- linux-user/elfload.c | 60 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 29 deletions(-)
linux-user/elfload.c +31 -29
@@ -2421,8 +2421,10 @@ static void load_elf_image(const char *image_name, int image_fd,mmap_lock (); -/* Find the maximum size of the image and allocate an appropriate -amount of memory to handle that. */ +/* +* Find the maximum size of the image and allocate an appropriate +* amount of memory to handle that. Locate the interpreter, if any. +*/ loaddr =-1 , hiaddr =0 ; info->alignment =0 ;for (i =0 ; i < ehdr->e_phnum; ++i) {
@@ -2438,6 +2440,33 @@ static void load_elf_image(const char *image_name, int image_fd, } ++info->nsegs; info->alignment |= eppnt->p_align; + }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { +char *interp_name; + +if (*pinterp_name) { + errmsg ="Multiple PT_INTERP entries" ; + goto exit_errmsg; + } + interp_name =malloc (eppnt->p_filesz); +if (!interp_name) { + goto exit_perror; + } + +if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { +memcpy (interp_name, bprm_buf + eppnt->p_offset, + eppnt->p_filesz); + }else { + retval =pread (image_fd, interp_name, eppnt->p_filesz, + eppnt->p_offset); +if (retval != eppnt->p_filesz) { + goto exit_perror; + } + } +if (interp_name[eppnt->p_filesz -1 ] !=0 ) { + errmsg ="Invalid PT_INTERP entry" ; + goto exit_errmsg; + } + *pinterp_name = interp_name; } }
@@ -2590,33 +2619,6 @@ static void load_elf_image(const char *image_name, int image_fd,if (vaddr_em > info->brk) { info->brk = vaddr_em; } - }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { -char *interp_name; - -if (*pinterp_name) { - errmsg ="Multiple PT_INTERP entries" ; - goto exit_errmsg; - } - interp_name =malloc (eppnt->p_filesz); -if (!interp_name) { - goto exit_perror; - } - -if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { -memcpy (interp_name, bprm_buf + eppnt->p_offset, - eppnt->p_filesz); - }else { - retval =pread (image_fd, interp_name, eppnt->p_filesz, - eppnt->p_offset); -if (retval != eppnt->p_filesz) { - goto exit_perror; - } - } -if (interp_name[eppnt->p_filesz -1 ] !=0 ) { - errmsg ="Invalid PT_INTERP entry" ; - goto exit_errmsg; - } - *pinterp_name = interp_name; #ifdef TARGET_MIPS }else if (eppnt->p_type ==PT_MIPS_ABIFLAGS ) {Mips_elf_abiflags_v0 abiflags;
Signature · 1 line
2.25.1
Re: [PATCH v10 6/8] linux-user/elfload: Move PT_INTERP detection to first loop
Quoted 38 lines · Richard Henderson
For BTI, we need to know if the executable is static or dynamic, which means looking for
PT_INTERPearlier.Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 60 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 29 deletions(-)linux-user/elfload.c +13
-2@@ -2421,8 +2421,10 @@ static void load_elf_image(const char *image_name, int image_fd,mmap_lock (); -/* Find the maximum size of the image and allocate an appropriate -amount of memory to handle that. */ +/* +* Find the maximum size of the image and allocate an appropriate +* amount of memory to handle that. Locate the interpreter, if any. +*/ loaddr =-1 , hiaddr =0 ; info->alignment =0 ;for (i =0 ; i < ehdr->e_phnum; ++i) {@@ -2438,6 +2440,33 @@ static void load_elf_image(const char *image_name, int image_fd, } ++info->nsegs; info->alignment |= eppnt->p_align; + }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { +char *interp_name; + +if (*pinterp_name) { + errmsg ="Multiple PT_INTERP entries" ; + goto exit_errmsg; + } + interp_name =malloc (eppnt->p_filesz); +if (!interp_name) {
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2461
Quoted 10 lines
linux-user/elfload.c +10
-0+ goto exit_perror; + } + +if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { +memcpy (interp_name, bprm_buf + eppnt->p_offset, + eppnt->p_filesz); + }else { + retval =pread (image_fd, interp_name, eppnt->p_filesz, + eppnt->p_offset); +if (retval != eppnt->p_filesz) {
Preexisting, free(interp_name)?
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2466
Quoted 5 lines
linux-user/elfload.c +5
-0+ goto exit_perror; + } + } +if (interp_name[eppnt->p_filesz -1 ] !=0 ) { + errmsg ="Invalid PT_INTERP entry" ;
Ditto, otherwise:
Quoted 41 lines
linux-user/elfload.c +3
-27+ goto exit_errmsg; + } + *pinterp_name = interp_name; } }@@ -2590,33 +2619,6 @@ static void load_elf_image(const char *image_name, int image_fd,if (vaddr_em > info->brk) { info->brk = vaddr_em; } - }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { -char *interp_name; - -if (*pinterp_name) { - errmsg ="Multiple PT_INTERP entries" ; - goto exit_errmsg; - } - interp_name =malloc (eppnt->p_filesz); -if (!interp_name) { - goto exit_perror; - } - -if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { -memcpy (interp_name, bprm_buf + eppnt->p_offset, - eppnt->p_filesz); - }else { - retval =pread (image_fd, interp_name, eppnt->p_filesz, - eppnt->p_offset); -if (retval != eppnt->p_filesz) { - goto exit_perror; - } - } -if (interp_name[eppnt->p_filesz -1 ] !=0 ) { - errmsg ="Invalid PT_INTERP entry" ; - goto exit_errmsg; - } - *pinterp_name = interp_name; #ifdef TARGET_MIPS }else if (eppnt->p_type ==PT_MIPS_ABIFLAGS ) {Mips_elf_abiflags_v0 abiflags;
Re: [PATCH v10 6/8] linux-user/elfload: Move PT_INTERP detection to first loop
Quoted 43 lines · Philippe Mathieu-Daudé
Quoted 42 lines · Richard Henderson
For BTI, we need to know if the executable is static or dynamic, which means looking for
PT_INTERPearlier.Signed-off-by Richard Henderson <[email protected]>--- linux-user/elfload.c | 60 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 29 deletions(-)linux-user/elfload.c +17
-2@@ -2421,8 +2421,10 @@ static void load_elf_image(const char *image_name, int image_fd,mmap_lock (); -/* Find the maximum size of the image and allocate an appropriate -amount of memory to handle that. */ +/* +* Find the maximum size of the image and allocate an appropriate +* amount of memory to handle that. Locate the interpreter, if any. +*/ loaddr =-1 , hiaddr =0 ; info->alignment =0 ;for (i =0 ; i < ehdr->e_phnum; ++i) {@@ -2438,6 +2440,33 @@ static void load_elf_image(const char *image_name, int image_fd, } ++info->nsegs; info->alignment |= eppnt->p_align; + }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { +char *interp_name; + +if (*pinterp_name) { + errmsg ="Multiple PT_INTERP entries" ; + goto exit_errmsg; + } + interp_name =malloc (eppnt->p_filesz); +if (!interp_name) { + goto exit_perror; + } + +if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) {
Quoted 8 lines
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2461
Quoted 6 lines
linux-user/elfload.c +6
-0+memcpy (interp_name, bprm_buf + eppnt->p_offset, + eppnt->p_filesz); + }else { + retval =pread (image_fd, interp_name, eppnt->p_filesz, + eppnt->p_offset); +if (retval != eppnt->p_filesz) {Preexisting,
free(interp_name)?
I just sent a patch using g_steal_pointer() instead:
https://lists.gnu.org/archive/html/qemu-devel/2020-10/msg00792.html
(Maybe I should have tagged it RFC as this is the first time I try this API).
Quoted 53 lines
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2466
Quoted 6 lines
linux-user/elfload.c +5
-0+ goto exit_perror; + } + } +if (interp_name[eppnt->p_filesz -1 ] !=0 ) { + errmsg ="Invalid PT_INTERP entry" ;Ditto, otherwise:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 41 lines
linux-user/elfload.c +3
-27+ goto exit_errmsg; + } + *pinterp_name = interp_name; } }@@ -2590,33 +2619,6 @@ static void load_elf_image(const char *image_name, int image_fd,if (vaddr_em > info->brk) { info->brk = vaddr_em; } - }else if (eppnt->p_type ==PT_INTERP && pinterp_name) { -char *interp_name; - -if (*pinterp_name) { - errmsg ="Multiple PT_INTERP entries" ; - goto exit_errmsg; - } - interp_name =malloc (eppnt->p_filesz); -if (!interp_name) { - goto exit_perror; - } - -if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { -memcpy (interp_name, bprm_buf + eppnt->p_offset, - eppnt->p_filesz); - }else { - retval =pread (image_fd, interp_name, eppnt->p_filesz, - eppnt->p_offset); -if (retval != eppnt->p_filesz) { - goto exit_perror; - } - } -if (interp_name[eppnt->p_filesz -1 ] !=0 ) { - errmsg ="Invalid PT_INTERP entry" ; - goto exit_errmsg; - } - *pinterp_name = interp_name; #ifdef TARGET_MIPS }else if (eppnt->p_type ==PT_MIPS_ABIFLAGS ) {Mips_elf_abiflags_v0 abiflags;
PATCH v10 7/8 linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes
In reply to Richard HendersonFor aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit, which indicates that the image should be mapped with guarded pages.
---
v9: Only map the startup executable with BTI; anything else must be
handled by the interpreter.
v10: Split out preparatory patches (pmm).
--- linux-user/qemu.h | 4 +++ linux-user/elfload.c | 68 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-)
linux-user/qemu.h +4 -0
@@ -61,6 +61,10 @@ struct image_info {abi_ulong interpreter_loadmap_addr;abi_ulong interpreter_pt_dynamic_addr;struct image_info *other_info; + +/* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */ +uint32_t note_flags; +#ifdef TARGET_MIPS int fp_abi;int interp_fp_abi;
linux-user/elfload.c +66 -2
92 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v10 7/8] linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes
Quoted 40 lines · Richard Henderson
For aarch64, this includes the
GNU_PROPERTY_AARCH64_FEATURE_1_BTIbit, which indicates that the image should be mapped with guarded pages.Signed-off-by Richard Henderson <[email protected]>---
v9: Only map the startup executable with BTI; anything else must be
handled by the interpreter.
v10: Split out preparatory patches (pmm).linux-user/elfload.c +26
-0@@ -2467,6 +2467,50 @@ static void load_elf_image(const char *image_name, int image_fd, goto exit_errmsg; } *pinterp_name = interp_name; + }else if (eppnt->p_type ==PT_GNU_PROPERTY ) { +/* Process NT_GNU_PROPERTY_TYPE_0. */ +const uint32_t gnu0_magic =const_le32 ('G' |'N' <<8 |'U' <<16 ); +uint32_t note[7 ]; + +/* +* The note contents are 7 words, but depending on LP64 vs ILP32 +* there may be an 8th padding word at the end. Check for and +* read the minimum size. Further checks below will validate +* that the sizes of everything involved are as we expect. +*/ +if (eppnt->p_filesz <sizeof (note)) { +continue ; + } +if (eppnt->p_offset + eppnt->p_filesz <=BPRM_BUF_SIZE ) { +memcpy (note, bprm_buf + eppnt->p_offset,sizeof (note)); + }else { + retval =pread (image_fd, note,sizeof (note), eppnt->p_offset); +if (retval !=sizeof (note)) { + goto exit_perror; + } + } +#ifdef BSWAP_NEEDED +for (i =0 ; i <ARRAY_SIZE (note); ++i) { +bswap32s (note + i); + }
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2505
Quoted 10 lines
linux-user/elfload.c +10
-0+#endif +/* +* Check that this is a NT_GNU_PROPERTY_TYPE_0 note. +* Again, descsz includes padding. Full size validation +* awaits checking the final payload. +*/ +if (note[0 ] !=4 ||/* namesz */ + note[1 ] <12 ||/* descsz */ + note[2 ] !=NT_GNU_PROPERTY_TYPE_0 ||/* type */ + note[3 ] != gnu0_magic) {/* name */
note[2] and note[3] are both basically magic numbers, AIUI. Why do we have a #define for one but we assemble the other with a const_le32() expression ?
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2512
Quoted 7 lines
linux-user/elfload.c +7
-0+continue ; + } +#ifdef TARGET_AARCH64 +if (note[4 ] ==GNU_PROPERTY_AARCH64_FEATURE_1_AND && + note[5 ] ==4 ) { + info->note_flags = note[6 ]; + }
The spec for the .note.gnu.property section (which AIUI is
https://raw.githubusercontent.com/wiki/hjl-tools/linux-abi/linux-abi-draft.pdf
) says that the n_desc (words 4 and up) is an array of program properties. There doesn't seem to be any guarantee that there is only one entry or that the FEATURE_1_AND entry is the first in the list. Don't we need to iterate through the array to find matches? This seems to be how the kernel does it:
https://elixir.bootlin.com/linux/latest/source/fs/binfmt_elf.c#L786
(Is it worth adding the infrastructure to parse notes generically the way the kernel has? I dunno if we think it's likely we'll want to do this for more note types and/or other architectures in future, so it might just be pointless complexity.)
thanks
-- PMM
Re: [PATCH v10 7/8] linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes
Quoted 8 lines · Peter Maydell
linux-user/elfload.c static void load_elf_image(const char *image_name, int image_fd, · line 2505
Quoted 4 lines
linux-user/elfload.c +4
-0+if (note[0 ] !=4 ||/* namesz */ + note[1 ] <12 ||/* descsz */ + note[2 ] !=NT_GNU_PROPERTY_TYPE_0 ||/* type */ + note[3 ] != gnu0_magic) {/* name */ note[2] and note[3] are both basically magic numbers, AIUI. Why do we have a #define for one but we assemble the other with a
const_le32()expression ?
Because one is defined as a number, and the other is defined as a string. And why *that* is, I don't know. Silliness, perhaps.
Quoted 8 lines
The spec for the .note.gnu.property section (which AIUI is
https://raw.githubusercontent.com/wiki/hjl-tools/linux-abi/linux-abi-draft.pdf
) says that then_desc(words 4 and up) is an array of program properties. There doesn't seem to be any guarantee that there is only one entry or that theFEATURE_1_ANDentry is the first in the list. Don't we need to iterate through the array to find matches? This seems to be how the kernel does it:
https://elixir.bootlin.com/linux/latest/source/fs/binfmt_elf.c#L786
Hmm. I missed that change since the first time I looked at the in-flight patch set.
Quoted 4 lines
(Is it worth adding the infrastructure to parse notes generically the way the kernel has? I dunno if we think it's likely we'll want to do this for more note types and/or other architectures in future, so it might just be pointless complexity.)
I dunno about that either. I'm not really sure what "generically" would look like without another exemplar. I'll look at what else arch_parse_elf_property() is being used for.
r~
PATCH v10 8/8 tests/tcg/aarch64: Add bti smoke test
In reply to Richard HendersonThis test requires gcc 10 for -mbranch-protection=standard.
---
v9: Expect and require gcc 10.
--- tests/tcg/aarch64/bti-1.c | 62 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 51 +++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 7 ++++ tests/tcg/configure.sh | 4 ++ 4 files changed, 124 insertions(+) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c
tests/tcg/aarch64/bti-1.c new file +62 -0
@@ -0,0 +1,62 @@ +/* +* Branch target identification, basic notskip cases. +*/ + +#include "bti-crt.inc.c" + +static void skip2_sigill (int sig,siginfo_t *info,ucontext_t *uc) +{ + uc->uc_mcontext.pc +=8 ; + uc->uc_mcontext.pstate =1 ; +} + +#define NOP "nop" +#define BTI_N "hint #32" +#define BTI_C "hint #34" +#define BTI_J "hint #36" +#define BTI_JC "hint #38" + +#define BTYPE_1 (DEST ) \ + asm("mov %0,#1; adr x16, 1f; br x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16") + +#define BTYPE_2 (DEST ) \ + asm("mov %0,#1; adr x16, 1f; blr x16; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x16", "x30") + +#define BTYPE_3 (DEST ) \ + asm("mov %0,#1; adr x15, 1f; br x15; 1: " DEST "; mov %0,#0" \ + : "=r"(skipped) : : "x15") + +#define TEST (WHICH ,DEST ,EXPECT ) \ + do { WHICH(DEST); fail += skipped ^ EXPECT; } while (0) + + +int main () +{ +int fail =0 ; +int skipped; + +/* Signal-like with SA_SIGINFO. */ +signal_info (SIGILL , skip2_sigill); + +TEST (BTYPE_1 ,NOP ,1 ); +TEST (BTYPE_1 ,BTI_N ,1 ); +TEST (BTYPE_1 ,BTI_C ,0 ); +TEST (BTYPE_1 ,BTI_J ,0 ); +TEST (BTYPE_1 ,BTI_JC ,0 ); + +TEST (BTYPE_2 ,NOP ,1 ); +TEST (BTYPE_2 ,BTI_N ,1 ); +TEST (BTYPE_2 ,BTI_C ,0 ); +TEST (BTYPE_2 ,BTI_J ,1 ); +TEST (BTYPE_2 ,BTI_JC ,0 ); + +TEST (BTYPE_3 ,NOP ,1 ); +TEST (BTYPE_3 ,BTI_N ,1 ); +TEST (BTYPE_3 ,BTI_C ,1 ); +TEST (BTYPE_3 ,BTI_J ,0 ); +TEST (BTYPE_3 ,BTI_JC ,0 ); + +return fail; +}
tests/tcg/aarch64/bti-crt.inc.c new file +51 -0
51 lines not on this page · raw message
tests/tcg/aarch64/Makefile.target +7 -0
@@ -25,6 +25,13 @@ run-pauth-%: QEMU_OPTS += -cpu max run-plugin-pauth-%: QEMU_OPTS += -cpu max endif +# BTI Tests +ifneq ($(DOCKER_IMAGE)$(CROSS_CC_HAS_ARMV8_BTI),) +AARCH64_TESTS += bti-1 +bti-%: CFLAGS += -mbranch-protection=standard +bti-%: LDFLAGS += -nostdlib +endif + # Semihosting smoke test for linux-user AARCH64_TESTS += semihosting run-semihosting: semihosting
tests/tcg/configure.sh +4 -0
@@ -240,6 +240,10 @@ for target in $target_list; do -march=armv8.3-a -o $TMPE $TMPC; then echo "CROSS_CC_HAS_ARMV8_3=y" >> $config_target_mak fi + if do_compiler "$target_compiler" $target_compiler_cflags \ + -mbranch-protection=standard -o $TMPE $TMPC; then + echo "CROSS_CC_HAS_ARMV8_BTI=y" >> $config_target_mak + fi ;; esac
Signature · 1 line
2.25.1
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH v10 0/8] linux-user: User support for AArch64 BTI
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
eaf5edb tests/tcg/aarch64: Add bti smoke test
c74e202 linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes 7e7c534 linux-user/elfload: Move PT_INTERP detection to first loop 1ded2cd linux-user/elfload: Adjust iteration over phdr
9a313b3 linux-user/elfload: Fix coding style in load_elf_image ddc27b7 include/elf: Add defines related to GNU property notes for AArch64 eca4240 linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI 6b3e8e3 linux-user/aarch64: Reset btype for signals
=== OUTPUT BEGIN ===
1/8 Checking commit 6b3e8e369613 (linux-user/aarch64: Reset btype for signals)
2/8 Checking commit eca424067459 (linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI) 3/8 Checking commit ddc27b75549d (include/elf: Add defines related to GNU property notes for AArch64) 4/8 Checking commit 9a313b30265c (linux-user/elfload: Fix coding style in load_elf_image) 5/8 Checking commit 1ded2cdcd8ed (linux-user/elfload: Adjust iteration over phdr)
6/8 Checking commit 7e7c5343dde5 (linux-user/elfload: Move PT_INTERP detection to first loop) 7/8 Checking commit c74e202361a9 (linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes) 8/8 Checking commit eaf5edb50de6 (tests/tcg/aarch64: Add bti smoke test)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#32:
new file mode 100644
ERROR: externs should be avoided in .c files
#117: FILE: tests/tcg/aarch64/bti-crt.inc.c:13:
+int main(void);
total: 1 errors, 1 warnings, 136 lines checked
Patch 8/8 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]