`from_timeval` overflows on a non-normalised `tv_usec`
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- rust
- Domain
- backend, networking
Research direction
Start in src/sys/unix.rs at from_timeval (around line 1296), then read its callers that convert timeout values returned by getsockopt. The report identifies the multiplication of tv_usec by 1000 as the overflow point and suggests constructing a duration from seconds and microseconds. Done means the conversion handles the reported non-normalised values without overflow; the payload does not name an existing test file.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Rust
- Stars
- 885
- Forks
- 328
- Avg merge
- 6d 12h
- Merged PRs (30d)
- 2
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-lang/socket2
-
Commit lockfileOpen
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
rust-lang/socket2#703 · 1 comment ·
Maintainers usually reply within 1 day
-
windows-sys 0.101.0Possibly taken @xtqqczze claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
SockAddr::path_len doesn't bound the length by sun_path: as_pathname panics since #691, as_abstract_namespace slices past itPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Build fails on 32-bit glibc (i686, armv7) with libc's `--cfg=libc_unstable_time64`: `tv_usec` is cast to `suseconds_t` (i32) for an i64 fieldPossibly taken @omrzk claimed this 28 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
rust-lang/socket2#684 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
rust-lang/socket2#683 · 2 comments ·
Maintainers usually reply within 1 day
All issues in rust-lang/socket2
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
infra
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
available bug good first issue tooling
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nixitov/Riptide-Native#817 ·
Maintainers usually reply within 1 day