1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2020 Google LLC.
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
12 char _license[] SEC("license") = "GPL";
14 #define DUMMY_STORAGE_VALUE 0xdeadbeef
16 __u32 monitored_pid = 0;
17 int inode_storage_result = -1;
18 int sk_storage_result = -1;
19 int task_storage_result = -1;
21 struct local_storage {
22 struct inode *exec_inode;
27 __uint(type, BPF_MAP_TYPE_INODE_STORAGE);
28 __uint(map_flags, BPF_F_NO_PREALLOC);
30 __type(value, struct local_storage);
31 } inode_storage_map SEC(".maps");
34 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
35 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
37 __type(value, struct local_storage);
38 } sk_storage_map SEC(".maps");
41 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
42 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
44 __type(value, struct local_storage);
45 } sk_storage_map2 SEC(".maps");
48 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
49 __uint(map_flags, BPF_F_NO_PREALLOC);
51 __type(value, struct local_storage);
52 } task_storage_map SEC(".maps");
55 __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
56 __uint(map_flags, BPF_F_NO_PREALLOC);
58 __type(value, struct local_storage);
59 } task_storage_map2 SEC(".maps");
61 SEC("lsm/inode_unlink")
62 int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
64 __u32 pid = bpf_get_current_pid_tgid() >> 32;
65 struct bpf_local_storage *local_storage;
66 struct local_storage *storage;
67 struct task_struct *task;
70 if (pid != monitored_pid)
73 task = bpf_get_current_task_btf();
77 task_storage_result = -1;
79 storage = bpf_task_storage_get(&task_storage_map, task, 0, 0);
83 /* Don't let an executable delete itself */
84 is_self_unlink = storage->exec_inode == victim->d_inode;
86 storage = bpf_task_storage_get(&task_storage_map2, task, 0,
87 BPF_LOCAL_STORAGE_GET_F_CREATE);
88 if (!storage || storage->value)
91 if (bpf_task_storage_delete(&task_storage_map, task))
94 /* Ensure that the task_storage_map is disconnected from the storage.
95 * The storage memory should not be freed back to the
98 local_storage = task->bpf_storage;
99 if (!local_storage || local_storage->smap)
102 task_storage_result = 0;
104 return is_self_unlink ? -EPERM : 0;
107 SEC("lsm.s/inode_rename")
108 int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
109 struct inode *new_dir, struct dentry *new_dentry,
112 struct local_storage *storage;
115 /* new_dentry->d_inode can be NULL when the inode is renamed to a file
116 * that did not exist before. The helper should be able to handle this
119 bpf_inode_storage_get(&inode_storage_map, new_dentry->d_inode, 0,
120 BPF_LOCAL_STORAGE_GET_F_CREATE);
122 storage = bpf_inode_storage_get(&inode_storage_map, old_dentry->d_inode,
127 if (storage->value != DUMMY_STORAGE_VALUE)
128 inode_storage_result = -1;
130 err = bpf_inode_storage_delete(&inode_storage_map, old_dentry->d_inode);
132 inode_storage_result = err;
137 SEC("lsm.s/socket_bind")
138 int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
141 __u32 pid = bpf_get_current_pid_tgid() >> 32;
142 struct local_storage *storage;
143 struct sock *sk = sock->sk;
145 if (pid != monitored_pid || !sk)
148 storage = bpf_sk_storage_get(&sk_storage_map, sk, 0, 0);
152 sk_storage_result = -1;
153 if (storage->value != DUMMY_STORAGE_VALUE)
156 /* This tests that we can associate multiple elements
157 * with the local storage.
159 storage = bpf_sk_storage_get(&sk_storage_map2, sk, 0,
160 BPF_LOCAL_STORAGE_GET_F_CREATE);
164 if (bpf_sk_storage_delete(&sk_storage_map2, sk))
167 storage = bpf_sk_storage_get(&sk_storage_map2, sk, 0,
168 BPF_LOCAL_STORAGE_GET_F_CREATE);
172 if (bpf_sk_storage_delete(&sk_storage_map, sk))
175 /* Ensure that the sk_storage_map is disconnected from the storage. */
176 if (!sk->sk_bpf_storage || sk->sk_bpf_storage->smap)
179 sk_storage_result = 0;
183 SEC("lsm.s/socket_post_create")
184 int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
185 int protocol, int kern)
187 __u32 pid = bpf_get_current_pid_tgid() >> 32;
188 struct local_storage *storage;
189 struct sock *sk = sock->sk;
191 if (pid != monitored_pid || !sk)
194 storage = bpf_sk_storage_get(&sk_storage_map, sk, 0,
195 BPF_LOCAL_STORAGE_GET_F_CREATE);
199 storage->value = DUMMY_STORAGE_VALUE;
204 /* This uses the local storage to remember the inode of the binary that a
205 * process was originally executing.
207 SEC("lsm.s/bprm_committed_creds")
208 void BPF_PROG(exec, struct linux_binprm *bprm)
210 __u32 pid = bpf_get_current_pid_tgid() >> 32;
211 struct local_storage *storage;
213 if (pid != monitored_pid)
216 storage = bpf_task_storage_get(&task_storage_map,
217 bpf_get_current_task_btf(), 0,
218 BPF_LOCAL_STORAGE_GET_F_CREATE);
220 storage->exec_inode = bprm->file->f_inode;
222 storage = bpf_inode_storage_get(&inode_storage_map, bprm->file->f_inode,
223 0, BPF_LOCAL_STORAGE_GET_F_CREATE);
227 storage->value = DUMMY_STORAGE_VALUE;