Skip to contentSkip to conversation

bpf: crypto: Use AES-CBC and AES-ECB libraries

PATCH Superseded Eric Biggers 2 participants 2 messages Sep 18

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: crypto: Use AES-CBC and AES-ECB libraries

BPF crypto was implemented using the lskcipher API, which doesn't seem to be going anywhere. lskcipher supports only ARC4 and block ciphers in CBC and ECB mode, and only with unoptimized implementations.

Library APIs also have been found to be a much better approach, for a variety of reasons, including reduced overhead, greater flexibility, and having to be explicit about the crypto algorithms that are supported.

We can safely ignore the theoretical ARC4 and non-AES block cipher support in BPF crypto as unused, which leaves AES-CBC and AES-ECB. AES-CBC was stated to be needed for decrypting packets using a homebrew UDP-based protocol
(https://lore.kernel.org/r/[email protected]/).
AES-ECB is used by the BPF self-tests, and it was stated to maybe be used in the future for QUIC-LB
(https://lore.kernel.org/r/[email protected]/).

Those reasons don't make much sense either, especially AES-ECB which isn't appropriate in new systems and should be dropped. Regardless, let's assume that both AES-CBC and AES-ECB need to be kept for now.

There are library APIs for both of these now, which are much easier to use and more efficient. Reimplement BPF crypto on top of them, greatly simplifying the code. As part of this, the bpf_crypto_type abstraction layer is removed, as it's not useful.

This removes the only user of the lskcipher API, so follow-up patches will be able to remove that code as well.

---

Changed in v2:
- Use 'depends on' instead of 'select', to match the usual convention for bpf kfuncs.
- Keep using EOPNOTSUPP for unsupported algorithms.
- Avoid reading *err_ret after it's been written.
- Clarify that non-AES block ciphers are dropped too.
- Mention that lskcipher will be deleted later.
- Update a comment.

 MAINTAINERS                  |   2 -
 crypto/Makefile              |   3 -
 crypto/bpf_crypto_skcipher.c |  83 ----------
 include/linux/bpf_crypto.h   |  24 ---
 kernel/bpf/Kconfig           |   9 ++
 kernel/bpf/Makefile          |   4 +-
 kernel/bpf/crypto.c          | 288 +++++++++++++++--------------------
 7 files changed, 134 insertions(+), 279 deletions(-)
 delete mode 100644 crypto/bpf_crypto_skcipher.c
 delete mode 100644 include/linux/bpf_crypto.h
MAINTAINERS +0 -2
index 6215fcb077705..0fcdfe48c62a0 100644
@@ -4936,8 +4936,6 @@ BPF [CRYPTO]
 M:	Vadim Fedorenko <[email protected]>
 L:	[email protected]
 S:	Maintained
-F:	crypto/bpf_crypto_skcipher.c
-F:	include/linux/bpf_crypto.h
 F:	kernel/bpf/crypto.c
 
 BPF [DOCUMENTATION] (Related to Standardization)
crypto/Makefile +0 -3
index 8386d55a9755e..33bb5ad595e8f 100644
@@ -22,9 +22,6 @@ crypto_skcipher-y += lskcipher.o
 crypto_skcipher-y += skcipher.o
 
 obj-$(CONFIG_CRYPTO_SKCIPHER2) += crypto_skcipher.o
-ifeq ($(CONFIG_BPF_SYSCALL),y)
-obj-$(CONFIG_CRYPTO_SKCIPHER2) += bpf_crypto_skcipher.o
-endif
 
 obj-$(CONFIG_CRYPTO_SEQIV) += seqiv.o
 obj-$(CONFIG_CRYPTO_ECHAINIV) += echainiv.o
crypto/bpf_crypto_skcipher.c deleted +0 -83

83 lines not on this page · raw message

include/linux/bpf_crypto.h deleted +0 -24
deleted file mode 100644
index a41e71d4e2d9f..0000000000000
@@ -1,24 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
-#ifndef _BPF_CRYPTO_H
-#define _BPF_CRYPTO_H
-
-struct bpf_crypto_type {
-	void *(*alloc_tfm)(const char *algo);
-	void (*free_tfm)(void *tfm);
-	int (*has_algo)(const char *algo);
-	int (*setkey)(void *tfm, const u8 *key, unsigned int keylen);
-	int (*setauthsize)(void *tfm, unsigned int authsize);
-	int (*encrypt)(void *tfm, const u8 *src, u8 *dst, unsigned int len, u8 *iv);
-	int (*decrypt)(void *tfm, const u8 *src, u8 *dst, unsigned int len, u8 *iv);
-	unsigned int (*ivsize)(void *tfm);
-	unsigned int (*statesize)(void *tfm);
-	u32 (*get_flags)(void *tfm);
-	struct module *owner;
-	char name[14];
-};
-
-int bpf_crypto_register_type(const struct bpf_crypto_type *type);
-int bpf_crypto_unregister_type(const struct bpf_crypto_type *type);
-
-#endif /* _BPF_CRYPTO_H */
kernel/bpf/Kconfig +9 -0
index d7d25477ef481..a44ecfa3e9ef5 100644
@@ -91,6 +91,15 @@ config BPF_UNPRIV_DEFAULT_OFF
 
 	  If you are unsure how to answer this question, answer Y.
 
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	depends on CRYPTO_LIB_AES_CBC
+	depends on CRYPTO_LIB_AES_ECB
+	help
+	  Provide the kfuncs needed for BPF programs to encrypt and decrypt
+	  data. The supported algorithms are AES-CBC and AES-ECB.
+
 source "kernel/bpf/preload/Kconfig"
 
 config BPF_LSM
kernel/bpf/Makefile +1 -3
index 9a92c348bbda6..c1f9b0d3468d3 100644
@@ -58,9 +58,7 @@ obj-$(CONFIG_BPF_SYSCALL) += cpumask.o
 # semantics within pahole are revisited accordingly.
 obj-${CONFIG_BPF_LSM} += bpf_lsm_proto.o bpf_lsm.o
 endif
-ifneq ($(CONFIG_CRYPTO),)
-obj-$(CONFIG_BPF_SYSCALL) += crypto.o
-endif
+obj-$(CONFIG_BPF_CRYPTO) += crypto.o
 obj-$(CONFIG_BPF_PRELOAD) += preload/
 
 obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
kernel/bpf/crypto.c +124 -164

419 lines not on this page · raw message

base-commit: 71b75648d1a21c0a6379741d00f41086ec95f14a

Signature · 1 line
2.55.0
#1 lore raw
Does this change create a mismatch with kernel/bpf/verifier.c?
Quoted 20 lines
kernel/bpf/Kconfig +9 -0
index d7d25477ef481..a44ecfa3e9ef5 100644
@@ -91,6 +91,15 @@ config BPF_UNPRIV_DEFAULT_OFF
 
 	  If you are unsure how to answer this question, answer Y.
 
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	depends on CRYPTO_LIB_AES_CBC
+	depends on CRYPTO_LIB_AES_ECB
+	help
+	  Provide the kfuncs needed for BPF programs to encrypt and decrypt
+	  data. The supported algorithms are AES-CBC and AES-ECB.
+
 source "kernel/bpf/preload/Kconfig"
 
 config BPF_LSM

[ ... ]

Quoted 5 lines
kernel/bpf/Makefile +0 -0
index 9a92c348bbda6..c1f9b0d3468d3 100644

kernel/bpf/Makefile obj-$(CONFIG_BPF_SYSCALL) += cpumask.o · line 64

Quoted 10 lines
kernel/bpf/Makefile +1 -3
@@ -58,9 +58,7 @@ obj-$(CONFIG_BPF_SYSCALL) += cpumask.o
 # semantics within pahole are revisited accordingly.
 obj-${CONFIG_BPF_LSM} += bpf_lsm_proto.o bpf_lsm.o
 endif
-ifneq ($(CONFIG_CRYPTO),)
-obj-$(CONFIG_BPF_SYSCALL) += crypto.o
-endif
+obj-$(CONFIG_BPF_CRYPTO) += crypto.o
 obj-$(CONFIG_BPF_PRELOAD) += preload/
 
 obj-$(CONFIG_BPF_SYSCALL) += relo_core.o

Does this change create a mismatch with kernel/bpf/verifier.c?

The build condition for kernel/bpf/crypto.o changes from CONFIG_CRYPTO to CONFIG_BPF_CRYPTO, but kernel/bpf/verifier.c still gates the matching BTF id on CONFIG_CRYPTO:

    BTF_SET_START(rcu_protected_types)
    ...
    #ifdef CONFIG_CRYPTO
    BTF_ID(struct, bpf_crypto_ctx)
    #endif
    ...
    BTF_SET_END(rcu_protected_types)

The two conditions can now diverge:

1. CONFIG_CRYPTO=y/m but BPF_CRYPTO=n: struct bpf_crypto_ctx is defined only in kernel/bpf/crypto.c, which is no longer compiled, so resolve_btfids emits 'WARN: resolve_btfids: unresolved symbol bpf_crypto_ctx' and leaves a bogus id 0 in rcu_protected_types.

2. BPF_CRYPTO=y but CONFIG_CRYPTO=n: crypto.o and its kfuncs are built, but the BTF_ID is compiled out, so bpf_crypto_ctx is not in rcu_protected_types. rcu_protected_object() then returns false, and btf_ld_kptr_type() tags a bpf_crypto_ctx kptr loaded from a map as PTR_UNTRUSTED instead of MEM_RCU. bpf_crypto_ctx_acquire() and bpf_crypto_encrypt/decrypt then reject that pointer, so the documented create -> kptr_xchg into map -> acquire pattern stops verifying.

Should verifier.c's #ifdef be updated to CONFIG_BPF_CRYPTO?

Quoted 4 lines
kernel/bpf/crypto.c +0 -0
index 51f89cecefb4d..59eaa285aacc5 100644

[ ... ]

Quoted 10 lines
kernel/bpf/crypto.c +4 -2
@@ -36,94 +30,53 @@ struct bpf_crypto_params {
 	u32 authsize;
 };
 
-static LIST_HEAD(bpf_crypto_types);
-static DECLARE_RWSEM(bpf_crypto_types_sem);
+enum bpf_crypto_algo_id {
+	BPF_ALGO_AES_CBC,
+	BPF_ALGO_AES_ECB,
+};

[ ... ]

Quoted 13 lines

/**
* struct bpf_crypto_ctx - refcounted BPF crypto context structure
- * @type: The pointer to bpf crypto type
- * @tfm: The pointer to instance of crypto API struct.
- * @siv_len: Size of IV and state storage for cipher
+ * @algo: The crypto algorithm ID
+ * @key: The crypto key
* @rcu: The RCU head used to free the crypto context with RCU safety.
* @usage: Object reference counter. When the refcount goes to 0, the
* memory is released back to the BPF allocator, which provides
* RCU safety.
*/
struct bpf_crypto_ctx {

kernel/bpf/crypto.c struct bpf_crypto_params { · line 78

Quoted 10 lines
kernel/bpf/crypto.c +4 -3
-	const struct bpf_crypto_type *type;
-	void *tfm;
-	u32 siv_len;
+	enum bpf_crypto_algo_id algo;
+	union {
+		struct aes_key aes;
+	} key;
 	struct rcu_head rcu;
 	refcount_t usage;
 };

Does moving the AES key into struct bpf_crypto_ctx make it readable by BPF programs?

Previously the key lived inside the crypto_lskcipher tfm, reachable only through void *tfm. A BPF program holding the ctx could read that field's numeric value but had no way to dereference it (void * yields a scalar, and bpf_probe_read_kernel() is gated behind perfmon_capable() and is not available to tc/xdp/sk_skb program types).

Now the expanded key is a field of the struct: key.aes is a struct aes_key, whose struct aes_enckey prefix contains union aes_enckey_arch k (u32 rndkeys[AES_MAX_KEYLENGTH_U32], include/crypto/aes.h:97-116). The AES key schedule is trivially invertible, so reading those words recovers the original key.

The read is permitted by the verifier. bpf_crypto_ctx_create() is KF_ACQUIRE, so R0 is PTR_TO_BTF_ID for struct bpf_crypto_ctx, and struct bpf_crypto_ctx is in vmlinux BTF (it is named in BTF_ID(struct, bpf_crypto_ctx) for the dtor list). In check_ptr_to_btf_access() the program-type hook that could restrict access is applied to writes only:

kernel/bpf/verifier.c:6124

    if (env->ops->btf_struct_access && !type_is_alloc(reg->type) &&
        atype == BPF_WRITE) {
        ...
        ret = env->ops->btf_struct_access(...);
    } else {
        ...
        ret = btf_struct_access(...);
    }

So a BPF_READ of ctx->key.aes.k.rndkeys[i] goes through the default btf_struct_access() (kernel/bpf/btf.c:7410), which resolves scalar/array members and permits the load. No field of bpf_crypto_ctx is marked opaque.

Concrete path: bpf_crypto_ctx_create()'s kernel-doc states the ctx "must either be embedded in a map as a kptr, or freed with bpf_crypto_ctx_release()". Program A (holding the secret key) creates the ctx and stores it as a kptr in a pinned map; program B (e.g. SEC("tc"), needing only CAP_BPF+CAP_NET_ADMIN, as in tools/testing/selftests/bpf/progs/crypto_sanity.c:80) obtains it with bpf_crypto_ctx_acquire() and reads the round keys out into a ringbuf.

Before this patch B could only use the ctx to encrypt/decrypt; it could not extract the key. The commit message does not mention key storage or BTF visibility. If the exposure is intentional it deserves a sentence in the changelog; otherwise the key needs to live behind an indirection (e.g. a separately-allocated struct pointed to by a void *) as it effectively did before.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35300286004