first set of __user annotations from kernel (Al Viro)
[platform/upstream/libdrm.git] / linux-core / drm_auth.c
1 /**
2  * \file drm_auth.h 
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 #define __NO_VERSION__
37 #include "drmP.h"
38
39 /**
40  * Generate a hash key from a magic.
41  *
42  * \param magic magic.
43  * \return hash key.
44  *
45  * The key is the modulus of the hash table size, #DRM_HASH_SIZE, which must be
46  * a power of 2.
47  */
48 static int DRM(hash_magic)(drm_magic_t magic)
49 {
50         return magic & (DRM_HASH_SIZE-1);
51 }
52
53 /**
54  * Find the file with the given magic number.
55  *
56  * \param dev DRM device.
57  * \param magic magic number.
58  *
59  * Searches in drm_device::magiclist within all files with the same hash key
60  * the one with matching magic number, while holding the drm_device::struct_sem
61  * lock.
62  */
63 static drm_file_t *DRM(find_file)(drm_device_t *dev, drm_magic_t magic)
64 {
65         drm_file_t        *retval = NULL;
66         drm_magic_entry_t *pt;
67         int               hash    = DRM(hash_magic)(magic);
68
69         down(&dev->struct_sem);
70         for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
71                 if (pt->magic == magic) {
72                         retval = pt->priv;
73                         break;
74                 }
75         }
76         up(&dev->struct_sem);
77         return retval;
78 }
79
80 /**
81  * Adds a magic number.
82  * 
83  * \param dev DRM device.
84  * \param priv file private data.
85  * \param magic magic number.
86  *
87  * Creates a drm_magic_entry structure and appends to the linked list
88  * associated the magic number hash key in drm_device::magiclist, while holding
89  * the drm_device::struct_sem lock.
90  */
91 int DRM(add_magic)(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
92 {
93         int               hash;
94         drm_magic_entry_t *entry;
95
96         DRM_DEBUG("%d\n", magic);
97
98         hash         = DRM(hash_magic)(magic);
99         entry        = DRM(alloc)(sizeof(*entry), DRM_MEM_MAGIC);
100         if (!entry) return -ENOMEM;
101         memset(entry, 0, sizeof(*entry));
102         entry->magic = magic;
103         entry->priv  = priv;
104         entry->next  = NULL;
105
106         down(&dev->struct_sem);
107         if (dev->magiclist[hash].tail) {
108                 dev->magiclist[hash].tail->next = entry;
109                 dev->magiclist[hash].tail       = entry;
110         } else {
111                 dev->magiclist[hash].head       = entry;
112                 dev->magiclist[hash].tail       = entry;
113         }
114         up(&dev->struct_sem);
115
116         return 0;
117 }
118
119 /**
120  * Remove a magic number.
121  * 
122  * \param dev DRM device.
123  * \param magic magic number.
124  *
125  * Searches and unlinks the entry in drm_device::magiclist with the magic
126  * number hash key, while holding the drm_device::struct_sem lock.
127  */
128 int DRM(remove_magic)(drm_device_t *dev, drm_magic_t magic)
129 {
130         drm_magic_entry_t *prev = NULL;
131         drm_magic_entry_t *pt;
132         int               hash;
133
134
135         DRM_DEBUG("%d\n", magic);
136         hash = DRM(hash_magic)(magic);
137
138         down(&dev->struct_sem);
139         for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
140                 if (pt->magic == magic) {
141                         if (dev->magiclist[hash].head == pt) {
142                                 dev->magiclist[hash].head = pt->next;
143                         }
144                         if (dev->magiclist[hash].tail == pt) {
145                                 dev->magiclist[hash].tail = prev;
146                         }
147                         if (prev) {
148                                 prev->next = pt->next;
149                         }
150                         up(&dev->struct_sem);
151                         return 0;
152                 }
153         }
154         up(&dev->struct_sem);
155
156         DRM(free)(pt, sizeof(*pt), DRM_MEM_MAGIC);
157
158         return -EINVAL;
159 }
160
161 /**
162  * Get a unique magic number (ioctl).
163  *
164  * \param inode device inode.
165  * \param filp file pointer.
166  * \param cmd command.
167  * \param arg pointer to a resulting drm_auth structure.
168  * \return zero on success, or a negative number on failure.
169  *
170  * If there is a magic number in drm_file::magic then use it, otherwise
171  * searches an unique non-zero magic number and add it associating it with \p
172  * filp.
173  */
174 int DRM(getmagic)(struct inode *inode, struct file *filp,
175                   unsigned int cmd, unsigned long arg)
176 {
177         static drm_magic_t sequence = 0;
178         static spinlock_t  lock     = SPIN_LOCK_UNLOCKED;
179         drm_file_t         *priv    = filp->private_data;
180         drm_device_t       *dev     = priv->dev;
181         drm_auth_t         auth;
182
183                                 /* Find unique magic */
184         if (priv->magic) {
185                 auth.magic = priv->magic;
186         } else {
187                 do {
188                         spin_lock(&lock);
189                         if (!sequence) ++sequence; /* reserve 0 */
190                         auth.magic = sequence++;
191                         spin_unlock(&lock);
192                 } while (DRM(find_file)(dev, auth.magic));
193                 priv->magic = auth.magic;
194                 DRM(add_magic)(dev, priv, auth.magic);
195         }
196
197         DRM_DEBUG("%u\n", auth.magic);
198         if (copy_to_user((drm_auth_t __user *)arg, &auth, sizeof(auth)))
199                 return -EFAULT;
200         return 0;
201 }
202
203 /**
204  * Authenticate with a magic.
205  *
206  * \param inode device inode.
207  * \param filp file pointer.
208  * \param cmd command.
209  * \param arg pointer to a drm_auth structure.
210  * \return zero if authentication successed, or a negative number otherwise.
211  *
212  * Checks if \p filp is associated with the magic number passed in \arg.
213  */
214 int DRM(authmagic)(struct inode *inode, struct file *filp,
215                    unsigned int cmd, unsigned long arg)
216 {
217         drm_file_t         *priv    = filp->private_data;
218         drm_device_t       *dev     = priv->dev;
219         drm_auth_t         auth;
220         drm_file_t         *file;
221
222         if (copy_from_user(&auth, (drm_auth_t __user *)arg, sizeof(auth)))
223                 return -EFAULT;
224         DRM_DEBUG("%u\n", auth.magic);
225         if ((file = DRM(find_file)(dev, auth.magic))) {
226                 file->authenticated = 1;
227                 DRM(remove_magic)(dev, auth.magic);
228                 return 0;
229         }
230         return -EINVAL;
231 }