Skip to content

Commit 06ca1fd

Browse files
committed
Handle overflows in FFI integer conversions
1 parent 5f4cf54 commit 06ca1fd

File tree

15 files changed

+112
-120
lines changed

15 files changed

+112
-120
lines changed

boring/src/aes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! ```
3939
//!
4040
use crate::ffi;
41-
use libc::{c_int, c_uint};
41+
use libc::c_int;
4242
use openssl_macros::corresponds;
4343
use std::mem::MaybeUninit;
4444
use std::ptr;
@@ -64,7 +64,7 @@ impl AesKey {
6464
let mut aes_key = MaybeUninit::uninit();
6565
let r = ffi::AES_set_encrypt_key(
6666
key.as_ptr(),
67-
key.len() as c_uint * 8,
67+
(key.len() * 8).try_into().map_err(|_| KeyError(()))?,
6868
aes_key.as_mut_ptr(),
6969
);
7070
if r == 0 {
@@ -88,7 +88,7 @@ impl AesKey {
8888
let mut aes_key = MaybeUninit::uninit();
8989
let r = ffi::AES_set_decrypt_key(
9090
key.as_ptr(),
91-
key.len() as c_uint * 8,
91+
(key.len() * 8).try_into().map_err(|_| KeyError(()))?,
9292
aes_key.as_mut_ptr(),
9393
);
9494

boring/src/bio.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::ffi;
2-
use crate::ffi::BIO_new_mem_buf;
31
use std::marker::PhantomData;
42
use std::ptr;
53
use std::slice;
64

75
use crate::cvt_p;
86
use crate::error::ErrorStack;
7+
use crate::ffi;
8+
use crate::ffi::BIO_new_mem_buf;
9+
use crate::try_int;
910

1011
pub struct MemBioSlice<'a>(*mut ffi::BIO, PhantomData<&'a [u8]>);
1112

@@ -19,15 +20,9 @@ impl Drop for MemBioSlice<'_> {
1920

2021
impl<'a> MemBioSlice<'a> {
2122
pub fn new(buf: &'a [u8]) -> Result<MemBioSlice<'a>, ErrorStack> {
22-
#[cfg(not(feature = "legacy-compat-deprecated"))]
23-
type BufLen = isize;
24-
#[cfg(feature = "legacy-compat-deprecated")]
25-
type BufLen = libc::c_int;
26-
2723
ffi::init();
2824

29-
assert!(buf.len() <= BufLen::MAX as usize);
30-
let bio = unsafe { cvt_p(BIO_new_mem_buf(buf.as_ptr().cast(), buf.len() as BufLen))? };
25+
let bio = unsafe { cvt_p(BIO_new_mem_buf(buf.as_ptr().cast(), try_int(buf.len())?))? };
3126

3227
Ok(MemBioSlice(bio, PhantomData))
3328
}
@@ -63,7 +58,7 @@ impl MemBio {
6358
unsafe {
6459
let mut ptr = ptr::null_mut();
6560
let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
66-
if ptr.is_null() {
61+
if ptr.is_null() || len < 0 {
6762
return &[];
6863
}
6964
slice::from_raw_parts(ptr.cast_const().cast(), len as usize)

boring/src/bn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl BigNumRef {
390390
unsafe {
391391
cvt(ffi::BN_generate_prime_ex(
392392
self.as_ptr(),
393-
bits as c_int,
393+
c_int::from(bits),
394394
c_int::from(safe),
395395
add.map(|n| n.as_ptr()).unwrap_or(ptr::null_mut()),
396396
rem.map(|n| n.as_ptr()).unwrap_or(ptr::null_mut()),

boring/src/dsa.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! using the private key that can be validated with the public key but not be generated
66
//! without the private key.
77
8-
use crate::ffi;
98
use foreign_types::{ForeignType, ForeignTypeRef};
109
use libc::c_uint;
1110
use openssl_macros::corresponds;
@@ -15,7 +14,9 @@ use std::ptr;
1514

1615
use crate::bn::{BigNum, BigNumRef};
1716
use crate::error::ErrorStack;
17+
use crate::ffi;
1818
use crate::pkey::{HasParams, HasPrivate, HasPublic, Private, Public};
19+
use crate::try_int;
1920
use crate::{cvt, cvt_p};
2021

2122
generic_foreign_type_and_impl_send_sync! {
@@ -195,7 +196,7 @@ impl Dsa<Private> {
195196
let dsa = Dsa::from_ptr(cvt_p(ffi::DSA_new())?);
196197
cvt(ffi::DSA_generate_parameters_ex(
197198
dsa.0,
198-
bits as c_uint,
199+
c_uint::from(bits),
199200
ptr::null(),
200201
0,
201202
ptr::null_mut(),

boring/src/ec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//! [`EcGroup`]: struct.EcGroup.html
1616
//! [`Nid`]: ../nid/struct.Nid.html
1717
//! [Eliptic Curve Cryptography]: https://wiki.openssl.org/index.php/Elliptic_Curve_Cryptography
18-
use crate::ffi;
1918
use foreign_types::{ForeignType, ForeignTypeRef};
2019
use libc::c_int;
2120
use openssl_macros::corresponds;
@@ -24,8 +23,10 @@ use std::ptr;
2423

2524
use crate::bn::{BigNumContextRef, BigNumRef};
2625
use crate::error::ErrorStack;
26+
use crate::ffi;
2727
use crate::nid::Nid;
2828
use crate::pkey::{HasParams, HasPrivate, HasPublic, Params, Private, Public};
29+
use crate::try_int;
2930
use crate::{cvt, cvt_n, cvt_p, init};
3031

3132
/// Compressed or Uncompressed conversion

boring/src/hash.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::ffi;
21
use openssl_macros::corresponds;
3-
use std::convert::TryInto;
42
use std::ffi::c_uint;
53
use std::fmt;
64
use std::io;
@@ -9,8 +7,10 @@ use std::ops::{Deref, DerefMut};
97
use std::ptr;
108

119
use crate::error::ErrorStack;
10+
use crate::ffi;
1211
use crate::ffi::{EVP_MD_CTX_free, EVP_MD_CTX_new};
1312
use crate::nid::Nid;
13+
use crate::try_int;
1414
use crate::{cvt, cvt_p};
1515

1616
#[derive(Copy, Clone, PartialEq, Eq)]
@@ -210,7 +210,7 @@ impl Hasher {
210210
self.init()?;
211211
}
212212
unsafe {
213-
let mut len = ffi::EVP_MAX_MD_SIZE.try_into().unwrap();
213+
let mut len = try_int(ffi::EVP_MAX_MD_SIZE)?;
214214
let mut buf = [0; ffi::EVP_MAX_MD_SIZE as usize];
215215
cvt(ffi::EVP_DigestFinal_ex(
216216
self.ctx,
@@ -220,7 +220,7 @@ impl Hasher {
220220
self.state = Finalized;
221221
Ok(DigestBytes {
222222
buf,
223-
len: len as usize,
223+
len: try_int(len)?,
224224
})
225225
}
226226
}

boring/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
201201
}
202202
}
203203

204+
fn try_int<F, T>(from: F) -> Result<T, ErrorStack>
205+
where
206+
F: TryInto<T> + Send + Sync + Copy + 'static,
207+
T: Send + Sync + Copy + 'static,
208+
{
209+
from.try_into()
210+
.map_err(|_| ErrorStack::internal_error_str("int overflow"))
211+
}
212+
204213
unsafe extern "C" fn free_data_box<T>(
205214
_parent: *mut c_void,
206215
ptr: *mut c_void,

boring/src/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ macro_rules! private_key_to_pem {
6060
) -> Result<Vec<u8>, crate::error::ErrorStack> {
6161
unsafe {
6262
let bio = crate::bio::MemBio::new()?;
63-
assert!(passphrase.len() <= ::libc::c_int::MAX as usize);
6463
cvt($f(bio.as_ptr(),
6564
self.as_ptr(),
6665
cipher.as_ptr(),
6766
passphrase.as_ptr() as *const _ as *mut _,
68-
passphrase.len() as ::libc::c_int,
67+
try_int(passphrase.len())?,
6968
None,
7069
ptr::null_mut()))?;
7170
Ok(bio.get_buf().to_owned())

boring/src/pkcs5.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::ffi;
2-
use libc::{c_int, c_uint};
2+
use std::ffi::c_int;
33
use std::ptr;
44

55
use crate::error::ErrorStack;
66
use crate::hash::MessageDigest;
77
use crate::symm::Cipher;
8-
use crate::{cvt, cvt_nz};
8+
use crate::{cvt, cvt_nz, try_int};
99

1010
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
1111
pub struct KeyIvPair {
@@ -90,17 +90,14 @@ pub fn pbkdf2_hmac(
9090
key: &mut [u8],
9191
) -> Result<(), ErrorStack> {
9292
unsafe {
93-
assert!(pass.len() <= c_int::MAX as usize);
94-
assert!(salt.len() <= c_int::MAX as usize);
95-
assert!(key.len() <= c_int::MAX as usize);
96-
9793
ffi::init();
94+
9895
cvt(ffi::PKCS5_PBKDF2_HMAC(
9996
pass.as_ptr().cast(),
10097
pass.len(),
10198
salt.as_ptr(),
10299
salt.len(),
103-
iter as c_uint,
100+
try_int(iter)?,
104101
hash.as_ptr(),
105102
key.len(),
106103
key.as_mut_ptr(),

boring/src/pkey.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
//! println!("{:?}", str::from_utf8(pub_key.as_slice()).unwrap());
4141
//! ```
4242
43-
use crate::ffi;
4443
use foreign_types::{ForeignType, ForeignTypeRef};
4544
use libc::{c_int, c_long};
4645
use openssl_macros::corresponds;
@@ -54,7 +53,9 @@ use crate::dh::Dh;
5453
use crate::dsa::Dsa;
5554
use crate::ec::EcKey;
5655
use crate::error::ErrorStack;
56+
use crate::ffi;
5757
use crate::rsa::Rsa;
58+
use crate::try_int;
5859
use crate::util::{invoke_passwd_cb, CallbackState};
5960
use crate::{cvt, cvt_0i, cvt_p};
6061

0 commit comments

Comments
 (0)