Skip to content

Commit fc8498a

Browse files
committed
reduced max message buffer size, use pod_read_unaligned in some places
1 parent abef920 commit fc8498a

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

decoder/decoder/src/crypto.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::DecoderError;
2-
use bytemuck::{from_bytes, Pod, Zeroable};
2+
use bytemuck::{pod_read_unaligned, Pod, Zeroable};
33
use chacha20poly1305::{AeadInPlace, KeyInit, XChaCha20Poly1305};
44
use ed25519_dalek::{Signature, Verifier, VerifyingKey, SIGNATURE_LENGTH};
55
use rand_chacha::ChaCha20Rng;
@@ -42,7 +42,7 @@ pub fn decrypt_decoder_payload<'a>(
4242
return Err(DecoderError::InvalidEncoderPayload);
4343
}
4444

45-
let header: DecoderPayloadHeader = *from_bytes(&payload[..size_of::<DecoderPayloadHeader>()]);
45+
let header: DecoderPayloadHeader = pod_read_unaligned(&payload[..size_of::<DecoderPayloadHeader>()]);
4646

4747
// first verify signature
4848
// signature should include chacha nonce and tag, otherwise attacker can alter nonce and get invalid frame
@@ -71,12 +71,12 @@ pub fn decrypt_decoder_payload<'a>(
7171
}
7272

7373
/// Gets a reference to the associated data of the given decoder payload.
74-
pub fn get_decoder_payload_associated_data<T: Pod>(payload: &[u8]) -> Result<&T, DecoderError> {
74+
pub fn get_decoder_payload_associated_data<T: Pod>(payload: &[u8]) -> Result<T, DecoderError> {
7575
if payload.len() < size_of::<DecoderPayloadHeader>() + size_of::<T>() {
7676
Err(DecoderError::InvalidEncoderPayload)
7777
} else {
7878
let associated_data = &payload[payload.len() - size_of::<T>()..];
79-
Ok(from_bytes(associated_data))
79+
Ok(pod_read_unaligned(associated_data))
8080
}
8181
}
8282

decoder/decoder/src/decode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct FrameData {
3434

3535
/// Performs all functionality related to decoding frames.
3636
pub fn decode(context: &mut DecoderContext, encoded_frame: &mut [u8]) -> Result<(), DecoderError> {
37-
let frame_info: FrameAssociatedData = *get_decoder_payload_associated_data(encoded_frame)?;
37+
let frame_info: FrameAssociatedData = get_decoder_payload_associated_data(encoded_frame)?;
3838

3939
// check frame we are decoding is monotonically increasing for security requirement 3
4040
if context
@@ -57,6 +57,7 @@ pub fn decode(context: &mut DecoderContext, encoded_frame: &mut [u8]) -> Result<
5757
&public_key,
5858
)?;
5959

60+
// shouldn't have alignmanet issues, frame data is 1 byte aligned
6061
let frame_data: &FrameData = try_from_bytes(frame_data)?;
6162

6263
// decoding succeeded, update last decoded timestamp

decoder/decoder/src/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ impl Opcode {
7979
}
8080

8181
pub const MAGIC: u8 = b'%';
82-
// 5 KiB
83-
pub const MAX_BODY_SIZE: usize = 5120;
82+
// 4.5 KiB
83+
pub const MAX_BODY_SIZE: usize = 4608;
8484
const CHUNK_SIZE: usize = 256;
8585

8686
/// Opcodes that don't require an ack response.

decoder/decoder/src/subscribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn subscribe(
8282
context: &mut DecoderContext,
8383
subscribe_data: &mut [u8],
8484
) -> Result<(), DecoderError> {
85-
let associated_data: SubscriptionAssociatedData = *get_decoder_payload_associated_data(subscribe_data)?;
85+
let associated_data: SubscriptionAssociatedData = get_decoder_payload_associated_data(subscribe_data)?;
8686
if associated_data.decoder_id != DECODER_ID {
8787
return Err(DecoderError::InvalidSubscription);
8888
}

0 commit comments

Comments
 (0)