-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate encode/decode functions and add fuzzing.
- Loading branch information
Showing
6 changed files
with
201 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters