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

feat(events): add chmod_common event #4339

Merged
merged 2 commits into from
Oct 10, 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
34 changes: 34 additions & 0 deletions docs/docs/events/builtin/extra/chmod_common.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# chmod_common
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the name chmod_common is so clear to the user.
A better name for this event may be file_chmod


## Intro

chmod_common - An event capturing changes to access permissions of files and directories.

## Description

This event captures any changes to the current working directory (typically by using the `chmod` and similar syscalls).

## Arguments

* `pathname`:`const char*`[K] - path of the file or directory
* `mode`:`mode_t`[K] - the mode to apply to the file or directory

## Hooks

### chmod_common

#### Type

kprobe

#### Purpose

Catch access permissions changes of files and directories.

## Example Use Case

## Issues

## Related Events

`chmod`, `fchmod`, `fchmodat`
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ nav:
- security_path_notify: docs/events/builtin/extra/security_path_notify.md
- set_fs_pwd: docs/events/builtin/extra/set_fs_pwd.md
- tracee_info: docs/events/builtin/extra/tracee_info.md
- chmod_common: docs/events/builtin/extra/chmod_common.md
- Syscalls:
- Overview: docs/events/builtin/syscalls/index.md
- syscalls:
Expand Down
24 changes: 22 additions & 2 deletions pkg/ebpf/c/tracee.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2500,14 +2500,14 @@ int BPF_KPROBE(trace_debugfs_create_file)
return 0;

char *name = (char *) PT_REGS_PARM1(ctx);
mode_t mode = (unsigned short) PT_REGS_PARM2(ctx);
umode_t mode = (unsigned short) PT_REGS_PARM2(ctx);
struct dentry *dentry = (struct dentry *) PT_REGS_PARM3(ctx);
void *dentry_path = get_dentry_path_str(dentry);
unsigned long proc_ops_addr = (unsigned long) PT_REGS_PARM5(ctx);

save_str_to_buf(&p.event->args_buf, name, 0);
save_str_to_buf(&p.event->args_buf, dentry_path, 1);
save_to_submit_buf(&p.event->args_buf, &mode, sizeof(mode_t), 2);
save_to_submit_buf(&p.event->args_buf, &mode, sizeof(umode_t), 2);
save_to_submit_buf(&p.event->args_buf, (void *) &proc_ops_addr, sizeof(u64), 3);

return events_perf_submit(&p, 0);
Expand Down Expand Up @@ -5161,6 +5161,26 @@ int BPF_KPROBE(trace_security_settime64)
return events_perf_submit(&p, 0);
}

SEC("kprobe/chmod_common")
int BPF_KPROBE(trace_chmod_common)
{
program_data_t p = {};
if (!init_program_data(&p, ctx, CHMOD_COMMON))
return 0;

if (!evaluate_scope_filters(&p))
return 0;

struct path *path = (struct path *) PT_REGS_PARM1(ctx);
umode_t mode = PT_REGS_PARM2(ctx);
void *file_path = get_path_str(path);

save_str_to_buf(&p.event->args_buf, file_path, 0);
save_to_submit_buf(&p.event->args_buf, &mode, sizeof(umode_t), 1);

return events_perf_submit(&p, 0);
}

// clang-format off

// Network Packets (works from ~5.2 and beyond)
Expand Down
1 change: 1 addition & 0 deletions pkg/ebpf/c/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum event_id_e
PROCESS_EXECUTE_FAILED_INTERNAL,
SECURITY_TASK_SETRLIMIT,
SECURITY_SETTIME64,
CHMOD_COMMON,
MAX_EVENT_ID,
NO_EVENT_SUBMIT,

