Skip to contentSkip to conversation

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

PATCH Superseded Eric Biggers 3 participants 5 messages Sep 22

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v3:
- Update the #ifdef in kernel/bpf/verifier.c.
- Store the key behind a void pointer so that BPF programs can't read it via BTF, as requested by the bpf-ci AI review.
- Update the selftests kconfig.
- Use WARN_ON_ONCE instead of WARN_ON.

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                | 295 +++++++++++++----------------
 kernel/bpf/verifier.c              |   2 +-
 tools/testing/selftests/bpf/config |   2 +-
 9 files changed, 143 insertions(+), 281 deletions(-)
 delete mode 100644 crypto/bpf_crypto_skcipher.c
 delete mode 100644 include/linux/bpf_crypto.h
MAINTAINERS +0 -2
index 6215fcb07770..0fcdfe48c62a 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 8386d55a9755..33bb5ad595e8 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 a41e71d4e2d9..000000000000
@@ -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 d7d25477ef48..a44ecfa3e9ef 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 9a92c348bbda..c1f9b0d3468d 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 +131 -164

426 lines not on this page · raw message

kernel/bpf/verifier.c +1 -1
index d62c0f74cff5..c6957ef12d76 100644
@@ -4664,7 +4664,7 @@ BTF_ID(struct, cgroup)
 BTF_ID(struct, bpf_cpumask)
 #endif
 BTF_ID(struct, task_struct)
-#ifdef CONFIG_CRYPTO
+#ifdef CONFIG_BPF_CRYPTO
 BTF_ID(struct, bpf_crypto_ctx)
 #endif
 #ifdef CONFIG_INET
tools/testing/selftests/bpf/config +1 -1
index d292cb60a5a4..2b883b388f90 100644
@@ -18,7 +18,7 @@ CONFIG_CRYPTO_SHA512=y
 CONFIG_CRYPTO_USER_API=y
 CONFIG_CRYPTO_USER_API_HASH=y
 CONFIG_CRYPTO_USER_API_SKCIPHER=y
-CONFIG_CRYPTO_SKCIPHER=y
+CONFIG_CRYPTO_CBC=y
 CONFIG_CRYPTO_ECB=y
 CONFIG_CRYPTO_AES=y
 CONFIG_DEBUG_INFO=y

base-commit: 79dc258c9392051420a26f1504c647bd3d27c66a

Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: BPF_CRYPTO is silently disabled on modular kernels --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: BPF_CRYPTO is silently disabled on modular kernels
--

commit 4eaa15850cd54db254c7280e28e3b2a751a2c186
Author: Eric Biggers <[email protected]>

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

This commit reimplements BPF crypto kfuncs to use the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It removes the bpf_crypto_type abstraction layer and simplifies the crypto implementation.

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 13 lines
kernel/bpf/Kconfig +5 -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

[Severity: High]
Will this silently disable BPF_CRYPTO on modular kernels?

In kernel/bpf/Kconfig, BPF_CRYPTO is defined as a boolean symbol. If the kernel is configured with CONFIG_CRYPTO_AES=m (which is common for many distribution kernels), the hidden library symbols CRYPTO_LIB_AES_CBC and CRYPTO_LIB_AES_ECB evaluate to 'm'.

Because a 'bool' config cannot be set to 'm', the 'depends on' directives here will force BPF_CRYPTO to evaluate to 'n', completely disabling it. Furthermore, because BPF_CRYPTO lacks a prompt string, users cannot see why it was disabled or enable it manually.

This causes the BPF crypto kfuncs to be silently omitted from the build, which would cause programs relying on them to fail to load.

Would it be better to conditionally select these libraries or find another way to express this requirement to ensure BPF_CRYPTO remains enabled?

Quoted 5 lines
kernel/bpf/Kconfig +4 -0
+	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"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Eric Biggers

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 5 lines · Eric Biggers
kernel/bpf/Kconfig +5 -0
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	depends on CRYPTO_LIB_AES_CBC
+	depends on CRYPTO_LIB_AES_ECB

This will break the build with CRYPTO_AES=m.

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

Quoted 6 lines

struct bpf_crypto_ctx {

kernel/bpf/crypto.c +2 -3
-	const struct bpf_crypto_type *type;
-	void *tfm;
-	u32 siv_len;
+	enum bpf_crypto_algo_id algo;
+	void *key;

No need for this. The bot was wrong.
Reading any field of struct bpf_crypto_ctx requires CAP_PERFMON.

pw-bot: cr

#3 lore raw
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 5 lines · Eric Biggers
kernel/bpf/Kconfig +5 -0
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	depends on CRYPTO_LIB_AES_CBC
+	depends on CRYPTO_LIB_AES_ECB

This will break the build with CRYPTO_AES=m.

No, the new implementation only calls library code. And if that library code isn't built-in, then this just doesn't get built at all, as per the 'depends on' lines.

I'm not sure what you expected. This could be a tristate, but that would mean it would be its own module, which doesn't seem conventional for kfuncs.

Quoted 9 lines

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

Quoted 6 lines

struct bpf_crypto_ctx {

kernel/bpf/crypto.c +2 -3
-	const struct bpf_crypto_type *type;
-	void *tfm;
-	u32 siv_len;
+	enum bpf_crypto_algo_id algo;
+	void *key;

No need for this. The bot was wrong.
Reading any field of struct bpf_crypto_ctx requires CAP_PERFMON.

Okay, it sounded like a weird BPF quirk where the type system was being used to enforce a security boundary. But if it's not needed, then that's helpful. I'll go back to the original struct embedding.

- Eric

#4 lore raw
Quoted 10 lines · Eric Biggers
Quoted 8 lines · Alexei Starovoitov

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 5 lines · Eric Biggers
kernel/bpf/Kconfig +5 -0
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	depends on CRYPTO_LIB_AES_CBC
+	depends on CRYPTO_LIB_AES_ECB

This will break the build with CRYPTO_AES=m.

Quoted 7 lines

No, the new implementation only calls library code. And if that library code isn't built-in, then this just doesn't get built at all, as per the 'depends on' lines.

I'm not sure what you expected. This could be a tristate, but that would mean it would be its own module, which doesn't seem conventional for kfuncs.

I see. peddle back. tristate is indeed overkill.

Quoted 10 lines
Quoted 9 lines

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

Quoted 6 lines

struct bpf_crypto_ctx {

kernel/bpf/crypto.c +2 -3
-	const struct bpf_crypto_type *type;
-	void *tfm;
-	u32 siv_len;
+	enum bpf_crypto_algo_id algo;
+	void *key;

No need for this. The bot was wrong.
Reading any field of struct bpf_crypto_ctx requires CAP_PERFMON.

Quoted 3 lines

Okay, it sounded like a weird BPF quirk where the type system was being used to enforce a security boundary. But if it's not needed, then that's helpful. I'll go back to the original struct embedding.

+1