From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_RED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDDDFC2B9F4 for ; Thu, 17 Jun 2021 17:03:38 +0000 (UTC) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5DF9D613AA for ; Thu, 17 Jun 2021 17:03:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5DF9D613AA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linux-kernel-mentees-bounces@lists.linuxfoundation.org Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 2B1E483D26; Thu, 17 Jun 2021 17:03:38 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id I3paz7d2qfyF; Thu, 17 Jun 2021 17:03:37 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 4CC1583D01; Thu, 17 Jun 2021 17:03:37 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 25617C000D; Thu, 17 Jun 2021 17:03:37 +0000 (UTC) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id C9788C000B for ; Thu, 17 Jun 2021 17:03:35 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id AB2C060E5F for ; Thu, 17 Jun 2021 17:03:35 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp3.osuosl.org (amavisd-new); dkim=pass (1024-bit key) header.d=ffwll.ch Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1COYP4emQLtf for ; Thu, 17 Jun 2021 17:03:34 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by smtp3.osuosl.org (Postfix) with ESMTPS id 6713F60E56 for ; Thu, 17 Jun 2021 17:03:33 +0000 (UTC) Received: by mail-wr1-x431.google.com with SMTP id e22so3991321wrc.1 for ; Thu, 17 Jun 2021 10:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=P2OqZXWKAR/Nh7o7FiibEBbsTU6CctnyyZIJIra5gAA=; b=cvvPQLBdUOqIj79dOux/F+t0GCmXHos+aHXHdeFtZfyHfVVSDKe0AQRWNWG+vf36Jh RDiiIh+3j5dSM1eHga3sJwJzGxkOKtWFyTRGn/12TvTMX2xLbt3K+Cj5zIkgkZI+EcYR xX8+gfGE5Z7meEKVw+rj5pUK7yYB5jb0ZSgsI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=P2OqZXWKAR/Nh7o7FiibEBbsTU6CctnyyZIJIra5gAA=; b=oC6lYWRrO+XPwRG/rqvr44NQOVA59MI6rYojlXLKlKB6rywtWdK9utjIlsK4Rd9B0m x4bslNCALfcwv6RgM4Y7Dn4KkggdLp8aMs1bwQIKZ3dyVi0/NscZvEOW+dqXBNFp/NsS 6hGTJ1Yc9vyNhXeZimowFxOn0Nk0islnnjA8a0BPOJfk4jp02lbza7/mQu/Q4JWvbvDf YLWWJQQt8zK/Y92Quz+Szvu4mO++IAZbsMk8FL2bFl+uPk+YllwcuA5wuAf4LuU8uY67 X5midEOXqoSjKIMaszwCS9qdRtQx24iRmVOtLD/YOZ16pABZhEzkvcTIl5RzjRXpxkY4 JOVg== X-Gm-Message-State: AOAM533KeWHU5o7hLw8D/hNRbBjIbjrkGdunn2cVR2bnp+KvH1bh2W/E RBUZARJp0DQw9BaBOyNQJ3791w== X-Google-Smtp-Source: ABdhPJwTwjrqCv16JL9cAsH58inHK2BluIVvwN7+zmPuBW9wkMsE6yrgGS+C7hKAYSLGaPPb+7o4nA== X-Received: by 2002:adf:ea49:: with SMTP id j9mr7082442wrn.366.1623949412107; Thu, 17 Jun 2021 10:03:32 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id t9sm5414660wmq.14.2021.06.17.10.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 10:03:31 -0700 (PDT) Date: Thu, 17 Jun 2021 19:03:29 +0200 From: Daniel Vetter To: Desmond Cheong Zhi Xi Subject: Re: [PATCH v2 1/2] drm: Add a locked version of drm_is_current_master Message-ID: Mail-Followup-To: Desmond Cheong Zhi Xi , maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@linux.ie, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, emil.l.velikov@gmail.com References: <20210615023645.6535-1-desmondcheongzx@gmail.com> <20210615023645.6535-2-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210615023645.6535-2-desmondcheongzx@gmail.com> X-Operating-System: Linux phenom 5.10.0-7-amd64 Cc: tzimmermann@suse.de, airlied@linux.ie, maarten.lankhorst@linux.intel.com, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, Daniel Vetter , emil.l.velikov@gmail.com, mripard@kernel.org, daniel@ffwll.ch, linux-kernel-mentees@lists.linuxfoundation.org X-BeenThere: linux-kernel-mentees@lists.linuxfoundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-kernel-mentees-bounces@lists.linuxfoundation.org Sender: "Linux-kernel-mentees" On Tue, Jun 15, 2021 at 10:36:44AM +0800, Desmond Cheong Zhi Xi wrote: > While checking the master status of the DRM file in > drm_is_current_master(), the device's master mutex should be > held. Without the mutex, the pointer fpriv->master may be freed > concurrently by another process calling drm_setmaster_ioctl(). This > could lead to use-after-free errors when the pointer is subsequently > dereferenced in drm_lease_owner(). > > The callers of drm_is_current_master() from drm_auth.c hold the > device's master mutex, but external callers do not. Hence, we implement > drm_is_current_master_locked() to be used within drm_auth.c, and > modify drm_is_current_master() to grab the device's master mutex > before checking the master status. > > Reported-by: Daniel Vetter > Signed-off-by: Desmond Cheong Zhi Xi > Reviewed-by: Emil Velikov > --- > drivers/gpu/drm/drm_auth.c | 23 +++++++++++++++++++---- > 1 file changed, 19 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/drm_auth.c b/drivers/gpu/drm/drm_auth.c > index 232abbba3686..c6bf52c310a9 100644 > --- a/drivers/gpu/drm/drm_auth.c > +++ b/drivers/gpu/drm/drm_auth.c > @@ -61,6 +61,8 @@ > * trusted clients. > */ > > +static bool drm_is_current_master_locked(struct drm_file *fpriv); A bit a bikeshed, but we try to avoid forward declarations when they're not needed. If you don't want to tear apart drm_is_current_master and the _locked version then just move them together. Can you pls do that and respin? Otherwise looks all great. -Daniel > + > int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) > { > struct drm_auth *auth = data; > @@ -223,7 +225,7 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data, > if (ret) > goto out_unlock; > > - if (drm_is_current_master(file_priv)) > + if (drm_is_current_master_locked(file_priv)) > goto out_unlock; > > if (dev->master) { > @@ -272,7 +274,7 @@ int drm_dropmaster_ioctl(struct drm_device *dev, void *data, > if (ret) > goto out_unlock; > > - if (!drm_is_current_master(file_priv)) { > + if (!drm_is_current_master_locked(file_priv)) { > ret = -EINVAL; > goto out_unlock; > } > @@ -321,7 +323,7 @@ void drm_master_release(struct drm_file *file_priv) > if (file_priv->magic) > idr_remove(&file_priv->master->magic_map, file_priv->magic); > > - if (!drm_is_current_master(file_priv)) > + if (!drm_is_current_master_locked(file_priv)) > goto out; > > drm_legacy_lock_master_cleanup(dev, master); > @@ -342,6 +344,13 @@ void drm_master_release(struct drm_file *file_priv) > mutex_unlock(&dev->master_mutex); > } > > +static bool drm_is_current_master_locked(struct drm_file *fpriv) > +{ > + lockdep_assert_held_once(&fpriv->master->dev->master_mutex); > + > + return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master; > +} > + > /** > * drm_is_current_master - checks whether @priv is the current master > * @fpriv: DRM file private > @@ -354,7 +363,13 @@ void drm_master_release(struct drm_file *file_priv) > */ > bool drm_is_current_master(struct drm_file *fpriv) > { > - return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master; > + bool ret; > + > + mutex_lock(&fpriv->master->dev->master_mutex); > + ret = drm_is_current_master_locked(fpriv); > + mutex_unlock(&fpriv->master->dev->master_mutex); > + > + return ret; > } > EXPORT_SYMBOL(drm_is_current_master); > > -- > 2.25.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ Linux-kernel-mentees mailing list Linux-kernel-mentees@lists.linuxfoundation.org https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees