Merge tag 'io_uring-5.15-2021-09-11' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_auth.c
1 /*
2  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Author Rickard E. (Rik) Faith <faith@valinux.com>
9  * Author Gareth Hughes <gareth@valinux.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30
31 #include <linux/slab.h>
32
33 #include <drm/drm_auth.h>
34 #include <drm/drm_drv.h>
35 #include <drm/drm_file.h>
36 #include <drm/drm_lease.h>
37 #include <drm/drm_print.h>
38
39 #include "drm_internal.h"
40 #include "drm_legacy.h"
41
42 /**
43  * DOC: master and authentication
44  *
45  * &struct drm_master is used to track groups of clients with open
46  * primary/legacy device nodes. For every &struct drm_file which has had at
47  * least once successfully became the device master (either through the
48  * SET_MASTER IOCTL, or implicitly through opening the primary device node when
49  * no one else is the current master that time) there exists one &drm_master.
50  * This is noted in &drm_file.is_master. All other clients have just a pointer
51  * to the &drm_master they are associated with.
52  *
53  * In addition only one &drm_master can be the current master for a &drm_device.
54  * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
55  * implicitly through closing/opening the primary device node. See also
56  * drm_is_current_master().
57  *
58  * Clients can authenticate against the current master (if it matches their own)
59  * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
60  * this allows controlled access to the device for an entire group of mutually
61  * trusted clients.
62  */
63
64 static bool drm_is_current_master_locked(struct drm_file *fpriv)
65 {
66         lockdep_assert_once(lockdep_is_held(&fpriv->master_lookup_lock) ||
67                             lockdep_is_held(&fpriv->minor->dev->master_mutex));
68
69         return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
70 }
71
72 /**
73  * drm_is_current_master - checks whether @priv is the current master
74  * @fpriv: DRM file private
75  *
76  * Checks whether @fpriv is current master on its device. This decides whether a
77  * client is allowed to run DRM_MASTER IOCTLs.
78  *
79  * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
80  * - the current master is assumed to own the non-shareable display hardware.
81  */
82 bool drm_is_current_master(struct drm_file *fpriv)
83 {
84         bool ret;
85
86         spin_lock(&fpriv->master_lookup_lock);
87         ret = drm_is_current_master_locked(fpriv);
88         spin_unlock(&fpriv->master_lookup_lock);
89
90         return ret;
91 }
92 EXPORT_SYMBOL(drm_is_current_master);
93
94 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
95 {
96         struct drm_auth *auth = data;
97         int ret = 0;
98
99         mutex_lock(&dev->master_mutex);
100         if (!file_priv->magic) {
101                 ret = idr_alloc(&file_priv->master->magic_map, file_priv,
102                                 1, 0, GFP_KERNEL);
103                 if (ret >= 0)
104                         file_priv->magic = ret;
105         }
106         auth->magic = file_priv->magic;
107         mutex_unlock(&dev->master_mutex);
108
109         DRM_DEBUG("%u\n", auth->magic);
110
111         return ret < 0 ? ret : 0;
112 }
113
114 int drm_authmagic(struct drm_device *dev, void *data,
115                   struct drm_file *file_priv)
116 {
117         struct drm_auth *auth = data;
118         struct drm_file *file;
119
120         DRM_DEBUG("%u\n", auth->magic);
121
122         mutex_lock(&dev->master_mutex);
123         file = idr_find(&file_priv->master->magic_map, auth->magic);
124         if (file) {
125                 file->authenticated = 1;
126                 idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
127         }
128         mutex_unlock(&dev->master_mutex);
129
130         return file ? 0 : -EINVAL;
131 }
132
133 struct drm_master *drm_master_create(struct drm_device *dev)
134 {
135         struct drm_master *master;
136
137         master = kzalloc(sizeof(*master), GFP_KERNEL);
138         if (!master)
139                 return NULL;
140
141         kref_init(&master->refcount);
142         drm_master_legacy_init(master);
143         idr_init(&master->magic_map);
144         master->dev = dev;
145
146         /* initialize the tree of output resource lessees */
147         INIT_LIST_HEAD(&master->lessees);
148         INIT_LIST_HEAD(&master->lessee_list);
149         idr_init(&master->leases);
150         idr_init(&master->lessee_idr);
151
152         return master;
153 }
154
155 static void drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
156                            bool new_master)
157 {
158         dev->master = drm_master_get(fpriv->master);
159         if (dev->driver->master_set)
160                 dev->driver->master_set(dev, fpriv, new_master);
161
162         fpriv->was_master = true;
163 }
164
165 static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
166 {
167         struct drm_master *old_master;
168         struct drm_master *new_master;
169
170         lockdep_assert_held_once(&dev->master_mutex);
171
172         WARN_ON(fpriv->is_master);
173         old_master = fpriv->master;
174         new_master = drm_master_create(dev);
175         if (!new_master)
176                 return -ENOMEM;
177         spin_lock(&fpriv->master_lookup_lock);
178         fpriv->master = new_master;
179         spin_unlock(&fpriv->master_lookup_lock);
180
181         fpriv->is_master = 1;
182         fpriv->authenticated = 1;
183
184         drm_set_master(dev, fpriv, true);
185
186         if (old_master)
187                 drm_master_put(&old_master);
188
189         return 0;
190 }
191
192 /*
193  * In the olden days the SET/DROP_MASTER ioctls used to return EACCES when
194  * CAP_SYS_ADMIN was not set. This was used to prevent rogue applications
195  * from becoming master and/or failing to release it.
196  *
197  * At the same time, the first client (for a given VT) is _always_ master.
198  * Thus in order for the ioctls to succeed, one had to _explicitly_ run the
199  * application as root or flip the setuid bit.
200  *
201  * If the CAP_SYS_ADMIN was missing, no other client could become master...
202  * EVER :-( Leading to a) the graphics session dying badly or b) a completely
203  * locked session.
204  *
205  *
206  * As some point systemd-logind was introduced to orchestrate and delegate
207  * master as applicable. It does so by opening the fd and passing it to users
208  * while in itself logind a) does the set/drop master per users' request and
209  * b)  * implicitly drops master on VT switch.
210  *
211  * Even though logind looks like the future, there are a few issues:
212  *  - some platforms don't have equivalent (Android, CrOS, some BSDs) so
213  * root is required _solely_ for SET/DROP MASTER.
214  *  - applications may not be updated to use it,
215  *  - any client which fails to drop master* can DoS the application using
216  * logind, to a varying degree.
217  *
218  * * Either due missing CAP_SYS_ADMIN or simply not calling DROP_MASTER.
219  *
220  *
221  * Here we implement the next best thing:
222  *  - ensure the logind style of fd passing works unchanged, and
223  *  - allow a client to drop/set master, iff it is/was master at a given point
224  * in time.
225  *
226  * Note: DROP_MASTER cannot be free for all, as an arbitrator user could:
227  *  - DoS/crash the arbitrator - details would be implementation specific
228  *  - open the node, become master implicitly and cause issues
229  *
230  * As a result this fixes the following when using root-less build w/o logind
231  * - startx
232  * - weston
233  * - various compositors based on wlroots
234  */
235 static int
236 drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
237 {
238         if (file_priv->pid == task_pid(current) && file_priv->was_master)
239                 return 0;
240
241         if (!capable(CAP_SYS_ADMIN))
242                 return -EACCES;
243
244         return 0;
245 }
246
247 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
248                         struct drm_file *file_priv)
249 {
250         int ret;
251
252         mutex_lock(&dev->master_mutex);
253
254         ret = drm_master_check_perm(dev, file_priv);
255         if (ret)
256                 goto out_unlock;
257
258         if (drm_is_current_master_locked(file_priv))
259                 goto out_unlock;
260
261         if (dev->master) {
262                 ret = -EBUSY;
263                 goto out_unlock;
264         }
265
266         if (!file_priv->master) {
267                 ret = -EINVAL;
268                 goto out_unlock;
269         }
270
271         if (!file_priv->is_master) {
272                 ret = drm_new_set_master(dev, file_priv);
273                 goto out_unlock;
274         }
275
276         if (file_priv->master->lessor != NULL) {
277                 DRM_DEBUG_LEASE("Attempt to set lessee %d as master\n", file_priv->master->lessee_id);
278                 ret = -EINVAL;
279                 goto out_unlock;
280         }
281
282         drm_set_master(dev, file_priv, false);
283 out_unlock:
284         mutex_unlock(&dev->master_mutex);
285         return ret;
286 }
287
288 static void drm_drop_master(struct drm_device *dev,
289                             struct drm_file *fpriv)
290 {
291         if (dev->driver->master_drop)
292                 dev->driver->master_drop(dev, fpriv);
293         drm_master_put(&dev->master);
294 }
295
296 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
297                          struct drm_file *file_priv)
298 {
299         int ret;
300
301         mutex_lock(&dev->master_mutex);
302
303         ret = drm_master_check_perm(dev, file_priv);
304         if (ret)
305                 goto out_unlock;
306
307         if (!drm_is_current_master_locked(file_priv)) {
308                 ret = -EINVAL;
309                 goto out_unlock;
310         }
311
312         if (!dev->master) {
313                 ret = -EINVAL;
314                 goto out_unlock;
315         }
316
317         if (file_priv->master->lessor != NULL) {
318                 DRM_DEBUG_LEASE("Attempt to drop lessee %d as master\n", file_priv->master->lessee_id);
319                 ret = -EINVAL;
320                 goto out_unlock;
321         }
322
323         drm_drop_master(dev, file_priv);
324 out_unlock:
325         mutex_unlock(&dev->master_mutex);
326         return ret;
327 }
328
329 int drm_master_open(struct drm_file *file_priv)
330 {
331         struct drm_device *dev = file_priv->minor->dev;
332         int ret = 0;
333
334         /* if there is no current master make this fd it, but do not create
335          * any master object for render clients
336          */
337         mutex_lock(&dev->master_mutex);
338         if (!dev->master) {
339                 ret = drm_new_set_master(dev, file_priv);
340         } else {
341                 spin_lock(&file_priv->master_lookup_lock);
342                 file_priv->master = drm_master_get(dev->master);
343                 spin_unlock(&file_priv->master_lookup_lock);
344         }
345         mutex_unlock(&dev->master_mutex);
346
347         return ret;
348 }
349
350 void drm_master_release(struct drm_file *file_priv)
351 {
352         struct drm_device *dev = file_priv->minor->dev;
353         struct drm_master *master;
354
355         mutex_lock(&dev->master_mutex);
356         master = file_priv->master;
357         if (file_priv->magic)
358                 idr_remove(&file_priv->master->magic_map, file_priv->magic);
359
360         if (!drm_is_current_master_locked(file_priv))
361                 goto out;
362
363         drm_legacy_lock_master_cleanup(dev, master);
364
365         if (dev->master == file_priv->master)
366                 drm_drop_master(dev, file_priv);
367 out:
368         if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
369                 /* Revoke any leases held by this or lessees, but only if
370                  * this is the "real" master
371                  */
372                 drm_lease_revoke(master);
373         }
374
375         /* drop the master reference held by the file priv */
376         if (file_priv->master)
377                 drm_master_put(&file_priv->master);
378         mutex_unlock(&dev->master_mutex);
379 }
380
381 /**
382  * drm_master_get - reference a master pointer
383  * @master: &struct drm_master
384  *
385  * Increments the reference count of @master and returns a pointer to @master.
386  */
387 struct drm_master *drm_master_get(struct drm_master *master)
388 {
389         kref_get(&master->refcount);
390         return master;
391 }
392 EXPORT_SYMBOL(drm_master_get);
393
394 /**
395  * drm_file_get_master - reference &drm_file.master of @file_priv
396  * @file_priv: DRM file private
397  *
398  * Increments the reference count of @file_priv's &drm_file.master and returns
399  * the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
400  *
401  * Master pointers returned from this function should be unreferenced using
402  * drm_master_put().
403  */
404 struct drm_master *drm_file_get_master(struct drm_file *file_priv)
405 {
406         struct drm_master *master = NULL;
407
408         spin_lock(&file_priv->master_lookup_lock);
409         if (!file_priv->master)
410                 goto unlock;
411         master = drm_master_get(file_priv->master);
412
413 unlock:
414         spin_unlock(&file_priv->master_lookup_lock);
415         return master;
416 }
417 EXPORT_SYMBOL(drm_file_get_master);
418
419 static void drm_master_destroy(struct kref *kref)
420 {
421         struct drm_master *master = container_of(kref, struct drm_master, refcount);
422         struct drm_device *dev = master->dev;
423
424         if (drm_core_check_feature(dev, DRIVER_MODESET))
425                 drm_lease_destroy(master);
426
427         drm_legacy_master_rmmaps(dev, master);
428
429         idr_destroy(&master->magic_map);
430         idr_destroy(&master->leases);
431         idr_destroy(&master->lessee_idr);
432
433         kfree(master->unique);
434         kfree(master);
435 }
436
437 /**
438  * drm_master_put - unreference and clear a master pointer
439  * @master: pointer to a pointer of &struct drm_master
440  *
441  * This decrements the &drm_master behind @master and sets it to NULL.
442  */
443 void drm_master_put(struct drm_master **master)
444 {
445         kref_put(&(*master)->refcount, drm_master_destroy);
446         *master = NULL;
447 }
448 EXPORT_SYMBOL(drm_master_put);
449
450 /* Used by drm_client and drm_fb_helper */
451 bool drm_master_internal_acquire(struct drm_device *dev)
452 {
453         mutex_lock(&dev->master_mutex);
454         if (dev->master) {
455                 mutex_unlock(&dev->master_mutex);
456                 return false;
457         }
458
459         return true;
460 }
461 EXPORT_SYMBOL(drm_master_internal_acquire);
462
463 /* Used by drm_client and drm_fb_helper */
464 void drm_master_internal_release(struct drm_device *dev)
465 {
466         mutex_unlock(&dev->master_mutex);
467 }
468 EXPORT_SYMBOL(drm_master_internal_release);