August 4th, 2024

Writing a system call tracer using eBPF

A system call tracer using eBPF technology has been developed to replicate strace functionalities, focusing on common system calls and enhancing system-level interaction monitoring in Linux.

Read original articleLink Icon
Writing a system call tracer using eBPF

The article discusses the development of a system call tracer using eBPF (Extended Berkeley Packet Filter) technology, which allows users to run custom programs within the Linux kernel. The tracer aims to replicate functionalities similar to the well-known tool strace, focusing on a limited set of common system calls. Key concepts include tracepoints, which are specific instrumentation points in the kernel, and eBPF maps, which facilitate data sharing between eBPF programs and user-space applications. The implementation involves writing eBPF programs that respond to system call entry and exit events, capturing system call arguments and return values. The loader program is responsible for setting up the environment, spawning a child process to execute the target program, and managing the eBPF programs. The article provides code snippets for defining data structures, writing eBPF programs, and handling system call logging. The final implementation allows for monitoring system calls made by a specified process, enhancing the ability to trace and debug system-level interactions in Linux.

- eBPF enables running custom programs in the Linux kernel for enhanced safety and maintainability.

- The tracer developed mimics the functionality of strace, focusing on a subset of system calls.

- Key components include tracepoints for hooking into kernel events and eBPF maps for data sharing.

- The implementation involves writing eBPF programs for system call entry and exit events.

- The loader program manages the execution environment and attaches eBPF programs to trace system calls.

Related

How eBPF is shaping the future of Linux and platform engineering

How eBPF is shaping the future of Linux and platform engineering

eBPF, developed by Daniel Borkmann, revolutionizes Linux by enabling custom programs in the kernel. It enhances networking, security, and observability, bridging monolithic and microkernel architectures for improved performance and flexibility.

Show HN: Xcapture-BPF – like Linux top, but with Xray vision

Show HN: Xcapture-BPF – like Linux top, but with Xray vision

0x.tools simplifies Linux application performance analysis without requiring upgrades or heavy frameworks. It offers thread monitoring, CPU usage tracking, system call analysis, and kernel wait location identification. The xcapture-bpf tool enhances performance data visualization through eBPF. Installation guides are available for RHEL 8.1 and Ubuntu 24.04.

Hiding Linux Processes with Bind Mounts

Hiding Linux Processes with Bind Mounts

The article explains a technique for hiding Linux processes using bind mounts, making them undetectable by standard tools. It highlights the method's implications for both offensive and defensive cybersecurity strategies.

eBPF Offensive Capabilities – Get Ready for Next-Gen Malware (2023)

eBPF Offensive Capabilities – Get Ready for Next-Gen Malware (2023)

eBPF technology in the Linux kernel allows low-level program execution, posing security risks as attackers can exploit its features. Safeguards exist, but ongoing vigilance is essential to mitigate potential abuses.

Logging C function calls with cosmopolitan Libc (2022)

Logging C function calls with cosmopolitan Libc (2022)

The Cosmopolitan Libc runtime's --ftrace flag logs C function calls, aiding debugging by tracing execution history and identifying issues like NULL pointers, while also supporting --strace for system call logging.

Link Icon 2 comments
By @khuey - 9 months
This example doesn't really gain much by using eBPF. The tracepoint machinery and perf_event_open is perfectly capable of recording pids and registers at syscall entry/exit (via PERF_SAMPLE_TID and PERF_SAMPLE_REGS_USER) into a ring buffer. `perf trace` does that today and it can be a useful replacement for strace in situations where strace disturbs the program's timing too much or otherwise cannot be used (e.g. you want to strace something that's already being ptraced by another process).

Where eBPF is powerful is that it allows you to extend the tracepoint ability to grab more complicated system call arguments. The first register argument to open(2) for instance, is a pointer to the filename to open. Merely reporting the registers is largely useless, the tracer needs to chase the pointer and report the string too. An eBPF filter can be used to recognize that the tracepoint is at an open(2) and to read the userspace memory that the relevant register points to. This enables a "full" strace replacement without using ptrace at all. There's some ongoing work to add this capability to `perf trace`.