load_config

This commit is contained in:
Pan Qiancheng 2025-04-10 13:31:56 +08:00
parent 80199319f0
commit e9aa7a1e62
14 changed files with 41 additions and 140 deletions

View File

@ -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"
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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

View File

@ -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/

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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];

View File

@ -5,10 +5,6 @@
#include <unistd.h>
#include <sys/shm.h>
// 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

View File

@ -2,7 +2,7 @@
#define STRUCT_H
#define _GNU_SOURCE
#define MAX_RULES 100
#define MAX_RULES 128
#define MAX_ARGS 10
typedef struct {

View File

@ -7,7 +7,7 @@
#include <sys/shm.h>
#include <time.h>
#define SHM_KEY 12345
#define SHM_KEY 0x78945
#define MAX_RULES 100
#define MAX_ARGS 10