Skip to content

Commit

Permalink
Fix an unaligned pointer read in Inotify::read_events
Browse files Browse the repository at this point in the history
Reported-by: Clippy
  • Loading branch information
asomers committed May 16, 2020
1 parent 465a8f7 commit 511c1eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
18 changes: 11 additions & 7 deletions src/sys/inotify.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -173,19 +174,22 @@ impl Inotify {
/// events could be read then the EAGAIN error is returned.
pub fn read_events(&self) -> Result<Vec<InotifyEvent>> {
let header_size = size_of::<libc::inotify_event>();
let mut buffer = [0u8; 4096];
const BUFSIZ: usize = 4096;
let mut buffer = [0u8; BUFSIZ];
let mut events = Vec::new();
let mut offset = 0;

let nread = read(self.fd, &mut buffer)?;

while (nread - offset) >= header_size {
let event = unsafe {
&*(
buffer
.as_ptr()
.offset(offset as isize) as *const libc::inotify_event
)
let mut event = MaybeUninit::<libc::inotify_event>::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 {
Expand Down

0 comments on commit 511c1eb

Please sign in to comment.