8#include <bpf/vmlinux.h>
9#include <bpf/bpf_helpers.h>
12#include "shared_rust_types.h"
29static __always_inline u32 get_inode_pol_id(
struct inode *inode)
31 u32 *policy_id = bpf_inode_storage_get(&
inode_storage, inode, 0, 0);
40static __always_inline u32 get_map_pol_id(
struct bpf_map *map)
44 return data->policy_id;
53static __always_inline u32 get_pid()
57 return bpf_get_current_pid_tgid() >> 32;
65 return bpf_get_current_task_btf();
76static __always_inline
struct c_policy_config *get_policy_config(u32 policy_id)
78 if (policy_id == NO_POL_ID) {
81 return bpf_map_lookup_elem(&
policy_map, &policy_id);
91static __always_inline
bool is_valid_policy_id(u32 policy_id)
93 return get_policy_config(policy_id) != NULL;
105get_target_task_data(
struct task_struct *t)
118 struct task_struct *task =
get_task();
119 return get_target_task_data(task);
128static __always_inline u32 get_target_task_pol_id(
struct task_struct *t)
143static __always_inline u32 get_task_pol_id()
160static __always_inline
void set_task_pinning(u32 flag)
163 struct task_struct *task =
get_task();
166 if (data && data->pol_id != NO_POL_ID) {
169 if (cfg && cfg->protect_pins) {
171 if (data->is_pinning != flag) {
172 data->is_pinning = flag;
173 u64 log[3] = { (u64)task->comm, (u64)task->tgid, (u64)flag };
174 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
175 "set task %s(%d) pinning to %lu", log,
196static __always_inline u32 get_object_valid_policy_id(
void *
object,
197 enum object_type type)
200 u32 current_pol_id = NO_POL_ID;
202 case OBJECT_TYPE_TASK:
203 current_pol_id = get_target_task_pol_id((
struct task_struct *)
object);
205 case OBJECT_TYPE_INODE:
206 current_pol_id = get_inode_pol_id((
struct inode *)
object);
208 case OBJECT_TYPE_MAP:
209 current_pol_id = get_map_pol_id((
struct bpf_map *)
object);
213 if (is_valid_policy_id(current_pol_id)) {
214 return current_pol_id;
226static __always_inline
void label_task(
struct task_struct *task,
227 const unsigned char *task_name,
230 u32 current_pol_id = get_object_valid_policy_id(task, OBJECT_TYPE_TASK);
231 if (current_pol_id == policy_id) {
234 }
else if (!is_valid_policy_id(policy_id)) {
241 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
242 "Did not label task %s(%d) since %d is invalid", log,
244 }
else if (current_pol_id != NO_POL_ID) {
246 u64 log[4] = { (u64)task_name, (u64)task->tgid, (u64)policy_id,
247 (u64)current_pol_id };
249 LOG_LEVEL_ERROR, LOG_REASON_ERROR,
250 "failed to label task %s(%d) as %d. Task already belongs to policy %d",
256 &
task_storage, task, &new_data, BPF_LOCAL_STORAGE_GET_F_CREATE);
257 u64 log[3] = { (u64)task_name, (u64)task->tgid, (u64)policy_id };
259 log_generic_msg(LOG_LEVEL_DEBUG, LOG_REASON_DEBUG,
260 "label task %s(%d) as %d", log,
sizeof(log));
262 log_generic_msg(LOG_LEVEL_ERROR, LOG_REASON_ERROR,
263 "failed to label task %s(%d) as %d", log,
276static __always_inline
void label_inode(
struct dentry *dentry,
277 struct inode *inode, u32 policy_id)
279 const unsigned char *name = dentry->d_name.name;
280 u32 current_pol_id = get_object_valid_policy_id(inode, OBJECT_TYPE_INODE);
281 if (current_pol_id == policy_id) {
284 }
else if (!is_valid_policy_id(policy_id)) {
286 u64 log[3] = { (u64)name, (u64)policy_id };
287 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
288 "Did not label inode %s since %d is invalid", log,
290 }
else if (current_pol_id != NO_POL_ID) {
292 u64 log[3] = { (u64)name, (u64)policy_id, (u64)current_pol_id };
294 LOG_LEVEL_ERROR, LOG_REASON_ERROR,
295 "failed to label inode for %s as %d. Inode already belongs to policy %d",
299 u32 *label = bpf_inode_storage_get(&
inode_storage, inode, &policy_id,
300 BPF_LOCAL_STORAGE_GET_F_CREATE);
301 u64 log[2] = { (u64)name, (u64)policy_id };
303 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
304 "label inode for '%s' as %d", log,
sizeof(log));
306 log_generic_msg(LOG_LEVEL_ERROR, LOG_REASON_ERROR,
307 "failed to label inode for %s as %d", log,
321static __always_inline
int label_map_with_id(
struct bpf_map *map, u32 policy_id)
329 map_data.policy_id = policy_id;
330 BPF_CORE_READ_STR_INTO(&map_data.name, map, name);
334 bpf_map_update_elem(&
map_to_pol_id, &map, &map_data, BPF_NOEXIST);
337 u64 data[4] = { (u64)map_data.name, (u64)BPF_CORE_READ(map,
id),
338 (u64)policy_id, (u64)err };
340 LOG_LEVEL_ERROR, LOG_REASON_ERROR,
341 "Error: update elem failed map %s(%d) for policy %d, code: %d",
344 u64 data[3] = { (u64)map_data.name, (u64)BPF_CORE_READ(map,
id),
346 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
347 "label map %s(%d) as %d", data,
sizeof(data));
361static __always_inline
int label_map(
struct bpf_map *map)
363 return label_map_with_id(map, get_task_pol_id());
366static __always_inline
int unlabel_map(
struct bpf_map *map)
382 BPF_CORE_READ_STR_INTO(&map_name, map, name);
383 u64 data[] = { (u64)map_name, (u64)BPF_CORE_READ(map,
id) };
385 log_generic_msg(LOG_LEVEL_ERROR, LOG_REASON_ERROR,
386 "Error: failed to unlabel map: %s(%d)", data,
389 log_generic_msg(LOG_LEVEL_TRACE, LOG_REASON_DEBUG,
"unlabel map %s(%d)",
#define BPF_MAP_NAME_LEN
the length of the name of an eBPF map
Definition constants.h:26
#define ALLOW
LSM return code for allowing an operation to continue.
Definition seabee_utils.h:19
struct task_struct * get_task()
gets the task for the current context
Definition seabee_utils.h:63
Definition seabee_maps.h:60
c_policy_config contains security levels for protected objects and corresponds to a policy id.
Definition shared_rust_types.h:31
Definition seabee_maps.h:27
Definition seabee_maps.h:65
Definition seabee_maps.h:46
Definition seabee_maps.h:12
Definition seabee_maps.h:17