bpftool: Reject non-autoload programs for light skeletons
Subsystems (4)
cover bpftool: Reject non-autoload programs for light skeletons
When generating a light skeleton (bpftool gen skeleton -L), bpf_object__load() skips non-autoload programs (e.g. SEC("?...")), but bpftool still emits struct bpf_prog_desc and link fields for all programs. This causes the loader program to store subsequent program FDs into preceding struct fields and leave trailing prog_fd fields unpopulated (0).
Following the discussion on the earlier "skip non-autoload programs" series [1] and Andrii's feedback [2], this series returns to the initial approach [3] of rejecting non-autoload programs when generating light skeletons, while incorporating the prior review feedback (moving bpf_object__gen_loader()/bpf_object__load() earlier in do_skeleton() so post-load autoload adjustments are observed and documenting the restriction in bpftool-gen.rst).
Supporting runtime toggling of non-autoload programs in light skeletons was considered, but does not fit cleanly with signed light skeletons where the attested loader blob should remain authoritative.
Patch 1 splits the two SEC("?kprobe") verifier failure tests out of test_global_percpu_data.c (which is in LSKELS_EXTRA) before Patch 2 rejects them, so selftests continue to build at every commit. Patch 2 implements the rejection in bpftool. Patch 3 adds a selftest verifying the rejection.
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/CAEf4BzaeKXEC-gBbxo36wdAGwwZxqkL6pV=oo0dke4S2BmuRmg@mail.gmail.com/
[3] https://lore.kernel.org/bpf/[email protected]/
Thiébaud Weksteen (3):
selftests/bpf: Move verifier failure tests out of
test_global_percpu_data
bpftool: Reject non-autoload programs for light skeletons selftests/bpf: Test non-autoload rejection in light skeletons
.../bpf/bpftool/Documentation/bpftool-gen.rst | 4 +- tools/bpf/bpftool/gen.c | 68 +++++++++++-------- .../selftests/bpf/prog_tests/bpftool_gen.c | 30 ++++++++ .../bpf/prog_tests/global_data_init.c | 3 +- .../bpf/progs/test_global_percpu_data.c | 23 ------- .../progs/test_global_percpu_data_failure.c | 30 ++++++++ 6 files changed, 106 insertions(+), 52 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpftool_gen.c create mode 100644 tools/testing/selftests/bpf/progs/test_global_percpu_data_failure.c
base-commit: 84afebd853437e44cb71f718be11d90e4c1a59dd
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH 1/3 selftests/bpf: Move verifier failure tests out of test_global_percpu_data
Commit 4c9241bd731a ("selftests/bpf: Add tests to verify global percpu data") added test_global_percpu_data.c to LSKELS_EXTRA. Later, commit 1ed2294b31fc ("selftests/bpf: Test verifier log for global percpu data") added two verifier failure tests (verifier_strncmp and verifier_snprintf), both marked as non-autoload (SEC("?kprobe")).
When bpftool generates light skeletons, non-autoload programs are skipped during loading, causing the loader program to store subsequent program FDs into incorrect skeleton struct fields (for example, dump_percpu_data's FD was stored into verifier_strncmp's descriptor, leaving dump_percpu_data.prog_fd unpopulated).
Move the two verifier failure tests to a dedicated test file, test_global_percpu_data_failure.c, and run them via RUN_TESTS in global_data_init.c. This leaves test_global_percpu_data.c with only autoloaded programs, allowing its light skeleton to be generated cleanly and preparing selftests for rejecting non-autoload programs in light skeletons.
--- .../bpf/prog_tests/global_data_init.c | 3 +- .../bpf/progs/test_global_percpu_data.c | 23 -------------- .../progs/test_global_percpu_data_failure.c | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_global_percpu_data_failure.c
tools/testing/selftests/bpf/prog_tests/global_data_init.c +2 -1
@@ -3,6 +3,7 @@#include "bpf/libbpf_internal.h" #include "test_global_percpu_data.skel.h" #include "test_global_percpu_data.lskel.h" +#include "test_global_percpu_data_failure.skel.h" void test_global_data_init (void ) {
@@ -372,7 +373,7 @@ static void test_global_percpu_data_rdonly_direct_write(void)static void test_global_percpu_data_verifier_log (void ) { -RUN_TESTS (test_global_percpu_data); +RUN_TESTS (test_global_percpu_data_failure); }static void test_global_percpu_data_iter (void )
tools/testing/selftests/bpf/progs/test_global_percpu_data.c +0 -23
@@ -42,29 +42,6 @@ int update_percpu_data(void *ctx)return 0 ; } -static const char fmt[] SEC(".percpu.fmt") = "data %d\n"; - -SEC("?kprobe") -__failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") -int verifier_strncmp(void *ctx) -{ -return bpf_strncmp ("test" ,5 , fmt); -} - -SEC("?kprobe") -__failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") -int verifier_snprintf(void *ctx) -{ -u64 args[] = { data }; -char buf[128 ]; -int len; - - len =bpf_snprintf (buf,sizeof (buf), fmt, args,sizeof (args)); -if (len >0 ) -bpf_printk ("snprintf: %s\n" , buf); -return 0 ; -} -volatile const __u32 num_cpus =0 ;volatile const int num_off;volatile const int elem_sz;
tools/testing/selftests/bpf/progs/test_global_percpu_data_failure.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" + +int data SEC(".percpu") = -1; +static const char fmt[] SEC(".percpu.fmt") = "data %d\n"; + +SEC("?kprobe") +__failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") +int verifier_strncmp(void *ctx) +{ +return bpf_strncmp ("test" ,5 , fmt); +} + +SEC("?kprobe") +__failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") +int verifier_snprintf(void *ctx) +{ +u64 args[] = { data }; +char buf[128 ]; +int len; + + len =bpf_snprintf (buf,sizeof (buf), fmt, args,sizeof (args)); +if (len >0 ) +bpf_printk ("snprintf: %s\n" , buf); +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.56.1.831.g720f3f5937-goog
PATCH 2/3 bpftool: Reject non-autoload programs for light skeletons
In reply to Thiébaud WeksteenWhen generating a light skeleton (bpftool gen skeleton -L), there is no libbpf runtime object or bpf_program__set_autoload() API available to enable or disable program autoloading. If a BPF program in the object has autoload disabled (e.g. via SEC("?...")), bpf_object__load() silently skips loading it, while bpftool still emits struct bpf_prog_desc fields and attach/detach/destroy helpers for all programs. As a result, the loader program writes subsequent program FDs into preceding skeleton struct fields and leaves trailing prog_fd fields unpopulated (0).
Move bpf_object__gen_loader() and bpf_object__load() out of gen_trace() into do_skeleton() before validating programs and emitting skeleton struct definitions, so that any autoload state adjustments performed during bpf_object__load() (e.g. for struct_ops programs) are observed and no partial header is emitted on error. Explicitly reject any non-autoload program when use_loader is true with an error message, and document this restriction in bpftool-gen.rst.
--- .../bpf/bpftool/Documentation/bpftool-gen.rst | 4 +- tools/bpf/bpftool/gen.c | 68 +++++++++++-------- 2 files changed, 44 insertions(+), 28 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-gen.rst +3 -1
@@ -184,7 +184,9 @@ OPTIONS -L, --use-loader For skeletons, generate a "light" skeleton (also known as "loader" skeleton). A light skeleton contains a loader eBPF program. It does not use - the majority of the libbpf infrastructure, and does not need libelf. + the majority of the libbpf infrastructure, and does not need libelf. BPF + programs marked as non-autoload (e.g., via **SEC("?...")**) are not + supported for light skeletons and will cause skeleton generation to fail. -S, --sign For skeletons, generate a signed skeleton. This option must be used with
tools/bpf/bpftool/gen.c +41 -27
127 lines not on this page · raw message
Signature · 1 line
2.56.1.831.g720f3f5937-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] bpftool: multi-line comment style in do_skeleton() violates BPF convention --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpftool: multi-line comment style in do_skeleton() violates BPF convention
--
commit 00c1d9ac900fa5925fd4c2012a8926354bce780d
Author: Thiébaud Weksteen <[email protected]>
bpftool: Reject non-autoload programs for light skeletons
This commit moves loader generation and object loading earlier in do_skeleton() and rejects BPF programs marked as non-autoload when generating light skeletons.
Quoted 4 lines
tools/bpf/bpftool/gen.c +0
-0
[ ... ]
Quoted 16 lines
tools/bpf/bpftool/gen.c +12
-0@@ -1316,6 +1301,29 @@ static int do_skeleton(int argc, char **argv) goto out_obj; } +/* If we are generating a loader, load the object early (before +* validating programs and emitting skeleton struct definitions), as +* bpf_object__load() may update a program's autoload status (e.g. +* for struct_ops programs). +*/ +if (use_loader) { +if (sign_progs) + gen_opts.gen_hash = true; + + err =bpf_object__gen_loader (obj, &gen_opts); +if (err) + goto out;
tools/bpf/bpftool/gen.c static int do_skeleton(int argc, char **argv) · line 1325
Quoted 10 lines
tools/bpf/bpftool/gen.c +10
-0+ + err =bpf_object__load (obj); +if (err) { +p_err ("failed to load object file" ); + goto out; + } +/* If there was no error during load, gen_opts is populated with +* the loader program. +*/ + }
[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of multi-line comments on its own line with the comment text starting on the next line.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/3 selftests/bpf: Test non-autoload rejection in light skeletons
In reply to Thiébaud WeksteenAdd a test (bpftool_gen/lskel_non_autoload) that invokes 'bpftool gen skeleton -L' on an object containing non-autoload programs (test_global_percpu_data_failure.bpf.o) and verifies that generation fails with the expected error message.
--- .../selftests/bpf/prog_tests/bpftool_gen.c | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpftool_gen.c
tools/testing/selftests/bpf/prog_tests/bpftool_gen.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <bpftool_helpers.h> +#include <test_progs.h> + +#define MAX_BPFTOOL_OUTPUT_LEN (64 * 1024) + +static char output[MAX_BPFTOOL_OUTPUT_LEN ]; + +static void test_lskel_non_autoload (void ) +{ +char cmd[MAX_BPFTOOL_CMD_LEN ]; +int ret; + + ret =snprintf (cmd,sizeof (cmd), +"gen skeleton -L test_global_percpu_data_failure.bpf.o 2>&1" ); +if (!ASSERT_GT (ret,0 ,"format cmd" )) +return ; + + ret =get_bpftool_command_output (cmd, output,sizeof (output)); +ASSERT_NEQ (ret,0 ,"gen skeleton -L non-autoload should fail" ); +ASSERT_HAS_SUBSTR (output, +"is marked as non-autoload, which is not supported for light skeletons" , +"error message" ); +} + +void test_bpftool_gen (void ) +{ +if (test__start_subtest ("lskel_non_autoload" )) +test_lskel_non_autoload (); +}
Signature · 1 line
2.56.1.831.g720f3f5937-goog