Skip to content

Commit 17fb2f3

Browse files
committed
selftests/bpf: Enable signature verification for some lskel tests
The test harness uses the verify_sig_setup.sh to generate the required key material for program signing. Generate key material for signing LSKEL some lskel programs and use xxd to convert the verification certificate into a C header file. Finally, update the main test runner to load this certificate into the session keyring via the add_key() syscall before executing any tests. Signed-off-by: KP Singh <[email protected]>
1 parent 17db85f commit 17fb2f3

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ xdp_redirect_multi
4444
xdp_synproxy
4545
xdp_hw_metadata
4646
xdp_features
47+
verification_cert.h

tools/testing/selftests/bpf/Makefile

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,16 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
496496
test_subskeleton.skel.h test_subskeleton_lib.skel.h \
497497
test_usdt.skel.h
498498

499-
LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \
500-
trace_printk.c trace_vprintk.c map_ptr_kern.c \
499+
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
501500
core_kern.c core_kern_overflow.c test_ringbuf.c \
502501
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c
503502

503+
LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
504+
504505
# Generate both light skeleton and libbpf skeleton for these
505506
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
506507
kfunc_call_test_subprog.c
507-
SKEL_BLACKLIST += $$(LSKELS)
508+
SKEL_BLACKLIST += $$(LSKELS) $$(LSKELS_SIGNED)
508509

