Skip to content

Commit 56f0729

Browse files
desmondcheongzxdanvet
authored andcommitted
drm: protect drm_master pointers in drm_lease.c
drm_file->master pointers should be protected by drm_device.master_mutex or drm_file.master_lookup_lock when being dereferenced. However, in drm_lease.c, there are multiple instances where drm_file->master is accessed and dereferenced while neither lock is held. This makes drm_lease.c vulnerable to use-after-free bugs. We address this issue in 2 ways: 1. Add a new drm_file_get_master() function that calls drm_master_get on drm_file->master while holding on to drm_file.master_lookup_lock. Since drm_master_get increments the reference count of master, this prevents master from being freed until we unreference it with drm_master_put. 2. In each case where drm_file->master is directly accessed and eventually dereferenced in drm_lease.c, we wrap the access in a call to the new drm_file_get_master function, then unreference the master pointer once we are done using it. Reported-by: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com> Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: https://patchwork.freedesktop.org/patch/msgid/20210712043508.11584-6-desmondcheongzx@gmail.com
1 parent 0b0860a commit 56f0729

File tree

4 files changed

+93
-20
lines changed

4 files changed

+93
-20
lines changed

drivers/gpu/drm/drm_auth.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,31 @@ struct drm_master *drm_master_get(struct drm_master *master)
389389
}
390390
EXPORT_SYMBOL(drm_master_get);
391391

392+
/**
393+
* drm_file_get_master - reference &drm_file.master of @file_priv
394+
* @file_priv: DRM file private
395+
*
396+
* Increments the reference count of @file_priv's &drm_file.master and returns
397+
* the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
398+
*
399+
* Master pointers returned from this function should be unreferenced using
400+
* drm_master_put().
401+
*/
402+
struct drm_master *drm_file_get_master(struct drm_file *file_priv)
403+
{
404+
struct drm_master *master = NULL;
405+
406+
spin_lock(&file_priv->master_lookup_lock);
407+
if (!file_priv->master)
408+
goto unlock;
409+
master = drm_master_get(file_priv->master);
410+
411+
unlock:
412+
spin_unlock(&file_priv->master_lookup_lock);
413+
return master;
414+
}
415+
EXPORT_SYMBOL(drm_file_get_master);
416+
392417
static void drm_master_destroy(struct kref *kref)
393418
{
394419
struct drm_master *master = container_of(kref, struct drm_master, refcount);

drivers/gpu/drm/drm_lease.c

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,19 @@ static bool _drm_has_leased(struct drm_master *master, int id)
106106
*/
107107
bool _drm_lease_held(struct drm_file *file_priv, int id)
108108
{
109-
if (!file_priv || !file_priv->master)
109+
bool ret;
110+
struct drm_master *master;
111+
112+
if (!file_priv)
110113
return true;
111114

112-
return _drm_lease_held_master(file_priv->master, id);
115+
master = drm_file_get_master(file_priv);
116+
if (!master)
117+
return true;
118+
ret = _drm_lease_held_master(master, id);
119+
drm_master_put(&master);
120+
121+
return ret;
113122
}
114123

115124
/**
@@ -128,13 +137,22 @@ bool drm_lease_held(struct drm_file *file_priv, int id)
128137
struct drm_master *master;
129138
bool ret;
130139

131-
if (!file_priv || !file_priv->master || !file_priv->master->lessor)
140+
if (!file_priv)
132141
return true;
133142

134-
master = file_priv->master;
143+
master = drm_file_get_master(file_priv);
144+
if (!master)
145+
return true;
146+
if (!master->lessor) {
147+
ret = true;
148+
goto out;
149+
}
135150
mutex_lock(&master->dev->mode_config.idr_mutex);
136151
ret = _drm_lease_held_master(master, id);
137152
mutex_unlock(&master->dev->mode_config.idr_mutex);
153+
154+
out:
155+
drm_master_put(&master);
138156
return ret;
139157
}
140158

@@ -154,10 +172,16 @@ uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
154172
int count_in, count_out;
155173
uint32_t crtcs_out = 0;
156174

157-
if (!file_priv || !file_priv->master || !file_priv->master->lessor)
175+
if (!file_priv)
158176
return crtcs_in;
159177

160-
master = file_priv->master;
178+
master = drm_file_get_master(file_priv);
179+
if (!master)
180+
return crtcs_in;
181+
if (!master->lessor) {
182+
crtcs_out = crtcs_in;
183+
goto out;
184+
}
161185
dev = master->dev;
162186

163187
count_in = count_out = 0;
@@ -176,6 +200,9 @@ uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
176200
count_in++;
177201
}
178202
mutex_unlock(&master->dev->mode_config.idr_mutex);
203+
204+
out:
205+
drm_master_put(&master);
179206
return crtcs_out;
180207
}
181208

@@ -489,7 +516,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
489516
size_t object_count;
490517
int ret = 0;
491518
struct idr leases;
492-
struct drm_master *lessor = lessor_priv->master;
519+
struct drm_master *lessor;
493520
struct drm_master *lessee = NULL;
494521
struct file *lessee_file = NULL;
495522
struct file *lessor_file = lessor_priv->filp;
@@ -501,12 +528,6 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
501528
if (!drm_core_check_feature(dev, DRIVER_MODESET))
502529
return -EOPNOTSUPP;
503530

504-
/* Do not allow sub-leases */
505-
if (lessor->lessor) {
506-
DRM_DEBUG_LEASE("recursive leasing not allowed\n");
507-
return -EINVAL;
508-
}
509-
510531
/* need some objects */
511532
if (cl->object_count == 0) {
512533
DRM_DEBUG_LEASE("no objects in lease\n");
@@ -518,12 +539,22 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
518539
return -EINVAL;
519540
}
520541

542+
lessor = drm_file_get_master(lessor_priv);
543+
/* Do not allow sub-leases */
544+
if (lessor->lessor) {
545+
DRM_DEBUG_LEASE("recursive leasing not allowed\n");
546+
ret = -EINVAL;
547+
goto out_lessor;
548+
}
549+
521550
object_count = cl->object_count;
522551

523552
object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
524553
array_size(object_count, sizeof(__u32)));
525-
if (IS_ERR(object_ids))
526-
return PTR_ERR(object_ids);
554+
if (IS_ERR(object_ids)) {
555+
ret = PTR_ERR(object_ids);
556+
goto out_lessor;
557+
}
527558

