nfs: bound namemap by using lru

Ticket: 8418
pull/15328/merge
Philippe Antoine 3 months ago committed by Victor Julien
parent a50f494ef6
commit aea7ee21b8

@ -20,7 +20,6 @@
use lru::LruCache;
use std;
use std::cmp;
use std::collections::HashMap;
use std::ffi::CString;
use std::num::NonZeroUsize;
@ -102,6 +101,7 @@ static mut ALPROTO_NFS: AppProto = ALPROTO_UNKNOWN;
*/
pub static mut NFS_CFG_MAX_REQ: usize = 512;
pub static mut NFS_CFG_MAX_NAMES: usize = 512;
#[derive(AppLayerFrameType)]
pub enum NFSFrameType {
@ -364,7 +364,7 @@ pub struct NFSState {
pub requestmap: LruCache<u32, NFSRequestXidMap>,
/// map file handle (1) to name (2)
pub namemap: HashMap<Vec<u8>, Vec<u8>>,
pub namemap: LruCache<Vec<u8>, Vec<u8>>,
/// transactions list
pub transactions: Vec<NFSTransaction>,
@ -423,7 +423,7 @@ impl NFSState {
NFSState {
state_data: AppLayerStateData::default(),
requestmap: LruCache::new(NonZeroUsize::new(unsafe { NFS_CFG_MAX_REQ }).unwrap()),
namemap: HashMap::new(),
namemap: LruCache::new(NonZeroUsize::new(unsafe { NFS_CFG_MAX_NAMES }).unwrap()),
transactions: Vec::new(),
ts_chunk_xid: 0,
tc_chunk_xid: 0,
@ -2390,6 +2390,18 @@ pub unsafe extern "C" fn SCRegisterNfsParser() {
SCLogError!("Invalid max-requests value");
}
}
let retval = conf_get("app-layer.protocols.nfs.max-names");
if let Some(val) = retval {
if let Ok(v) = val.parse::<usize>() {
if v > 0 {
NFS_CFG_MAX_NAMES = v;
} else {
SCLogError!("Invalid max-names value, must be >0");
}
} else {
SCLogError!("Invalid max-names value");
}
}
} else {
SCLogDebug!("Protocol detector and parser disabled for nfs.");
}

@ -209,7 +209,7 @@ impl NFSState {
SCLogDebug!("LOOKUP handle {:?}", rd.handle);
self.namemap
.insert(rd.handle.value.to_vec(), xidmap.file_name.to_vec());
.put(rd.handle.value.to_vec(), xidmap.file_name.to_vec());
resp_handle = rd.handle.value.to_vec();
} else {
self.set_event(NFSEvent::MalformedData);
@ -223,7 +223,7 @@ impl NFSState {
if let Some(h) = rd.handle {
SCLogDebug!("handle {:?}", h);
self.namemap
.insert(h.value.to_vec(), xidmap.file_name.to_vec());
.put(h.value.to_vec(), xidmap.file_name.to_vec());
resp_handle = h.value.to_vec();
}
} else {
@ -256,7 +256,7 @@ impl NFSState {
SCLogDebug!("e {:?}", e);
if let Some(ref h) = e.handle {
SCLogDebug!("h {:?}", h);
self.namemap.insert(h.value.to_vec(), e.name_vec.to_vec());
self.namemap.put(h.value.to_vec(), e.name_vec.to_vec());
}
}
}

@ -376,7 +376,7 @@ impl NFSState {
}
Nfs4ResponseContent::GetFH(_s, Some(ref rd)) if insert_filename_with_getfh => {
self.namemap
.insert(rd.value.to_vec(), xidmap.file_name.to_vec());
.put(rd.value.to_vec(), xidmap.file_name.to_vec());
}
Nfs4ResponseContent::PutRootFH(s) if s == NFS4_OK && xidmap.file_name.is_empty() => {
xidmap.file_name = b"<mount_root>".to_vec();

Loading…
Cancel
Save