Skip to content

[Bug] SAL 的 SIOCGIFCONF 存在缓冲区越界写和未初始化栈数据泄漏 #11763

Description

@Huoyanlifusu

RT-Thread Version

master(bf38ee2130634ac6571e816dde81b04818d75444)

Affected area

Networking

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

Not specified; general lwp / sal module

Develop Toolchain

GCC

Describe the bug

sal_ioctlsocket() 处理 SIOCGIFCONF 时存在两个内存安全问题。

相关代码:

case SIOCGIFCONF:
{
struct ifconf *ifconf_tmp;
ifconf_tmp = (struct ifconf *)arg;
int count_size = 0;
for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
{
struct sal_ifreq sal_ifreq_temp;
count_size++;
netdev = rt_list_entry(node, struct netdev, list);
rt_strcpy(sal_ifreq_temp.ifr_ifrn.ifrn_name, netdev->name);
rt_memcpy(ifconf_tmp->ifc_ifcu.ifcu_buf, &sal_ifreq_temp, sizeof(struct sal_ifreq));
ifconf_tmp->ifc_ifcu.ifcu_buf += sizeof(struct sal_ifreq);
}
ifconf_tmp->ifc_len = sizeof(struct sal_ifreq) * count_size;
ifconf_tmp->ifc_ifcu.ifcu_buf = ifconf_tmp->ifc_ifcu.ifcu_buf - sizeof(struct sal_ifreq) * count_size;
return 0;

  1. 用户输入 ifc_len 未校验,可能越界写

输入的 ifc_len 表示 ifc_buf 的容量,但代码没有使用它限制写入数量。例如调用者只分配一个 ifreq,系统存在两个网卡时,第二次 rt_memcpy() 会写出缓冲区边界。ifc_buf == NULL、ifc_len == 0 的容量查询也可能触发空指针写入。

  1. 未初始化栈上变量,可能导致栈内存泄漏

sal_ifreq_temp 没有初始化,只写入了接口名,随后却复制了整个结构体。因此名称数组的剩余部分、ifr_ifru 联合体和 padding 中的栈残留数据都会进入调用者的缓冲区。在 LWP 场景下,调用者可以在 ioctl() 返回后直接读取这些字节,构成内核栈信息泄漏;同时接口地址等字段也会是不可预测的值。

Other additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions