detect/asn1: Update ASN1 struct lifetime

- 'static is only realistic when allocating and leaking it over the
FFI boundary
pull/5149/head
Emmanuel Thompson 5 years ago committed by Victor Julien
parent 627e90a4bd
commit 4fc45b5c60

@ -25,7 +25,7 @@ use parse_rules::DetectAsn1Data;
/// Container for parsed Asn1 objects /// Container for parsed Asn1 objects
#[derive(Debug)] #[derive(Debug)]
pub struct Asn1(Vec<BerObject<'static>>); pub struct Asn1<'a>(Vec<BerObject<'a>>);
/// Errors possible during decoding of Asn1 /// Errors possible during decoding of Asn1
#[derive(Debug)] #[derive(Debug)]
@ -70,7 +70,7 @@ impl std::fmt::Display for Asn1CheckError {
} }
} }
impl Asn1 { impl<'a> Asn1<'a> {
/// Checks each BerObject contained in self with the provided detection /// Checks each BerObject contained in self with the provided detection
/// data, returns the first successful match if one occurs /// data, returns the first successful match if one occurs
fn check(&self, ad: &DetectAsn1Data) -> Result<Option<Asn1Check>, Asn1CheckError> { fn check(&self, ad: &DetectAsn1Data) -> Result<Option<Asn1Check>, Asn1CheckError> {
@ -161,7 +161,7 @@ impl Asn1 {
None None
} }
fn from_slice(input: &'static [u8], ad: &DetectAsn1Data) -> Result<Asn1, Asn1DecodeError> { fn from_slice(input: &'a [u8], ad: &DetectAsn1Data) -> Result<Asn1<'a>, Asn1DecodeError> {
let mut results = Vec::new(); let mut results = Vec::new();
let mut rest = input; let mut rest = input;
@ -196,11 +196,11 @@ impl Asn1 {
/// Decodes Asn1 objects from an input + length while applying the offset /// Decodes Asn1 objects from an input + length while applying the offset
/// defined in the asn1 keyword options /// defined in the asn1 keyword options
fn asn1_decode( fn asn1_decode<'a>(
buffer: &'static [u8], buffer: &'a [u8],
buffer_offset: u32, buffer_offset: u32,
ad: &DetectAsn1Data, ad: &DetectAsn1Data,
) -> Result<Asn1, Asn1DecodeError> { ) -> Result<Asn1<'a>, Asn1DecodeError> {
// Get offset // Get offset
let offset = if let Some(absolute_offset) = ad.absolute_offset { let offset = if let Some(absolute_offset) = ad.absolute_offset {
absolute_offset absolute_offset
@ -243,19 +243,19 @@ fn asn1_decode(
/// input must be a valid buffer of at least input_len bytes /// input must be a valid buffer of at least input_len bytes
/// pointer must be freed using `rs_asn1_free` /// pointer must be freed using `rs_asn1_free`
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rs_asn1_decode( pub extern "C" fn rs_asn1_decode(
input: *const u8, input: *const u8,
input_len: u16, input_len: u16,
buffer_offset: u32, buffer_offset: u32,
ad_ptr: *const DetectAsn1Data, ad_ptr: *const DetectAsn1Data,
) -> *mut Asn1 { ) -> *mut Asn1<'static> {
if input.is_null() || input_len == 0 || ad_ptr.is_null() { if input.is_null() || input_len == 0 || ad_ptr.is_null() {
return std::ptr::null_mut(); return std::ptr::null_mut();
} }
let slice = build_slice!(input, input_len as usize); let slice = build_slice!(input, input_len as usize);
let ad = &*ad_ptr; let ad = unsafe { &*ad_ptr };
let res = asn1_decode(slice, buffer_offset, ad); let res = asn1_decode(slice, buffer_offset, ad);

Loading…
Cancel
Save