rust: format detect files

Ticket: 3836
pull/15636/head
Philippe Antoine 3 months ago committed by Victor Julien
parent 8a4eea44d8
commit e0152178da

@ -98,7 +98,8 @@ fn parse_byteextract(input: &str) -> IResult<&str, SCDetectByteExtractData, Rule
let (_, values) = nom8::multi::separated_list1(
tag(","),
preceded(multispace0, nom8::bytes::complete::is_not(",")),
).parse(input)?;
)
.parse(input)?;
if values.len() < DETECT_BYTE_EXTRACT_FIXED_PARAM_COUNT
|| values.len() > DETECT_BYTE_EXTRACT_MAX_PARAM_COUNT

@ -158,7 +158,8 @@ fn parse_bytemath(input: &str) -> IResult<&str, DetectByteMathData, RuleParseErr
let (_, values) = nom8::multi::separated_list1(
tag(","),
preceded(multispace0, nom8::bytes::complete::is_not(",")),
).parse(input)?;
)
.parse(input)?;
if values.len() < DETECT_BYTEMATH_FIXED_PARAM_COUNT
|| values.len() > DETECT_BYTEMATH_MAX_PARAM_COUNT
@ -350,7 +351,7 @@ fn parse_bytemath(input: &str) -> IResult<&str, DetectByteMathData, RuleParseErr
// Using left/right shift further restricts the value of nbytes. Note that
// validation has already ensured nbytes is in [1..10]
match byte_math.oper {
ByteMathOperator::LeftShift | ByteMathOperator::RightShift if byte_math.nbytes > 4 => {
ByteMathOperator::LeftShift | ByteMathOperator::RightShift if byte_math.nbytes > 4 => {
return Err(make_error(format!("nbytes must be 1 through 4 (inclusive) when used with \"<<\" or \">>\"; {} is not valid", byte_math.nbytes)));
}
_ => {}

@ -27,7 +27,7 @@ use nom8::sequence::preceded;
use nom8::{Err, IResult, Parser};
use std::ffi::CStr;
use std::os::raw::{c_double, c_char, c_void};
use std::os::raw::{c_char, c_double, c_void};
use std::slice;
#[repr(C)]
@ -74,7 +74,8 @@ fn parse_entropy<'a>(
let (_, values) = nom8::multi::separated_list1(
tag(","),
preceded(multispace0, nom8::bytes::complete::is_not(",")),
).parse(input)?;
)
.parse(input)?;
if values.len() < DETECT_ENTROPY_FIXED_PARAM_COUNT
|| values.len() > DETECT_ENTROPY_MAX_PARAM_COUNT
@ -168,8 +169,7 @@ fn calculate_entropy(data: &[u8]) -> f64 {
#[no_mangle]
pub unsafe extern "C" fn SCDetectEntropyMatch(
c_data: *const c_void, length: i32, ctx: &DetectEntropyData,
calculated_entropy: *mut c_double,
c_data: *const c_void, length: i32, ctx: &DetectEntropyData, calculated_entropy: *mut c_double,
) -> bool {
if c_data.is_null() {
return false;

@ -94,18 +94,15 @@ pub fn parse_float_value<T: DetectFloatType>(input: &str) -> IResult<&str, T> {
// Handle numeric parsing, including scientific notation
map_opt(
recognize((
opt(alt((tag("+"), tag("-")))), // Handle optional signs
opt(alt((tag("+"), tag("-")))), // Handle optional signs
alt((digit1, recognize((tag("."), digit1)))), // Handle integers & `.5`
opt((tag("."), digit1)), // Handle decimals like `5.`
opt((
tag_no_case("e"),
opt(alt((tag("+"), tag("-")))),
digit1,
)), // Handle `1e10`, `-1e-5`
opt((tag("."), digit1)), // Handle decimals like `5.`
opt((tag_no_case("e"), opt(alt((tag("+"), tag("-")))), digit1)), // Handle `1e10`, `-1e-5`
)),
|float_str: &str| <T as DetectFloatType>::from_str(float_str),
),
)).parse(input)
))
.parse(input)
}
fn detect_parse_float_start_equal<T: DetectFloatType>(
i: &str,
@ -133,7 +130,8 @@ pub fn detect_parse_float_start_interval<T: DetectFloatType>(
let (i, _) = opt(is_a(" ")).parse(i)?;
let (i, arg2) = verify(parse_float_value::<T>, |x| {
*x > arg1 && *x - arg1 > <T as FloatCore>::epsilon()
}).parse(i)?;
})
.parse(i)?;
let mode = if neg.is_some() {
DetectFloatMode::DetectFloatModeNegRg
} else {
@ -150,7 +148,8 @@ fn detect_parse_float_mode(i: &str) -> IResult<&str, DetectFloatMode> {
value(DetectFloatMode::DetectFloatModeLt, tag("<")),
value(DetectFloatMode::DetectFloatModeNe, tag("!=")),
value(DetectFloatMode::DetectFloatModeEqual, tag("=")),
)).parse(i)?;
))
.parse(i)?;
Ok((i, mode))
}
@ -223,7 +222,8 @@ fn detect_parse_float_notending<T: DetectFloatType>(i: &str) -> IResult<&str, De
detect_parse_float_start_interval,
detect_parse_float_start_equal,
detect_parse_float_start_symbol,
)).parse(i)?;
))
.parse(i)?;
Ok((i, float))
}

@ -77,7 +77,8 @@ pub fn detect_parse_iprep(i: &str) -> IResult<&str, DetectIPRepData, RuleParseEr
let (_, values) = nom8::multi::separated_list1(
tag(","),
preceded(multispace0, nom8::bytes::complete::is_not(",")),
).parse(i)?;
)
.parse(i)?;
let args = values.len();
if args == 4 || args == 3 {
@ -112,26 +113,43 @@ pub fn detect_parse_iprep(i: &str) -> IResult<&str, DetectIPRepData, RuleParseEr
arg2: 0,
mode,
};
return Ok((i, DetectIPRepData { du8, cat, cmd, isnotset: false, }));
return Ok((
i,
DetectIPRepData {
du8,
cat,
cmd,
isnotset: false,
},
));
} else {
let (isnotset, mode, arg1) = match values[2].trim() {
"isset" => { (false, DetectUintMode::DetectUintModeGte, 0) },
"isnotset" => { (true, DetectUintMode::DetectUintModeEqual, 0) },
_ => { return Err(make_error("invalid mode".to_string())); },
"isset" => (false, DetectUintMode::DetectUintModeGte, 0),
"isnotset" => (true, DetectUintMode::DetectUintModeEqual, 0),
_ => {
return Err(make_error("invalid mode".to_string()));
}
};
let du8 = DetectUintData::<u8> {
arg1,
arg2: 0,
mode,
};
return Ok((i, DetectIPRepData { du8, cat, cmd, isnotset, }));
return Ok((
i,
DetectIPRepData {
du8,
cat,
cmd,
isnotset,
},
));
}
} else if args < 3 {
return Err(make_error("too few arguments".to_string()));
} else {
} else {
return Err(make_error("too many arguments".to_string()));
}
}
#[no_mangle]

@ -194,7 +194,8 @@ fn parse_op(input: &str) -> IResult<&str, VersionCompareOp> {
map(tag("<="), |_| VersionCompareOp::Lte),
map(tag("<"), |_| VersionCompareOp::Lt),
)),
).parse(input)
)
.parse(input)
}
/// Parse the next part of the version.
@ -204,7 +205,8 @@ fn parse_next_version_part(input: &str) -> IResult<&str, u8> {
map_res(
take_till(|c| c == '.' || c == '-' || c == ' '),
|s: &str| s.parse::<u8>(),
).parse(input)
)
.parse(input)
}
/// Parse a version string into a SuricataVersion.
@ -229,7 +231,8 @@ fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> {
let (input, key) = preceded(
multispace0,
take_while(|c: char| c.is_alphanumeric() || c == '-' || c == '_'),
).parse(input)?;
)
.parse(input)?;
let (input, value) = preceded(multispace0, take_till(|c: char| c == ',')).parse(input)?;
Ok((input, (key, value)))
}

