From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
kernel-team@fb.com, yhs@fb.com, jose.marchesi@oracle.com,
Eduard Zingerman <eddyz87@gmail.com>
Subject: [PATCH bpf-next 0/3] bpf: allow ctx writes using BPF_ST_MEM instruction
Date: Fri, 3 Mar 2023 00:55:04 +0200 [thread overview]
Message-ID: <20230302225507.3413720-1-eddyz87@gmail.com> (raw)
The function verifier.c:convert_ctx_access() applies some rewrites to BPF
instructions that read from or write to the BPF program context.
For example, the write instruction for the `struct bpf_sockopt::retval`
field:
*(u32 *)(r1 + offsetof(struct bpf_sockopt, retval)) = r2
Is transformed to:
*(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, tmp_reg)) = r9
r9 = *(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, current_task))
r9 = *(u64 *)(r9 + offsetof(struct task_struct, bpf_ctx))
*(u32 *)(r9 + offsetof(struct bpf_cg_run_ctx, retval)) = r2
r9 = *(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, tmp_reg))
Currently, the verifier only supports such transformations for LDX
(memory-to-register read) and STX (register-to-memory write) instructions.
Error is reported for ST instructions (immediate-to-memory write).
This is fine because clang does not currently emit ST instructions.
However, new `-mcpu=v4` clang flag is planned, which would allow to emit
ST instructions (discussed in [1]).
This patch-set adjusts the verifier to support ST instructions in
`verifier.c:convert_ctx_access()`.
The patches #1 and #2 were previously shared as part of RFC [2]. The
changes compared to that RFC are:
- In patch #1, a bug in the handling of the
`struct __sk_buff::queue_mapping` field was fixed.
- Patch #3 is added, which is a set of disassembler-based test cases for
context access rewrites. The test cases cover all fields for which the
handling code is modified in patch #1.
[1] Propose some new instructions for -mcpu=v4
https://lore.kernel.org/bpf/4bfe98be-5333-1c7e-2f6d-42486c8ec039@meta.com/
[2] RFC Support for BPF_ST instruction in LLVM C compiler
https://lore.kernel.org/bpf/20221231163122.1360813-1-eddyz87@gmail.com/
Eduard Zingerman (3):
bpf: allow ctx writes using BPF_ST_MEM instruction
selftests/bpf: test if pointer type is tracked for BPF_ST_MEM
selftests/bpf: Disassembler tests for verifier.c:convert_ctx_access()
kernel/bpf/cgroup.c | 49 +-
kernel/bpf/verifier.c | 79 +-
net/core/filter.c | 79 +-
tools/testing/selftests/bpf/Makefile | 2 +-
tools/testing/selftests/bpf/disasm.c | 1 +
tools/testing/selftests/bpf/disasm.h | 1 +
.../selftests/bpf/prog_tests/ctx_rewrite.c | 917 ++++++++++++++++++
tools/testing/selftests/bpf/verifier/ctx.c | 11 -
tools/testing/selftests/bpf/verifier/unpriv.c | 23 +
9 files changed, 1057 insertions(+), 105 deletions(-)
create mode 120000 tools/testing/selftests/bpf/disasm.c
create mode 120000 tools/testing/selftests/bpf/disasm.h
create mode 100644 tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c
--
2.39.1
next reply other threads:[~2023-03-02 22:55 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-02 22:55 Eduard Zingerman [this message]
2023-03-02 22:55 ` [PATCH bpf-next 1/3] bpf: allow ctx writes using BPF_ST_MEM instruction Eduard Zingerman
2023-03-03 20:21 ` Alexei Starovoitov
2023-03-03 21:17 ` Eduard Zingerman
2023-03-03 22:56 ` Eduard Zingerman
2023-03-02 22:55 ` [PATCH bpf-next 2/3] selftests/bpf: test if pointer type is tracked for BPF_ST_MEM Eduard Zingerman
2023-03-02 22:55 ` [PATCH bpf-next 3/3] selftests/bpf: Disassembler tests for verifier.c:convert_ctx_access() Eduard Zingerman
2023-03-03 20:28 ` Alexei Starovoitov
2023-03-03 21:24 ` Eduard Zingerman
2023-03-03 21:35 ` Alexei Starovoitov
2023-03-03 21:42 ` Eduard Zingerman
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=20230302225507.3413720-1-eddyz87@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jose.marchesi@oracle.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--cc=yhs@fb.com \
/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.