528559
idr_init(&leases);
529560

@@ -534,14 +565,15 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
534565
if (ret) {
535566
DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
536567
idr_destroy(&leases);
537-
return ret;
568+
goto out_lessor;
538569
}
539570

540571
/* Allocate a file descriptor for the lease */
541572
fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
542573
if (fd < 0) {
543574
idr_destroy(&leases);
544-
return fd;
575+
ret = fd;
576+
goto out_lessor;
545577
}
546578

547579
DRM_DEBUG_LEASE("Creating lease\n");
@@ -577,6 +609,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
577609
/* Hook up the fd */
578610
fd_install(fd, lessee_file);
579611

612+
drm_master_put(&lessor);
580613
DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
581614
return 0;
582615

@@ -586,6 +619,8 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
586619
out_leases:
587620
put_unused_fd(fd);
588621

622+
out_lessor:
623+
drm_master_put(&lessor);
589624
DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
590625
return ret;
591626
}
@@ -608,7 +643,7 @@ int drm_mode_list_lessees_ioctl(struct drm_device *dev,
608643
struct drm_mode_list_lessees *arg = data;
609644
__u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
610645
__u32 count_lessees = arg->count_lessees;
611-
struct drm_master *lessor = lessor_priv->master, *lessee;
646+
struct drm_master *lessor, *lessee;
612647
int count;
613648
int ret = 0;
614649

@@ -619,6 +654,7 @@ int drm_mode_list_lessees_ioctl(struct drm_device *dev,
619654
if (!drm_core_check_feature(dev, DRIVER_MODESET))
620655
return -EOPNOTSUPP;
621656

657+
lessor = drm_file_get_master(lessor_priv);
622658
DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
623659

624660
mutex_lock(&dev->mode_config.idr_mutex);
@@ -642,6 +678,7 @@ int drm_mode_list_lessees_ioctl(struct drm_device *dev,
642678
arg->count_lessees = count;
643679

644680
mutex_unlock(&dev->mode_config.idr_mutex);
681+
drm_master_put(&lessor);
645682

646683
return ret;
647684
}
@@ -661,7 +698,7 @@ int drm_mode_get_lease_ioctl(struct drm_device *dev,
661698
struct drm_mode_get_lease *arg = data;
662699
__u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
663700
__u32 count_objects = arg->count_objects;
664-
struct drm_master *lessee = lessee_priv->master;
701+
struct drm_master *lessee;
665702
struct idr *object_idr;
666703
int count;
667704
void *entry;
@@ -675,6 +712,7 @@ int drm_mode_get_lease_ioctl(struct drm_device *dev,
675712
if (!drm_core_check_feature(dev, DRIVER_MODESET))
676713
return -EOPNOTSUPP;
677714

715+
lessee = drm_file_get_master(lessee_priv);
678716
DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
679717

680718
mutex_lock(&dev->mode_config.idr_mutex);
@@ -702,6 +740,7 @@ int drm_mode_get_lease_ioctl(struct drm_device *dev,
702740
arg->count_objects = count;
703741

704742
mutex_unlock(&dev->mode_config.idr_mutex);
743+
drm_master_put(&lessee);
705744

706745
return ret;
707746
}
@@ -720,7 +759,7 @@ int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
720759
void *data, struct drm_file *lessor_priv)
721760
{
722761
struct drm_mode_revoke_lease *arg = data;
723-
struct drm_master *lessor = lessor_priv->master;
762+
struct drm_master *lessor;
724763
struct drm_master *lessee;
725764
int ret = 0;
726765

@@ -730,6 +769,7 @@ int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
730769
if (!drm_core_check_feature(dev, DRIVER_MODESET))
731770
return -EOPNOTSUPP;
732771

772+
lessor = drm_file_get_master(lessor_priv);
733773
mutex_lock(&dev->mode_config.idr_mutex);
734774

735775
lessee = _drm_find_lessee(lessor, arg->lessee_id);
@@ -750,6 +790,7 @@ int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
750790

751791
fail:
752792
mutex_unlock(&dev->mode_config.idr_mutex);
793+
drm_master_put(&lessor);
753794

754795
return ret;
755796
}

include/drm/drm_auth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct drm_master {
107107
};
108108

109109
struct drm_master *drm_master_get(struct drm_master *master);
110+
struct drm_master *drm_file_get_master(struct drm_file *file_priv);
110111
void drm_master_put(struct drm_master **master);
111112
bool drm_is_current_master(struct drm_file *fpriv);
112113

include/drm/drm_file.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ struct drm_file {
233233
* this only matches &drm_device.master if the master is the currently
234234
* active one.
235235
*
236+
* When dereferencing this pointer, either hold struct
237+
* &drm_device.master_mutex for the duration of the pointer's use, or
238+
* use drm_file_get_master() if struct &drm_device.master_mutex is not
239+
* currently held and there is no other need to hold it. This prevents
240+
* @master from being freed during use.
241+
*
236242
* See also @authentication and @is_master and the :ref:`section on
237243
* primary nodes and authentication <drm_primary_node>`.
238244
*/

0 commit comments

Comments
 (0)