tools/testing/selftests/bpf/progs/local_storage.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/local_storage.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/local_storage.c
Extension
.c
Size
5083 bytes
Lines
214
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct local_storage {
	struct inode *exec_inode;
	__u32 value;
};

struct {
	__uint(type, BPF_MAP_TYPE_INODE_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, struct local_storage);
} inode_storage_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
	__type(key, int);
	__type(value, struct local_storage);
} sk_storage_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
	__type(key, int);
	__type(value, struct local_storage);
} sk_storage_map2 SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, struct local_storage);
} task_storage_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, struct local_storage);
} task_storage_map2 SEC(".maps");

SEC("lsm/inode_unlink")
int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
{
	__u32 pid = bpf_get_current_pid_tgid() >> 32;
	struct local_storage *storage;
	struct task_struct *task;
	bool is_self_unlink;

	if (pid != monitored_pid)
		return 0;

	task = bpf_get_current_task_btf();
	if (!task)
		return 0;

	task_storage_result = -1;

	storage = bpf_task_storage_get(&task_storage_map, task, 0, 0);
	if (!storage)
		return 0;

	/* Don't let an executable delete itself */
	is_self_unlink = storage->exec_inode == victim->d_inode;

	storage = bpf_task_storage_get(&task_storage_map2, task, 0,
				       BPF_LOCAL_STORAGE_GET_F_CREATE);
	if (!storage || storage->value)
		return 0;

	if (bpf_task_storage_delete(&task_storage_map2, task))
		return 0;

	if (bpf_task_storage_delete(&task_storage_map, task))
		return 0;

	task_storage_result = 0;

	return is_self_unlink ? -EPERM : 0;
}

SEC("lsm.s/inode_rename")
int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
	     struct inode *new_dir, struct dentry *new_dentry,
	     unsigned int flags)
{
	struct local_storage *storage;
	int err;

	/* new_dentry->d_inode can be NULL when the inode is renamed to a file
	 * that did not exist before. The helper should be able to handle this

Annotation

Implementation Notes