kernel/sulog: Optimise the sulog log format

This commit is contained in:
ShirkNeko
2025-11-14 12:55:57 +08:00
parent 7afcdb3059
commit 7e7713ee4a

View File

@@ -72,6 +72,33 @@ static void ksu_get_cmdline(char *full_comm, const char *comm, size_t buf_len)
full_comm[n < buf_len ? n : buf_len - 1] = '\0'; full_comm[n < buf_len ? n : buf_len - 1] = '\0';
} }
static void sanitize_string(char *str, size_t len)
{
if (!str || len == 0)
return;
size_t read_pos = 0, write_pos = 0;
while (read_pos < len && str[read_pos] != '\0') {
char c = str[read_pos];
if (c == '\n' || c == '\r') {
read_pos++;
continue;
}
if (c == ' ' && write_pos > 0 && str[write_pos - 1] == ' ') {
read_pos++;
continue;
}
str[write_pos++] = c;
read_pos++;
}
str[write_pos] = '\0';
}
static bool dedup_should_print(uid_t uid, u8 type, const char *content, size_t len) static bool dedup_should_print(uid_t uid, u8 type, const char *content, size_t len)
{ {
struct dedup_key key = { struct dedup_key key = {
@@ -178,9 +205,11 @@ void ksu_sulog_report_su_grant(uid_t uid, const char *comm, const char *method)
get_timestamp(timestamp, sizeof(timestamp)); get_timestamp(timestamp, sizeof(timestamp));
ksu_get_cmdline(full_comm, comm, sizeof(full_comm)); ksu_get_cmdline(full_comm, comm, sizeof(full_comm));
sanitize_string(full_comm, sizeof(full_comm));
snprintf(log_buf, sizeof(log_buf), snprintf(log_buf, sizeof(log_buf),
"[%s]SU_GRANT:UID=%d COMM=%s METHOD=%s PID=%d", "[%s] SU_GRANT: UID=%d COMM=%s METHOD=%s PID=%d\n",
timestamp, uid, full_comm, method ? method : "unknown", current->pid); timestamp, uid, full_comm, method ? method : "unknown", current->pid);
sulog_add_entry(log_buf, strlen(log_buf), uid, DEDUP_SU_GRANT); sulog_add_entry(log_buf, strlen(log_buf), uid, DEDUP_SU_GRANT);
@@ -197,9 +226,11 @@ void ksu_sulog_report_su_attempt(uid_t uid, const char *comm, const char *target
get_timestamp(timestamp, sizeof(timestamp)); get_timestamp(timestamp, sizeof(timestamp));
ksu_get_cmdline(full_comm, comm, sizeof(full_comm)); ksu_get_cmdline(full_comm, comm, sizeof(full_comm));
sanitize_string(full_comm, sizeof(full_comm));
snprintf(log_buf, sizeof(log_buf), snprintf(log_buf, sizeof(log_buf),
"[%s]SU_EXEC:UID=%d COMM=%s TARGET=%s RESULT=%s PID=%d", "[%s] SU_EXEC: UID=%d COMM=%s TARGET=%s RESULT=%s PID=%d\n",
timestamp, uid, full_comm, target_path ? target_path : "unknown", timestamp, uid, full_comm, target_path ? target_path : "unknown",
success ? "SUCCESS" : "DENIED", current->pid); success ? "SUCCESS" : "DENIED", current->pid);
@@ -217,9 +248,11 @@ void ksu_sulog_report_permission_check(uid_t uid, const char *comm, bool allowed
get_timestamp(timestamp, sizeof(timestamp)); get_timestamp(timestamp, sizeof(timestamp));
ksu_get_cmdline(full_comm, comm, sizeof(full_comm)); ksu_get_cmdline(full_comm, comm, sizeof(full_comm));
sanitize_string(full_comm, sizeof(full_comm));
snprintf(log_buf, sizeof(log_buf), snprintf(log_buf, sizeof(log_buf),
"[%s]PERM_CHECK:UID=%d COMM=%s RESULT=%s PID=%d", "[%s] PERM_CHECK: UID=%d COMM=%s RESULT=%s PID=%d\n",
timestamp, uid, full_comm, allowed ? "ALLOWED" : "DENIED", current->pid); timestamp, uid, full_comm, allowed ? "ALLOWED" : "DENIED", current->pid);
sulog_add_entry(log_buf, strlen(log_buf), uid, DEDUP_PERM_CHECK); sulog_add_entry(log_buf, strlen(log_buf), uid, DEDUP_PERM_CHECK);
@@ -236,9 +269,11 @@ void ksu_sulog_report_manager_operation(const char *operation, uid_t manager_uid
get_timestamp(timestamp, sizeof(timestamp)); get_timestamp(timestamp, sizeof(timestamp));
ksu_get_cmdline(full_comm, NULL, sizeof(full_comm)); ksu_get_cmdline(full_comm, NULL, sizeof(full_comm));
sanitize_string(full_comm, sizeof(full_comm));
snprintf(log_buf, sizeof(log_buf), snprintf(log_buf, sizeof(log_buf),
"[%s]MANAGER_OP:OP=%s MANAGER_UID=%d TARGET_UID=%d COMM=%s PID=%d", "[%s] MANAGER_OP: OP=%s MANAGER_UID=%d TARGET_UID=%d COMM=%s PID=%d\n",
timestamp, operation ? operation : "unknown", manager_uid, target_uid, full_comm, current->pid); timestamp, operation ? operation : "unknown", manager_uid, target_uid, full_comm, current->pid);
sulog_add_entry(log_buf, strlen(log_buf), manager_uid, DEDUP_MANAGER_OP); sulog_add_entry(log_buf, strlen(log_buf), manager_uid, DEDUP_MANAGER_OP);
@@ -255,9 +290,11 @@ void ksu_sulog_report_syscall(uid_t uid, const char *comm, const char *syscall,
get_timestamp(timestamp, sizeof(timestamp)); get_timestamp(timestamp, sizeof(timestamp));
ksu_get_cmdline(full_comm, comm, sizeof(full_comm)); ksu_get_cmdline(full_comm, comm, sizeof(full_comm));
sanitize_string(full_comm, sizeof(full_comm));
snprintf(log_buf, sizeof(log_buf), snprintf(log_buf, sizeof(log_buf),
"[%s]SYSCALL:UID=%d COMM=%s SYSCALL=%s ARGS=%s PID=%d", "[%s] SYSCALL: UID=%d COMM=%s SYSCALL=%s ARGS=%s PID=%d\n",
timestamp, uid, full_comm, syscall ? syscall : "unknown", timestamp, uid, full_comm, syscall ? syscall : "unknown",
args ? args : "none", current->pid); args ? args : "none", current->pid);