Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applayer plugin 5053 v3.19 #12364

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
3 changes: 1 addition & 2 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl From<Direction> for u8 {
pub type AppProto = u16;

pub const ALPROTO_UNKNOWN : AppProto = 0;
pub static mut ALPROTO_FAILED : AppProto = 0; // updated during init
pub const ALPROTO_FAILED : AppProto = 1;

pub const IPPROTO_TCP : u8 = 6;
pub const IPPROTO_UDP : u8 = 17;
Expand Down Expand Up @@ -252,7 +252,6 @@ pub fn init_ffi(context: &'static SuricataContext)
{
unsafe {
SC = Some(context);
ALPROTO_FAILED = StringToAppProto("failed\0".as_ptr());
}
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/ldap/ldap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ fn probe(input: &[u8], direction: Direction, rdir: *mut u8) -> AppProto {
Ok((_, msg)) => {
let ldap_msg = LdapMessage::from(msg);
if ldap_msg.is_unknown() {
return unsafe { ALPROTO_FAILED };
return ALPROTO_FAILED;
}
if direction == Direction::ToServer && !ldap_msg.is_request() {
unsafe {
Expand All @@ -537,7 +537,7 @@ fn probe(input: &[u8], direction: Direction, rdir: *mut u8) -> AppProto {
return ALPROTO_UNKNOWN;
}
Err(_e) => {
return unsafe { ALPROTO_FAILED };
return ALPROTO_FAILED;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/modbus/modbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub extern "C" fn rs_modbus_probe(
match MODBUS_PARSER.probe(slice, Direction::Unknown) {
Status::Recognized => unsafe { ALPROTO_MODBUS },
Status::Incomplete => ALPROTO_UNKNOWN,
Status::Unrecognized => unsafe { ALPROTO_FAILED },
Status::Unrecognized => ALPROTO_FAILED,
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/ntp/ntp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub extern "C" fn ntp_probing_parser(_flow: *const Flow,
return ALPROTO_UNKNOWN;
},
Err(_) => {
return unsafe{ALPROTO_FAILED};
return ALPROTO_FAILED;
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/smb/smb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ fn smb_probe_tcp(flags: u8, slice: &[u8], rdir: *mut u8, begins: bool) -> AppPro
}
}
SCLogDebug!("no smb");
unsafe { return ALPROTO_FAILED; }
return ALPROTO_FAILED;
}

// probing confirmation parser
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup-app-layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def patch_app_layer_protos_h(protoname):
open(filename, "w").write(output.getvalue())

def patch_app_layer_protos_c(protoname):
filename = "src/app-layer-protos.c"
filename = "src/app-layer.c"
print("Patching %s." % (filename))
output = io.StringIO()

Expand Down
42 changes: 21 additions & 21 deletions src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static inline int PMGetProtoInspect(AppLayerProtoDetectThreadCtx *tctx,
}

/* alproto bit field */
uint8_t pm_results_bf[(ALPROTO_MAX / 8) + 1];
uint8_t pm_results_bf[(AlprotoMax / 8) + 1];
memset(pm_results_bf, 0, sizeof(pm_results_bf));

/* loop through unique pattern id's. Can't use search_cnt here,
Expand Down Expand Up @@ -324,7 +324,7 @@ static inline int PMGetProtoInspect(AppLayerProtoDetectThreadCtx *tctx,
/** \internal
* \brief Run Pattern Sigs against buffer
* \param direction direction for the patterns
* \param pm_results[out] AppProto array of size ALPROTO_MAX */
* \param pm_results[out] AppProto array of size AlprotoMax */
static AppProto AppLayerProtoDetectPMGetProto(AppLayerProtoDetectThreadCtx *tctx, Flow *f,
const uint8_t *buf, uint32_t buflen, uint8_t flags, AppProto *pm_results, bool *rflow)
{
Expand Down Expand Up @@ -694,7 +694,7 @@ static uint32_t AppLayerProtoDetectProbingParserGetMask(AppProto alproto)
{
SCEnter();

if (!(alproto > ALPROTO_UNKNOWN && alproto < ALPROTO_FAILED)) {
if (!AppProtoIsValid(alproto)) {
FatalError("Unknown protocol detected - %u", alproto);
}

Expand Down Expand Up @@ -804,7 +804,7 @@ static AppLayerProtoDetectProbingParserElement *AppLayerProtoDetectProbingParser
"register the probing parser. min_depth >= max_depth");
goto error;
}
if (alproto <= ALPROTO_UNKNOWN || alproto >= ALPROTO_MAX) {
if (alproto <= ALPROTO_UNKNOWN || alproto >= AlprotoMax) {
SCLogError("Invalid arguments sent to register "
"the probing parser. Invalid alproto - %d",
alproto);
Expand Down Expand Up @@ -1411,7 +1411,7 @@ AppProto AppLayerProtoDetectGetProto(AppLayerProtoDetectThreadCtx *tctx, Flow *f
AppProto pm_alproto = ALPROTO_UNKNOWN;

if (!FLOW_IS_PM_DONE(f, flags)) {
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
uint16_t pm_matches = AppLayerProtoDetectPMGetProto(
tctx, f, buf, buflen, flags, pm_results, reverse_flow);
if (pm_matches > 0) {
Expand Down Expand Up @@ -1725,12 +1725,12 @@ int AppLayerProtoDetectSetup(void)
}
}

alpd_ctx.alproto_names = SCCalloc(ALPROTO_MAX, sizeof(char *));
alpd_ctx.alproto_names = SCCalloc(AlprotoMax, sizeof(char *));
if (unlikely(alpd_ctx.alproto_names == NULL)) {
FatalError("Unable to alloc alproto_names.");
}
// to realloc when dynamic protos are added
alpd_ctx.expectation_proto = SCCalloc(ALPROTO_MAX, sizeof(uint8_t));
alpd_ctx.expectation_proto = SCCalloc(AlprotoMax, sizeof(uint8_t));
if (unlikely(alpd_ctx.expectation_proto == NULL)) {
FatalError("Unable to alloc expectation_proto.");
}
Expand Down Expand Up @@ -2090,7 +2090,7 @@ AppProto AppLayerProtoDetectGetProtoByName(const char *alproto_name)

AppProto a;
AppProto b = StringToAppProto(alproto_name);
for (a = 0; a < ALPROTO_MAX; a++) {
for (a = 0; a < AlprotoMax; a++) {
if (alpd_ctx.alproto_names[a] != NULL && AppProtoEquals(b, a)) {
// That means return HTTP_ANY if HTTP1 or HTTP2 is enabled
SCReturnCT(b, "AppProto");
Expand Down Expand Up @@ -2121,11 +2121,11 @@ void AppLayerProtoDetectSupportedAppProtocols(AppProto *alprotos)
{
SCEnter();

memset(alprotos, 0, ALPROTO_MAX * sizeof(AppProto));
memset(alprotos, 0, AlprotoMax * sizeof(AppProto));

int alproto;

for (alproto = 0; alproto != ALPROTO_MAX; alproto++) {
for (alproto = 0; alproto != AlprotoMax; alproto++) {
if (alpd_ctx.alproto_names[alproto] != NULL)
alprotos[alproto] = 1;
}
Expand Down Expand Up @@ -2229,7 +2229,7 @@ static int AppLayerProtoDetectTest03(void)
AppLayerProtoDetectSetup();

uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2276,7 +2276,7 @@ static int AppLayerProtoDetectTest04(void)
uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
Flow f;
memset(&f, 0x00, sizeof(f));
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
f.protomap = FlowGetProtoMapping(IPPROTO_TCP);

Expand Down Expand Up @@ -2314,7 +2314,7 @@ static int AppLayerProtoDetectTest05(void)
AppLayerProtoDetectSetup();

uint8_t l7data[] = "HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2358,7 +2358,7 @@ static int AppLayerProtoDetectTest06(void)
AppLayerProtoDetectSetup();

uint8_t l7data[] = "220 Welcome to the OISF FTP server\r\n";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2404,7 +2404,7 @@ static int AppLayerProtoDetectTest07(void)
Flow f;
memset(&f, 0x00, sizeof(f));
f.protomap = FlowGetProtoMapping(IPPROTO_TCP);
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));

const char *buf = "HTTP";
Expand Down Expand Up @@ -2458,7 +2458,7 @@ static int AppLayerProtoDetectTest08(void)
0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
0x00
};
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2513,7 +2513,7 @@ static int AppLayerProtoDetectTest09(void)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x02
};
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2563,7 +2563,7 @@ static int AppLayerProtoDetectTest10(void)
0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
};
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2608,7 +2608,7 @@ static int AppLayerProtoDetectTest11(void)

uint8_t l7data[] = "CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
uint8_t l7data_resp[] = "HTTP/1.1 405 Method Not Allowed\r\n";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
memset(pm_results, 0, sizeof(pm_results));
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2733,7 +2733,7 @@ static int AppLayerProtoDetectTest13(void)

uint8_t l7data[] = "CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
uint8_t l7data_resp[] = "HTTP/1.1 405 Method Not Allowed\r\n";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];

Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down Expand Up @@ -2804,7 +2804,7 @@ static int AppLayerProtoDetectTest14(void)

uint8_t l7data[] = "CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
uint8_t l7data_resp[] = "HTTP/1.1 405 Method Not Allowed\r\n";
AppProto pm_results[ALPROTO_MAX];
AppProto pm_results[AlprotoMax];
uint32_t cnt;
Flow f;
memset(&f, 0x00, sizeof(f));
Expand Down
8 changes: 4 additions & 4 deletions src/app-layer-frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
struct FrameConfig {
SC_ATOMIC_DECLARE(uint64_t, types);
};
/* This array should be allocated to contain ALPROTO_MAX protocols. */
/* This array should be allocated to contain AlprotoMax protocols. */
static struct FrameConfig *frame_config;

void FrameConfigInit(void)
{
frame_config = SCCalloc(ALPROTO_MAX, sizeof(struct FrameConfig));
frame_config = SCCalloc(AlprotoMax, sizeof(struct FrameConfig));
if (unlikely(frame_config == NULL)) {
FatalError("Unable to alloc frame_config.");
}
for (AppProto p = 0; p < ALPROTO_MAX; p++) {
for (AppProto p = 0; p < AlprotoMax; p++) {
SC_ATOMIC_INIT(frame_config[p].types);
}
}
Expand All @@ -55,7 +55,7 @@ void FrameConfigDeInit(void)
void FrameConfigEnableAll(void)
{
const uint64_t bits = UINT64_MAX;
for (AppProto p = 0; p < ALPROTO_MAX; p++) {
for (AppProto p = 0; p < AlprotoMax; p++) {
struct FrameConfig *fc = &frame_config[p];
SC_ATOMIC_OR(fc->types, bits);
}
Expand Down
40 changes: 32 additions & 8 deletions src/app-layer-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ int AppLayerParserSetup(void)
{
SCEnter();
// initial allocation that will later be grown using realloc,
// when new protocols register themselves and make ALPROTO_MAX grow
alp_ctx.ctxs = SCCalloc(ALPROTO_MAX, sizeof(AppLayerParserProtoCtx[FLOW_PROTO_MAX]));
// when new protocols register themselves and make AlprotoMax grow
alp_ctx.ctxs = SCCalloc(AlprotoMax, sizeof(AppLayerParserProtoCtx[FLOW_PROTO_MAX]));
if (unlikely(alp_ctx.ctxs == NULL)) {
FatalError("Unable to alloc alp_ctx.ctxs.");
}
Expand All @@ -261,7 +261,7 @@ void AppLayerParserPostStreamSetup(void)
{
/* lets set a default value for stream_depth */
for (int flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
for (AppProto alproto = 0; alproto < AlprotoMax; alproto++) {
if (!(alp_ctx.ctxs[alproto][flow_proto].internal_flags &
APP_LAYER_PARSER_INT_STREAM_DEPTH_SET)) {
alp_ctx.ctxs[alproto][flow_proto].stream_depth = stream_config.reassembly_depth;
Expand Down Expand Up @@ -290,14 +290,14 @@ AppLayerParserThreadCtx *AppLayerParserThreadCtxAlloc(void)
if (tctx == NULL)
goto end;

tctx->alproto_local_storage = SCCalloc(ALPROTO_MAX, sizeof(void *[FLOW_PROTO_MAX]));
tctx->alproto_local_storage = SCCalloc(AlprotoMax, sizeof(void *[FLOW_PROTO_MAX]));
if (unlikely(tctx->alproto_local_storage == NULL)) {
SCFree(tctx);
tctx = NULL;
goto end;
}
for (uint8_t flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
for (AppProto alproto = 0; alproto < AlprotoMax; alproto++) {
uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);

tctx->alproto_local_storage[alproto][flow_proto] =
Expand All @@ -314,7 +314,7 @@ void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
SCEnter();

for (uint8_t flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
for (AppProto alproto = 0; alproto < AlprotoMax; alproto++) {
uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);

AppLayerParserDestroyProtocolParserLocalStorage(
Expand Down Expand Up @@ -1695,11 +1695,32 @@ static void ValidateParser(AppProto alproto)
static void ValidateParsers(void)
{
AppProto p = 0;
for ( ; p < ALPROTO_MAX; p++) {
for (; p < AlprotoMax; p++) {
ValidateParser(p);
}
}

#define ARRAY_CAP_STEP 16
static void (**preregistered_callbacks)(void) = NULL;
static size_t preregistered_callbacks_nb = 0;
static size_t preregistered_callbacks_cap = 0;

int AppLayerParserPreRegister(void (*Register)(void))
{
if (preregistered_callbacks_nb == preregistered_callbacks_cap) {
void *tmp = SCRealloc(preregistered_callbacks,
sizeof(void *) * (preregistered_callbacks_cap + ARRAY_CAP_STEP));
if (tmp == NULL) {
return 1;
}
preregistered_callbacks_cap += ARRAY_CAP_STEP;
preregistered_callbacks = tmp;
}
preregistered_callbacks[preregistered_callbacks_nb] = Register;
preregistered_callbacks_nb++;
return 0;
}

void AppLayerParserRegisterProtocolParsers(void)
{
SCEnter();
Expand Down Expand Up @@ -1752,6 +1773,9 @@ void AppLayerParserRegisterProtocolParsers(void)
} else {
SCLogInfo("Protocol detection and parser disabled for pop3 protocol.");
}
for (size_t i = 0; i < preregistered_callbacks_nb; i++) {
preregistered_callbacks[i]();
}

ValidateParsers();
}
Expand Down Expand Up @@ -1795,7 +1819,7 @@ void AppLayerParserRegisterUnittests(void)
AppLayerParserProtoCtx *ctx;

for (ip = 0; ip < FLOW_PROTO_DEFAULT; ip++) {
for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
for (alproto = 0; alproto < AlprotoMax; alproto++) {
ctx = &alp_ctx.ctxs[alproto][ip];
if (ctx->RegisterUnittests == NULL)
continue;
Expand Down
1 change: 1 addition & 0 deletions src/app-layer-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ typedef AppLayerGetTxIterTuple (*AppLayerGetTxIteratorFunc)
typedef int (*AppLayerParserGetFrameIdByNameFn)(const char *frame_name);
typedef const char *(*AppLayerParserGetFrameNameByIdFn)(const uint8_t id);

int AppLayerParserPreRegister(void (*Register)(void));
/**
* \brief Register app layer parser for the protocol.
*
Expand Down
Loading
Loading