drm_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 <linux/export.h>
  31. #include <linux/slab.h>
  32. #include <drm/drm_auth.h>
  33. #include <drm/drm_drv.h>
  34. #include <drm/drm_file.h>
  35. #include <drm/drm_lease.h>
  36. #include <drm/drm_print.h>
  37. #include "drm_internal.h"
  38. /**
  39. * DOC: master and authentication
  40. *
  41. * &struct drm_master is used to track groups of clients with open
  42. * primary device nodes. For every &struct drm_file which has had at
  43. * least once successfully became the device master (either through the
  44. * SET_MASTER IOCTL, or implicitly through opening the primary device node when
  45. * no one else is the current master that time) there exists one &drm_master.
  46. * This is noted in &drm_file.is_master. All other clients have just a pointer
  47. * to the &drm_master they are associated with.
  48. *
  49. * In addition only one &drm_master can be the current master for a &drm_device.
  50. * It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
  51. * implicitly through closing/opening the primary device node. See also
  52. * drm_is_current_master().
  53. *
  54. * Clients can authenticate against the current master (if it matches their own)
  55. * using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
  56. * this allows controlled access to the device for an entire group of mutually
  57. * trusted clients.
  58. */
  59. static bool drm_is_current_master_locked(struct drm_file *fpriv)
  60. {
  61. lockdep_assert_once(lockdep_is_held(&fpriv->master_lookup_lock) ||
  62. lockdep_is_held(&fpriv->minor->dev->master_mutex));
  63. return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
  64. }
  65. /**
  66. * drm_is_current_master - checks whether @priv is the current master
  67. * @fpriv: DRM file private
  68. *
  69. * Checks whether @fpriv is current master on its device. This decides whether a
  70. * client is allowed to run DRM_MASTER IOCTLs.
  71. *
  72. * Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
  73. * - the current master is assumed to own the non-shareable display hardware.
  74. */
  75. bool drm_is_current_master(struct drm_file *fpriv)
  76. {
  77. bool ret;
  78. spin_lock(&fpriv->master_lookup_lock);
  79. ret = drm_is_current_master_locked(fpriv);
  80. spin_unlock(&fpriv->master_lookup_lock);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL(drm_is_current_master);
  84. int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
  85. {
  86. struct drm_auth *auth = data;
  87. int ret = 0;
  88. guard(mutex)(&dev->master_mutex);
  89. if (!file_priv->magic) {
  90. ret = idr_alloc(&file_priv->master->magic_map, file_priv,
  91. 1, 0, GFP_KERNEL);
  92. if (ret >= 0)
  93. file_priv->magic = ret;
  94. }
  95. auth->magic = file_priv->magic;
  96. drm_dbg_core(dev, "%u\n", auth->magic);
  97. return ret < 0 ? ret : 0;
  98. }
  99. int drm_authmagic(struct drm_device *dev, void *data,
  100. struct drm_file *file_priv)
  101. {
  102. struct drm_auth *auth = data;
  103. struct drm_file *file;
  104. drm_dbg_core(dev, "%u\n", auth->magic);
  105. guard(mutex)(&dev->master_mutex);
  106. file = idr_find(&file_priv->master->magic_map, auth->magic);
  107. if (file) {
  108. file->authenticated = 1;
  109. idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
  110. }
  111. return file ? 0 : -EINVAL;
  112. }
  113. struct drm_master *drm_master_create(struct drm_device *dev)
  114. {
  115. struct drm_master *master;
  116. master = kzalloc_obj(*master);
  117. if (!master)
  118. return NULL;
  119. kref_init(&master->refcount);
  120. idr_init_base(&master->magic_map, 1);
  121. master->dev = dev;
  122. /* initialize the tree of output resource lessees */
  123. INIT_LIST_HEAD(&master->lessees);
  124. INIT_LIST_HEAD(&master->lessee_list);
  125. idr_init(&master->leases);
  126. idr_init_base(&master->lessee_idr, 1);
  127. return master;
  128. }
  129. static void drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
  130. bool new_master)
  131. {
  132. dev->master = drm_master_get(fpriv->master);
  133. if (dev->driver->master_set)
  134. dev->driver->master_set(dev, fpriv, new_master);
  135. fpriv->was_master = true;
  136. }
  137. static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  138. {
  139. struct drm_master *old_master;
  140. struct drm_master *new_master;
  141. lockdep_assert_held_once(&dev->master_mutex);
  142. WARN_ON(fpriv->is_master);
  143. old_master = fpriv->master;
  144. new_master = drm_master_create(dev);
  145. if (!new_master)
  146. return -ENOMEM;
  147. spin_lock(&fpriv->master_lookup_lock);
  148. fpriv->master = new_master;
  149. spin_unlock(&fpriv->master_lookup_lock);
  150. fpriv->is_master = 1;
  151. fpriv->authenticated = 1;
  152. drm_set_master(dev, fpriv, true);
  153. if (old_master)
  154. drm_master_put(&old_master);
  155. return 0;
  156. }
  157. /*
  158. * In the olden days the SET/DROP_MASTER ioctls used to return EACCES when
  159. * CAP_SYS_ADMIN was not set. This was used to prevent rogue applications
  160. * from becoming master and/or failing to release it.
  161. *
  162. * At the same time, the first client (for a given VT) is _always_ master.
  163. * Thus in order for the ioctls to succeed, one had to _explicitly_ run the
  164. * application as root or flip the setuid bit.
  165. *
  166. * If the CAP_SYS_ADMIN was missing, no other client could become master...
  167. * EVER :-( Leading to a) the graphics session dying badly or b) a completely
  168. * locked session.
  169. *
  170. *
  171. * As some point systemd-logind was introduced to orchestrate and delegate
  172. * master as applicable. It does so by opening the fd and passing it to users
  173. * while in itself logind a) does the set/drop master per users' request and
  174. * b) * implicitly drops master on VT switch.
  175. *
  176. * Even though logind looks like the future, there are a few issues:
  177. * - some platforms don't have equivalent (Android, CrOS, some BSDs) so
  178. * root is required _solely_ for SET/DROP MASTER.
  179. * - applications may not be updated to use it,
  180. * - any client which fails to drop master* can DoS the application using
  181. * logind, to a varying degree.
  182. *
  183. * * Either due missing CAP_SYS_ADMIN or simply not calling DROP_MASTER.
  184. *
  185. *
  186. * Here we implement the next best thing:
  187. * - ensure the logind style of fd passing works unchanged, and
  188. * - allow a client to drop/set master, iff it is/was master at a given point
  189. * in time.
  190. *
  191. * Note: DROP_MASTER cannot be free for all, as an arbitrator user could:
  192. * - DoS/crash the arbitrator - details would be implementation specific
  193. * - open the node, become master implicitly and cause issues
  194. *
  195. * As a result this fixes the following when using root-less build w/o logind
  196. * - startx
  197. * - weston
  198. * - various compositors based on wlroots
  199. */
  200. static int
  201. drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
  202. {
  203. if (file_priv->was_master &&
  204. rcu_access_pointer(file_priv->pid) == task_tgid(current))
  205. return 0;
  206. if (!capable(CAP_SYS_ADMIN))
  207. return -EACCES;
  208. return 0;
  209. }
  210. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  211. struct drm_file *file_priv)
  212. {
  213. int ret;
  214. guard(mutex)(&dev->master_mutex);
  215. ret = drm_master_check_perm(dev, file_priv);
  216. if (ret)
  217. return ret;
  218. if (drm_is_current_master_locked(file_priv))
  219. return ret;
  220. if (dev->master)
  221. return -EBUSY;
  222. if (!file_priv->master)
  223. return -EINVAL;
  224. if (!file_priv->is_master)
  225. return drm_new_set_master(dev, file_priv);
  226. if (file_priv->master->lessor != NULL) {
  227. drm_dbg_lease(dev,
  228. "Attempt to set lessee %d as master\n",
  229. file_priv->master->lessee_id);
  230. return -EINVAL;
  231. }
  232. drm_set_master(dev, file_priv, false);
  233. return ret;
  234. }
  235. static void drm_drop_master(struct drm_device *dev,
  236. struct drm_file *fpriv)
  237. {
  238. if (dev->driver->master_drop)
  239. dev->driver->master_drop(dev, fpriv);
  240. drm_master_put(&dev->master);
  241. }
  242. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  243. struct drm_file *file_priv)
  244. {
  245. int ret;
  246. guard(mutex)(&dev->master_mutex);
  247. ret = drm_master_check_perm(dev, file_priv);
  248. if (ret)
  249. return ret;
  250. if (!drm_is_current_master_locked(file_priv))
  251. return -EINVAL;
  252. if (!dev->master)
  253. return -EINVAL;
  254. if (file_priv->master->lessor != NULL) {
  255. drm_dbg_lease(dev,
  256. "Attempt to drop lessee %d as master\n",
  257. file_priv->master->lessee_id);
  258. return -EINVAL;
  259. }
  260. drm_drop_master(dev, file_priv);
  261. return ret;
  262. }
  263. int drm_master_open(struct drm_file *file_priv)
  264. {
  265. struct drm_device *dev = file_priv->minor->dev;
  266. int ret = 0;
  267. /* if there is no current master make this fd it, but do not create
  268. * any master object for render clients
  269. */
  270. guard(mutex)(&dev->master_mutex);
  271. if (!dev->master) {
  272. ret = drm_new_set_master(dev, file_priv);
  273. } else {
  274. spin_lock(&file_priv->master_lookup_lock);
  275. file_priv->master = drm_master_get(dev->master);
  276. spin_unlock(&file_priv->master_lookup_lock);
  277. }
  278. return ret;
  279. }
  280. void drm_master_release(struct drm_file *file_priv)
  281. {
  282. struct drm_device *dev = file_priv->minor->dev;
  283. struct drm_master *master;
  284. guard(mutex)(&dev->master_mutex);
  285. master = file_priv->master;
  286. if (file_priv->magic)
  287. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  288. if (!drm_is_current_master_locked(file_priv))
  289. goto out;
  290. if (dev->master == file_priv->master)
  291. drm_drop_master(dev, file_priv);
  292. out:
  293. if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
  294. /* Revoke any leases held by this or lessees, but only if
  295. * this is the "real" master
  296. */
  297. drm_lease_revoke(master);
  298. }
  299. /* drop the master reference held by the file priv */
  300. if (file_priv->master)
  301. drm_master_put(&file_priv->master);
  302. }
  303. /**
  304. * drm_master_get - reference a master pointer
  305. * @master: &struct drm_master
  306. *
  307. * Increments the reference count of @master and returns a pointer to @master.
  308. */
  309. struct drm_master *drm_master_get(struct drm_master *master)
  310. {
  311. kref_get(&master->refcount);
  312. return master;
  313. }
  314. EXPORT_SYMBOL(drm_master_get);
  315. /**
  316. * drm_file_get_master - reference &drm_file.master of @file_priv
  317. * @file_priv: DRM file private
  318. *
  319. * Increments the reference count of @file_priv's &drm_file.master and returns
  320. * the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
  321. *
  322. * Master pointers returned from this function should be unreferenced using
  323. * drm_master_put().
  324. */
  325. struct drm_master *drm_file_get_master(struct drm_file *file_priv)
  326. {
  327. struct drm_master *master = NULL;
  328. spin_lock(&file_priv->master_lookup_lock);
  329. if (!file_priv->master)
  330. goto unlock;
  331. master = drm_master_get(file_priv->master);
  332. unlock:
  333. spin_unlock(&file_priv->master_lookup_lock);
  334. return master;
  335. }
  336. EXPORT_SYMBOL(drm_file_get_master);
  337. static void drm_master_destroy(struct kref *kref)
  338. {
  339. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  340. struct drm_device *dev = master->dev;
  341. if (drm_core_check_feature(dev, DRIVER_MODESET))
  342. drm_lease_destroy(master);
  343. idr_destroy(&master->magic_map);
  344. idr_destroy(&master->leases);
  345. idr_destroy(&master->lessee_idr);
  346. kfree(master->unique);
  347. kfree(master);
  348. }
  349. /**
  350. * drm_master_put - unreference and clear a master pointer
  351. * @master: pointer to a pointer of &struct drm_master
  352. *
  353. * This decrements the &drm_master behind @master and sets it to NULL.
  354. */
  355. void drm_master_put(struct drm_master **master)
  356. {
  357. kref_put(&(*master)->refcount, drm_master_destroy);
  358. *master = NULL;
  359. }
  360. EXPORT_SYMBOL(drm_master_put);
  361. /* Used by drm_client and drm_fb_helper */
  362. bool drm_master_internal_acquire(struct drm_device *dev)
  363. {
  364. mutex_lock(&dev->master_mutex);
  365. if (dev->master) {
  366. mutex_unlock(&dev->master_mutex);
  367. return false;
  368. }
  369. return true;
  370. }
  371. EXPORT_SYMBOL(drm_master_internal_acquire);
  372. /* Used by drm_client and drm_fb_helper */
  373. void drm_master_internal_release(struct drm_device *dev)
  374. {
  375. mutex_unlock(&dev->master_mutex);
  376. }
  377. EXPORT_SYMBOL(drm_master_internal_release);