Skip to content

Commit fdf828b

Browse files
bors[bot]Gleb Pomykalov
and
Gleb Pomykalov
authored
Merge #1206
1206: Fix unaligned casting of cmsg data to af_alg_iv r=asomers a=glebpom Casting a pointer to `cmsg_data` to `af_alg_iv` is incorrect since it's not properly aligned. As of the [`cmsg` man page](http://man7.org/linux/man-pages/man3/cmsg.3.html) "Applications should not cast it to a pointer type matching the payload, but should instead use memcpy(3) to copy data to or from a suitably declared object." Co-authored-by: Gleb Pomykalov <[email protected]>
2 parents 154ff58 + 0a99945 commit fdf828b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3232
- Fixed a bug in nix::unistd that would result in an infinite loop
3333
when a group or user lookup required a buffer larger than
3434
16KB. (#[1198](https://github.com/nix-rust/nix/pull/1198))
35+
- Fixed unaligned casting of `cmsg_data` to `af_alg_iv` (#[1206](https://github.com/nix-rust/nix/pull/1206))
3536

3637
### Removed
3738

src/sys/socket/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,26 @@ impl<'a> ControlMessage<'a> {
657657
}
658658
#[cfg(any(target_os = "android", target_os = "linux"))]
659659
ControlMessage::AlgSetIv(iv) => {
660+
let af_alg_iv = libc::af_alg_iv {
661+
ivlen: iv.len() as u32,
662+
iv: [0u8; 0],
663+
};
664+
665+
let size = mem::size_of::<libc::af_alg_iv>();
666+
660667
unsafe {
661-
let alg_iv = cmsg_data as *mut libc::af_alg_iv;
662-
(*alg_iv).ivlen = iv.len() as u32;
668+
ptr::copy_nonoverlapping(
669+
&af_alg_iv as *const _ as *const u8,
670+
cmsg_data,
671+
size,
672+
);
663673
ptr::copy_nonoverlapping(
664674
iv.as_ptr(),
665-
(*alg_iv).iv.as_mut_ptr(),
675+
cmsg_data.add(size),
666676
iv.len()
667677
);
668678
};
679+
669680
return
670681
},
671682
#[cfg(any(target_os = "android", target_os = "linux"))]

0 commit comments

Comments
 (0)