gpt4 book ai didi

linux-kernel - 如何使用 BPF 过滤核函数参数?

转载 作者:行者123 更新时间:2023-12-04 04:58:47 25 4
gpt4 key购买 nike

如何使用 Berkeley Packet Filter (BPF) 过滤内核中的函数参数?该函数应该是任何非内联函数,而不仅仅是系统调用。此外,最好可以取消引用函数参数中的指针以进行验证。

我搜索了互联网,但找不到任何用例。大多数 Material 只描述了如何使用 seccomp/seccomp-BPF。

似乎集成了 eBPF 和 kprobe/jprobe 来实现 Hook 。但是我在网上找不到很好的例子。

最佳答案

eBPF 可能是你想要的。如果您还没有找到它们,您应该查看 the bcc (BPF Compiler Collection) tools 提供的示例。 .

特别是the example tool argdist 确实依赖于 kprobes 并且您可能会感兴趣:

argdist probes functions you specify and collects parameter values into a histogram or a frequency count. This can be used to understand the distribution of values a certain parameter takes, filter and print interesting parameters without attaching a debugger, and obtain general execution statistics on various functions.

For example, suppose you want to find what allocation sizes are common in your application:

# ./argdist -p 2420 -C 'p:c:malloc(size_t size):size_t:size'
[01:42:29]
p:c:malloc(size_t size):size_t:size
COUNT EVENT
[01:42:30]
p:c:malloc(size_t size):size_t:size
COUNT EVENT

[…]



(摘自 argdist example uses )。

作为记录,到目前为止我发现的大多数 eBPF 示例都位于以下位置之一:
  • linux/samples/bpf在 Linux 内核源代码中。
  • bcc/tools bcc 的目录。
  • (对于涉及 tc 的网络示例,在 iproute2 包源中的 iproute2/examples/tc 目录下。)
  • 关于linux-kernel - 如何使用 BPF 过滤核函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38671037/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com