1010#define MAX_PROCESSES 1000
1111#define MAX_RULES 100
1212
13- // ========== 固定敏感路径放到 .rodata ==========
13+ // ========== Fixed sensitive paths in .rodata ==========
1414const volatile char SENSITIVE_PASSWD [] SEC (".rodata" ) = "/etc/passwd" ;
1515const volatile char SENSITIVE_SHADOW [] SEC (".rodata" ) = "/etc/shadow" ;
1616const volatile char SENSITIVE_SUDOERS [] SEC (".rodata" ) = "/etc/sudoers" ;
1717const volatile char SENSITIVE_ROOT [] SEC (".rodata" ) = "/root/" ;
1818
19- // ========== map 结构 ==========
19+ // ========== Map structures ==========
2020struct access_rule {
2121 char target_path [MAX_FILENAME_LEN ];
2222 char redirect_path [MAX_FILENAME_LEN ];
@@ -76,30 +76,30 @@ struct {
7676 __uint (max_entries , 256 * 1024 );
7777} syscall_events SEC (".maps" );
7878
79- // ========== helpers ==========
79+ // ========== Helper functions ==========
8080static __always_inline void update_stats (__u32 idx ) {
81- // 可选统计map,略
81+ // Optional statistics map, omitted
8282}
8383
8484static __always_inline int bpf_prefix_match (const char * path , const volatile char * prefix , int len ) {
8585 return bpf_strncmp (path , len , (const char * )prefix ) == 0 ;
8686}
8787
88- // ✅ 简化字符串匹配避免verifier无限循环
88+ // ✅ Simplified string matching to avoid verifier infinite loops
8989static __always_inline int match_rule_prefix (const char * path , const char * rule_path ) {
90- // 简化版本:只检查前16个字符以避免verifier复杂度
90+ // Simplified version: only check first 16 characters to avoid verifier complexity
9191 #pragma unroll
9292 for (int i = 0 ; i < 16 ; i ++ ) {
9393 char a = path [i ];
9494 char b = rule_path [i ];
95- if (b == '\0' ) return 1 ; // rule匹配完成
96- if (a == '\0' ) return 0 ; // path结束但rule未完成
97- if (a != b ) return 0 ; // 字符不匹配
95+ if (b == '\0' ) return 1 ; // rule match completed
96+ if (a == '\0' ) return 0 ; // path ended but rule not completed
97+ if (a != b ) return 0 ; // character mismatch
9898 }
9999 return 1 ;
100100}
101101
102- // ✅ 固定敏感路径检测( .rodata)
102+ // ✅ Fixed sensitive path detection ( .rodata)
103103static __always_inline int is_sensitive_path (const char * path ) {
104104 if (bpf_prefix_match (path , SENSITIVE_PASSWD , 11 )) return 1 ;
105105 if (bpf_prefix_match (path , SENSITIVE_SHADOW , 11 )) return 1 ;
@@ -108,7 +108,7 @@ static __always_inline int is_sensitive_path(const char *path) {
108108 return 0 ;
109109}
110110
111- // ✅ threat_score 简化
111+ // ✅ Simplified threat_score calculation
112112static __always_inline int calc_threat_score (const char * path , __u32 uid ) {
113113 int s = 0 ;
114114 if (is_sensitive_path (path )) s += 50 ;
@@ -117,7 +117,7 @@ static __always_inline int calc_threat_score(const char *path, __u32 uid) {
117117 return s ;
118118}
119119
120- // ringbuf event 填充
120+ // ringbuf event population
121121static __always_inline void log_event (__u32 pid , __u32 uid , __u32 gid ,
122122 __u32 syscall_nr ,
123123 const char * orig , const char * mod ,
@@ -149,16 +149,16 @@ int tp_openat(struct trace_event_raw_sys_enter *ctx) {
149149 char fname [MAX_FILENAME_LEN ];
150150 bpf_probe_read_user_str (fname , sizeof (fname ), (void * )ctx -> args [1 ]);
151151
152- // threat_score
152+ // Calculate threat score
153153 __u32 score = calc_threat_score (fname , uid );
154154
155- // 简化规则处理,先只记录事件
155+ // Simplified rule processing, only log events for now
156156 log_event (pid , uid , gid , 257 , fname , fname , "OPENAT" , 3 , 0 , score );
157157
158158 return 0 ;
159159}
160160
161- // ========== execve hook + 阻断 ==========
161+ // ========== execve hook + blocking ==========
162162SEC ("tp/syscalls/sys_enter_execve" )
163163int tp_execve (struct trace_event_raw_sys_enter * ctx ) {
164164 __u32 pid = bpf_get_current_pid_tgid () >> 32 ;
@@ -168,10 +168,10 @@ int tp_execve(struct trace_event_raw_sys_enter *ctx) {
168168 char fname [MAX_FILENAME_LEN ];
169169 bpf_probe_read_user_str (fname , sizeof (fname ), (void * )ctx -> args [0 ]);
170170
171- // threat_score > 50 阻断
171+ // Block if threat score > 50
172172 __u32 score = calc_threat_score (fname , uid );
173173
174- // 简化处理,只记录execve事件
174+ // Simplified processing, only log execve events
175175 log_event (pid , uid , gid , 59 , fname , fname , "EXECVE" , 3 , 0 , score );
176176
177177 return 0 ;
0 commit comments