Closed
bpftool: batch 模式长度检查可被注释绕过,超长批处理行的残留部分被当作独立命令执行(待 backport 上游 fc2a7f5) #252
Lei-ChenAdd on  9 days agoClosed at  5 days ago
Lei-Chen
9 days ago Create

问题描述

bpftool batch file <FILE> 的 do_batch() 在读取批处理行后,先截断 # 之后的注释内容,再用 strlen(buf) == sizeof(buf) - 1 判断该行是否填满了 65536 字节的缓冲区:

while (fgets(buf, sizeof(buf), fp)) {
        cp = strchr(buf, '#');
        if (cp)
                *cp = '\0';          /* 先截断注释 */

        if (strlen(buf) == sizeof(buf) - 1) {   /* 后做长度检查 */
                errno = E2BIG;
                break;
        }

因此,当一行长度超过缓冲区、且前 65535 字节内出现 # 时,截断会让 strlen(buf) 变小,长度检查被绕过。该行未被 fgets() 读走的剩余部分仍留在文件流中,下一轮循环会把它当作一条独立命令解析并执行。续行(行尾 \)路径同理:超长的续行含 # 时绕过 command is too long 检查,其残留部分同样会被执行。

影响:批处理文件的行长校验形同虚设,操作者/上层工具以为已通过限制(例如对拼接内容做长度截断、把用户可控文本写进 batch 文件)的输入,其尾部会被 bpftool 当作新的子命令执行,属于输入校验绕过导致的非预期命令执行。

影响范围

  • bpftool 7.2.0(openEuler openEuler-24.03-LTS*、openEuler-26.09、master 等仍为 7.2.0 的分支)
  • 触发条件:使用 bpftool batch file,且批处理文件中存在 >65535 字节、且在缓冲区范围内含 # 的行(或同类续行)
  • 现有 Patch0–Patch3 只涉及 perf_event/libbpf 兼容,未触及 src/main.c

复现(7.2.0 基线,已本地构建验证)

构造批处理文件:第一行 version# + 65527 个 x + version(总长 65543 字节,# 位于第 8 字节)。

$ bpftool batch file poc-batch.txt
bpftool v7.2.0
using libbpf v1.2
features:
bpftool v7.2.0          <-- 残留部分被当作第二条命令执行
using libbpf v1.2
features:
processed 2 commands
$ echo $?
0

超长续行(第一行 vers\,第二行 # + 65527 个 x + versionversion)同样可复现:

$ bpftool batch file poc-batch-cont.txt
... 同上,version 被打印两次,processed 2 commands

修复

把两处长度检查移到注释截断之前,使检查看到的是 fgets() 实际读入的完整内容;恰好填满缓冲区的行也一并拒绝(批处理命令行远离该上限,无兼容性影响)。

上游提交:https://github.com/libbpf/bpftool/commit/fc2a7f540f9df19851fce2328bab4283da830af7

修复后行为(同一 PoC):

$ bpftool batch file poc-batch.txt
Error: reading batch file failed: Argument list too long        (exit 255)
$ bpftool batch file poc-batch-cont.txt
Error: command 0 is too long                                   (exit 255)

正常批处理(普通行、行尾注释、整行注释、续行)行为不变:processed 3 commands,退出码 0。

openEuler 侧进展

已按 openEuler backport 规范生成源码补丁 backport-fix-batch-line-length-check-bypass.patch(新增 Patch4、Release +1、%changelog 登记),并在以下分支验证整条补丁链可 -F0 干净应用、构建通过、PoC 由“执行残留命令”变为“拒绝超长行”:

  • openEuler-24.03-LTS-SP3(Release 7.2.0-2)
  • openEuler-26.09(Release 7.2.0-3)
  • master(Release 7.2.0-3)

其余 24.03 兄弟分支(LTS / -Next / SP1 / SP2 / SP4)版本与补丁链一致,确认同样受影响,将通过代表 PR 的 /sync 同步。

是否 CVE

上游未为此提交分配 CVE,属普通缺陷/安全加固修复,故按「缺陷」跟踪,不使用 CVE/UNFIXED 标签。

二、缺陷分析结构反馈
影响性分析说明:

缺陷严重等级:(Critical/High/Moderate/Low)

缺陷根因说明:

受影响版本排查(受影响/不受影响):
openEuler-20.03-LTS-SP4
openEuler-22.03-LTS-SP4
openEuler-24.03-LTS-SP1
openEuler-24.03-LTS-SP3
openEuler-24.03-LTS-SP4

修复是否涉及abi变化(是/否):
openEuler-20.03-LTS-SP4
openEuler-22.03-LTS-SP4
openEuler-24.03-LTS-SP1
openEuler-24.03-LTS-SP3
openEuler-24.03-LTS-SP4

likedislike
LLei-Chen
9 days ago changed custom category from 任务 to 缺陷
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Assign bitcoffee as responsible person
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Modify description of issue
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Assign yalogr、MrRlu、weiyj__lk、happyseeker、wonleing、hello-leichen as responsible person
openeuler-ci-bot
openeuler-ci-botCollaborators
9 days ago Comments:

以下的要求不是强制性的, 未按模板评论时对issue无任何影响
issue处理注意事项:
1. 当前issue受影响的分支提交pr时, 须在pr描述中填写当前issue编号进行关联, 否则无法关闭当前issue;
2. 模板内容需要填写完整, 无论是受影响或者不受影响都需要填写完整内容,未引入的分支不需要填写, 否则无法关闭当前issue;
3. 以下为模板中需要填写完整的内容, 请复制到评论区回复, 注: 内容的标题名称(影响性分析说明, 缺陷严重等级, 受影响版本排查(受影响/不受影响), 修复是否涉及abi变化(是/否))不能省略,省略后defect-manager将无法正常解析填写内容.
评论区可能使用到的指令说明:

指令 指令说明 使用权限
/check-issue 触发defect-manager校验 不限
/reason xxx /reason +挂起或取消条件 不限

影响性分析说明:

缺陷严重等级:(Critical/High/Moderate/Low)

缺陷根因说明:

受影响版本排查(受影响/不受影响):

  1. openEuler-20.03-LTS-SP4:
  2. openEuler-22.03-LTS-SP4:
  3. openEuler-24.03-LTS-SP1:
  4. openEuler-24.03-LTS-SP3:
  5. openEuler-24.03-LTS-SP4:

abi变化(是/否):

  1. openEuler-20.03-LTS-SP4:
  2. openEuler-22.03-LTS-SP4:
  3. openEuler-24.03-LTS-SP1:
  4. openEuler-24.03-LTS-SP3:
  5. openEuler-24.03-LTS-SP4:

缺陷issue处理具体操作请参考:
https://atomgit.com/openeuler/cve-manager/blob/master/cve-vulner-manager/doc/md/defect-manager-manual.md
pr关联issue具体操作请参考:
https://docs.atomgit.com/docs/help/home/org_project/pullrequests/pr-related-issue

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Added label: sig/sig-ebpf
openeuler-ci-bot
openeuler-ci-botCollaborators
9 days ago Comments:

Welcome To openEuler Community

Hey @hello-leichen , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands. You can self-configure the PR merge rules for this repository. For more details, please refer to Here.

Contact Guide

If you have any questions, please contact the SIG: sig-ebpf ,
and any of the maintainers: @MrRlu, @happyseeker, @weiyj__lk, @wonleing, @yalogr ,
and any of the committers: @LemmyHuang, @bitcoffee, @linford-wu, @perfectwcy, @supercharge1 .

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Added label: DEFECT/UNFIXED
LLei-Chen
9 days ago Associated pull request
LLei-Chen
9 days ago Associated pull request
LLei-Chen
9 days ago Associated pull request
Lei-Chen
9 days ago Comments:

已提交 PR:

其余 24.03 分支(LTS、LTS-Next、SP1、SP2、SP4)已确认受影响,已通过 SP3 PR 的 /sync 排队同步。

影响性分析说明: bpftool 的 do_batch() 在检查 fgets() 行长限制之前先按 '#' 截断注释;超长批处理行(或续行)在前 65535 字节内出现 '#' 时可绕过长度检查,未读取的行尾会被当作下一条 bpftool 子命令解析执行。

缺陷严重等级: Moderate

缺陷根因说明: 注释截断发生在行长校验之前,导致长度检查基于被截断的短行,剩余尾部被后续解析。

受影响版本排查(受影响/不受影响):

  1. openEuler-20.03-LTS-SP4: 受影响(本次未提交 PR,后续单独回移)
  2. openEuler-22.03-LTS-SP4: 受影响(本次未提交 PR,后续单独回移)
  3. openEuler-24.03-LTS: 受影响(由 SP3 PR /sync 同步)
  4. openEuler-24.03-LTS-Next: 受影响(由 SP3 PR /sync 同步)
  5. openEuler-24.03-LTS-SP1: 受影响(由 SP3 PR /sync 同步)
  6. openEuler-24.03-LTS-SP2: 受影响(由 SP3 PR /sync 同步)
  7. openEuler-24.03-LTS-SP3: 受影响(PR #17)
  8. openEuler-24.03-LTS-SP4: 受影响(由 SP3 PR /sync 同步)
  9. openEuler-26.09: 受影响(PR #18)
  10. master: 受影响(PR #19)

abi变化(是/否):

  1. openEuler-20.03-LTS-SP4: 否
  2. openEuler-22.03-LTS-SP4: 否
  3. openEuler-24.03-LTS: 否
  4. openEuler-24.03-LTS-Next: 否
  5. openEuler-24.03-LTS-SP1: 否
  6. openEuler-24.03-LTS-SP2: 否
  7. openEuler-24.03-LTS-SP3: 否
  8. openEuler-24.03-LTS-SP4: 否
  9. openEuler-26.09: 否
  10. master: 否
likedislike
Lei-Chen
9 days ago Comments:

/check-issue

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
9 days ago Removed responsible person MrRlu、bitcoffee、weiyj__lk、happyseeker、wonleing、hello-leichen
openeuler-ci-bot
openeuler-ci-botCollaborators
9 days ago Comments:

@yalogr 经过defect-manager解析,已分析的内容如下表所示:

状态 需分析 内容
已分析 1.影响性分析说明 bpftool的do_batch()在检查fgets()行长限制之前先按'#'截断注释;超长批处理行(或续行)在前65535字节内出现'#'时可绕过长度检查,未读取的行尾会被当作下一条bpftool子命令解析执行。
已分析 2.缺陷严重等级 Moderate
已分析 3.缺陷根因定位 注释截断发生在行长校验之前,导致长度检查基于被截断的短行,剩余尾部被后续解析。
已分析 4.受影响版本排查 openEuler-20.03-LTS-SP4:受影响,openEuler-22.03-LTS-SP4:受影响,openEuler-24.03-LTS:受影响,openEuler-24.03-LTS-Next:受影响,openEuler-24.03-LTS-SP1:受影响,openEuler-24.03-LTS-SP2:受影响,openEuler-24.03-LTS-SP3:受影响,openEuler-24.03-LTS-SP4:受影响,openEuler-26.09:受影响
已分析 5.abi变化 openEuler-20.03-LTS-SP4:否,openEuler-22.03-LTS-SP4:否,openEuler-24.03-LTS:否,openEuler-24.03-LTS-Next:否,openEuler-24.03-LTS-SP1:否,openEuler-24.03-LTS-SP2:否,openEuler-24.03-LTS-SP3:否,openEuler-24.03-LTS-SP4:否,openEuler-26.09:否

请确认分析内容的准确性,确认无误后,您可以进行后续步骤,否则您可以继续分析

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
6 days ago Associated pull request
nickyang
nickyangCollaborators
6 days ago Comments:

/check-issue

likedislike
nickyangnickyangCollaborators
6 days ago Associated pull request
openeuler-ci-botopeneuler-ci-botCollaborators
5 days ago Associated pull request
nickyang
nickyangCollaborators
5 days ago Comments:

2003不涉及,其它涉及分支PR均已合入

likedislike
nickyang
nickyangCollaborators
5 days ago Comments:

/close

likedislike
openeuler-ci-bot
openeuler-ci-botCollaborators
5 days ago Comments:

Notice

@yangl777 , you can't close an issue unless you are the author of it or a collaborator.

likedislike
LLemmyHuangCollaborators
5 days ago changed custom state from 待办的 to 已完成
LLemmyHuangCollaborators
5 days ago Close issue
openeuler-ci-botopeneuler-ci-botCollaborators
5 days ago changed custom state from 已完成 to 待办的
openeuler-ci-botopeneuler-ci-botCollaborators
5 days ago Reopen issue
openeuler-ci-bot
openeuler-ci-botCollaborators
5 days ago Comments:

@yalogr
关闭issue前,需要将受影响的分支在合并pr时关联上当前issue编号: #252
受影响分支: openEuler-20.03-LTS-SP4
具体操作参考: https://docs.atomgit.com/docs/help/home/org_project/pullrequests/pr-related-issue

likedislike
nickyang
nickyangCollaborators
5 days ago Comments:

openEuler-20.03-LTS-SP4:不受影响

likedislike
nickyang
nickyangCollaborators
5 days ago Comments:

受影响版本排查(受影响/不受影响):
openEuler-20.03-LTS-SP4:不受影响

修复是否涉及abi变化(是/否):
openEuler-20.03-LTS-SP4:否

likedislike
nickyang
nickyangCollaborators
5 days ago Comments:

/check-issue

likedislike
nickyang
nickyangCollaborators
5 days ago Comments:

影响性分析说明: bpftool 的 do_batch() 在检查 fgets() 行长限制之前先按 '#' 截断注释;超长批处理行(或续行)在前 65535 字节内出现 '#' 时可绕过长度检查,未读取的行尾会被当作下一条 bpftool 子命令解析执行。

缺陷严重等级: Moderate

缺陷根因说明: 注释截断发生在行长校验之前,导致长度检查基于被截断的短行,剩余尾部被后续解析。

受影响版本排查(受影响/不受影响):

openEuler-20.03-LTS-SP4: 不受影响
openEuler-22.03-LTS-SP4: 受影响
openEuler-24.03-LTS-Next: 受影响
openEuler-24.03-LTS-SP1: 受影响
openEuler-24.03-LTS-SP3: 受影响
openEuler-24.03-LTS-SP4: 受影响
openEuler-26.09: 受影响
master: 受影响
abi变化(是/否):
openEuler-20.03-LTS-SP4: 否
openEuler-22.03-LTS-SP4: 否
openEuler-24.03-LTS-Next: 否
openEuler-24.03-LTS-SP1: 否
openEuler-24.03-LTS-SP3: 否
openEuler-24.03-LTS-SP4: 否
openEuler-26.09: 否
master: 否

likedislike
openeuler-ci-bot
openeuler-ci-botCollaborators
5 days ago Comments:

@yalogr 经过defect-manager解析,已分析的内容如下表所示:

状态 需分析 内容
已分析 1.影响性分析说明 bpftool的do_batch()在检查fgets()行长限制之前先按'#'截断注释;超长批处理行(或续行)在前65535字节内出现'#'时可绕过长度检查,未读取的行尾会被当作下一条bpftool子命令解析执行。
已分析 2.缺陷严重等级 Moderate
已分析 3.缺陷根因定位 注释截断发生在行长校验之前,导致长度检查基于被截断的短行,剩余尾部被后续解析。
已分析 4.受影响版本排查 openEuler-20.03-LTS-SP4:不受影响,openEuler-22.03-LTS-SP4:受影响,openEuler-24.03-LTS-Next:受影响,openEuler-24.03-LTS-SP1:受影响,openEuler-24.03-LTS-SP3:受影响,openEuler-24.03-LTS-SP4:受影响,openEuler-26.09:受影响
已分析 5.abi变化 openEuler-20.03-LTS-SP4:否,openEuler-22.03-LTS-SP4:否,openEuler-24.03-LTS-Next:否,openEuler-24.03-LTS-SP1:否,openEuler-24.03-LTS-SP3:否,openEuler-24.03-LTS-SP4:否,openEuler-26.09:否

请确认分析内容的准确性,确认无误后,您可以进行后续步骤,否则您可以继续分析

likedislike
nickyangnickyangCollaborators
5 days ago changed custom state from 待办的 to 已完成
nickyangnickyangCollaborators
5 days ago Close issue
openeuler-ci-botopeneuler-ci-botCollaborators
5 days ago Added label: DEFECT/FIXED; Removed label: DEFECT/UNFIXED,sig/sig-ebpf