Skip to content
Open
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
2 changes: 1 addition & 1 deletion rust/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ if HAVE_BINDGEN
--default-enum-style rust \
--allowlist-type 'AppProto.*' \
--allowlist-function 'AppProto.*' \
--allowlist-function 'FileAppendData' \
--allowlist-function 'File.*' \
--allowlist-type 'SC.*' \
--allowlist-function 'SC.*' \
--allowlist-var 'SC.*' \
Expand Down
73 changes: 22 additions & 51 deletions rust/src/filecontainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,16 @@

//! This module handles file container operations (open, append, close).

use std::os::raw::c_void;
use std::ptr;

use crate::core::*;

#[repr(C)]
#[derive(Debug)]
pub struct FileContainer {
head: *mut c_void,
tail: *mut c_void,
}

impl Default for FileContainer {
fn default() -> Self {
Self {
head: ptr::null_mut(),
tail: ptr::null_mut(),
}
}
}

// Defined in util-file.h
#[allow(unused_doc_comments)]
/// cbindgen:ignore
extern "C" {
#[cfg(not(test))]
pub fn FileContainerRecycle(file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig);
#[cfg(not(test))]
pub fn FileAppendGAPById(
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32,
) -> i32;
#[cfg(not(test))]
pub fn FileAppendDataById(
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32,
) -> i32;
#[cfg(not(test))]
pub fn FileCloseFileById(
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32, flags: u16,
) -> i32;
#[cfg(not(test))]
pub fn FileOpenFileWithId(
file_container: &mut FileContainer, sbcfg: &StreamingBufferConfig, track_id: u32,
name: *const u8, name_len: u16, data: *const u8, data_len: u32, flags: u16,
) -> i32;
}
pub use suricata_sys::sys::FileContainer;
#[cfg(not(test))]
use suricata_sys::sys::{
FileAppendDataById, FileAppendGAPById, FileCloseFileById, FileContainerRecycle,
FileOpenFileWithId,
};

#[cfg(test)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -103,15 +65,26 @@ pub(super) unsafe fn FileOpenFileWithId(
0
}

