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

Đang mở Phù hợp với người mới
#685 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@pederbe đang làm issue này rồi.

Từ ngày 16/11/2026.

  • #690 của @pederbe — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend, networking

Hướng nghiên cứu

Bắt đầu tại src/sys/unix.rs, ở from_timeval (khoảng dòng 1296), sau đó đọc các hàm gọi nó để chuyển đổi giá trị timeout do getsockopt trả về. Báo cáo xác định phép nhân tv_usec với 1000 là điểm gây tràn số và đề xuất tạo duration từ giây và micro giây. Công việc hoàn tất khi phép chuyển đổi xử lý được các giá trị chưa chuẩn hóa được báo cáo mà không bị tràn số; payload không nêu tên tệp kiểm thử hiện có nào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
885
Fork
328
Merge trung bình
6 ngày 12 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/socket2

Tất cả issue của rust-lang/socket2

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.