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

update(helpers): add examples for some eBPF helpers #66

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/linux/helper-function/bpf_get_current_comm.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,19 @@ This helper call can be used in the following program types:

### Example

!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome
```c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

SEC("tp/syscalls/sys_enter_open")
int sys_open_trace(void *ctx) {
// TASK_COMM_LEN is defined in vmlinux.h
char comm[TASK_COMM_LEN];
if (bpf_get_current_comm(comm, TASK_COMM_LEN)) {
bpf_printk("Failed to get comm\n");
return 0;
}
bpf_printk("Hello from %s\n", comm);
return 0;
}
```
28 changes: 26 additions & 2 deletions docs/linux/helper-function/bpf_probe_write_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,29 @@ This helper call can be used in the following program types:

### Example

!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome
```c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>

// We do it in the exit to not alter the syscall behavior. The userspace program
// will see the new filename only after the syscall execution.
SEC("fexit/__x64_sys_open")
int BPF_PROG(p_open, struct pt_regs *regs, long ret) {
// If it is our example program overwrite the open path.
struct task_struct *task = (struct task_struct *)bpf_get_current_task_btf();
if (bpf_strncmp(task->comm, TASK_COMM_LEN, "example") != 0) {
return 0;
}

// SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
// first param is the pointer to filename.
void *filename_ptr = (void *)PT_REGS_PARM1_CORE_SYSCALL(regs);
const char filename[16] = "/tmp/new";
if (bpf_probe_write_user(filename_ptr, filename, 16)) {
bpf_printk("Failed to write new filename\n");
}
return 0;
}
```
16 changes: 14 additions & 2 deletions docs/linux/helper-function/bpf_strncmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,17 @@ This helper call can be used in the following program types:

### Example

!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome
```c
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

SEC("tp_btf/sys_enter")
int sys_enter_trace(void *ctx) {
struct task_struct *task = (struct task_struct *)bpf_get_current_task_btf();
if (bpf_strncmp(task->comm, TASK_COMM_LEN, "cat") != 0) {
return 0;
}
bpf_printk("Hello, I'm a cat!\n");
return 0;
}
```
Loading