diff --git a/.vscode/settings.json b/.vscode/settings.json index 2ae650d..a62f712 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,7 +20,10 @@ "signal_handlers.h": "c", "terminal_utils.h": "c", "stdlib.h": "c", - "init_cleanup.h": "c" + "init_cleanup.h": "c", + "stdbool.h": "c", + "stat.h": "c", + "debug.h": "c" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/build/config.o b/build/config.o index 51c4978..f238911 100644 Binary files a/build/config.o and b/build/config.o differ diff --git a/build/execve_interceptor.o b/build/execve_interceptor.o index 84a2c4e..368d94b 100644 Binary files a/build/execve_interceptor.o and b/build/execve_interceptor.o differ diff --git a/build/init_cleanup.o b/build/init_cleanup.o index 96df6cb..370a793 100644 Binary files a/build/init_cleanup.o and b/build/init_cleanup.o differ diff --git a/build/intercept.so b/build/intercept.so index 9de3ec5..9669441 100755 Binary files a/build/intercept.so and b/build/intercept.so differ diff --git a/logs/execve.log b/logs/execve.log index 697596d..e69de29 100644 --- a/logs/execve.log +++ b/logs/execve.log @@ -1,73 +0,0 @@ -[Thu Apr 10 10:26:49 2025 -] Command: /usr/bin/lesspipe -arg[0]: lesspipe -[Thu Apr 10 10:26:49 2025 -] Command: /usr/bin/dircolors -arg[0]: dircolors -arg[1]: -b -[Thu Apr 10 10:26:50 2025 -] Command: /usr/bin/ls -arg[0]: ls -arg[1]: --color=auto -arg[2]: -CF -[Thu Apr 10 10:26:55 2025 -] Command: /usr/bin/ls -arg[0]: ls -arg[1]: --color=auto -[Thu Apr 10 10:27:07 2025 -] Command: /usr/bin/lesspipe -arg[0]: lesspipe -[Thu Apr 10 10:27:07 2025 -] Command: /usr/bin/dircolors -arg[0]: dircolors -arg[1]: -b -[Thu Apr 10 10:27:09 2025 -] Command: /usr/bin/ls -arg[0]: ls -arg[1]: --color=auto -arg[2]: -CF -[Thu Apr 10 10:39:27 2025 -] Command: /bin/lesspipe -arg[0]: lesspipe -[Thu Apr 10 10:39:27 2025 -] Command: /bin/dircolors -arg[0]: dircolors -arg[1]: -b -[Thu Apr 10 10:39:28 2025 -] Command: /bin/ls -arg[0]: ls -arg[1]: --color=auto -arg[2]: -CF -[Thu Apr 10 10:39:41 2025 -] Command: /usr/lib/command-not-found -arg[0]: /usr/lib/command-not-found -arg[1]: -- -arg[2]: nvidia-smi -[Thu Apr 10 10:40:28 2025 -] Command: /bin/lesspipe -arg[0]: lesspipe -[Thu Apr 10 10:40:28 2025 -] Command: /bin/dircolors -arg[0]: dircolors -arg[1]: -b -[Thu Apr 10 10:40:29 2025 -] Command: /bin/ls -arg[0]: ls -arg[1]: --color=auto -arg[2]: -CF -[Thu Apr 10 10:43:39 2025 -] Command: /bin/ls -arg[0]: ls -arg[1]: --color=auto -[Thu Apr 10 10:45:00 2025 -] Command: /bin/lesspipe -arg[0]: lesspipe -[Thu Apr 10 10:45:00 2025 -] Command: /bin/dircolors -arg[0]: dircolors -arg[1]: -b -[Thu Apr 10 10:45:01 2025 -] Command: /bin/ls -arg[0]: ls -arg[1]: --color=auto -arg[2]: -CF diff --git a/logs/execve_out.log b/logs/execve_out.log index c8ad028..e69de29 100644 --- a/logs/execve_out.log +++ b/logs/execve_out.log @@ -1,21 +0,0 @@ - [DEBUG][PID 12935] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 12935] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build/ config/ logs/ output.txt src/ test_bash.sh* tests/ - [DEBUG][PID 13023] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 13023] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build config logs output.txt src test_bash.sh tests - [DEBUG][PID 13349] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 13349] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build/ config/ logs/ output.txt src/ test_bash.sh* tests/ - [DEBUG][PID 26525] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 26525] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build/ config/ logs/ output.txt src/ test_bash.sh* tests/ - [DEBUG][PID 27494] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 27494] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build/ config/ logs/ output.txt src/ test_bash.sh* tests/ - [DEBUG][PID 30654] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 30654] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build config logs output.txt src test_bash.sh tests - [DEBUG][PID 32199] src/pty_dup.c:43:dupIO(): forkpty result is: 0. - [DEBUG][PID 32199] src/pty_dup.c:50:dupIO(): Child process ready. -Makefile README.md build/ config/ logs/ output.txt src/ test_bash.sh* tests/ diff --git a/src/config.c b/src/config.c index 227cfaa..47a205f 100644 --- a/src/config.c +++ b/src/config.c @@ -8,31 +8,29 @@ #include "debug.h" -// Global variables (defined in execve_interceptor.c) -extern ConfigData *shared_config; -extern int shm_id; +ConfigData *load_config() { + ConfigData *shared_config; + // First load, attach to shared memory in read-only mode + DEBUG_LOG("Attaching to shared memory for config data"); -void load_config_if_needed() { - if (shared_config == NULL) { - // First load, attach to shared memory - DEBUG_LOG("Attaching to shared memory for config data"); - shm_id = shmget(SHM_KEY, sizeof(ConfigData), 0644); + // 不指定大小,让shmget自动获取已存在的共享内存段大小 + int shm_id = shmget(SHM_KEY, SHM_SIZE, 0); // 把size参数设为0 - DEBUG_LOG("shm_id is: %d", shm_id); + DEBUG_LOG("shm_id is: %d", shm_id); - if (shm_id == -1) { - perror("shmget failed"); - return; - } - - shared_config = (ConfigData *)shmat(shm_id, NULL, 0); - if (shared_config == (void *)-1) { - perror("shmat failed"); - shared_config = NULL; - return; - } - DEBUG_LOG("Successfully attached to shared memory"); + if (shm_id == -1) { + perror("shmget failed"); + return NULL; } + shared_config = (ConfigData *)shmat(shm_id, NULL, SHM_RDONLY); + if (shared_config == (void *)-1) { + perror("shmat failed"); + DEBUG_LOG("Call shmat failed."); + shared_config = NULL; + return NULL; + } + DEBUG_LOG("Successfully attached to shared memory"); DEBUG_LOG("Current configuration has %d rules", shared_config->rule_count); + return shared_config; } \ No newline at end of file diff --git a/src/config.h b/src/config.h index 73cbd3a..c62285c 100644 --- a/src/config.h +++ b/src/config.h @@ -3,8 +3,6 @@ #include "exec_hook.h" -int load_config_to_shm(); -int config_file_modified(); -void load_config_if_needed(); +ConfigData *load_config(); #endif // CONFIG_H \ No newline at end of file diff --git a/src/exec_hook.h b/src/exec_hook.h index b3575a0..98f95d3 100644 --- a/src/exec_hook.h +++ b/src/exec_hook.h @@ -40,12 +40,7 @@ #define ANSI_COLOR_RESET "\033[0m" #define ANSI_COLOR_BLUE "\x1b[34m" -#define SHM_KEY 789357 - -// Global variable, pointing to the configuration data in shared memory -extern ConfigData *shared_config; -extern int shm_id; -extern time_t last_modified_time; -// extern int is_initialized; +#define SHM_KEY 0x78945 +#define SHM_SIZE 512 * 1024 #endif // EXEC_HOOK_H \ No newline at end of file diff --git a/src/execve_interceptor.c b/src/execve_interceptor.c index e39b3ca..ac35352 100644 --- a/src/execve_interceptor.c +++ b/src/execve_interceptor.c @@ -15,12 +15,6 @@ #include "rules.h" #include "utils.h" -// Global variables (declared in exec_hook.h and defined here) -ConfigData *shared_config = NULL; -int shm_id = -1; -time_t last_modified_time = 0; -// int is_initialized = 0; - #ifdef HOOK // Original pointer orig_execve_type orig_execve = NULL; @@ -77,7 +71,8 @@ int enhance_execve(const char *filename, char *const argv[], #endif // Load configuration (only if needed) - load_config_if_needed(); + ConfigData *shared_config; + shared_config = load_config(); DEBUG_LOG("Loaded done."); @@ -117,6 +112,16 @@ int enhance_execve(const char *filename, char *const argv[], #endif } + // 如果rule是0,也直接返回 + if (shared_config->rule_count == 0) { +#ifdef HOOK + return orig_execve(filename, argv, envp); +#else + return execve(filename, argv, envp); + // return 1; +#endif + } + write_log(filename, argv); const char *basename = argv[0]; diff --git a/src/init_cleanup.c b/src/init_cleanup.c index dcab9c4..e8ddda0 100644 --- a/src/init_cleanup.c +++ b/src/init_cleanup.c @@ -5,10 +5,6 @@ #include #include -// Global variables (defined in execve_interceptor.c) -extern ConfigData *shared_config; -extern int shm_id; - // // Constructor, executed when the library is loaded // __attribute__((constructor)) static void initialize() { // if (is_initialized) return; @@ -24,7 +20,7 @@ __attribute__((destructor)) void cleanup_shared_memory() { // Log output paths DEBUG_LOG("Log file: %s", LOG_FILE); DEBUG_LOG("Log out file: %s", LOG_OUT_FILE); - DEBUG_LOG("Shared memory ID: %d", shm_id); + // DEBUG_LOG("Shared memory ID: %d", shm_id); // if (shared_config != NULL) { // DEBUG_LOG("Cleaning up shared memory."); // // Detach shared memory segment diff --git a/src/struct.h b/src/struct.h index cf33a9e..ebbba4a 100644 --- a/src/struct.h +++ b/src/struct.h @@ -2,7 +2,7 @@ #define STRUCT_H #define _GNU_SOURCE -#define MAX_RULES 100 +#define MAX_RULES 128 #define MAX_ARGS 10 typedef struct { diff --git a/tests/shm_reader.c b/tests/shm_reader.c index fe254f3..c7184d5 100644 --- a/tests/shm_reader.c +++ b/tests/shm_reader.c @@ -7,7 +7,7 @@ #include #include -#define SHM_KEY 12345 +#define SHM_KEY 0x78945 #define MAX_RULES 100 #define MAX_ARGS 10