#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
struct verity_info {
__u8 has_roothash;
__u8 sig_valid;
__u32 setintegrity_cnt;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 64);
__type(key, __u32);
__type(value, struct verity_info);
} verity_devices SEC(".maps");
int alloc_count;
char _license[] SEC("license") = "GPL";
SEC("lsm.s/bdev_setintegrity")
int BPF_PROG(bdev_setintegrity, struct block_device *bdev,
enum lsm_integrity_type type, const void *value, size_t size)
{
struct verity_info zero = {};
struct verity_info *info;
__u32 dev;
char buf;
(void)bpf_copy_from_user(&buf, sizeof(buf), NULL);
dev = bdev->bd_dev;
info = bpf_map_lookup_elem(&verity_devices, &dev);
if (!info) {
bpf_map_update_elem(&verity_devices, &dev, &zero, BPF_NOEXIST);
info = bpf_map_lookup_elem(&verity_devices, &dev);
if (!info)
return 0;
}
if (type == LSM_INT_DMVERITY_ROOTHASH)
info->has_roothash = 1;
else if (type == LSM_INT_DMVERITY_SIG_VALID)
info->sig_valid = (value != NULL);
__sync_fetch_and_add(&info->setintegrity_cnt, 1);
return 0;
}
SEC("lsm/bdev_free_security")
void BPF_PROG(bdev_free_security, struct block_device *bdev)
{
__u32 dev = bdev->bd_dev;
bpf_map_delete_elem(&verity_devices, &dev);
}
SEC("lsm.s/bdev_alloc_security")
int BPF_PROG(bdev_alloc_security, struct block_device *bdev)
{
char buf;
(void)bpf_copy_from_user(&buf, sizeof(buf), NULL);
__sync_fetch_and_add(&alloc_count, 1);
return 0;
}