SockAddr's PartialEq and Hash read uninitialised padding bytes of padded sockaddr types

未关闭
#693 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
40/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
security

调研方向

Start in src/sockaddr.rs around line 485, where PartialEq/Hash compare as_bytes() over the first len bytes, and trace how SockAddr::new, try_init, set_length and SockAddrStorage::view_as establish that length without checking padding initialisation. Reproduce with the posted sockaddr_can example under Miri to confirm the uninitialised range at offsets 2..4. Done means either init-checking in those constructors or a family-aware comparison/hash that never touches padding, with the reproducer no longer reporting UB.

由索引模型根据 Issue 内容生成。

描述

SockAddr's PartialEq and Hash impls go through as_bytes, which views the first
len bytes of the storage as &[u8]. They assume every one of those bytes is initialised.
A caller can follow the documented unsafe API exactly and still leave some uninitialised.
SockAddrStorage::view_as::<T>() accepts any of the platform's sockaddr_* types, and a
typed write of a T leaves T's padding uninitialised. SockAddr::new asks only that the
family and length match the type. On Linux, libc::sockaddr_can has padding at offsets 2..4,
so comparing such an address reads uninitialised memory:

use std::mem::size_of;
use socket2::{SockAddr, SockAddrStorage, socklen_t};

fn main() {
    let mut storage = SockAddrStorage::zeroed();
    let can = unsafe { storage.view_as::<libc::sockaddr_can>() };
    let mut value: libc::sockaddr_can = unsafe { std::mem::zeroed() };
    value.can_family = libc::AF_CAN as libc::sa_family_t;
    value.can_ifindex = 1;
    *can = value;
    let addr = unsafe { SockAddr::new(storage, size_of::<libc::sockaddr_can>() as socklen_t) };
    assert!(addr == addr);
}
error: Undefined Behavior: reading memory at alloc263[0x0..0x18], but memory is uninitialized at [0x2..0x4], and this operation requires initialized memory
            3: <socket2::SockAddr as std::cmp::PartialEq>::eq
                at src/sockaddr.rs:485:9: 485:44

This was run under Miri with socket2 0.6.5 (8b8b96f) and current master (b6c3b8e), libc
0.2.191 and rustc 1.102.0-nightly (2026-11-12), on x86_64-unknown-linux-gnu.

A possible fix is to require, in SockAddr::new, try_init, set_length and view_as, that
every byte below len is initialised, padding included. Another is to compare and hash by
family rather than by raw bytes.

Found by Crustify, an experimental UB/soundness auditing agent developed at UC Berkeley and running on Claude Opus 5.5, then manually reviewed and independently reproduced.

主要语言
Rust
星标
885
派生
328
平均合并
6 天 12 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/socket2 的其他 Issue

查看 rust-lang/socket2 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。