From a28d2581cbf8fac52f705adbbd08d004dec2992c Mon Sep 17 00:00:00 2001 From: Evgeny Kurnevsky Date: Fri, 26 Oct 2018 12:57:35 +0300 Subject: [PATCH 1/3] fix(tcp_server): allow connection_id higher 0xF0 --- src/toxcore/tcp/packet/data.rs | 4 ++-- src/toxcore/tcp/packet/disconnect_notification.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/toxcore/tcp/packet/data.rs b/src/toxcore/tcp/packet/data.rs index 10a0100d2..f12a781ca 100644 --- a/src/toxcore/tcp/packet/data.rs +++ b/src/toxcore/tcp/packet/data.rs @@ -13,7 +13,7 @@ Serialized form: Length | Content -------- | ------ -`1` | connection_id [ `0x10` .. `0xF0` ) +`1` | connection_id [ `0x10` .. `0xFF` ] variable | Data */ @@ -27,7 +27,7 @@ pub struct Data { impl FromBytes for Data { named!(from_bytes, do_parse!( - connection_id: verify!(be_u8, |id| id >= 0x10 && id < 0xF0) >> + connection_id: verify!(be_u8, |id| id >= 0x10) >> data: rest >> (Data { connection_id, data: data.to_vec() }) )); diff --git a/src/toxcore/tcp/packet/disconnect_notification.rs b/src/toxcore/tcp/packet/disconnect_notification.rs index d13a944bf..d5f7720e9 100644 --- a/src/toxcore/tcp/packet/disconnect_notification.rs +++ b/src/toxcore/tcp/packet/disconnect_notification.rs @@ -23,7 +23,7 @@ Serialized form: Length | Content ------ | ------ `1` | `0x03` -`1` | connection_id +`1` | connection_id [ `0x10` .. `0xFF` ] */ #[derive(Debug, PartialEq, Clone)] @@ -35,7 +35,7 @@ pub struct DisconnectNotification { impl FromBytes for DisconnectNotification { named!(from_bytes, do_parse!( tag!("\x03") >> - connection_id: verify!(be_u8, |id| id >= 0x10 && id < 0xF0) >> + connection_id: verify!(be_u8, |id| id >= 0x10) >> (DisconnectNotification { connection_id }) )); } From 590eeb2b205b31ab9255a922511ecd11be14301d Mon Sep 17 00:00:00 2001 From: Evgeny Kurnevsky Date: Fri, 26 Oct 2018 21:28:15 +0300 Subject: [PATCH 2/3] fix(tcp_server): fix tests --- src/toxcore/tcp/codec.rs | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/toxcore/tcp/codec.rs b/src/toxcore/tcp/codec.rs index f24aeb7c8..c5aae076a 100644 --- a/src/toxcore/tcp/codec.rs +++ b/src/toxcore/tcp/codec.rs @@ -335,50 +335,29 @@ mod tests { // Mallory cannot decode the payload of EncryptedPacket assert!(mallory_codec.decode(&mut buf).err().is_some()); } - fn encode_bytes_to_packet(channel: &Channel, bytes: &[u8]) -> Vec { - // encrypt it - let encrypted = channel.encrypt(bytes); - - // create EncryptedPacket - let encrypted_packet = EncryptedPacket { payload: encrypted }; - - // serialize EncryptedPacket to binary form - let mut stack_buf = [0; MAX_TCP_ENC_PACKET_SIZE]; - let (_, encrypted_packet_size) = encrypted_packet.to_bytes((&mut stack_buf, 0)).unwrap(); - stack_buf[..encrypted_packet_size].to_vec() - } #[test] fn decode_packet_imcomplete() { - let (alice_channel, bob_channel) = create_channels(); + let (alice_channel, _) = create_channels(); - let mut buf = BytesMut::from(encode_bytes_to_packet(&alice_channel,b"\x00")); - let mut bob_codec = Codec::new(bob_channel); + let mut buf = BytesMut::new(); + let mut bob_codec = Codec::new(alice_channel); - // not enought bytes to decode Packet - assert!(bob_codec.decode(&mut buf).err().is_some()); + // not enough bytes to decode Packet + assert!(bob_codec.decode(&mut buf).unwrap().is_none()); } #[test] fn decode_packet_error() { - let (alice_channel, bob_channel) = create_channels(); + let (alice_channel, _) = create_channels(); let mut alice_codec = Codec::new(alice_channel); - let mut bob_codec = Codec::new(bob_channel); let mut buf = BytesMut::new(); - // bad Data with connection id = 0x0F - let packet = Packet::Data( Data { connection_id: 0x0F, data: vec![13; 42] } ); + // bad Data with connection id = 0 + let packet = Packet::Data( Data { connection_id: 0, data: vec![13; 42] } ); alice_codec.encode(packet.clone(), &mut buf).expect("Alice should encode"); - assert!(bob_codec.decode(&mut buf).is_err()); - - buf.clear(); - - // bad Data with connection id = 0xF0 - let packet = Packet::Data( Data { connection_id: 0xF0, data: vec![13; 42] } ); - - alice_codec.encode(packet.clone(), &mut buf).expect("Alice should encode"); - assert!(bob_codec.decode(&mut buf).is_err()); + assert!(alice_codec.decode(&mut buf).is_err()); } #[test] From e2fac59c55c7f7c01ea979f6d0904b0160053d11 Mon Sep 17 00:00:00 2001 From: Evgeny Kurnevsky Date: Fri, 26 Oct 2018 13:00:33 +0300 Subject: [PATCH 3/3] fix(tcp_server): verify connection_id in ConnectNotification and RouteResponse packets --- src/toxcore/tcp/packet/connect_notification.rs | 4 ++-- src/toxcore/tcp/packet/route_response.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/toxcore/tcp/packet/connect_notification.rs b/src/toxcore/tcp/packet/connect_notification.rs index d0f8f08e3..b8b1cec0b 100644 --- a/src/toxcore/tcp/packet/connect_notification.rs +++ b/src/toxcore/tcp/packet/connect_notification.rs @@ -13,7 +13,7 @@ Serialized form: Length | Content ------ | ------ `1` | `0x02` -`1` | connection_id +`1` | connection_id [ `0x10` .. `0xFF` ] */ #[derive(Debug, PartialEq, Clone)] @@ -25,7 +25,7 @@ pub struct ConnectNotification { impl FromBytes for ConnectNotification { named!(from_bytes, do_parse!( tag!("\x02") >> - connection_id: be_u8 >> + connection_id: verify!(be_u8, |id| id >= 0x10) >> (ConnectNotification { connection_id }) )); } diff --git a/src/toxcore/tcp/packet/route_response.rs b/src/toxcore/tcp/packet/route_response.rs index 979d210dd..018f4134e 100644 --- a/src/toxcore/tcp/packet/route_response.rs +++ b/src/toxcore/tcp/packet/route_response.rs @@ -19,7 +19,7 @@ Serialized form: Length | Content ------ | ------ `1` | `0x01` -`1` | connection_id +`1` | connection_id [ `0x10` .. `0xFF` ] `32` | Public Key */ @@ -34,7 +34,7 @@ pub struct RouteResponse { impl FromBytes for RouteResponse { named!(from_bytes, do_parse!( tag!("\x01") >> - connection_id: be_u8 >> + connection_id: verify!(be_u8, |id| id >= 0x10) >> pk: call!(PublicKey::from_bytes) >> (RouteResponse { connection_id, pk }) ));