From f57b7f4a849ea3ba213d66cf385a0e4aacb53141 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Fri, 24 Jan 2025 16:31:01 +0100 Subject: [PATCH] snmp: restrict rust visibility --- rust/src/snmp/detect.rs | 16 +++++++-------- rust/src/snmp/log.rs | 3 +-- rust/src/snmp/snmp.rs | 44 +++++++++++++++++------------------------ 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/rust/src/snmp/detect.rs b/rust/src/snmp/detect.rs index 0c07538c7fe2..fa1010c343d4 100644 --- a/rust/src/snmp/detect.rs +++ b/rust/src/snmp/detect.rs @@ -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 { @@ -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); @@ -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 { @@ -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 { @@ -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); @@ -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 { @@ -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, diff --git a/rust/src/snmp/log.rs b/rust/src/snmp/log.rs index f55644e1b463..91e900f5da65 100644 --- a/rust/src/snmp/log.rs +++ b/rust/src/snmp/log.rs @@ -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); diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index 2b3927aa6b74..10f6617d0222 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -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; @@ -35,18 +35,18 @@ use nom7::error::{ErrorKind, make_error}; use suricata_sys::sys::AppProto; #[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>, @@ -55,7 +55,7 @@ pub struct SNMPState<'a> { tx_id: u64, } -pub struct SNMPPduInfo<'a> { +pub(super) struct SNMPPduInfo<'a> { pub pdu_type: PduType, pub err: ErrorStatus, @@ -65,7 +65,7 @@ pub struct SNMPPduInfo<'a> { pub vars: Vec>, } -pub struct SNMPTransaction<'a> { +pub(super) struct SNMPTransaction<'a> { /// PDU version pub version: u32, @@ -94,7 +94,7 @@ impl Transaction for SNMPTransaction<'_> { } impl<'a> SNMPState<'a> { - pub fn new() -> SNMPState<'a> { + fn new() -> SNMPState<'a> { Default::default() } } @@ -240,7 +240,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, @@ -254,8 +254,7 @@ 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 _; @@ -263,14 +262,12 @@ pub extern "C" fn rs_snmp_state_new(_orig_state: *mut std::os::raw::c_void, _ori /// 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, @@ -280,8 +277,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, @@ -291,8 +287,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 { @@ -303,24 +298,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 { @@ -357,7 +349,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, @@ -424,7 +416,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);