已开启
AArch64 的 read_cpuinfo 固定读取 1024 字节且未补 NUL,存在越界解析和字段截断风险 #90
super dog.创建于 5月14日
5月14日 添加了label:sig/Compiler
openeuler-ci-bot
5月14日 评论:
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 .


9月9日 关联了pull request:AArch64: avoid host CPU detection for TSV110 defaults
问题描述
在
gcc/common/config/aarch64/aarch64-common.cc中,read_cpuinfo()使用固定的char cpuinfo[1024]缓冲区读取/proc/cpuinfo,但读取后没有根据fread()返回值补 NUL 终止,也没有处理字段超出前 1023 字节的情况,随后直接把该缓冲区传给strstr()解析CPU implementer和CPU part。这种实现存在两个明显风险:一是当读取内容刚好填满缓冲区或末尾没有 NUL 时,后续字符串搜索会越过有效数据范围继续扫描;二是当相关字段未出现在前 1023 字节内时,会被错误判定为“未找到”,导致平台识别不稳定。
根因分析
fread(cpuinfo, sizeof(char), sizeof(cpuinfo) - 1, fp);的返回值没有保存,也没有根据实际读取长度追加cpuinfo[nread] = '\0'。strstr(cpuinfo, "CPU implementer")和strstr(cpuinfo, "CPU part")依赖的是 C 风格 NUL 终止字符串,而当前缓冲区并没有被可靠终止。/proc/cpuinfo较长或字段位置靠后时,结果会受到文件布局影响。修复方向
建议把这段解析改成更稳妥的逐行读取或显式长度受控解析逻辑:
fread()返回值并在有效位置补 NUL,至少先消除越界字符串扫描风险。/proc/cpuinfo,按键值对查找CPU implementer与CPU part,避免依赖固定大小缓冲区。INVALID_IMP。