Skip to content

ROX-33217: Instrument inode tracking on directory being created path mkdir#465

Merged
JoukoVirtanen merged 32 commits intomainfrom
jv-ROX-33217-instrument-inode-tracking-on-directory-being-created-path_mkdir
Apr 10, 2026
Merged

ROX-33217: Instrument inode tracking on directory being created path mkdir#465
JoukoVirtanen merged 32 commits intomainfrom
jv-ROX-33217-instrument-inode-tracking-on-directory-being-created-path_mkdir

Conversation

@JoukoVirtanen
Copy link
Copy Markdown
Contributor

@JoukoVirtanen JoukoVirtanen commented Apr 1, 2026

Description

Directory creation events need to be handled correctly. When a directory is created in a tracked directory its inode should be added to a hash set in kernel space. In user space an entry needs to be added into a map with the inode as the key and file path as the value.

An alternative approach can be found at #449

Checklist

  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
  • Added integration tests
  • Added regression tests

If any of these don't apply, please comment below.

Testing Performed

Checked metrics

$ grep path_mkdir */metrics 
test_mkdir_ignored/metrics:# HELP stackrox_fact_kernel_path_mkdir_events Events processed by the path_mkdir LSM hook.
test_mkdir_ignored/metrics:# TYPE stackrox_fact_kernel_path_mkdir_events counter
test_mkdir_ignored/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="RingbufferFull"} 0
test_mkdir_ignored/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Added"} 0
test_mkdir_ignored/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Error"} 0
test_mkdir_ignored/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Ignored"} 2
test_mkdir_ignored/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Total"} 3
test_mkdir_nested/metrics:# HELP stackrox_fact_kernel_path_mkdir_events Events processed by the path_mkdir LSM hook.
test_mkdir_nested/metrics:# TYPE stackrox_fact_kernel_path_mkdir_events counter
test_mkdir_nested/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="RingbufferFull"} 0
test_mkdir_nested/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Added"} 0
test_mkdir_nested/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Total"} 3
test_mkdir_nested/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Ignored"} 0
test_mkdir_nested/metrics:stackrox_fact_kernel_path_mkdir_events_total{label="Error"} 0
$ grep d_instantiate */metrics 
test_mkdir_ignored/metrics:# HELP stackrox_fact_kernel_d_instantiate_events Events processed by the d_instantiate LSM hook.
test_mkdir_ignored/metrics:# TYPE stackrox_fact_kernel_d_instantiate_events counter
test_mkdir_ignored/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Error"} 0
test_mkdir_ignored/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Added"} 2
test_mkdir_ignored/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Ignored"} 36
test_mkdir_ignored/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="RingbufferFull"} 0
test_mkdir_ignored/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Total"} 37
test_mkdir_nested/metrics:# HELP stackrox_fact_kernel_d_instantiate_events Events processed by the d_instantiate LSM hook.
test_mkdir_nested/metrics:# TYPE stackrox_fact_kernel_d_instantiate_events counter
test_mkdir_nested/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="RingbufferFull"} 0
test_mkdir_nested/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Added"} 6
test_mkdir_nested/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Error"} 0
test_mkdir_nested/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Total"} 115
test_mkdir_nested/metrics:stackrox_fact_kernel_d_instantiate_events_total{label="Ignored"} 112

@JoukoVirtanen JoukoVirtanen marked this pull request as ready for review April 1, 2026 23:30
@JoukoVirtanen JoukoVirtanen requested a review from a team as a code owner April 1, 2026 23:30
Comment on lines +309 to +314
umode_t mode = BPF_CORE_READ(inode, i_mode);
if (!S_ISDIR(mode)) {
bpf_map_delete_elem(&mkdir_context, &pid_tgid);
m->d_instantiate.ignored++;
return 0;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should check if m->d_instantiate.ignored actually increases, I have a feeling this condition should never be met.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

m->d_instantiate.ignored increases, but I don't know if it is due to this or other locations with m->d_instantiate.ignored++;.

@JoukoVirtanen JoukoVirtanen force-pushed the jv-ROX-33217-instrument-inode-tracking-on-directory-being-created-path_mkdir branch from 4a6a257 to b0ef4fe Compare April 6, 2026 15:39
@JoukoVirtanen
Copy link
Copy Markdown
Contributor Author

/retest

@JoukoVirtanen JoukoVirtanen requested a review from Molter73 April 8, 2026 21:57
Copy link
Copy Markdown
Contributor

@Molter73 Molter73 left a comment

Choose a reason for hiding this comment

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

Just a couple small comments, we should be good to merge soon.

Comment on lines +303 to +306
if (inode == NULL) {
m->d_instantiate.ignored++;
goto cleanup;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably should move this check a bit higher, there is no point in getting the context if there is no inode to work upon. You can still jump to cleanup, bpf_map_delete_elem will fail if there is no context to remove from the map, but we are already silently ignoring it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

process: Process,
file: FileData,
#[serde(skip)]
event_type: file_activity_type_t,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the point of this field? FileData already encodes this same information in its type, doesn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remove. I also went with your suggestion below.

self.metrics.events.dropped();
warn!("Failed to send event: {e}");
// Skip directory creation events - we track them internally but don't send to sensor
if event.event_type() != fact_ebpf::file_activity_type_t::DIR_ACTIVITY_CREATION {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, I see now why you added event_type, don't do this, add a MkDir variant to FileData and let the information be encoded in the enum. You will need to add a is_mkdir() method or something of the sort, but that's is more idiomatic anyways.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@JoukoVirtanen JoukoVirtanen force-pushed the jv-ROX-33217-instrument-inode-tracking-on-directory-being-created-path_mkdir branch from 3fd3840 to 0874869 Compare April 9, 2026 17:45
@JoukoVirtanen JoukoVirtanen requested a review from Molter73 April 10, 2026 04:50
Copy link
Copy Markdown
Contributor

@Molter73 Molter73 left a comment

Choose a reason for hiding this comment

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

LGTM! Just a few small comments I would like to see addressed, but they shouldn't block merging. Thanks for the effort on this one!

JoukoVirtanen and others added 4 commits April 10, 2026 07:47
@JoukoVirtanen JoukoVirtanen merged commit 6b5f77c into main Apr 10, 2026
31 of 33 checks passed
@JoukoVirtanen JoukoVirtanen deleted the jv-ROX-33217-instrument-inode-tracking-on-directory-being-created-path_mkdir branch April 10, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants