1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Google LLC. */
3
4 #include <vmlinux.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7
8 #include "bpf_misc.h"
9 #include "bpf_experimental.h"
10
11 static char buf[64];
12
13 SEC("lsm.s/file_open")
14 __success
BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_sleepable)15 int BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_sleepable)
16 {
17 struct file *acquired;
18
19 acquired = bpf_get_task_exe_file(bpf_get_current_task_btf());
20 if (!acquired)
21 return 0;
22
23 bpf_put_file(acquired);
24 return 0;
25 }
26
27 SEC("lsm/file_open")
28 __success
BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_non_sleepable,struct file * file)29 int BPF_PROG(get_task_exe_file_and_put_kfunc_from_current_non_sleepable, struct file *file)
30 {
31 struct file *acquired;
32
33 acquired = bpf_get_task_exe_file(bpf_get_current_task_btf());
34 if (!acquired)
35 return 0;
36
37 bpf_put_file(acquired);
38 return 0;
39 }
40
41 SEC("lsm.s/task_alloc")
42 __success
BPF_PROG(get_task_exe_file_and_put_kfunc_from_argument,struct task_struct * task)43 int BPF_PROG(get_task_exe_file_and_put_kfunc_from_argument,
44 struct task_struct *task)
45 {
46 struct file *acquired;
47
48 acquired = bpf_get_task_exe_file(task);
49 if (!acquired)
50 return 0;
51
52 bpf_put_file(acquired);
53 return 0;
54 }
55
56 SEC("lsm.s/inode_getattr")
57 __success
BPF_PROG(path_d_path_from_path_argument,struct path * path)58 int BPF_PROG(path_d_path_from_path_argument, struct path *path)
59 {
60 int ret;
61
62 ret = bpf_path_d_path(path, buf, sizeof(buf));
63 __sink(ret);
64 return 0;
65 }
66
67 SEC("lsm.s/file_open")
68 __success
BPF_PROG(path_d_path_from_file_argument,struct file * file)69 int BPF_PROG(path_d_path_from_file_argument, struct file *file)
70 {
71 int ret;
72 struct path *path;
73
74 /* The f_path member is a path which is embedded directly within a
75 * file. Therefore, a pointer to such embedded members are still
76 * recognized by the BPF verifier as being PTR_TRUSTED as it's
77 * essentially PTR_TRUSTED w/ a non-zero fixed offset.
78 */
79 path = &file->f_path;
80 ret = bpf_path_d_path(path, buf, sizeof(buf));
81 __sink(ret);
82 return 0;
83 }
84
85 char _license[] SEC("license") = "GPL";
86