Skip to content

fix(net-traffic): initialize rules before loading network filters - #144

Open
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/net-traffic-rule-filtering
Open

fix(net-traffic): initialize rules before loading network filters#144
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/net-traffic-rule-filtering

Conversation

@yuKing123-king

Copy link
Copy Markdown
Contributor

修复内容

本次修改主要修复 observe/net-traffic 用户态规则初始化和规则安装时序问题。

修复的 Bug

  1. 默认 PID 规则错误

    原代码使用:

    struct Rule rule = {0};
    

此时 rule.pid 默认值为 0,会导致未指定 -p 时规则仍然可能按照 PID 0 进行匹配。

当前通过 init_rule() 显式初始化:

 memset(&rule, 0, sizeof(rule));
 rule.pid = -1;

pid = -1 表示不启用 PID 过滤。

  1. 入方向过滤值无法正确表示

    入方向使用值 -1,因此将 Rule::dir 从无符号类型改为有符号的 short,避免 -1 被解释为 65535。

  2. 远端 IPv4 地址字节序不一致

    原代码直接将 inet_addr() 的结果写入规则:

    rule.remote_ip = inet_addr(optarg);

    当前转换为主机字节序:

    rule.remote_ip = ntohl(inet_addr(optarg));

    使用户态规则中的 IP 格式与 BPF 侧比较使用的格式一致。

  3. 规则安装时序存在竞态

    原流程是在 BPF attach 后才更新 filter map。attach 和 map 更新之间可能产生没有按照目标规则过滤的事件。

    当前调整为:

    open
    fix_attach_point
    load
    update filter map
    create ring buffer
    attach

    在 BPF 程序 attach 前先写入过滤规则,减少规则尚未生效期间产生的未过滤事件。

代码改动

  • 新增 init_rule(),统一初始化全局规则。
  • 将 Rule::dir 改为有符号 short。
  • 将远端 IP 转换为主机字节序。
  • 删除未使用的 std::vector rules。
  • 增加规则写入 BPF map 的代码。
  • 调整 filter map 更新和 BPF attach 的顺序。

Signed-off-by: Wang Yu <wangyu6@uniontech.com>
@rwenz2004

Copy link
Copy Markdown

/review

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ No major issues detected

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.

2 participants