Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe i32 to usize conversion from network data #2788

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,46 @@ pub type Result<T> = std::result::Result<T, self::Error>;
// Re-export ordered-float, since it is used by the generator
// FIXME: check the guidance around type reexports
pub use ordered_float::OrderedFloat;

use std::io::{Error as IoErr, ErrorKind as IoErrKind};
use std::ops::RangeBounds;

/// A trait similar to try-from but with additional checks
pub trait TryIntoRange<T>: TryInto<T> {
type ErrTy;

fn try_into_range(self, between: impl RangeBounds<Self>)
-> std::result::Result<T, Self::ErrTy>;
}

impl TryIntoRange<usize> for i32 {
type ErrTy = IoErr;

fn try_into_range(
self,
between: impl RangeBounds<Self>,
) -> std::result::Result<usize, Self::ErrTy> {
if between.contains(&self) {
self.try_into()
.map_err(|_| IoErr::from(IoErrKind::InvalidData))
} else {
Err(IoErr::from(IoErrKind::InvalidData))
}
}
}

impl TryIntoRange<usize> for u32 {
type ErrTy = IoErr;

fn try_into_range(
self,
between: impl RangeBounds<Self>,
) -> std::result::Result<usize, Self::ErrTy> {
if between.contains(&self) {
self.try_into()
.map_err(|_| IoErr::from(IoErrKind::InvalidData))
} else {
Err(IoErr::from(IoErrKind::InvalidData))
}
}
}
8 changes: 6 additions & 2 deletions lib/rs/src/protocol/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::{
};
use super::{TOutputProtocol, TOutputProtocolFactory, TSetIdentifier, TStructIdentifier, TType};
use crate::transport::{TReadTransport, TWriteTransport};
use crate::TryIntoRange;
use crate::{ProtocolError, ProtocolErrorKind};

const BINARY_PROTOCOL_VERSION_1: u32 = 0x8001_0000;
Expand Down Expand Up @@ -112,7 +113,7 @@ where
// in the non-strict version the first message field
// is the message name. strings (byte arrays) are length-prefixed,
// so we've just read the length in the first 4 bytes
let name_size = BigEndian::read_i32(&first_bytes) as usize;
let name_size = BigEndian::read_i32(&first_bytes).try_into_range(0..=i32::MAX)?;
let mut name_buf: Vec<u8> = vec![0; name_size];
self.transport.read_exact(&mut name_buf)?;
let name = String::from_utf8(name_buf)?;
Expand Down Expand Up @@ -154,7 +155,10 @@ where
}

fn read_bytes(&mut self) -> crate::Result<Vec<u8>> {
let num_bytes = self.transport.read_i32::<BigEndian>()? as usize;
let num_bytes = self
.transport
.read_i32::<BigEndian>()?
.try_into_range(0..=i32::MAX)?;
let mut buf = vec![0u8; num_bytes];
self.transport
.read_exact(&mut buf)
Expand Down
11 changes: 7 additions & 4 deletions lib/rs/src/transport/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
// specific language governing permissions and limitations
// under the License.

use super::{TReadTransport, TReadTransportFactory, TWriteTransport, TWriteTransportFactory};
use crate::TryIntoRange;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::cmp;
use std::io;
use std::io::{Read, Write};

use super::{TReadTransport, TReadTransportFactory, TWriteTransport, TWriteTransportFactory};

/// Default capacity of the read buffer in bytes.
const READ_CAPACITY: usize = 4096;

Expand Down Expand Up @@ -91,13 +91,16 @@ where
{
fn read(&mut self, b: &mut [u8]) -> io::Result<usize> {
if self.cap - self.pos == 0 {
let message_size = self.chan.read_i32::<BigEndian>()? as usize;
let message_size = self
.chan
.read_i32::<BigEndian>()?
.try_into_range(0..=i32::MAX)?; // Range should be smaller?

let buf_capacity = cmp::max(message_size, READ_CAPACITY);
self.buf.resize(buf_capacity, 0);

self.chan.read_exact(&mut self.buf[..message_size])?;
self.cap = message_size as usize;
self.cap = message_size;
self.pos = 0;
}

Expand Down