Commit c627716e authored by Thomas Hellstrom's avatar Thomas Hellstrom Committed by Ben Hutchings

drm: Fix an unwanted master inheritance v2

commit a0af2e53 upstream.

A client calling drmSetMaster() using a file descriptor that was opened
when another client was master would inherit the latter client's master
object and all its authenticated clients.

This is unwanted behaviour, and when this happens, instead allocate a
brand new master object for the client calling drmSetMaster().

Fixes a BUG() throw in vmw_master_set().
Signed-off-by: default avatarThomas Hellstrom <thellstrom@vmware.com>
Signed-off-by: default avatarDave Airlie <airlied@redhat.com>
[bwh: Backported to 3.2:
 - s/master_mutex/struct_mutex/
 - drm_new_set_master() must drop struct_mutex while calling
   drm_driver::master_create
 - Adjust filename, context, indentation]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent 377ace8b
...@@ -218,6 +218,62 @@ static int drm_cpu_valid(void) ...@@ -218,6 +218,62 @@ static int drm_cpu_valid(void)
return 1; return 1;
} }
/**
* drm_new_set_master - Allocate a new master object and become master for the
* associated master realm.
*
* @dev: The associated device.
* @fpriv: File private identifying the client.
*
* This function must be called with dev::struct_mutex held.
* Returns negative error code on failure. Zero on success.
*/
int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
{
struct drm_master *old_master;
int ret;
lockdep_assert_held_once(&dev->struct_mutex);
/* create a new master */
fpriv->minor->master = drm_master_create(fpriv->minor);
if (!fpriv->minor->master)
return -ENOMEM;
/* take another reference for the copy in the local file priv */
old_master = fpriv->master;
fpriv->master = drm_master_get(fpriv->minor->master);
if (dev->driver->master_create) {
mutex_unlock(&dev->struct_mutex);
ret = dev->driver->master_create(dev, fpriv->master);
mutex_lock(&dev->struct_mutex);
if (ret)
goto out_err;
}
if (dev->driver->master_set) {
ret = dev->driver->master_set(dev, fpriv, true);
if (ret)
goto out_err;
}
fpriv->is_master = 1;
fpriv->allowed_master = 1;
fpriv->authenticated = 1;
if (old_master)
drm_master_put(&old_master);
return 0;
out_err:
/* drop both references and restore old master on failure */
drm_master_put(&fpriv->minor->master);
drm_master_put(&fpriv->master);
fpriv->master = old_master;
return ret;
}
/** /**
* Called whenever a process opens /dev/drm. * Called whenever a process opens /dev/drm.
* *
...@@ -279,43 +335,10 @@ static int drm_open_helper(struct inode *inode, struct file *filp, ...@@ -279,43 +335,10 @@ static int drm_open_helper(struct inode *inode, struct file *filp,
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
if (!priv->minor->master) { if (!priv->minor->master) {
/* create a new master */ /* create a new master */
priv->minor->master = drm_master_create(priv->minor); ret = drm_new_set_master(dev, priv);
if (!priv->minor->master) {
mutex_unlock(&dev->struct_mutex);
ret = -ENOMEM;
goto out_free;
}
priv->is_master = 1;
/* take another reference for the copy in the local file priv */
priv->master = drm_master_get(priv->minor->master);
priv->authenticated = 1;
mutex_unlock(&dev->struct_mutex);
if (dev->driver->master_create) {
ret = dev->driver->master_create(dev, priv->master);
if (ret) {
mutex_lock(&dev->struct_mutex);
/* drop both references if this fails */
drm_master_put(&priv->minor->master);
drm_master_put(&priv->master);
mutex_unlock(&dev->struct_mutex);
goto out_free;
}
}
mutex_lock(&dev->struct_mutex);
if (dev->driver->master_set) {
ret = dev->driver->master_set(dev, priv, true);
if (ret) {
/* drop both references if this fails */
drm_master_put(&priv->minor->master);
drm_master_put(&priv->master);
mutex_unlock(&dev->struct_mutex);
goto out_free;
}
}
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
if (ret)
goto out_free;
} else { } else {
/* get a reference to the master */ /* get a reference to the master */
priv->master = drm_master_get(priv->minor->master); priv->master = drm_master_get(priv->minor->master);
......
...@@ -225,6 +225,10 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data, ...@@ -225,6 +225,10 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data,
if (!file_priv->minor->master && if (!file_priv->minor->master &&
file_priv->minor->master != file_priv->master) { file_priv->minor->master != file_priv->master) {
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
if (!file_priv->allowed_master) {
ret = drm_new_set_master(dev, file_priv);
goto out_unlock;
}
file_priv->minor->master = drm_master_get(file_priv->master); file_priv->minor->master = drm_master_get(file_priv->master);
file_priv->is_master = 1; file_priv->is_master = 1;
if (dev->driver->master_set) { if (dev->driver->master_set) {
...@@ -234,10 +238,11 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data, ...@@ -234,10 +238,11 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data,
drm_master_put(&file_priv->minor->master); drm_master_put(&file_priv->minor->master);
} }
} }
out_unlock:
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
} }
return 0; return ret;
} }
int drm_dropmaster_ioctl(struct drm_device *dev, void *data, int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
......
...@@ -430,6 +430,11 @@ struct drm_file { ...@@ -430,6 +430,11 @@ struct drm_file {
void *driver_priv; void *driver_priv;
int is_master; /* this file private is a master for a minor */ int is_master; /* this file private is a master for a minor */
/*
* This client is allowed to gain master privileges for @master.
* Protected by struct drm_device::struct_mutex.
*/
unsigned allowed_master:1;
struct drm_master *master; /* master this node is currently associated with struct drm_master *master; /* master this node is currently associated with
N.B. not always minor->master */ N.B. not always minor->master */
struct list_head fbs; struct list_head fbs;
...@@ -1254,6 +1259,7 @@ extern int drm_fasync(int fd, struct file *filp, int on); ...@@ -1254,6 +1259,7 @@ extern int drm_fasync(int fd, struct file *filp, int on);
extern ssize_t drm_read(struct file *filp, char __user *buffer, extern ssize_t drm_read(struct file *filp, char __user *buffer,
size_t count, loff_t *offset); size_t count, loff_t *offset);
extern int drm_release(struct inode *inode, struct file *filp); extern int drm_release(struct inode *inode, struct file *filp);
extern int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
/* Mapping support (drm_vm.h) */ /* Mapping support (drm_vm.h) */
extern int drm_mmap(struct file *filp, struct vm_area_struct *vma); extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment