From 0a99945e5c8be0d37661a73bccf7fc715bc59610 Mon Sep 17 00:00:00 2001 From: Gleb Pomykalov Date: Tue, 7 Apr 2020 22:20:18 +0300 Subject: Fix unaligned castting of cmsg data to af_alg_iv --- CHANGELOG.md | 1 + src/sys/socket/mod.rs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d565a9a..f23be293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Fixed a bug in nix::unistd that would result in an infinite loop when a group or user lookup required a buffer larger than 16KB. (#[1198](https://github.com/nix-rust/nix/pull/1198)) +- Fixed unaligned casting of `cmsg_data` to `af_alg_iv` (#[1206](https://github.com/nix-rust/nix/pull/1206)) ### Removed diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index aa280747..6b2d1ad0 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -657,15 +657,26 @@ impl<'a> ControlMessage<'a> { } #[cfg(any(target_os = "android", target_os = "linux"))] ControlMessage::AlgSetIv(iv) => { + let af_alg_iv = libc::af_alg_iv { + ivlen: iv.len() as u32, + iv: [0u8; 0], + }; + + let size = mem::size_of::(); + unsafe { - let alg_iv = cmsg_data as *mut libc::af_alg_iv; - (*alg_iv).ivlen = iv.len() as u32; + ptr::copy_nonoverlapping( + &af_alg_iv as *const _ as *const u8, + cmsg_data, + size, + ); ptr::copy_nonoverlapping( iv.as_ptr(), - (*alg_iv).iv.as_mut_ptr(), + cmsg_data.add(size), iv.len() ); }; + return }, #[cfg(any(target_os = "android", target_os = "linux"))] -- cgit v1.2.3