Let's analyze x86-64 Linux system calls!
Beginner Hint 1: The Calling Convention for System Calls and GCC's Extended Inline Assembly
- The calling convention for system calls on x86-64 Linux is described in the
A.2.1 Calling Conventionssection of theSystem V Application Binary Interface AMD64 Architecture Processor Supplementwhich is available at https://gitlab.com/x86-psABIs/x86-64-ABI, and https://man7.org/linux/man-pages/man2/syscall.2.html.- To execute a system call, we need to specify a system call number to the
raxregister, and up to six arguments to therdi,rsi,rdx,r10,r8, andr9registers sequentially, and then execute asyscallinstruction. - The result of the system call execution is stored in the
raxregister.
- To execute a system call, we need to specify a system call number to the
- GCC's extended inline assembly syntax is described at https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html and https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html
- The extended inline assembly used for the first
syscallinstruction in this challenge specifies the following:- A
OutputOperandsparameter is unspecified. - A
InputOperandsparameter set318in theraxregister,&result_1in therdiregister,sizeof(result_1)in thersiregister, and0in therdxregister, before executing the extended inline assembly. - A
Clobbersparameter specifies that thercxregister,r11register, and memory will be modified during the execution of the extended inline assembly.- Note that a
syscallinstruction itself modifies thercxandr11registers.
- Note that a
- A
- The extended inline assembly used for the second
syscallinstruction in this challenge specifies the following:- A
OutputOperandsparameter specifies that a content of theraxregister are assigned toresult_2after the extended inline assembly executes. - A
InputOperandsparameter set102in theraxregister before executing the extended inline assembly. - A
Clobbersparameter specifies that thercxregister,r11register will be modified during the execution of the extended inline assembly.
- A
- The extended inline assembly used for the first
Beginner Hint 2: Approach to Solving the Challenge
- There are several ways to approach this challenge.
- One ways is to look up the system call numbers for x86-64 Linux and check the arguments and return values for the corresponding system call.
- Another way is to use a decompiler.
- Some decompilers can perfectly decompile syscall calls from the start, while others produce poor decompiled results initially but can be perfectly decompiled with additional operations.