All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@gmail.com>
To: dri-devel@lists.freedesktop.org
Cc: Dave Airlie <airlied@redhat.com>
Subject: [PATCH v2 2/6] drm/gem: implement vma access management
Date: Fri, 23 Aug 2013 13:13:24 +0200	[thread overview]
Message-ID: <1377256408-746-3-git-send-email-dh.herrmann@gmail.com> (raw)
In-Reply-To: <1377256408-746-1-git-send-email-dh.herrmann@gmail.com>

We implement automatic vma mmap() access management for all drivers using
gem_mmap. We use the vma manager to add each open-file that creates a
gem-handle to the vma-node of the underlying gem object. Once the handle
is destroyed, we drop the open-file again.

This allows us to use drm_vma_node_is_allowed() on _any_ gem object to see
whether an open-file is granted access. In drm_gem_mmap() we use this to
verify that unprivileged users cannot guess gem offsets and map arbitrary
buffers.

Note that this manages access for _all_ gem users (also TTM+GEM), but the
actual access checks are only done for drm_gem_mmap(). TTM drivers use the
TTM mmap helpers, which need to do that separately.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
---
 drivers/gpu/drm/drm_gem.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index d6122ae..b2d59b2 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -298,6 +298,7 @@ drm_gem_handle_delete(struct drm_file *filp, u32 handle)
 	spin_unlock(&filp->table_lock);
 
 	drm_gem_remove_prime_handles(obj, filp);
+	drm_vma_node_revoke(&obj->vma_node, filp->filp);
 
 	if (dev->driver->gem_close_object)
 		dev->driver->gem_close_object(obj, filp);
@@ -357,6 +358,11 @@ drm_gem_handle_create_tail(struct drm_file *file_priv,
 	}
 	*handlep = ret;
 
+	ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
+	if (ret) {
+		drm_gem_handle_delete(file_priv, *handlep);
+		return ret;
+	}
 
 	if (dev->driver->gem_open_object) {
 		ret = dev->driver->gem_open_object(obj, file_priv);
@@ -701,6 +707,7 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)
 	struct drm_device *dev = obj->dev;
 
 	drm_gem_remove_prime_handles(obj, file_priv);
+	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
 
 	if (dev->driver->gem_close_object)
 		dev->driver->gem_close_object(obj, file_priv);
@@ -793,6 +800,10 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement the
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
+ * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
+ * callers must verify access restrictions before calling this helper.
+ *
  * NOTE: This function has to be protected with dev->struct_mutex
  *
  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
@@ -841,6 +852,9 @@ EXPORT_SYMBOL(drm_gem_mmap_obj);
  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  * contain the fake offset we created when the GTT map ioctl was called on
  * the object) and map it with a call to drm_gem_mmap_obj().
+ *
+ * If the caller is not granted access to the buffer object, the mmap will fail
+ * with EACCES. Please see the vma manager for more information.
  */
 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 {
@@ -861,6 +875,9 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 	if (!node) {
 		mutex_unlock(&dev->struct_mutex);
 		return drm_mmap(filp, vma);
+	} else if (!drm_vma_node_is_allowed(node, filp)) {
+		mutex_unlock(&dev->struct_mutex);
+		return -EACCES;
 	}
 
 	obj = container_of(node, struct drm_gem_object, vma_node);
-- 
1.8.3.4

  parent reply	other threads:[~2013-08-23 11:13 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-23 11:13 [PATCH v2 0/6] DRM: VMA Access Management and Render Nodes David Herrmann
2013-08-23 11:13 ` [PATCH v2 1/6] drm/vma: add access management helpers David Herrmann
2013-08-23 11:13 ` David Herrmann [this message]
2013-08-23 11:13 ` [PATCH v2 3/6] drm: verify vma access in TTM+GEM drivers David Herrmann
2013-08-23 11:13 ` [PATCH v2 4/6] drm: implement experimental render nodes David Herrmann
2013-08-23 11:13 ` [PATCH v2 5/6] drm/i915: Support " David Herrmann
2013-08-23 11:29   ` Chris Wilson
2013-08-23 21:13     ` Kristian Høgsberg
2013-08-23 22:51   ` Daniel Vetter
2013-08-23 11:13 ` [PATCH v2 6/6] drm/nouveau: " David Herrmann
2013-08-23 11:28 ` [PATCH v2 0/6] DRM: VMA Access Management and Render Nodes Christian König
2013-08-23 12:31   ` David Herrmann
2013-08-23 12:34     ` Christian König
2013-08-23 12:47       ` David Herrmann
2013-08-23 13:34   ` Alex Deucher
2013-08-23 12:00 ` Martin Peres
2013-08-25 15:09   ` David Herrmann
2013-08-25 18:22     ` Martin Peres
2013-08-25 16:28 ` [PATCH 1/7] drm/vma: add access management helpers David Herrmann
2013-08-25 16:28   ` [PATCH 2/7] drm/gem: implement vma access management David Herrmann
2013-08-25 16:28   ` [PATCH 3/7] drm: verify vma access in TTM+GEM drivers David Herrmann
2013-08-25 16:29   ` [PATCH 4/7] drm: implement experimental render nodes David Herrmann
2013-08-25 16:29   ` [PATCH 5/7] drm/i915: Support " David Herrmann
2013-08-25 16:29   ` [PATCH 6/7] drm/nouveau: " David Herrmann
2013-08-25 16:29   ` [PATCH 7/7] drm/radeon: support " David Herrmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1377256408-746-3-git-send-email-dh.herrmann@gmail.com \
    --to=dh.herrmann@gmail.com \
    --cc=airlied@redhat.com \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.