Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzzing #187

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target
corpus
artifacts
25 changes: 25 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

[package]
name = "wayland-test-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
publish = false

[package.metadata]
cargo-fuzz = true

[dependencies.wayland-commons]
path = "../wayland-commons/"

[dependencies.wayland-test]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "message_parser"
path = "fuzz_targets/message_parser.rs"
46 changes: 46 additions & 0 deletions fuzz/fuzz_targets/message_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate wayland_commons;

use std::{mem, slice};
use std::os::unix::io::RawFd;
use wayland_commons::wire::{Message, ArgumentType};

unsafe fn convert_slice<T: Sized>(data: &[u8]) -> &[T] {
let n = mem::size_of::<T>();
slice::from_raw_parts(
data.as_ptr() as *const T,
data.len()/n,
)
}

fn get_arg_types(data: &[u8]) -> [ArgumentType; 16] {
use ArgumentType::*;

let mut res = [Int; 16];
assert_eq!(data.len(), 16);
for i in 0..16 {
res[i] = match data[i] & 0b111 {
0 => Int,
1 => Uint,
2 => Fixed,
3 => Str,
4 => Object,
5 => NewId,
6 => Array,
7 => Fd,
_ => unreachable!(),
}
}
res
}

fuzz_target!(|data: &[u8]| {
if data.len() < 32 { return; }
// 4 `RawFd`s
let fds: &[RawFd] = unsafe { convert_slice(&data[..16]) };
// 16 `ArgumentType`s
let args = get_arg_types(&data[16..32]);
let data: &[u32] = unsafe { convert_slice(&data[32..]) };
let _res = Message::from_raw(data, &args, fds);
});
2 changes: 1 addition & 1 deletion wayland-commons/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl Message {
let opcode = (word_2 & 0x0000FFFF) as u16;
let len = (word_2 >> 16) as usize / 4;

if len < 2 {
if len < 2 || len > raw.len() {
return Err(MessageParseError::Malformed);
}

Expand Down