我编写了一个简单的ebpf程序(使用libbpf),在其中我钩住了sendto syscall。
libbpf版本:
SEC("kprobe/sendto")
int BPF_KPROBE(entry_sendto, int sockfd, char* buf, size_t len)
{
bpf_printk("libbpf - entry_sendto - 0 %p", ctx);
bpf_printk("libbpf - entry_sendto - 1 %p", PT_REGS_PARM1(ctx));
bpf_printk("libbpf - entry_sendto - 2 %p", PT_REGS_PARM2(ctx));
bpf_printk("libbpf - entry_sendto - 3 %p", PT_REGS_PARM3(ctx));
bpf_printk("libbpf - entry_sendto - 1 %d", (int)PT_REGS_PARM1(ctx));
bpf_printk("libbpf - entry_sendto - 2 %s", (char *)PT_REGS_PARM2(ctx));
bpf_printk("libbpf - entry_sendto - 3 %d", (int)PT_REGS_PARM3(ctx));
bpf_printk("libbpf - entry_sendto params - 1 %d", sockfd);
bpf_printk("libbpf - entry_sendto params - 2 %s", buf);
bpf_printk("libbpf - entry_sendto params - 3 %d", len);
return 0;
}bcc版本:
int syscall__probe_entry_sendto(struct pt_regs* ctx, int sockfd, char* buf, size_t len, int flags,
const struct sockaddr* dest_addr, size_t addrlen) {
bpf_trace_printk("bcc - entry_sendto - 0 %p", ctx);
bpf_trace_printk("bcc - entry_sendto - 1 %p", PT_REGS_PARM1(ctx));
bpf_trace_printk("bcc - entry_sendto - 2 %p", PT_REGS_PARM2(ctx));
bpf_trace_printk("bcc - entry_sendto - 3 %p", PT_REGS_PARM3(ctx));
bpf_trace_printk("bcc - entry_sendto - 1 %d", (int)PT_REGS_PARM1(ctx));
bpf_trace_printk("bcc - entry_sendto - 2 %s", (char *)PT_REGS_PARM2(ctx));
bpf_trace_printk("bcc - entry_sendto - 3 %d", (int)PT_REGS_PARM3(ctx));
bpf_trace_printk("bcc - entry_sendto params - 1 %d", sockfd);
bpf_trace_printk("bcc - entry_sendto params - 2 %s", buf);
bpf_trace_printk("bcc - entry_sendto params - 3 %d", len);
return 0;
}我运行了一个简单的curl请求来检查钩子,得到了以下内容:
curl-49713 [002] d... 15631.753730: bpf_trace_printk: libbpf - entry_sendto - 0 00000000eca092cd
curl-49713 [002] d... 15631.753731: bpf_trace_printk: libbpf - entry_sendto - 1 00000000bfcdc9b6
curl-49713 [002] d... 15631.753731: bpf_trace_printk: libbpf - entry_sendto - 2 0000000000000000
curl-49713 [002] d... 15631.753731: bpf_trace_printk: libbpf - entry_sendto - 3 ffffffffffffffff
curl-49713 [002] d... 15631.753732: bpf_trace_printk: libbpf - entry_sendto - 1 67403608
curl-49713 [002] d... 15631.753733: bpf_trace_printk: libbpf - entry_sendto - 2
curl-49713 [002] d... 15631.753734: bpf_trace_printk: libbpf - entry_sendto - 3 -1
curl-49713 [002] d... 15631.753735: bpf_trace_printk: libbpf - entry_sendto params - 1 67403608
curl-49713 [002] d... 15631.753736: bpf_trace_printk: libbpf - entry_sendto params - 2
curl-49713 [002] d... 15631.753736: bpf_trace_printk: libbpf - entry_sendto params - 3 -1
curl-49713 [002] d... 15631.753737: bpf_trace_printk: bcc - entry_sendto - 0 00000000eca092cd
curl-49713 [002] d... 15631.753737: bpf_trace_printk: bcc - entry_sendto - 1 00000000bfcdc9b6
curl-49713 [002] d... 15631.753738: bpf_trace_printk: bcc - entry_sendto - 2 0000000000000000
curl-49713 [002] d... 15631.753738: bpf_trace_printk: bcc - entry_sendto - 3 ffffffffffffffff
curl-49713 [002] d... 15631.753738: bpf_trace_printk: bcc - entry_sendto - 1 67403608
curl-49713 [002] d... 15631.753739: bpf_trace_printk: bcc - entry_sendto - 2
curl-49713 [002] d... 15631.753739: bpf_trace_printk: bcc - entry_sendto - 3 -1
curl-49713 [002] d... 15631.753740: bpf_trace_printk: bcc - entry_sendto params - 1 6
curl-49713 [002] d... 15631.753740: bpf_trace_printk: bcc - entry_sendto params - 2 8000
curl-49713 [002] d... 15631.753740: bpf_trace_printk: bcc - entry_sendto params - 3 1我不明白如何读取libbpf中sendto系统的参数。ctx结构在BCC钩子和libbpf钩子之间是相同的(至少按地址排列)。
你看到我做错什么了吗?我错过什么了吗?任何帮助都将不胜感激!
发布于 2022-06-05 14:16:15
如果您的内核启用了CONFIG_ARCH_HAS_SYSCALL_WRAPPER,那么ctx将被包装两次。而bcc专门处理函数args:https://github.com/iovisor/bcc/commit/2da34267fcae4485f4e05a17521214749f6f0edd。
https://stackoverflow.com/questions/69842674
复制相似问题