Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(driver): collect dev and inode in all open-family syscalls #1755

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion driver/SCHEMA_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.18.2
2.19.0
42 changes: 39 additions & 3 deletions driver/bpf/fillers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,8 @@ FILLER(sys_openat2_x, true)
uint32_t flags;
unsigned long val;
uint32_t mode;
unsigned long dev = 0;
unsigned long ino = 0;
long retval;
int32_t fd;
int res;
Expand Down Expand Up @@ -3329,7 +3331,21 @@ FILLER(sys_openat2_x, true)
* resolve (extracted from open_how structure)
* Note that we convert them into the ppm portable representation before pushing them to the ring
*/
return bpf_push_u32_to_ring(data, resolve);
res = bpf_push_u32_to_ring(data, resolve);
CHECK_RES(res);

bpf_get_fd_dev_ino(retval, &dev, &ino);

/*
* dev
*/
res = bpf_push_u32_to_ring(data, dev);
CHECK_RES(res);

/*
* ino
*/
return bpf_push_u64_to_ring(data, ino);
}

FILLER(sys_open_by_handle_at_x, true)
Expand Down Expand Up @@ -3367,7 +3383,15 @@ FILLER(sys_open_by_handle_at_x, true)
}

/* Parameter 4: path (type: PT_FSPATH) */
return bpf_push_empty_param(data);
res = bpf_push_empty_param(data);
CHECK_RES(res);

/* Parameter 5: dev (type: PT_UINT32) */
res = bpf_push_u32_to_ring(data, 0);
CHECK_RES(res);

/* Parameter 6: ino (type: PT_UINT64) */
return bpf_push_u64_to_ring(data, 0);
}

FILLER(open_by_handle_at_x_extra_tail_1, true)
Expand All @@ -3388,7 +3412,19 @@ FILLER(open_by_handle_at_x_extra_tail_1, true)

/* Parameter 4: path (type: PT_FSPATH) */
char* filepath = bpf_d_path_approx(data, &(f->f_path));
return bpf_val_to_ring_mem(data,(unsigned long)filepath, KERNEL);
int res = bpf_val_to_ring_mem(data,(unsigned long)filepath, KERNEL);

unsigned long dev = 0;
unsigned long ino = 0;

bpf_get_fd_dev_ino(retval, &dev, &ino);

/* Parameter 5: dev (type: PT_UINT32) */
res = bpf_push_u32_to_ring(data, dev);
CHECK_RES(res);

/* Parameter 6: ino (type: PT_UINT64) */
return bpf_push_u64_to_ring(data, ino);
}

FILLER(sys_io_uring_setup_x, true)
Expand Down
4 changes: 2 additions & 2 deletions driver/event_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ const struct ppm_event_info g_event_info[] = {
[PPME_CONTAINER_JSON_2_E] = {"container", EC_PROCESS | EC_METAEVENT, EF_MODIFIES_STATE | EF_LARGE_PAYLOAD, 1, {{"json", PT_CHARBUF, PF_NA} } }, /// TODO: do we need SKIPPARSERESET flag?
[PPME_CONTAINER_JSON_2_X] = {"NA", EC_UNKNOWN, EF_UNUSED, 0},
[PPME_SYSCALL_OPENAT2_E] = {"openat2", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 5, {{"dirfd", PT_FD, PF_DEC}, {"name", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT}, {"resolve", PT_FLAGS32, PF_HEX, openat2_flags} } },
[PPME_SYSCALL_OPENAT2_X] = {"openat2", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 6, {{"fd", PT_FD, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"name", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT}, {"resolve", PT_FLAGS32, PF_HEX, openat2_flags} } },
[PPME_SYSCALL_OPENAT2_X] = {"openat2", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 8, {{"fd", PT_FD, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"name", PT_FSRELPATH, PF_NA, DIRFD_PARAM(1)}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"mode", PT_UINT32, PF_OCT}, {"resolve", PT_FLAGS32, PF_HEX, openat2_flags}, {"dev", PT_UINT32, PF_HEX}, {"ino", PT_UINT64, PF_DEC} } },
[PPME_SYSCALL_MPROTECT_E] = {"mprotect", EC_MEMORY | EC_SYSCALL, EF_NONE, 3, {{"addr", PT_UINT64, PF_HEX}, {"length", PT_UINT64, PF_DEC}, {"prot", PT_FLAGS32, PF_HEX, prot_flags} } },
[PPME_SYSCALL_MPROTECT_X] = {"mprotect", EC_MEMORY | EC_SYSCALL, EF_NONE, 1, {{"res", PT_ERRNO, PF_DEC} } },
[PPME_SYSCALL_EXECVEAT_E] = {"execveat", EC_PROCESS | EC_SYSCALL, EF_MODIFIES_STATE, 3, {{"dirfd", PT_FD, PF_DEC}, {"pathname", PT_FSRELPATH, PF_NA, DIRFD_PARAM(0)}, {"flags", PT_FLAGS32, PF_HEX, execveat_flags} } },
Expand All @@ -389,7 +389,7 @@ const struct ppm_event_info g_event_info[] = {
[PPME_SYSCALL_CLONE3_E] = {"clone3", EC_PROCESS | EC_SYSCALL, EF_MODIFIES_STATE, 0},
[PPME_SYSCALL_CLONE3_X] = {"clone3", EC_PROCESS | EC_SYSCALL, EF_MODIFIES_STATE, 21, {{"res", PT_PID, PF_DEC}, {"exe", PT_CHARBUF, PF_NA}, {"args", PT_BYTEBUF, PF_NA}, {"tid", PT_PID, PF_DEC}, {"pid", PT_PID, PF_DEC}, {"ptid", PT_PID, PF_DEC}, {"cwd", PT_CHARBUF, PF_NA}, {"fdlimit", PT_INT64, PF_DEC}, {"pgft_maj", PT_UINT64, PF_DEC}, {"pgft_min", PT_UINT64, PF_DEC}, {"vm_size", PT_UINT32, PF_DEC}, {"vm_rss", PT_UINT32, PF_DEC}, {"vm_swap", PT_UINT32, PF_DEC}, {"comm", PT_CHARBUF, PF_NA}, {"cgroups", PT_BYTEBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, clone_flags}, {"uid", PT_UINT32, PF_DEC}, {"gid", PT_UINT32, PF_DEC}, {"vtid", PT_PID, PF_DEC}, {"vpid", PT_PID, PF_DEC}, {"pidns_init_start_ts", PT_UINT64, PF_DEC} } },
[PPME_SYSCALL_OPEN_BY_HANDLE_AT_E] = {"open_by_handle_at", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 0},
[PPME_SYSCALL_OPEN_BY_HANDLE_AT_X] = {"open_by_handle_at", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 4, {{"fd", PT_FD, PF_DEC}, {"mountfd", PT_FD, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"path", PT_FSPATH, PF_NA} } },
[PPME_SYSCALL_OPEN_BY_HANDLE_AT_X] = {"open_by_handle_at", EC_FILE | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 6, {{"fd", PT_FD, PF_DEC}, {"mountfd", PT_FD, PF_DEC}, {"flags", PT_FLAGS32, PF_HEX, file_flags}, {"path", PT_FSPATH, PF_NA}, {"dev", PT_UINT32, PF_HEX}, {"ino", PT_UINT64, PF_DEC} } },
[PPME_SYSCALL_IO_URING_SETUP_E] = {"io_uring_setup", EC_IO_OTHER | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 0},
[PPME_SYSCALL_IO_URING_SETUP_X] = {"io_uring_setup", EC_IO_OTHER | EC_SYSCALL, EF_CREATES_FD | EF_MODIFIES_STATE, 8, {{"res", PT_ERRNO, PF_DEC}, {"entries", PT_UINT32, PF_DEC}, {"sq_entries", PT_UINT32, PF_DEC},{"cq_entries", PT_UINT32, PF_DEC},{"flags", PT_FLAGS32, PF_HEX, io_uring_setup_flags},{"sq_thread_cpu", PT_UINT32, PF_DEC}, {"sq_thread_idle", PT_UINT32, PF_DEC},{"features", PT_FLAGS32, PF_HEX, io_uring_setup_feats}}},
[PPME_SYSCALL_IO_URING_ENTER_E] = {"io_uring_enter", EC_IO_OTHER | EC_SYSCALL, EF_NONE, 0},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ int BPF_PROG(open_by_handle_at_x,
SEC("tp_btf/sys_exit")
int BPF_PROG(t1_open_by_handle_at_x, struct pt_regs *regs, long ret)
{
dev_t dev = 0;
uint64_t ino = 0;

struct auxiliary_map *auxmap = auxmap__get();
if(!auxmap)
{
Expand All @@ -90,10 +93,10 @@ int BPF_PROG(t1_open_by_handle_at_x, struct pt_regs *regs, long ret)

/*=============================== COLLECT PARAMETERS ===========================*/

/* Parameter 4: path (type: PT_FSPATH) */
/* We collect the file path from the file descriptor only if it is valid */
/* We collect the file path, dev and ino from the file descriptor only if it is valid */
if(ret > 0)
{
/* Parameter 4: path (type: PT_FSPATH) */
struct file *f = extract__file_struct_from_fd(ret);
if(f != NULL)
{
Expand All @@ -103,12 +106,20 @@ int BPF_PROG(t1_open_by_handle_at_x, struct pt_regs *regs, long ret)
{
auxmap__store_empty_param(auxmap);
}

extract__dev_and_ino_from_fd(ret, &dev, &ino);
}
else
{
auxmap__store_empty_param(auxmap);
}

/* Parameter 5: dev (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, dev);

/* Parameter 6: ino (type: PT_UINT64) */
auxmap__store_u64_param(auxmap, ino);

/*=============================== COLLECT PARAMETERS ===========================*/

auxmap__finalize_event_header(auxmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ int BPF_PROG(openat2_x,
/* Parameter 6: resolve (type: PT_FLAGS32) */
auxmap__store_u32_param(auxmap, openat2_resolve_to_scap(how.resolve));

dev_t dev = 0;
uint64_t ino = 0;

if(ret > 0)
{
extract__dev_and_ino_from_fd(ret, &dev, &ino);
}

/* Parameter 7: dev (type: PT_UINT32) */
auxmap__store_u32_param(auxmap, dev);

/* Parameter 8: ino (type: PT_UINT64) */
auxmap__store_u64_param(auxmap, ino);

/*=============================== COLLECT PARAMETERS ===========================*/

auxmap__finalize_event_header(auxmap);
Expand Down
28 changes: 28 additions & 0 deletions driver/ppm_fillers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4960,6 +4960,8 @@ int f_sys_openat2_x(struct event_filler_arguments *args)
unsigned long flags;
unsigned long val;
unsigned long mode;
uint32_t dev = 0;
uint64_t ino = 0;
int res;
int32_t fd;
int64_t retval;
Expand Down Expand Up @@ -5030,6 +5032,20 @@ int f_sys_openat2_x(struct event_filler_arguments *args)
res = val_to_ring(args, resolve, 0, true, 0);
CHECK_RES(res);

get_fd_dev_ino(retval, &dev, &ino);

/*
* dev
*/
res = val_to_ring(args, dev, 0, false, 0);
CHECK_RES(res);

/*
* ino
*/
res = val_to_ring(args, ino, 0, false, 0);
CHECK_RES(res);

return add_sentinel(args);
}

Expand Down Expand Up @@ -5096,6 +5112,8 @@ int f_sys_open_by_handle_at_x(struct event_filler_arguments *args)
unsigned long val = 0;
unsigned long flags = 0;
int res = 0;
uint32_t dev = 0;
uint64_t ino = 0;
long retval = 0;
char *pathname = NULL;
int32_t mountfd = 0;
Expand Down Expand Up @@ -5146,6 +5164,16 @@ int f_sys_open_by_handle_at_x(struct event_filler_arguments *args)
res = val_to_ring(args, (unsigned long)pathname, 0, false, 0);
CHECK_RES(res);

get_fd_dev_ino(retval, &dev, &ino);

/* Parameter 5: dev (type: PT_UINT32) */
res = val_to_ring(args, dev, 0, false, 0);
CHECK_RES(res);

/* Parameter 6: ino (type: PT_UINT64) */
res = val_to_ring(args, ino, 0, false, 0);
CHECK_RES(res);

return add_sentinel(args);
}

Expand Down
2 changes: 1 addition & 1 deletion test/drivers/test_suites/actions_suite/ia32.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ TEST(Actions, ia32_openat2_x)

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(6);
evt_test->assert_num_params_pushed(8);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#define MAX_FSPATH_LEN 4096

void do___open_by_handle_atX_success(int *open_by_handle_fd, int *dirfd, char *fspath, int use_mountpoint)
void do___open_by_handle_atX_success(int *open_by_handle_fd, int *dirfd, char *fspath, uint32_t *dev, uint64_t *inode, int use_mountpoint)
{
/*
* 0. Create (temporary) mount point (if use_mountpoint).
Expand Down Expand Up @@ -100,7 +100,15 @@ void do___open_by_handle_atX_success(int *open_by_handle_fd, int *dirfd, char *f
}

/*
* 6. Cleaning phase.
* 6. Get dev and ino.
*/
struct stat file_stat;
assert_syscall_state(SYSCALL_SUCCESS, "fstat", syscall(__NR_fstat, *open_by_handle_fd, &file_stat), NOT_EQUAL, -1);
*dev = (uint32_t)file_stat.st_dev;
*inode = file_stat.st_ino;

/*
* 7. Cleaning phase.
*/
close(*open_by_handle_fd);
free(fhp);
Expand Down Expand Up @@ -147,8 +155,10 @@ TEST(SyscallExit, open_by_handle_atX_success)
int open_by_handle_fd;
int dirfd;
char fspath[MAX_FSPATH_LEN];
do___open_by_handle_atX_success(&open_by_handle_fd, &dirfd, fspath, 0);

uint32_t dev;
uint64_t inode;
do___open_by_handle_atX_success(&open_by_handle_fd, &dirfd, fspath, &dev, &inode, 0);

/*=============================== TRIGGER SYSCALL ===========================*/

evt_test->disable_capture();
Expand Down Expand Up @@ -177,9 +187,15 @@ TEST(SyscallExit, open_by_handle_atX_success)
/* Parameter 4: path (type: PT_FSPATH) */
evt_test->assert_charbuf_param(4, fspath);

/* Parameter 5: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(5, dev);

/* Parameter 6: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(6, inode);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(4);
evt_test->assert_num_params_pushed(6);

}

Expand All @@ -194,7 +210,9 @@ TEST(SyscallExit, open_by_handle_atX_success_mp)
int open_by_handle_fd;
int dirfd;
char fspath[MAX_FSPATH_LEN];
do___open_by_handle_atX_success(&open_by_handle_fd, &dirfd, fspath, 1);
uint32_t dev;
uint64_t inode;
do___open_by_handle_atX_success(&open_by_handle_fd, &dirfd, fspath, &dev, &inode, 1);

/*=============================== TRIGGER SYSCALL ===========================*/

Expand Down Expand Up @@ -225,9 +243,15 @@ TEST(SyscallExit, open_by_handle_atX_success_mp)
/* Parameter 4: path (type: PT_FSPATH) */
evt_test->assert_charbuf_param(4, fspath);

/* Parameter 5: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(5, dev);

/* Parameter 6: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(6, inode);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(4);
evt_test->assert_num_params_pushed(6);
}

TEST(SyscallExit, open_by_handle_atX_failure)
Expand Down Expand Up @@ -281,8 +305,14 @@ TEST(SyscallExit, open_by_handle_atX_failure)
/* Parameter 4: path (type: PT_FSPATH) */
evt_test->assert_empty_param(4);

/* Parameter 5: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(5, (uint32_t)0);

/* Parameter 6: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(6, (uint64_t)0);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(4);
evt_test->assert_num_params_pushed(6);
}
#endif
38 changes: 35 additions & 3 deletions test/drivers/test_suites/syscall_exit_suite/openat2_x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ TEST(SyscallExit, openat2X_success)
how.resolve = RESOLVE_BENEATH | RESOLVE_NO_MAGICLINKS;
int32_t fd = syscall(__NR_openat2, dirfd, pathname, &how, sizeof(struct open_how));
assert_syscall_state(SYSCALL_SUCCESS, "openat2", fd, NOT_EQUAL, -1);

/* Call `fstat` to retrieve the `dev` and `ino`. */
struct stat file_stat;
assert_syscall_state(SYSCALL_SUCCESS, "fstat", syscall(__NR_fstat, fd, &file_stat), NOT_EQUAL, -1);
uint32_t dev = (uint32_t)file_stat.st_dev;
uint64_t inode = file_stat.st_ino;
close(fd);

/*=============================== TRIGGER SYSCALL ===========================*/
Expand Down Expand Up @@ -60,9 +66,15 @@ TEST(SyscallExit, openat2X_success)
/* Parameter 6: resolve (type: PT_FLAGS32) */
evt_test->assert_numeric_param(6, (uint32_t)PPM_RESOLVE_BENEATH | PPM_RESOLVE_NO_MAGICLINKS);

/* Parameter 7: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(7, dev);

/* Parameter 8: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(8, inode);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(6);
evt_test->assert_num_params_pushed(8);
}

TEST(SyscallExit, openat2X_failure)
Expand Down Expand Up @@ -122,9 +134,17 @@ TEST(SyscallExit, openat2X_failure)
/* Parameter 6: resolve (type: PT_FLAGS32) */
evt_test->assert_numeric_param(6, (uint32_t)PPM_RESOLVE_BENEATH | PPM_RESOLVE_NO_MAGICLINKS);

/* Syscall fails so dev=0 && ino=0. */

/* Parameter 7: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(7, (uint32_t)0);

/* Parameter 8: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(8, (uint64_t)0);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(6);
evt_test->assert_num_params_pushed(8);
}

TEST(SyscallExit, openat2X_create_success)
Expand All @@ -144,6 +164,12 @@ TEST(SyscallExit, openat2X_create_success)
syscall(__NR_unlinkat, AT_FDCWD, pathname, 0); /* remove file before creating it */
int32_t fd = syscall(__NR_openat2, dirfd, pathname, &how, sizeof(struct open_how));
assert_syscall_state(SYSCALL_SUCCESS, "openat2", fd, NOT_EQUAL, -1);

/* Call `fstat` to retrieve the `dev` and `ino`. */
struct stat file_stat;
assert_syscall_state(SYSCALL_SUCCESS, "fstat", syscall(__NR_fstat, fd, &file_stat), NOT_EQUAL, -1);
uint32_t dev = (uint32_t)file_stat.st_dev;
uint64_t inode = file_stat.st_ino;
close(fd);

/*=============================== TRIGGER SYSCALL ===========================*/
Expand Down Expand Up @@ -181,8 +207,14 @@ TEST(SyscallExit, openat2X_create_success)
/* Parameter 6: resolve (type: PT_FLAGS32) */
evt_test->assert_numeric_param(6, (uint32_t)PPM_RESOLVE_BENEATH | PPM_RESOLVE_NO_MAGICLINKS);

/* Parameter 7: dev (type: PT_UINT32) */
evt_test->assert_numeric_param(7, dev);

/* Parameter 8: ino (type: PT_UINT64) */
evt_test->assert_numeric_param(8, inode);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(6);
evt_test->assert_num_params_pushed(8);
}
#endif
Loading
Loading