Skip to content

Commit 0a13c24

Browse files
authored
Merge pull request #1661 from retrocpugeek/fix/cpu-exception-sigill
Terminate the emulated process on an illegal-instruction CPU exception
2 parents 1e0df0b + 5d2835a commit 0a13c24

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

qiling/arch/mips_const.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44
#
55

6+
from enum import IntEnum
7+
68
from unicorn.mips_const import *
79

10+
11+
class EXCP(IntEnum):
12+
# subset of QEMU's MIPS exception codes, as reported to unicorn interrupt hooks
13+
SYSCALL = 17 # system call
14+
BREAK = 18 # breakpoint
15+
RI = 20 # reserved (illegal) instruction
16+
817
reg_map = {
918
"r0": UC_MIPS_REG_0,
1019
"r1": UC_MIPS_REG_1,

qiling/os/linux/linux.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from qiling.arch.x86_const import GS_SEGMENT_ADDR, GS_SEGMENT_SIZE
1313
from qiling.arch.x86_utils import GDTManager, SegmentManager86, SegmentManager64
1414
from qiling.arch import arm_utils
15+
from qiling.arch.cortex_m_const import EXCP
16+
from qiling.arch.mips_const import EXCP as MIPS_EXCP
1517
from qiling.cc import QlCC, intel, arm, mips, riscv, ppc
1618
from qiling.const import QL_ARCH, QL_OS
1719
from qiling.os.fcall import QlFunctionCall
@@ -56,7 +58,8 @@ def load(self):
5658
# ARM
5759
if self.ql.arch.type == QL_ARCH.ARM:
5860
self.ql.arch.enable_vfp()
59-
self.ql.hook_intno(self.hook_syscall, 2)
61+
self.ql.hook_intno(self.hook_syscall, EXCP.SWI)
62+
self.ql.hook_intno(self.hook_cpu_exception, EXCP.UDEF)
6063
self.thread_class = thread.QlLinuxARMThread
6164
arm_utils.init_linux_traps(self.ql, {
6265
'memory_barrier': 0xffff0fa0,
@@ -66,13 +69,15 @@ def load(self):
6669

6770
# MIPS32
6871
elif self.ql.arch.type == QL_ARCH.MIPS:
69-
self.ql.hook_intno(self.hook_syscall, 17)
72+
self.ql.hook_intno(self.hook_syscall, MIPS_EXCP.SYSCALL)
73+
self.ql.hook_intno(self.hook_cpu_exception, MIPS_EXCP.RI)
7074
self.thread_class = thread.QlLinuxMIPS32Thread
7175

7276
# ARM64
7377
elif self.ql.arch.type == QL_ARCH.ARM64:
7478
self.ql.arch.enable_vfp()
75-
self.ql.hook_intno(self.hook_syscall, 2)
79+
self.ql.hook_intno(self.hook_syscall, EXCP.SWI)
80+
self.ql.hook_intno(self.hook_cpu_exception, EXCP.UDEF)
7681
self.thread_class = thread.QlLinuxARM64Thread
7782

7883
# X86
@@ -137,6 +142,24 @@ def setup_procfs(self):
137142
def hook_syscall(self, ql, intno = None):
138143
return self.load_syscall()
139144

145+
def hook_cpu_exception(self, ql, intno = None):
146+
# A cpu exception the kernel would turn into a fatal signal that
147+
# terminates the process (e.g. SIGILL on an undefined instruction).
148+
# Emulate that termination by stopping cleanly instead of letting the
149+
# unhandled-interrupt dispatcher raise QlErrorCoreHook. This commonly
150+
# happens with shellcode that falls through into trailing data once a
151+
# terminal syscall (e.g. a denied execve) returns instead of replacing
152+
# the image.
153+
signame = {
154+
EXCP.UDEF: 'SIGILL', # ARM / ARM64 undefined instruction
155+
MIPS_EXCP.RI: 'SIGILL', # MIPS reserved (illegal) instruction
156+
}.get(intno, f'exception {intno:#x}')
157+
158+
pc = ql.arch.regs.arch_pc
159+
160+
ql.log.debug(f'CPU raised {signame} at {pc:#x}; terminating emulated process')
161+
ql.stop()
162+
140163
def register_function_after_load(self, function):
141164
if function not in self.function_after_load_list:
142165
self.function_after_load_list.append(function)

tests/test_shellcode.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ def test_linux_arm64(self):
123123
ql.os.set_syscall('execve', graceful_execve, QL_INTERCEPT.EXIT)
124124
ql.run()
125125

126+
# the tests above end the emulation from the execve hook, so they never reach
127+
# the shellcode's trailing '/bin/sh' string. the two below deliberately let
128+
# execve return and fall through into it: those bytes are not valid code, so
129+
# the cpu raises an undefined/reserved instruction exception. the kernel would
130+
# deliver a fatal SIGILL and terminate the process, and qiling must do the
131+
# same. before this fix nothing hooked those exceptions, so the unhandled
132+
# interrupt was turned into QlErrorCoreHook and ql.run() blew up.
133+
def _run_past_execve(self, code: bytes, archtype: QL_ARCH):
134+
def returning_execve(ql: Qiling, pathname: int, argv: int, envp: int, retval: int):
135+
assert retval != 0, 'execve is not expected to return on success'
136+
137+
ql = Qiling(code=code, archtype=archtype, ostype=QL_OS.LINUX, verbose=QL_VERBOSE.OFF)
138+
ql.os.set_syscall('execve', returning_execve, QL_INTERCEPT.EXIT)
139+
ql.run()
140+
141+
def test_linux_arm64_illegal_instruction(self):
142+
print("Linux ARM 64bit illegal instruction")
143+
self._run_past_execve(ARM64_LIN, QL_ARCH.ARM64)
144+
145+
def test_linux_mips32_illegal_instruction(self):
146+
print("Linux MIPS 32bit EL illegal instruction")
147+
self._run_past_execve(MIPS32EL_LIN, QL_ARCH.MIPS)
148+
126149
# #This shellcode needs to be changed to something simpler not requiring rootfs
127150
# def test_windows_x86(self):
128151
# print("Windows X86 32bit Shellcode")

0 commit comments

Comments
 (0)