Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp449482imi; Thu, 21 Jul 2022 04:31:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vXv4XtNzNWW0nAQzq03mQgkttG19Yoijkeh0fjKnpkpdUXP/JKUZEUiYLDcBjlF/HOUD8A X-Received: by 2002:a63:eb0c:0:b0:415:c521:4bf with SMTP id t12-20020a63eb0c000000b00415c52104bfmr36440597pgh.25.1658403064494; Thu, 21 Jul 2022 04:31:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658403064; cv=none; d=google.com; s=arc-20160816; b=A2SKrn0lt7jh0ktAXDBl1DuCWVffwipvlWBAItQdIRA5FtxhcSvvWpHYB2jSHTlh7L sG97IXmwY6J0EKNDtCvLOoGTMqIbR0y/iy2zyaA+2ZVQ3qkAXDBqCKGGIb7v3ZZkGQ19 CMlhVBap8RJOWdnJmBLK7XlHGxnjKQmC3B3DpmRN/XZriCmH4D2OjE3wx7fhn7F1EVv7 TIQjZdYYc6qbULXYfH+ELmpUQsUbb5NA7SxHHG8qOXtEemd92QOltlwt0xw3WAw5aAN5 lV94K5J5k4VjmGVrz6vXshgRaPlOzvzRfyPXouXbXFZEX3WMqXewhbg5RCSvn9NZwS+S DNYA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=nQnBxusK/GJrhqe0l8yMk6uwSkRxDRwxFV5E82g0Ddg=; b=sbJN8ZPzK9Mb+uTBE7F6xdd6qGUySvc86YWYQ14wp18T65iOi9xYayXQrpwhYCfx6t oOw0cR/pPdF/+IgVjzZNX7RRkAPodXDtrtTbOiGYbd3iKEP3xq+31eaT+fqGDDq7i1vV addDGxagQxoxIqJhlTTB+7SJdvQUTHKpOH9qNp1HLXCgXr6yHArGm6xDXAslIbhtW1JK aC7g8tqsnLx3/R65Rqe+fDGHcY4EYfB627G377hEFZZps6ThzhwlhAQinQ+YnyZCy6VN FHih+bF/FbQ5F17BXWx72eeBwdI/N/CNtp8iUhmHD8v+KJnGSeeeSAEs0OBULc25CwCu rt3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qEJeipUs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c6-20020a170903234600b0016d17ddf6cfsi2370233plh.463.2022.07.21.04.30.47; Thu, 21 Jul 2022 04:31:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qEJeipUs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233088AbiGULPD (ORCPT + 99 others); Thu, 21 Jul 2022 07:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229747AbiGULPC (ORCPT ); Thu, 21 Jul 2022 07:15:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3317182128; Thu, 21 Jul 2022 04:15:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C120461B36; Thu, 21 Jul 2022 11:15:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44EFDC3411E; Thu, 21 Jul 2022 11:14:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1658402100; bh=Z0bgFvFC+a5XePrtTgP5iWpqKqXLerZ1ITI2EwqeVQU=; h=From:To:Cc:Subject:Date:From; b=qEJeipUsqQ5BECh/krnTuufoQDJ1XlPpQ1jM8fLvBDvul4hTv8yEBKh/XG/EklpfE 88vhl7GhZjHFz/DAE3tOxbdMAMv54UzdlmPsOcKZ8Hh+S1mkSzBbi1rK3zBxao1PuR tf3H/a+0T5e02uQ2DKXZbd/u1M9SO7g9NAP+kASSUBiairWWclkv0r3iFy/d/6Qohu jnXMr7OesWc1dhtXw/5K7nMehLHqjIKJjxwEQlkHrioLjrdHlIIGUzZpokvMtVIUlp cWT+LfYaz342Ab8QgHv1YArXepu8PdeyrPNp0/rk728G9o4r9nl0XyQDA8hFjfKAmM hXtxnbXK7AaKw== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org Subject: [PATCH 1/1] bpf: Drop unprotected find_vpid() in favour of find_get_pid() Date: Thu, 21 Jul 2022 12:14:30 +0100 Message-Id: <20220721111430.416305-1-lee@kernel.org> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The documentation for find_pid() clearly states: "Must be called with the tasklist_lock or rcu_read_lock() held." Presently we do neither. In an ideal world we would wrap the in-lined call to find_vpid() along with get_pid_task() in the suggested rcu_read_lock() and have done. However, looking at get_pid_task()'s internals, it already does that independently, so this would lead to deadlock. Instead, we'll use find_get_pid() which searches for the vpid, then takes a reference to it preventing early free, all within the safety of rcu_read_lock(). Once we have our reference we can safely make use of it up until the point it is put. Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: John Fastabend Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: KP Singh Cc: Stanislav Fomichev Cc: Hao Luo Cc: Jiri Olsa Cc: bpf@vger.kernel.org Fixes: 41bdc4b40ed6f ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY") Signed-off-by: Lee Jones --- kernel/bpf/syscall.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 83c7136c5788d..c20cff30581c4 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4385,6 +4385,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr, const struct perf_event *event; struct task_struct *task; struct file *file; + struct pid *ppid; int err; if (CHECK_ATTR(BPF_TASK_FD_QUERY)) @@ -4396,7 +4397,9 @@ static int bpf_task_fd_query(const union bpf_attr *attr, if (attr->task_fd_query.flags != 0) return -EINVAL; - task = get_pid_task(find_vpid(pid), PIDTYPE_PID); + ppid = find_get_pid(pid); + task = get_pid_task(ppid, PIDTYPE_PID); + put_pid(ppid); if (!task) return -ENOENT; -- 2.37.0.170.g444d1eabd0-goog