Skip to contentSkip to conversation

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

PATCH Superseded Eric Biggers 3 participants 6 messages Aug 31

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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", "cbc(aes)", "ecb(aes)", 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 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.

---
 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          | 271 +++++++++++++++--------------------
 7 files changed, 122 insertions(+), 274 deletions(-)
 delete mode 100644 crypto/bpf_crypto_skcipher.c
 delete mode 100644 include/linux/bpf_crypto.h
MAINTAINERS +0 -2
index 3a19da74d00c..16f13270ce1f 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 eb3de35734f0..822599b42be2 100644
@@ -87,6 +87,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
+	select CRYPTO_LIB_AES_CBC
+	select 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 90255d80e5be..ab2e5d2782e6 100644
@@ -55,9 +55,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 +112 -159

391 lines not on this page · raw message

base-commit: cee9395acd8043be0644b25c34bfa86623f2b935

Signature · 1 line
2.55.0
#1 lore raw
The claim that lskcipher supports only these three specific algorithm names is not accurate. The cbc and ecb templates in crypto/cbc.c and crypto/ecb.c instantiate lskcipher interfaces for any plain…
Quoted 3 lines

BPF crypto was implemented using the lskcipher API, which doesn't seem to be going anywhere. lskcipher supports only "arc4", "cbc(aes)", "ecb(aes)", and only with unoptimized implementations.

The claim that lskcipher supports only these three specific algorithm names is not accurate. The cbc and ecb templates in crypto/cbc.c and crypto/ecb.c instantiate lskcipher interfaces for any plain cipher algorithm, not just AES:

crypto/cbc.c:crypto_cbc_create() calls lskcipher_alloc_instance_simple() crypto/ecb.c:crypto_ecb_create() calls lskcipher_alloc_instance_simple()

So cbc(<any-cipher>) and ecb(<any-cipher>) are valid lskcipher algorithms. The kernel includes about 14 plain cipher algorithms besides AES, including sm4, des, des3_ede, camellia, twofish, aria, blowfish, cast5, cast6, serpent, seed, khazad, anubis, tea, and xtea.

The pre-patch code passed params->algo directly to crypto_alloc_lskcipher() with no whitelist, so a BPF program could use cbc(sm4), cbc(des3_ede), ecb(camellia), or ecb(twofish).

After this patch, bpf_crypto_find_algo() only matches the two-entry table with cbc(aes) and ecb(aes), so all non-AES cbc/ecb modes now fail.

The changelog justifies dropping ARC4 but does not mention that this also drops support for all non-AES block ciphers. Should the commit message explicitly state that non-AES cbc()/ecb() support is being removed, or should the characterization of lskcipher's scope be corrected?

Additionally, the error code for an unsupported algorithm changes from -EOPNOTSUPP (old has_algo failure path) to -ENOENT (new bpf_crypto_find_algo() path), which is a visible behavior change for existing BPF programs that inspect errno.

[ ... ]

Quoted 7 lines
kernel/bpf/Kconfig +0 -0
index eb3de35734f09..822599b42be2e 100644
@@ -87,6 +87,15 @@ config BPF_UNPRIV_DEFAULT_OFF
 
 	  If you are unsure how to answer this question, answer Y.

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 10 lines
kernel/bpf/Kconfig +9 -0
 
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	select CRYPTO_LIB_AES_CBC
+	select 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.
+

The def_bool y makes BPF_CRYPTO invisible and unconditionally enabled whenever CONFIG_BPF_SYSCALL=y, so the AES library code is always linked with no opt-out.

Before this patch, kernel/bpf/Makefile gated crypto.o on:

  ifneq ($(CONFIG_CRYPTO),)
  obj-$(CONFIG_BPF_SYSCALL) += crypto.o
  endif

so a CONFIG_BPF_SYSCALL=y, CONFIG_CRYPTO=n kernel previously built neither the BPF crypto kfuncs nor any AES code. Now the AES key-schedule, CBC, and ECB library code is unconditionally present.

This is particularly notable given that the commit message says AES-ECB "isn't appropriate in new systems and should be dropped" yet it is now unconditionally linked into every BPF_SYSCALL kernel.

Should this be a user-selectable option with a prompt like "bool 'BPF crypto kfuncs'" so it can be disabled, or is the intent really to make it always-on?

[ ... ]

Quoted 6 lines
crypto/bpf_crypto_skcipher.c deleted +0 -0
deleted file mode 100644
index a88798d3e8c87..0000000000000
@@ -1,83 +0,0 @@