@ -74,22 +74,16 @@ where
}
#[no_mangle]
pub unsafe extern "C" fn SCDetectU8ToJson(
js: &mut JsonBuilder, du: &DetectUintData<u8>,
) -> bool {
pub unsafe extern "C" fn SCDetectU8ToJson(js: &mut JsonBuilder, du: &DetectUintData<u8>) -> bool {
return detect_uint_to_json(js, du).is_ok();
}
#[no_mangle]
pub unsafe extern "C" fn SCDetectU16ToJson(
js: &mut JsonBuilder, du: &DetectUintData<u16>,
) -> bool {
pub unsafe extern "C" fn SCDetectU16ToJson(js: &mut JsonBuilder, du: &DetectUintData<u16>) -> bool {
return detect_uint_to_json(js, du).is_ok();
}
#[no_mangle]
pub unsafe extern "C" fn SCDetectU32ToJson(
js: &mut JsonBuilder, du: &DetectUintData<u32>,
) -> bool {
pub unsafe extern "C" fn SCDetectU32ToJson(js: &mut JsonBuilder, du: &DetectUintData<u32>) -> bool {
return detect_uint_to_json(js, du).is_ok();
}

@ -99,7 +99,8 @@ fn parse_transform_base64(
let (_, values) = nom8::multi::separated_list1(
tag(","),
preceded(multispace0, nom8::bytes::complete::is_not(",")),
).parse(input)?;
)
.parse(input)?;
// Too many options?
if values.len() > DETECT_TRANSFORM_BASE64_MAX_PARAM_COUNT {

@ -18,8 +18,8 @@
use crate::detect::SIGMATCH_NOOPT;
use suricata_sys::sys::{
DetectEngineCtx, DetectEngineThreadCtx, InspectionBuffer, SCDetectHelperTransformRegister,
SCDetectSignatureAddTransform, SCTransformTableElmt, Signature, SCInspectionBufferCheckAndExpand,
SCInspectionBufferTruncate,
SCDetectSignatureAddTransform, SCInspectionBufferCheckAndExpand, SCInspectionBufferTruncate,
SCTransformTableElmt, Signature,
};
use std::os::raw::{c_int, c_void};

@ -18,8 +18,8 @@
use crate::detect::SIGMATCH_NOOPT;
use suricata_sys::sys::{
DetectEngineCtx, DetectEngineThreadCtx, InspectionBuffer, SCDetectHelperTransformRegister,
SCDetectSignatureAddTransform, SCTransformTableElmt, Signature, SCInspectionBufferCheckAndExpand,
SCInspectionBufferTruncate,
SCDetectSignatureAddTransform, SCInspectionBufferCheckAndExpand, SCInspectionBufferTruncate,
SCTransformTableElmt, Signature,
};
use crate::ffi::hashing::{G_DISABLE_HASHING, SC_SHA1_LEN, SC_SHA256_LEN};

@ -108,7 +108,7 @@ unsafe extern "C" fn xor_free(_de: *mut DetectEngineCtx, ctx: *mut c_void) {
std::mem::drop(Box::from_raw(ctx as *mut DetectTransformXorData));
}
unsafe extern "C" fn xor_id(data: *mut *const u8, length: *mut u32, ctx: *const c_void,) {
unsafe extern "C" fn xor_id(data: *mut *const u8, length: *mut u32, ctx: *const c_void) {
if data.is_null() || length.is_null() || ctx.is_null() {
return;
}

@ -85,7 +85,9 @@ fn parse_uint_index_nb(s: &str) -> IResult<&str, DetectUintIndex> {
}
fn parse_uint_index_val(s: &str) -> Option<DetectUintIndex> {
let (_s, arg1) = alt((parse_uint_index_precise, parse_uint_index_nb)).parse(s).ok()?;
let (_s, arg1) = alt((parse_uint_index_precise, parse_uint_index_nb))
.parse(s)
.ok()?;
Some(arg1)
}
@ -386,7 +388,9 @@ pub fn detect_parse_uint_bitflags<T1: DetectIntType, T2: EnumString<T1>>(
}
// otherwise, try strings for bitmask
let (s, modifier) = parse_bitchars_modifier(s, defmod).ok()?;
let (s, _) = take_while::<_, &str, Error<_>>(|c| c == ' ' || c == '\t').parse(s).ok()?;
let (s, _) = take_while::<_, &str, Error<_>>(|c| c == ' ' || c == '\t')
.parse(s)
.ok()?;
if let Ok((rem, l)) = parse_flag_list::<T1, T2>(s, singlechar) {
if !rem.is_empty() {
SCLogError!("junk at the end of bitflags");
@ -528,7 +532,8 @@ pub fn detect_parse_uint_unit(i: &str) -> IResult<&str, u64> {
value(1024 * 1024, tag_no_case("mb")),
value(1024 * 1024 * 1024, tag_no_case("gib")),
value(1024 * 1024 * 1024, tag_no_case("gb")),
)).parse(i)?;
))
.parse(i)?;
return Ok((i, unit));
}
@ -587,7 +592,8 @@ pub fn detect_parse_uint_start_interval<T: DetectIntType>(
let (i, _) = opt(is_a(" ")).parse(i)?;
let (i, arg2) = verify(detect_parse_uint_value, |x| {
x > &arg1 && *x - arg1 > T::one()
}).parse(i)?;
})
.parse(i)?;
let mode = if neg.is_some() {
DetectUintMode::DetectUintModeNegRg
} else {
@ -628,7 +634,8 @@ fn detect_parse_uint_start_interval_inclusive<T: DetectIntType>(
let (i, _) = opt(is_a(" ")).parse(i)?;
let (i, arg2) = verify(detect_parse_uint_value::<T>, |x| {
*x > arg1 && *x < T::max_value()
}).parse(i)?;
})
.parse(i)?;
let mode = if neg.is_some() {
DetectUintMode::DetectUintModeNegRg
} else {
@ -653,7 +660,8 @@ pub fn detect_parse_uint_mode(i: &str) -> IResult<&str, DetectUintMode> {
value(DetectUintMode::DetectUintModeNe, tag("!=")),
value(DetectUintMode::DetectUintModeNe, tag("!")),
value(DetectUintMode::DetectUintModeEqual, tag("=")),
)).parse(i)?;
))
.parse(i)?;
return Ok((i, mode));
}
@ -770,7 +778,8 @@ pub(crate) fn detect_parse_uint_notending<T: DetectIntType>(
detect_parse_uint_start_interval,
detect_parse_uint_start_equal,
detect_parse_uint_start_symbol,
)).parse(i)?;
))
.parse(i)?;
Ok((i, uint))
}
@ -786,7 +795,8 @@ pub fn detect_parse_uint_inclusive<T: DetectIntType>(i: &str) -> IResult<&str, D
detect_parse_uint_start_interval_inclusive,
detect_parse_uint_start_equal,
detect_parse_uint_start_symbol,
)).parse(i)?;
))
.parse(i)?;
let (i, _) = all_consuming(take_while(|c| c == ' ')).parse(i)?;
Ok((i, uint))
}

@ -41,4 +41,4 @@ rustfmt --check rust/src/dns/*.rs rust/src/applayertemplate/*.rs rust/src/asn1/*
rust/src/http2/*.rs rust/src/ike/*.rs rust/src/modbus/*.rs rust/src/mqtt/*.rs \
rust/src/nfs/*.rs rust/src/pgsql/*.rs rust/src/rdp/*.rs rust/src/sdp/*.rs \
rust/src/sip/*.rs rust/src/telnet/*.rs rust/src/tftp/*.rs rust/src/x509/*.rs \
rust/src/snmp/*.rs rust/src/llmnr/*.rs
rust/src/snmp/*.rs rust/src/llmnr/*.rs rust/src/detect/*.rs

Loading…
Cancel
Save