Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3519644rwb; Fri, 16 Dec 2022 16:40:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Z+LXB1X+9k9wa1jjbSr0YjHXR1FK1xB0sMaHp2NJSMEyi3lqt3sXXeX31z1Skz3DkPdCf X-Received: by 2002:a05:6a20:12d6:b0:a7:8ce5:6fc6 with SMTP id v22-20020a056a2012d600b000a78ce56fc6mr48892998pzg.13.1671237609456; Fri, 16 Dec 2022 16:40:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671237609; cv=none; d=google.com; s=arc-20160816; b=DpTpaCS57uihMNImF3ixs4u5qMRfmRLwJZdvKP6YTT9X5Lr+UZmhqEYbT3NJAY1jYS fuE74EXFiH3tpg34uUCt7HoVuz7SoZc+mzL98Fitdt+oNzTtXXRaOhJ7zrW9c9cQnGXI kB8BjejSlWNqvVAWQWWxR9jjqR29/483pb8v8SVPVfSr1BOMcFjm0dIfl47whlhgVGmt C/HKJQXIQA3GUim2423HkUyOcBRORTaqUkJw+9JxxWuMn9bTkgWife3eKSMPLcu3NihR B7l0yDpRfgd6xw9vzCMYkIl278YIgSXLd1JIJRmu9d4Ep6U2YH5oaE5Jyyt/fBIbWArE dEOA== 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=R8rAH2GjPEN9cY2fq9wcLQvkq0XDelpfrBUCyI4W8M0=; b=Ya8buJ4S4M2lL+3E9jlTEyGUZ3KqC+Enx7ANW8ipXz5/9sbHCGkXWFbVOOm5zxoy4y n2Q5/tbcUQW3In/fPhn238hY2XtaoCXQukPJHrl9G03olzM4BoJrv+SH5Sg1BL96gy+n 9azjadhbWIsqOpgeWPqoAtm74oc2o+r4nsJwmZl7tUCfZ22TwkTV2/d7ye4UQoVhqPvt 0nIpeusxhb995lpBarXlVvJWLsqJ7KIlZVVBd6ZOa0hqU26tbPSFtimwEPq3ky/UeuMA +BD7ezX7HVBHxb+kn7jerv/PAfH78NUSIqZffGXp317PPKhKvqVZ1dPUsRfCXZkRuCTX IpDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gVLraqrB; 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 e65-20020a636944000000b00476e640ddfesi4225444pgc.80.2022.12.16.16.40.00; Fri, 16 Dec 2022 16:40:09 -0800 (PST) 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=gVLraqrB; 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 S229785AbiLQAYm (ORCPT + 69 others); Fri, 16 Dec 2022 19:24:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231192AbiLQAXr (ORCPT ); Fri, 16 Dec 2022 19:23:47 -0500 Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E3D640833 for ; Fri, 16 Dec 2022 16:20:34 -0800 (PST) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-144bd860fdbso5297258fac.0 for ; Fri, 16 Dec 2022 16:20:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=R8rAH2GjPEN9cY2fq9wcLQvkq0XDelpfrBUCyI4W8M0=; b=gVLraqrBI+CrXfl1q9XWLveth0b2gcmRhlBQRTvPhYEv33+nDHVTAlpeX2gnLCEi7N M0tc5wNsQbUrSCvQH7/jOfhyabJYHMZ+2/4cWYuIR0nubbdW0JTx94PrSELNjzR7s6l1 osxNLFbYVEZJtMJgBHP5bcuKq/OxlGj7+tB3tmSnT5QApImssWMsNSp1KFEyLRYhPEH4 xQiLnfTvCuJEp11PIgr8tgxhMqgK5iVfJlTz3FXtyoDnglB/k1B9bndI8PTpZeIhmGP5 wRXgNDrg9zzuyHbjgSim4vo/ArQF2+KEQR2bn0io5tXaWdL76P4yNC4Gg2WfCZQ/firw hg2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R8rAH2GjPEN9cY2fq9wcLQvkq0XDelpfrBUCyI4W8M0=; b=Tkl+Z/GAkZ6XAx/pxUvuVCw6+IQ4WwL+G+M3iIW23jU1++dfXQghTnDV8f7snQOiHO uU6HNn/PROrOzerY15PoHheT4flshohWobBI8twLgVwKF5fJV2PmAz4DXMtF93bFvRSo xYyP1KfLJXydwrj4IiK9nw2U8D6c28gqdRcuZNjSEQpBJ9Usw06nAotueT6bNhQGnYUc ty0sBWmt16Mg+lE7R8i4z9Ae2TWtmhq6ECjVl1iE1M1XRwHSfzmveaUTyh/+vty59Ay9 uoGuBb2F8l0N1HmKRgkkVyngq7yL9KbSNj2iAbET9CREcgTow5bTe2MeYdxTSZYUFlA8 xeIA== X-Gm-Message-State: AFqh2kofNCQa6UFbRcQSwZvOtqn5noiU1c0PbYzmtCU51oDmePRT11jM 2T05Z9VTXaCLUmpZYnqvB/pyccNQ9DY4lm/su5U= X-Received: by 2002:a05:6870:3411:b0:144:5f0d:9fcb with SMTP id g17-20020a056870341100b001445f0d9fcbmr825544oah.38.1671236433326; Fri, 16 Dec 2022 16:20:33 -0800 (PST) MIME-Version: 1.0 References: <20221216233355.542197-1-robdclark@gmail.com> In-Reply-To: From: Rob Clark Date: Fri, 16 Dec 2022 16:20:33 -0800 Message-ID: Subject: Re: [PATCH] drm/panfrost: Fix GEM handle creation UAF To: Chia-I Wu Cc: dri-devel@lists.freedesktop.org, Rob Clark , Tomeu Vizoso , open list , Steven Price , Alyssa Rosenzweig 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 Fri, Dec 16, 2022 at 3:59 PM Chia-I Wu wrote: > > On Fri, Dec 16, 2022 at 3:34 PM Rob Clark wrote: > > > > From: Rob Clark > > > > Relying on an unreturned handle to hold a reference to an object we > > dereference is not safe. Userspace can guess the handle and race us > > by closing the handle from another thread. The _create_with_handle() > > that returns an object ptr is pretty much a pattern to avoid. And > > ideally creating the handle would be done after any needed dererencing. > > But in this case creation of the mapping is tied to the handle creation. > > Fortunately the mapping is refcnt'd and holds a reference to the object, > > so we can drop the handle's reference once we hold a mapping reference. > > > > Signed-off-by: Rob Clark > > --- > > drivers/gpu/drm/panfrost/panfrost_drv.c | 7 +++++++ > > drivers/gpu/drm/panfrost/panfrost_gem.c | 10 +++++++--- > > 2 files changed, 14 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/gpu/drm/panfrost/panfrost_drv.c b/drivers/gpu/drm/panfrost/panfrost_drv.c > > index 2fa5afe21288..aa5848de647c 100644 > > --- a/drivers/gpu/drm/panfrost/panfrost_drv.c > > +++ b/drivers/gpu/drm/panfrost/panfrost_drv.c > > @@ -98,6 +98,13 @@ static int panfrost_ioctl_create_bo(struct drm_device *dev, void *data, > > return PTR_ERR(bo); > > > > mapping = panfrost_gem_mapping_get(bo, priv); > > + > > + /* > > + * Now that the mapping holds a reference to the bo until we no longer > > + * need it, we can safely drop the handle's reference. > > + */ > Not too familiar with panfrost, but I don't see > panfrost_gem_mapping_get hold a reference to the bo? It doesn't directly, but the mapping already holds a reference, taken before the handle reference is dropped It is all a bit too subtle for my taste. > > + drm_gem_object_put(&bo->base.base); > > + > > if (!mapping) { > > drm_gem_object_put(&bo->base.base); > > return -EINVAL; > > diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c > > index 293e799e2fe8..e3e21c500d24 100644 > > --- a/drivers/gpu/drm/panfrost/panfrost_gem.c > > +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c > > @@ -234,6 +234,10 @@ struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t > > return &obj->base.base; > > } > > > > +/* > > + * NOTE: if this succeeds, both the handle and the returned object have > > + * an outstanding reference. > > + */ > I might suggest dropping the "_with_handle" suffix. Maybe _and_handle would be a better name (for this and other cases that return both a handle and obj)? > The naming convention is used in several drivers. I think we should > make it the case that the _with_handle variants always return the > handle without the pointer. (And with the change, it immediately > becomes clear that qxl and vmwgfx also have similar issues). ugg, yeah, qxl does have the issue in the qxl_mode_dumb_create path. I overlooked that it returns an obj pointer by reference. on the surface vmwgfx looked ok, but I could have overlooked something. BR, -R > > struct panfrost_gem_object * > > panfrost_gem_create_with_handle(struct drm_file *file_priv, > > struct drm_device *dev, size_t size, > > @@ -261,10 +265,10 @@ panfrost_gem_create_with_handle(struct drm_file *file_priv, > > * and handle has the id what user can see. > > */ > > ret = drm_gem_handle_create(file_priv, &shmem->base, handle); > > - /* drop reference from allocate - handle holds it now. */ > > - drm_gem_object_put(&shmem->base); > > - if (ret) > > + if (ret) { > > + drm_gem_object_put(&shmem->base); > > return ERR_PTR(ret); > > + } > > > > return bo; > > } > > -- > > 2.38.1 > >