[ ... ]

crypto/bpf_crypto_skcipher.c line 11

Quoted 4 lines
crypto/bpf_crypto_skcipher.c deleted +0 -4
-static void *bpf_crypto_lskcipher_alloc_tfm(const char *algo)
-{
-	return crypto_alloc_lskcipher(algo, 0, 0);
-}

Deleting this file removes the last in-tree caller of crypto_alloc_lskcipher(). After this patch, the symbol only exists at its definition and export in crypto/lskcipher.c, with no remaining callers anywhere in the tree.

Given the premise that the lskcipher API "doesn't seem to be going anywhere," should crypto_alloc_lskcipher() be removed in a follow-up, or left as an exported API with zero users?

[ ... ]

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

[ ... ]

Quoted 12 lines
kernel/bpf/crypto.c +0 -1
@@ -146,7 +99,6 @@ __bpf_kfunc struct bpf_crypto_ctx *
 bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz,
 		      int *err)
 {
-	const struct bpf_crypto_type *type;
 	struct bpf_crypto_ctx *ctx;
 
 	if (!params || params->reserved[0] || params->reserved[1] ||
@@ -155,69 +107,40 @@ bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz,
 		return NULL;
 	}
 

[ ... ]

Quoted 26 lines
kernel/bpf/crypto.c +26 -0
+	ctx = kzalloc_obj(*ctx);
+	if (!ctx) {
+		*err = -ENOMEM;
+		return NULL;
+	}
+
+	if (!bpf_crypto_find_algo(params, &ctx->algo)) {
+		*err = -ENOENT;
+		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(1);
+		*err = -ENOENT;
+		break;
+	}
+

kernel/bpf/crypto.c bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz, · line 144

Quoted 10 lines
kernel/bpf/crypto.c +6 -1
+out:
+	if (*err) {
+		kfree_sensitive(ctx);
+		return NULL;
+	}
+
 	refcount_set(&ctx->usage, 1);
-
 	return ctx;
 }

Does this code have a double-fetch issue? The function now uses the caller-supplied 'int *err' out-parameter as its own internal success
flag: every failure path stores into *err, then the 'if (*err)' at the 'out:' label re-reads it to decide whether to free ctx and fail.

The 'err' parameter is classified by the verifier as KF_ARG_PTR_TO_MEM, so a BPF program can legally pass a pointer into a shared map value rather than its own stack. That makes the store followed by the reload a double fetch of concurrently-writable memory.

The pre-patch version never read *err - it used distinct error labels and returned NULL unconditionally on error, so the failure decision could not be influenced by the caller.

If the reload observes 0 because another CPU zeroed the map value between the store and the load, the function takes the success path for a create that actually failed. For instance, if aes_preparekey() returns -EINVAL from aes_check_keylen() before assigning enc_key->len and enc_key->nrounds, the kzalloc'ed key stays all-zero with nrounds == 0.

A later bpf_crypto_encrypt()/bpf_crypto_decrypt() on that ctx reaches aes_encrypt_generic()/aes_decrypt_generic(), which compute 'int n = nrounds - 1' (= -1) and then run 'do { ... } while (--n);', reading 16 bytes past the round-key pointer per iteration for roughly 2^32 iterations - an unbounded out-of-bounds read and unrecoverable soft lockup.

The reload can also mis-handle the -ENOENT path from bpf_crypto_find_algo(), returning a ctx whose algo is uninitialized (BPF_ALGO_AES_CBC == 0) with no key.

Could this be fixed by using a local 'int ret' for the internal flow and assigning '*err = ret' exactly once?

[ ... ]

