drm_auth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #include <drm/drm_lease.h>
  34. /**
  35. * DOC: master and authentication
  36. *
  37. * &struct drm_master is used to track groups of clients with open
  38. * primary/legacy device nodes. For every &struct drm_file which has had at
  39. * least once successfully became the device master (either through the
  40. * SET_MASTER IOCTL, or implicitly through opening the primary device node when
  41. * no one else is the current master that time) there exists one &drm_master.
  42. * This is noted in &drm_file.is_master. All other clients have just a pointer
  43. * to the &drm_master they are associated with.
  44. *
  45. * In addition only one &drm_master can be the current master for a &drm_device.
  46. * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
  47. * implicitly through closing/openeing the primary device node. See also
  48. * drm_is_current_master().
  49. *
  50. * Clients can authenticate against the current master (if it matches their own)
  51. * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
  52. * this allows controlled access to the device for an entire group of mutually
  53. * trusted clients.
  54. */
  55. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  56. {
  57. struct drm_auth *auth = data;
  58. int ret = 0;
  59. mutex_lock(&dev->master_mutex);
  60. if (!file_priv->magic) {
  61. ret = idr_alloc(&file_priv->master->magic_map, file_priv,
  62. 1, 0, GFP_KERNEL);
  63. if (ret >= 0)
  64. file_priv->magic = ret;
  65. }
  66. auth->magic = file_priv->magic;
  67. mutex_unlock(&dev->master_mutex);
  68. DRM_DEBUG("%u\n", auth->magic);
  69. return ret < 0 ? ret : 0;
  70. }
  71. int drm_authmagic(struct drm_device *dev, void *data,
  72. struct drm_file *file_priv)
  73. {
  74. struct drm_auth *auth = data;
  75. struct drm_file *file;
  76. DRM_DEBUG("%u\n", auth->magic);
  77. mutex_lock(&dev->master_mutex);
  78. file = idr_find(&file_priv->master->magic_map, auth->magic);
  79. if (file) {
  80. file->authenticated = 1;
  81. idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
  82. }
  83. mutex_unlock(&dev->master_mutex);
  84. return file ? 0 : -EINVAL;
  85. }
  86. struct drm_master *drm_master_create(struct drm_device *dev)
  87. {
  88. struct drm_master *master;
  89. master = kzalloc(sizeof(*master), GFP_KERNEL);
  90. if (!master)
  91. return NULL;
  92. kref_init(&master->refcount);
  93. spin_lock_init(&master->lock.spinlock);
  94. init_waitqueue_head(&master->lock.lock_queue);
  95. idr_init(&master->magic_map);
  96. master->dev = dev;
  97. /* initialize the tree of output resource lessees */
  98. master->lessor = NULL;
  99. master->lessee_id = 0;
  100. INIT_LIST_HEAD(&master->lessees);
  101. INIT_LIST_HEAD(&master->lessee_list);
  102. idr_init(&master->leases);
  103. idr_init(&master->lessee_idr);
  104. return master;
  105. }
  106. static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
  107. bool new_master)
  108. {
  109. int ret = 0;
  110. dev->master = drm_master_get(fpriv->master);
  111. if (dev->driver->master_set) {
  112. ret = dev->driver->master_set(dev, fpriv, new_master);
  113. if (unlikely(ret != 0)) {
  114. drm_master_put(&dev->master);
  115. }
  116. }
  117. return ret;
  118. }
  119. static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  120. {
  121. struct drm_master *old_master;
  122. int ret;
  123. lockdep_assert_held_once(&dev->master_mutex);
  124. WARN_ON(fpriv->is_master);
  125. old_master = fpriv->master;
  126. fpriv->master = drm_master_create(dev);
  127. if (!fpriv->master) {
  128. fpriv->master = old_master;
  129. return -ENOMEM;
  130. }
  131. if (dev->driver->master_create) {
  132. ret = dev->driver->master_create(dev, fpriv->master);
  133. if (ret)
  134. goto out_err;
  135. }
  136. fpriv->is_master = 1;
  137. fpriv->authenticated = 1;
  138. ret = drm_set_master(dev, fpriv, true);
  139. if (ret)
  140. goto out_err;
  141. if (old_master)
  142. drm_master_put(&old_master);
  143. return 0;
  144. out_err:
  145. /* drop references and restore old master on failure */
  146. drm_master_put(&fpriv->master);
  147. fpriv->master = old_master;
  148. fpriv->is_master = 0;
  149. return ret;
  150. }
  151. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  152. struct drm_file *file_priv)
  153. {
  154. int ret = 0;
  155. mutex_lock(&dev->master_mutex);
  156. if (drm_is_current_master(file_priv))
  157. goto out_unlock;
  158. if (dev->master) {
  159. ret = -EINVAL;
  160. goto out_unlock;
  161. }
  162. if (!file_priv->master) {
  163. ret = -EINVAL;
  164. goto out_unlock;
  165. }
  166. if (!file_priv->is_master) {
  167. ret = drm_new_set_master(dev, file_priv);
  168. goto out_unlock;
  169. }
  170. if (file_priv->master->lessor != NULL) {
  171. DRM_DEBUG_LEASE("Attempt to set lessee %d as master\n", file_priv->master->lessee_id);
  172. ret = -EINVAL;
  173. goto out_unlock;
  174. }
  175. ret = drm_set_master(dev, file_priv, false);
  176. out_unlock:
  177. mutex_unlock(&dev->master_mutex);
  178. return ret;
  179. }
  180. static void drm_drop_master(struct drm_device *dev,
  181. struct drm_file *fpriv)
  182. {
  183. if (dev->driver->master_drop)
  184. dev->driver->master_drop(dev, fpriv);
  185. drm_master_put(&dev->master);
  186. }
  187. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  188. struct drm_file *file_priv)
  189. {
  190. int ret = -EINVAL;
  191. mutex_lock(&dev->master_mutex);
  192. if (!drm_is_current_master(file_priv))
  193. goto out_unlock;
  194. if (!dev->master)
  195. goto out_unlock;
  196. if (file_priv->master->lessor != NULL) {
  197. DRM_DEBUG_LEASE("Attempt to drop lessee %d as master\n", file_priv->master->lessee_id);
  198. ret = -EINVAL;
  199. goto out_unlock;
  200. }
  201. ret = 0;
  202. drm_drop_master(dev, file_priv);
  203. out_unlock:
  204. mutex_unlock(&dev->master_mutex);
  205. return ret;
  206. }
  207. int drm_master_open(struct drm_file *file_priv)
  208. {
  209. struct drm_device *dev = file_priv->minor->dev;
  210. int ret = 0;
  211. /* if there is no current master make this fd it, but do not create
  212. * any master object for render clients */
  213. mutex_lock(&dev->master_mutex);
  214. if (!dev->master)
  215. ret = drm_new_set_master(dev, file_priv);
  216. else
  217. file_priv->master = drm_master_get(dev->master);
  218. mutex_unlock(&dev->master_mutex);
  219. return ret;
  220. }
  221. void drm_master_release(struct drm_file *file_priv)
  222. {
  223. struct drm_device *dev = file_priv->minor->dev;
  224. struct drm_master *master = file_priv->master;
  225. mutex_lock(&dev->master_mutex);
  226. if (file_priv->magic)
  227. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  228. if (!drm_is_current_master(file_priv))
  229. goto out;
  230. if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
  231. /*
  232. * Since the master is disappearing, so is the
  233. * possibility to lock.
  234. */
  235. mutex_lock(&dev->struct_mutex);
  236. if (master->lock.hw_lock) {
  237. if (dev->sigdata.lock == master->lock.hw_lock)
  238. dev->sigdata.lock = NULL;
  239. master->lock.hw_lock = NULL;
  240. master->lock.file_priv = NULL;
  241. wake_up_interruptible_all(&master->lock.lock_queue);
  242. }
  243. mutex_unlock(&dev->struct_mutex);
  244. }
  245. if (dev->master == file_priv->master)
  246. drm_drop_master(dev, file_priv);
  247. out:
  248. if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
  249. /* Revoke any leases held by this or lessees, but only if
  250. * this is the "real" master
  251. */
  252. drm_lease_revoke(master);
  253. }
  254. /* drop the master reference held by the file priv */
  255. if (file_priv->master)
  256. drm_master_put(&file_priv->master);
  257. mutex_unlock(&dev->master_mutex);
  258. }
  259. /**
  260. * drm_is_current_master - checks whether @priv is the current master
  261. * @fpriv: DRM file private
  262. *
  263. * Checks whether @fpriv is current master on its device. This decides whether a
  264. * client is allowed to run DRM_MASTER IOCTLs.
  265. *
  266. * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
  267. * - the current master is assumed to own the non-shareable display hardware.
  268. */
  269. bool drm_is_current_master(struct drm_file *fpriv)
  270. {
  271. return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
  272. }
  273. EXPORT_SYMBOL(drm_is_current_master);
  274. /**
  275. * drm_master_get - reference a master pointer
  276. * @master: &struct drm_master
  277. *
  278. * Increments the reference count of @master and returns a pointer to @master.
  279. */
  280. struct drm_master *drm_master_get(struct drm_master *master)
  281. {
  282. kref_get(&master->refcount);
  283. return master;
  284. }
  285. EXPORT_SYMBOL(drm_master_get);
  286. static void drm_master_destroy(struct kref *kref)
  287. {
  288. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  289. struct drm_device *dev = master->dev;
  290. if (drm_core_check_feature(dev, DRIVER_MODESET))
  291. drm_lease_destroy(master);
  292. if (dev->driver->master_destroy)
  293. dev->driver->master_destroy(dev, master);
  294. drm_legacy_master_rmmaps(dev, master);
  295. idr_destroy(&master->magic_map);
  296. idr_destroy(&master->leases);
  297. idr_destroy(&master->lessee_idr);
  298. kfree(master->unique);
  299. kfree(master);
  300. }
  301. /**
  302. * drm_master_put - unreference and clear a master pointer
  303. * @master: pointer to a pointer of &struct drm_master
  304. *
  305. * This decrements the &drm_master behind @master and sets it to NULL.
  306. */
  307. void drm_master_put(struct drm_master **master)
  308. {
  309. kref_put(&(*master)->refcount, drm_master_destroy);
  310. *master = NULL;
  311. }
  312. EXPORT_SYMBOL(drm_master_put);