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