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

未关闭 适合新手
#685 0 条评论 0 个 reaction 已指派 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 返回的 timeout 值进行转换的调用方。报告指出,将 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 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/socket2 的其他 Issue

查看 rust-lang/socket2 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。