Welcome back. In our last lesson, we learned how to use the /proc filesystem to inspect a running process's memory layout. This gave us a static snapshot—a map of what the process's address space looks like at a moment in time. Today, we shift our focus from the static structure to the dynamic link between a program and the operating system: the system call, or syscall.
This lesson will explain precisely how a user-space program transitions into the kernel to request services it cannot perform on its own. Understanding this mechanism is fundamental to binary exploitation, as many exploits culminate in making a specific syscall, such as execve, to take control of a system. We will demystify the concepts of user mode and kernel mode and trace the exact path a request takes from a line of C code to the heart of the Linux kernel.
The Need for Separation: User Mode vs. Kernel Mode
First, we need to answer a fundamental question: why can't a user program simply perform any action it wants? Why can't a simple C program directly write to a hard drive sector or manipulate network hardware? The answer lies in a crucial security principle enforced by the CPU itself: privilege separation.
Modern operating systems are designed to be multi-tasking and multi-user. To prevent a faulty or malicious program from crashing the entire system or spying on other processes, the CPU provides different operational modes. Your programs run in a restricted "user mode" (often called Ring 3 on x86 architectures), while the operating system kernel runs in a privileged "kernel mode" (Ring 0).
The following video provides an excellent conceptual overview of this hardware-enforced separation.
How a Single Bit Inside Your Processor Shields Your Operating System's Integrity
This video from Core Dumped explains the hardware basis for privilege separation, using the idea of a 'mode bit' and interrupts.
First, watch the segment explaining privileged instructions and the mode bit to understand how the CPU enforces restrictions. Then, watch the explanation of kernel mode vs. user mode to see what kinds of operations are exclusive to the kernel. This establishes why we need a special mechanism to ask the kernel for help.
As you saw, operations like direct hardware I/O and manipulation of memory management tables are privileged instructions that can only be executed in kernel mode. A user program running in user mode that attempts to execute a privileged instruction will trigger a hardware exception, and the OS will likely terminate the program.
So, if a program needs to do something like read a file or send a network packet, it must have a legitimate, controlled way to ask the kernel to perform the action on its behalf. This is the role of the system call.
The System Call: A Bridge to the Kernel
A system call is the programmatic interface to the kernel. From a developer's perspective, it often looks like a normal function call (e.g., write(...)), but behind the scenes, a special mechanism is at work to transition the CPU from user mode to kernel mode.
Generally, you don't invoke syscalls directly. Instead, you call wrapper functions in the standard C library (glibc). These wrappers handle the low-level, architecture-specific details needed to trigger the mode switch. The strace utility is an excellent tool for observing these syscalls in action. For example, if you run strace echo "hello", you'll see a write syscall, not printf.
The following video provides a practical demonstration, tracing a call to write() down to the assembly instruction that enters the kernel.
Syscalls, Kernel vs. User Mode and Linux Kernel Source Code - bin 0x09
LiveOverflow provides a hands-on walkthrough, using radare2 to trace a syscall and explaining the underlying hardware interactions.
Watch from the beginning to about the five-minute mark. Pay close attention to the following points: The distinction between a library function like printf and the underlying write syscall. Following the function call through the PLT and into glibc. The final, crucial instructions: loading the syscall number into the eax register and executing the syscall instruction. The explanation of the IA32_LSTAR Model-Specific Register (MSR), which tells the CPU where to jump in the kernel.
The x86-64 SYSCALL Mechanism
The video gave you a great visual tour. Now, let's formalize the steps for making a syscall on a modern 64-bit Linux system. This process is a well-defined Application Binary Interface (ABI).
- Syscall Number: The specific syscall to be executed is identified by a number. This number is placed in the
RAXregister. For example, on x86-64,readis syscall 0,writeis 1, andexecveis 59. - Arguments: The arguments for the syscall are passed in a specific sequence of registers:
RDI,RSI,RDX,R10,R8, andR9. - Instruction: The user-space program executes the
SYSCALLinstruction. - Hardware Transition: This special instruction causes the CPU to perform several actions atomically:
- It saves the address of the instruction after
SYSCALLinto theRCXregister (this is the return address). - It saves the user-space flags register (
RFLAGS) intoR11. - It reads the target instruction pointer from a special Model-Specific Register (MSR) called
MSR_LSTARand jumps to that address. - It switches the CPU from user mode (Ring 3) to kernel mode (Ring 0).
- It saves the address of the instruction after
The MSR_LSTAR register is configured by the kernel once during boot-up to point to its official syscall entry point. This is how the kernel ensures that user-space code can only enter it at a single, controlled location.
The following article provides a definitive, step-by-step description of this flow.
Anatomy of a system call, part 1
This article from LWN.net provides an authoritative explanation of the x86-64 syscall invocation path.
Please read the section titled "x86_64 syscall invocation". Focus on the final paragraph which begins "And this gives us all we need". This paragraph beautifully summarizes the entire user-to-kernel transition, connecting the user-space register setup to the kernel's entry point via the SYSCALL instruction and the MSR_LSTAR register.
Inside the Kernel: Handling the Syscall
Once the SYSCALL instruction has transferred control to the kernel's entry point (the function system_call in entry_64.S), the kernel's work begins.

