Skip to content

Commit 366e49f

Browse files
committed
http2/detect: http2.window can now use index
Ticket: 7480
1 parent 6e25abf commit 366e49f

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

doc/userguide/rules/http2-keywords.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Match on the value of the HTTP2 value field present in a WINDOWUPDATE frame.
6363

6464
http2.window uses an :ref:`unsigned 32-bit integer <rules-integer-keywords>`.
6565

66+
http2.window is also a :ref:`multi-integer <multi-integers>`.
67+
6668
This keyword takes a numeric argument after a colon and supports additional qualifiers, such as:
6769

6870
* ``>`` (greater than)

rust/src/detect/uint.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,26 @@ pub unsafe extern "C" fn SCDetectU8ArrayParse(
603603
return std::ptr::null_mut();
604604
}
605605

606+
#[no_mangle]
607+
pub unsafe extern "C" fn SCDetectU32Free(ctx: &mut DetectUintData<u32>) {
608+
// Just unbox...
609+
std::mem::drop(Box::from_raw(ctx));
610+
}
611+
612+
#[no_mangle]
613+
pub unsafe extern "C" fn SCDetectU32ArrayParse(
614+
ustr: *const std::os::raw::c_char,
615+
) -> *mut c_void {
616+
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
617+
if let Ok(s) = ft_name.to_str() {
618+
if let Some(ctx) = detect_parse_array_uint::<u32>(s) {
619+
let boxed = Box::new(ctx);
620+
return Box::into_raw(boxed) as *mut c_void;
621+
}
622+
}
623+
return std::ptr::null_mut();
624+
}
625+
606626
#[no_mangle]
607627
pub unsafe extern "C" fn SCDetectU8ArrayFree(ctx: &mut DetectUintArrayData<u8>) {
608628
std::mem::drop(Box::from_raw(ctx));

rust/src/http2/detect.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,40 +164,30 @@ pub unsafe extern "C" fn SCHttp2PriorityMatch(
164164
return http2_match_priority(tx, direction.into(), ctx);
165165
}
166166

167-
fn http2_tx_get_next_window(
168-
tx: &HTTP2Transaction, direction: Direction, nb: u32,
167+
fn get_http2_window(frame: &HTTP2Frame) -> Option<u8> {
168+
if let HTTP2FrameTypeData::WINDOWUPDATE(wu) = &frame.data {
169+
return Some(return wu.sizeinc);
170+
}
171+
return None;
172+
}
173+
174+
fn http2_match_window(
175+
tx: &HTTP2Transaction, direction: Direction, ctx: &DetectUintArrayData<u32>,
169176
) -> std::os::raw::c_int {
170-
let mut pos = 0_u32;
171-
if direction == Direction::ToServer {
172-
for i in 0..tx.frames_ts.len() {
173-
if let HTTP2FrameTypeData::WINDOWUPDATE(wu) = tx.frames_ts[i].data {
174-
if pos == nb {
175-
return wu.sizeinc as i32;
176-
} else {
177-
pos += 1;
178-
}
179-
}
180-
}
177+
let frames = if direction == Direction::ToServer {
178+
&tx.frames_ts
181179
} else {
182-
for i in 0..tx.frames_tc.len() {
183-
if let HTTP2FrameTypeData::WINDOWUPDATE(wu) = tx.frames_tc[i].data {
184-
if pos == nb {
185-
return wu.sizeinc as i32;
186-
} else {
187-
pos += 1;
188-
}
189-
}
190-
}
191-
}
192-
return -1;
180+
&tx.frames_tc
181+
};
193182
}
194183

195184
#[no_mangle]
196-
pub unsafe extern "C" fn SCHttp2TxGetNextWindow(
197-
tx: *mut std::os::raw::c_void, direction: u8, nb: u32,
185+
pub unsafe extern "C" fn SCHttp2WindowMatch(
186+
tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
198187
) -> std::os::raw::c_int {
199188
let tx = cast_pointer!(tx, HTTP2Transaction);
200-
return http2_tx_get_next_window(tx, direction.into(), nb);
189+
let ctx = cast_pointer!(ctx, DetectUintArrayData<u32>);
190+
return http2_match_window(tx, direction.into(), ctx);
201191
}
202192

203193
#[no_mangle]

src/detect-http2.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,7 @@ static int DetectHTTP2windowMatch(DetectEngineThreadCtx *det_ctx,
414414
const SigMatchCtx *ctx)
415415

416416
{
417-
uint32_t nb = 0;
418-
int value = SCHttp2TxGetNextWindow(txv, flags, nb);
419-
const DetectU32Data *du32 = (const DetectU32Data *)ctx;
420-
while (value >= 0) {
421-
if (DetectU32Match(value, du32)) {
422-
return 1;
423-
}
424-
nb++;
425-
value = SCHttp2TxGetNextWindow(txv, flags, nb);
426-
}
427-
return 0;
417+
return SCHttp2WindowMatch(txv, flags, ctx);
428418
}
429419

430420
/**
@@ -442,7 +432,7 @@ static int DetectHTTP2windowSetup (DetectEngineCtx *de_ctx, Signature *s, const
442432
if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP2) != 0)
443433
return -1;
444434

445-
DetectU32Data *wu = DetectU32Parse(str);
435+
DetectU32Data *wu = SCDetectU32ArrayParse(str);
446436
if (wu == NULL)
447437
return -1;
448438

@@ -462,7 +452,7 @@ static int DetectHTTP2windowSetup (DetectEngineCtx *de_ctx, Signature *s, const
462452
*/
463453
void DetectHTTP2windowFree(DetectEngineCtx *de_ctx, void *ptr)
464454
{
465-
SCDetectU32Free(ptr);
455+
SCDetectU32ArrayFree(ptr);
466456
}
467457

468458
/**

0 commit comments

Comments
 (0)