From nobody Sat Jan 3 00:17:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D645E95A63 for ; Sat, 7 Oct 2023 12:46:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343998AbjJGMqb (ORCPT ); Sat, 7 Oct 2023 08:46:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344026AbjJGMqP (ORCPT ); Sat, 7 Oct 2023 08:46:15 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3F41115 for ; Sat, 7 Oct 2023 05:45:53 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-279013f9875so2265720a91.2 for ; Sat, 07 Oct 2023 05:45:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1696682752; x=1697287552; 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=Y5nHGgcorL6nTo4Bd2JPYgDkdxlRNl3YeAVkUVjikAc=; b=a9TFgEW8qNv6lI4l2HKipMyNN+jMY78H2Xz5vYIDcPFj9Mktd0VGiOawfpepn+X6Te LXPiO+cbFgw1DVFPL4OL3EdXy/8roTZ+rQPlr5gw4W3GKaY03C2r+7M7tJTMDtooJ9Md rN5bPddpBL+y3P081RvP/+hRztmvbyQji7FzP8fagVMh0D7X1f/znLbWYr1VJtqylOe7 MQ5/7pgiGf93cRhAj6AP1MvtHqFmFxdp+bpjFMvBfDYBapaiG+8tla4VYIPplDjFCXTa N+9kCf3jMZ4P8LwW2/UP1SJaZZNH23Qg3qrTSve0Hh2QMVSDSkNiHlXWVJsdtivyeej9 hTPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696682752; x=1697287552; 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=Y5nHGgcorL6nTo4Bd2JPYgDkdxlRNl3YeAVkUVjikAc=; b=FbkOTzD4MtD7TuR2bTEXO+5br4i1XqUqrlBsZFoDBz3M9yQDHdFlHLD5KH6CE+48Bz 0Ifl+owP2ZMS4FI1+pK904NetDu4nnRSMV5huq/+mxYNmfZ60LdoATMzpun15nFj6idF AgZlu75M78/KQetQ75UcO4dW2740VHEC31mppMpteHKwqpL6/ZJFaA6IR9vwgkxeoy77 vQtCBxyWehTSf2ff6ScBcJlrDGntbY5xkkh6cE7dmYLyEBdKUHZ8o1IzO23P4ZGEIwVq a/srQsClMpTLQ1awmGlNy+U5AinMFsCW+Do7QZqyjLv0BWOxhxOxRCbBz+/VSO+0O9mU iaSQ== X-Gm-Message-State: AOJu0YyDc4o5QLbL2yE1G3QySzUuO3bb9voCdZUjDSNCD8EV4mvMOJCW axc64qsP9QgPBTc6Bv5hMM/Ktg== X-Google-Smtp-Source: AGHT+IGlVZ36ENR5ndQomm9+bSekaLzX2lrkJ5cYf8fwBGgagYuwuccybjivPO984y+m870S7FP9Qg== X-Received: by 2002:a17:90b:374b:b0:26b:4a9e:3c7e with SMTP id ne11-20020a17090b374b00b0026b4a9e3c7emr10333461pjb.4.1696682752627; Sat, 07 Oct 2023 05:45:52 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id d6-20020a17090ad3c600b00256799877ffsm5095388pjw.47.2023.10.07.05.45.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 05:45:52 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, tj@kernel.org, linux-kernel@vger.kernel.org, Chuyi Zhou Subject: [PATCH bpf-next v4 6/8] bpf: Let bpf_iter_task_new accept null task ptr Date: Sat, 7 Oct 2023 20:45:20 +0800 Message-Id: <20231007124522.34834-7-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231007124522.34834-1-zhouchuyi@bytedance.com> References: <20231007124522.34834-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When using task_iter to iterate all threads of a specific task, we enforce that the user must pass a valid task pointer to ensure safety. However, when iterating all threads/process in the system, BPF verifier still require a valid ptr instead of "nullable" pointer, even though it's pointless, which is a kind of surprising from usability standpoint. It would be nice if we could let that kfunc accept a explicit null pointer when we are using BPF_TASK_ITER_ALL_{PROCS, THREADS} and a valid pointer when using BPF_TASK_ITER_THREAD. Given a trival kfunc: __bpf_kfunc void FN(struct TYPE_A *obj); BPF Prog would reject a nullptr for obj. The error info is: "arg#x pointer type xx xx must point to scalar, or struct with scalar" reported by get_kfunc_ptr_arg_type(). The reg->type is SCALAR_VALUE and the btf type of ref_t is not scalar or scalar_struct which leads to the rejection of get_kfunc_ptr_arg_type. This patch add "__nullable" annotation: __bpf_kfunc void FN(struct TYPE_A *obj__nullable); Here __nullable indicates obj can be optional, user can pass a explicit nullptr or a normal TYPE_A pointer. In get_kfunc_ptr_arg_type(), we will detect whether the current arg is optional and register is null, If so, return a new kfunc_ptr_arg_type KF_ARG_PTR_TO_NULL and skip to the next arg in check_kfunc_args(). Signed-off-by: Chuyi Zhou --- kernel/bpf/task_iter.c | 7 +++++-- kernel/bpf/verifier.c | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index c2e1c3cbffea..773be9a221f5 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -873,7 +873,7 @@ enum { }; =20 __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it, - struct task_struct *task, unsigned int flags) + struct task_struct *task__nullable, unsigned int flags) { struct bpf_iter_task_kern *kit =3D (void *)it; =20 @@ -885,14 +885,17 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_tas= k *it, switch (flags) { case BPF_TASK_ITER_ALL_THREADS: case BPF_TASK_ITER_ALL_PROCS: + break; case BPF_TASK_ITER_PROC_THREADS: + if (!task__nullable) + return -EINVAL; break; default: return -EINVAL; } =20 if (flags =3D=3D BPF_TASK_ITER_PROC_THREADS) - kit->task =3D task; + kit->task =3D task__nullable; else kit->task =3D &init_task; kit->pos =3D kit->task; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 3a60cc87520e..d09697dbfd9c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10310,6 +10310,11 @@ static bool is_kfunc_arg_refcounted_kptr(const str= uct btf *btf, const struct btf return __kfunc_param_match_suffix(btf, arg, "__refcounted_kptr"); } =20 +static bool is_kfunc_arg_nullable(const struct btf *btf, const struct btf_= param *arg) +{ + return __kfunc_param_match_suffix(btf, arg, "__nullable"); +} + static bool is_kfunc_arg_scalar_with_name(const struct btf *btf, const struct btf_param *arg, const char *name) @@ -10452,6 +10457,7 @@ enum kfunc_ptr_arg_type { KF_ARG_PTR_TO_CALLBACK, KF_ARG_PTR_TO_RB_ROOT, KF_ARG_PTR_TO_RB_NODE, + KF_ARG_PTR_TO_NULL, }; =20 enum special_kfunc_type { @@ -10608,6 +10614,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, if (is_kfunc_arg_callback(env, meta->btf, &args[argno])) return KF_ARG_PTR_TO_CALLBACK; =20 + if (is_kfunc_arg_nullable(meta->btf, &args[argno]) && register_is_null(re= g)) + return KF_ARG_PTR_TO_NULL; =20 if (argno + 1 < nargs && (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], ®s[regno + 1])= || @@ -11158,7 +11166,8 @@ static int check_kfunc_args(struct bpf_verifier_env= *env, struct bpf_kfunc_call_ } =20 if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) && - (register_is_null(reg) || type_may_be_null(reg->type))) { + (register_is_null(reg) || type_may_be_null(reg->type)) && + !is_kfunc_arg_nullable(meta->btf, &args[i])) { verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i); return -EACCES; } @@ -11183,6 +11192,8 @@ static int check_kfunc_args(struct bpf_verifier_env= *env, struct bpf_kfunc_call_ return kf_arg_type; =20 switch (kf_arg_type) { + case KF_ARG_PTR_TO_NULL: + continue; case KF_ARG_PTR_TO_ALLOC_BTF_ID: case KF_ARG_PTR_TO_BTF_ID: if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta)) --=20 2.20.1