Skip to content

Commit

Permalink
Separate encode/decode functions and add fuzzing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jq-rs committed Sep 16, 2020
1 parent 130e524 commit 39d943f
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 34 deletions.
34 changes: 34 additions & 0 deletions mles-client/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

[package]
name = "mles-client-fuzz"
version = "0.1.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.3"
mles-utils = "1.1"
bytes = "0.4"

[dependencies.mles-client]
path = ".."

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

[[bin]]
name = "mles_client_fuzz_decode"
path = "fuzz_targets/mles_client_fuzz_decode.rs"
test = false
doc = false

[[bin]]
name = "mles_client_fuzz_encode"
path = "fuzz_targets/mles_client_fuzz_encode.rs"
test = false
doc = false
68 changes: 68 additions & 0 deletions mles-client/fuzz/fuzz_targets/codecs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2016 Alex Crichton
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Mles-support Copyright (c) 2020 Mles developers
*
*/
use bytes::BytesMut;
use std::io::{self};
use mles_utils::MsgHdr;

pub fn msg_decode(buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
// HDRKEYL is header min size
if buf.len() >= MsgHdr::get_hdrkey_len() {
let msghdr = MsgHdr::decode(buf.to_vec());
if msghdr.get_type() != b'M' {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let hdr_len = msghdr.get_len() as usize;
if 0 == hdr_len {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let len = buf.len();
if len < (MsgHdr::get_hdrkey_len() + hdr_len) {
return Ok(None);
}
if MsgHdr::get_hdrkey_len() + hdr_len < len {
buf.split_to(MsgHdr::get_hdrkey_len());
return Ok(Some(buf.split_to(hdr_len)));
}
buf.split_to(MsgHdr::get_hdrkey_len());
return Ok(Some(buf.split_to(hdr_len)));
}
Ok(None)
}

pub fn msg_encode(data: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> {
buf.extend_from_slice(&data[..]);
Ok(())
}

14 changes: 14 additions & 0 deletions mles-client/fuzz/fuzz_targets/mles_client_fuzz_decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_main]
#![allow(dead_code)]
use libfuzzer_sys::fuzz_target;
use bytes::{BytesMut, BufMut};

mod codecs;
use crate::codecs::msg_decode;

fuzz_target!(|data: &[u8]| {
// fuzzed code goes here
let mut buf = BytesMut::with_capacity(data.len());
buf.put_slice(data);
let _ = msg_decode(&mut buf);
});
13 changes: 13 additions & 0 deletions mles-client/fuzz/fuzz_targets/mles_client_fuzz_encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_main]
#![allow(dead_code)]
use libfuzzer_sys::fuzz_target;
use bytes::BytesMut;

mod codecs;
use crate::codecs::msg_encode;

fuzz_target!(|data: &[u8]| {
// fuzzed code goes here
let mut buf = BytesMut::new();
let _ = msg_encode(data.to_vec(), &mut buf);
});
68 changes: 68 additions & 0 deletions mles-client/src/codecs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2016 Alex Crichton
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Mles-support Copyright (c) 2020 Mles developers
*
*/
use bytes::BytesMut;
use std::io::{self};
use mles_utils::MsgHdr;

pub fn msg_decode(buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
// HDRKEYL is header min size
if buf.len() >= MsgHdr::get_hdrkey_len() {
let msghdr = MsgHdr::decode(buf.to_vec());
if msghdr.get_type() != b'M' {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let hdr_len = msghdr.get_len() as usize;
if 0 == hdr_len {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let len = buf.len();
if len < (MsgHdr::get_hdrkey_len() + hdr_len) {
return Ok(None);
}
if MsgHdr::get_hdrkey_len() + hdr_len < len {
buf.split_to(MsgHdr::get_hdrkey_len());
return Ok(Some(buf.split_to(hdr_len)));
}
buf.split_to(MsgHdr::get_hdrkey_len());
return Ok(Some(buf.split_to(hdr_len)));
}
Ok(None)
}

pub fn msg_encode(data: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> {
buf.extend_from_slice(&data[..]);
Ok(())
}

38 changes: 4 additions & 34 deletions mles-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Mles-support Copyright (c) 2017-2019 Mles developers
* Mles-support Copyright (c) 2017-2020 Mles developers
*
*/
/*
* Mles client example based on Tokio core-connect example.
*/
mod ws;
mod codecs;

use std::io::{self, Read, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
Expand Down Expand Up @@ -194,42 +195,12 @@ fn main() {
}

struct Bytes;

fn msg_decode(msghdr: MsgHdr, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
if msghdr.get_type() != b'M' {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let hdr_len = msghdr.get_len() as usize;
if 0 == hdr_len {
let len = buf.len();
buf.split_to(len);
return Ok(None);
}
let len = buf.len();
if len < (MsgHdr::get_hdrkey_len() + hdr_len) {
return Ok(None);
}
if MsgHdr::get_hdrkey_len() + hdr_len < len {
buf.split_to(MsgHdr::get_hdrkey_len());
return Ok(Some(buf.split_to(hdr_len)));
}
buf.split_to(MsgHdr::get_hdrkey_len());
Ok(Some(buf.split_to(hdr_len)))
}

impl Decoder for Bytes {
type Item = BytesMut;
type Error = io::Error;

fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
// HDRKEYL is header min size
if buf.len() >= MsgHdr::get_hdrkey_len() {
let msghdr = MsgHdr::decode(buf.to_vec());
return msg_decode(msghdr, buf);
}
Ok(None)
codecs::msg_decode(buf)
}

fn decode_eof(&mut self, buf: &mut BytesMut) -> io::Result<Option<BytesMut>> {
Expand All @@ -242,8 +213,7 @@ impl Encoder for Bytes {
type Error = io::Error;

fn encode(&mut self, data: Vec<u8>, buf: &mut BytesMut) -> io::Result<()> {
buf.extend_from_slice(&data[..]);
Ok(())
codecs::msg_encode(data, buf)
}
}

Expand Down

0 comments on commit 39d943f

Please sign in to comment.