Repository navigation
fix: harden unsafe socket code against alignment UB and bad cmsg - #102
Merged
Merged
Conversation
- Use #[repr(align(8))] buffer for cmsghdr ancillary data to satisfy alignment requirements on all platforms (was 1-byte aligned [u8; 64]) (#4) - Use ptr::read_unaligned and addr_of! for cmsg header fields and TTL data instead of creating &cmsghdr references from raw CMSG pointers (#4) - Validate cmsg_len >= CMSG_LEN(sizeof(c_int)) before reading TTL integer, preventing read past cmsg boundary on malformed ancillary data (#27) - Add SAFETY comment for MaybeUninit -> &[u8] cast in test (#33) All 493 tests pass; clippy clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hardens unsafe socket code to eliminate alignment UB and out-of-bounds reads in the ICMP receiver's ancillary-data (CMSG) handling.
Changes
[u8; 64]cmsg buffer with a#[repr(align(8))]wrapper to satisfystruct cmsghdralignment requirements (8-byte on Linux/FreeBSD, 4-byte on macOS/NetBSD).&*cmsgdereferences withptr::addr_of!+read_unalignedforcmsg_level,cmsg_type,cmsg_len, and the TTL/hop-limit integer — eliminating unaligned reference UB that works on x86 only by hardware tolerance.cmsg_len >= CMSG_LEN(sizeof(c_int))before reading the TTL integer, preventing read past the cmsg boundary on malformed/truncated ancillary data.recv()initializes the firstnbytes beforefrom_raw_partsis called on theMaybeUninitbuffer in the raw-socket test.Testing
cargo clippy --all-targets -- -D warningsclean.#[repr(align(8))]struct is defined inline inrecv_icmp_with_ttl— no public API change.Risk
Medium — touches
unsafecode in the ICMP receive path, but the changes are mechanical: aligned buffer +read_unaligned+ bounds check. No logic changes.