Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_syscall_source event #3953

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

oshaked1
Copy link
Contributor

@oshaked1 oshaked1 commented Apr 3, 2024

1. Explain what the PR does

Add a new event, check_syscall_source, which reports invocations of syscalls from unusual code locations (stack, heap and anonymous VMAs).
It is implemented using a new tail call placed in sys_enter, which triggers the analysis only for syscalls that were selected using a filter, e.g. tracee -e check_syscall_source.args.syscall=open,openat.
Closes #4002

2. Explain how to test it

The easiest way to test the event is using the included tester program, which executes a small shellcode that invokes the exit syscall from the stack/heap/anonymous VMA according to the command line argument. Note that it must be compiled with -z execstack to be able to run the shellcode from the stack.

Another way of testing the event (for arbitrary syscalls) is by packing a program:

# compile statically, so libc syscall wrappers are not loaded to separate (legitimate) code areas
gcc -o my_program -static my_program.c

# pack the program
upx -o my_program_upx my_program

# run it
./my_program_upx

@oshaked1
Copy link
Contributor Author

oshaked1 commented Apr 3, 2024

The ebpf code for this event contains a call to the bpf_find_vma helper if it exists (an alternative logic is implemented for older kernels where the helper doesn't exist).

Because this helper uses a callback function or in verifier terms a "bpf-to-bpf call", this program cannot be combined with tail calls (which it uses) on some older kernels, even though the helper call would not be called on those kernels.

As a result, this event must be split into 2 separate programs that will be loaded conditionally based on kernel version. Currently no such mechanism exists, but is in the works. When such a mechanism will be implemented this event will be split into 2 programs so it can be merged.

EDIT

The logic for newer kernels which uses the bpf_find_vma call was removed, so the event will now only work on kernel version < 6.1. When conditional program loading will be implemented, the logic for newer kernels will be added as a separate program.

@oshaked1 oshaked1 force-pushed the syscall_checker branch 3 times, most recently from c45b5fc to a6c4e35 Compare April 4, 2024 15:17
@oshaked1 oshaked1 marked this pull request as ready for review April 7, 2024 11:22
@oshaked1 oshaked1 force-pushed the syscall_checker branch 4 times, most recently from d90fe4e to 3a29a8c Compare May 12, 2024 18:53
This event checks, for preselected syscalls, the VMA from which the syscall was called.
It determines if the VMA is the stack, the heap or an anonymous VMA (these are unusual for syscalls to be called from)
and submits an event if so.
A unique source is identified by a combination of process (tgid and group leader start time),
vma address and syscall number.
Currently there are no supported platforms without the
CONFIG_ARCH_HAS_SYSCALL_WRAPPER configuration.
Added cache flushes to prevent issues on ARM CPUs
Simplified shellcode to a simple exit(0).
Filtering on check_syscall_source.args.syscall results in the selected syscalls being added
to the tail call map for the check_syscall program.
A recent performance change (f806cb4)
results in task info not being populated until the event is submitted.
Init events should be created and submitted even if they should not be emitted.
The usage of this helper prevents the program from being loaded in older kernels,
which prevents tracee from running.
The alternative VMA lookup logic works only for RB trees (pre 6.1),
so this event simply does not generate any output on newer kernels.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add check_syscall_source event
2 participants