Expand Down
1 change: 1 addition & 0 deletions pkg/ebpf/probes/probe_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func NewDefaultProbeGroup(module *bpf.Module, netEnabled bool) (*ProbeGroup, err
Dup2Ret: NewTraceProbe(SyscallExit, "dup2", "trace_ret_dup2"),
Dup3: NewTraceProbe(SyscallEnter, "dup3", "trace_dup3"),
Dup3Ret: NewTraceProbe(SyscallExit, "dup3", "trace_ret_dup3"),
ChmodCommon: NewTraceProbe(KProbe, "chmod_common", "trace_chmod_common"),

TestUnavailableHook: NewTraceProbe(KProbe, "non_existing_func", "empty_kprobe"),
ExecTest: NewTraceProbe(RawTracepoint, "raw_syscalls:sched_process_exec", "tracepoint__exec_test"),
Expand Down
1 change: 1 addition & 0 deletions pkg/ebpf/probes/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const (
Dup2Ret
Dup3
Dup3Ret
ChmodCommon
)

// Test probe handles
Expand Down
42 changes: 30 additions & 12 deletions pkg/events/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const (
ProcessExecuteFailedInternal
SecurityTaskSetrlimit
SecuritySettime64
ChmodCommon
MaxCommonID
)

Expand Down Expand Up @@ -262,7 +263,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "int", Name: "flags"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NDStrahilevitz we need standardise names or migrate entirely to types instead of strings. Seeing this change and double checking 4737625#diff-561afb519199f76436d77252de43fdbc7020a8603bb0dd8226d8eea314c4ba32R29-R154, I realized that we can't be confident of the type size based on the name only, check these:

https://elixir.bootlin.com/linux/v6.11.1/source/include/linux/types.h#L23
https://elixir.bootlin.com/linux/v6.11.1/source/include/linux/types.h#L24

https://elixir.bootlin.com/linux/v6.11.1/source/arch/x86/include/uapi/asm/posix_types_32.h#L11
https://elixir.bootlin.com/linux/v6.11.1/source/include/uapi/asm-generic/posix_types.h#L24

unsigned short OR unsigned int.

I'll review all ArgZeroValueFromType() named types again and chose the larger option when necessary.

},
dependencies: Dependencies{
probes: []Probe{
Expand Down Expand Up @@ -2241,7 +2242,7 @@ var CoreEvents = map[ID]Definition{
sets: []string{"syscalls", "fs", "fs_dir_ops"},
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand Down Expand Up @@ -2288,7 +2289,7 @@ var CoreEvents = map[ID]Definition{
sets: []string{"default", "syscalls", "fs", "fs_file_ops"},
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand Down Expand Up @@ -2408,7 +2409,7 @@ var CoreEvents = map[ID]Definition{
sets: []string{"default", "syscalls", "fs", "fs_file_attr"},
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand All @@ -2432,7 +2433,7 @@ var CoreEvents = map[ID]Definition{
sets: []string{"default", "syscalls", "fs", "fs_file_attr"},
params: []trace.ArgMeta{
{Type: "int", Name: "fd"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand Down Expand Up @@ -3417,7 +3418,7 @@ var CoreEvents = map[ID]Definition{
sets: []string{"syscalls", "fs", "fs_file_ops"},
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
{Type: "dev_t", Name: "dev"},
},
dependencies: Dependencies{
Expand Down Expand Up @@ -5980,7 +5981,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "const char*", Name: "name"},
{Type: "int", Name: "oflag"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
{Type: "struct mq_attr*", Name: "attr"},
},
dependencies: Dependencies{
Expand Down Expand Up @@ -6411,7 +6412,7 @@ var CoreEvents = map[ID]Definition{
{Type: "int", Name: "dirfd"},
{Type: "const char*", Name: "pathname"},
{Type: "int", Name: "flags"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand All @@ -6436,7 +6437,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "int", Name: "dirfd"},
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
Expand All @@ -6461,7 +6462,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "int", Name: "dirfd"},
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
{Type: "dev_t", Name: "dev"},
},
dependencies: Dependencies{
Expand Down Expand Up @@ -6694,7 +6695,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "int", Name: "dirfd"},
{Type: "const char*", Name: "pathname"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
{Type: "int", Name: "flags"},
},
dependencies: Dependencies{
Expand Down Expand Up @@ -12065,7 +12066,7 @@ var CoreEvents = map[ID]Definition{
params: []trace.ArgMeta{
{Type: "const char*", Name: "file_name"},
{Type: "const char*", Name: "path"},
{Type: "mode_t", Name: "mode"},
{Type: "umode_t", Name: "mode"},
geyslan marked this conversation as resolved.
Show resolved Hide resolved
{Type: "void*", Name: "proc_ops_addr"},
},
},
Expand Down Expand Up @@ -13042,6 +13043,23 @@ var CoreEvents = map[ID]Definition{
{Type: "int", Name: "tz_dsttime"},
},
},
ChmodCommon: {
id: ChmodCommon,
id32Bit: Sys32Undefined,
name: "chmod_common",
version: NewVersion(1, 0, 0),
syscall: true,
sets: []string{},
params: []trace.ArgMeta{
{Type: "const char*", Name: "pathname"},
{Type: "umode_t", Name: "mode"},
},
dependencies: Dependencies{
probes: []Probe{
{handle: probes.ChmodCommon, required: true},
},
},
},
//
// Begin of Signal Events (Control Plane)
//
Expand Down
8 changes: 1 addition & 7 deletions pkg/events/parse_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,7 @@ func ParseArgs(event *trace.Event) error {
parseOpenFlagArgument(flagsArg, uint64(flags))
}
}
case Mknod, Mknodat, Chmod, Fchmod, Fchmodat:
if modeArg := GetArg(event, "mode"); modeArg != nil {
if mode, isUint32 := modeArg.Value.(uint32); isUint32 {
parseInodeMode(modeArg, uint64(mode))
}
}
case SecurityInodeMknod:
case Mknod, Mknodat, SecurityInodeMknod, Chmod, Fchmod, Fchmodat, ChmodCommon:
if modeArg := GetArg(event, "mode"); modeArg != nil {
if mode, isUint16 := modeArg.Value.(uint16); isUint16 {
parseInodeMode(modeArg, uint64(mode))
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/event_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ func Test_EventFilters(t *testing.T) {
expectEvent(anyHost, "fakeprog1", testutils.CPUForTests, anyPID, 0, events.Openat, orPolNames("comm-event-data-64"), orPolIDs(64),
expectArg("dirfd", int32(0)),
expectArg("flags", int32(0)),
expectArg("mode", uint32(0)),
expectArg("mode", uint16(0)),
),
},
[]string{},
Expand All @@ -1615,7 +1615,7 @@ func Test_EventFilters(t *testing.T) {
[]trace.Event{
expectEvent(anyHost, "fakeprog2", testutils.CPUForTests, anyPID, 0, events.Open, orPolNames("comm-event-data-42"), orPolIDs(42),
expectArg("flags", int32(0)),
expectArg("mode", uint32(0)),
expectArg("mode", uint16(0)),
),
},
[]string{},
Expand Down Expand Up @@ -1683,7 +1683,7 @@ func Test_EventFilters(t *testing.T) {
expectEvent(anyHost, "fakeprog1", testutils.CPUForTests, anyPID, 0, events.Openat, orPolNames("comm-event-retval-64"), orPolIDs(64),
expectArg("dirfd", int32(0)),
expectArg("flags", int32(0)),
expectArg("mode", uint32(0)),
expectArg("mode", uint16(0)),
),
},
[]string{},
Expand Down
Loading