Skip to content

Commit

Permalink
snmp: restrict rust visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Jan 24, 2025
1 parent 59df06d commit cfc4a8d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
16 changes: 8 additions & 8 deletions rust/src/snmp/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ unsafe extern "C" fn snmp_detect_pdutype_free(_de: *mut c_void, ctx: *mut c_void
rs_detect_u32_free(ctx);
}

pub unsafe extern "C" fn snmp_detect_usm_setup(
unsafe extern "C" fn snmp_detect_usm_setup(
de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char,
) -> c_int {
if DetectSignatureSetAppProto(s, ALPROTO_SNMP) != 0 {
Expand All @@ -117,7 +117,7 @@ pub unsafe extern "C" fn snmp_detect_usm_setup(
return 0;
}

pub unsafe extern "C" fn snmp_detect_usm_get(
unsafe extern "C" fn snmp_detect_usm_get(
tx: *const c_void, _flow_flags: u8, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool {
let tx = cast_pointer!(tx, SNMPTransaction);
Expand All @@ -129,7 +129,7 @@ pub unsafe extern "C" fn snmp_detect_usm_get(
return false;
}

pub unsafe extern "C" fn snmp_detect_usm_get_data(
unsafe extern "C" fn snmp_detect_usm_get_data(
de: *mut c_void, transforms: *const c_void, flow: *const c_void, flow_flags: u8,
tx: *const c_void, list_id: c_int,
) -> *mut c_void {
Expand All @@ -144,7 +144,7 @@ pub unsafe extern "C" fn snmp_detect_usm_get_data(
);
}

pub unsafe extern "C" fn snmp_detect_community_setup(
unsafe extern "C" fn snmp_detect_community_setup(
de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char,
) -> c_int {
if DetectSignatureSetAppProto(s, ALPROTO_SNMP) != 0 {
Expand All @@ -156,7 +156,7 @@ pub unsafe extern "C" fn snmp_detect_community_setup(
return 0;
}

pub unsafe extern "C" fn snmp_detect_community_get(
unsafe extern "C" fn snmp_detect_community_get(
tx: *const c_void, _flow_flags: u8, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool {
let tx = cast_pointer!(tx, SNMPTransaction);
Expand All @@ -168,7 +168,7 @@ pub unsafe extern "C" fn snmp_detect_community_get(
return false;
}

pub unsafe extern "C" fn snmp_detect_community_get_data(
unsafe extern "C" fn snmp_detect_community_get_data(
de: *mut c_void, transforms: *const c_void, flow: *const c_void, flow_flags: u8,
tx: *const c_void, list_id: c_int,
) -> *mut c_void {
Expand All @@ -182,8 +182,8 @@ pub unsafe extern "C" fn snmp_detect_community_get_data(
snmp_detect_community_get,
);
}
#[no_mangle]
pub unsafe extern "C" fn ScDetectSNMPRegister() {

pub(super) unsafe extern "C" fn detect_snmp_register() {
let kw = SCSigTableElmt {
name: b"snmp.version\0".as_ptr() as *const libc::c_char,
desc: b"match SNMP version\0".as_ptr() as *const libc::c_char,
Expand Down
3 changes: 1 addition & 2 deletions rust/src/snmp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ fn snmp_log_response(jsb: &mut JsonBuilder, tx: &SNMPTransaction) -> Result<(),
return Ok(());
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_log_json_response(
pub(super) unsafe extern "C" fn rs_snmp_log_json_response(
tx: *const std::os::raw::c_void, jsb: *mut std::os::raw::c_void,
) -> bool {
let tx = cast_pointer!(tx, SNMPTransaction);
Expand Down
44 changes: 18 additions & 26 deletions rust/src/snmp/snmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::snmp::snmp_parser::*;
use crate::core::{self, *};
use crate::applayer::{self, *};
use super::log::rs_snmp_log_json_response;
use super::detect::ScDetectSNMPRegister;
use super::detect::detect_snmp_register;
use std;
use std::ffi::CString;

Expand All @@ -34,18 +34,18 @@ use nom7::{Err, IResult};
use nom7::error::{ErrorKind, make_error};

#[derive(AppLayerEvent)]
pub enum SNMPEvent {
enum SNMPEvent {
MalformedData,
UnknownSecurityModel,
VersionMismatch,
}

#[derive(Default)]
pub struct SNMPState<'a> {
struct SNMPState<'a> {
state_data: AppLayerStateData,

/// SNMP protocol version
pub version: u32,
version: u32,

/// List of transactions for this session
transactions: Vec<SNMPTransaction<'a>>,
Expand All @@ -54,7 +54,7 @@ pub struct SNMPState<'a> {
tx_id: u64,
}

pub struct SNMPPduInfo<'a> {
pub(super) struct SNMPPduInfo<'a> {
pub pdu_type: PduType,

pub err: ErrorStatus,
Expand All @@ -64,7 +64,7 @@ pub struct SNMPPduInfo<'a> {
pub vars: Vec<Oid<'a>>,
}

pub struct SNMPTransaction<'a> {
pub(super) struct SNMPTransaction<'a> {
/// PDU version
pub version: u32,

Expand Down Expand Up @@ -93,7 +93,7 @@ impl Transaction for SNMPTransaction<'_> {
}

impl<'a> SNMPState<'a> {
pub fn new() -> SNMPState<'a> {
fn new() -> SNMPState<'a> {
Default::default()
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'a> SNMPState<'a> {
}

impl<'a> SNMPTransaction<'a> {
pub fn new(direction: Direction, version: u32, id: u64) -> SNMPTransaction<'a> {
fn new(direction: Direction, version: u32, id: u64) -> SNMPTransaction<'a> {
SNMPTransaction {
version,
info: None,
Expand All @@ -253,23 +253,20 @@ impl<'a> SNMPTransaction<'a> {
}

/// Returns *mut SNMPState
#[no_mangle]
pub extern "C" fn rs_snmp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void {
extern "C" fn rs_snmp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void {
let state = SNMPState::new();
let boxed = Box::new(state);
return Box::into_raw(boxed) as *mut _;
}

/// Params:
/// - state: *mut SNMPState as void pointer
#[no_mangle]
pub extern "C" fn rs_snmp_state_free(state: *mut std::os::raw::c_void) {
extern "C" fn rs_snmp_state_free(state: *mut std::os::raw::c_void) {
let mut snmp_state = unsafe{ Box::from_raw(state as *mut SNMPState) };
snmp_state.free();
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_parse_request(_flow: *const Flow,
unsafe extern "C" fn rs_snmp_parse_request(_flow: *const Flow,
state: *mut std::os::raw::c_void,
_pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice,
Expand All @@ -279,8 +276,7 @@ pub unsafe extern "C" fn rs_snmp_parse_request(_flow: *const Flow,
state.parse(stream_slice.as_slice(), Direction::ToServer).into()
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_parse_response(_flow: *const Flow,
unsafe extern "C" fn rs_snmp_parse_response(_flow: *const Flow,
state: *mut std::os::raw::c_void,
_pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice,
Expand All @@ -290,8 +286,7 @@ pub unsafe extern "C" fn rs_snmp_parse_response(_flow: *const Flow,
state.parse(stream_slice.as_slice(), Direction::ToClient).into()
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void,
unsafe extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void,
tx_id: u64)
-> *mut std::os::raw::c_void
{
Expand All @@ -302,24 +297,21 @@ pub unsafe extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void,
}
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_state_get_tx_count(state: *mut std::os::raw::c_void)
unsafe extern "C" fn rs_snmp_state_get_tx_count(state: *mut std::os::raw::c_void)
-> u64
{
let state = cast_pointer!(state,SNMPState);
state.tx_id
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_state_tx_free(state: *mut std::os::raw::c_void,
unsafe extern "C" fn rs_snmp_state_tx_free(state: *mut std::os::raw::c_void,
tx_id: u64)
{
let state = cast_pointer!(state,SNMPState);
state.free_tx(tx_id);
}

#[no_mangle]
pub extern "C" fn rs_snmp_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void,
extern "C" fn rs_snmp_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void,
_direction: u8)
-> std::os::raw::c_int
{
Expand Down Expand Up @@ -356,7 +348,7 @@ fn parse_pdu_envelope_version(i:&[u8]) -> IResult<&[u8],u32> {
}

#[no_mangle]
pub unsafe extern "C" fn rs_snmp_probing_parser(_flow: *const Flow,
unsafe extern "C" fn rs_snmp_probing_parser(_flow: *const Flow,
_direction: u8,
input:*const u8,
input_len: u32,
Expand Down Expand Up @@ -423,7 +415,7 @@ pub unsafe extern "C" fn rs_register_snmp_parser() {
LogTx: rs_snmp_log_json_response,
};
OutputPreRegisterLogger(reg_data);
SigTablePreRegister(ScDetectSNMPRegister);
SigTablePreRegister(detect_snmp_register);
if AppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
// port 161
_ = AppLayerRegisterProtocolDetection(&parser, 1);
Expand Down

0 comments on commit cfc4a8d

Please sign in to comment.