impl FileContainer {
pub fn free(&mut self, cfg: &'static SuricataFileContext) {
pub trait FileContainerWrapper {
fn free(&mut self, cfg: &'static SuricataFileContext);
fn file_open(
&mut self, cfg: &'static SuricataFileContext, track_id: u32, name: &[u8], flags: u16,
) -> i32;
fn file_append(
&mut self, cfg: &'static SuricataFileContext, track_id: &u32, data: &[u8], is_gap: bool,
) -> i32;
fn file_close(&mut self, cfg: &'static SuricataFileContext, track_id: &u32, flags: u16) -> i32;
}

impl FileContainerWrapper for FileContainer {
fn free(&mut self, cfg: &'static SuricataFileContext) {
SCLogDebug!("freeing self");
unsafe {
FileContainerRecycle(self, cfg.files_sbcfg);
}
}

pub fn file_open(
fn file_open(
&mut self, cfg: &'static SuricataFileContext, track_id: u32, name: &[u8], flags: u16,
) -> i32 {
SCLogDebug!("FILE {:p} OPEN flags {:04X}", &self, flags);
Expand All @@ -130,7 +103,7 @@ impl FileContainer {
}
}

pub fn file_append(
fn file_append(
&mut self, cfg: &'static SuricataFileContext, track_id: &u32, data: &[u8], is_gap: bool,
) -> i32 {
SCLogDebug!("FILECONTAINER: append {}", data.len());
Expand Down Expand Up @@ -166,9 +139,7 @@ impl FileContainer {
res
}

pub fn file_close(
&mut self, cfg: &'static SuricataFileContext, track_id: &u32, flags: u16,
) -> i32 {
fn file_close(&mut self, cfg: &'static SuricataFileContext, track_id: &u32, flags: u16) -> i32 {
SCLogDebug!("FILECONTAINER: CLOSEing");

unsafe { FileCloseFileById(self, cfg.files_sbcfg, *track_id, ptr::null(), 0u32, flags) }
Expand Down
1 change: 1 addition & 0 deletions rust/src/http2/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::conf::conf_get;
use crate::core::*;
use crate::direction::Direction;
use crate::dns::dns::DnsVariant;
use crate::filecontainer::FileContainerWrapper;
use crate::filetracker::*;
use crate::flow::Flow;
use crate::frames::Frame;
Expand Down
24 changes: 10 additions & 14 deletions rust/src/http2/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/

use super::detect;
use crate::core::{StreamingBufferConfig, SuricataFileContext};
use crate::core::SuricataFileContext;
use crate::direction::Direction;
use crate::filecontainer::FileContainer;
use crate::flow::Flow;
use crate::http2::http2::HTTP2Transaction;
use crate::http2::http2::SURICATA_HTTP2_FILE_CONFIG;
Expand All @@ -30,25 +29,22 @@ use nom7::combinator::{map_res, value};
use nom7::error::{make_error, ErrorKind};
use nom7::{Err, IResult};
use std::str::FromStr;
use suricata_sys::sys::{HttpRangeContainerBlock, SCHttpRangeContainerOpenFile, SCHttpRangeAppendData};

// Defined in app-layer-htp-file.h
#[allow(unused_doc_comments)]
/// cbindgen:ignore
extern "C" {
#[cfg(not(test))]
pub fn SCHTPFileCloseHandleRange(
sbcfg: &StreamingBufferConfig, fc: *mut FileContainer, flags: u16,
c: *mut HttpRangeContainerBlock, data: *const u8, data_len: u32,
) -> bool;
}
use suricata_sys::sys::{
HttpRangeContainerBlock, SCHttpRangeAppendData, SCHttpRangeContainerOpenFile,
};

#[cfg(test)]
#[allow(non_snake_case)]
pub(super) unsafe fn SCHttpRangeFreeBlock(_range: *mut HttpRangeContainerBlock) {}
#[cfg(not(test))]
pub(super) use suricata_sys::sys::SCHttpRangeFreeBlock;

#[cfg(test)]
use crate::core::StreamingBufferConfig;
#[cfg(test)]
use crate::filecontainer::FileContainer;
#[cfg(not(test))]
pub(super) use suricata_sys::sys::SCHTPFileCloseHandleRange;
#[cfg(test)]
#[allow(non_snake_case)]
pub(super) unsafe fn SCHTPFileCloseHandleRange(
Expand Down
1 change: 1 addition & 0 deletions rust/src/nfs/nfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::core::*;
use crate::direction::Direction;
use crate::direction::DIR_BOTH;
use crate::filetracker::*;
use crate::filecontainer::FileContainerWrapper;
use crate::flow::{Flow, flow_get_last_time};
use crate::frames::*;

Expand Down
1 change: 1 addition & 0 deletions rust/src/smb/smb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::flow::{Flow, FLOW_DIR_REVERSED, flow_get_flags, flow_get_last_time, f
use crate::frames::*;
use crate::conf::*;
use crate::applayer::{AppLayerResult, AppLayerTxData, AppLayerEvent};
use crate::filecontainer::FileContainerWrapper;

use crate::smb::nbss_records::*;
use crate::smb::smb1_records::*;
Expand Down
12 changes: 12 additions & 0 deletions rust/sys/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file is automatically generated. Do not edit.

use super::sys::FileContainer;

impl Default for FileContainer {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed by Filetracker Default implementation...

fn default() -> Self {
Self {
head: std::ptr::null_mut(),
tail: std::ptr::null_mut(),
}
}
}
1 change: 1 addition & 0 deletions rust/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#![allow(non_snake_case)]
#![allow(clippy::all)]

pub mod file;
pub mod jsonbuilder;
pub mod sys;
42 changes: 42 additions & 0 deletions rust/sys/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,11 @@ pub struct File_ {
_unused: [u8; 0],
}
pub type File = File_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct AppLayerTxData {
_unused: [u8; 0],
}
extern "C" {
#[doc = " \\brief Given a protocol name, checks if the parser is enabled in\n the conf file.\n\n \\param alproto_name Name of the app layer protocol.\n\n \\retval 1 If enabled.\n \\retval 0 If disabled."]
pub fn SCAppLayerParserConfParserEnabled(
Expand Down Expand Up @@ -793,6 +798,9 @@ extern "C" {
extern "C" {
pub fn SCAppLayerParserStateIssetFlag(pstate: *mut AppLayerParserState, flag: u16) -> u16;
}
extern "C" {
pub fn FileApplyTxFlags(txd: *const AppLayerTxData, direction: u8, file: *mut File);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to bindgen this one

}
extern "C" {
pub fn SCAppLayerRegisterParserAlias(
proto_name: *const ::std::os::raw::c_char, proto_alias: *const ::std::os::raw::c_char,
Expand Down Expand Up @@ -879,6 +887,34 @@ extern "C" {
data_len: u32,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Open a new File\n\n \\param ffc flow container\n \\param sbcfg buffer config\n \\param name filename character array\n \\param name_len filename len\n \\param data initial data\n \\param data_len initial data len\n \\param flags open flags\n\n \\retval ff flowfile object\n\n \\note filename is not a string, so it's not nul terminated.\n\n If flags contains the FILE_USE_DETECT bit, the pruning code will\n consider not just the content_stored tracker, but also content_inspected.\n It's the responsibility of the API user to make sure this tracker is\n properly updated."]
pub fn FileOpenFileWithId(
arg1: *mut FileContainer, arg2: *const StreamingBufferConfig, track_id: u32,
name: *const u8, name_len: u16, data: *const u8, data_len: u32, flags: u16,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn FileAppendDataById(
arg1: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn FileAppendGAPById(
ffc: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn FileCloseFileById(
arg1: *mut FileContainer, sbcfg: *const StreamingBufferConfig, track_id: u32,
data: *const u8, data_len: u32, flags: u16,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn FileContainerRecycle(arg1: *mut FileContainer, cfg: *const StreamingBufferConfig);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct HttpRangeContainerBuffer {
Expand Down Expand Up @@ -924,6 +960,12 @@ extern "C" {
len: u32,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn SCHTPFileCloseHandleRange(
sbcfg: *const StreamingBufferConfig, arg1: *mut FileContainer, arg2: u16,
arg3: *mut HttpRangeContainerBlock, arg4: *const u8, arg5: u32,
) -> bool;
}
pub type FrameId = i64;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
30 changes: 0 additions & 30 deletions src/app-layer-htp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,36 +260,6 @@ int HTPFileStoreChunk(HtpTxUserData *tx, const uint8_t *data, uint32_t data_len,
SCReturnInt(retval);
}

/** \brief close range, add reassembled file if possible
* \retval true if reassembled file was added
* \retval false if no reassembled file was added
*/
bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *files,
const uint16_t flags, HttpRangeContainerBlock *c, const uint8_t *data, uint32_t data_len)
{
bool added = false;
if (SCHttpRangeAppendData(sbcfg, c, data, data_len) < 0) {
SCLogDebug("Failed to append data");
}
if (c->container) {
// we only call HttpRangeClose if we may some new data
// ie we do not call it if we skipped all this range request
THashDataLock(c->container->hdata);
if (c->container->error) {
SCLogDebug("range in ERROR state");
}
File *ranged = HttpRangeClose(sbcfg, c, flags);
if (ranged && files) {
/* HtpState owns the constructed file now */
FileContainerAdd(files, ranged);
added = true;
}
DEBUG_VALIDATE_BUG_ON(ranged && !files);
THashDataUnlock(c->container->hdata);
}
return added;
}

/**
* \brief Close the file in the flow
*
Expand Down
2 changes: 0 additions & 2 deletions src/app-layer-htp-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ int HTPFileOpen(
HtpState *, HtpTxUserData *, const uint8_t *, uint16_t, const uint8_t *, uint32_t, uint8_t);
int HTPFileOpenWithRange(HtpState *, HtpTxUserData *, const uint8_t *, uint16_t, const uint8_t *,
uint32_t, const htp_tx_t *, const bstr *rawvalue, HtpTxUserData *htud);
bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t,
HttpRangeContainerBlock *, const uint8_t *, uint32_t);
int HTPFileStoreChunk(HtpTxUserData *, const uint8_t *, uint32_t, uint8_t);

int HTPParseContentRange(const bstr *rawvalue, HTTPContentRange *range);
Expand Down
30 changes: 30 additions & 0 deletions src/app-layer-htp-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,33 @@ void SCHttpRangeFreeBlock(HttpRangeContainerBlock *b)
SCFree(b);
}
}

/** \brief close range, add reassembled file if possible
* \retval true if reassembled file was added
* \retval false if no reassembled file was added
*/
bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *files,
const uint16_t flags, HttpRangeContainerBlock *c, const uint8_t *data, uint32_t data_len)
{
bool added = false;
if (SCHttpRangeAppendData(sbcfg, c, data, data_len) < 0) {
SCLogDebug("Failed to append data");
}
if (c->container) {
// we only call HttpRangeClose if we may some new data
// ie we do not call it if we skipped all this range request
THashDataLock(c->container->hdata);
if (c->container->error) {
SCLogDebug("range in ERROR state");
}
File *ranged = HttpRangeClose(sbcfg, c, flags);
if (ranged && files) {
/* HtpState owns the constructed file now */
FileContainerAdd(files, ranged);
added = true;
}
DEBUG_VALIDATE_BUG_ON(ranged && !files);
THashDataUnlock(c->container->hdata);
}
return added;
}
3 changes: 3 additions & 0 deletions src/app-layer-htp-range.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ HttpRangeContainerBlock *SCHttpRangeContainerOpenFile(const unsigned char *key,
int SCHttpRangeAppendData(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock *c,
const uint8_t *data, uint32_t len);

bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t,
HttpRangeContainerBlock *, const uint8_t *, uint32_t);

#ifndef SURICATA_BINDGEN_H

#include "util-streaming-buffer.h"
Expand Down
Loading
Loading