已开启
AArch64 的 read_cpuinfo 固定读取 1024 字节且未补 NUL,存在越界解析和字段截断风险 #90
super dog.创建于  5月14日
super dog.
super dog.
5月14日 创建

问题描述

在 gcc/common/config/aarch64/aarch64-common.cc 中,read_cpuinfo() 使用固定的 char cpuinfo[1024] 缓冲区读取 /proc/cpuinfo,但读取后没有根据 fread() 返回值补 NUL 终止,也没有处理字段超出前 1023 字节的情况,随后直接把该缓冲区传给 strstr() 解析 CPU implementer 和 CPU part。

这种实现存在两个明显风险:一是当读取内容刚好填满缓冲区或末尾没有 NUL 时,后续字符串搜索会越过有效数据范围继续扫描;二是当相关字段未出现在前 1023 字节内时,会被错误判定为“未找到”,导致平台识别不稳定。

根因分析

  1. fread(cpuinfo, sizeof(char), sizeof(cpuinfo) - 1, fp); 的返回值没有保存,也没有根据实际读取长度追加 cpuinfo[nread] = '\0'。
  2. 后续 strstr(cpuinfo, "CPU implementer") 和 strstr(cpuinfo, "CPU part") 依赖的是 C 风格 NUL 终止字符串,而当前缓冲区并没有被可靠终止。
  3. 解析策略只看固定大小的前 1023 字节,既没有循环读取,也没有按行解析,因此当 /proc/cpuinfo 较长或字段位置靠后时,结果会受到文件布局影响。

修复方向

建议把这段解析改成更稳妥的逐行读取或显式长度受控解析逻辑:

  • 保存 fread() 返回值并在有效位置补 NUL,至少先消除越界字符串扫描风险。
  • 更推荐改成逐行读取 /proc/cpuinfo,按键值对查找 CPU implementer 与 CPU part,避免依赖固定大小缓冲区。
  • 如果继续保留一次性读取方案,也应当在字段未完整读入时显式处理,而不是静默回退成 INVALID_IMP。
likedislike
openeuler-ci-botopeneuler-ci-bot成员
5月14日 添加了label:sig/Compiler
openeuler-ci-bot
openeuler-ci-bot成员
5月14日 评论:

Welcome To openEuler Community

Hey @papertager , 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.

Contact Guide

If you have any questions, please contact the SIG: Compiler ,
and any of the maintainers: @SegFault, @alexanderbill, @cf-zhao, @chenzheng1030, @eastb233, @kuenking111_admin, @li-yancheng, @wangqiang95, @wd-gitcode ,
and any of the committers: @huang-xiaoquan, @lin-4682, @xiongzhou4 .

likedislike
Sspy20051623
9月9日 关联了pull request:AArch64: avoid host CPU detection for TSV110 defaults