Commit c21b0f82ca96 for kernel

commit c21b0f82ca96750e81a38b041c037de6b5dab60a
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Nov 18 09:44:50 2026 +0100

    selftests/bpf: Test which ctx registers a uprobe prog can write

    A uprobe program can write only the pt_regs fields that ptrace can set
    to any value. Add verifier tests for x86-64, arm64 and riscv:

      - writes to the general registers, the stack pointer and the
        instruction pointer load
      - writes to cs, ss, flags and orig_ax on x86-64, to pstate and
        orig_x0 on arm64, and to status, badaddr and cause on riscv fail
        with "invalid bpf_context access"
      - a 4-byte write to the low half of a read-only field fails too
      - reading a read-only field still works
      - on x86-64, a kprobe program that writes ctx->flags fails to load

    Other arches get one test that just loads.

    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://patch.msgid.link/20261118084451.12590-3-jolsa@kernel.org

diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 6a494c381266..5a31f04677e0 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -131,6 +131,7 @@
 #include "verifier_unpriv.skel.h"
 #include "verifier_unwind.skel.h"
 #include "verifier_unpriv_perf.skel.h"
+#include "verifier_uprobe_write_ctx.skel.h"
 #include "verifier_value_adj_spill.skel.h"
 #include "verifier_value.skel.h"
 #include "verifier_value_illegal_alu.skel.h"
@@ -321,6 +322,7 @@ void test_verifier_uninit(void)               { RUN(verifier_uninit); }
 void test_verifier_unpriv(void)               { RUN(verifier_unpriv); }
 void test_verifier_unwind(void)               { RUN(verifier_unwind); }
 void test_verifier_unpriv_perf(void)          { RUN(verifier_unpriv_perf); }
+void test_verifier_uprobe_write_ctx(void)     { RUN(verifier_uprobe_write_ctx); }
 void test_verifier_value_adj_spill(void)      { RUN(verifier_value_adj_spill); }
 void test_verifier_value(void)                { RUN(verifier_value); }
 void test_verifier_value_illegal_alu(void)    { RUN(verifier_value_illegal_alu); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c b/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
new file mode 100644
index 000000000000..f5ed453b1798
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+/*
+ * A uprobe prog can write only the registers that ptrace can set to any
+ * value. The fields the kernel restores on the way to user space (segments,
+ * flags, privilege state) are read-only.
+ */
+
+#define WRITE_CTX(name, field, sz)					\
+__naked void name(void)							\
+{									\
+	asm volatile ("							\
+	r2 = 0;								\
+	*(" #sz " *)(r1 + %[off]) = r2;					\
+	r0 = 0;								\
+	exit;								\
+"	:								\
+	: __imm_const(off, offsetof(struct pt_regs, field))		\
+	: __clobber_all);						\
+}
+
+#define READ_CTX(name, field)						\
+__naked void name(void)							\
+{									\
+	asm volatile ("							\
+	r0 = *(u64 *)(r1 + %[off]);					\
+	r0 = 0;								\
+	exit;								\
+"	:								\
+	: __imm_const(off, offsetof(struct pt_regs, field))		\
+	: __clobber_all);						\
+}
+
+#if defined(__TARGET_ARCH_x86)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ax")
+__success
+WRITE_CTX(write_ax, ax, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->r15")
+__success
+WRITE_CTX(write_r15, r15, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->di")
+__success
+WRITE_CTX(write_di, di, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ip")
+__success
+WRITE_CTX(write_ip, ip, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->sp")
+__success
+WRITE_CTX(write_sp, sp, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->ax")
+__success
+WRITE_CTX(write_ax_u32, ax, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->orig_ax")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_orig_ax, orig_ax, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->cs")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cs, cs, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->cs")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cs_u32, cs, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->flags")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_flags, flags, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->ss")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_ss, ss, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->flags")
+__success
+READ_CTX(read_flags, flags)
+
+SEC("kprobe")
+__description("kprobe: write ctx->flags")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(kprobe_write_flags, flags, u64)
+
+#elif defined(__TARGET_ARCH_arm64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->regs[0]")
+__success
+WRITE_CTX(write_x0, regs[0], u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->regs[30]")
+__success
+WRITE_CTX(write_x30, regs[30], u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->sp")
+__success
+WRITE_CTX(write_sp, sp, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->pc")
+__success
+WRITE_CTX(write_pc, pc, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->pstate")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_pstate, pstate, u64)
+
+SEC("uprobe")
+__description("uprobe: write lower half of ctx->pstate")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_pstate_u32, pstate, u32)
+
+SEC("uprobe")
+__description("uprobe: write ctx->orig_x0")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_orig_x0, orig_x0, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->pstate")
+__success
+READ_CTX(read_pstate, pstate)
+
+#elif defined(__TARGET_ARCH_riscv)
+
+SEC("uprobe")
+__description("uprobe: write ctx->epc")
+__success
+WRITE_CTX(write_epc, epc, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->a0")
+__success
+WRITE_CTX(write_a0, a0, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->t6")
+__success
+WRITE_CTX(write_t6, t6, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->status")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_status, status, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->badaddr")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_badaddr, badaddr, u64)
+
+SEC("uprobe")
+__description("uprobe: write ctx->cause")
+__failure __msg("invalid bpf_context access")
+WRITE_CTX(write_cause, cause, u64)
+
+SEC("uprobe")
+__description("uprobe: read ctx->status")
+__success
+READ_CTX(read_status, status)
+
+#else
+
+SEC("uprobe")
+__description("uprobe: ctx writes not checked on this arch")
+__success
+__naked void unsupported(void)
+{
+	asm volatile ("r0 = 0; exit;" ::: __clobber_all);
+}
+
+#endif
+
+char _license[] SEC("license") = "GPL";