From c550ab7aa40a7ef42ebf29c759af09768d4e2736 Mon Sep 17 00:00:00 2001 From: Mattia Meleleo Date: Thu, 11 Jan 2024 14:41:17 +0100 Subject: [PATCH] fix: race condition in EventsTrace ctx init --- non-GPL/Events/EventsTrace/EventsTrace.c | 10 ++++++- testing/test_bins/create_rename_delete_file.c | 26 +++++++++++++++++-- testing/testrunner/tests.go | 13 +++++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/non-GPL/Events/EventsTrace/EventsTrace.c b/non-GPL/Events/EventsTrace/EventsTrace.c index a66321d7..2469346b 100644 --- a/non-GPL/Events/EventsTrace/EventsTrace.c +++ b/non-GPL/Events/EventsTrace/EventsTrace.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -109,6 +110,7 @@ uint64_t g_events_env = 0; uint64_t g_features_env = 0; bool g_print_features_init = 0; +bool g_features_printed = 0; bool g_unbuffer_stdout = 0; bool g_libbpf_verbose = 0; @@ -859,6 +861,10 @@ static void out_network_connection_closed_event(struct ebpf_net_event *evt) static int event_ctx_callback(struct ebpf_event_header *evt_hdr) { + if (g_print_features_init) + while (!g_features_printed) + usleep(100000); + switch (evt_hdr->type) { case EBPF_EVENT_PROCESS_FORK: out_process_fork((struct ebpf_process_fork_event *)evt_hdr); @@ -945,8 +951,10 @@ int main(int argc, char **argv) goto out; } - if (g_print_features_init) + if (g_print_features_init) { print_init_msg(ebpf_event_ctx__get_features(ctx)); + g_features_printed = 1; + } while (!exiting) { err = ebpf_event_ctx__next(ctx, 10); diff --git a/testing/test_bins/create_rename_delete_file.c b/testing/test_bins/create_rename_delete_file.c index 549ee881..f98a8418 100644 --- a/testing/test_bins/create_rename_delete_file.c +++ b/testing/test_bins/create_rename_delete_file.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "common.h" @@ -25,13 +26,34 @@ int main() filename_orig, filename_new); FILE *f; + // create CHECK(f = fopen(filename_orig, "w"), NULL); + // rename CHECK(rename(filename_orig, filename_new), -1); + + // modify(permissions) CHECK(chmod(filename_new, S_IRWXU | S_IRWXG | S_IRWXO), -1); - CHECK(fprintf(f, "test"), -1); - CHECK(ftruncate(fileno(f), 0), -1); + + int fd = fileno(f); + + // modify(content) + CHECK(write(fd, "test", 4), -1); + + // modify(content) + struct iovec iov[2]; + iov[0].iov_base = "test2"; + iov[0].iov_len = 5; + iov[1].iov_base = "test3"; + iov[1].iov_len = 5; + CHECK(writev(fd, iov, 2), -1); + + // modify(content) + CHECK(ftruncate(fd, 0), -1); + CHECK(fclose(f), EOF); + + // delete CHECK(unlink(filename_new), -1); return 0; diff --git a/testing/testrunner/tests.go b/testing/testrunner/tests.go index 71ab9476..a8a45710 100644 --- a/testing/testrunner/tests.go +++ b/testing/testrunner/tests.go @@ -215,7 +215,7 @@ func TestFileDelete(et *EventsTraceInstance) { AssertStringsEqual(fileDeleteEvent.Finfo.Type, "FILE") AssertUint64NotEqual(fileDeleteEvent.Finfo.Inode, 0) AssertUint64Equal(fileDeleteEvent.Finfo.Mode, 100777) - AssertUint64Equal(fileDeleteEvent.Finfo.Size, 4) + AssertUint64Equal(fileDeleteEvent.Finfo.Size, 0) AssertUint64Equal(fileDeleteEvent.Finfo.Uid, 0) AssertUint64Equal(fileDeleteEvent.Finfo.Gid, 0) } @@ -401,7 +401,7 @@ func TestFileModify(et *EventsTraceInstance) { TestFail("failed to unmarshal json", err) } - eventsCount := 3 // chmod, write, truncate + eventsCount := 4 // chmod, write, writev, truncate events := make([]FileModifyEvent, 0, eventsCount) for { var event FileModifyEvent @@ -427,12 +427,17 @@ func TestFileModify(et *EventsTraceInstance) { // write AssertStringsEqual(events[1].Path, binOutput.FileNameNew) AssertStringsEqual(events[1].ChangeType, "CONTENT") + AssertUint64Equal(events[1].Finfo.Size, 4) - // truncate + // writev AssertStringsEqual(events[2].Path, binOutput.FileNameNew) AssertStringsEqual(events[2].ChangeType, "CONTENT") + AssertUint64Equal(events[2].Finfo.Size, 4+5+5) - AssertTrue(events[1].Finfo.Size != events[2].Finfo.Size) + // truncate + AssertStringsEqual(events[3].Path, binOutput.FileNameNew) + AssertStringsEqual(events[3].ChangeType, "CONTENT") + AssertUint64Equal(events[3].Finfo.Size, 0) } func TestTtyWrite(et *EventsTraceInstance) {