509510
test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
510511
linked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o
@@ -535,6 +536,7 @@ HEADERS_FOR_BPF_OBJS := $(wildcard $(BPFDIR)/*.bpf.h) \
535536
# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, bpf_gcc, etc)
536537
define DEFINE_TEST_RUNNER
537538

539+
LSKEL_SIGN := -S -k $(PRIVATE_KEY) -i $(VERIFICATION_CERT)
538540
TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
539541
TRUNNER_BINARY := $1$(if $2,-)$2
540542
TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \
@@ -550,6 +552,7 @@ TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \
550552
$$(TRUNNER_BPF_SRCS)))
551553
TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
552554
TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
555+
TRUNNER_BPF_LSKELS_SIGNED := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS_SIGNED))
553556
TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
554557

555558
# Evaluate rules now with extra TRUNNER_XXX variables above already defined
@@ -604,6 +607,15 @@ $(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
604607
$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
605608
$(Q)rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
606609

610+
$(TRUNNER_BPF_LSKELS_SIGNED): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
611+
$$(call msg,GEN-SKEL,$(TRUNNER_BINARY) (signed),$$@)
612+
$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$<
613+
$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o)
614+
$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o)
615+
$(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
616+
$(Q)$$(BPFTOOL) gen skeleton $(LSKEL_SIGN) $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
617+
$(Q)rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
618+
607619
$(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/%
608620

609621
# .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites
@@ -653,6 +665,7 @@ $(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d: \
653665
$(TRUNNER_EXTRA_HDRS) \
654666
$(TRUNNER_BPF_SKELS) \
655667
$(TRUNNER_BPF_LSKELS) \
668+
$(TRUNNER_BPF_LSKELS_SIGNED) \
656669
$(TRUNNER_BPF_SKELS_LINKED) \
657670
$$(BPFOBJ) | $(TRUNNER_OUTPUT)
658671

@@ -667,6 +680,7 @@ $(foreach N,$(patsubst $(TRUNNER_OUTPUT)/%.o,%,$(TRUNNER_EXTRA_OBJS)), \
667680
$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
668681
%.c \
669682
$(TRUNNER_EXTRA_HDRS) \
683+
$(VERIFY_SIG_HDR) \
670684
$(TRUNNER_TESTS_HDR) \
671685
$$(BPFOBJ) | $(TRUNNER_OUTPUT)
672686
$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
@@ -697,6 +711,18 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
697711

698712
endef
699713

714+
VERIFY_SIG_SETUP := $(CURDIR)/verify_sig_setup.sh
715+
VERIFY_SIG_HDR := verification_cert.h
716+
VERIFICATION_CERT := $(BUILD_DIR)/signing_key.der
717+
PRIVATE_KEY := $(BUILD_DIR)/signing_key.pem
718+
719+
$(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
720+
$(Q)mkdir -p $(BUILD_DIR)
721+
$(Q)$(VERIFY_SIG_SETUP) genkey $(BUILD_DIR)
722+
723+
$(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
724+
$(Q)xxd -i -n test_progs_verification_cert $< > $@
725+
700726
# Define test_progs test runner.
701727
TRUNNER_TESTS_DIR := prog_tests
702728
TRUNNER_BPF_PROGS_DIR := progs
@@ -716,6 +742,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
716742
disasm.c \
717743
disasm_helpers.c \
718744
json_writer.c \
745+
$(VERIFY_SIG_HDR) \
719746
flow_dissector_load.h \
720747
ip_check_defrag_frags.h
721748
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
@@ -725,7 +752,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
725752
$(OUTPUT)/uprobe_multi \
726753
$(TEST_KMOD_TARGETS) \
727754
ima_setup.sh \
728-
verify_sig_setup.sh \
755+
$(VERIFY_SIG_SETUP) \
729756
$(wildcard progs/btf_dump_test_case_*.c) \
730757
$(wildcard progs/*.bpf.o)
731758
TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE

tools/testing/selftests/bpf/test_progs.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
#include <netinet/in.h>
1515
#include <sys/select.h>
1616
#include <sys/socket.h>
17+
#include <linux/keyctl.h>
1718
#include <sys/un.h>
1819
#include <bpf/btf.h>
1920
#include <time.h>
2021
#include "json_writer.h"
2122

2223
#include "network_helpers.h"
24+
#include "verification_cert.h"
2325

2426
/* backtrace() and backtrace_symbols_fd() are glibc specific,
2527
* use header file when glibc is available and provide stub
@@ -1928,6 +1930,13 @@ static void free_test_states(void)
19281930
}
19291931
}
19301932

1933+
static __u32 register_session_key(const char *key_data, size_t key_data_size)
1934+
{
1935+
return syscall(__NR_add_key, "asymmetric", "libbpf_session_key",
1936+
(const void *)key_data, key_data_size,
1937+
KEY_SPEC_SESSION_KEYRING);
1938+
}
1939+
19311940
int main(int argc, char **argv)
19321941
{
19331942
static const struct argp argp = {
@@ -1961,6 +1970,10 @@ int main(int argc, char **argv)
19611970
/* Use libbpf 1.0 API mode */
19621971
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
19631972
libbpf_set_print(libbpf_print_fn);
1973+
err = register_session_key((const char *)test_progs_verification_cert,
1974+
test_progs_verification_cert_len);
1975+
if (err < 0)
1976+
return err;
19641977

19651978
traffic_monitor_set_print(traffic_monitor_print_fn);
19661979

tools/testing/selftests/bpf/verify_sig_setup.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ usage()
3232
exit 1
3333
}
3434

35-
setup()
35+
genkey()
3636
{
3737
local tmp_dir="$1"
3838

@@ -46,8 +46,15 @@ setup()
4646
openssl x509 -in ${tmp_dir}/signing_key.pem -out \
4747
${tmp_dir}/signing_key.der -outform der
4848

49-
key_id=$(cat ${tmp_dir}/signing_key.der | keyctl padd asymmetric ebpf_testing_key @s)
49+
}
5050

51+
setup()
52+
{
53+
local tmp_dir="$1"
54+
55+
genkey "${tmp_dir}"
56+
57+
key_id=$(cat ${tmp_dir}/signing_key.der | keyctl padd asymmetric ebpf_testing_key @s)
5158
keyring_id=$(keyctl newring ebpf_testing_keyring @s)
5259
keyctl link $key_id $keyring_id
5360
}
@@ -105,6 +112,8 @@ main()
105112

106113
if [[ "${action}" == "setup" ]]; then
107114
setup "${tmp_dir}"
115+
elif [[ "${action}" == "genkey" ]]; then
116+
genkey "${tmp_dir}"
108117
elif [[ "${action}" == "cleanup" ]]; then
109118
cleanup "${tmp_dir}"
110119
elif [[ "${action}" == "fsverity-create-sign" ]]; then

0 commit comments

Comments
 (0)