drm_auth.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Author Rickard E. (Rik) Faith <faith@valinux.com>
  9. * Author Gareth Hughes <gareth@valinux.com>
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the next
  19. * paragraph) shall be included in all copies or substantial portions of the
  20. * Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  26. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  27. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. * OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. #include <drm/drmP.h>
  31. #include "drm_internal.h"
  32. #include "drm_legacy.h"
  33. /**
  34. * DOC: master and authentication
  35. *
  36. * struct &drm_master is used to track groups of clients with open
  37. * primary/legacy device nodes. For every struct &drm_file which has had at
  38. * least once successfully became the device master (either through the
  39. * SET_MASTER IOCTL, or implicitly through opening the primary device node when
  40. * no one else is the current master that time) there exists one &drm_master.
  41. * This is noted in the is_master member of &drm_file. All other clients have
  42. * just a pointer to the &drm_master they are associated with.
  43. *
  44. * In addition only one &drm_master can be the current master for a &drm_device.
  45. * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
  46. * implicitly through closing/openeing the primary device node. See also
  47. * drm_is_current_master().
  48. *
  49. * Clients can authenticate against the current master (if it matches their own)
  50. * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
  51. * this allows controlled access to the device for an entire group of mutually
  52. * trusted clients.
  53. */
  54. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  55. {
  56. struct drm_auth *auth = data;
  57. int ret = 0;
  58. mutex_lock(&dev->master_mutex);
  59. if (!file_priv->magic) {
  60. ret = idr_alloc(&file_priv->master->magic_map, file_priv,
  61. 1, 0, GFP_KERNEL);
  62. if (ret >= 0)
  63. file_priv->magic = ret;
  64. }
  65. auth->magic = file_priv->magic;
  66. mutex_unlock(&dev->master_mutex);
  67. DRM_DEBUG("%u\n", auth->magic);
  68. return ret < 0 ? ret : 0;
  69. }
  70. int drm_authmagic(struct drm_device *dev, void *data,
  71. struct drm_file *file_priv)
  72. {
  73. struct drm_auth *auth = data;
  74. struct drm_file *file;
  75. DRM_DEBUG("%u\n", auth->magic);
  76. mutex_lock(&dev->master_mutex);
  77. file = idr_find(&file_priv->master->magic_map, auth->magic);
  78. if (file) {
  79. file->authenticated = 1;
  80. idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
  81. }
  82. mutex_unlock(&dev->master_mutex);
  83. return file ? 0 : -EINVAL;
  84. }
  85. static struct drm_master *drm_master_create(struct drm_device *dev)
  86. {
  87. struct drm_master *master;
  88. master = kzalloc(sizeof(*master), GFP_KERNEL);
  89. if (!master)
  90. return NULL;
  91. kref_init(&master->refcount);
  92. spin_lock_init(&master->lock.spinlock);
  93. init_waitqueue_head(&master->lock.lock_queue);
  94. idr_init(&master->magic_map);
  95. master->dev = dev;
  96. return master;
  97. }
  98. static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
  99. bool new_master)
  100. {
  101. int ret = 0;
  102. dev->master = drm_master_get(fpriv->master);
  103. if (dev->driver->master_set) {
  104. ret = dev->driver->master_set(dev, fpriv, new_master);
  105. if (unlikely(ret != 0)) {
  106. drm_master_put(&dev->master);
  107. }
  108. }
  109. return ret;
  110. }
  111. static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  112. {
  113. struct drm_master *old_master;
  114. int ret;
  115. lockdep_assert_held_once(&dev->master_mutex);
  116. old_master = fpriv->master;
  117. fpriv->master = drm_master_create(dev);
  118. if (!fpriv->master) {
  119. fpriv->master = old_master;
  120. return -ENOMEM;
  121. }
  122. if (dev->driver->master_create) {
  123. ret = dev->driver->master_create(dev, fpriv->master);
  124. if (ret)
  125. goto out_err;
  126. }
  127. fpriv->is_master = 1;
  128. fpriv->authenticated = 1;
  129. ret = drm_set_master(dev, fpriv, true);
  130. if (ret)
  131. goto out_err;
  132. if (old_master)
  133. drm_master_put(&old_master);
  134. return 0;
  135. out_err:
  136. /* drop references and restore old master on failure */
  137. drm_master_put(&fpriv->master);
  138. fpriv->master = old_master;
  139. return ret;
  140. }
  141. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  142. struct drm_file *file_priv)
  143. {
  144. int ret = 0;
  145. mutex_lock(&dev->master_mutex);
  146. if (drm_is_current_master(file_priv))
  147. goto out_unlock;
  148. if (dev->master) {
  149. ret = -EINVAL;
  150. goto out_unlock;
  151. }
  152. if (!file_priv->master) {
  153. ret = -EINVAL;
  154. goto out_unlock;
  155. }
  156. if (!file_priv->is_master) {
  157. ret = drm_new_set_master(dev, file_priv);
  158. goto out_unlock;
  159. }
  160. ret = drm_set_master(dev, file_priv, false);
  161. out_unlock:
  162. mutex_unlock(&dev->master_mutex);
  163. return ret;
  164. }
  165. static void drm_drop_master(struct drm_device *dev,
  166. struct drm_file *fpriv)
  167. {
  168. if (dev->driver->master_drop)
  169. dev->driver->master_drop(dev, fpriv);
  170. drm_master_put(&dev->master);
  171. }
  172. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  173. struct drm_file *file_priv)
  174. {
  175. int ret = -EINVAL;
  176. mutex_lock(&dev->master_mutex);
  177. if (!drm_is_current_master(file_priv))
  178. goto out_unlock;
  179. if (!dev->master)
  180. goto out_unlock;
  181. ret = 0;
  182. drm_drop_master(dev, file_priv);
  183. out_unlock:
  184. mutex_unlock(&dev->master_mutex);
  185. return ret;
  186. }
  187. int drm_master_open(struct drm_file *file_priv)
  188. {
  189. struct drm_device *dev = file_priv->minor->dev;
  190. int ret = 0;
  191. /* if there is no current master make this fd it, but do not create
  192. * any master object for render clients */
  193. mutex_lock(&dev->master_mutex);
  194. if (!dev->master)
  195. ret = drm_new_set_master(dev, file_priv);
  196. else
  197. file_priv->master = drm_master_get(dev->master);
  198. mutex_unlock(&dev->master_mutex);
  199. return ret;
  200. }
  201. void drm_master_release(struct drm_file *file_priv)
  202. {
  203. struct drm_device *dev = file_priv->minor->dev;
  204. struct drm_master *master = file_priv->master;
  205. mutex_lock(&dev->master_mutex);
  206. if (file_priv->magic)
  207. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  208. if (!drm_is_current_master(file_priv))
  209. goto out;
  210. if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
  211. /*
  212. * Since the master is disappearing, so is the
  213. * possibility to lock.
  214. */
  215. mutex_lock(&dev->struct_mutex);
  216. if (master->lock.hw_lock) {
  217. if (dev->sigdata.lock == master->lock.hw_lock)
  218. dev->sigdata.lock = NULL;
  219. master->lock.hw_lock = NULL;
  220. master->lock.file_priv = NULL;
  221. wake_up_interruptible_all(&master->lock.lock_queue);
  222. }
  223. mutex_unlock(&dev->struct_mutex);
  224. }
  225. if (dev->master == file_priv->master)
  226. drm_drop_master(dev, file_priv);
  227. out:
  228. /* drop the master reference held by the file priv */
  229. if (file_priv->master)
  230. drm_master_put(&file_priv->master);
  231. mutex_unlock(&dev->master_mutex);
  232. }
  233. /**
  234. * drm_is_current_master - checks whether @priv is the current master
  235. * @fpriv: DRM file private
  236. *
  237. * Checks whether @fpriv is current master on its device. This decides whether a
  238. * client is allowed to run DRM_MASTER IOCTLs.
  239. *
  240. * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
  241. * - the current master is assumed to own the non-shareable display hardware.
  242. */
  243. bool drm_is_current_master(struct drm_file *fpriv)
  244. {
  245. return fpriv->is_master && fpriv->master == fpriv->minor->dev->master;
  246. }
  247. EXPORT_SYMBOL(drm_is_current_master);
  248. /**
  249. * drm_master_get - reference a master pointer
  250. * @master: struct &drm_master
  251. *
  252. * Increments the reference count of @master and returns a pointer to @master.
  253. */
  254. struct drm_master *drm_master_get(struct drm_master *master)
  255. {
  256. kref_get(&master->refcount);
  257. return master;
  258. }
  259. EXPORT_SYMBOL(drm_master_get);
  260. static void drm_master_destroy(struct kref *kref)
  261. {
  262. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  263. struct drm_device *dev = master->dev;
  264. if (dev->driver->master_destroy)
  265. dev->driver->master_destroy(dev, master);
  266. drm_legacy_master_rmmaps(dev, master);
  267. idr_destroy(&master->magic_map);
  268. kfree(master->unique);
  269. kfree(master);
  270. }
  271. /**
  272. * drm_master_put - unreference and clear a master pointer
  273. * @master: pointer to a pointer of struct &drm_master
  274. *
  275. * This decrements the &drm_master behind @master and sets it to NULL.
  276. */
  277. void drm_master_put(struct drm_master **master)
  278. {
  279. kref_put(&(*master)->refcount, drm_master_destroy);
  280. *master = NULL;
  281. }
  282. EXPORT_SYMBOL(drm_master_put);