From: "Thiébaud Weksteen" <tweek@google.com>
To: Quentin Monnet <qmo@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Shuah Khan <shuah@kernel.org>, Leon Hwang <leon.hwang@linux.dev>
Cc: "Thiébaud Weksteen" <tweek@google.com>,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Sid Nayyar" <sidnayyar@google.com>
Subject: [PATCH bpf 2/3] bpftool: Reject non-autoload programs for light skeletons
Date: Wed, 25 Nov 2026 11:49:10 +1100 [thread overview]
Message-ID: <20261125004911.3937193-3-tweek@google.com> (raw)
In-Reply-To: <20261125004911.3937193-1-tweek@google.com>
When 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.
Fixes: d510296d331a ("bpftool: Use syscall/loader program in "prog load" and "gen skeleton" command.")
Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
.../bpf/bpftool/Documentation/bpftool-gen.rst | 4 +-
tools/bpf/bpftool/gen.c | 68 +++++++++++--------
2 files changed, 44 insertions(+), 28 deletions(-)
diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
index d0a36f442db7..18106dcdd352 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
@@ -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
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ee680f7c2116..edb6f7ef508e 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -691,9 +691,10 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
obj_name);
}
-static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
+static int gen_trace(struct bpf_object *obj, const char *obj_name,
+ const char *header_guard,
+ const struct gen_loader_opts *opts)
{
- DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
struct bpf_load_and_run_opts sopts = {};
char sig_buf[MAX_SIG_SIZE];
__u8 prog_sha[SHA256_DIGEST_LENGTH];
@@ -702,23 +703,6 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
char ident[256];
int err = 0;
- if (sign_progs)
- opts.gen_hash = true;
-
- err = bpf_object__gen_loader(obj, &opts);
- if (err)
- return err;
-
- err = bpf_object__load(obj);
- if (err) {
- p_err("failed to load object file");
- goto out;
- }
-
- /* If there was no error during load then gen_loader_opts
- * are populated with the loader program.
- */
-
/* finish generating 'struct skel' */
codegen("\
\n\
@@ -742,7 +726,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
goto cleanup; \n\
skel->ctx.sz = (char *)&skel->links - (char *)skel; \n\
",
- obj_name, opts.data_sz);
+ obj_name, opts->data_sz);
bpf_object__for_each_map(map, obj) {
const void *mmap_data = NULL;
size_t mmap_size = 0;
@@ -785,22 +769,22 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
static const char opts_data[] __attribute__((__aligned__(8))) = \"\\\n\
",
obj_name);
- print_hex(opts.data, opts.data_sz);
+ print_hex(opts->data, opts->data_sz);
codegen("\
\n\
\"; \n\
static const char opts_insn[] __attribute__((__aligned__(8))) = \"\\\n\
");
- print_hex(opts.insns, opts.insns_sz);
+ print_hex(opts->insns, opts->insns_sz);
codegen("\
\n\
\";\n");
if (sign_progs) {
- sopts.insns = opts.insns;
- sopts.insns_sz = opts.insns_sz;
- sopts.data = opts.data;
- sopts.data_sz = opts.data_sz;
+ sopts.insns = opts->insns;
+ sopts.insns_sz = opts->insns_sz;
+ sopts.data = opts->data;
+ sopts.data_sz = opts->data_sz;
sopts.excl_prog_hash = prog_sha;
sopts.excl_prog_hash_sz = sizeof(prog_sha);
sopts.signature = sig_buf;
@@ -1240,6 +1224,7 @@ static int do_skeleton(int argc, char **argv)
char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
size_t map_cnt = 0, prog_cnt = 0, attach_map_cnt = 0, file_sz, mmap_sz;
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+ DECLARE_LIBBPF_OPTS(gen_loader_opts, gen_opts);
char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
struct bpf_object *obj = NULL;
const char *file;
@@ -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;
+
+ 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.
+ */
+ }
+
bpf_object__for_each_map(map, obj) {
if (!get_map_ident(map, ident, sizeof(ident))) {
p_err("ignoring unrecognized internal map '%s'...",
@@ -1329,6 +1337,12 @@ static int do_skeleton(int argc, char **argv)
map_cnt++;
}
bpf_object__for_each_program(prog, obj) {
+ if (use_loader && !bpf_program__autoload(prog)) {
+ p_err("program '%s' is marked as non-autoload, which is not supported for light skeletons",
+ bpf_program__name(prog));
+ err = -1;
+ goto out;
+ }
prog_cnt++;
}
@@ -1441,7 +1455,7 @@ static int do_skeleton(int argc, char **argv)
goto out;
}
if (use_loader) {
- err = gen_trace(obj, obj_name, header_guard);
+ err = gen_trace(obj, obj_name, header_guard, &gen_opts);
goto out;
}
--
2.56.1.831.g720f3f5937-goog
next prev parent reply other threads:[~2026-11-25 0:49 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 0:49 [PATCH bpf 0/3] bpftool: Reject non-autoload programs for light skeletons Thiébaud Weksteen
2026-11-25 0:49 ` [PATCH bpf 1/3] selftests/bpf: Move verifier failure tests out of test_global_percpu_data Thiébaud Weksteen
2026-11-25 0:49 ` Thiébaud Weksteen [this message]
2026-11-25 0:59 ` [PATCH bpf 2/3] bpftool: Reject non-autoload programs for light skeletons sashiko-bot
2026-11-25 0:49 ` [PATCH bpf 3/3] selftests/bpf: Test non-autoload rejection in " Thiébaud Weksteen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125004911.3937193-3-tweek@google.com \
--to=tweek@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=qmo@kernel.org \
--cc=shuah@kernel.org \
--cc=sidnayyar@google.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox