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

feat(events): add stack_pivot event #4403

Merged
merged 3 commits into from
Jan 15, 2025
Merged

Conversation

oshaked1
Copy link
Contributor

@oshaked1 oshaked1 commented Dec 2, 2024

Closes #4404

1. Explain what the PR does

Add stack_pivot event, which detects usage of the stack pivoting technique as part of a ROP exploit, by monitoring selected syscalls and verifying the location of the stack pointer.

2. Explain how to test it

Run tracee as follows:

sudo dist/tracee -e stack_pivot.args.syscall=exit_group

Compile and run the tester program:

gcc -o stack_pivot tests/e2e-inst-signatures/scripts/stack_pivot.c
./stack_pivot

After 15 seconds a stack pivot event should be triggered.

3. Other comments

Like suspicious_syscall_source, this event makes use of event parameters to determine which syscalls should be monitored. The new probe group created for suspicious_syscall_source was made generic for "syscall checker" events, and each time a probe is triggered only the "checkers" which selected that syscall are run.

pkg/ebpf/c/maps.h Outdated Show resolved Hide resolved
pkg/ebpf/c/tracee.bpf.c Outdated Show resolved Hide resolved
pkg/events/core.go Outdated Show resolved Hide resolved
Comment on lines 5288 to 5343
const char *vma_type_str = get_vma_type_str(get_vma_type(task, vma));
unsigned long vma_start = BPF_CORE_READ(vma, vm_start);
unsigned long vma_size = BPF_CORE_READ(vma, vm_end) - vma_start;
unsigned long vma_flags = BPF_CORE_READ(vma, vm_flags);

switch (vma_type) {
case VMA_STACK:
vma_type_str = "main stack";
break;
case VMA_THREAD_STACK:
vma_type_str = "thread stack";
break;
case VMA_HEAP:
vma_type_str = "heap";
break;
case VMA_GOLANG_HEAP:
// Goroutine stacks are allocated on the golang heap
vma_type_str = "golang heap/stack";
break;
case VMA_ANON:
vma_type_str = "anonymous";
break;
case VMA_VDSO:
vma_type_str = "vdso";
break;
default:
vma_type_str = "unknown";
break;
}
save_to_submit_buf(&p.event->args_buf, &syscall, sizeof(syscall), 0);
save_to_submit_buf(&p.event->args_buf, &ip, sizeof(ip), 1);
save_str_to_buf(&p.event->args_buf, (void *) vma_type_str, 2);
save_to_submit_buf(&p.event->args_buf, &vma_start, sizeof(vma_start), 3);
save_to_submit_buf(&p.event->args_buf, &vma_size, sizeof(vma_size), 4);
save_to_submit_buf(&p.event->args_buf, &vma_flags, sizeof(vma_flags), 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your commits are mixing stack_pivot and suspicious_syscalls events.
This commit states it adds the stack pivot event, however, it also modifies logic of the suspicious syscall event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I changed is moving the logic to get vma_type_str into a separate function (because it's used by check_stack_pivot as well).

@oshaked1 oshaked1 force-pushed the stack_pivot branch 2 times, most recently from 912719f to 8e06ee3 Compare January 15, 2025 15:13
Convert `check_syscall_source` to use this probe instead of having a dedicated probe.
The attachment mechansim is also generic and supports the registration of future syscall checkers.
Golang heaps can be determined by a pattern in the address, dictated by address hints supplied to mmap while allocating memory for them.
Thread stacks can be identified by tracking the stack VMA for all newly created threads.
This event detects usage of the stack pivot technique used during ROP exploits by checking the user's stack pointer at selected syscalls.
If the stack pointer does not point to the stack, an event is triggered.
Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanivagman yanivagman merged commit c97369d into aquasecurity:main Jan 15, 2025
41 checks passed
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 stack_pivot event
2 participants