Skip to content

Commit eb91382

Browse files
committed
Use arrays to store syscalls
Uses arrays to store configured syscalls to ensure easier memory management and stop syscall-interceptor from segfaulting if too many calls are configured
1 parent 93393a6 commit eb91382

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

parse_config.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@
5050
}} syscall_default = {{NULL, -1, false, false, NULL, -1, 0, false, NULL, -1, 0, false, NULL, -1, 0, false, NULL, -1, 0, false, NULL, -1, 0, false, NULL, -1, 0, false, NULL, NULL}};
5151
5252
typedef struct conf_syscall conf_syscall;
53+
const int calls_size = {CALLLEN};
54+
conf_syscall calls[{CALLLEN}];
55+
int finishInit = 0;
5356
54-
conf_syscall *
55-
get_calls() {{
57+
void get_calls() {{
5658
{PYSTRUCTBUILD}
57-
return {FIRSTVARNAME};
59+
finishInit = 1;
5860
}};
5961
"""
6062

@@ -67,8 +69,7 @@
6769
"set_arg_long": '{varname}->{argname}_long = {arg};\n',
6870
"set_arg_matchtype": '{varname}->{argname}_matchtype = {matchtype};\n',
6971
"set_arg_isfdesc": '{varname}->{argname}_fdesc = {isfdesc};\n',
70-
"set_next": "{varname}->next = {nextcall};\n",
71-
"set_prev": "{varname}->prev = {prevcall};\n",
72+
"set_array": "calls[{num}] = *{varname};\n",
7273
}
7374

7475

@@ -205,39 +206,15 @@ def build_c_code(self) -> str:
205206
i = i - 1
206207
c_structs = c_structs + "\n"
207208

208-
linked_list_setup: str = ""
209-
if len(self.syscalls) == 1:
210-
linked_list_setup = linked_list_setup + structbuild_template["set_next"].format(varname="call1", nextcall="NULL")
211-
linked_list_setup = linked_list_setup + structbuild_template["set_prev"].format(varname="call1", prevcall="NULL")
212-
else:
213-
for i in range(1, len(self.syscalls) + 1):
214-
if i == 1:
215-
linked_list_setup = linked_list_setup + structbuild_template[
216-
"set_next"
217-
].format(varname="call" + str(i), nextcall="call" + str(i + 1))
218-
linked_list_setup = linked_list_setup + structbuild_template[
219-
"set_prev"
220-
].format(varname="call" + str(i), prevcall="NULL")
221-
elif i == len(self.syscalls):
222-
linked_list_setup = linked_list_setup + structbuild_template[
223-
"set_prev"
224-
].format(varname="call" + str(i), prevcall="call" + str(i - 1))
225-
linked_list_setup = linked_list_setup + structbuild_template[
226-
"set_next"
227-
].format(varname="call" + str(i), nextcall="NULL")
228-
else:
229-
linked_list_setup = linked_list_setup + structbuild_template[
230-
"set_prev"
231-
].format(varname="call" + str(i), prevcall="call" + str(i - 1))
232-
linked_list_setup = linked_list_setup + structbuild_template[
233-
"set_next"
234-
].format(varname="call" + str(i), nextcall="call" + str(i + 1))
235-
236-
c_structs = c_structs + linked_list_setup
209+
call_arr_setup: str = ""
210+
for i in range(0, len(self.syscalls)):
211+
call_arr_setup = call_arr_setup + structbuild_template["set_array"].format(num=str(i), varname="call"+str(i+1))
212+
213+
c_structs = c_structs + call_arr_setup
237214
c_code = header_template.format(
238215
PYLOGFILE=self.log_file,
239216
PYSTRUCTBUILD=c_structs,
240-
FIRSTVARNAME="call1",
217+
CALLLEN=len(self.syscalls),
241218
)
242219
return c_code
243220

src/main.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ argcmp(int matchtype, long config_arg_long, char *config_arg_char, long sys_arg)
4141
default:
4242
return_val = false;
4343
}
44-
4544
return return_val;
4645
}
4746

@@ -188,24 +187,22 @@ log_call(conf_syscall * call)
188187
static int
189188
hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long *result)
190189
{
191-
conf_syscall *call = get_calls ();
192-
while (true)
190+
if (finishInit == 0)
191+
get_calls ();
192+
for (int i=0; i < calls_size; i++)
193193
{
194-
if (call->callnum == syscall_number && match_args (call, arg0, arg1, arg2, arg3, arg4, arg5))
194+
if (calls[i].callnum == syscall_number && match_args (&calls[i], arg0, arg1, arg2, arg3, arg4, arg5))
195195
{
196-
if (call->log)
197-
log_call (call);
198-
if (call->block)
196+
if (calls[i].log)
197+
log_call (&calls[i]);
198+
if (calls[i].block)
199199
{
200200
*result = -ENOTSUP;
201201
return 0;
202202
}
203203
}
204-
if (!call->next)
205-
return 1;
206-
207-
call = call->next;
208204
}
205+
return 1;
209206
}
210207

211208
static __attribute__ ((constructor))

0 commit comments

Comments
 (0)