Skip to content

Commit

Permalink
fix(test/libsinsp_e2e): avoid possible heap use after free.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Oct 7, 2024
1 parent c8e5618 commit 634af98
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions test/libsinsp_e2e/forking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ TEST_F(sys_call_test, forking_clone_nofs) {
int flags = CLONE_FS | CLONE_VM;
int drflags = PPM_CL_CLONE_FS | PPM_CL_CLONE_VM;

const int STACK_SIZE = 65536; /* Stack size for cloned child */
char* stack; /* Start of stack buffer area */
char* stackTop; /* End of stack buffer area */

//
// FILTER
//
Expand All @@ -477,10 +481,7 @@ TEST_F(sys_call_test, forking_clone_nofs) {
// TEST CODE
//
run_callback_t test = [&](sinsp* inspector) {
const int STACK_SIZE = 65536; /* Stack size for cloned child */
char* stack; /* Start of stack buffer area */
char* stackTop; /* End of stack buffer area */
clone_params cp; /* Passed to child function */
clone_params cp; /* Passed to child function */
int status;
pid_t pid;

Expand Down Expand Up @@ -520,7 +521,6 @@ TEST_F(sys_call_test, forking_clone_nofs) {
close(cp.fd);

sleep(1);
free(stack);
};

//
Expand Down Expand Up @@ -579,7 +579,10 @@ TEST_F(sys_call_test, forking_clone_nofs) {
}
};

ASSERT_NO_FATAL_FAILURE({ event_capture::run(test, callback, filter); });
after_capture_t cleanup = [&](sinsp* inspector) { free(stack); };

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, filter, event_capture::do_nothing, cleanup); });
EXPECT_EQ(callnum, 4);
}

Expand All @@ -603,6 +606,10 @@ TEST_F(sys_call_test, forking_clone_cwd) {
int drflags = PPM_CL_CLONE_VM | PPM_CL_CLONE_FS | PPM_CL_CLONE_FILES | PPM_CL_CLONE_SIGHAND |
PPM_CL_CLONE_THREAD;

const int STACK_SIZE = 65536; /* Stack size for cloned child */
char* stack; /* Start of stack buffer area */
char* stackTop; /* End of stack buffer area */

//
// FILTER
//
Expand All @@ -612,10 +619,6 @@ TEST_F(sys_call_test, forking_clone_cwd) {
// TEST CODE
//
run_callback_t test = [&](sinsp* inspector) {
const int STACK_SIZE = 65536; /* Stack size for cloned child */
char* stack; /* Start of stack buffer area */
char* stackTop; /* End of stack buffer area */

ASSERT_TRUE(getcwd(oriwd, 1024) != NULL);

/* Allocate stack for child */
Expand All @@ -636,8 +639,6 @@ TEST_F(sys_call_test, forking_clone_cwd) {
std::string tmps = getcwd(bcwd, 256);

ASSERT_TRUE(chdir(oriwd) == 0);

free(stack);
};

//
Expand Down Expand Up @@ -677,7 +678,10 @@ TEST_F(sys_call_test, forking_clone_cwd) {
}
};

ASSERT_NO_FATAL_FAILURE({ event_capture::run(test, callback, filter); });
after_capture_t cleanup = [&](sinsp* inspector) { free(stack); };

ASSERT_NO_FATAL_FAILURE(
{ event_capture::run(test, callback, filter, event_capture::do_nothing, cleanup); });

EXPECT_EQ(3, callnum);
}
Expand Down

0 comments on commit 634af98

Please sign in to comment.