From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795017247; b=RjGyQdsQva19de6GWdDDDbEzkSTTYuOF9FQCl+HwB+r9nGdz9fIEJWWsSDc4oL1QK0nt 3AK+BnY4gNOKlkxMvYCpBRmUxsPdIzGeUuOj+1om//jIiEYA0cbWS3aJqraESOi1+vK5W 1Fvh6wLCVaofBSWWJpQDDSeEIbCR18fJj8nviYpmWoYvX73IaHbWW1ByGWZpMsSNnvIKy hgB99IuhWJOUIlcFWtWOrTOxsZfhvAvuhAnxiAQLSiI0aw+1NWo7HLXa3AxYZczhwxUB6 d/EugzAhZZVgrLbu0cDdboP2L0NvfQ6KWM5604DpyGwBYaDsfah0wvnSaKvZ5m4uuCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795017247; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=RpekKvLF9VIC5gI4exZpdvvCuMrMuhzRbvJI6H4Z7ak=; b=q4Kj/c7ReAeFavZDQJ799s6M+C1/Lp10nefPg9XpYFBEINnCqcYVHaegZ22RDEUbDSkk 2ILhDblLq7U8P+nE02my5afpijdutv/nbJrsapk0WEtXOxNa+al2nUyWuAU1lFJWutBqy qwlRndO/7pGXg5bOlmWCKrX4aggEKhg/zGMUP4TRXoJWLY1WCCndHJNrFND27X9+7CDlY 4FNzd2YsxtOlLy0Mo1mTH+cDLqxV285jEpaT6Q85T3iqCX4NCK/x8DkYQkuq9AZDp6mGQ thxQ3GHA9hV06KcgFFzkzeDlyzQ6wjmoIVgW8iOYxKJTzMUJsrUX4pNTrADOAbd9ljg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=EeUsEgCS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F372606A33F; Wed, 18 Nov 2026 15:54:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=EeUsEgCS Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CE2C1F000CA; Wed, 18 Nov 2026 15:54:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795017247; bh=RpekKvLF9VIC5gI4exZpdvvCuMrMuhzRbvJI6H4Z7ak=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EeUsEgCSddEa1xeKEvsJpXSZXo+K1XZ6DtGwNAv8QjPMX/FzUeZhl3s8hQx+4+siQ q9HVmYN4N29wDkbBB6MhnAWNlaAAvvLsu6BGFwZKD9KyH37K1xAtVfVvM5UNuzdcT3 RjimIUKUSj2V5QPT4aZXwEzESvI6IQp+4TrYeBjE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Sasha Levin Subject: [PATCH 7.3 0815/2651] bpf: Adjust pc-relative insn copied into its own patch Date: Wed, 18 Nov 2026 15:34:15 +0100 Message-ID: <20261118150317.238679710@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150247.972732815@linuxfoundation.org> References: <20261118150247.972732815@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 67a3b916a7ae7c1a6bed08555417d444de6f5838 ] bpf_do_misc_fixups() inits may_goto count by patching the first insn of a subprog with [ST, ST, first insn]. bpf_convert_ctx_accesses() patches insn that needs a barrier with [nospec, insn]. bpf_adj_branches() doesn't adjust insns inside the patch, so when the copy is a call, ld_imm64 of a func or a jump that points backward its target is off by the number of insns in front of it: static int A(void) { return 1; } static int A2(void) { return 2; } static int B(void) { int i, ret = A(); for (i = 0; i < 10 && can_loop; i++) ... return ret; } When A is placed in front of B and may_goto is timed 'call A' becomes call of A + 2. B returns 2 when A + 2 is the start of A2. Otherwise the prog is rejected with "verifier bug: No program to jit at insn". Add bpf_adj_moved_insn() and use it for both copies. Fixes: 011832b97b31 ("bpf: Introduce may_goto instruction") Fixes: d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20260924233130.1213812-4-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- kernel/bpf/fixups.c | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index d6f83521fc78e..e187e51120858 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -355,6 +355,44 @@ struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, return new_prog; } +/* + * insn was moved down by delta insns inside its own patch. Operands relative + * to the pc that point in front of the old position did not move with it. + */ +static int bpf_adj_moved_insn(struct bpf_insn *insn, u32 delta) +{ + u8 class = BPF_CLASS(insn->code), op = BPF_OP(insn->code); + s64 off = insn->imm, off_min = S32_MIN; + bool is_imm = true; + + if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) { + /* subprog that started at the old position starts with the patch */ + if (off >= 0) + return 0; + } else if ((class == BPF_JMP || class == BPF_JMP32) && + op != BPF_CALL && op != BPF_EXIT) { + if (insn->code != (BPF_JMP32 | BPF_JA)) { + off = insn->off; + off_min = S16_MIN; + is_imm = false; + } + /* jump to itself stays */ + if (off >= -1) + return 0; + } else { + return 0; + } + + off -= delta; + if (off < off_min) + return -ERANGE; + if (is_imm) + insn->imm = off; + else + insn->off = off; + return 0; +} + /* * For all jmp insns in a given 'prog' that point to 'tgt_idx' insn adjust the * jump offset by 'delta'. @@ -845,7 +883,13 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env) struct bpf_insn *patch = insn_buf; *patch++ = BPF_ST_NOSPEC(); - *patch++ = *insn; + *patch = *insn; + ret = bpf_adj_moved_insn(patch++, 1); + if (ret) { + verbose(env, "insn %d cannot be patched due to 16-bit range\n", + env->insn_aux_data[i + delta].orig_idx); + return ret; + } cnt = patch - insn_buf; new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); if (!new_prog) @@ -2477,7 +2521,14 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) BPF_MAX_LOOPS); } /* Copy first actual insn to preserve it */ - insn_buf[cnt++] = env->prog->insnsi[subprog_start]; + insn_buf[cnt] = env->prog->insnsi[subprog_start]; + ret = bpf_adj_moved_insn(&insn_buf[cnt], cnt); + if (ret) { + verbose(env, "insn %d cannot be patched due to 16-bit range\n", + env->insn_aux_data[subprog_start].orig_idx); + return ret; + } + cnt++; new_prog = bpf_patch_insn_data(env, subprog_start, insn_buf, cnt); if (!new_prog) -- 2.53.0