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

(RFC) rust: mark extern "C" ffi functions as unsafe - v4 #6276

Closed
wants to merge 3 commits into from
Closed
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
45 changes: 23 additions & 22 deletions rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl AppLayerTxData {
macro_rules!export_tx_data_get {
($name:ident, $type:ty) => {
#[no_mangle]
pub unsafe extern "C" fn $name(tx: *mut std::os::raw::c_void)
pub unsafe extern "C" fn $name(tx: *mut std::ffi::c_void)
-> *mut crate::applayer::AppLayerTxData
{
let tx = &mut *(tx as *mut $type);
Expand Down Expand Up @@ -238,40 +238,41 @@ pub struct RustParser {
/// UNSAFE !
#[macro_export]
macro_rules! build_slice {
($buf:ident, $len:expr) => ( unsafe{ std::slice::from_raw_parts($buf, $len) } );
($buf:ident, $len:expr) => ( std::slice::from_raw_parts($buf, $len) );
}

/// Cast pointer to a variable, as a mutable reference to an object
///
/// UNSAFE !
#[macro_export]
macro_rules! cast_pointer {
($ptr:ident, $ty:ty) => ( unsafe{ &mut *($ptr as *mut $ty) } );
($ptr:ident, $ty:ty) => ( &mut *($ptr as *mut $ty) );
}

pub type ParseFn = extern "C" fn (flow: *const Flow,
pub type ParseFn = unsafe extern "C" fn (flow: *const Flow,
state: *mut c_void,
pstate: *mut c_void,
input: *const u8,
input_len: u32,
data: *const c_void,
flags: u8) -> AppLayerResult;
pub type ProbeFn = extern "C" fn (flow: *const Flow, flags: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto;
pub type ProbeFn = unsafe extern "C" fn (flow: *const Flow, flags: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto;
pub type StateAllocFn = extern "C" fn (*mut c_void, AppProto) -> *mut c_void;
pub type StateFreeFn = extern "C" fn (*mut c_void);
pub type StateTxFreeFn = extern "C" fn (*mut c_void, u64);
pub type StateGetTxFn = extern "C" fn (*mut c_void, u64) -> *mut c_void;
pub type StateGetTxCntFn = extern "C" fn (*mut c_void) -> u64;
pub type StateGetProgressFn = extern "C" fn (*mut c_void, u8) -> c_int;
pub type GetDetectStateFn = extern "C" fn (*mut c_void) -> *mut DetectEngineState;
pub type SetDetectStateFn = extern "C" fn (*mut c_void, &mut DetectEngineState) -> c_int;
pub type GetEventInfoFn = extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8;
pub type GetEventsFn = extern "C" fn (*mut c_void) -> *mut AppLayerDecoderEvents;
pub type StateFreeFn = unsafe extern "C" fn (*mut c_void);
pub type StateTxFreeFn = unsafe
extern "C" fn (*mut c_void, u64);
pub type StateGetTxFn = unsafe extern "C" fn (*mut c_void, u64) -> *mut c_void;
pub type StateGetTxCntFn = unsafe extern "C" fn (*mut c_void) -> u64;
pub type StateGetProgressFn = unsafe extern "C" fn (*mut c_void, u8) -> c_int;
pub type GetDetectStateFn = unsafe extern "C" fn (*mut c_void) -> *mut DetectEngineState;
pub type SetDetectStateFn = unsafe extern "C" fn (*mut c_void, &mut DetectEngineState) -> c_int;
pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int;
pub type GetEventInfoByIdFn = unsafe extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8;
pub type GetEventsFn = unsafe extern "C" fn (*mut c_void) -> *mut AppLayerDecoderEvents;
pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void;
pub type LocalStorageFreeFn = extern "C" fn (*mut c_void);
pub type GetFilesFn = extern "C" fn (*mut c_void, u8) -> *mut FileContainer;
pub type GetTxIteratorFn = extern "C" fn (ipproto: u8, alproto: AppProto,
pub type GetFilesFn = unsafe extern "C" fn (*mut c_void, u8) -> *mut FileContainer;
pub type GetTxIteratorFn = unsafe extern "C" fn (ipproto: u8, alproto: AppProto,
state: *mut c_void,
min_tx_id: u64,
max_tx_id: u64,
Expand Down Expand Up @@ -313,7 +314,7 @@ pub const APP_LAYER_PARSER_BYPASS_READY : u8 = BIT_U8!(4);
pub const APP_LAYER_PARSER_OPT_ACCEPT_GAPS: u32 = BIT_U32!(0);
pub const APP_LAYER_PARSER_OPT_UNIDIR_TXS: u32 = BIT_U32!(1);

pub type AppLayerGetTxIteratorFn = extern "C" fn (ipproto: u8,
pub type AppLayerGetTxIteratorFn = unsafe extern "C" fn (ipproto: u8,
alproto: AppProto,
alstate: *mut c_void,
min_tx_id: u64,
Expand All @@ -330,13 +331,13 @@ extern {

#[repr(C)]
pub struct AppLayerGetTxIterTuple {
tx_ptr: *mut std::os::raw::c_void,
tx_ptr: *mut std::ffi::c_void,
tx_id: u64,
has_next: bool,
}

impl AppLayerGetTxIterTuple {
pub fn with_values(tx_ptr: *mut std::os::raw::c_void, tx_id: u64, has_next: bool) -> AppLayerGetTxIterTuple {
pub fn with_values(tx_ptr: *mut std::ffi::c_void, tx_id: u64, has_next: bool) -> AppLayerGetTxIterTuple {
AppLayerGetTxIterTuple {
tx_ptr: tx_ptr, tx_id: tx_id, has_next: has_next,
}
Expand Down Expand Up @@ -376,7 +377,7 @@ impl LoggerFlags {
macro_rules!export_tx_get_detect_state {
($name:ident, $type:ty) => (
#[no_mangle]
pub extern "C" fn $name(tx: *mut std::os::raw::c_void)
pub unsafe extern "C" fn $name(tx: *mut std::ffi::c_void)
-> *mut core::DetectEngineState
{
let tx = cast_pointer!(tx, $type);
Expand All @@ -397,7 +398,7 @@ macro_rules!export_tx_get_detect_state {
macro_rules!export_tx_set_detect_state {
($name:ident, $type:ty) => (
#[no_mangle]
pub extern "C" fn $name(tx: *mut std::os::raw::c_void,
pub unsafe extern "C" fn $name(tx: *mut std::ffi::c_void,
de_state: &mut core::DetectEngineState) -> std::os::raw::c_int
{
let tx = cast_pointer!(tx, $type);
Expand Down
2 changes: 1 addition & 1 deletion rust/src/applayertemplate/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), Js
}

#[no_mangle]
pub extern "C" fn rs_template_logger_log(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool {
pub unsafe extern "C" fn rs_template_logger_log(tx: *mut std::ffi::c_void, js: &mut JsonBuilder) -> bool {
let tx = cast_pointer!(tx, TemplateTransaction);
log_template(tx, js).is_ok()
}
85 changes: 40 additions & 45 deletions rust/src/applayertemplate/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std;
use crate::core::{self, ALPROTO_UNKNOWN, AppProto, Flow, IPPROTO_TCP};
use std::mem::transmute;
use crate::applayer::{self, *};
use std::ffi::CString;
use std::ffi::{c_void, CString};
use nom;
use super::parser;

Expand Down Expand Up @@ -277,7 +277,7 @@ export_tx_set_detect_state!(

/// C entry point for a probing parser.
#[no_mangle]
pub extern "C" fn rs_template_probing_parser(
pub unsafe extern "C" fn rs_template_probing_parser(
_flow: *const Flow,
_direction: u8,
input: *const u8,
Expand All @@ -288,45 +288,44 @@ pub extern "C" fn rs_template_probing_parser(
if input_len > 1 && input != std::ptr::null_mut() {
let slice = build_slice!(input, input_len as usize);
if probe(slice).is_ok() {
return unsafe { ALPROTO_TEMPLATE };
return ALPROTO_TEMPLATE;
}
}
return ALPROTO_UNKNOWN;
}

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

#[no_mangle]
pub extern "C" fn rs_template_state_free(state: *mut std::os::raw::c_void) {
// Just unbox...
let _drop: Box<TemplateState> = unsafe { transmute(state) };
pub unsafe extern "C" fn rs_template_state_free(state: *mut c_void) {
std::mem::drop(Box::from_raw(state as *mut TemplateState));
}

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

#[no_mangle]
pub extern "C" fn rs_template_parse_request(
pub unsafe extern "C" fn rs_template_parse_request(
_flow: *const Flow,
state: *mut std::os::raw::c_void,
pstate: *mut std::os::raw::c_void,
state: *mut std::ffi::c_void,
pstate: *mut std::ffi::c_void,
input: *const u8,
input_len: u32,
_data: *const std::os::raw::c_void,
_data: *const std::ffi::c_void,
_flags: u8,
) -> AppLayerResult {
let eof = unsafe {
let eof = {
if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0 {
true
} else {
Expand All @@ -353,16 +352,16 @@ pub extern "C" fn rs_template_parse_request(
}

#[no_mangle]
pub extern "C" fn rs_template_parse_response(
pub unsafe extern "C" fn rs_template_parse_response(
_flow: *const Flow,
state: *mut std::os::raw::c_void,
pstate: *mut std::os::raw::c_void,
state: *mut std::ffi::c_void,
pstate: *mut std::ffi::c_void,
input: *const u8,
input_len: u32,
_data: *const std::os::raw::c_void,
_data: *const std::ffi::c_void,
_flags: u8,
) -> AppLayerResult {
let _eof = unsafe {
let _eof = {
if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0 {
true
} else {
Expand All @@ -383,14 +382,14 @@ pub extern "C" fn rs_template_parse_response(
}

#[no_mangle]
pub extern "C" fn rs_template_state_get_tx(
state: *mut std::os::raw::c_void,
pub unsafe extern "C" fn rs_template_state_get_tx(
state: *mut std::ffi::c_void,
tx_id: u64,
) -> *mut std::os::raw::c_void {
) -> *mut std::ffi::c_void {
let state = cast_pointer!(state, TemplateState);
match state.get_tx(tx_id) {
Some(tx) => {
return unsafe { transmute(tx) };
return transmute(tx);
}
None => {
return std::ptr::null_mut();
Expand All @@ -399,16 +398,16 @@ pub extern "C" fn rs_template_state_get_tx(
}

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

#[no_mangle]
pub extern "C" fn rs_template_tx_get_alstate_progress(
tx: *mut std::os::raw::c_void,
pub unsafe extern "C" fn rs_template_tx_get_alstate_progress(
tx: *mut std::ffi::c_void,
_direction: u8,
) -> std::os::raw::c_int {
let tx = cast_pointer!(tx, TemplateTransaction);
Expand All @@ -421,8 +420,8 @@ pub extern "C" fn rs_template_tx_get_alstate_progress(
}

#[no_mangle]
pub extern "C" fn rs_template_state_get_events(
tx: *mut std::os::raw::c_void
pub unsafe extern "C" fn rs_template_state_get_events(
tx: *mut std::ffi::c_void
) -> *mut core::AppLayerDecoderEvents {
let tx = cast_pointer!(tx, TemplateTransaction);
return tx.events;
Expand All @@ -445,18 +444,18 @@ pub extern "C" fn rs_template_state_get_event_info_by_id(_event_id: std::os::raw
return -1;
}
#[no_mangle]
pub extern "C" fn rs_template_state_get_tx_iterator(
pub unsafe extern "C" fn rs_template_state_get_tx_iterator(
_ipproto: u8,
_alproto: AppProto,
state: *mut std::os::raw::c_void,
state: *mut std::ffi::c_void,
min_tx_id: u64,
_max_tx_id: u64,
istate: &mut u64,
) -> applayer::AppLayerGetTxIterTuple {
let state = cast_pointer!(state, TemplateState);
match state.tx_iterator(min_tx_id, istate) {
Some((tx, out_tx_id, has_next)) => {
let c_tx = unsafe { transmute(tx) };
let c_tx = transmute(tx);
let ires = applayer::AppLayerGetTxIterTuple::with_values(
c_tx,
out_tx_id,
Expand All @@ -475,19 +474,17 @@ pub extern "C" fn rs_template_state_get_tx_iterator(
/// No required for parsing, but an example function for retrieving a
/// pointer to the request buffer from C for detection.
#[no_mangle]
pub extern "C" fn rs_template_get_request_buffer(
tx: *mut std::os::raw::c_void,
pub unsafe extern "C" fn rs_template_get_request_buffer(
tx: *mut std::ffi::c_void,
buf: *mut *const u8,
len: *mut u32,
) -> u8
{
let tx = cast_pointer!(tx, TemplateTransaction);
if let Some(ref request) = tx.request {
if request.len() > 0 {
unsafe {
*len = request.len() as u32;
*buf = request.as_ptr();
}
*len = request.len() as u32;
*buf = request.as_ptr();
return 1;
}
}
Expand All @@ -496,19 +493,17 @@ pub extern "C" fn rs_template_get_request_buffer(

/// Get the response buffer for a transaction from C.
#[no_mangle]
pub extern "C" fn rs_template_get_response_buffer(
tx: *mut std::os::raw::c_void,
pub unsafe extern "C" fn rs_template_get_response_buffer(
tx: *mut std::ffi::c_void,
buf: *mut *const u8,
len: *mut u32,
) -> u8
{
let tx = cast_pointer!(tx, TemplateTransaction);
if let Some(ref response) = tx.response {
if response.len() > 0 {
unsafe {
*len = response.len() as u32;
*buf = response.as_ptr();
}
*len = response.len() as u32;
*buf = response.as_ptr();
return 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/asn1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn asn1_decode<'a>(
/// input must be a valid buffer of at least input_len bytes
/// pointer must be freed using `rs_asn1_free`
#[no_mangle]
pub extern "C" fn rs_asn1_decode(
pub unsafe extern "C" fn rs_asn1_decode(
input: *const u8, input_len: u16, buffer_offset: u32, ad_ptr: *const DetectAsn1Data,
) -> *mut Asn1<'static> {
if input.is_null() || input_len == 0 || ad_ptr.is_null() {
Expand All @@ -220,7 +220,7 @@ pub extern "C" fn rs_asn1_decode(

let slice = build_slice!(input, input_len as usize);

let ad = unsafe { &*ad_ptr };
let ad = &*ad_ptr;

let res = asn1_decode(slice, buffer_offset, ad);

Expand Down
2 changes: 1 addition & 1 deletion rust/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

use std::os::raw::c_char;
use std::os::raw::c_void;
use std::ffi::c_void;
use std::os::raw::c_int;
use std::ffi::{CString, CStr};
use std::ptr;
Expand Down
Loading