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