`MsgHdrMut::control_len` includes cmsg padding that `recvmsg` does not write

Aperta
#695 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
46/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
networking

Direzione di ricerca

Start from the definition of MsgHdrMut::control_len and MsgHdrMut::with_control; compare the documented guarantee against how recvmsg advances msg_controllen with CMSG_SPACE while writing only CMSG_LEN bytes. Reproduce with the payload's Socket::pair/MaybeUninit snippet, then choose the documentation-only or zeroing approach and add a regression test. Done means the docs say to walk control messages with CMSG_FIRSTHDR/CMSG_NXTHDR, or the buffer is initialized so control[..control_len()] is safe, with the reproduction covered.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

The documentation of MsgHdrMut::control_len says it "can be used to determine how much, if any, of the control buffer was filled by recvmsg". Since the control buffer is &mut [MaybeUninit<u8>], callers will reasonably treat control[..control_len()] as initialised. On Linux, though, the kernel advances msg_controllen by CMSG_SPACE(len) for each control message but writes only CMSG_LEN(len) bytes, so the alignment padding after each payload is counted and never written. With an uninitialised buffer, reading control[..control_len()] as &[u8] is then undefined behaviour in the caller.

Receiving one SCM_RIGHTS message with one fd over a Socket::pair(Domain::UNIX, Type::DGRAM, None), into control buffers pre-filled with 0xAA and then 0x55:

let mut control = [MaybeUninit::new(fill); 64];
let mut hdr = MsgHdrMut::new().with_buffers(&mut bufs).with_control(&mut control);
b.recvmsg(&mut hdr, 0).unwrap();
let len = hdr.control_len(); // 24
// bytes 20..24 still hold `fill` after both calls
control_len() = 24
bytes [20, 21, 22, 23] of control[..24] were not written by recvmsg: 0xaa / 0x55 left from the pre-fill

Run with socket2 0.6.5 and current master (b6c3b8e), Rust 1.100.0, glibc 2.39 and Linux 6.14 on x86_64.

A fix could be to document that only the cmsg_len bytes of each message are written and that the buffer should be walked with CMSG_FIRSTHDR/CMSG_NXTHDR, or to zero the buffer in with_control so that the current wording holds.

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.

Lingua principale
Rust
Stelle
885
Fork
328
Merge medio
6g 12h
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/socket2

Tutte le issue di rust-lang/socket2

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.