Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp854078pxv; Thu, 1 Jul 2021 10:43:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhr7jP2gLf1R5CLOF4soFKJdx17LuvL+Df3fl2b5cm4Iw04RCj0KDWG4FlXgvfzPf5nWUl X-Received: by 2002:a05:6e02:b47:: with SMTP id f7mr384117ilu.135.1625161435849; Thu, 01 Jul 2021 10:43:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625161435; cv=none; d=google.com; s=arc-20160816; b=GH+SJuvogR2slov7bDwRgL+JDMWBUmyVKrFbLCBfeD4qVtGthKe/wyiKlvwEIO6Hpl BQYJab7L1C3r3vcR2XB5MhEAlbh+AyQPgptqd5LpfNCKos6VGKT67UGRugh7EkW/WP3t FtkBbNHkjN1RO2p59v2mBpVxL4bkw/3ugsX1AoZuN/o7RCgmVKmOf+Bkfw1+KiCAWYMZ 3zvwcSCq4hruEcftsM7dKRJgvminR7iEnlT8q2jmSylhX/2zDAfDeFG7Z8ib0MvnQYPp rjHOZMdLb9lFgQ0dUtefwi7mS7+Yl2wDMeO1Id/gOsptFJryac73w/Vixi+jtWYYoRgJ J1iA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CtdxbqSeV/2csKmNSvUS45gYT6/rPtvycxiemXQFqoE=; b=r6fi8ZTED4+rTYCYn0U9ngGPTTrpifOR1rX/5RYrubK7aQoWOffojD3N8chdQ2mPrt ExsvgkDZm8w8U0KBSbMLEMg1OsexuOBu5SAAxE+XAmyzxA4fwZGsuCTsz7wp60D82t38 rHIx93Qpl0TS6u7QjEAnMyaOOoxxCTHzg+k2QFz7lkEBdBQ+F5yi42nYJghWPNf2P1Zf Qs+q/rBVMN3ExUTnYAYtq45xN9azVOHEWIsYqtVe7GYJ0NdcdMtK4dK4/r8cli8wifgE l8ai4UA74vPBU1q3OQKyU6dw2qL0+300DaKxgFR/gLaIP5RiKVU0TAfD9KU4IC+mf1Bg n+rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=beHRMuc2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r4si511281ioh.93.2021.07.01.10.43.21; Thu, 01 Jul 2021 10:43:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=beHRMuc2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S231342AbhGAQ5S (ORCPT + 99 others); Thu, 1 Jul 2021 12:57:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbhGAQ5S (ORCPT ); Thu, 1 Jul 2021 12:57:18 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEFD4C061765; Thu, 1 Jul 2021 09:54:47 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id b12so5317737pfv.6; Thu, 01 Jul 2021 09:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CtdxbqSeV/2csKmNSvUS45gYT6/rPtvycxiemXQFqoE=; b=beHRMuc24lHXasbRdKRDNS6aTDyIugj8sUzTKr2eEW+qWKPqqi3KWacrdE0QS/QoG0 uqEXXfqmxcMCBnDnffQC2FkpcxwXIBBxibZQOYhnOXncI5/Rc8FXjLz9l9PwPHH2iQ9l LpMhtimOJpvhODJbfnhRoicACZnkaF+y+9BK4kO+14Bw6WmZeVFmLLmN+Z4WSD3/lFeb 16dqF7RxxdReZLPDhBWlAv5HQ3fkbsAQFuSg0tkOtJdXM4o3zBziNk26dE+v85gBV8Au Zdcv2R70XOt4pjLJ1VPEnBoU9MiG6BEzgDGxUC5P08t80hhiPqsuC1bDTyckyEblS3Yd g5aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CtdxbqSeV/2csKmNSvUS45gYT6/rPtvycxiemXQFqoE=; b=cWCd3HWJ2eW5f3QhViln0vzVLeyy2jOqdJ+U389Mcy93Ul3MsNLJqQBt/UTjHnntGi n1HmgjISlOg+ierWQY7+eFt4JfK9q6dUqDWUqQpgzPRc/Kbc2fJgVeX61eZIEJ404rzC tjdOv6wYiYzq5blwKx81xH8r6+P48PvG7DOHFf1PnAEzztMXpvUmaISX1mv8APQLxPi6 EFi1kTrUvyZWNIS2QXQnIF8AZEyKPXT9ifEnRYE8AZD7fUO2kq0DV8u+peEIeZBpZ4va DwYzV9opN1r/5aYncBzxmFFLdxUM3zzdIQaTcVoq0dv5FV7mz6k2WOD5bAObez2EwqbF GbCA== X-Gm-Message-State: AOAM533A++WY4WIPlPB3b9lscxC1kL06KFR6pC8Fsa1UOICUw74mm0r4 rFAsgo1ogEskovazD163JOI= X-Received: by 2002:a05:6a00:2162:b029:308:9346:2f55 with SMTP id r2-20020a056a002162b029030893462f55mr624241pff.49.1625158487473; Thu, 01 Jul 2021 09:54:47 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id s20sm398738pgv.1.2021.07.01.09.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jul 2021 09:54:47 -0700 (PDT) From: Desmond Cheong Zhi Xi To: maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@linux.ie, daniel@ffwll.ch, sumit.semwal@linaro.org, christian.koenig@amd.com Cc: Desmond Cheong Zhi Xi , dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, emil.l.velikov@gmail.com, Daniel Vetter Subject: [PATCH v7 3/5] drm: add a locked version of drm_is_current_master Date: Fri, 2 Jul 2021 00:53:56 +0800 Message-Id: <20210701165358.19053-4-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210701165358.19053-1-desmondcheongzx@gmail.com> References: <20210701165358.19053-1-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 51 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/drm_auth.c b/drivers/gpu/drm/drm_auth.c index f00e5abdbbf4..ab1863c5a5a0 100644 --- a/drivers/gpu/drm/drm_auth.c +++ b/drivers/gpu/drm/drm_auth.c @@ -61,6 +61,35 @@ * trusted clients. */ +static bool drm_is_current_master_locked(struct drm_file *fpriv) +{ + lockdep_assert_held_once(&fpriv->minor->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 + * + * Checks whether @fpriv is current master on its device. This decides whether a + * client is allowed to run DRM_MASTER IOCTLs. + * + * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting + * - the current master is assumed to own the non-shareable display hardware. + */ +bool drm_is_current_master(struct drm_file *fpriv) +{ + bool ret; + + mutex_lock(&fpriv->minor->dev->master_mutex); + ret = drm_is_current_master_locked(fpriv); + mutex_unlock(&fpriv->minor->dev->master_mutex); + + return ret; +} +EXPORT_SYMBOL(drm_is_current_master); + int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_auth *auth = data; @@ -223,7 +252,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 +301,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 +350,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,22 +371,6 @@ void drm_master_release(struct drm_file *file_priv) mutex_unlock(&dev->master_mutex); } -/** - * drm_is_current_master - checks whether @priv is the current master - * @fpriv: DRM file private - * - * Checks whether @fpriv is current master on its device. This decides whether a - * client is allowed to run DRM_MASTER IOCTLs. - * - * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting - * - the current master is assumed to own the non-shareable display hardware. - */ -bool drm_is_current_master(struct drm_file *fpriv) -{ - return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master; -} -EXPORT_SYMBOL(drm_is_current_master); - /** * drm_master_get - reference a master pointer * @master: &struct drm_master -- 2.25.1