2021-05-27 00:09:36 +05:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2021-09-23 21:59:15 +05:00
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <stdbool.h>
|
2021-05-27 00:09:36 +05:00
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
|
|
|
|
#define DUMMY_STORAGE_VALUE 0xdeadbeef
|
|
|
|
|
|
|
|
int monitored_pid = 0;
|
|
|
|
int inode_storage_result = -1;
|
|
|
|
int sk_storage_result = -1;
|
|
|
|
|
2021-09-23 21:59:15 +05:00
|
|
|
struct dummy_storage {
|
2021-05-27 00:09:36 +05:00
|
|
|
__u32 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
__uint(type, BPF_MAP_TYPE_INODE_STORAGE);
|
|
|
|
__uint(map_flags, BPF_F_NO_PREALLOC);
|
|
|
|
__type(key, int);
|
2021-09-23 21:59:15 +05:00
|
|
|
__type(value, struct dummy_storage);
|
2021-05-27 00:09:36 +05:00
|
|
|
} 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);
|
2021-09-23 21:59:15 +05:00
|
|
|
__type(value, struct dummy_storage);
|
2021-05-27 00:09:36 +05:00
|
|
|
} sk_storage_map SEC(".maps");
|
|
|
|
|
2021-09-23 21:59:15 +05:00
|
|
|
/* TODO Use vmlinux.h once BTF pruning for embedded types is fixed.
|
|
|
|
*/
|
|
|
|
struct sock {} __attribute__((preserve_access_index));
|
|
|
|
struct sockaddr {} __attribute__((preserve_access_index));
|
|
|
|
struct socket {
|
|
|
|
struct sock *sk;
|
|
|
|
} __attribute__((preserve_access_index));
|
|
|
|
|
|
|
|
struct inode {} __attribute__((preserve_access_index));
|
|
|
|
struct dentry {
|
|
|
|
struct inode *d_inode;
|
|
|
|
} __attribute__((preserve_access_index));
|
|
|
|
struct file {
|
|
|
|
struct inode *f_inode;
|
|
|
|
} __attribute__((preserve_access_index));
|
|
|
|
|
2021-05-27 00:09:36 +05:00
|
|
|
|
|
|
|
SEC("lsm/inode_unlink")
|
|
|
|
int BPF_PROG(unlink_hook, struct inode *dir, struct dentry *victim)
|
|
|
|
{
|
|
|
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
2021-09-23 21:59:15 +05:00
|
|
|
struct dummy_storage *storage;
|
|
|
|
int err;
|
2021-05-27 00:09:36 +05:00
|
|
|
|
|
|
|
if (pid != monitored_pid)
|
|
|
|
return 0;
|
|
|
|
|
2021-09-23 21:59:15 +05:00
|
|
|
storage = bpf_inode_storage_get(&inode_storage_map, victim->d_inode, 0,
|
|
|
|
BPF_LOCAL_STORAGE_GET_F_CREATE);
|
2021-05-27 00:09:36 +05:00
|
|
|
if (!storage)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (storage->value != DUMMY_STORAGE_VALUE)
|
|
|
|
inode_storage_result = -1;
|
|
|
|
|
2021-09-23 21:59:15 +05:00
|
|
|
err = bpf_inode_storage_delete(&inode_storage_map, victim->d_inode);
|
2021-05-27 00:09:36 +05:00
|
|
|
if (!err)
|
|
|
|
inode_storage_result = err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("lsm/socket_bind")
|
|
|
|
int BPF_PROG(socket_bind, struct socket *sock, struct sockaddr *address,
|
|
|
|
int addrlen)
|
|
|
|
{
|
|
|
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
2021-09-23 21:59:15 +05:00
|
|
|
struct dummy_storage *storage;
|
2021-05-27 00:09:36 +05:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (pid != monitored_pid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
|
|
|
|
BPF_LOCAL_STORAGE_GET_F_CREATE);
|
|
|
|
if (!storage)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (storage->value != DUMMY_STORAGE_VALUE)
|
|
|
|
sk_storage_result = -1;
|
|
|
|
|
|
|
|
err = bpf_sk_storage_delete(&sk_storage_map, sock->sk);
|
|
|
|
if (!err)
|
|
|
|
sk_storage_result = err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("lsm/socket_post_create")
|
|
|
|
int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
|
|
|
|
int protocol, int kern)
|
|
|
|
{
|
|
|
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
2021-09-23 21:59:15 +05:00
|
|
|
struct dummy_storage *storage;
|
2021-05-27 00:09:36 +05:00
|
|
|
|
|
|
|
if (pid != monitored_pid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
storage = bpf_sk_storage_get(&sk_storage_map, sock->sk, 0,
|
|
|
|
BPF_LOCAL_STORAGE_GET_F_CREATE);
|
|
|
|
if (!storage)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
storage->value = DUMMY_STORAGE_VALUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:59:15 +05:00
|
|
|
SEC("lsm/file_open")
|
|
|
|
int BPF_PROG(file_open, struct file *file)
|
2021-05-27 00:09:36 +05:00
|
|
|
{
|
|
|
|
__u32 pid = bpf_get_current_pid_tgid() >> 32;
|
2021-09-23 21:59:15 +05:00
|
|
|
struct dummy_storage *storage;
|
2021-05-27 00:09:36 +05:00
|
|
|
|
|
|
|
if (pid != monitored_pid)
|
2021-09-23 21:59:15 +05:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!file->f_inode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
storage = bpf_inode_storage_get(&inode_storage_map, file->f_inode, 0,
|
|
|
|
BPF_LOCAL_STORAGE_GET_F_CREATE);
|
2021-05-27 00:09:36 +05:00
|
|
|
if (!storage)
|
2021-09-23 21:59:15 +05:00
|
|
|
return 0;
|
2021-05-27 00:09:36 +05:00
|
|
|
|
|
|
|
storage->value = DUMMY_STORAGE_VALUE;
|
2021-09-23 21:59:15 +05:00
|
|
|
return 0;
|
2021-05-27 00:09:36 +05:00
|
|
|
}
|