@@ -2,16 +2,16 @@ use std::collections::HashMap;
2
2
use std:: ffi:: OsStr ;
3
3
use std:: fs;
4
4
use std:: path:: Path ;
5
-
5
+ use std :: os :: unix :: fs :: PermissionsExt ;
6
6
use anyhow:: { anyhow, Result , Result as AnyhowResult } ;
7
7
use crossbeam:: channel:: Sender ;
8
8
use fs_extra:: dir:: { copy, create_all, CopyOptions } ;
9
- use fs_extra:: file:: { copy as file_copy, CopyOptions as FileCopyOptions } ;
9
+ use fs_extra:: file:: { copy as file_copy, remove , CopyOptions as FileCopyOptions } ;
10
10
use libraspserver:: proto:: { PidMissingProbeConfig , ProbeConfigData } ;
11
11
use log:: * ;
12
12
13
13
use crate :: cpython:: { python_attach, CPythonProbe , CPythonProbeState } ;
14
- use crate :: golang:: { golang_attach, GolangProbe , GolangProbeState } ;
14
+ use crate :: golang:: { check_golang_version , golang_attach, GolangProbe , GolangProbeState } ;
15
15
use crate :: jvm:: { check_java_version, java_attach, java_detach, JVMProbe , JVMProbeState } ;
16
16
use crate :: nodejs:: { check_nodejs_version, nodejs_attach, NodeJSProbe } ;
17
17
use crate :: php:: { php_attach, PHPProbeState } ;
@@ -41,8 +41,8 @@ impl RASPManager {
41
41
) -> AnyhowResult < ( ) > {
42
42
debug ! ( "starting comm with probe, target pid: {}" , process_info. pid) ;
43
43
let mnt_namespace = process_info. get_mnt_ns ( ) ?;
44
- let nspid = if let Some ( nspid) = ProcessInfo :: read_nspid ( process_info . pid ) ? {
45
- nspid
44
+ let nspid = if process_info . nspid != 0 {
45
+ process_info . nspid
46
46
} else {
47
47
process_info. pid
48
48
} ;
@@ -259,8 +259,8 @@ impl RASPManager {
259
259
}
260
260
}
261
261
262
- serde_json:: to_string ( & valid_messages) ?;
263
- // self.write_message_to_config_file(pid, nspid, valid_messages_string)?;
262
+ let valid_messages_string = serde_json:: to_string ( & valid_messages) ?;
263
+ self . write_message_to_config_file ( pid, nspid, valid_messages_string) ?;
264
264
265
265
Ok ( ( ) )
266
266
}
@@ -324,9 +324,9 @@ impl RASPManager {
324
324
let runtime_info = & process_info. runtime . clone ( ) . unwrap ( ) ;
325
325
let root_dir = format ! ( "/proc/{}/root" , process_info. pid) ;
326
326
let pid = process_info. pid ;
327
- ProcessInfo :: read_nspid ( pid ) ? . ok_or ( anyhow ! ( "can not read nspid: {}" , pid ) ) ? ;
327
+ let nspid = process_info . nspid ;
328
328
// delete config
329
- // self.delete_config_file(pid, nspid)?;
329
+ self . delete_config_file ( pid, nspid) ?;
330
330
let attach_result = match runtime_info. name {
331
331
"JVM" => match JVMProbeState :: inspect_process ( process_info) ? {
332
332
ProbeState :: Attached => {
@@ -371,7 +371,7 @@ impl RASPManager {
371
371
let to = format ! ( "{}{}" , root_dir. clone( ) , settings:: RASP_JAVA_AGENT_BIN ( ) ) ;
372
372
let _ = self . copy_file_from_to_dest ( settings:: RASP_JAVA_JATTACH_BIN ( ) , root_dir. clone ( ) ) ;
373
373
let _ = self . copy_file_from_to_dest ( settings:: RASP_JAVA_AGENT_BIN ( ) , root_dir. clone ( ) ) ;
374
- info ! ( "copy from jattach /SmithAgent.jar to {}" , to. clone( ) ) ;
374
+ info ! ( "copy from java /SmithAgent.jar to {}" , to. clone( ) ) ;
375
375
}
376
376
}
377
377
Err ( e) => {
@@ -430,6 +430,14 @@ impl RASPManager {
430
430
Ok ( true )
431
431
}
432
432
ProbeState :: NotAttach => {
433
+ if !runtime_info. version . is_empty ( ) {
434
+ match check_golang_version ( & runtime_info. version ) {
435
+ Ok ( _) => { }
436
+ Err ( e) => {
437
+ return Err ( anyhow ! ( e) ) ;
438
+ }
439
+ }
440
+ }
433
441
let mut golang_attach = |pid : i32 , bpf : bool | -> AnyhowResult < bool > {
434
442
if bpf {
435
443
if let Some ( bpf_manager) = self . ebpf_comm . as_mut ( ) {
@@ -881,66 +889,40 @@ impl MntNamespaceTracer {
881
889
}
882
890
883
891
impl RASPManager {
884
- /*
885
892
pub fn write_message_to_config_file (
886
893
& self ,
887
894
pid : i32 ,
888
895
nspid : i32 ,
889
896
message : String ,
890
897
) -> AnyhowResult < ( ) > {
891
- let config_dir = "/ var/run/elkeid_rasp";
898
+ let config_dir = format ! ( "/proc/{}/root/ var/run/elkeid_rasp", pid ) ;
892
899
let config_path = format ! ( "{}/{}.json" , config_dir, nspid) ;
893
900
let config_path_bak = format ! ( "{}.bak" , config_path) ;
894
- debug!("write message to {} {}", config_path_bak, message);
895
- crate::async_command::run_async_process(
896
- Command::new(crate::settings::RASP_NS_ENTER_BIN()).args([
897
- "-m",
898
- "-t",
899
- pid.to_string().as_str(),
900
- "sh",
901
- "-c",
902
- "PATH=/bin:/usr/bin:/sbin",
903
- format!(
904
- "mkdir -p {} && echo '{}' > {} && mv {} {}",
905
- config_dir, message, config_path_bak, config_path_bak, config_path
906
- )
907
- .as_str(),
908
- ]),
909
- )?;
910
- let ns_thread = thread::Builder::new().spawn(move || -> AnyhowResult<()> {
911
- debug!("switch namespace");
912
- libraspserver::ns::switch_namespace(pid);
913
- if !Path::new(&config_dir).exists() {
914
- fs_extra::dir::create(config_dir, true)?;
915
- }
916
- fs_extra::file::write_all(&config_path_bak, message.as_str())?;
917
- let mut option = fs_extra::file::CopyOptions::new();
918
- option.overwrite = true;
919
- fs_extra::file::move_file(config_path_bak, config_path, &option)?;
920
- Ok(())
921
- }).unwrap();
922
- ns_thread.join()?;
901
+ info ! ( "write message to {} {}" , config_path_bak, message) ;
902
+
903
+ if !Path :: new ( & config_dir) . exists ( ) {
904
+ fs_extra:: dir:: create ( & config_dir, true ) ?;
905
+ }
906
+ fs:: set_permissions ( & config_dir, fs:: Permissions :: from_mode ( 0o666 ) ) ?;
907
+ fs_extra:: file:: write_all ( & config_path_bak, message. as_str ( ) ) ?;
908
+ fs:: set_permissions ( & config_path_bak, fs:: Permissions :: from_mode ( 0o777 ) ) ?;
909
+ let mut option = fs_extra:: file:: CopyOptions :: new ( ) ;
910
+ option. overwrite = true ;
911
+ fs_extra:: file:: move_file ( config_path_bak, config_path, & option) ?;
912
+ info ! ( "write message success" ) ;
923
913
924
914
Ok ( ( ) )
925
915
}
926
916
927
917
pub fn delete_config_file ( & self , pid : i32 , nspid : i32 ) -> AnyhowResult < ( ) > {
928
- let config_path = format!("/var/run/elkeid_rasp/{}.json", nspid);
918
+
919
+ let config_path = format ! ( "/proc/{}/root/var/run/elkeid_rasp/{}.json" , pid, nspid) ;
929
920
if Path :: new ( & config_path) . exists ( ) {
930
- crate::async_command::run_async_process(
931
- Command::new(crate::settings::RASP_NS_ENTER_BIN()).args([
932
- "-m",
933
- "-t",
934
- pid.to_string().as_str(),
935
- "sh",
936
- "-c",
937
- format!("rm {}", config_path).as_str(),
938
- ]),
939
- )?;
921
+ info ! ( "delete config file: {}" , config_path) ;
922
+ remove ( config_path) ?
940
923
}
941
924
Ok ( ( ) )
942
925
}
943
- */
944
926
}
945
927
946
928
fn read_dir < P > ( path : P ) -> AnyhowResult < Vec < fs:: DirEntry > >
0 commit comments