`from_timeval` overflows on a non-normalised `tv_usec`

Offen Anfängerfreundlich
#685 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@pederbe arbeitet bereits daran.

Seit 16.11.2026.

  • #690 von @pederbe — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
rust
Bereich
backend, networking

Rechercherichtung

Beginne in src/sys/unix.rs bei from_timeval (etwa Zeile 1296) und lies anschließend die Aufrufer, die von getsockopt zurückgegebene Timeout-Werte umwandeln. Der Bericht nennt die Multiplikation von tv_usec mit 1000 als Stelle, an der der Überlauf auftritt, und schlägt vor, eine Duration aus Sekunden und Mikrosekunden zu erstellen. Das Ziel ist erreicht, wenn die Umwandlung die gemeldeten nicht normalisierten Werte ohne Überlauf verarbeitet; im Payload wird keine vorhandene Testdatei genannt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

from_timeval in src/sys/unix.rs (main at 8b8b96f, line 1296) computes (duration.tv_usec as u32) * 1000, which overflows (and panics in debug builds) when tv_usec >= 4_294_968.

const fn from_timeval(duration: libc::timeval) -> Option<Duration> {
    if duration.tv_sec == 0 && duration.tv_usec == 0 {
        None
    } else {
        let sec = duration.tv_sec as u64;
        let nsec = (duration.tv_usec as u32) * 1000;   // overflows for tv_usec >= 4_294_968
        Some(Duration::new(sec, nsec))
    }
}

from_timeval is only called on values returned by getsockopt for the timeout options. On Linux the kernel always returns a normalised timeval (tv_usec < 1_000_000), so I could not trigger this through the public API. I am reporting it as a hardening issue: if a platform ever returns a non-normalised value, debug builds panic and release builds return a wrong duration. Duration::from_micros(tv_usec as u64) added to Duration::from_secs(sec) would avoid the overflow. Feel free to close if you consider the input impossible.

Kani harness that fails on this commit (cargo kani --harness from_timeval_no_overflow, in src/sys/unix.rs):

#[cfg(kani)]
mod kani_proofs {
    use super::from_timeval;

    #[kani::proof]
    fn from_timeval_no_overflow() {
        let sec: i64 = kani::any();
        let usec: i64 = kani::any();
        kani::assume(sec >= 0 && sec < 1000);
        kani::assume(usec >= 0 && usec <= u32::MAX as i64);
        let _ = from_timeval(libc::timeval { tv_sec: sec, tv_usec: usec });
    }
}

This was found with Kani 0.68.0 (CBMC 6.11.0) in an experiment where AI agents verified popular crates using the Kani agent plugin (model-checking/kani#5059). The text was written with AI assistance; the reproducer and the Kani harness were re-run on the commit named above.

Vorherrschende Sprache
Rust
Sterne
885
Forks
328
Ø Merge
6 T. 12 Std.
Gemergte PRs (30 T.)
2

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus rust-lang/socket2

Alle Issues in rust-lang/socket2

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.