bpf: crypto: Use AES-CBC and AES-ECB libraries
Subsystems (6)
PATCH v4 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 v4:
- Switch back to the embedded struct used in v1 and v2.
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 | 288 +++++++++++++---------------- kernel/bpf/verifier.c | 2 +- tools/testing/selftests/bpf/config | 2 +- 9 files changed, 136 insertions(+), 281 deletions(-) delete mode 100644 crypto/bpf_crypto_skcipher.c delete mode 100644 include/linux/bpf_crypto.h
MAINTAINERS +0 -2
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
kernel/bpf/verifier.c +1 -1
@@ -4669,7 +4669,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
@@ -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: 91f8613d95ad8cd99d8baf094806d1ef98bc6380
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: BPF_CRYPTO is silently disabled if AES libraries are modules --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: BPF_CRYPTO is silently disabled if AES libraries are modules --
commit 1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>
bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new BPF_CRYPTO Kconfig option to conditionally compile crypto.c based on the new AES library dependencies.
kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disable BPF_CRYPTO if the AES libraries are built as modules?
BPF_CRYPTO is defined as a bool in Kconfig but depends on CRYPTO_LIB_AES_CBC and CRYPTO_LIB_AES_ECB, which are tristate symbols. Under Kconfig rules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.
Consequently, kernel/bpf/crypto.c might not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.
Quoted 3 lines
kernel/bpf/Kconfig +3
-0+ help + Provide the kfuncs needed for BPF programs to encrypt and decrypt + data. The supported algorithms are AES-CBC and AES-ECB.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.
The bot is correct. Looks like a regression.
Quoted 37 lines · Alexei Starovoitov
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.
Quoted 7 lines
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.The bot is correct. Looks like a regression.
Users can enable the libraries indirectly by setting CONFIG_CRYPTO_AES=y, CONFIG_CRYPTO_ECB=y, and CONFIG_CRYPTO_CBC=y in their kconfig, as the self-tests config does.
I do not know what you expect. The libraries themselves do not contain independent functionality (besides functions that other things in the kernel can call) and thus are hidden symbols themselves, as per the usual convention in the kernel.
As I said on v1, if you want a prompt for BPF_CRYPTO, I can add that. I can't find any other example of kfuncs having prompts, though.
It is really up to the people who actually want these kfuncs to say what they actually expect them to do and how they expect to enable them. It has been really hard to get that information, unfortunately.
- Eric
Quoted 50 lines · Eric Biggers
Quoted 37 lines · Alexei Starovoitov
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.Quoted 7 lines
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.The bot is correct. Looks like a regression.
Users can enable the libraries indirectly by setting
CONFIG_CRYPTO_AES=y,CONFIG_CRYPTO_ECB=y, andCONFIG_CRYPTO_CBC=yin their kconfig, as the self-tests config does.
Quoted 7 lines
I do not know what you expect. The libraries themselves do not contain independent functionality (besides functions that other things in the kernel can call) and thus are hidden symbols themselves, as per the usual convention in the kernel.
As I said on v1, if you want a prompt for
BPF_CRYPTO, I can add that. I can't find any other example of kfuncs having prompts, though.
No. prompt is not necessary.
My question is why disable BPF_CRYPTO when these are modules?
Quoted 56 lines · Alexei Starovoitov
Quoted 55 lines · Eric Biggers
Quoted 37 lines · Alexei Starovoitov
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.Quoted 7 lines
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.The bot is correct. Looks like a regression.
Users can enable the libraries indirectly by setting
CONFIG_CRYPTO_AES=y,CONFIG_CRYPTO_ECB=y, andCONFIG_CRYPTO_CBC=yin their kconfig, as the self-tests config does.I do not know what you expect. The libraries themselves do not contain independent functionality (besides functions that other things in the kernel can call) and thus are hidden symbols themselves, as per the usual convention in the kernel.
Quoted 5 lines
Quoted 2 lines
As I said on v1, if you want a prompt for
BPF_CRYPTO, I can add that. I can't find any other example of kfuncs having prompts, though.No. prompt is not necessary.
My question is why disableBPF_CRYPTOwhen these are modules?
If libaes is built as a module, then either bpf_crypto would have to be built as its own module (which we already ruled out on the last thread), or else this code would have to be merged into libaes. From my perspective putting this functionality in libaes seems weird because this acts more like a user of the crypto code than part of it. But maybe it would be more aligned with how kfuncs are usually implemented?
Note that it's kind of hard to actually build a kernel with libaes as a module anyway, though, due to so many things needing AES support.
- Eric
Quoted 70 lines · Eric Biggers
Quoted 56 lines · Alexei Starovoitov
Quoted 55 lines · Eric Biggers
Quoted 37 lines · Alexei Starovoitov
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.Quoted 7 lines
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.The bot is correct. Looks like a regression.
Users can enable the libraries indirectly by setting
CONFIG_CRYPTO_AES=y,CONFIG_CRYPTO_ECB=y, andCONFIG_CRYPTO_CBC=yin their kconfig, as the self-tests config does.I do not know what you expect. The libraries themselves do not contain independent functionality (besides functions that other things in the kernel can call) and thus are hidden symbols themselves, as per the usual convention in the kernel.
Quoted 5 lines
Quoted 2 lines
As I said on v1, if you want a prompt for
BPF_CRYPTO, I can add that. I can't find any other example of kfuncs having prompts, though.No. prompt is not necessary.
My question is why disableBPF_CRYPTOwhen these are modules?If libaes is built as a module, then either
bpf_cryptowould have to be built as its own module (which we already ruled out on the last thread), or else this code would have to be merged into libaes. From my perspective putting this functionality in libaes seems weird because this acts more like a user of the crypto code than part of it. But maybe it would be more aligned with how kfuncs are usually implemented?
Quoted 2 lines
Note that it's kind of hard to actually build a kernel with libaes as a module anyway, though, due to so many things needing AES support.
I'm still missing why it's hard to make this kfuncs work with CONFIG_CRYPTO_AES=m
Quoted 71 lines · Alexei Starovoitov
Quoted 70 lines · Eric Biggers
Quoted 56 lines · Alexei Starovoitov
Quoted 55 lines · Eric Biggers
Quoted 37 lines · Alexei Starovoitov
Quoted 36 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf:BPF_CRYPTOis silently disabled if AES libraries are modules --commit
1bcee0286f1c7c6c86befd107081ebb8a0fb0eed
Author: Eric Biggers <[email protected]>bpf: crypto: Use AES-CBC and AES-ECB libraries
This commit reimplements BPF crypto using the AES-CBC and AES-ECB library APIs instead of the lskcipher API. It adds a new
BPF_CRYPTOKconfigoption to conditionally compilecrypto.cbased on the new AES library dependencies.kernel/bpf/Kconfig config BPF_UNPRIV_DEFAULT_OFF · line 98
Quoted 13 lines
kernel/bpf/Kconfig +5
-0@@ -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]
Does this dependency logic silently disableBPF_CRYPTOif the AES libraries are built as modules?
BPF_CRYPTOis defined as a bool inKconfigbut depends onCRYPTO_LIB_AES_CBCandCRYPTO_LIB_AES_ECB, which are tristate symbols. UnderKconfigrules, when a bool depends on a tristate that evaluates to m, the dependency evaluates to m, which downgrades the bool to n.Quoted 7 lines
Quoted 5 lines
Consequently,
kernel/bpf/crypto.cmight not be compiled, and the crypto kfuncs could be silently stripped from the kernel. Existing BPF programs using crypto kfuncs will fail to load with "unknown kfunc". Because the AES library symbols lack user prompts, users cannot manually fix this by explicitly setting them to =y in menuconfig.The bot is correct. Looks like a regression.
Users can enable the libraries indirectly by setting
CONFIG_CRYPTO_AES=y,CONFIG_CRYPTO_ECB=y, andCONFIG_CRYPTO_CBC=yin their kconfig, as the self-tests config does.I do not know what you expect. The libraries themselves do not contain independent functionality (besides functions that other things in the kernel can call) and thus are hidden symbols themselves, as per the usual convention in the kernel.
Quoted 5 lines
Quoted 2 lines
As I said on v1, if you want a prompt for
BPF_CRYPTO, I can add that. I can't find any other example of kfuncs having prompts, though.No. prompt is not necessary.
My question is why disableBPF_CRYPTOwhen these are modules?If libaes is built as a module, then either
bpf_cryptowould have to be built as its own module (which we already ruled out on the last thread), or else this code would have to be merged into libaes. From my perspective putting this functionality in libaes seems weird because this acts more like a user of the crypto code than part of it. But maybe it would be more aligned with how kfuncs are usually implemented?
Quoted 4 lines
Quoted 2 lines
Note that it's kind of hard to actually build a kernel with libaes as a module anyway, though, due to so many things needing AES support.
I'm still missing why it's hard to make this kfuncs work with
CONFIG_CRYPTO_AES=m
Module code can only be called directly by module code, not built-in code. So CONFIG_CRYPTO_LIB_AES=m && CONFIG_BPF_CRYPTO=y cannot work; you'd get a linker error. For a module to offer these kfuncs they would either need to be their own module (e.g. bpf_crypto.ko), or else merged into the module of the underlying functions (libaes.ko, though again, in practice libaes is usually built-in anyway for other reasons).
- Eric
Quoted 7 lines · Eric Biggers
Module code can only be called directly by module code, not built-in code. So
CONFIG_CRYPTO_LIB_AES=m&&CONFIG_BPF_CRYPTO=ycannot work; you'd get a linker error. For a module to offer these kfuncs they would either need to be their own module (e.g. bpf_crypto.ko), or else merged into the module of the underlying functions (libaes.ko, though again, in practice libaes is usually built-in anyway for other reasons).
ok. I feel it's better to go back to v1 approach:
select CRYPTO_LIB_AES_CBC
select CRYPTO_LIB_AES_ECB
with no prompt.
That will keep kfuncs on every BPF_SYSCALL kernel without a bpf_crypto.ko and without moving the kfuncs into libaes.
Another alternative is:
depends on CRYPTO_LIB_AES=y && CRYPTO_LIB_AES_CBC && CRYPTO_LIB_AES_ECB but select is imo better.
[email protected]
AI reviewThis isn't a bug, but struct aes_enckey pads itself so the round keys can be 16-byte aligned for better performance (as documented in include/crypto/aes.h). Putting the 4-byte algo enum before the…
Quoted 4 lines
kernel/bpf/crypto.c +0
-0
[ ... ]
Quoted 16 lines
kernel/bpf/crypto.c +12
-0@@ -36,94 +30,53 @@ struct bpf_crypto_params {u32 authsize; }; +enum bpf_crypto_algo_id { +BPF_ALGO_AES_CBC , +BPF_ALGO_AES_ECB , +}; + +/** +* struct bpf_crypto_ctx - refcounted BPF crypto context structure +* @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
kernel/bpf/crypto.c struct bpf_crypto_params { · line 54
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+* RCU safety. +*/ +struct bpf_crypto_ctx { +enum bpf_crypto_algo_id algo; +union { +struct aes_key aes; + } key; +struct rcu_head rcu; +refcount_t usage; +};
This isn't a bug, but struct aes_enckey pads itself so the round keys can be 16-byte aligned for better performance (as documented in include/crypto/aes.h). Putting the 4-byte algo enum before the key union means key.aes lands at offset 4, so the round keys can never hit that alignment. Would putting key first (or aligning it) be worth considering here?
[ ... ]
Quoted 19 lines
kernel/bpf/crypto.c +19
-0+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) +{
kernel/bpf/crypto.c __bpf_kfunc void bpf_crypto_ctx_release_dtor(void *ctx) · line 226
Quoted 10 lines
kernel/bpf/crypto.c +10
-0+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 ; +}
This isn't a bug, but both helpers repeat the same src_len % AES_BLOCK_SIZE and dst_len < src_len checks. Could that validation stay in bpf_crypto_crypt() next to the existing length checks, leaving only the iv_len check per mode?
Also, bpf_aes_ecb_crypt() takes an iv parameter but never uses it (only iv_len is checked). Would it read more clearly to drop the iv argument from this function, or drop both iv and iv_len and do the iv_len != 0 check in the ECB arm of bpf_crypto_crypt()?
---
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/35815287581
Quoted 6 lines · On Wed, Sep 23, 2026 at 04:22:57AM +0000, [email protected] wrote:
This isn't a bug, but struct
aes_enckeypads itself so the round keys can be 16-byte aligned for better performance (as documented ininclude/crypto/aes.h). Putting the 4-byte algo enum before the key union means key.aes lands at offset 4, so the round keys can never hit that alignment. Would putting key first (or aligning it) be worth considering here?
Sure, but it's very much bikeshedding. If someone cared about performance enough to be micro-optimizing the data alignment here, they would also have noticed than the optimized implementations of AES modes aren't actually being used, which is way, *way* more important. Coincidentally, I'm addressing that in a separate series
(https://lore.kernel.org/linux-crypto/[email protected]/)...
I would not consider it worth worrying about the alignment here.
Quoted 4 lines
This isn't a bug, but both helpers repeat the same
src_len%AES_BLOCK_SIZEanddst_len<src_lenchecks. Could that validation stay inbpf_crypto_crypt()next to the existing length checks, leaving only theiv_lencheck per mode?
It's done the way it is because it makes it more straightforward to add AES-GCM support, which is desired as per previous discussions.
Anyway, these random bpf-ci bot comments on every version are getting a bit annoying. Especially when it explicitly says "This isn't a bug", and then the instructions at the end of the email assume it is.
- Eric