Skip to content

Commit e16f616

Browse files
alan-maguireKernel Patches Daemon
authored andcommitted
bpftool: Use libcrypto feature test to optionally support signing
New libcrypto test verifies presence of openssl3 needed for BPF signing; use that feature to conditionally compile signing-related code so bpftool build will not break in the absence of libcrypto v3. Fixes: 40863f4 ("bpftool: Add support for signing BPF programs") Suggested-by: Quentin Monnet <[email protected]> Signed-off-by: Alan Maguire <[email protected]>
1 parent ebd5928 commit e16f616

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

tools/bpf/bpftool/Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ FEATURE_TESTS := clang-bpf-co-re
101101
FEATURE_TESTS += llvm
102102
FEATURE_TESTS += libcap
103103
FEATURE_TESTS += libbfd
104+
FEATURE_TESTS += libcrypto
104105
FEATURE_TESTS += libbfd-liberty
105106
FEATURE_TESTS += libbfd-liberty-z
106107
FEATURE_TESTS += disassembler-four-args
@@ -110,6 +111,7 @@ FEATURE_TESTS += libelf-zstd
110111
FEATURE_DISPLAY := clang-bpf-co-re
111112
FEATURE_DISPLAY += llvm
112113
FEATURE_DISPLAY += libcap
114+
FEATURE_DISPLAY += libcrypto
113115
FEATURE_DISPLAY += libbfd
114116
FEATURE_DISPLAY += libbfd-liberty
115117
FEATURE_DISPLAY += libbfd-liberty-z
@@ -130,8 +132,14 @@ include $(FEATURES_DUMP)
130132
endif
131133
endif
132134

133-
LIBS = $(LIBBPF) -lelf -lz -lcrypto
134-
LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz -lcrypto
135+
LIBS = $(LIBBPF) -lelf -lz
136+
LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
137+
138+
ifeq ($(feature-libcrypto),1)
139+
CFLAGS += -DUSE_CRYPTO
140+
LIBS += -lcrypto
141+
LIBS_BOOTSTRAP += -lcrypto
142+
endif
135143

136144
ifeq ($(feature-libelf-zstd),1)
137145
LIBS += -lzstd
@@ -194,7 +202,10 @@ endif
194202

195203
BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
196204

197-
BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o sign.o)
205+
BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o)
206+
ifeq ($(feature-libcrypto),1)
207+
BOOTSTRAP_OBJS += $(addprefix $(BOOTSTRAP_OUTPUT),sign.o)
208+
endif
198209
$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
199210

200211
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o

tools/bpf/bpftool/gen.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,16 +688,15 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
688688
static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
689689
{
690690
DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
691-
struct bpf_load_and_run_opts sopts = {};
692-
char sig_buf[MAX_SIG_SIZE];
693-
__u8 prog_sha[SHA256_DIGEST_LENGTH];
694691
struct bpf_map *map;
695692

696693
char ident[256];
697694
int err = 0;
698695

696+
#ifdef USE_CRYPTO
699697
if (sign_progs)
700698
opts.gen_hash = true;
699+
#endif
701700

702701
err = bpf_object__gen_loader(obj, &opts);
703702
if (err)
@@ -790,7 +789,12 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
790789
\n\
791790
\";\n");
792791

792+
#ifdef USE_CRYPTO
793793
if (sign_progs) {
794+
struct bpf_load_and_run_opts sopts = {};
795+
char sig_buf[MAX_SIG_SIZE];
796+
__u8 prog_sha[SHA256_DIGEST_LENGTH];
797+
794798
sopts.insns = opts.insns;
795799
sopts.insns_sz = opts.insns_sz;
796800
sopts.excl_prog_hash = prog_sha;
@@ -831,7 +835,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
831835
opts.keyring_id = skel->keyring_id; \n\
832836
");
833837
}
834-
838+
#endif /* USE_CRYPTO */
835839
codegen("\
836840
\n\
837841
opts.ctx = (struct bpf_loader_ctx *)skel; \n\
@@ -1406,13 +1410,14 @@ static int do_skeleton(int argc, char **argv)
14061410

14071411
printf("\t} links;\n");
14081412
}
1409-
1413+
#ifdef USE_CRYPTO
14101414
if (sign_progs) {
14111415
codegen("\
14121416
\n\
14131417
__s32 keyring_id; \n\
14141418
");
14151419
}
1420+
#endif /* USE_CRYPTO */
14161421

14171422
if (btf) {
14181423
err = codegen_datasecs(obj, obj_name);
@@ -1990,7 +1995,9 @@ static int do_help(int argc, char **argv)
19901995
" %1$s %2$s help\n"
19911996
"\n"
19921997
" " HELP_SPEC_OPTIONS " |\n"
1998+
#ifdef USE_CRYPTO
19931999
" {-L|--use-loader} | [ {-S|--sign } {-k} <private_key.pem> {-i} <certificate.x509> ]}\n"
2000+
#endif
19942001
"",
19952002
bin_name, "gen");
19962003

tools/bpf/bpftool/prog.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,12 +1931,10 @@ static int try_loader(struct gen_loader_opts *gen)
19311931
{
19321932
struct bpf_load_and_run_opts opts = {};
19331933
struct bpf_loader_ctx *ctx;
1934-
char sig_buf[MAX_SIG_SIZE];
1935-
__u8 prog_sha[SHA256_DIGEST_LENGTH];
19361934
int ctx_sz = sizeof(*ctx) + 64 * max(sizeof(struct bpf_map_desc),
19371935
sizeof(struct bpf_prog_desc));
19381936
int log_buf_sz = (1u << 24) - 1;
1939-
int err, fds_before, fd_delta;
1937+
int err = 0, fds_before, fd_delta;
19401938
char *log_buf = NULL;
19411939

19421940
ctx = alloca(ctx_sz);
@@ -1947,7 +1945,7 @@ static int try_loader(struct gen_loader_opts *gen)
19471945
ctx->log_size = log_buf_sz;
19481946
log_buf = malloc(log_buf_sz);
19491947
if (!log_buf)
1950-
return -ENOMEM;
1948+
goto out;
19511949
ctx->log_buf = (long) log_buf;
19521950
}
19531951
opts.ctx = ctx;
@@ -1956,8 +1954,11 @@ static int try_loader(struct gen_loader_opts *gen)
19561954
opts.insns = gen->insns;
19571955
opts.insns_sz = gen->insns_sz;
19581956
fds_before = count_open_fds();
1959-
1957+
#ifdef USE_CRYPTO
19601958
if (sign_progs) {
1959+
char sig_buf[MAX_SIG_SIZE];
1960+
__u8 prog_sha[SHA256_DIGEST_LENGTH];
1961+
19611962
opts.excl_prog_hash = prog_sha;
19621963
opts.excl_prog_hash_sz = sizeof(prog_sha);
19631964
opts.signature = sig_buf;
@@ -1976,6 +1977,7 @@ static int try_loader(struct gen_loader_opts *gen)
19761977
goto out;
19771978
}
19781979
}
1980+
#endif
19791981
err = bpf_load_and_run(&opts);
19801982
fd_delta = count_open_fds() - fds_before;
19811983
if (err < 0 || verifier_logs) {

tools/bpf/bpftool/sign.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (C) 2025 Google LLC.
44
*/
55

6+
#ifdef USE_CRYPTO
67
#ifndef _GNU_SOURCE
78
#define _GNU_SOURCE
89
#endif
@@ -209,3 +210,4 @@ int bpftool_prog_sign(struct bpf_load_and_run_opts *opts)
209210
DISPLAY_OSSL_ERR(err < 0);
210211
return err;
211212
}
213+
#endif /* USE_CRYPTO */

0 commit comments

Comments
 (0)