drm_auth.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <drm/drmP.h>
  35. #include "drm_internal.h"
  36. struct drm_magic_entry {
  37. struct list_head head;
  38. struct drm_hash_item hash_item;
  39. struct drm_file *priv;
  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. mutex_lock(&dev->struct_mutex);
  58. if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
  59. pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
  60. retval = pt->priv;
  61. }
  62. mutex_unlock(&dev->struct_mutex);
  63. return retval;
  64. }
  65. /**
  66. * Adds a magic number.
  67. *
  68. * \param dev DRM device.
  69. * \param priv file private data.
  70. * \param magic magic number.
  71. *
  72. * Creates a drm_magic_entry structure and appends to the linked list
  73. * associated the magic number hash key in drm_device::magiclist, while holding
  74. * the drm_device::struct_mutex lock.
  75. */
  76. static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
  77. drm_magic_t magic)
  78. {
  79. struct drm_magic_entry *entry;
  80. struct drm_device *dev = master->minor->dev;
  81. DRM_DEBUG("%d\n", magic);
  82. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  83. if (!entry)
  84. return -ENOMEM;
  85. entry->priv = priv;
  86. entry->hash_item.key = (unsigned long)magic;
  87. mutex_lock(&dev->struct_mutex);
  88. drm_ht_insert_item(&master->magiclist, &entry->hash_item);
  89. list_add_tail(&entry->head, &master->magicfree);
  90. mutex_unlock(&dev->struct_mutex);
  91. return 0;
  92. }
  93. /**
  94. * Remove a magic number.
  95. *
  96. * \param dev DRM device.
  97. * \param magic magic number.
  98. *
  99. * Searches and unlinks the entry in drm_device::magiclist with the magic
  100. * number hash key, while holding the drm_device::struct_mutex lock.
  101. */
  102. int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
  103. {
  104. struct drm_magic_entry *pt;
  105. struct drm_hash_item *hash;
  106. struct drm_device *dev = master->minor->dev;
  107. DRM_DEBUG("%d\n", magic);
  108. mutex_lock(&dev->struct_mutex);
  109. if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
  110. mutex_unlock(&dev->struct_mutex);
  111. return -EINVAL;
  112. }
  113. pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
  114. drm_ht_remove_item(&master->magiclist, hash);
  115. list_del(&pt->head);
  116. mutex_unlock(&dev->struct_mutex);
  117. kfree(pt);
  118. return 0;
  119. }
  120. /**
  121. * Get a unique magic number (ioctl).
  122. *
  123. * \param inode device inode.
  124. * \param file_priv DRM file private.
  125. * \param cmd command.
  126. * \param arg pointer to a resulting drm_auth structure.
  127. * \return zero on success, or a negative number on failure.
  128. *
  129. * If there is a magic number in drm_file::magic then use it, otherwise
  130. * searches an unique non-zero magic number and add it associating it with \p
  131. * file_priv.
  132. * This ioctl needs protection by the drm_global_mutex, which protects
  133. * struct drm_file::magic and struct drm_magic_entry::priv.
  134. */
  135. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  136. {
  137. static drm_magic_t sequence = 0;
  138. static DEFINE_SPINLOCK(lock);
  139. struct drm_auth *auth = data;
  140. /* Find unique magic */
  141. if (file_priv->magic) {
  142. auth->magic = file_priv->magic;
  143. } else {
  144. do {
  145. spin_lock(&lock);
  146. if (!sequence)
  147. ++sequence; /* reserve 0 */
  148. auth->magic = sequence++;
  149. spin_unlock(&lock);
  150. } while (drm_find_file(file_priv->master, auth->magic));
  151. file_priv->magic = auth->magic;
  152. drm_add_magic(file_priv->master, file_priv, auth->magic);
  153. }
  154. DRM_DEBUG("%u\n", auth->magic);
  155. return 0;
  156. }
  157. /**
  158. * Authenticate with a magic.
  159. *
  160. * \param inode device inode.
  161. * \param file_priv DRM file private.
  162. * \param cmd command.
  163. * \param arg pointer to a drm_auth structure.
  164. * \return zero if authentication successed, or a negative number otherwise.
  165. *
  166. * Checks if \p file_priv is associated with the magic number passed in \arg.
  167. * This ioctl needs protection by the drm_global_mutex, which protects
  168. * struct drm_file::magic and struct drm_magic_entry::priv.
  169. */
  170. int drm_authmagic(struct drm_device *dev, void *data,
  171. struct drm_file *file_priv)
  172. {
  173. struct drm_auth *auth = data;
  174. struct drm_file *file;
  175. DRM_DEBUG("%u\n", auth->magic);
  176. if ((file = drm_find_file(file_priv->master, auth->magic))) {
  177. file->authenticated = 1;
  178. drm_remove_magic(file_priv->master, auth->magic);
  179. return 0;
  180. }
  181. return -EINVAL;
  182. }