Skip to content

Commit a1d5eba

Browse files
Matthew Leachctmarinas
authored andcommitted
arm64: big-endian: don't treat code as data when copying sigret code
Currently the sigreturn compat code is copied to an offset in the vectors table. When using a BE kernel this data will be stored in the wrong endianess so when returning from a signal on a 32-bit BE system, arbitrary code will be executed. Instead of declaring the code inside a struct and copying that, use the assembler's .byte directives to store the code in the correct endianess regardless of platform endianess. Acked-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Matthew Leach <matthew.leach@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 55b8954 commit a1d5eba

3 files changed

Lines changed: 46 additions & 29 deletions

File tree

‎arch/arm64/kernel/kuser32.S‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
*
2828
* See Documentation/arm/kernel_user_helpers.txt for formal definitions.
2929
*/
30+
31+
#include <asm/unistd32.h>
32+
3033
.align 5
3134
.globl __kuser_helper_start
3235
__kuser_helper_start:
@@ -75,3 +78,42 @@ __kuser_helper_version: // 0xffff0ffc
7578
.word ((__kuser_helper_end - __kuser_helper_start) >> 5)
7679
.globl __kuser_helper_end
7780
__kuser_helper_end:
81+
82+
/*
83+
* AArch32 sigreturn code
84+
*
85+
* For ARM syscalls, the syscall number has to be loaded into r7.
86+
* We do not support an OABI userspace.
87+
*
88+
* For Thumb syscalls, we also pass the syscall number via r7. We therefore
89+
* need two 16-bit instructions.
90+
*/
91+
.globl __aarch32_sigret_code_start
92+
__aarch32_sigret_code_start:
93+
94+
/*
95+
* ARM Code
96+
*/
97+
.byte __NR_compat_sigreturn, 0x70, 0xa0, 0xe3 // mov r7, #__NR_compat_sigreturn
98+
.byte __NR_compat_sigreturn, 0x00, 0x00, 0xef // svc #__NR_compat_sigreturn
99+
100+
/*
101+
* Thumb code
102+
*/
103+
.byte __NR_compat_sigreturn, 0x27 // svc #__NR_compat_sigreturn
104+
.byte __NR_compat_sigreturn, 0xdf // mov r7, #__NR_compat_sigreturn
105+
106+
/*
107+
* ARM code
108+
*/
109+
.byte __NR_compat_rt_sigreturn, 0x70, 0xa0, 0xe3 // mov r7, #__NR_compat_rt_sigreturn
110+
.byte __NR_compat_rt_sigreturn, 0x00, 0x00, 0xef // svc #__NR_compat_rt_sigreturn
111+
112+
/*
113+
* Thumb code
114+
*/
115+
.byte __NR_compat_rt_sigreturn, 0x27 // svc #__NR_compat_rt_sigreturn
116+
.byte __NR_compat_rt_sigreturn, 0xdf // mov r7, #__NR_compat_rt_sigreturn
117+
118+
.globl __aarch32_sigret_code_end
119+
__aarch32_sigret_code_end:

‎arch/arm64/kernel/signal32.c‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,6 @@ struct compat_rt_sigframe {
100100

101101
#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
102102

103-
/*
104-
* For ARM syscalls, the syscall number has to be loaded into r7.
105-
* We do not support an OABI userspace.
106-
*/
107-
#define MOV_R7_NR_SIGRETURN (0xe3a07000 | __NR_compat_sigreturn)
108-
#define SVC_SYS_SIGRETURN (0xef000000 | __NR_compat_sigreturn)
109-
#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | __NR_compat_rt_sigreturn)
110-
#define SVC_SYS_RT_SIGRETURN (0xef000000 | __NR_compat_rt_sigreturn)
111-
112-
/*
113-
* For Thumb syscalls, we also pass the syscall number via r7. We therefore
114-
* need two 16-bit instructions.
115-
*/
116-
#define SVC_THUMB_SIGRETURN (((0xdf00 | __NR_compat_sigreturn) << 16) | \
117-
0x2700 | __NR_compat_sigreturn)
118-
#define SVC_THUMB_RT_SIGRETURN (((0xdf00 | __NR_compat_rt_sigreturn) << 16) | \
119-
0x2700 | __NR_compat_rt_sigreturn)
120-
121-
const compat_ulong_t aarch32_sigret_code[6] = {
122-
/*
123-
* AArch32 sigreturn code.
124-
* We don't construct an OABI SWI - instead we just set the imm24 field
125-
* to the EABI syscall number so that we create a sane disassembly.
126-
*/
127-
MOV_R7_NR_SIGRETURN, SVC_SYS_SIGRETURN, SVC_THUMB_SIGRETURN,
128-
MOV_R7_NR_RT_SIGRETURN, SVC_SYS_RT_SIGRETURN, SVC_THUMB_RT_SIGRETURN,
129-
};
130-
131103
static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
132104
{
133105
compat_sigset_t cset;

‎arch/arm64/kernel/vdso.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ static struct page *vectors_page[1];
5858
static int alloc_vectors_page(void)
5959
{
6060
extern char __kuser_helper_start[], __kuser_helper_end[];
61+
extern char __aarch32_sigret_code_start[], __aarch32_sigret_code_end[];
62+
6163
int kuser_sz = __kuser_helper_end - __kuser_helper_start;
64+
int sigret_sz = __aarch32_sigret_code_end - __aarch32_sigret_code_start;
6265
unsigned long vpage;
6366

6467
vpage = get_zeroed_page(GFP_ATOMIC);
@@ -72,7 +75,7 @@ static int alloc_vectors_page(void)
7275

7376
/* sigreturn code */
7477
memcpy((void *)vpage + AARCH32_KERN_SIGRET_CODE_OFFSET,
75-
aarch32_sigret_code, sizeof(aarch32_sigret_code));
78+
__aarch32_sigret_code_start, sigret_sz);
7679

7780
flush_icache_range(vpage, vpage + PAGE_SIZE);
7881
vectors_page[0] = virt_to_page(vpage);

0 commit comments

Comments
 (0)