Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3579730rwb; Fri, 16 Dec 2022 17:50:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf4mp4SX80Eltw/+aZWVbP27/MkNE7A19rUfsm14zGnWYmSwx5deg0AZXQ+KmAuk0jeGeLGn X-Received: by 2002:a17:906:9bf0:b0:7c0:9e1d:2e8b with SMTP id de48-20020a1709069bf000b007c09e1d2e8bmr25456732ejc.23.1671241816158; Fri, 16 Dec 2022 17:50:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671241816; cv=none; d=google.com; s=arc-20160816; b=omGs/gDDGq59htnPV0gi8ImS1+Dl8WcNtEXKZVfSypAFbdpdKI9NztMQ0cj6r/b37o +//ANHQQEEo9hVuxgD4HBezQPgiWqdGzMg0HykcQdsMV334bg8SD3LarYaHgel0B5DBy IOqFOEqXjT7LkmY3x0gw/uxvsVxfEBZNpNnuEFGhhM+Fl6DBY+HwePka1MlWhZswkfQK i4T7/NFlbhbUxcri2Jru7dZM6A1FZrsnemrcGev+N8AJ7pb9PYARFPvNNSwKLNsDSn4r lqrhxk1bvzk2TVwgNnI6wA9xbUXluTFvtqROFVgDo7yFslI4ZTUqbelAGJomxrVXx0Vf ry6A== 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=N2BdpDmynsGLfYwZJze3eSzx6Qv1LTya1e+z5hUsqmE=; b=bLUbIXUiNgyeoH9m+mBQ0TMEEiFRVgotxXiYlE9zV3Za+fPcQVj8vqZi4YDYEtBzye 5fAPuCQmGps7kUhpgst91jDAcxa9wrDz35x85GGaut/DOYyn0QHRqL6dT9IPtag1SlcT GRtbFn7l2vyZ/TnXsrmP5thprh3ojlhxSYrvy/049GM/lQChAo8BmOW2bhLK5eesGlX7 fmM1OSYTsl/QB6l/OlOJ9jqNj9XZNzpdiFizK+U3fa2BHhlBMVAkWIKplLBGURjRsjpQ kPJworJwJWpHZYUlZDWSy/mV/byAear+eEnZ5+cGje+gwdAhSd7QFBBmdhxRvY9yYDnl KlVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gBW5AQYO; 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 un2-20020a170907cb8200b007bd112b2764si3222151ejc.381.2022.12.16.17.49.59; Fri, 16 Dec 2022 17:50:16 -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=gBW5AQYO; 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 S229863AbiLQAyL (ORCPT + 68 others); Fri, 16 Dec 2022 19:54:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230106AbiLQAxt (ORCPT ); Fri, 16 Dec 2022 19:53:49 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AE0EDDB for ; Fri, 16 Dec 2022 16:53:21 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id i9so5882724edj.4 for ; Fri, 16 Dec 2022 16:53:21 -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=N2BdpDmynsGLfYwZJze3eSzx6Qv1LTya1e+z5hUsqmE=; b=gBW5AQYO4rFsTaG3SVrW2wngY+SAI9LKsn1P5sKw/E6teVWPSifYOZZkoY7I3NSLZf kXM6oZVgsQ0VThAxEqHVMFLImdTnoYonfOUQHdEVMJwJgnxn01GyBz8kUbpoarUaKfwW IIdKP6j1tSanrdDC8Pssb68OC5XWnMjzbCaKeoxOj4Nk/zpSibzkgqCxPjleTJMbhXWV bZ2C+u/DNeDvM/AzCbGpIkTQaJMS4QVSQJl4/49CQ342Pi6OOvaxDzlB+IrfEkKnwM9t pbBlfe3HZubnBORrEv9bWjSypXYkRezxYm19E2zhN9FxMAbGM5bwh1kMc9ut1hghlxPv ExPw== 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=N2BdpDmynsGLfYwZJze3eSzx6Qv1LTya1e+z5hUsqmE=; b=4Xd+DrEP5G/F/DwDER4JE0jSFBdEl7yBnw7d3zEfSTMnbnlrG2AM7S1srPocDV/cKh 7xtPS/tP87KCjROwOxodZKHQj5dEu06QrFl0BDguI/EPrWvmyYUb8ALSVzZ4Ws8PBDdO MA1ZX/fEtn/YZ9lw2g2hZ6WkY7P5OP8TIFZswEMaycc69ne5ZG01y7MZJQxRqwY1hD0S +nlRlR7XR6p8OTeqdNgCnjY6Jk2nO1umwBxRrjJYOpTkjY4kKeQRMw7rLSaRWVsqhwMn lO4/EppDo663tco4h5Dfnf6OmRfMY8QfivXHJ6kZYM/eT0G2NBmhl38HgPxg2RFBPIBB Z2oA== X-Gm-Message-State: ANoB5pmi6p2gmYXMi5m4Mk0PXj6IeLFYvEhULcTuQ+PiRzpeJAE4AiWd gS+inXXt3RrzxTNoHHg7etulCiiWCWpaDFhYKC0= X-Received: by 2002:a50:fc91:0:b0:46c:673b:93ac with SMTP id f17-20020a50fc91000000b0046c673b93acmr21013301edq.317.1671238399989; Fri, 16 Dec 2022 16:53:19 -0800 (PST) MIME-Version: 1.0 References: <20221216233355.542197-1-robdclark@gmail.com> In-Reply-To: From: Chia-I Wu Date: Fri, 16 Dec 2022 16:53:08 -0800 Message-ID: Subject: Re: [PATCH] drm/panfrost: Fix GEM handle creation UAF To: Rob Clark 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 4:20 PM Rob Clark wrote: > > 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. Ack. > > > + 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)? Sounds good. We will want that for panfrost, qxl, and vmwgfx. The other drivers and helpers only need the handle. Their _with_handle should be fixed to not return the pointer. > > > 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 > > >