2020-03-29 08:43:49 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Google LLC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/btf.h>
|
2020-03-29 08:43:51 +08:00
|
|
|
#include <linux/lsm_hooks.h>
|
|
|
|
#include <linux/bpf_lsm.h>
|
2020-03-29 08:43:52 +08:00
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/bpf_verifier.h>
|
2020-08-26 02:29:18 +08:00
|
|
|
#include <net/bpf_sk_storage.h>
|
|
|
|
#include <linux/bpf_local_storage.h>
|
2020-03-29 08:43:51 +08:00
|
|
|
|
|
|
|
/* For every LSM hook that allows attachment of BPF programs, declare a nop
|
|
|
|
* function where a BPF program can be attached.
|
|
|
|
*/
|
|
|
|
#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
|
|
|
|
noinline RET bpf_lsm_##NAME(__VA_ARGS__) \
|
|
|
|
{ \
|
|
|
|
return DEFAULT; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <linux/lsm_hook_defs.h>
|
|
|
|
#undef LSM_HOOK
|
2020-03-29 08:43:49 +08:00
|
|
|
|
2020-03-29 08:43:52 +08:00
|
|
|
#define BPF_LSM_SYM_PREFX "bpf_lsm_"
|
|
|
|
|
|
|
|
int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
|
|
|
|
const struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
if (!prog->gpl_compatible) {
|
|
|
|
bpf_log(vlog,
|
|
|
|
"LSM programs must have a GPL compatible license\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(BPF_LSM_SYM_PREFX, prog->aux->attach_func_name,
|
|
|
|
sizeof(BPF_LSM_SYM_PREFX) - 1)) {
|
|
|
|
bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
|
|
|
|
prog->aux->attach_btf_id, prog->aux->attach_func_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:29:18 +08:00
|
|
|
static const struct bpf_func_proto *
|
|
|
|
bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
switch (func_id) {
|
|
|
|
case BPF_FUNC_inode_storage_get:
|
|
|
|
return &bpf_inode_storage_get_proto;
|
|
|
|
case BPF_FUNC_inode_storage_delete:
|
|
|
|
return &bpf_inode_storage_delete_proto;
|
|
|
|
case BPF_FUNC_sk_storage_get:
|
|
|
|
return &sk_storage_get_btf_proto;
|
|
|
|
case BPF_FUNC_sk_storage_delete:
|
|
|
|
return &sk_storage_delete_btf_proto;
|
|
|
|
default:
|
|
|
|
return tracing_prog_func_proto(func_id, prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 08:43:49 +08:00
|
|
|
const struct bpf_prog_ops lsm_prog_ops = {
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct bpf_verifier_ops lsm_verifier_ops = {
|
2020-08-26 02:29:18 +08:00
|
|
|
.get_func_proto = bpf_lsm_func_proto,
|
2020-03-29 08:43:49 +08:00
|
|
|
.is_valid_access = btf_ctx_access,
|
|
|
|
};
|