From patchwork Thu Feb 20 22:15:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13984561 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0BC71DE881 for ; Thu, 20 Feb 2025 22:15:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740089744; cv=none; b=tVI18ut8CkvLinL2Aqrna6Bbs4xgUJ40Br/GsYBoU2NBL9cpw9+l2XIwfXSg2+6FI0pFn85b0qghvIwYzsECbsRy7rwzNau6kxKCJG1Mf4JNrkGi+dA5C+flODaqCDZZNuLaLThdh7OFZsoxsHHrTmd1GUJi6byHfAY92rGL0gY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740089744; c=relaxed/simple; bh=FjmkIwblnzc1BffhJswgrozzGN4BEEZJr3M+4kThQoo=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=b+2KqkIoqaoV75LXewestDw9MA5ukvLI5DgoEpks33rfjqLYqWxD4duAqIkWPB6v0sysw+A6xvRn1+I/yeWorbP7bfMGDaP2i6AwcgFspymZ1RKYrntTA1pTw5qNSEFlHPKH4YUnaSvv/pj/l+JOwocX/hktfsGHT0O/pPuu1jc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nfOXnPqC; arc=none smtp.client-ip=209.85.214.180 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.b="nfOXnPqC" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2211acda7f6so30969465ad.3 for ; Thu, 20 Feb 2025 14:15:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740089742; x=1740694542; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VUlEJReGACOX1GpKQqg6MAi5kmt0tU9SUBqX7PBIS2Y=; b=nfOXnPqCJ5GSuE7qZrLq6iRNYA/D6gL8IvWBnd6HoF/EA56lqr5BEOKRqPrq3LZC6R ZDUnJAj8ZgpTcaHrnSPjFQgZePY5V0P4p3WDCRN2zvrRSVguBVw1zgRoXb/Gi04BON1u XclOIhwTdlreuDxdsymqRgq5xbe+6pjm8fo7P77+LbCRdPpb1wjwsRGZ8CxX8f8XZIvX hSFEKEGOv1bm3BdC1oeom3x59RzkIs5pPdi1KOIGyscZIErZ/3jhsVTAkMvlmi6jYCCM Fi0jpGfx6PpNa/K5+vrrHTkiMoKU4UBxiXBgMqnvN2dHzCd04rwNi0imanSMg6PCqCB7 LR/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740089742; x=1740694542; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VUlEJReGACOX1GpKQqg6MAi5kmt0tU9SUBqX7PBIS2Y=; b=Phee8Cp6IzrVGE/P84gfugm1ShZNrHAhS/p0RYJdXrpNBmlA6/AoMzsfBfCEfvqkcU gqYBu7wpMG9y0/K+qLyodmBO+T2pH+SPEMxWMRa7B3OzSkAuYBgxbTEmy3+Fjwfq127w sGlbP2psm+MYbhOZ1F46U5M7HV7F5fKZaYDyZKDXWGRZ1/Zc0pFpSIi1g1JZdN2joad9 RAsWkDcOuv2rr6UYXgRIbeMr0ASRNy39L8MDPblunoBwEFyMjkKN0dWA3QoIRs7r1qfI ynEG4PErgQvT1FKkau8noZrKiFLs41ZkZQrEJz8rfEdOYtNZDc5L+ynDuRvt1/0GIYRF u4ig== X-Gm-Message-State: AOJu0YwqwDuj7WEcYf+vcu4lKL+h3BuUxQCHm7HVhpKMxSBksmZoyA6n wPyWaPdHQGCk/pIUAlI3+3Vesce1CpL9I0nm0xc3Zg59wHjRKKN0auqThw== X-Gm-Gg: ASbGnctdYOWy8IOX/+el+ihdhF4Dxj8WCCFDkDKaC5lu7NSQq7zkcwAgCAHTQ3xShHb tvU1rVegEvO+Hjx75O0Pvni61jU/h2xcJx3bnVtFmFPOpt8wp+a1t5L7iZXDEsIK0RlhNEjFOpn m6I7JxnvYGlfWr8INi5hFZmnQaDPDK1rabCAHZlgfSaxz6SKqWFcmJ5OrjBhvLKp3vHatGGIBJV /HOKq8CpCfAtr54LZQ5jt2dX4cZUPNZFzeRKLxIicuUSPj/4zmAy8DMetvWC7jiVChQUEtXm+/I pbL54b2tRnOFFAMVFhwBIBxcXBCUiSbDSI2x4H7tkXMNhPuwW6qh3KN3di9IRpnolg== X-Google-Smtp-Source: AGHT+IE0o29TVyfOFlh2vD/cbCwxpOck7R/98+Q4l5kPCCcae9WeaU7yWTccN+WaDQjeTrqyNCht8A== X-Received: by 2002:a05:6a00:8c2:b0:730:7d3f:8c6c with SMTP id d2e1a72fcca58-73426d9002cmr802260b3a.22.1740089741897; Thu, 20 Feb 2025 14:15:41 -0800 (PST) Received: from localhost.localdomain (c-76-146-13-146.hsd1.wa.comcast.net. [76.146.13.146]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-732425467adsm14852251b3a.15.2025.02.20.14.15.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 14:15:41 -0800 (PST) From: Amery Hung To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, alexei.starovoitov@gmail.com, martin.lau@kernel.org, ameryhung@gmail.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 1/2] bpf: Do not allow tail call in strcut_ops program with __ref argument Date: Thu, 20 Feb 2025 14:15:31 -0800 Message-ID: <20250220221532.1079331-1-ameryhung@gmail.com> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Reject struct_ops programs with refcounted kptr arguments (arguments tagged with __ref suffix) that tail call. Once a refcounted kptr is passed to a struct_ops program from the kernel, it can be freed or xchged into maps. As there is no guarantee a callee can get the same valid refcounted kptr in the ctx, we cannot allow such usage. Signed-off-by: Amery Hung Acked-by: Eduard Zingerman --- kernel/bpf/verifier.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 85b2d4e65834..8f1df279e432 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -22450,10 +22450,11 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) const struct bpf_struct_ops *st_ops; const struct btf_member *member; struct bpf_prog *prog = env->prog; + bool has_refcounted_arg = false; u32 btf_id, member_idx; struct btf *btf; const char *mname; - int err; + int i, err; if (!prog->gpl_compatible) { verbose(env, "struct ops programs must have a GPL compatible license\n"); @@ -22523,6 +22524,23 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) return -EACCES; } + for (i = 0; i < st_ops_desc->arg_info[member_idx].cnt; i++) { + if (st_ops_desc->arg_info[member_idx].info->refcounted) { + has_refcounted_arg = true; + break; + } + } + + /* Tail call is not allowed for programs with refcounted arguments since we + * cannot guarantee that valid refcounted kptrs will be passed to the callee. + */ + for (i = 0; i < env->subprog_cnt; i++) { + if (has_refcounted_arg && env->subprog_info[i].has_tail_call) { + verbose(env, "program with __ref argument cannot tail call\n"); + return -EINVAL; + } + } + prog->aux->attach_func_proto = func_proto; prog->aux->attach_func_name = mname; env->ops = st_ops->verifier_ops; From patchwork Thu Feb 20 22:15:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13984562 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B11F11EB9ED for ; Thu, 20 Feb 2025 22:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740089745; cv=none; b=TxOTk95FlkrZhDeb+i9/75JJEr1dcd3i0WXLwhR7w//RmgJcLyTLYlBKPIxshkf4txhzlDL7K0yAvx+ZMYQNaBOydBZ/hdr1PDj6sVNV1StEOiXdaELy1LOzkWbLaBxuZa+eJSRQYf0G381jO5VB2ATk7I2AuwNc9BzkxFdHWnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740089745; c=relaxed/simple; bh=D3amauyLaaZw/EoRfPBN+QHv39Kf9g2r4RygCwuhtmA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rjt36TQqk/DyDQTyQdJyhOtbINIjVLWRvEy0xcudtkI3hQlzsXeETQyA7BcgwbheKAHlnL+ATvbKzLx7lZZEKFPDLNuI/tFR0p+kDDr13SxPuLnmthZeHhpUI6XRJXEhkAoggNYAraJJEX+Sfzs55VfxrePMLL8FxNUNf5hxt0s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=La2Y5EqS; arc=none smtp.client-ip=209.85.214.175 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.b="La2Y5EqS" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-221206dbd7eso28968985ad.2 for ; Thu, 20 Feb 2025 14:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740089743; x=1740694543; 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; bh=13YlboN/hEjGLL5hNA6CwAwrc5jCiPOW2r8f54Ki1Ys=; b=La2Y5EqSA6tMNLznXdq664HzGkzutzJ+2Ost2Oij4Xjkr3vpk3SpHe6w6fbqmMK3xy qR6rbo5XrJ+96RYbd8F7wiisWqZAcczUQPkJIbOnV38aYAhqW5k5Ktw9S9ulVZKQ302Y PKUMKxhCh0VjvDOxOvWswWf7SSyXYalBOo5CRk5Osc3h0cz/pJcMHMmH60SSpaTRJ8oB eYx2tebV9YCr0mLcVHNtdwHLzfMapNyTLxzJitdMtlkCoGpzF8LMFZaY3Vm0a0JG6F+a z61HKuPOgS+9eemg5cWlzZmBOMoIOVL6tsQ0gwPiSpJGQ5BQ3sA7bM4uVSBJeQkxNi7W ICjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740089743; x=1740694543; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=13YlboN/hEjGLL5hNA6CwAwrc5jCiPOW2r8f54Ki1Ys=; b=VZeaFRSI9T/3BDvyxbjDgRlLOIDK2Z2q+1KU9XO++zBn6kOx9AiFmkIhyVzzzMAE+S 46QUXzeDbNP0O2vgWWzwTJcAiUw9EgSm7gpbtqwdOm6PHqOKiHNslO9zV9jn1UPub7TT UxsUpH09SxMf3ewFVuHXaNxDh52rXdG0DjIVwh/ZxZj+IjelvP0Ypwi+FcGdKEL/iu1Y ujob7iRu4U3eN6jY6EMTyaCCywOktULTrTarbVFib9GljJRvoROWpptt+TqoEDRHMsQQ VIoXNRxYvGXoMcIJmHpoiCD4beiPZUlIKRIl7fV13gSa8onkpjfJdNH7lBTv8JqfxKDt HqJA== X-Gm-Message-State: AOJu0YxHKpuR6uMYRAZzvtEp3oCY8lr+TR+7e9lb+kZf4P7yfQiJCnT4 ZoHZfhFkDwm9xPoMMlxZ34/vt2arYWqLePSGYN9vkAbkErBweWCiADVH+Q== X-Gm-Gg: ASbGncs5MuWaZn3PwGIRdG9dlGNcp6SqOx8qKbE17HV+Z+P7lSHshS9w9D1aPqY6tRX GLwW7xuu9VoWZyCGGWPAc35Tce8Qs4SGRQxVCthPxYmtaBXWWoNwPwcZV6KJaFa+jAZL0Y/pStz x6cags981fzWNKihSfttLrck1NbqA/PRGZp11+Nx6uiC5Vb7A5fw1B5RyVGgVP8iizOZnwLMJPX Y2ppu8Hi7kRdWCm9o3IxfaY2/2eeNcL1IzWXRx4zmd3AbGII+j2rtA8EFYDMtvIHsB//OOSI7V6 6q40a+UXilpYU0qukPP3W40XmoCDvuWXb4c8jxWll0norc00f6RbRfTLqIAsTRn9TQ== X-Google-Smtp-Source: AGHT+IF3/ZugQldXCiLTdC/2OoybSVxy3yIJW7iqSO1fYUR+wY4vaSp3QPk0LXTesBAghsQLFXbQHA== X-Received: by 2002:a05:6a20:2589:b0:1ee:cab3:4278 with SMTP id adf61e73a8af0-1eef3d8d2edmr1541935637.32.1740089742701; Thu, 20 Feb 2025 14:15:42 -0800 (PST) Received: from localhost.localdomain (c-76-146-13-146.hsd1.wa.comcast.net. [76.146.13.146]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-732425467adsm14852251b3a.15.2025.02.20.14.15.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 14:15:42 -0800 (PST) From: Amery Hung To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, alexei.starovoitov@gmail.com, martin.lau@kernel.org, ameryhung@gmail.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 2/2] selftests/bpf: Test struct_ops program with __ref arg calling bpf_tail_call Date: Thu, 20 Feb 2025 14:15:32 -0800 Message-ID: <20250220221532.1079331-2-ameryhung@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250220221532.1079331-1-ameryhung@gmail.com> References: <20250220221532.1079331-1-ameryhung@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Test if the verifier rejects struct_ops program with __ref argument calling bpf_tail_call(). Signed-off-by: Amery Hung Acked-by: Eduard Zingerman --- .../prog_tests/test_struct_ops_refcounted.c | 2 ++ .../struct_ops_refcounted_fail__tail_call.c | 36 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 1 + 3 files changed, 39 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__tail_call.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c index e290a2f6db95..da60c715fc59 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c @@ -3,10 +3,12 @@ #include "struct_ops_refcounted.skel.h" #include "struct_ops_refcounted_fail__ref_leak.skel.h" #include "struct_ops_refcounted_fail__global_subprog.skel.h" +#include "struct_ops_refcounted_fail__tail_call.skel.h" void test_struct_ops_refcounted(void) { RUN_TESTS(struct_ops_refcounted); RUN_TESTS(struct_ops_refcounted_fail__ref_leak); RUN_TESTS(struct_ops_refcounted_fail__global_subprog); + RUN_TESTS(struct_ops_refcounted_fail__tail_call); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__tail_call.c b/tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__tail_call.c new file mode 100644 index 000000000000..3b125025a1f2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__tail_call.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include "../test_kmods/bpf_testmod.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} prog_array SEC(".maps"); + +/* Test that the verifier rejects a program with referenced kptr arguments + * that tail call + */ +SEC("struct_ops/test_refcounted") +__failure __msg("program with __ref argument cannot tail call") +int refcounted_fail__tail_call(unsigned long long *ctx) +{ + struct task_struct *task = (struct task_struct *)ctx[1]; + + bpf_task_release(task); + bpf_tail_call(ctx, &prog_array, 0); + + return 0; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_ref_acquire = { + .test_refcounted = (void *)refcounted_fail__tail_call, +}; + diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index 89dc502de9d4..578bfc40dd05 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -1130,6 +1130,7 @@ static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = { }; static const struct bpf_verifier_ops bpf_testmod_verifier_ops = { + .get_func_proto = bpf_base_func_proto, .is_valid_access = bpf_testmod_ops_is_valid_access, };