#include <linux/bpf.h>
#include <linux/bpf_mem_alloc.h>
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/filter.h>
#include <linux/skbuff.h>
#include <crypto/aes-cbc.h>
#include <crypto/aes-ecb.h>
struct bpf_crypto_params {
char type[14];
u8 reserved[2];
char algo[128];
u8 key[256];
u32 key_len;
u32 authsize;
};
enum bpf_crypto_algo_id {
BPF_ALGO_AES_CBC,
BPF_ALGO_AES_ECB,
};
static const struct {
const char *type_name;
const char *algo_name;
enum bpf_crypto_algo_id algo;
} bpf_crypto_algos[] = {
{ "skcipher", "cbc(aes)", BPF_ALGO_AES_CBC },
{ "skcipher", "ecb(aes)", BPF_ALGO_AES_ECB },
};
static bool bpf_crypto_find_algo(const struct bpf_crypto_params *params,
enum bpf_crypto_algo_id *id_ret)
{
for (size_t i = 0; i < ARRAY_SIZE(bpf_crypto_algos); i++) {
if (strncmp(bpf_crypto_algos[i].type_name, params->type,
sizeof(params->type)) == 0 &&
strncmp(bpf_crypto_algos[i].algo_name, params->algo,
sizeof(params->algo)) == 0) {
*id_ret = bpf_crypto_algos[i].algo;
return true;
}
}
return false;
}
struct bpf_crypto_ctx {
union {
struct aes_key aes;
} key;
enum bpf_crypto_algo_id algo;
struct rcu_head rcu;
refcount_t usage;
};
__bpf_kfunc_start_defs();
__bpf_kfunc struct bpf_crypto_ctx *
bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz,
int *err_ret)
{
struct bpf_crypto_ctx *ctx;
int err;
if (!params ||
params__sz != sizeof(struct bpf_crypto_params) ||
params->reserved[0] || params->reserved[1]) {
*err_ret = -EINVAL;
return NULL;
}
if (!params->key_len || params->key_len > sizeof(params->key)) {
*err_ret = -EINVAL;
return NULL;
}
ctx = kzalloc_obj(*ctx);
if (!ctx) {
*err_ret = -ENOMEM;
return NULL;
}
if (!bpf_crypto_find_algo(params, &ctx->algo)) {
err = -EOPNOTSUPP;
goto out;
}
switch (ctx->algo) {
case BPF_ALGO_AES_CBC:
case BPF_ALGO_AES_ECB:
if (params->authsize)
err = -EOPNOTSUPP;
else
err = aes_preparekey(&ctx->key.aes, params->key,
params->key_len);
break;
default:
WARN_ON_ONCE(1);
err = -EOPNOTSUPP;
break;
}
out:
if (err) {
kfree_sensitive(ctx);
*err_ret = err;
return NULL;
}
refcount_set(&ctx->usage, 1);
*err_ret = 0;
return ctx;
}
static void crypto_free_cb(struct rcu_head *head)
{
struct bpf_crypto_ctx *ctx;
ctx = container_of(head, struct bpf_crypto_ctx, rcu);
kfree_sensitive(ctx);
}
__bpf_kfunc struct bpf_crypto_ctx *
bpf_crypto_ctx_acquire(struct bpf_crypto_ctx *ctx)
{
if (!refcount_inc_not_zero(&ctx->usage))
return NULL;
return ctx;
}
__bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
{
if (refcount_dec_and_test(&ctx->usage))
call_rcu(&ctx->rcu, crypto_free_cb);
}
__bpf_kfunc void bpf_crypto_ctx_release_dtor(void *ctx)
{
bpf_crypto_ctx_release(ctx);
}
CFI_NOSEAL(bpf_crypto_ctx_release_dtor);
static int bpf_aes_cbc_crypt(u8 *dst, u32 dst_len, const u8 *src, u32 src_len,
u8 *iv, u32 iv_len,
const struct bpf_crypto_ctx *ctx, bool decrypt)
{
if (iv_len != AES_BLOCK_SIZE)
return -EINVAL;
if (src_len % AES_BLOCK_SIZE || dst_len < src_len)
return -EINVAL;
if (decrypt)
aes_cbc_decrypt(dst, src, src_len, iv, &ctx->key.aes);
else
aes_cbc_encrypt(dst, src, src_len, iv, &ctx->key.aes);
return 0;
}
static int bpf_aes_ecb_crypt(u8 *dst, u32 dst_len, const u8 *src, u32 src_len,
u8 *iv, u32 iv_len,
const struct bpf_crypto_ctx *ctx, bool decrypt)
{
if (iv_len != 0)
return -EINVAL;
if (src_len % AES_BLOCK_SIZE || dst_len < src_len)
return -EINVAL;
if (decrypt)
aes_ecb_decrypt(dst, src, src_len, &ctx->key.aes);
else
aes_ecb_encrypt(dst, src, src_len, &ctx->key.aes);
return 0;
}
static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
const struct bpf_dynptr_kern *src,
const struct bpf_dynptr_kern *dst,
const struct bpf_dynptr_kern *iv,
bool decrypt)
{
u32 src_len, dst_len, iv_len;
const u8 *psrc;
u8 *pdst, *piv;
if (__bpf_dynptr_is_rdonly(dst))
return -EINVAL;
iv_len = iv ? __bpf_dynptr_size(iv) : 0;
src_len = __bpf_dynptr_size(src);
dst_len = __bpf_dynptr_size(dst);
if (!src_len || !dst_len)
return -EINVAL;
psrc = __bpf_dynptr_data(src, src_len);
if (!psrc)
return -EINVAL;
pdst = __bpf_dynptr_data_rw(dst, dst_len);
if (!pdst)
return -EINVAL;
piv = iv_len ? __bpf_dynptr_data_rw(iv, iv_len) : NULL;
if (iv_len && !piv)
return -EINVAL;
switch (ctx->algo) {
case BPF_ALGO_AES_CBC:
return bpf_aes_cbc_crypt(pdst, dst_len, psrc, src_len, piv,
iv_len, ctx, decrypt);
case BPF_ALGO_AES_ECB:
return bpf_aes_ecb_crypt(pdst, dst_len, psrc, src_len, piv,
iv_len, ctx, decrypt);
default:
return -EINVAL;
}
}
__bpf_kfunc int bpf_crypto_decrypt(struct bpf_crypto_ctx *ctx,
const struct bpf_dynptr *src,
const struct bpf_dynptr *dst,
const struct bpf_dynptr *iv__nullable)
{
const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
const struct bpf_dynptr_kern *iv_kern = (struct bpf_dynptr_kern *)iv__nullable;
return bpf_crypto_crypt(ctx, src_kern, dst_kern, iv_kern, true);
}
__bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,
const struct bpf_dynptr *src,
const struct bpf_dynptr *dst,
const struct bpf_dynptr *iv__nullable)
{
const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
const struct bpf_dynptr_kern *iv_kern = (struct bpf_dynptr_kern *)iv__nullable;
return bpf_crypto_crypt(ctx, src_kern, dst_kern, iv_kern, false);
}
__bpf_kfunc_end_defs();
BTF_KFUNCS_START(crypt_init_kfunc_btf_ids)
BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_crypto_ctx_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_crypto_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_KFUNCS_END(crypt_init_kfunc_btf_ids)
static const struct btf_kfunc_id_set crypt_init_kfunc_set = {
.owner = THIS_MODULE,
.set = &crypt_init_kfunc_btf_ids,
};
BTF_KFUNCS_START(crypt_kfunc_btf_ids)
BTF_ID_FLAGS(func, bpf_crypto_decrypt, KF_RCU)
BTF_ID_FLAGS(func, bpf_crypto_encrypt, KF_RCU)
BTF_KFUNCS_END(crypt_kfunc_btf_ids)
static const struct btf_kfunc_id_set crypt_kfunc_set = {
.owner = THIS_MODULE,
.set = &crypt_kfunc_btf_ids,
};
BTF_ID_LIST(bpf_crypto_dtor_ids)
BTF_ID(struct, bpf_crypto_ctx)
BTF_ID(func, bpf_crypto_ctx_release_dtor)
static int __init crypto_kfunc_init(void)
{
int ret;
const struct btf_id_dtor_kfunc bpf_crypto_dtors[] = {
{
.btf_id = bpf_crypto_dtor_ids[0],
.kfunc_btf_id = bpf_crypto_dtor_ids[1]
},
};
ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &crypt_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &crypt_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &crypt_kfunc_set);
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
&crypt_init_kfunc_set);
return ret ?: register_btf_id_dtor_kfuncs(bpf_crypto_dtors,
ARRAY_SIZE(bpf_crypto_dtors),
THIS_MODULE);
}
late_initcall