xref: /freebsd/sys/dev/drm2/drm_auth.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1*592ffb21SWarner Losh /**
2*592ffb21SWarner Losh  * \file drm_auth.c
3*592ffb21SWarner Losh  * IOCTLs for authentication
4*592ffb21SWarner Losh  *
5*592ffb21SWarner Losh  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6*592ffb21SWarner Losh  * \author Gareth Hughes <gareth@valinux.com>
7*592ffb21SWarner Losh  */
8*592ffb21SWarner Losh 
9*592ffb21SWarner Losh /*
10*592ffb21SWarner Losh  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
11*592ffb21SWarner Losh  *
12*592ffb21SWarner Losh  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13*592ffb21SWarner Losh  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14*592ffb21SWarner Losh  * All Rights Reserved.
15*592ffb21SWarner Losh  *
16*592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
17*592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
18*592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
19*592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20*592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
21*592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
22*592ffb21SWarner Losh  *
23*592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
24*592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
25*592ffb21SWarner Losh  * Software.
26*592ffb21SWarner Losh  *
27*592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28*592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29*592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30*592ffb21SWarner Losh  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31*592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32*592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33*592ffb21SWarner Losh  * OTHER DEALINGS IN THE SOFTWARE.
34*592ffb21SWarner Losh  */
35*592ffb21SWarner Losh 
36*592ffb21SWarner Losh #include <sys/cdefs.h>
37*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
38*592ffb21SWarner Losh 
39*592ffb21SWarner Losh static struct mtx drm_magic_lock;
40*592ffb21SWarner Losh 
41*592ffb21SWarner Losh /**
42*592ffb21SWarner Losh  * Find the file with the given magic number.
43*592ffb21SWarner Losh  *
44*592ffb21SWarner Losh  * \param dev DRM device.
45*592ffb21SWarner Losh  * \param magic magic number.
46*592ffb21SWarner Losh  *
47*592ffb21SWarner Losh  * Searches in drm_device::magiclist within all files with the same hash key
48*592ffb21SWarner Losh  * the one with matching magic number, while holding the drm_device::struct_mutex
49*592ffb21SWarner Losh  * lock.
50*592ffb21SWarner Losh  */
drm_find_file(struct drm_master * master,drm_magic_t magic)51*592ffb21SWarner Losh static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
52*592ffb21SWarner Losh {
53*592ffb21SWarner Losh 	struct drm_file *retval = NULL;
54*592ffb21SWarner Losh 	struct drm_magic_entry *pt;
55*592ffb21SWarner Losh 	struct drm_hash_item *hash;
56*592ffb21SWarner Losh 	struct drm_device *dev = master->minor->dev;
57*592ffb21SWarner Losh 
58*592ffb21SWarner Losh 	DRM_LOCK(dev);
59*592ffb21SWarner Losh 	if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
60*592ffb21SWarner Losh 		pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
61*592ffb21SWarner Losh 		retval = pt->priv;
62*592ffb21SWarner Losh 	}
63*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
64*592ffb21SWarner Losh 	return retval;
65*592ffb21SWarner Losh }
66*592ffb21SWarner Losh 
67*592ffb21SWarner Losh /**
68*592ffb21SWarner Losh  * Adds a magic number.
69*592ffb21SWarner Losh  *
70*592ffb21SWarner Losh  * \param dev DRM device.
71*592ffb21SWarner Losh  * \param priv file private data.
72*592ffb21SWarner Losh  * \param magic magic number.
73*592ffb21SWarner Losh  *
74*592ffb21SWarner Losh  * Creates a drm_magic_entry structure and appends to the linked list
75*592ffb21SWarner Losh  * associated the magic number hash key in drm_device::magiclist, while holding
76*592ffb21SWarner Losh  * the drm_device::struct_mutex lock.
77*592ffb21SWarner Losh  */
drm_add_magic(struct drm_master * master,struct drm_file * priv,drm_magic_t magic)78*592ffb21SWarner Losh static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
79*592ffb21SWarner Losh 			 drm_magic_t magic)
80*592ffb21SWarner Losh {
81*592ffb21SWarner Losh 	struct drm_magic_entry *entry;
82*592ffb21SWarner Losh 	struct drm_device *dev = master->minor->dev;
83*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", magic);
84*592ffb21SWarner Losh 
85*592ffb21SWarner Losh 	entry = malloc(sizeof(*entry), DRM_MEM_MAGIC, M_ZERO | M_NOWAIT);
86*592ffb21SWarner Losh 	if (!entry)
87*592ffb21SWarner Losh 		return -ENOMEM;
88*592ffb21SWarner Losh 	entry->priv = priv;
89*592ffb21SWarner Losh 	entry->hash_item.key = (unsigned long)magic;
90*592ffb21SWarner Losh 	DRM_LOCK(dev);
91*592ffb21SWarner Losh 	drm_ht_insert_item(&master->magiclist, &entry->hash_item);
92*592ffb21SWarner Losh 	list_add_tail(&entry->head, &master->magicfree);
93*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
94*592ffb21SWarner Losh 
95*592ffb21SWarner Losh 	return 0;
96*592ffb21SWarner Losh }
97*592ffb21SWarner Losh 
98*592ffb21SWarner Losh /**
99*592ffb21SWarner Losh  * Remove a magic number.
100*592ffb21SWarner Losh  *
101*592ffb21SWarner Losh  * \param dev DRM device.
102*592ffb21SWarner Losh  * \param magic magic number.
103*592ffb21SWarner Losh  *
104*592ffb21SWarner Losh  * Searches and unlinks the entry in drm_device::magiclist with the magic
105*592ffb21SWarner Losh  * number hash key, while holding the drm_device::struct_mutex lock.
106*592ffb21SWarner Losh  */
drm_remove_magic(struct drm_master * master,drm_magic_t magic)107*592ffb21SWarner Losh int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
108*592ffb21SWarner Losh {
109*592ffb21SWarner Losh 	struct drm_magic_entry *pt;
110*592ffb21SWarner Losh 	struct drm_hash_item *hash;
111*592ffb21SWarner Losh 	struct drm_device *dev = master->minor->dev;
112*592ffb21SWarner Losh 
113*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", magic);
114*592ffb21SWarner Losh 
115*592ffb21SWarner Losh 	DRM_LOCK(dev);
116*592ffb21SWarner Losh 	if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
117*592ffb21SWarner Losh 		DRM_UNLOCK(dev);
118*592ffb21SWarner Losh 		return -EINVAL;
119*592ffb21SWarner Losh 	}
120*592ffb21SWarner Losh 	pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
121*592ffb21SWarner Losh 	drm_ht_remove_item(&master->magiclist, hash);
122*592ffb21SWarner Losh 	list_del(&pt->head);
123*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
124*592ffb21SWarner Losh 
125*592ffb21SWarner Losh 	free(pt, DRM_MEM_MAGIC);
126*592ffb21SWarner Losh 
127*592ffb21SWarner Losh 	return 0;
128*592ffb21SWarner Losh }
129*592ffb21SWarner Losh 
130*592ffb21SWarner Losh /**
131*592ffb21SWarner Losh  * Get a unique magic number (ioctl).
132*592ffb21SWarner Losh  *
133*592ffb21SWarner Losh  * \param inode device inode.
134*592ffb21SWarner Losh  * \param file_priv DRM file private.
135*592ffb21SWarner Losh  * \param cmd command.
136*592ffb21SWarner Losh  * \param arg pointer to a resulting drm_auth structure.
137*592ffb21SWarner Losh  * \return zero on success, or a negative number on failure.
138*592ffb21SWarner Losh  *
139*592ffb21SWarner Losh  * If there is a magic number in drm_file::magic then use it, otherwise
140*592ffb21SWarner Losh  * searches an unique non-zero magic number and add it associating it with \p
141*592ffb21SWarner Losh  * file_priv.
142*592ffb21SWarner Losh  * This ioctl needs protection by the drm_global_mutex, which protects
143*592ffb21SWarner Losh  * struct drm_file::magic and struct drm_magic_entry::priv.
144*592ffb21SWarner Losh  */
drm_getmagic(struct drm_device * dev,void * data,struct drm_file * file_priv)145*592ffb21SWarner Losh int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
146*592ffb21SWarner Losh {
147*592ffb21SWarner Losh 	static drm_magic_t sequence = 0;
148*592ffb21SWarner Losh 	struct drm_auth *auth = data;
149*592ffb21SWarner Losh 
150*592ffb21SWarner Losh 	/* Find unique magic */
151*592ffb21SWarner Losh 	if (file_priv->magic) {
152*592ffb21SWarner Losh 		auth->magic = file_priv->magic;
153*592ffb21SWarner Losh 	} else {
154*592ffb21SWarner Losh 		do {
155*592ffb21SWarner Losh 			mtx_lock(&drm_magic_lock);
156*592ffb21SWarner Losh 			if (!sequence)
157*592ffb21SWarner Losh 				++sequence;	/* reserve 0 */
158*592ffb21SWarner Losh 			auth->magic = sequence++;
159*592ffb21SWarner Losh 			mtx_unlock(&drm_magic_lock);
160*592ffb21SWarner Losh 		} while (drm_find_file(file_priv->master, auth->magic));
161*592ffb21SWarner Losh 		file_priv->magic = auth->magic;
162*592ffb21SWarner Losh 		drm_add_magic(file_priv->master, file_priv, auth->magic);
163*592ffb21SWarner Losh 	}
164*592ffb21SWarner Losh 
165*592ffb21SWarner Losh 	DRM_DEBUG("%u\n", auth->magic);
166*592ffb21SWarner Losh 
167*592ffb21SWarner Losh 	return 0;
168*592ffb21SWarner Losh }
169*592ffb21SWarner Losh 
170*592ffb21SWarner Losh /**
171*592ffb21SWarner Losh  * Authenticate with a magic.
172*592ffb21SWarner Losh  *
173*592ffb21SWarner Losh  * \param inode device inode.
174*592ffb21SWarner Losh  * \param file_priv DRM file private.
175*592ffb21SWarner Losh  * \param cmd command.
176*592ffb21SWarner Losh  * \param arg pointer to a drm_auth structure.
177*592ffb21SWarner Losh  * \return zero if authentication successed, or a negative number otherwise.
178*592ffb21SWarner Losh  *
179*592ffb21SWarner Losh  * Checks if \p file_priv is associated with the magic number passed in \arg.
180*592ffb21SWarner Losh  * This ioctl needs protection by the drm_global_mutex, which protects
181*592ffb21SWarner Losh  * struct drm_file::magic and struct drm_magic_entry::priv.
182*592ffb21SWarner Losh  */
drm_authmagic(struct drm_device * dev,void * data,struct drm_file * file_priv)183*592ffb21SWarner Losh int drm_authmagic(struct drm_device *dev, void *data,
184*592ffb21SWarner Losh 		  struct drm_file *file_priv)
185*592ffb21SWarner Losh {
186*592ffb21SWarner Losh 	struct drm_auth *auth = data;
187*592ffb21SWarner Losh 	struct drm_file *file;
188*592ffb21SWarner Losh 
189*592ffb21SWarner Losh 	DRM_DEBUG("%u\n", auth->magic);
190*592ffb21SWarner Losh 	if ((file = drm_find_file(file_priv->master, auth->magic))) {
191*592ffb21SWarner Losh 		file->authenticated = 1;
192*592ffb21SWarner Losh 		drm_remove_magic(file_priv->master, auth->magic);
193*592ffb21SWarner Losh 		return 0;
194*592ffb21SWarner Losh 	}
195*592ffb21SWarner Losh 	return -EINVAL;
196*592ffb21SWarner Losh }
197*592ffb21SWarner Losh 
198*592ffb21SWarner Losh static int
drm_magic_init(void * arg)199*592ffb21SWarner Losh drm_magic_init(void *arg)
200*592ffb21SWarner Losh {
201*592ffb21SWarner Losh 
202*592ffb21SWarner Losh 	mtx_init(&drm_magic_lock, "drm_getmagic__lock", NULL, MTX_DEF);
203*592ffb21SWarner Losh 	return (0);
204*592ffb21SWarner Losh }
205*592ffb21SWarner Losh 
206*592ffb21SWarner Losh static void
drm_magic_fini(void * arg)207*592ffb21SWarner Losh drm_magic_fini(void *arg)
208*592ffb21SWarner Losh {
209*592ffb21SWarner Losh 
210*592ffb21SWarner Losh 	mtx_destroy(&drm_magic_lock);
211*592ffb21SWarner Losh }
212*592ffb21SWarner Losh 
213*592ffb21SWarner Losh SYSINIT(drm_magic_init, SI_SUB_KLD, SI_ORDER_MIDDLE, drm_magic_init, NULL);
214*592ffb21SWarner Losh SYSUNINIT(drm_magic_fini, SI_SUB_KLD, SI_ORDER_MIDDLE, drm_magic_fini, NULL);
215