Skip to content
Closed
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
3 changes: 2 additions & 1 deletion rust/src/detect/byte_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions rust/src/detect/byte_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)));
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions rust/src/detect/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions rust/src/detect/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}

Expand Down Expand Up @@ -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))
}

Expand Down
34 changes: 26 additions & 8 deletions rust/src/detect/iprep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 6 additions & 3 deletions rust/src/detect/requires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)))
}
Expand Down
12 changes: 3 additions & 9 deletions rust/src/detect/tojson/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion rust/src/detect/transforms/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rust/src/detect/transforms/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions rust/src/detect/transforms/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/detect/transforms/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 18 additions & 8 deletions rust/src/detect/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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));
}

Expand Down Expand Up @@ -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))
}

Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/rustfmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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