Skip to content

Commit 431844d

Browse files
committed
Add logging functionality
1 parent 28d20f1 commit 431844d

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

config.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
log_file: /dev/log
1+
log_file: ./log.txt
22
syscalls:
33
- umount2:
44
log: true
@@ -14,8 +14,6 @@ syscalls:
1414
arg0_char: true
1515
arg1: 2
1616
arg1_char: false
17-
- fsmount:
17+
- getdents64:
1818
log: true
1919
block: false
20-
arg0: "/etc/abroot.conf"
21-
arg0_char: true

parse_config.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ header_template = """
2121
#define LOG_FILE "{PYLOGFILE}"
2222
2323
struct syscall {{
24+
char *name;
2425
long callnum;
2526
bool log;
2627
bool block;
@@ -39,7 +40,7 @@ struct syscall {{
3940
4041
struct syscall *next;
4142
struct syscall *prev;
42-
}} syscall_default = {{-1, false, false, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, NULL}};
43+
}} syscall_default = {{NULL, -1, false, false, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, -1, NULL, NULL}};
4344
4445
typedef struct syscall syscall;
4546
@@ -52,7 +53,7 @@ return {FIRSTVARNAME};
5253

5354
structbuild_template = {
5455
"var_define": "syscall *{varname} = (struct syscall *) malloc(sizeof(syscall));\nmemcpy({varname}, &syscall_default, sizeof(syscall));\n",
55-
"set_name": '{varname}->callnum = {name};\n',
56+
"set_name": '{varname}->name = (char *) malloc(strlen("{name}")+1);\nstrcpy({varname}->name, "{name}");\n{varname}->callnum = {name};\n',
5657
"set_log": "{varname}->log = {log};\n",
5758
"set_block": "{varname}->block = {block};\n",
5859
"set_arg_char": '{varname}->{argname} = (char *) malloc(strlen("{arg}")+1);\nstrcpy({varname}->{argname}, "{arg}");\n',
@@ -200,7 +201,7 @@ class Syscall:
200201
if parsed.get("log") is not None:
201202
call.log = bool(parsed.get("log"))
202203
if parsed.get("block") is not None:
203-
call.block = bool(parsed.get("log"))
204+
call.block = bool(parsed.get("block"))
204205
if parsed.get("arg0") is not None:
205206
call.arg0 = str(parsed.get("arg0"))
206207
call.arg0_char = bool(parsed.get("arg0_char"))

src/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ top_srcdir = @top_srcdir@
336336
lib_LTLIBRARIES = libsyscall_interceptor.la
337337
libsyscall_interceptor_la_SOURCES = main.c
338338
libsyscall_interceptor_la_CFLAGS = -fpic -lsyscall_intercept
339-
libsyscall_interceptor_la_LDFLAGS = -version-info 1:0:0 -Og
339+
libsyscall_interceptor_la_LDFLAGS = -version-info 1:0:0
340340
all: all-am
341341

342342
.SUFFIXES:

src/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <syscall.h>
88
#include <errno.h>
99
#include <strings.h>
10+
#include <time.h>
1011

1112
bool
1213
has_flag(long search_flag, long all_flags) {
@@ -50,6 +51,17 @@ match_args(syscall *call, long arg0, long arg1, long arg2, long arg3, long arg4,
5051
return arg0_match && arg1_match && arg2_match && arg3_match && arg4_match && arg5_match;
5152
}
5253

54+
void
55+
log_call(syscall *call) {
56+
FILE *log_file;
57+
time_t t = time(NULL);
58+
struct tm timestruc = *localtime(&t);
59+
60+
log_file = fopen(LOG_FILE, "a");
61+
fprintf(log_file, "Intercepted call %s at %d-%02d-%02d %02d:%02d:%02d\n", call->name, timestruc.tm_year+1900, timestruc.tm_mon+1, timestruc.tm_mday, timestruc.tm_hour, timestruc.tm_min, timestruc.tm_sec);
62+
fclose(log_file);
63+
}
64+
5365
static int
5466
hook (long syscall_number,
5567
long arg0, long arg1,
@@ -62,6 +74,9 @@ hook (long syscall_number,
6274
if (call->callnum == syscall_number
6375
&& match_args(call, arg0, arg1, arg2, arg3, arg4, arg5))
6476
{
77+
if (call->log) {
78+
log_call(call);
79+
}
6580
if (call->block) {
6681
*result = -ENOTSUP;
6782
return 0;

0 commit comments

Comments
 (0)