xref: /dragonfly/sys/dev/drm/drm_auth.c (revision 65cc0652)
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 <drm/drmP.h>
37 #include "drm_internal.h"
38 
39 /**
40  * Find the file with the given magic number.
41  *
42  * \param dev DRM device.
43  * \param magic magic number.
44  *
45  * Searches in drm_device::magiclist within all files with the same hash key
46  * the one with matching magic number, while holding the drm_device::struct_mutex
47  * lock.
48  */
49 static struct drm_file *drm_find_file(struct drm_device *dev, drm_magic_t magic)
50 {
51 	struct drm_file *retval = NULL;
52 	struct drm_magic_entry *pt;
53 	struct drm_hash_item *hash;
54 
55 	mutex_lock(&dev->struct_mutex);
56 	if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
57 		pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
58 		retval = pt->priv;
59 	}
60 	mutex_unlock(&dev->struct_mutex);
61 	return retval;
62 }
63 
64 /**
65  * Inserts the given magic number into the hash table of used magic number
66  * lists.
67  */
68 static int drm_add_magic(struct drm_device *dev, struct drm_file *priv,
69 			 drm_magic_t magic)
70 {
71 	struct drm_magic_entry *entry;
72 
73 	DRM_DEBUG("%d\n", magic);
74 
75 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
76 	if (!entry)
77 		return -ENOMEM;
78 	entry->priv = priv;
79 	entry->hash_item.key = (unsigned long)magic;
80 	mutex_lock(&dev->struct_mutex);
81 	drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
82 	list_add_tail(&entry->head, &dev->magicfree);
83 	mutex_unlock(&dev->struct_mutex);
84 
85 	return 0;
86 }
87 
88 /**
89  * Removes the given magic number from the hash table of used magic number
90  * lists.
91  */
92 int drm_remove_magic(struct drm_device *dev, drm_magic_t magic)
93 {
94 	struct drm_magic_entry *pt;
95 	struct drm_hash_item *hash;
96 
97 	DRM_DEBUG("%d\n", magic);
98 
99 	mutex_lock(&dev->struct_mutex);
100 	if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
101 		mutex_unlock(&dev->struct_mutex);
102 		return -EINVAL;
103 	}
104 	pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
105 	drm_ht_remove_item(&dev->magiclist, hash);
106 	list_del(&pt->head);
107 	mutex_unlock(&dev->struct_mutex);
108 
109 	kfree(pt);
110 
111 	return 0;
112 }
113 
114 /**
115  * Get a unique magic number (ioctl).
116  *
117  * \param inode device inode.
118  * \param file_priv DRM file private.
119  * \param cmd command.
120  * \param arg pointer to a resulting drm_auth structure.
121  * \return zero on success, or a negative number on failure.
122  *
123  * If there is a magic number in drm_file::magic then use it, otherwise
124  * searches an unique non-zero magic number and add it associating it with \p
125  * file_priv.
126  * This ioctl needs protection by the drm_global_mutex, which protects
127  * struct drm_file::magic and struct drm_magic_entry::priv.
128  */
129 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
130 {
131 	static drm_magic_t sequence = 0;
132 	static struct spinlock lock = SPINLOCK_INITIALIZER(&lock, "drm_gm");
133 	struct drm_auth *auth = data;
134 
135 	/* Find unique magic */
136 	if (file_priv->magic) {
137 		auth->magic = file_priv->magic;
138 	} else {
139 		do {
140 			spin_lock(&lock);
141 			if (!sequence)
142 				++sequence;	/* reserve 0 */
143 			auth->magic = sequence++;
144 			spin_unlock(&lock);
145 		} while (drm_find_file(dev, auth->magic));
146 		file_priv->magic = auth->magic;
147 		drm_add_magic(dev, file_priv, auth->magic);
148 	}
149 
150 	DRM_DEBUG("%u\n", auth->magic);
151 
152 	return 0;
153 }
154 
155 /**
156  * Authenticate with a magic.
157  *
158  * \param inode device inode.
159  * \param file_priv DRM file private.
160  * \param cmd command.
161  * \param arg pointer to a drm_auth structure.
162  * \return zero if authentication successed, or a negative number otherwise.
163  *
164  * Checks if \p file_priv is associated with the magic number passed in \arg.
165  * This ioctl needs protection by the drm_global_mutex, which protects
166  * struct drm_file::magic and struct drm_magic_entry::priv.
167  */
168 int drm_authmagic(struct drm_device *dev, void *data,
169 		  struct drm_file *file_priv)
170 {
171 	struct drm_auth *auth = data;
172 	struct drm_file *file;
173 
174 	DRM_DEBUG("%u\n", auth->magic);
175 	if ((file = drm_find_file(dev, auth->magic))) {
176 		file->authenticated = 1;
177 		drm_remove_magic(dev, auth->magic);
178 		return 0;
179 	}
180 	return -EINVAL;
181 }
182