summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorKumar Kartikeya Dwivedi <memxor@gmail.com>2026-09-24 19:06:35 +0200
committerAlexei Starovoitov <ast@kernel.org>2026-09-24 21:05:55 +0000
commit9774ae5951c21e2c145a88ccbf9899f727dc2e2a (patch)
tree1d3a4d01ff0ef6a1048e75f996aa78763668dffe /kernel
parent9bf1f086e7dd74ab6da594cdb88a55dd39eb0af9 (diff)
bpf: Correct verifier diagnostic attribution for stack reads
Verifier memory diagnostics currently label every fixed-offset stack read rejected by check_stack_read_fixed_off() as uninitialized. Dynptr, iterator, and IRQ-flag slots instead contain initialized verifier-managed state, so the report incorrectly suggests initialization or CAP_PERFMON. The variable-offset read without a destination register is currently reached by atomic read-modify-write instructions, but that follows from the call sites rather than the check_stack_read() interface. Its diagnostic also attributes the access to a helper. Classify rejected stack reads by slot type. Retain the existing uninitialized report for STACK_INVALID, and describe verifier-managed slots as opaque state. Use instruction-neutral wording for the variable-offset read while leaving the existing verifier messages unchanged. Reported-by: Sashiko <sashiko-bot@kernel.org> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20260815065956.49D2B1F000E9@smtp.kernel.org/ Link: https://patch.msgid.link/20260924170646.2366016-2-memxor@gmail.com
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/verifier.c63
1 files changed, 45 insertions, 18 deletions
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 7fd00c540cff..7f220fbcc7e9 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -3979,19 +3979,46 @@ static int mark_reg_stack_read(struct bpf_verifier_env *env,
return 0;
}
-static void bpf_diag_stack_read_uninit(struct bpf_verifier_env *env, int off, int i,
- int size)
+static void bpf_diag_stack_read_invalid(struct bpf_verifier_env *env, int off, int i, int size,
+ enum bpf_stack_slot_type type)
{
- const char *reason;
+ const char *problem, *reason, *suggestion, *kind;
+
+ if (type == STACK_INVALID) {
+ reason = bpf_diag_fmt(
+ env, "This rejected read uses %d bytes at stack offset %d, but byte %d in that range is uninitialized on this path. "
+ "Programs loaded with CAP_PERFMON can be allowed to read uninitialized stack bytes, but this program is being rejected without that allowance.",
+ size, off, i);
+ bpf_diag_memory(
+ env, env->insn_idx, "uninitialized stack read", reason,
+ "Initialize every byte in the stack range before reading it, adjust the offset and size so the read covers only initialized bytes, "
+ "or load with CAP_PERFMON if uninitialized stack reads are intended.");
+ return;
+ }
- reason = bpf_diag_fmt(env,
- "This rejected read uses %d bytes at stack offset %d, but byte %d in that range is uninitialized on this path. "
- "Programs loaded with CAP_PERFMON can be allowed to read uninitialized stack bytes, but this program is being rejected without that allowance.",
- size, off, i);
- bpf_diag_memory(
- env, env->insn_idx, "uninitialized stack read", reason,
- "Initialize every byte in the stack range before reading it, adjust the offset and size so the read covers only initialized bytes, "
- "or load with CAP_PERFMON if uninitialized stack reads are intended.");
+ switch (type) {
+ case STACK_DYNPTR:
+ kind = "dynptr";
+ suggestion = "Use dynptr helpers or kfuncs to access the object represented by the dynptr instead of reading the dynptr state directly.";
+ break;
+ case STACK_ITER:
+ kind = "iterator";
+ suggestion = "Use iterator kfuncs to advance or destroy the iterator instead of reading its state directly.";
+ break;
+ case STACK_IRQ_FLAG:
+ kind = "IRQ flag";
+ suggestion = "Pass the saved IRQ flag to the matching restore kfunc instead of reading its state directly.";
+ break;
+ default:
+ return;
+ }
+
+ problem = bpf_diag_fmt(env, "direct read of %s stack state", kind);
+ reason = bpf_diag_fmt(
+ env, "This rejected read uses %d bytes at stack offset %d, but byte %d in that range belongs to verifier-managed %s state. "
+ "This state has an opaque representation that BPF programs cannot read directly.",
+ size, off, i, kind);
+ bpf_diag_memory(env, env->insn_idx, problem, reason, suggestion);
}
/* Read the stack at 'off' and put the results into the register indicated by
@@ -4083,7 +4110,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
} else {
verbose(env, "invalid read from stack off %d+%d size %d\n",
off, i, size);
- bpf_diag_stack_read_uninit(env, off, i, size);
+ bpf_diag_stack_read_invalid(env, off, i, size, type);
}
return -EACCES;
}
@@ -4142,7 +4169,7 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
} else {
verbose(env, "invalid read from stack off %d+%d size %d\n",
off, i, size);
- bpf_diag_stack_read_uninit(env, off, i, size);
+ bpf_diag_stack_read_invalid(env, off, i, size, type);
}
return -EACCES;
}
@@ -4240,13 +4267,13 @@ static int check_stack_read(struct bpf_verifier_env *env,
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n",
tn_buf, off, size);
- reason = bpf_diag_fmt(env,
- "The helper would access the stack through variable offset %s plus fixed offset %d and size %d. "
- "Helper stack memory arguments require a constant stack offset and a precise initialized range.",
+ reason = bpf_diag_fmt(
+ env, "The instruction would access the stack through variable offset %s plus fixed offset %d and size %d. "
+ "This stack access requires a constant stack offset and a precise initialized range.",
tn_buf, off, size);
bpf_diag_memory(
- env, env->insn_idx, "variable stack access", reason,
- "Use a fixed stack offset for helper memory arguments, or copy the needed bytes into a fixed stack slot first.");
+ env, env->insn_idx, "variable-offset stack access", reason,
+ "Use a fixed stack offset for the instruction, selecting the target stack slot on separate control-flow paths if necessary.");
return -EACCES;
}
/* Variable offset is prohibited for unprivileged mode for simplicity