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
64static 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 */
86bool 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}
96EXPORT_SYMBOL(drm_is_current_master);
97
98int 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
118int 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
137struct 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
159static 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
169static 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 */
239static int
240drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
241{
242#ifdef __linux__
243	if (file_priv->was_master &&
244	    rcu_access_pointer(file_priv->pid) == task_tgid(current))
245		return 0;
246#endif
247
248	if (!capable(CAP_SYS_ADMIN))
249		return -EACCES;
250
251	return 0;
252}
253
254int drm_setmaster_ioctl(struct drm_device *dev, void *data,
255			struct drm_file *file_priv)
256{
257	int ret;
258
259	mutex_lock(&dev->master_mutex);
260
261	ret = drm_master_check_perm(dev, file_priv);
262	if (ret)
263		goto out_unlock;
264
265	if (drm_is_current_master_locked(file_priv))
266		goto out_unlock;
267
268	if (dev->master) {
269		ret = -EBUSY;
270		goto out_unlock;
271	}
272
273	if (!file_priv->master) {
274		ret = -EINVAL;
275		goto out_unlock;
276	}
277
278	if (!file_priv->is_master) {
279		ret = drm_new_set_master(dev, file_priv);
280		goto out_unlock;
281	}
282
283	if (file_priv->master->lessor != NULL) {
284		drm_dbg_lease(dev,
285			      "Attempt to set lessee %d as master\n",
286			      file_priv->master->lessee_id);
287		ret = -EINVAL;
288		goto out_unlock;
289	}
290
291	drm_set_master(dev, file_priv, false);
292out_unlock:
293	mutex_unlock(&dev->master_mutex);
294	return ret;
295}
296
297static void drm_drop_master(struct drm_device *dev,
298			    struct drm_file *fpriv)
299{
300	if (dev->driver->master_drop)
301		dev->driver->master_drop(dev, fpriv);
302	drm_master_put(&dev->master);
303}
304
305int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
306			 struct drm_file *file_priv)
307{
308	int ret;
309
310	mutex_lock(&dev->master_mutex);
311
312	ret = drm_master_check_perm(dev, file_priv);
313	if (ret)
314		goto out_unlock;
315
316	if (!drm_is_current_master_locked(file_priv)) {
317		ret = -EINVAL;
318		goto out_unlock;
319	}
320
321	if (!dev->master) {
322		ret = -EINVAL;
323		goto out_unlock;
324	}
325
326	if (file_priv->master->lessor != NULL) {
327		drm_dbg_lease(dev,
328			      "Attempt to drop lessee %d as master\n",
329			      file_priv->master->lessee_id);
330		ret = -EINVAL;
331		goto out_unlock;
332	}
333
334	drm_drop_master(dev, file_priv);
335out_unlock:
336	mutex_unlock(&dev->master_mutex);
337	return ret;
338}
339
340int drm_master_open(struct drm_file *file_priv)
341{
342	struct drm_device *dev = file_priv->minor->dev;
343	int ret = 0;
344
345	/* if there is no current master make this fd it, but do not create
346	 * any master object for render clients
347	 */
348	mutex_lock(&dev->master_mutex);
349	if (!dev->master) {
350		ret = drm_new_set_master(dev, file_priv);
351	} else {
352		spin_lock(&file_priv->master_lookup_lock);
353		file_priv->master = drm_master_get(dev->master);
354		spin_unlock(&file_priv->master_lookup_lock);
355	}
356	mutex_unlock(&dev->master_mutex);
357
358	return ret;
359}
360
361void drm_master_release(struct drm_file *file_priv)
362{
363	struct drm_device *dev = file_priv->minor->dev;
364	struct drm_master *master;
365
366	mutex_lock(&dev->master_mutex);
367	master = file_priv->master;
368	if (file_priv->magic)
369		idr_remove(&file_priv->master->magic_map, file_priv->magic);
370
371	if (!drm_is_current_master_locked(file_priv))
372		goto out;
373
374	drm_legacy_lock_master_cleanup(dev, master);
375
376	if (dev->master == file_priv->master)
377		drm_drop_master(dev, file_priv);
378out:
379	if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
380		/* Revoke any leases held by this or lessees, but only if
381		 * this is the "real" master
382		 */
383		drm_lease_revoke(master);
384	}
385
386	/* drop the master reference held by the file priv */
387	if (file_priv->master)
388		drm_master_put(&file_priv->master);
389	mutex_unlock(&dev->master_mutex);
390}
391
392/**
393 * drm_master_get - reference a master pointer
394 * @master: &struct drm_master
395 *
396 * Increments the reference count of @master and returns a pointer to @master.
397 */
398struct drm_master *drm_master_get(struct drm_master *master)
399{
400	kref_get(&master->refcount);
401	return master;
402}
403EXPORT_SYMBOL(drm_master_get);
404
405/**
406 * drm_file_get_master - reference &drm_file.master of @file_priv
407 * @file_priv: DRM file private
408 *
409 * Increments the reference count of @file_priv's &drm_file.master and returns
410 * the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
411 *
412 * Master pointers returned from this function should be unreferenced using
413 * drm_master_put().
414 */
415struct drm_master *drm_file_get_master(struct drm_file *file_priv)
416{
417	struct drm_master *master = NULL;
418
419	spin_lock(&file_priv->master_lookup_lock);
420	if (!file_priv->master)
421		goto unlock;
422	master = drm_master_get(file_priv->master);
423
424unlock:
425	spin_unlock(&file_priv->master_lookup_lock);
426	return master;
427}
428EXPORT_SYMBOL(drm_file_get_master);
429
430static void drm_master_destroy(struct kref *kref)
431{
432	struct drm_master *master = container_of(kref, struct drm_master, refcount);
433	struct drm_device *dev = master->dev;
434
435	if (drm_core_check_feature(dev, DRIVER_MODESET))
436		drm_lease_destroy(master);
437
438	drm_legacy_master_rmmaps(dev, master);
439
440	idr_destroy(&master->magic_map);
441	idr_destroy(&master->leases);
442	idr_destroy(&master->lessee_idr);
443
444	kfree(master->unique);
445	kfree(master);
446}
447
448/**
449 * drm_master_put - unreference and clear a master pointer
450 * @master: pointer to a pointer of &struct drm_master
451 *
452 * This decrements the &drm_master behind @master and sets it to NULL.
453 */
454void drm_master_put(struct drm_master **master)
455{
456	kref_put(&(*master)->refcount, drm_master_destroy);
457	*master = NULL;
458}
459EXPORT_SYMBOL(drm_master_put);
460
461/* Used by drm_client and drm_fb_helper */
462bool drm_master_internal_acquire(struct drm_device *dev)
463{
464	mutex_lock(&dev->master_mutex);
465	if (dev->master) {
466		mutex_unlock(&dev->master_mutex);
467		return false;
468	}
469
470	return true;
471}
472EXPORT_SYMBOL(drm_master_internal_acquire);
473
474/* Used by drm_client and drm_fb_helper */
475void drm_master_internal_release(struct drm_device *dev)
476{
477	mutex_unlock(&dev->master_mutex);
478}
479EXPORT_SYMBOL(drm_master_internal_release);
480