From patchwork Wed Oct 11 12:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 151365 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp492019vqb; Wed, 11 Oct 2023 05:12:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG5wwwR0grCeZM399CYOkPaM03RMopn6BvsQMUT4Bn+mkoI3CXjembWm1X09Va7NH6U7kOS X-Received: by 2002:a05:6a21:a587:b0:163:c167:964a with SMTP id gd7-20020a056a21a58700b00163c167964amr26320185pzc.1.1697026372235; Wed, 11 Oct 2023 05:12:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697026372; cv=none; d=google.com; s=arc-20160816; b=tNP35//+cIFlmscrma6olovh/xvZTYoOIpmlT6aRIevGSRqD2u5739gLSUI6qnv1z/ Gxynl0+SyPQiujcQNo7zuvleUyL+quJr31IHic524MmF8edoU/EJMqKHYg9nGI2anJjg g0TuObd2EN5b1Jw+isTIMEoKhLuJuYUYCxhRP0N7oFtFMgshnYg1m78+ZUu3wutknEVL 3bxCOfQixg3wmA3t8pslsNUESGrps16FKgBA/o16PUUJCso370J3AcbTJNRFqdbhlXId OpDxCLmoS3tDpCVGI0OTpmXs67QBvDnQiMnAF3TPeEBg+GT4Nc1fPCcFUddQbPtl/LoI QCFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OvtOLR/3l5/FirKuucsBQhHg+ePC25J/Wauwusn+tsY=; fh=DR7g1EcWKOXTEoooUPBSJXUaklSrDEYzv6YDdhz1CwE=; b=q/j+nDtJfZDPlBhbVk1VXoXgSJtP/Tx3XZ5baI08W0drh0v1zZqHRqt8zKHr1HaHCn QIB52Lyc0ybVEcRn6LDXY4vcKwd8xKyOWxFNXuq8Abxb+gcMTPQ2z3tCKBAe89x137XB 3E8GPbfM/XZBxpMZNQWSoz2tB5Ieoel1e3ocEHIjgo+ccWy43IElH6ayICn8YcFlWJhE IYc6l/+G3YB6AD7qEmiD0uOPddaYSiJjNBKoXZsCRfQXN8H8XkWaSgYd2SAws7IjO0Nq lmrjJeTKAfajyLWFFTyp1wly8ERI7UFwFQBjFHs75YPETgAi/mXy2DM1hWNUHNXH8rkI +Iqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=M1H0DPw3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id f17-20020a631011000000b005637e28270fsi5372784pgl.243.2023.10.11.05.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 05:12:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=M1H0DPw3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 78A4D836D622; Wed, 11 Oct 2023 05:12:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346863AbjJKMLq (ORCPT + 18 others); Wed, 11 Oct 2023 08:11:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346923AbjJKMKj (ORCPT ); Wed, 11 Oct 2023 08:10:39 -0400 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16127109 for ; Wed, 11 Oct 2023 05:09:49 -0700 (PDT) Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-6c4bf619b57so4588856a34.1 for ; Wed, 11 Oct 2023 05:09:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1697026175; x=1697630975; 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=OvtOLR/3l5/FirKuucsBQhHg+ePC25J/Wauwusn+tsY=; b=M1H0DPw3MpA19a7O02MHrJc+UyiAwU+KgsefNHBsHMQOveRmg6YoD+RXnPKSfLRWme vc+JbpGiyoAuS1d9csD9ctg9RBeOerKsybh3o8+idVJPTbAXTGhxMJO1Bbft6qLCN4nq kv5ZDjXroIinv4hpqidLqn7sGWQHV1Gv1XBaJ+i0FKd+jKhBERcFvAeKhSDZUML+1UN7 OnlvqF3/hS361IwM19/7L2RsvhiUGtzJsAAwNT7bgPbsxbIHJwfopfIeV4/ZvvxTFShD Wokj5ePoZ8oothX9MfhZ6Rnipr4lSavLFA9tCgWmLqrnAP3kmQMApQ7jr5j0DovQ1GLp UjcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697026175; x=1697630975; 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=OvtOLR/3l5/FirKuucsBQhHg+ePC25J/Wauwusn+tsY=; b=c1u5ewJbtnUBNz5xw/CIvCSMGfrf84cJXdAjdv+TzrYm9RFDA99hQrQ+rHalZioShT 6XXNa7Gh+w1DikzVihWgKbtYpsu1FwODmGSdeGCvbSEixsTjmNN8DV48Q6imwiTI37k3 ObiRhl4iSFXcV7fuU7yxRks2qJ5PdON7tVuMgy1bblS6T6Xe0Sqald3nwd1kgZ9o6C2h EV6WvvbF/rD3lVilTipsVSX6MjAozBckrFHjyO+KLhq2OSrdKAUIDvqyqwusXxt/g2i1 HuM1DEFO+D30sXIkQzMDhQt8L9iMeXiJq/3gTHWtUDLGGZUacmAMZ0Uw9HOZyTmfCEs2 c5WA== X-Gm-Message-State: AOJu0Yzp2YCqc/0JOLefRqn08qRxlTHvPF5z3LBYM7x2QgIuu1KWlM2X t4JyO4EliO/nKFc+zEVqaPKZ0Q== X-Received: by 2002:a05:6830:6685:b0:6c4:be02:635f with SMTP id cq5-20020a056830668500b006c4be02635fmr27430243otb.5.1697026175171; Wed, 11 Oct 2023 05:09:35 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id u13-20020a62ed0d000000b006930db1e6cfsm9962769pfh.62.2023.10.11.05.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 05:09:34 -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 v5 6/8] bpf: Let bpf_iter_task_new accept null task ptr Date: Wed, 11 Oct 2023 20:08:55 +0800 Message-Id: <20231011120857.251943-7-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231011120857.251943-1-zhouchuyi@bytedance.com> References: <20231011120857.251943-1-zhouchuyi@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 11 Oct 2023 05:12:47 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779461125546148394 X-GMAIL-MSGID: 1779461125546148394 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 Acked-by: Andrii Nakryiko --- 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 caeddad3d2f1..0772545568f1 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -873,7 +873,7 @@ enum { }; __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 = (void *)it; @@ -885,14 +885,17 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *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; } if (flags == BPF_TASK_ITER_PROC_THREADS) - kit->task = task; + kit->task = task__nullable; else kit->task = &init_task; kit->pos = 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 struct btf *btf, const struct btf return __kfunc_param_match_suffix(btf, arg, "__refcounted_kptr"); } +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, }; 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; + if (is_kfunc_arg_nullable(meta->btf, &args[argno]) && register_is_null(reg)) + return KF_ARG_PTR_TO_NULL; 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_ } 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; 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))