Skip to content

Commit 9619039

Browse files
Merge pull request #7 from SentinalFS/feat/fullFilePath
feat: Preparation for getting full path in user space
2 parents 4aaa67f + 6ba82f2 commit 9619039

8 files changed

Lines changed: 178 additions & 68 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"data_types.h": "c",
2222
"chrono": "c",
2323
"text_encoding": "c",
24-
"typeinfo": "c"
24+
"typeinfo": "c",
25+
"utils.h": "c"
2526
}
2627
}

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "helpers/read_write.h"
77
#include "helpers/rename.h"
88
#include "helpers/delete.h"
9+
#include "utils/utils.h"
910
// Common includes and definitions can stay here
1011

1112
#endif /* COMMON_H */

data_types.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "headers.h"
55

6+
// Lets match parent/filename & inode with data in crds
7+
68
struct {
79
__uint(type, BPF_MAP_TYPE_RINGBUF);
810
__uint(max_entries, 512 * 1024);
@@ -13,24 +15,12 @@ struct {
1315
__uint(max_entries, 256 * 1024);
1416
} rename_events SEC(".maps");
1517

16-
struct
17-
{
18-
__uint(type, BPF_MAP_TYPE_HASH);
19-
__type(key, struct inode_key);
20-
__type(value, u32);
21-
__uint(max_entries, 256);
22-
} monitored_inodes SEC(".maps");
23-
24-
25-
struct inode_key
26-
{
27-
u32 inode;
28-
};
29-
3018
struct data_t
3119
{
3220
u32 pid;
3321
u32 uid;
22+
u64 inode;
23+
char parent_filename[FILE_NAME_SIZE];
3424
char filename[FILE_NAME_SIZE];
3525
char comm[COMM_SIZE];
3626
u64 timestamp;
@@ -42,6 +32,10 @@ struct rename_data_t
4232
{
4333
u32 pid;
4434
u32 uid;
35+
u32 inode_old;
36+
u32 inode_new;
37+
char new_parent_filename[FILE_NAME_SIZE];
38+
char old_parent_filename[FILE_NAME_SIZE];
4539
char old_filename[FILE_NAME_SIZE];
4640
char new_filename[FILE_NAME_SIZE];
4741
char comm[COMM_SIZE];

headers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include <bpf/bpf_tracing.h>
88
#include <bpf/bpf_endian.h>
99

10-
#define FILE_NAME_SIZE 144
10+
#define FILE_NAME_SIZE 176
1111
#define OTYPE_SIZE 16
1212
#define COMM_SIZE 32
1313
#define DCACHE_NEGATIVE_DENTRY 0x0020
14+
#define NEGATIVE_INODE_NUMBER -1
1415

1516
#endif

helpers/delete.h

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,82 @@
33

44
#include "../headers.h"
55
#include "../data_types.h"
6+
#include "../utils/utils.h"
67

78
static __always_inline int trace_file_delete(struct pt_regs *ctx, struct dentry *de)
89
{
910
if (!de)
1011
{
11-
bpf_trace_printk("trace_file_delete: dentry is NULL\n", sizeof("trace_file_delete: dentry is NULL\n"));
1212
return 0;
1313
}
1414

1515
struct data_t *data = bpf_ringbuf_reserve(&events, sizeof(struct data_t), 0);
1616
if (!data)
1717
{
18-
bpf_trace_printk("trace_file_delete: failed to reserve ringbuf\n", sizeof("trace_file_delete: failed to reserve ringbuf\n"));
1918
return 0;
2019
}
2120
__builtin_memset(data, 0, sizeof(*data));
2221

22+
// specify the operation type
23+
char OPRN[] = "DELETE";
24+
25+
// Read filename from dentry
2326
struct qstr d_name_qstr = {};
2427
bpf_core_read(&d_name_qstr, sizeof(d_name_qstr), &de->d_name);
25-
2628
if (d_name_qstr.len <= 0 || !d_name_qstr.name || d_name_qstr.name == (void *)-1UL || d_name_qstr.name == NULL)
2729
{
28-
bpf_ringbuf_discard(data, 0);
29-
return 0;
30+
DISCARD_AND_RETURN(data);
3031
}
3132

33+
// Get parent dentry
34+
struct dentry *parent_de = NULL;
35+
bpf_core_read(&parent_de, sizeof(parent_de), &de->d_parent);
36+
if (!parent_de)
37+
{
38+
DISCARD_AND_RETURN(data);
39+
}
40+
41+
// Read parent filename from parent dentry
42+
struct qstr parent_d_name_qstr = {};
43+
bpf_core_read(&parent_d_name_qstr, sizeof(parent_d_name_qstr), &parent_de->d_name);
44+
if (parent_d_name_qstr.len <= 0 || !parent_d_name_qstr.name || parent_d_name_qstr.name == (void *)-1UL || parent_d_name_qstr.name == NULL)
45+
{
46+
DISCARD_AND_RETURN(data);
47+
}
48+
49+
// Check for negative dentries
50+
unsigned char d_parent_flags = 0;
3251
unsigned char d_flags = 0;
52+
bpf_core_read(&d_parent_flags, sizeof(d_parent_flags), &parent_de->d_flags);
3353
bpf_core_read(&d_flags, sizeof(d_flags), &de->d_flags);
34-
if (d_flags & DCACHE_NEGATIVE_DENTRY) {
35-
bpf_trace_printk("trace_file_delete: negative dentry detected (file likely non-existent). Discarding.\n", sizeof("trace_file_delete: negative dentry detected (file likely non-existent). Discarding.\n"));
36-
bpf_ringbuf_discard(data, 0);
37-
return 0;
54+
if ((d_parent_flags & DCACHE_NEGATIVE_DENTRY) || (d_flags & DCACHE_NEGATIVE_DENTRY)) {
55+
bpf_trace_printk("Negative dentry detected: parent flags: %u, dentry flags: %u\n", d_parent_flags, d_flags);
56+
DISCARD_AND_RETURN(data);
3857
}
3958

40-
char fname[FILE_NAME_SIZE] = {};
41-
bpf_core_read_str(fname, sizeof(fname), d_name_qstr.name);
59+
// Get the inode number
60+
struct inode *inode_ptr = NULL;
61+
bpf_core_read(&inode_ptr, sizeof(inode_ptr), &de->d_inode);
62+
if (inode_ptr)
63+
bpf_core_read(&data->inode, sizeof(data->inode), &inode_ptr->i_ino);
64+
else
65+
data->inode = NEGATIVE_INODE_NUMBER;
4266

43-
char OPRN[] = "DELETE";
44-
__builtin_memcpy(data->filename, fname, sizeof(data->filename));
45-
__builtin_memcpy(data->otype, OPRN, sizeof(data->otype));
67+
// Get cgroup ID
68+
int cgroup_id = bpf_get_current_cgroup_id();
69+
70+
// Copy data into the data structure
4671
data->pid = bpf_get_current_pid_tgid() >> 32;
4772
data->uid = bpf_get_current_uid_gid();
4873
data->timestamp = bpf_ktime_get_ns();
49-
50-
bpf_get_current_comm(&data->comm, sizeof(data->comm));
51-
52-
int cgroup_id = bpf_get_current_cgroup_id();
5374
data->cgroup_id = cgroup_id;
5475

76+
bpf_core_read_str(data->filename, sizeof(data->filename), d_name_qstr.name);
77+
bpf_core_read_str(data->parent_filename, sizeof(data->parent_filename), parent_d_name_qstr.name);
78+
__builtin_memcpy(data->otype, OPRN, sizeof(data->otype));
79+
80+
bpf_get_current_comm(&data->comm, sizeof(data->comm));
81+
5582
bpf_ringbuf_submit(data, 0);
5683
return 0;
5784
}

helpers/read_write.h

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33

44
#include "../headers.h"
55
#include "../data_types.h"
6+
#include "../utils/utils.h"
67

78
static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file *file, const char *operation)
89
{
910
if (!file)
1011
return 0;
1112

13+
// Declare the ring buffer for events
1214
struct data_t *data = bpf_ringbuf_reserve(&events, sizeof(struct data_t), 0);
1315
if (!data)
1416
{
1517
return 0;
1618
}
1719
__builtin_memset(data, 0, sizeof(*data));
1820

21+
// Check if the file pointer is valid, get the dentry from the file structure
1922
struct dentry *de = NULL;
2023
bpf_core_read(&de, sizeof(de), &file->f_path.dentry);
2124
if (!de)
@@ -24,6 +27,7 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
2427
return 0;
2528
}
2629

30+
// Get filename
2731
struct qstr d_name = {};
2832
bpf_core_read(&d_name, sizeof(d_name), &de->d_name);
2933
if (d_name.len == 0)
@@ -32,17 +36,25 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
3236
return 0;
3337
}
3438

35-
char fname[FILE_NAME_SIZE] = {};
36-
bpf_core_read_str(fname, sizeof(fname), d_name.name);
37-
38-
data->pid = bpf_get_current_pid_tgid() >> 32;
39-
data->uid = bpf_get_current_uid_gid();
40-
data->timestamp = bpf_ktime_get_ns();
39+
// Get parent dentry
40+
struct dentry *parent_de = NULL;
41+
bpf_core_read(&parent_de, sizeof(parent_de), &de->d_parent);
42+
if (!parent_de)
43+
{
44+
bpf_ringbuf_discard(data, 0);
45+
return 0;
46+
}
4147

42-
__builtin_memcpy(data->filename, fname, sizeof(data->filename));
43-
__builtin_memcpy(data->otype, operation, sizeof(data->otype));
44-
bpf_get_current_comm(&data->comm, sizeof(data->comm));
48+
// Get parent filename
49+
struct qstr parent_d_name = {};
50+
bpf_core_read(&parent_d_name, sizeof(parent_d_name), &parent_de->d_name);
51+
if (parent_d_name.len == 0)
52+
{
53+
bpf_ringbuf_discard(data, 0);
54+
return 0;
55+
}
4556

57+
// Get Inode from file structure
4658
struct inode *inode_ptr = NULL;
4759
bpf_core_read(&inode_ptr, sizeof(inode_ptr), &file->f_inode);
4860
if (!inode_ptr)
@@ -51,23 +63,24 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
5163
return 0;
5264
}
5365

54-
u32 inode_num = 0;
55-
bpf_core_read(&inode_num, sizeof(inode_num), &inode_ptr->i_ino);
66+
// Get the current cgroup ID
67+
int cgroup_id = bpf_get_current_cgroup_id();
5668

57-
struct inode_key key = {.inode = inode_num};
69+
// Copy the data into the event structure
70+
data->pid = bpf_get_current_pid_tgid() >> 32;
71+
data->uid = bpf_get_current_uid_gid();
72+
data->timestamp = bpf_ktime_get_ns();
73+
data->cgroup_id = cgroup_id;
5874

59-
bpf_trace_printk("LOG: filename=%s otype=%s comm=%s\n", sizeof("LOG: filename=%s otype=%s comm=%s\n"), data->filename, data->otype, data->comm);
60-
bpf_trace_printk("LOG: inode=%u\n", sizeof("LOG: inode=%u\n"), inode_num);
75+
bpf_core_read_str(data->filename, sizeof(data->filename), d_name.name);
76+
bpf_core_read_str(data->parent_filename, sizeof(data->parent_filename), parent_d_name.name);
77+
bpf_core_read(&data->inode, sizeof(data->inode), &inode_ptr->i_ino);
78+
__builtin_memcpy(data->otype, operation, sizeof(data->otype));
6179

62-
u32 *monitored = bpf_map_lookup_elem(&monitored_inodes, &key);
63-
if (!monitored)
64-
{
65-
bpf_ringbuf_discard(data, 0);
66-
return 0;
67-
}
80+
bpf_get_current_comm(&data->comm, sizeof(data->comm));
6881

69-
int cgroup_id = bpf_get_current_cgroup_id();
70-
data->cgroup_id = cgroup_id;
82+
bpf_trace_printk("LOG: filename=%s otype=%s comm=%s\n", sizeof("LOG: filename=%s otype=%s comm=%s\n"), data->filename, data->otype, data->comm);
83+
bpf_trace_printk("LOG: inode=%u\n", sizeof("LOG: inode=%u\n"), data->inode);
7184

7285
bpf_ringbuf_submit(data, 0);
7386

0 commit comments

Comments
 (0)