Skip to content

Commit

Permalink
new(modern_bpf): add fchmod syscall
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <[email protected]>
  • Loading branch information
Andreagit97 committed Jul 29, 2022
1 parent a73a268 commit 18e3527
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions driver/modern_bpf/definitions/events_dimensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
#define CHROOT_E_SIZE HEADER_LEN
#define FCHDIR_E_SIZE HEADER_LEN + sizeof(int64_t) + PARAM_LEN
#define FCHDIR_X_SIZE HEADER_LEN + sizeof(int64_t) + PARAM_LEN
#define FCHMOD_E_SIZE HEADER_LEN
#define FCHMOD_X_SIZE HEADER_LEN + sizeof(int64_t) * 2 + sizeof(uint32_t) + PARAM_LEN * 3

#endif /* __EVENT_DIMENSIONS_H__ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2022 The Falco Authors.
*
* This file is dual licensed under either the MIT or GPL 2. See MIT.txt
* or GPL2.txt for full copies of the license.
*/

#include <helpers/interfaces/fixed_size_event.h>

/*=============================== ENTER EVENT ===========================*/

SEC("tp_btf/sys_enter")
int BPF_PROG(fchmod_e,
struct pt_regs *regs,
long id)
{
struct ringbuf_struct ringbuf;
if(!ringbuf__reserve_space(&ringbuf, FCHMOD_E_SIZE))
{
return 0;
}

ringbuf__store_event_header(&ringbuf, PPME_SYSCALL_FCHMOD_E, FCHMOD_E_SIZE);

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

// Here we have no parameters to collect.

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

ringbuf__submit_event(&ringbuf);

return 0;
}

/*=============================== ENTER EVENT ===========================*/

/*=============================== EXIT EVENT ===========================*/

SEC("tp_btf/sys_exit")
int BPF_PROG(fchmod_x,
struct pt_regs *regs,
long ret)
{

struct ringbuf_struct ringbuf;
if(!ringbuf__reserve_space(&ringbuf, FCHMOD_X_SIZE))
{
return 0;
}

ringbuf__store_event_header(&ringbuf, PPME_SYSCALL_FCHMOD_X, FCHMOD_X_SIZE);

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

/* Parameter 1: res (type: PT_ERRNO) */
ringbuf__store_s64(&ringbuf, ret);

/* Parameter 2: fd (type: PT_FD) */
s32 fd = (s32)extract__syscall_argument(regs, 0);
ringbuf__store_s64(&ringbuf, (s64)fd);

/* Parameter 3: mode (type: PT_MODE) */
unsigned long mode = extract__syscall_argument(regs, 1);
ringbuf__store_u32(&ringbuf, chmod_mode_to_scap(mode));

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

ringbuf__submit_event(&ringbuf);

return 0;
}

/*=============================== EXIT EVENT ===========================*/
39 changes: 39 additions & 0 deletions test/modern_bpf/test_suites/syscall_enter_suite/fchmod_e.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../../event_class/event_class.h"

#ifdef __NR_fchmod
TEST(SyscallEnter, fchmodE)
{
auto evt_test = new event_test(__NR_fchmod, ENTER_EVENT);

evt_test->enable_capture();

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

int32_t mock_fd = -1;
uint32_t mode = 0;
assert_syscall_state(SYSCALL_FAILURE, "fchmod", syscall(__NR_fchmod, mock_fd, mode));

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

evt_test->disable_capture();

evt_test->assert_event_presence();

if(HasFatalFailure())
{
return;
}

evt_test->parse_event();

evt_test->assert_header();

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

// Here we have no parameters to assert.

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

evt_test->assert_num_params_pushed(0);
}
#endif
47 changes: 47 additions & 0 deletions test/modern_bpf/test_suites/syscall_exit_suite/fchmod_x.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "../../event_class/event_class.h"

#ifdef __NR_fchmod
TEST(SyscallExit, fchmodX)
{
auto evt_test = new event_test(__NR_fchmod, EXIT_EVENT);

evt_test->enable_capture();

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

int32_t mock_fd = -1;
uint32_t mode = S_IXUSR;
assert_syscall_state(SYSCALL_FAILURE, "fchmod", syscall(__NR_fchmod, mock_fd, mode));
int64_t errno_value = -errno;

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

evt_test->disable_capture();

evt_test->assert_event_presence();

if(HasFatalFailure())
{
return;
}

evt_test->parse_event();

evt_test->assert_header();

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

/* Parameter 1: res (type: PT_ERRNO) */
evt_test->assert_numeric_param(1, (int64_t)errno_value);

/* Parameter 2: fd (type: PT_FD) */
evt_test->assert_numeric_param(2, (int64_t)mock_fd);

/* Parameter 3: mode (type: PT_MODE) */
evt_test->assert_numeric_param(3, (uint32_t)PPM_S_IXUSR);

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

evt_test->assert_num_params_pushed(3);
}
#endif
2 changes: 2 additions & 0 deletions userspace/libpman/src/events_prog_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static const char* event_prog_names[PPM_EVENT_MAX] = {
[PPME_SYSCALL_CHROOT_X] = "chroot_x",
[PPME_SYSCALL_FCHDIR_E] = "fchdir_e",
[PPME_SYSCALL_FCHDIR_X] = "fchdir_x",
[PPME_SYSCALL_FCHMOD_E] = "fchmod_e",
[PPME_SYSCALL_FCHMOD_X] = "fchmod_x",
};

/* Some events can require more than one bpf program to collect all the data. */
Expand Down

0 comments on commit 18e3527

Please sign in to comment.