From 511c1eb5b2fe20fa3a46277fd883a79eb0f99d7a Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 16 May 2020 16:13:12 -0600 Subject: [PATCH] Fix an unaligned pointer read in Inotify::read_events Reported-by: Clippy --- CHANGELOG.md | 2 ++ src/sys/inotify.rs | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e186d94630..e674e3fe38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). 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)) - Fixed `readlink`/`readlinkat` when reading symlinks longer than `PATH_MAX` (#[1231](https://github.com/nix-rust/nix/pull/1231)) +- Fixed unaligned pointer read in `Inotify::read_events`. + (#[1244](https://github.com/nix-rust/nix/pull/1244)) ### Removed diff --git a/src/sys/inotify.rs b/src/sys/inotify.rs index e6c2cf64d2..9b544b90e4 100644 --- a/src/sys/inotify.rs +++ b/src/sys/inotify.rs @@ -30,8 +30,9 @@ use libc::{ }; use std::ffi::{OsString,OsStr,CStr}; use std::os::unix::ffi::OsStrExt; -use std::mem::size_of; +use std::mem::{MaybeUninit, size_of}; use std::os::unix::io::{RawFd,AsRawFd,FromRawFd}; +use std::ptr; use unistd::read; use Result; use NixPath; @@ -173,7 +174,8 @@ impl Inotify { /// events could be read then the EAGAIN error is returned. pub fn read_events(&self) -> Result> { let header_size = size_of::(); - let mut buffer = [0u8; 4096]; + const BUFSIZ: usize = 4096; + let mut buffer = [0u8; BUFSIZ]; let mut events = Vec::new(); let mut offset = 0; @@ -181,11 +183,13 @@ impl Inotify { while (nread - offset) >= header_size { let event = unsafe { - &*( - buffer - .as_ptr() - .offset(offset as isize) as *const libc::inotify_event - ) + let mut event = MaybeUninit::::uninit(); + ptr::copy_nonoverlapping( + buffer.as_ptr().offset(offset as isize), + event.as_mut_ptr() as *mut u8, + (BUFSIZ - offset).min(header_size) + ); + event.assume_init() }; let name = match event.len {