xref: /openbsd-src/sys/dev/pci/drm/drm_auth.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
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 #ifdef notyet
70 	return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
71 #else
72 	return (fpriv->is_master ==1);
73 #endif
74 }
75 
76 /**
77  * drm_is_current_master - checks whether @priv is the current master
78  * @fpriv: DRM file private
79  *
80  * Checks whether @fpriv is current master on its device. This decides whether a
81  * client is allowed to run DRM_MASTER IOCTLs.
82  *
83  * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
84  * - the current master is assumed to own the non-shareable display hardware.
85  */
86 bool drm_is_current_master(struct drm_file *fpriv)
87 {
88 	bool ret;
89 
90 	spin_lock(&fpriv->master_lookup_lock);
91 	ret = drm_is_current_master_locked(fpriv);
92 	spin_unlock(&fpriv->master_lookup_lock);
93 
94 	return ret;
95 }
96 EXPORT_SYMBOL(drm_is_current_master);
97 
98 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
99 {
100 	struct drm_auth *auth = data;
101 	int ret = 0;
102 
103 	mutex_lock(&dev->master_mutex);
104 	if (!file_priv->magic) {
105 		ret = idr_alloc(&file_priv->master->magic_map, file_priv,
106 				1, 0, GFP_KERNEL);
107 		if (ret >= 0)
108 			file_priv->magic = ret;
109 	}
110 	auth->magic = file_priv->magic;
111 	mutex_unlock(&dev->master_mutex);
112 
113 	drm_dbg_core(dev, "%u\n", auth->magic);
114 
115 	return ret < 0 ? ret : 0;
116 }
117 
118 int drm_authmagic(struct drm_device *dev, void *data,
119 		  struct drm_file *file_priv)
120 {
121 	struct drm_auth *auth = data;
122 	struct drm_file *file;
123 
124 	drm_dbg_core(dev, "%u\n", auth->magic);
125 
126 	mutex_lock(&dev->master_mutex);
127 	file = idr_find(&file_priv->master->magic_map, auth->magic);
128 	if (file) {
129 		file->authenticated = 1;
130 		idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
131 	}
132 	mutex_unlock(&dev->master_mutex);
133 
134 	return file ? 0 : -EINVAL;
135 }
136 
137 struct drm_master *drm_master_create(struct drm_device *dev)
138 {
139 	struct drm_master *master;
140 
141 	master = kzalloc(sizeof(*master), GFP_KERNEL);
142 	if (!master)
143 		return NULL;
144 
145 	kref_init(&master->refcount);
146 	drm_master_legacy_init(master);
147 	idr_init_base(&master->magic_map, 1);
148 	master->dev = dev;
149 
150 	/* initialize the tree of output resource lessees */
151 	INIT_LIST_HEAD(&master->lessees);
152 	INIT_LIST_HEAD(&master->lessee_list);
153 	idr_init(&master->leases);
154 	idr_init_base(&master->lessee_idr, 1);
155 
156 	return master;
157 }
158 
159 static void drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
160 			   bool new_master)
161 {
162 	dev->master = drm_master_get(fpriv->master);
163 	if (dev->driver->master_set)
164 		dev->driver->master_set(dev, fpriv, new_master);
165 
166 	fpriv->was_master = true;
167 }
168 
169 static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
170 {
171 	struct drm_master *old_master;
172 	struct drm_master *new_master;
173 
174 	lockdep_assert_held_once(&dev->master_mutex);
175 
176 	WARN_ON(fpriv->is_master);
177 	old_master = fpriv->master;
178 	new_master = drm_master_create(dev);
179 	if (!new_master)
180 		return -ENOMEM;
181 	spin_lock(&fpriv->master_lookup_lock);
182 	fpriv->master = new_master;
183 	spin_unlock(&fpriv->master_lookup_lock);
184 
185 	fpriv->is_master = 1;
186 	fpriv->authenticated = 1;
187 
188 	drm_set_master(dev, fpriv, true);
189 
190 	if (old_master)
191 		drm_master_put(&old_master);
192 
193 	return 0;
194 }
195 
196 /*
197  * In the olden days the SET/DROP_MASTER ioctls used to return EACCES when
198  * CAP_SYS_ADMIN was not set. This was used to prevent rogue applications
199  * from becoming master and/or failing to release it.
200  *
201  * At the same time, the first client (for a given VT) is _always_ master.
202  * Thus in order for the ioctls to succeed, one had to _explicitly_ run the
203  * application as root or flip the setuid bit.
204  *
205  * If the CAP_SYS_ADMIN was missing, no other client could become master...
206  * EVER :-( Leading to a) the graphics session dying badly or b) a completely
207  * locked session.
208  *
209  *
210  * As some point systemd-logind was introduced to orchestrate and delegate
211  * master as applicable. It does so by opening the fd and passing it to users
212  * while in itself logind a) does the set/drop master per users' request and
213  * b)  * implicitly drops master on VT switch.
214  *
215  * Even though logind looks like the future, there are a few issues:
216  *  - some platforms don't have equivalent (Android, CrOS, some BSDs) so
217  * root is required _solely_ for SET/DROP MASTER.
218  *  - applications may not be updated to use it,
219  *  - any client which fails to drop master* can DoS the application using
220  * logind, to a varying degree.
221  *
222  * * Either due missing CAP_SYS_ADMIN or simply not calling DROP_MASTER.
223  *
224  *
225  * Here we implement the next best thing:
226  *  - ensure the logind style of fd passing works unchanged, and
227  *  - allow a client to drop/set master, iff it is/was master at a given point
228  * in time.
229  *
230  * Note: DROP_MASTER cannot be free for all, as an arbitrator user could:
231  *  - DoS/crash the arbitrator - details would be implementation specific
232  *  - open the node, become master implicitly and cause issues
233  *
234  * As a result this fixes the following when using root-less build w/o logind
235  * - startx
236  * - weston
237  * - various compositors based on wlroots
238  */
239 static int
240 drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
241 {
242 #ifdef __linux__
243 	if (file_priv->pid == task_pid(current) && file_priv->was_master)
244 		return 0;
245 #endif
246 
247 	if (!capable(CAP_SYS_ADMIN))
248 		return -EACCES;
249 
250 	return 0;
251 }
252 
253 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
254 			struct drm_file *file_priv)
255 {
256 	int ret;
257 
258 	mutex_lock(&dev->master_mutex);
259 
260 	ret = drm_master_check_perm(dev, file_priv);
261 	if (ret)
262 		goto out_unlock;
263 
264 	if (drm_is_current_master_locked(file_priv))
265 		goto out_unlock;
266 
267 	if (dev->master) {
268 		ret = -EBUSY;
269 		goto out_unlock;
270 	}
271 
272 	if (!file_priv->master) {
273 		ret = -EINVAL;
274 		goto out_unlock;
275 	}
276 
277 	if (!file_priv->is_master) {
278 		ret = drm_new_set_master(dev, file_priv);
279 		goto out_unlock;
280 	}
281 
282 	if (file_priv->master->lessor != NULL) {
283 		drm_dbg_lease(dev,
284 			      "Attempt to set lessee %d as master\n",
285 			      file_priv->master->lessee_id);
286 		ret = -EINVAL;
287 		goto out_unlock;
288 	}
289 
290 	drm_set_master(dev, file_priv, false);
291 out_unlock:
292 	mutex_unlock(&dev->master_mutex);
293 	return ret;
294 }
295 
296 static void drm_drop_master(struct drm_device *dev,
297 			    struct drm_file *fpriv)
298 {
299 	if (dev->driver->master_drop)
300 		dev->driver->master_drop(dev, fpriv);
301 	drm_master_put(&dev->master);
302 }
303 
304 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
305 			 struct drm_file *file_priv)
306 {
307 	int ret;
308 
309 	mutex_lock(&dev->master_mutex);
310 
311 	ret = drm_master_check_perm(dev, file_priv);
312 	if (ret)
313 		goto out_unlock;
314 
315 	if (!drm_is_current_master_locked(file_priv)) {
316 		ret = -EINVAL;
317 		goto out_unlock;
318 	}
319 
320 	if (!dev->master) {
321 		ret = -EINVAL;
322 		goto out_unlock;
323 	}
324 
325 	if (file_priv->master->lessor != NULL) {
326 		drm_dbg_lease(dev,
327 			      "Attempt to drop lessee %d as master\n",
328 			      file_priv->master->lessee_id);
329 		ret = -EINVAL;
330 		goto out_unlock;
331 	}
332 
333 	drm_drop_master(dev, file_priv);
334 out_unlock:
335 	mutex_unlock(&dev->master_mutex);
336 	return ret;
337 }
338 
339 int drm_master_open(struct drm_file *file_priv)
340 {
341 	struct drm_device *dev = file_priv->minor->dev;
342 	int ret = 0;
343 
344 	/* if there is no current master make this fd it, but do not create
345 	 * any master object for render clients
346 	 */
347 	mutex_lock(&dev->master_mutex);
348 	if (!dev->master) {
349 		ret = drm_new_set_master(dev, file_priv);
350 	} else {
351 		spin_lock(&file_priv->master_lookup_lock);
352 		file_priv->master = drm_master_get(dev->master);
353 		spin_unlock(&file_priv->master_lookup_lock);
354 	}
355 	mutex_unlock(&dev->master_mutex);
356 
357 	return ret;
358 }
359 
360 void drm_master_release(struct drm_file *file_priv)
361 {
362 	struct drm_device *dev = file_priv->minor->dev;
363 	struct drm_master *master;
364 
365 	mutex_lock(&dev->master_mutex);
366 	master = file_priv->master;
367 	if (file_priv->magic)
368 		idr_remove(&file_priv->master->magic_map, file_priv->magic);
369 
370 	if (!drm_is_current_master_locked(file_priv))
371 		goto out;
372 
373 	drm_legacy_lock_master_cleanup(dev, master);
374 
375 	if (dev->master == file_priv->master)
376 		drm_drop_master(dev, file_priv);
377 out:
378 	if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
379 		/* Revoke any leases held by this or lessees, but only if
380 		 * this is the "real" master
381 		 */
382 		drm_lease_revoke(master);
383 	}
384 
385 	/* drop the master reference held by the file priv */
386 	if (file_priv->master)
387 		drm_master_put(&file_priv->master);
388 	mutex_unlock(&dev->master_mutex);
389 }
390 
391 /**
392  * drm_master_get - reference a master pointer
393  * @master: &struct drm_master
394  *
395  * Increments the reference count of @master and returns a pointer to @master.
396  */
397 struct drm_master *drm_master_get(struct drm_master *master)
398 {
399 	kref_get(&master->refcount);
400 	return master;
401 }
402 EXPORT_SYMBOL(drm_master_get);
403 
404 /**
405  * drm_file_get_master - reference &drm_file.master of @file_priv
406  * @file_priv: DRM file private
407  *
408  * Increments the reference count of @file_priv's &drm_file.master and returns
409  * the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
410  *
411  * Master pointers returned from this function should be unreferenced using
412  * drm_master_put().
413  */
414 struct drm_master *drm_file_get_master(struct drm_file *file_priv)
415 {
416 	struct drm_master *master = NULL;
417 
418 	spin_lock(&file_priv->master_lookup_lock);
419 	if (!file_priv->master)
420 		goto unlock;
421 	master = drm_master_get(file_priv->master);
422 
423 unlock:
424 	spin_unlock(&file_priv->master_lookup_lock);
425 	return master;
426 }
427 EXPORT_SYMBOL(drm_file_get_master);
428 
429 static void drm_master_destroy(struct kref *kref)
430 {
431 	struct drm_master *master = container_of(kref, struct drm_master, refcount);
432 	struct drm_device *dev = master->dev;
433 
434 	if (drm_core_check_feature(dev, DRIVER_MODESET))
435 		drm_lease_destroy(master);
436 
437 	drm_legacy_master_rmmaps(dev, master);
438 
439 	idr_destroy(&master->magic_map);
440 	idr_destroy(&master->leases);
441 	idr_destroy(&master->lessee_idr);
442 
443 	kfree(master->unique);
444 	kfree(master);
445 }
446 
447 /**
448  * drm_master_put - unreference and clear a master pointer
449  * @master: pointer to a pointer of &struct drm_master
450  *
451  * This decrements the &drm_master behind @master and sets it to NULL.
452  */
453 void drm_master_put(struct drm_master **master)
454 {
455 	kref_put(&(*master)->refcount, drm_master_destroy);
456 	*master = NULL;
457 }
458 EXPORT_SYMBOL(drm_master_put);
459 
460 /* Used by drm_client and drm_fb_helper */
461 bool drm_master_internal_acquire(struct drm_device *dev)
462 {
463 	mutex_lock(&dev->master_mutex);
464 	if (dev->master) {
465 		mutex_unlock(&dev->master_mutex);
466 		return false;
467 	}
468 
469 	return true;
470 }
471 EXPORT_SYMBOL(drm_master_internal_acquire);
472 
473 /* Used by drm_client and drm_fb_helper */
474 void drm_master_internal_release(struct drm_device *dev)
475 {
476 	mutex_unlock(&dev->master_mutex);
477 }
478 EXPORT_SYMBOL(drm_master_internal_release);
479