Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.133 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100122; b=bIdFfPNJ/M/IbZOkKyVOCgmeGMLgt8Omv90SGK+vnyXgsSXL0dnHpo99HzdKz3cR5w8p 59WuNYULpQbmLWp5HiSY9kbBpXYblqPJOnxQTVS4EObVjQRybGQzc26ZxpM9dPrGX89eo yr0jfyP5pN1ul1tu7MprBeZqOY9xoX7Oq24poY4Dusl7N8wQMZ8+25TrZLZUoQlGZWrNb 1gYI5JLrEQhSrZIaOC+fqjuUeYQH/1FkUGanfWhc14fD74l90GNC385wwVUFW8bkDLIjw S1vYAcWZlZPvZL5GqC5H6CQVxobT78E8NIvFGTZ8VDaRpHKSMcoYo5JxcKA+MV4xN/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100122; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=whgAJUSnNZLPllRQdWrVkDxtR54+diyYXNavTLJ36+Q=; b=hGa7Xq3NRfBNsWQcS1fm+WEoma3HsAIznxK6ThdINre4r+MIQ4To3JygqYDQDzcz7cip XF+GKN+gnV7K+YAGBL2dOm+VGlpNf/H4c0+1NSh5+x2IGv7cRNad4riKhnOYCyc+sTxHD tqwH5XHsG3K9un3laEUqEr+unjnqUpIURy2T7MvaUliMAGHZqLCHX428dQl5zHH5MZl05 M5eLukEPE6YEsIbgsnuK+KvfzbfuMowKKJCom5rtdk3vflipdI4iqPHIyU+XZVTbu5/+q 2mgV531+o46XLfcTk5rdpnDocMPhhfo+WY3wsg3Y64eB/Z71Qt98FZ26j3ea3SrnxLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=zeq1dt2L; arc=none smtp.remote-ip=74.125.231.133 Received: from mail-oo2-f5.google.com (mail-oo2-f5.google.com [74.125.231.133]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 331CC606A31A for ; Tue, 1 Dec 2026 04:42:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 331CC606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=zeq1dt2L Received: by mail-oo2-f5.google.com with SMTP id 46e09a7af769-86203d56413so1793631a34.1 for ; Mon, 30 Nov 2026 20:42:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100121; x=1796704921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=whgAJUSnNZLPllRQdWrVkDxtR54+diyYXNavTLJ36+Q=; b=zeq1dt2L2N45W1NYdO+qlDfjhHQjMRDr7kA79mQ8oxwv6nppIdViaSIL8T487Vz/qn 7htAbaL1Cgw4as6yMWz/hcRXMO+Y9RWZZsZILFie+27JhHN9LqXXmirAIJltmfcTW17R LOqlsOqZgClJrMUH9oPCSHsOjwEyeuNQ26erFESLBDW4gVhsnU+EcRBrhjvR0B1Zt1te zFb6PgXPrAvGTdZoGAj2MaFURnB0+BdpJO1JHf0XkaItbZ0NJROgZ/IS1VFceC7TDfHl KcsbUyvo3Isn/AvVoO0uGgj50C4nQqZjzSMxKuGFPBrJNs61wCF6MMy2jf76D41vZDnf /XLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100121; x=1796704921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=whgAJUSnNZLPllRQdWrVkDxtR54+diyYXNavTLJ36+Q=; b=YIbes6Ok75qQwFltszIW0UP8L9p4lsLe8y1SxHLSHy4lLW6pEHyDm/HDz73s9k7N3g ETfv3tMsy1R8RsU2bQajJaR4dtfXGf+arpN920Mc2guaRk/CHngY2cfKHUuqhmuthMM1 4+UX1AdYDek9H9V1CdI7yJRcOw5iDpGxQk5hYKJxU7/zmXVTvMMAX5fe7m8T9n/dui0R oseYy6tscYR+LWBjdvMLgCe8GHiHYPNMofE6STohLiZOEqNdxiTQO6ikcReu1iVImE70 kJusGTvoBHZCqD5blG/SlDY8KYt4s4VE6rJ4vAyjwBHN2Gx5h/0U+3sl/f+VA0Afprac ztFQ== X-Gm-Message-State: APuU4Z9ox6k4CK6RU9Fj15JzYyqSo7knhITIcEAiOgu6H7N4q3LkZybV IR+AvMfFg5vv7G9FdAbIwi6GcB+snrq6hGe+asN6CMltWZjIHjv/s9MVutpyy4pwiDE= X-Gm-Gg: AZFdTZe7jEipoVj6nYmVcCU4pjUU8SW+9V5vvyI1b65ImxgooMCl/Y6o/bfwtpbyCRK kyYdpCm2fZZ85PTwrj0p24gJ5WZJJuosvHy7RrEGWVDQYS6XuW3xJUPxqZhyIRXsfoULisKa2Zc kwUxtDSXyS8vMKHvXPETubS/HpDpUFSKk++9AFObqk4RgTZHRpZFVhw6vGEYArJmckw4st2ppEO o+9YllYkisnB15zd7ejebAL+tj49Gtcw10OMYODlqk6UNSe/cIjgcsGxcJxp/4Gu53VSDTGSq9Q LeSdgs2Yj3daZSLCBJuBUncQZjweuL1NMRkDqdMUZriBBWB2g6OVgPxpd2aMt7wGIdrWbMMUG4Q qGFBX+i0qiP0mKYvyIKfMNL4nk1ogNTPudd9qoJ9Y0UADTmNTbRaeEZpkR1OHC7nc4TeseqXU39 4ulIEJNuziqAMDbRkgo16EoiZ/PX1L9kO2PAdyfll+K/xiMXErvqb5GQbQ0G68zDo4wAI3mWFhD g/5sYOCHp5yTrKY3nSAHV1un141rVq4iMdwnab8kHwiEi8znqp+CvxotEee2JSpKudPesR5dBHp 37fI27mM3OPZ2D2mD+Aqr/5jFsrS4I3RU0I9QSq8O232c1Jl85BvvD1AUq7MlMt0JoUiEFqYZat U8u+bEetqjACid9mwvxRawkN3xMSmBVoOv/74 X-Received: by 2002:a05:6830:25d1:b0:829:8a9b:de79 with SMTP id 46e09a7af769-863151bb287mr36086667a34.4.1796100121044; Mon, 30 Nov 2026 20:42:01 -0800 (PST) Received: from localhost ([2a03:2880:10ff:55::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863168bdaf7sm26170167a34.5.2026.11.30.20.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:59 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 39/73] bpf: Refuse freplace on a function that can unwind Date: Tue, 1 Dec 2026 05:39:06 +0100 Message-ID: X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6693; i=memxor@gmail.com; h=from:subject:message-id; bh=yL2h7wVi+m+DQyui1x+R3XOcm5yqg1b8QvFhXt/ALjA=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb0H1nFXed3y+Kmp8+S3k8qV63Y4cbb/+KSYVpktFClZu lOnuKGVhEONikBVTZCn5v4/J+ETl70DbZdwwc1iZQIYwcHEKwETSGxj+yj8Jyt2wLaT76M3YlTy7dz g/uGNsz+Z3Zqnk/OhKV9Glixj+10vqbT1hfyFlprDxjTnacwpFvu74uNuk64EaQ+JKnugGBgA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit From: Alexei Starovoitov Currently an extension can replace a global function that can unwind: __weak int policy(int nice) the caller: { call policy while (!ready) unwind_goto pad ; ... return 0; } The verifier adds a may_goto to the loop, so the kernel can cancel the program in policy(), and each caller gets unwind_goto and a landing pad after its call for that. An extension is a program of its own. When it is cancelled, it ends in its main program and returns: it never unwinds into the program that it extends. With an extension in place of policy(), a cancellation inside the extension would end the extension only. The caller would continue after the call, past the landing pad that it has for this case. Refuse to replace a function that can unwind. bpf_check_attach_target() already refuses fexit and fsession on such a function. Let the same check cover freplace, with the same message: Only fentry can attach to policy, which can unwind At this point of the series a function can unwind if it has unwind_exit, a may_goto of the verifier, a recursion check or an access to arena that cancels the program, or if it calls such a function and has no unwind_goto of its own after the call. In a program with an arena that is most functions. The main program leaves with exit, so the check skipped it until now. Mark the main program when it has a site, and refuse freplace on it then. fexit and fsession stay allowed there. An extension with a loop of its own loads and attaches as before. libxdp attaches XDP programs that way, to the placeholder functions of its dispatcher, which cannot unwind. Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 6 +++++- kernel/bpf/fixups.c | 6 ++++-- kernel/bpf/verifier.c | 11 +++++++---- .../testing/selftests/bpf/prog_tests/unwind_attach.c | 3 ++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 3d699dd1045f..91d8831de4e9 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1475,7 +1475,11 @@ struct bpf_subprog_info { bool has_tail_call: 1; bool might_throw: 1; bool throws: 1; /* as the main pass found it, callx included */ - bool can_unwind: 1; /* has unwind_exit */ + /* + * It has an unwind_exit, or a landing pad of it leaves with one, see + * bpf_mark_subprogs_that_unwind(). Subprog 0 has the bit for such a site, for freplace. + */ + bool can_unwind: 1; bool recursive: 1; /* on a cycle of the call graph */ bool in_recursion: 1; /* recursive, or a recursive subprog gets to it */ bool addr_taken: 1; /* callx calls nothing else */ diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 75ad6f567e14..a20830a628e7 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -650,7 +650,8 @@ bool bpf_site_is_used(struct bpf_verifier_env *env, int i, const struct bpf_pad_ * The main pass looked at more ways to unwind than the program has in the end: a loop that has a * trip count after all has no may_goto. Find the subprogs that get unwind_exit: those that are * left at a site. A call is a site if it calls one of them. The main pass asks the same when a - * global subprog unwinds, see settle_late_unwinds(). + * global subprog unwinds, see settle_late_unwinds(). The main program leaves with exit: it is + * marked for bpf_check_attach_target(), see bpf_insert_sites(). */ void bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env) { @@ -664,7 +665,7 @@ void bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env) changed = false; for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; - if (sub->can_unwind || !sub->bail_unwinds) + if (sub->can_unwind || (s && !sub->bail_unwinds)) continue; for (i = sub->start; i < sub[1].start && !sub->can_unwind; i++) { sub->can_unwind = aux[i].loop_guard || @@ -903,6 +904,7 @@ int bpf_insert_sites(struct bpf_verifier_env *env) goto out; } bpf_mark_subprogs_that_unwind(env); + env->prog->aux->can_unwind = env->subprog_info[0].can_unwind; for (s = 0; s < env->subprog_cnt && !err; s++) { sub = &env->subprog_info[s]; cnt = collect_pads(env, s, new, pad_aux, &pads); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 07fdc4fc7ebc..784810772b11 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -25437,10 +25437,13 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, prog_extension ? "Extension" : "Tracing"); return -EINVAL; } - /* unwind_exit of the main prog is exit */ - tgt_can_unwind = subprog && aux->func && aux->func[subprog]->aux->can_unwind; - /* unwind_exit would return to the trampoline that called the target */ - if (tgt_can_unwind && !prog_extension && + tgt_can_unwind = aux->func ? aux->func[subprog]->aux->can_unwind : aux->can_unwind; + /* + * unwind_exit or cancel_exit would return to the trampoline that called the target. + * The main program leaves with exit. An extension ends in its own main program and + * never unwinds into its caller, so it replaces no function that can unwind. + */ + if (tgt_can_unwind && (subprog || prog_extension) && prog->expected_attach_type != BPF_TRACE_FENTRY) { bpf_log(log, "Only fentry can attach to %s, which can unwind\n", tname); return -EINVAL; diff --git a/tools/testing/selftests/bpf/prog_tests/unwind_attach.c b/tools/testing/selftests/bpf/prog_tests/unwind_attach.c index 674c5272cf75..5ec96f478493 100644 --- a/tools/testing/selftests/bpf/prog_tests/unwind_attach.c +++ b/tools/testing/selftests/bpf/prog_tests/unwind_attach.c @@ -103,8 +103,9 @@ void test_unwind_attach(void) /* unwind_exit would return to the trampoline */ attach(tgt_fd, "global_unwinds", "fexit_unwind", "Only fentry can attach to global_unwinds", 0); - attach(tgt_fd, "global_unwinds", "freplace_returns", NULL, 9); /* Nothing unwinds from an extension into the prog that it extends */ + attach(tgt_fd, "global_unwinds", "freplace_returns", + "Only fentry can attach to global_unwinds", 0); attach(plain_fd, "global_never_unwinds", "freplace_unwinds", "unwind_exit in the main program of an extension", 0); ASSERT_EQ(run(tgt_fd), 15, "retval"); -- 2.53.1-Meta