Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4052520pxv; Mon, 28 Jun 2021 20:42:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQMWkLoeg89Q0aFSNmNB5jM+qFi1L6urM/r6hB/ixFvATevE7OAmGwtGFZ5ZOFoHSTs4fx X-Received: by 2002:a17:907:98fa:: with SMTP id ke26mr27316767ejc.309.1624938140289; Mon, 28 Jun 2021 20:42:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624938140; cv=none; d=google.com; s=arc-20160816; b=O0IxcNHTqmZ4F8tuYiYDu2MBQRGqrSlxVPEZrFPLkK917Lct6B87KHo9wfiCEjFgYh LmxZKwhqK5zD5z0of2mOCVtdh53iAWuaaPbNBm/IZ1HMTI6p0i3KU3tmdNcEILEXJlEX 4ypMUxE97iAP9xoZasRHLc08hBjacotNTULcQKxtrsaujAsbTH4gqVZoaojFl9V210bk VoWG9+0Q7xZUaUfH6J6tazHSNX4IDSQv5F8hJQosrSV0Phd1t7zwuUHjfC2dS3lP79e1 mFHIkqZbMn1Mil1kkJpjZPCzuiukuqOE0UscpjJjPGGeIL5S87mALNcWjq1mDtQmiD9b 51Hw== 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=9loa3KpkDErmzl3oFkCvkO2WisLBmhfHvqpUa9ZzsPk=; b=G3Xl4RW5L/lcpb17SpKFTBOhxPQInNLpRPePmR6ObkCyAn/8UtyodyfPACNQWRWDBC CtGWyBLmb+iegF/N5Zrp6PRR2xcL6pzMjVm4Azg/nX5w9mv70HvWNNvgUhIXXpxjWfqR j2yF8GOciLeRt4HdpqpVtcBYOapmTFGMZ/NKogo1UXaBPkQR7dFenZW/qKec5qd62lvH VyAM36+WZ+h0Wx1wYFiSFMXwE570q4ogzPjagLQbSoscuckh4F8JnmOpWy/GktXAZwo4 qJ2BirpvxFaC7iPYvcPO1iKclnE59xRQsd+1w7usqBAJ0rFkb8nuQhQyrmDxQYo+YO3q CP4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=G7FSVe14; 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 r4si15612710edy.114.2021.06.28.20.41.56; Mon, 28 Jun 2021 20:42:20 -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=G7FSVe14; 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 S232011AbhF2Dkh (ORCPT + 99 others); Mon, 28 Jun 2021 23:40:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231987AbhF2Dkg (ORCPT ); Mon, 28 Jun 2021 23:40:36 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F529C061574; Mon, 28 Jun 2021 20:38:09 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id ce18-20020a17090aff12b0290170a3e3eb07so1513254pjb.1; Mon, 28 Jun 2021 20:38:09 -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=9loa3KpkDErmzl3oFkCvkO2WisLBmhfHvqpUa9ZzsPk=; b=G7FSVe14ahODXBDr0BOl1sQvrjwoxJUpI+MZtNh+UHqMdVWVWqOPj9MivvWStR06X2 Vhl0o617/jXzrD2xhg0wzaIulK1dXs6+HOz3c/ctcekjnNww7k1FBEuoY7idtNVJ3amv 1bfmmtJ3vOsuGdYfVIamfuq4pZZTYIpoAPtGjJ798uZuVK58VkxX10viO7AEk7YJqYKo tELSjSxKmsLswcWvtsFDpGYeIda7VqPY0d4NzEb48dabA+8QIvRSv8QLlGtyAcjTmUrE ZcROH930Oj2nikUjPRjLBGYcbilDuLMSFkKtwsry3KNrjSL/CSPivDzNfTP3qH8KT1og nTHg== 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=9loa3KpkDErmzl3oFkCvkO2WisLBmhfHvqpUa9ZzsPk=; b=DmZmDPbn/5iaLXhSdPvZjKTw/FbBA+FWIG2Y+hWTrpuoNqfV9IAG8clStza5UVmDQg L3FzoxeorK0TGsQb8xIl+zwfWm/vuGkznSFvlddE11HwDrz6nhVDQ7dL6I6CcamEqbKy i0GSzJn6yKEke9zjTrCrWdc/7a7iLjF9MOTb4JjooA6emfeYL4pVq70btCO1NJU/ITNt GrAMkL7EFhHxL9gm0KVKppZZ3dUQtbSisJ5+5F8O3HLe664KB5Z9T4WrT/YoGu2wcOd+ sgr8+/J93N0ld8OMd3PXfyHeVHoLNYLdVakagDCpxZjCaFr75bptKfwJcTIJoOs5I4rj zKcA== X-Gm-Message-State: AOAM531ceiBL76vMOk4vNXrTWFcxP4ba0Bqf16HHQSGVYyyRxy4AFO5k FJLfTot3tsoAFYZEJBL8+gI= X-Received: by 2002:a17:90a:ea82:: with SMTP id h2mr10814482pjz.203.1624937888846; Mon, 28 Jun 2021 20:38:08 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id d129sm14769076pfd.218.2021.06.28.20.38.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Jun 2021 20:38:08 -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 v5 2/3] drm: add a locked version of drm_is_current_master Date: Tue, 29 Jun 2021 11:37:05 +0800 Message-Id: <20210629033706.20537-3-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210629033706.20537-1-desmondcheongzx@gmail.com> References: <20210629033706.20537-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 --- 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