SockAddr::path_len doesn't bound the length by sun_path: as_pathname panics since #691, as_abstract_namespace slices past it

Abierto Apto para principiantes
#694 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@kolinabir ya está trabajando en esto.

Desde el 4/12/2026.

  • #701 de @kolinabir — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust
Área
networking

Línea de trabajo

Empieza en src/sys/unix.rs alrededor de las líneas 944-966 en path_len, que alimenta a as_pathname y as_abstract_namespace; el issue fija el panic en storage.sun_path[len - 1]. Reproduce con el ejemplo de sockaddr_storage pegado bajo cargo +nightly miri run y de forma nativa, luego limita la longitud a sun_path.len() para que ambos casos produzcan 107 bytes. Terminado cuando ambas rutas devuelvan slices acotados bajo Miri y cargo test --features all pase; espera leer el código circundante de punteros/retag unsafe antes de tocarlo.

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

Descripción

SockAddr::path_len computes the path length from self.len() without bounding it by sun_path's 108 bytes. SockAddrStorage::view_as's documentation example copies a received sockaddr_storage whole and passes size_of::<sockaddr_storage>() as the length, and for an AF_UNIX address built that way len is 126:

  • Since #691, as_pathname() (and as_unix()) panics, because path_len now indexes storage.sun_path[len - 1].
  • as_abstract_namespace() returns a 125-byte slice that runs 17 bytes past sun_path. Miri's Stacked Borrows rejects it, because the pointer comes from a reference to sun_path alone. Tree Borrows accepts it. Before #691, as_pathname() did the same.
use std::mem::size_of;
use socket2::{SockAddr, SockAddrStorage, socklen_t};

fn main() {
    let mut received: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
    let un = unsafe { &mut *(&mut received as *mut _ as *mut libc::sockaddr_un) };
    un.sun_family = libc::AF_UNIX as libc::sa_family_t;
    un.sun_path[1] = b'a' as libc::c_char; // abstract name; set sun_path[0] instead for a pathname
    let mut storage = SockAddrStorage::zeroed();
    unsafe { *storage.view_as::<libc::sockaddr_storage>() = received };
    let addr = unsafe { SockAddr::new(storage, size_of::<libc::sockaddr_storage>() as socklen_t) };
    println!("{}", addr.as_abstract_namespace().unwrap().len()); // 125
}

cargo +nightly miri run:

error: Undefined Behavior: trying to retag from <736> for SharedReadOnly permission at alloc263[0x6e], but that tag does not exist in the borrow stack for this location
   --> src/sys/unix.rs:966:13
help: <736> was created by a SharedReadOnly retag at offsets [0x2..0x6e]

With un.sun_path[0] = b'a' and as_pathname(), natively and under Miri:

thread 'main' panicked at src/sys/unix.rs:944:19:
index out of bounds: the len is 108 but the index is 125

This was run on master at b6c3b8e with libc 0.2.191 and rustc 1.102.0-nightly (2026-11-12), on x86_64-unknown-linux-gnu.

A possible fix is to bound the length in path_len:

let len = (self.len() as usize - offset_of_path(storage)).min(storage.sun_path.len());

I tested it on master: both cases then return 107 bytes, natively and under Miri, and cargo test --features all passes.

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.