lib/bpf_legacy: Use userspace SHA-1 code instead of AF_ALG
Subsystems (5)
PATCH v3 1/1 lib/bpf_legacy: Use userspace SHA-1 code instead of AF_ALG
Add a basic SHA-1 implementation to lib/, and make lib/bpf_legacy.c use it to calculate SHA-1 digests instead of the previous AF_ALG-based code.
This eliminates the dependency on AF_ALG, specifically the kernel config options CONFIG_CRYPTO_USER_API_HASH and CONFIG_CRYPTO_SHA1.
Over the years AF_ALG has been very problematic, and it is also not supported on all kernels. Escalating to the kernel's privileged execution context merely to calculate software algorithms, which can be done in userspace instead, is not something that should have ever been supported. Even on kernels that support it, the syscall overhead of AF_ALG means that it is often slower than userspace code.
Let's do the right thing here, and allow people to disable AF_ALG support (or not enable it) on systems where iproute2 is the only user.
---
Changed in v3:
- Added Alexei's ack
Changed in v2:
- Corrected error handling for when bpf_obj_hash() fails
- Added more detail to commit message
include/bpf_util.h | 5 -- include/sha1.h | 18 ++++++ include/uapi/linux/if_alg.h | 61 -------------------- lib/Makefile | 2 +- lib/bpf_legacy.c | 109 +++++++++--------------------------- lib/sha1.c | 108 +++++++++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 149 deletions(-) create mode 100644 include/sha1.h delete mode 100644 include/uapi/linux/if_alg.h create mode 100644 lib/sha1.c
include/bpf_util.h +0 -5
@@ -12,11 +12,10 @@#include <linux/bpf.h> #include <linux/btf.h> #include <linux/filter.h> #include <linux/magic.h> #include <linux/elf-em.h> -#include <linux/if_alg.h> #include "utils.h" #include "bpf_scm.h" #define BPF_ENV_UDS "TC_BPF_UDS"
@@ -38,14 +37,10 @@# define TRACEFS_MAGIC 0x74726163#endif #define TRACE_DIR_MNT "/sys/kernel/tracing" -#ifndef AF_ALG -# define AF_ALG 38 -#endif -#ifndef EM_BPF # define EM_BPF 247#endif struct bpf_cfg_ops {
include/sha1.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* SHA-1 message digest algorithm +* +* Copyright 2025 Google LLC +*/ +#ifndef __SHA1_H__ +#define __SHA1_H__ + +#include <linux/types.h> +#include <stddef.h> + +#define SHA1_DIGEST_SIZE 20 +#define SHA1_BLOCK_SIZE 64 + +void sha1 (const __u8 *data,size_t len,__u8 out[SHA1_DIGEST_SIZE ]); + +#endif /* __SHA1_H__ */
include/uapi/linux/if_alg.h deleted +0 -61
61 lines not on this page · raw message
lib/Makefile +1 -1
@@ -4,11 +4,11 @@ include ../config.mk CFLAGS += -fPIC UTILOBJ = utils.o utils_math.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o json_print.o json_print_math.o \ names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o \ - ppp_proto.o bridge.o + ppp_proto.o bridge.o sha1.o ifeq ($(HAVE_ELF),y) ifeq ($(HAVE_LIBBPF),y) UTILOBJ += bpf_libbpf.o endif
lib/bpf_legacy.c +27 -82
199 lines not on this page · raw message
lib/sha1.c new file +108 -0
108 lines not on this page · raw message
base-commit: e570876032359a388636fddd9197d4f852eba94f
Signature · 1 line
2.52.0
Quoted 10 lines · Eric Biggers
include/sha1.h new file +4
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* SHA-1 message digest algorithm +*
include/sha1.h line 14
Quoted 10 lines
include/sha1.h new file +10
-0+* Copyright 2025 Google LLC +*/ +#ifndef __SHA1_H__ +#define __SHA1_H__ + +#include <linux/types.h> +#include <stddef.h> + +#define SHA1_DIGEST_SIZE 20 +#define SHA1_BLOCK_SIZE 64
How come these are not part of the uapi?
I applied this to iproute2-next to get as much soak time as possible. Anyone using legacy bpf (added Toke in case he knows) in particular should test with top of tree.
Quoted 24 lines · David Ahern
Quoted 10 lines · Eric Biggers
include/sha1.h new file +4
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* SHA-1 message digest algorithm +* include/sha1.h line 14
Quoted 10 lines
include/sha1.h new file +10
-0+* Copyright 2025 Google LLC +*/ +#ifndef __SHA1_H__ +#define __SHA1_H__ + +#include <linux/types.h> +#include <stddef.h> + +#define SHA1_DIGEST_SIZE 20 +#define SHA1_BLOCK_SIZE 64How come these are not part of the uapi?
Quoted 3 lines
I applied this to iproute2-next to get as much soak time as possible. Anyone using legacy bpf (added Toke in case he knows) in particular should test with top of tree.
Hmm, not aware of any users of the old code. I believe most distros build iproute2 with libbpf support these days; that's certainly the case in Red Hat land.
-Toke