From: Anton Protopopov <a.s.protopopov@gmail.com>
To: Eduard Zingerman <eddyz87@gmail.com>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Anton Protopopov <aspsk@isovalent.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Quentin Monnet <qmo@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>
Subject: Re: [PATCH v7 bpf-next 09/12] libbpf: support llvm-generated indirect jumps
Date: Tue, 28 Oct 2025 11:42:29 +0000 [thread overview]
Message-ID: <aQCsJZ6549YD9Wou@mail.gmail.com> (raw)
In-Reply-To: <b6f1be926ea382a9d4d30bdb8d09fa6b06d00165.camel@gmail.com>
On 25/10/27 03:59PM, Eduard Zingerman wrote:
> On Mon, 2025-10-27 at 15:38 -0700, Eduard Zingerman wrote:
> > [...]
> >
> > > +static int create_jt_map(struct bpf_object *obj, struct bpf_program *prog, struct reloc_desc *relo)
> > > +{
> > > + const __u32 jt_entry_size = 8;
> > > + int sym_off = relo->sym_off;
> > > + int jt_size = relo->sym_size;
> > > + __u32 max_entries = jt_size / jt_entry_size;
> > > + __u32 value_size = sizeof(struct bpf_insn_array_value);
> > > + struct bpf_insn_array_value val = {};
> > > + int subprog_idx;
> > > + int map_fd, err;
> > > + __u64 insn_off;
> > > + __u64 *jt;
> > > + __u32 i;
> > > +
> > > + map_fd = find_jt_map(obj, prog, sym_off);
> > > + if (map_fd >= 0)
> > > + return map_fd;
> > > +
> > > + if (sym_off % jt_entry_size) {
> > > + pr_warn("jumptable start %d should be multiple of %u\n",
> > > + sym_off, jt_entry_size);
> > > + return -EINVAL;
> > > + }
> > > +
> > > + if (jt_size % jt_entry_size) {
> > > + pr_warn("jumptable size %d should be multiple of %u\n",
> > > + jt_size, jt_entry_size);
> > > + return -EINVAL;
> > > + }
> > > +
> > > + map_fd = bpf_map_create(BPF_MAP_TYPE_INSN_ARRAY, ".jumptables",
> > > + 4, value_size, max_entries, NULL);
> > > + if (map_fd < 0)
> > > + return map_fd;
> > > +
> > > + if (!obj->jumptables_data) {
> > > + pr_warn("map '.jumptables': ELF file is missing jump table data\n");
> > > + err = -EINVAL;
> > > + goto err_close;
> > > + }
> > > + if (sym_off + jt_size > obj->jumptables_data_sz) {
> > > + pr_warn("jumptables_data size is %zd, trying to access %d\n",
> > > + obj->jumptables_data_sz, sym_off + jt_size);
> > > + err = -EINVAL;
> > > + goto err_close;
> > > + }
> > > +
> > > + jt = (__u64 *)(obj->jumptables_data + sym_off);
> > > + for (i = 0; i < max_entries; i++) {
> > > + /*
> > > + * The offset should be made to be relative to the beginning of
> > > + * the main function, not the subfunction.
> > > + */
> > > + insn_off = jt[i]/sizeof(struct bpf_insn);
> > > + if (!prog->subprogs) {
> > > + insn_off -= prog->sec_insn_off;
> > > + } else {
> > > + subprog_idx = find_subprog_idx(prog, relo->insn_idx);
> >
> > Nit: find_subprog_idx(prog, relo->insn_idx) can be moved outside of the loop, I think.
> >
> > > + if (subprog_idx < 0) {
> > > + pr_warn("invalid jump insn idx[%d]: %d, no subprog found\n",
> > > + i, relo->insn_idx);
> > > + err = -EINVAL;
> > > + }
> > > + insn_off -= prog->subprogs[subprog_idx].sec_insn_off;
> > > + insn_off += prog->subprogs[subprog_idx].sub_insn_off;
> > > + }
>
> I think I found a bug, related to this code path.
> Consider the following test case:
>
> SEC("socket")
> __naked void foo(void)
> {
> asm volatile (" \
> .pushsection .jumptables,\"\",@progbits; \
> jt0_%=: \
> .quad ret0_%=; \
> .quad ret1_%=; \
> .size jt0_%=, 16; \
> .global jt0_%=; \
> .popsection; \
> \
> r0 = jt0_%= ll; \
> r0 += 8; \
> r0 = *(u64 *)(r0 + 0); \
> .8byte %[gotox_r0]; \
> ret0_%=: \
> r0 = 0; \
> exit; \
> ret1_%=: \
> r0 = 1; \
> call bar; \
> exit; \
> " : \
> : __imm_insn(gotox_r0, BPF_RAW_INSN(BPF_JMP | BPF_JA | BPF_X, BPF_REG_0, 0, 0 , 0))
> : __clobber_all);
> }
>
> __used
> static int bar(void)
> {
> return 0;
> }
>
> Note a call instruction referring bar(). It triggers the code path
> above (we need a test case with subprograms in verifier_gotox).
> The test case fails to load with the following error:
>
> libbpf: invalid jump insn idx[0]: 0, no subprog found
> libbpf: prog 'foo': relo #0: can't create jump table: sym_off 368
> libbpf: prog 'foo': failed to relocate data references: -EINVAL
>
> If I remove the `call bar;`, test case loads and passes.
>
> > > +
> > > + /*
> > > + * LLVM-generated jump tables contain u64 records, however
> > > + * should contain values that fit in u32.
> > > + */
> > > + if (insn_off > UINT32_MAX) {
> > > + pr_warn("invalid jump table value %llx at offset %d\n",
> ^^^^
> Nit: maybe add 0x prefix here?
Sure, added.
> > > + jt[i], sym_off + i);
> > > + err = -EINVAL;
> > > + goto err_close;
> > > + }
> > > +
> > > + val.orig_off = insn_off;
> > > + err = bpf_map_update_elem(map_fd, &i, &val, 0);
> > > + if (err)
> > > + goto err_close;
> > > + }
> >
> > [...]
next prev parent reply other threads:[~2025-10-28 11:35 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-26 19:26 [PATCH v7 bpf-next 00/12] BPF indirect jumps Anton Protopopov
2025-10-26 19:26 ` [PATCH v7 bpf-next 01/12] bpf, x86: add new map type: instructions array Anton Protopopov
2025-10-26 20:12 ` Anton Protopopov
2025-10-26 22:34 ` kernel test robot
2025-10-26 22:59 ` kernel test robot
2025-10-27 21:44 ` Eduard Zingerman
2025-10-28 10:10 ` Anton Protopopov
2025-10-29 21:54 ` Eduard Zingerman
2025-10-26 19:26 ` [PATCH v7 bpf-next 02/12] selftests/bpf: add selftests for new insn_array map Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 03/12] bpf: support instructions arrays with constants blinding Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 04/12] selftests/bpf: test instructions arrays with blinding Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 05/12] bpf, x86: allow indirect jumps to r8...r15 Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 06/12] bpf, x86: add support for indirect jumps Anton Protopopov
2025-10-26 20:41 ` Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 07/12] bpf: disasm: add support for BPF_JMP|BPF_JA|BPF_X Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 08/12] bpf, docs: do not state that indirect jumps are not supported Anton Protopopov
2025-10-27 6:30 ` Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 09/12] libbpf: support llvm-generated indirect jumps Anton Protopopov
2025-10-26 20:15 ` Anton Protopopov
2025-10-27 22:09 ` Eduard Zingerman
2025-10-27 22:38 ` Eduard Zingerman
2025-10-27 22:59 ` Eduard Zingerman
2025-10-28 11:36 ` Anton Protopopov
2025-10-28 11:42 ` Anton Protopopov [this message]
2025-10-26 19:27 ` [PATCH v7 bpf-next 10/12] bpftool: Recognize insn_array map type Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 11/12] selftests/bpf: add new verifier_gotox test Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 12/12] selftests/bpf: add C-level selftests for indirect jumps Anton Protopopov
2025-10-27 23:25 ` Eduard Zingerman
2025-10-28 10:59 ` Anton Protopopov
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=aQCsJZ6549YD9Wou@mail.gmail.com \
--to=a.s.protopopov@gmail.com \
--cc=andrii@kernel.org \
--cc=aspsk@isovalent.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=qmo@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.