Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp800830rwb; Thu, 4 Aug 2022 11:07:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR4BajWHVplwXW93ehnzeztv9mvQIXjxzCQXx7hGAeCG9waU5KdgFExKIh9gUeW3p2lUIdT8 X-Received: by 2002:a05:6a00:b8b:b0:52e:1138:6e6a with SMTP id g11-20020a056a000b8b00b0052e11386e6amr2869905pfj.72.1659636425985; Thu, 04 Aug 2022 11:07:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659636425; cv=none; d=google.com; s=arc-20160816; b=IIZvNHHoFsVKqz5dKqlsSxgIcPQyehPly1A2oTavkdeKnQiEyHxnUvIkNKDw0muyKk Tja73RqpKVwdYaGdhNnItRtd1gZ51DwzMH5WrVi6NjO7lLvUmBMBkk9tKcFMjJjtVggR di5Tf2JzCYa6YVW5HnGnVMmtg8XRZhQeDbv6L2oeNsZyVPNXFZnxT+m99uONF2RReFCL mxT+ZfE7yhgmWWQcZvRTY/tgsy0QdAY9XAxlO9hrSWzajHr5ETjLN77gamFC4WONA1+f jmpEEp9binaxA/eDSCBKXJzlCzlP+TyHJgEngEXAtKfSWHUlZd8QPM8lPBm3TdpgAgIU lQhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=QsqdAv7lzfCzVrls5e3D/1HBUNjDsakydLuhi3H9Yhg=; b=ppMSycBy2kin0BsTgdE2JmnF978FHmh8H/1vM2fGasDmOGcwV7AK1efiahDjdzOJM1 QWxGRs7uUuHZKt461xX32Ji8RshqunmmEKL1Zbs1H6egavF8CdBermah9EsTkfPALDtZ fmqGdh1PJ1EkfAG1wj01VY3AfqQo4bQlaQNXVGQB5+d+hqXet+PewZhy0HbWBIDRLIxQ CyALi827+UrPk5EkGhozg7IwNhpdMsIiX2VFUDCGY36eQ29TJsv1aRAbwSM3ugQcbATY iILnmGqN07HFvFdIKHJJp+yR67iANLC2wQLW/7rcSADvlIkduCk/VGItEnpaJE1Sklww Z5TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="KmSc8/HS"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j9-20020aa78009000000b005286cc2b29bsi1414140pfi.253.2022.08.04.11.06.50; Thu, 04 Aug 2022 11:07:05 -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=@gmail.com header.s=20210112 header.b="KmSc8/HS"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236637AbiHDRRP (ORCPT + 99 others); Thu, 4 Aug 2022 13:17:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232927AbiHDRRO (ORCPT ); Thu, 4 Aug 2022 13:17:14 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13FAA6A485; Thu, 4 Aug 2022 10:17:12 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id i14so475384ejg.6; Thu, 04 Aug 2022 10:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QsqdAv7lzfCzVrls5e3D/1HBUNjDsakydLuhi3H9Yhg=; b=KmSc8/HSgtUjLn/eY3kel/629G+B2lunQLmO6hXxq1nf+hYt51xD57hD1DUHn14NDV GuU4KrLJBRZPUCk/xtb8q7q4qV+gtwvIFPFYmCfo6ZczQeKDHnxNi6Q4FPM08VGpxN9F AT2hgVK6n6aHUnbW7RdrTyTADlBpkKFIdJi/mlRALt5sk3rDKNYsClN4hAF6HYk2k8ud AJh7F1chqTi24O7vdHwXGlWZHnmz1jqddqvqXf9b9QUzCL28CUpEyeuoo2C/WzE5HbWU m49pWie4g/HPSf5BUPsb/VsJx59+rh8m4muCh1vOzFiPnCaCOs0qaYRbKAY0vyKYSgmW zbNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QsqdAv7lzfCzVrls5e3D/1HBUNjDsakydLuhi3H9Yhg=; b=r9X5X9bseGsz6VjJWkRCtgkqSA603GliEdUhsmup86x0lLzMkZ2OALE3BKOMqEzJM9 mh6T2cT2SADWRZSE0Kqth9XrZ0iZ/6VBEiiH/Vbu/kwMfw7R+2+fijMic4RfVv0OCDbZ 9dTitVekV2LGk7UqFdMND+gdtieyvhVkFiH4B+OrLevx/tMwjwKaFc8bGLC6qrXG6d5z IOLMWqjEit0PwSixEySW1gMzS6zRwyWwLG5+iMjScY6xGdNgFNvB/KJ0xdrL22DKtWnU VceIL3ayB2fCscpSpOwKP3CeHXjssodVMUYO7hf0oQRihL14i4IXx9gWouRuFdI/bqVB LSjA== X-Gm-Message-State: ACgBeo3o34pWLtCvmV4r0BgMrBXIM3kwoNAd6Z/3FmVFhmavvKDdbZAR XfqFyNnF182o6GA9wD1iAJvuGRsI3T25dUBsciY= X-Received: by 2002:a17:907:6e02:b0:72b:9f16:1bc5 with SMTP id sd2-20020a1709076e0200b0072b9f161bc5mr2244766ejc.676.1659633430590; Thu, 04 Aug 2022 10:17:10 -0700 (PDT) MIME-Version: 1.0 References: <20220803134821.425334-1-lee@kernel.org> In-Reply-To: <20220803134821.425334-1-lee@kernel.org> From: Alexei Starovoitov Date: Thu, 4 Aug 2022 10:16:59 -0700 Message-ID: Subject: Re: [PATCH v2 1/1] bpf: Drop unprotected find_vpid() in favour of find_get_pid() To: Lee Jones Cc: LKML , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 On Wed, Aug 3, 2022 at 6:48 AM Lee Jones wrote: > > The documentation for find_pid() clearly states: > > "Must be called with the tasklist_lock or rcu_read_lock() held." > > Presently we do neither. > > Let's 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 > --- > > v1 => v2: > * Commit log update - no code differences > > 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); rcu_read_lock/unlock around this line would be a cheaper and faster alternative than pid's refcount inc/dec.