Skip to content

Commit

Permalink
Trun UnsanitizedPacketOffsets into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Oct 16, 2019
1 parent f10a36d commit 2d0cb53
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions core/src/sigverify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,36 @@ impl PacketOffsets {
pubkey_start,
}
}

pub fn new_with_packet(packet_offsets: PacketOffsets) -> Self {
Self::new(
packet_offsets.sig_len,
packet_offsets.sig_start,
packet_offsets.msg_start,
packet_offsets.pubkey_start,
)
}
}

type UnsanitizedPacketOffsets = (bool, u32, u32, u32, u32);
struct UnsanitizedPacketOffsets {
pub correct: bool,
pub packet_offsets: PacketOffsets,
}

impl UnsanitizedPacketOffsets {
pub fn new(
correct: bool,
sig_len: u32,
sig_start: u32,
msg_start: u32,
pubkey_start: u32,
) -> Self {
Self {
correct,
packet_offsets: PacketOffsets::new(sig_len, sig_start, msg_start, pubkey_start),
}
}
}

pub fn init() {
if let Some(api) = perf_libs::api() {
Expand Down Expand Up @@ -128,7 +155,7 @@ fn do_get_packet_offsets(packet: &Packet, current_offset: u32) -> UnsanitizedPac
let pubkey_start = msg_start + msg_header_size + pubkey_len_size;

if sig_len_maybe_trusted == sig_len_untrusted {
(
UnsanitizedPacketOffsets::new(
true,
sig_len_maybe_trusted as u32,
sig_start as u32,
Expand All @@ -137,7 +164,7 @@ fn do_get_packet_offsets(packet: &Packet, current_offset: u32) -> UnsanitizedPac
)
} else {
// a malformed packet is detected!!
(
UnsanitizedPacketOffsets::new(
false,
sig_len_untrusted as u32,
sig_start as u32,
Expand All @@ -148,9 +175,9 @@ fn do_get_packet_offsets(packet: &Packet, current_offset: u32) -> UnsanitizedPac
}

fn get_packet_offsets(packet: &Packet, current_offset: u32) -> PacketOffsets {
let ret = do_get_packet_offsets(packet, current_offset);
if ret.0 {
PacketOffsets::new(ret.1, ret.2, ret.3, ret.4)
let unsanitized_packet_offsets = do_get_packet_offsets(packet, current_offset);
if unsanitized_packet_offsets.correct {
PacketOffsets::new_with_packet(unsanitized_packet_offsets.packet_offsets)
} else {
// force sigverify to fail by returning zeros
PacketOffsets::new(0, 0, 0, 0)
Expand Down Expand Up @@ -429,11 +456,13 @@ mod tests {
tx.signatures = vec![Signature::default(); actual_num_sigs as usize];
let packet = sigverify::make_packet_from_transaction(tx.clone());

let (trustworthy, sig_len, _sig_start, _msg_start_offset, _pubkey_offset) =
sigverify::do_get_packet_offsets(&packet, 0);
let unsanitized_packet_offsets = sigverify::do_get_packet_offsets(&packet, 0);

assert_eq!(trustworthy, false);
assert_eq!(sig_len, actual_num_sigs);
assert_eq!(unsanitized_packet_offsets.correct, false);
assert_eq!(
unsanitized_packet_offsets.packet_offsets.sig_len,
actual_num_sigs
);
}

#[test]
Expand All @@ -457,8 +486,7 @@ mod tests {
tx.signatures = vec![Signature::default(); actual_num_sigs];
let packet = sigverify::make_packet_from_transaction(tx.clone());

let (_trustworthy, _sig_len, _sig_start, _msg_start_starl, actual_pubkey_start) =
sigverify::do_get_packet_offsets(&packet, 0);
let unsanitized_packet_offsets = sigverify::do_get_packet_offsets(&packet, 0);

let expected_sig_size = 1;
let expected_sigs_size = actual_num_sigs * size_of::<Signature>();
Expand All @@ -469,7 +497,10 @@ mod tests {
+ expected_msg_header_size
+ expected_pubkey_size;

assert_eq!(expected_pubkey_start, actual_pubkey_start as usize);
assert_eq!(
expected_pubkey_start,
unsanitized_packet_offsets.packet_offsets.pubkey_start as usize
);
}

#[test]
Expand Down

0 comments on commit 2d0cb53

Please sign in to comment.