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

Abierto
#693 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
40/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust
Área
security

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Rust
Estrellas
885
Forks
328
Merge medio
6 d 12 h
PR fusionados (30 d)
2

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/socket2

Todos los issues de rust-lang/socket2

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.