The first thing the kernel must do is save the state of the user-space process so it can be resumed later. This involves saving all the general-purpose registers onto a special, per-process kernel stack.
The image below details the first few assembly instructions of the kernel's syscall entry handler, entry_SYSCALL_64. Given your assembly background, this should provide a concrete view of the initial steps.

After saving the user-space state, the kernel can proceed with the actual request.
- Dispatching: The kernel reads the syscall number from the
RAXregister. It uses this number as an index into an array of function pointers called thesys_call_table. - Executing: It calls the function pointer found in the table. This function (e.g.,
sys_read,sys_write) is the kernel's implementation of the requested service. - Returning: Once the kernel function completes, it places its return value (e.g., number of bytes read, or an error code) back into the
RAXregister. - Restoring State: The kernel executes the
SYSRETinstruction. This instruction is the counterpart toSYSCALL. It restores the user-space state (instruction pointer fromRCX, flags fromR11, etc.) and switches the CPU back to user mode (Ring 3).
Execution seamlessly resumes in the user-space program at the instruction immediately following the original SYSCALL.
The following reading explains this kernel-side machinery, including how developers define syscall handlers and how the sys_call_table is constructed.
The Definitive Guide to Linux System Calls
This article from the Packagecloud blog provides deep technical details on various system call methods. We will focus on the modern 64-bit implementation.
Please read the subsection titled "64-bit fast system calls". Start from the introduction and continue through "Using syscall system calls with your own assembly" to see how to manually make a syscall. Then, jump to the section "Kernel-side: syscall entry point" to understand how the sys_call_table is used. Finally, read the brief section on "Returning from a syscall system call with sysret".
Conclusion
In this lesson, we've journeyed from user space to kernel space and back again, demystifying the system call mechanism. This is the fundamental process by which all meaningful interactions between a program and the operating system occur.
Key Takeaways:
- The CPU enforces privilege separation through user mode (Ring 3) and kernel mode (Ring 0) to protect the system.
- To request privileged services like I/O, a user program must perform a system call.
- On x86-64, a syscall is initiated by loading the syscall number into
RAXand arguments into other registers, then executing theSYSCALLinstruction. - The
SYSCALLinstruction causes a hardware-managed transition to kernel mode, jumping to an entry point predefined by the kernel in theMSR_LSTARregister. - The kernel saves the user process's state, uses the syscall number in
RAXto find the correct handler in thesys_call_table, executes the request, and returns control using theSYSRETinstruction.
Now that you understand the "how" of system calls, our next lesson will focus on the "what." We will explore some of the most common and powerful syscalls used in exploitation, such as execve, mmap, mprotect, read, and write, and discuss why they are the essential building blocks for crafting payloads.