kernel/bpf/crypto.c struct bpf_crypto_ctx { · line 84

Quoted 3 lines
kernel/bpf/crypto.c +0 -0
@@ -86,23 +83,17 @@ struct bpf_crypto_ctx {
  * As crypto API functions use GFP_KERNEL allocations, this function can
  * only be used in sleepable BPF programs.

The kernel-doc for bpf_crypto_ctx_create() still justifies the KF_SLEEPABLE requirement with "As crypto API functions use GFP_KERNEL allocations."

This patch removes every crypto API call from the function (crypto_alloc_lskcipher()/crypto_lskcipher_setkey() are gone). The body now only does kzalloc_obj(), bpf_crypto_find_algo(), and aes_preparekey(), and the AES library is documented as usable in "Any context".

The conclusion still holds because kzalloc_obj() uses GFP_KERNEL, not because of the crypto API. Could the comment be updated to reflect the actual reason for the sleepable restriction after this refactoring?

---
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/33431341694

#4 lore raw
In reply to Eric Biggers
Quoted 5 lines · Eric Biggers

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 relates to my recent patch fixing state preservation in lskcipher's unaligned path, which exercised ARC4 through BPF:

https://lore.kernel.org/r/[email protected]

While looking at your patch, I tested the actual BPF-visible algorithm surface. The ECB and CBC templates allow considerably more than AES.

I compared:

A: cee9395acd80 (v7.3-rc1)
B: cee9395acd80 plus this patch

both used the same arm64 QEMU configuration and the same BPF/userspace test artifacts. Each request was exactly one cipher block.

"OK" means context creation plus BPF encrypt/decrypt round-trip succeeded.

Algorithm Bytes A B
--------------------------------------------------------

ecb(aes)                         16  OK         OK
cbc(aes)                         16  OK         OK
ecb(aes-lib)                     16  OK         -ENOENT
cbc(aes-lib)                     16  OK         -ENOENT
ecb(anubis)                      16  OK         -ENOENT
cbc(anubis)                      16  OK         -ENOENT
ecb(anubis-generic)              16  OK         -ENOENT
cbc(anubis-generic)              16  OK         -ENOENT
ecb(aria)                        16  OK         -ENOENT
cbc(aria)                        16  OK         -ENOENT
ecb(aria-generic)                16  OK         -ENOENT
cbc(aria-generic)                16  OK         -ENOENT
ecb(blowfish)                     8  OK         -ENOENT
cbc(blowfish)                     8  OK         -ENOENT
ecb(blowfish-generic)             8  OK         -ENOENT
cbc(blowfish-generic)             8  OK         -ENOENT
ecb(camellia)                    16  OK         -ENOENT
cbc(camellia)                    16  OK         -ENOENT
ecb(camellia-generic)            16  OK         -ENOENT
cbc(camellia-generic)            16  OK         -ENOENT
ecb(cast5)                        8  OK         -ENOENT
cbc(cast5)                        8  OK         -ENOENT
ecb(cast5-generic)                8  OK         -ENOENT
cbc(cast5-generic)                8  OK         -ENOENT
ecb(cast6)                       16  OK         -ENOENT
cbc(cast6)                       16  OK         -ENOENT
ecb(cast6-generic)               16  OK         -ENOENT
cbc(cast6-generic)               16  OK         -ENOENT
ecb(des)                          8  OK         -ENOENT
cbc(des)                          8  OK         -ENOENT
ecb(des-generic)                  8  OK         -ENOENT
cbc(des-generic)                  8  OK         -ENOENT
ecb(des3_ede)                     8  OK         -ENOENT
cbc(des3_ede)                     8  OK         -ENOENT
ecb(des3_ede-generic)             8  OK         -ENOENT
cbc(des3_ede-generic)             8  OK         -ENOENT
ecb(khazad)                       8  OK         -ENOENT
cbc(khazad)                       8  OK         -ENOENT
ecb(khazad-generic)               8  OK         -ENOENT
cbc(khazad-generic)               8  OK         -ENOENT
ecb(seed)                        16  OK         -ENOENT
cbc(seed)                        16  OK         -ENOENT
ecb(seed-generic)                16  OK         -ENOENT
cbc(seed-generic)                16  OK         -ENOENT
ecb(serpent)                     16  OK         -ENOENT
cbc(serpent)                     16  OK         -ENOENT
ecb(serpent-generic)             16  OK         -ENOENT
cbc(serpent-generic)             16  OK         -ENOENT
ecb(sm4)                         16  OK         -ENOENT
cbc(sm4)                         16  OK         -ENOENT
ecb(sm4-generic)                 16  OK         -ENOENT
cbc(sm4-generic)                 16  OK         -ENOENT
ecb(tea)                          8  OK         -ENOENT
cbc(tea)                          8  OK         -ENOENT
ecb(tea-generic)                  8  OK         -ENOENT
cbc(tea-generic)                  8  OK         -ENOENT
ecb(xtea)                         8  OK         -ENOENT
cbc(xtea)                         8  OK         -ENOENT
ecb(xtea-generic)                 8  OK         -ENOENT
cbc(xtea-generic)                 8  OK         -ENOENT
ecb(xeta)                         8  OK         -ENOENT
cbc(xeta)                         8  OK         -ENOENT
ecb(xeta-generic)                 8  OK         -ENOENT
cbc(xeta-generic)                 8  OK         -ENOENT
ecb(twofish)                     16  OK         -ENOENT
cbc(twofish)                     16  OK         -ENOENT
ecb(twofish-generic)             16  OK         -ENOENT
cbc(twofish-generic)             16  OK         -ENOENT
arc4                              1  OK         -ENOENT
arc4-generic                      1  OK         -ENOENT
ecb(arc4)                         1  OK         -ENOENT
ecb(arc4-generic)                 1  OK         -ENOENT

This leaves only 2 of the 72 tested algorithm names: the other 70, covering AES aliases, non-AES block ciphers, and ARC4, now return -ENOENT.

This table describes the BPF-visible support removed by this patch. That may well be worthwhile cleanup, but it is still the removal of existing, likely only theoretical, support.

Thanks,
Karl

#5 lore raw
Quoted 7 lines · Karl Mehltretter
Quoted 5 lines · Eric Biggers

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.

Quoted 7 lines

This relates to my recent patch fixing state preservation in lskcipher's unaligned path, which exercised ARC4 through BPF:

https://lore.kernel.org/r/[email protected]

While looking at your patch, I tested the actual BPF-visible algorithm surface. The ECB and CBC templates allow considerably more than AES.

Right, apparently any "crypto_cipher" can be composed with "ecb" or "cbc" as an "lskcipher". I think in this case it's only of theoretical interest and is a bug, not a feature, though. Especially given the presence of ARC4, DES, TEA, etc. on that list.

We see this a lot with the "Crypto API", where a new kernel feature actually uses one or two algorithms, then unnecessarily allows every single algorithm to be theoretically reachable (including insecure, obsolete, or nonsense options) just because the API takes a string.

- Eric

#2 lore raw
In reply to [email protected]
Quoted 17 lines · On Mon, Aug 31, 2026 at 08:29:02PM +0000, [email protected] wrote:

The claim that lskcipher supports only these three specific algorithm names is not accurate. The cbc and ecb templates in crypto/cbc.c and crypto/ecb.c instantiate lskcipher interfaces for any plain cipher algorithm, not just AES:

crypto/cbc.c:crypto_cbc_create() calls lskcipher_alloc_instance_simple() crypto/ecb.c:crypto_ecb_create() calls lskcipher_alloc_instance_simple()

So cbc(<any-cipher>) and ecb(<any-cipher>) are valid lskcipher algorithms. The kernel includes about 14 plain cipher algorithms besides AES, including sm4, des, des3_ede, camellia, twofish, aria, blowfish, cast5, cast6, serpent, seed, khazad, anubis, tea, and xtea.

The pre-patch code passed params->algo directly to crypto_alloc_lskcipher() with no whitelist, so a BPF program could use cbc(sm4), cbc(des3_ede), ecb(camellia), or ecb(twofish).

Quoted 8 lines

After this patch, bpf_crypto_find_algo() only matches the two-entry table with cbc(aes) and ecb(aes), so all non-AES cbc/ecb modes now fail.

The changelog justifies dropping ARC4 but does not mention that this also drops support for all non-AES block ciphers. Should the commit message explicitly state that non-AES cbc()/ecb() support is being removed, or should the characterization of lskcipher's scope be corrected?

I'll update the commit message to clarify that other block ciphers are dropped as well.

Quoted 4 lines

Additionally, the error code for an unsupported algorithm changes from -EOPNOTSUPP (old has_algo failure path) to -ENOENT (new bpf_crypto_find_algo() path), which is a visible behavior change for existing BPF programs that inspect errno.

I'll change it to EOPNOTSUPP.

Quoted 24 lines

kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98

Quoted 9 lines
kernel/bpf/Kconfig +9 -0
+config BPF_CRYPTO
+	def_bool y
+	depends on BPF_SYSCALL
+	select CRYPTO_LIB_AES_CBC
+	select 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.
+

The def_bool y makes BPF_CRYPTO invisible and unconditionally enabled whenever CONFIG_BPF_SYSCALL=y, so the AES library code is always linked with no opt-out.

Before this patch, kernel/bpf/Makefile gated crypto.o on:

  ifneq ($(CONFIG_CRYPTO),)
  obj-$(CONFIG_BPF_SYSCALL) += crypto.o
  endif

so a CONFIG_BPF_SYSCALL=y, CONFIG_CRYPTO=n kernel previously built neither the BPF crypto kfuncs nor any AES code. Now the AES key-schedule, CBC, and ECB library code is unconditionally present.

Quoted 7 lines

This is particularly notable given that the commit message says AES-ECB "isn't appropriate in new systems and should be dropped" yet it is now unconditionally linked into every BPF_SYSCALL kernel.

Should this be a user-selectable option with a prompt like "bool 'BPF crypto kfuncs'" so it can be disabled, or is the intent really to make it always-on?

Almost every kernel has CRYPTO and CRYPTO_SKCIPHER2 enabled anyway, and CRYPTO_SKCIPHER2 also enables ECB support. So the change isn't actually as drastic as the bot implies, but I'd be glad to give BPF_CRYPTO a prompt if the BPF folks are okay with that.

Quoted 8 lines

Deleting this file removes the last in-tree caller of crypto_alloc_lskcipher(). After this patch, the symbol only exists at its definition and export in crypto/lskcipher.c, with no remaining callers anywhere in the tree.

Given the premise that the lskcipher API "doesn't seem to be going anywhere," should crypto_alloc_lskcipher() be removed in a follow-up, or left as an exported API with zero users?

Yes, lskcipher should be deleted in a follow-up series.

Quoted 27 lines

Does this code have a double-fetch issue? The function now uses the caller-supplied 'int *err' out-parameter as its own internal success
flag: every failure path stores into *err, then the 'if (*err)' at the 'out:' label re-reads it to decide whether to free ctx and fail.

The 'err' parameter is classified by the verifier as KF_ARG_PTR_TO_MEM, so a BPF program can legally pass a pointer into a shared map value rather than its own stack. That makes the store followed by the reload a double fetch of concurrently-writable memory.

The pre-patch version never read *err - it used distinct error labels and returned NULL unconditionally on error, so the failure decision could not be influenced by the caller.

If the reload observes 0 because another CPU zeroed the map value between the store and the load, the function takes the success path for a create that actually failed. For instance, if aes_preparekey() returns -EINVAL from aes_check_keylen() before assigning enc_key->len and enc_key->nrounds, the kzalloc'ed key stays all-zero with nrounds == 0.

A later bpf_crypto_encrypt()/bpf_crypto_decrypt() on that ctx reaches aes_encrypt_generic()/aes_decrypt_generic(), which compute 'int n = nrounds - 1' (= -1) and then run 'do { ... } while (--n);', reading 16 bytes past the round-key pointer per iteration for roughly 2^32 iterations - an unbounded out-of-bounds read and unrecoverable soft lockup.

Quoted 6 lines

The reload can also mis-handle the -ENOENT path from bpf_crypto_find_algo(), returning a ctx whose algo is uninitialized (BPF_ALGO_AES_CBC == 0) with no key.

Could this be fixed by using a local 'int ret' for the internal flow and assigning '*err = ret' exactly once?

I'll change it to use a local variable.

Quoted 14 lines

Review struct bpf_crypto_ctx { · line 84

Quoted 3 lines
(quoted diff) +0 -0
@@ -86,23 +83,17 @@ struct bpf_crypto_ctx {
  * As crypto API functions use GFP_KERNEL allocations, this function can
  * only be used in sleepable BPF programs.

The kernel-doc for bpf_crypto_ctx_create() still justifies the KF_SLEEPABLE requirement with "As crypto API functions use GFP_KERNEL allocations."

This patch removes every crypto API call from the function (crypto_alloc_lskcipher()/crypto_lskcipher_setkey() are gone). The body now only does kzalloc_obj(), bpf_crypto_find_algo(), and aes_preparekey(), and the AES library is documented as usable in "Any context".

Quoted 3 lines

The conclusion still holds because kzalloc_obj() uses GFP_KERNEL, not because of the crypto API. Could the comment be updated to reflect the actual reason for the sleepable restriction after this refactoring?

I'll update the comment.

- Eric

#3 lore raw
Quoted 4 lines · Eric Biggers

Almost every kernel has CRYPTO and CRYPTO_SKCIPHER2 enabled anyway, and CRYPTO_SKCIPHER2 also enables ECB support. So the change isn't actually as drastic as the bot implies, but I'd be glad to give BPF_CRYPTO a prompt if the BPF folks are okay with that.

I don't see other examples of kfuncs with visible kconfig options. The convention is to automatically enable them when the parent subsystem is enabled.

It feels like bpf crypto is a de facto standalone feature, so I'm not sure how well that applies here. Regardless, for now I'll use "depends on CRYPTO_LIB_AES_CBC" and "depends on CRYPTO_LIB_AES_ECB".

- Eric