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

オープン 初心者向け
#685 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@pederbe がすでに取り組んでいます。

2026年11月16日 から。

  • #690 @pederbe による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

src/sys/unix.rs の from_timeval(1296行目付近)から始め、getsockopt が返すタイムアウト値を変換する呼び出し元を読みます。報告では、tv_usec に 1000 を掛ける箇所がオーバーフローの発生点として特定されており、秒とマイクロ秒から Duration を構築する方法が提案されています。変換で報告された正規化されていない値をオーバーフローせずに処理できれば完了です。payload には既存のテストファイル名は記載されていません。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
885
フォーク
328
平均マージ
6日 12時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/socket2 のほかの issue

rust-lang/socket2 の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。