drm_auth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Created: Tue Feb 2 08:37:54 1999 by [email protected]
  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 <[email protected]>
  9. * Author Gareth Hughes <[email protected]>
  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/slab.h>
  31. #include <drm/drm_auth.h>
  32. #include <drm/drm_drv.h>
  33. #include <drm/drm_file.h>
  34. #include <drm/drm_lease.h>
  35. #include <drm/drm_print.h>
  36. #include "drm_internal.h"
  37. #include "drm_legacy.h"
  38. /**
  39. * DOC: master and authentication
  40. *
  41. * &struct drm_master is used to track groups of clients with open
  42. * primary/legacy 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. mutex_lock(&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. mutex_unlock(&dev->master_mutex);
  97. drm_dbg_core(dev, "%u\n", auth->magic);
  98. return ret < 0 ? ret : 0;
  99. }
  100. int drm_authmagic(struct drm_device *dev, void *data,
  101. struct drm_file *file_priv)
  102. {
  103. struct drm_auth *auth = data;
  104. struct drm_file *file;
  105. drm_dbg_core(dev, "%u\n", auth->magic);
  106. mutex_lock(&dev->master_mutex);
  107. file = idr_find(&file_priv->master->magic_map, auth->magic);
  108. if (file) {
  109. file->authenticated = 1;
  110. idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
  111. }
  112. mutex_unlock(&dev->master_mutex);
  113. return file ? 0 : -EINVAL;
  114. }
  115. struct drm_master *drm_master_create(struct drm_device *dev)
  116. {
  117. struct drm_master *master;
  118. master = kzalloc(sizeof(*master), GFP_KERNEL);
  119. if (!master)
  120. return NULL;
  121. kref_init(&master->refcount);
  122. drm_master_legacy_init(master);
  123. idr_init_base(&master->magic_map, 1);
  124. master->dev = dev;
  125. /* initialize the tree of output resource lessees */
  126. INIT_LIST_HEAD(&master->lessees);
  127. INIT_LIST_HEAD(&master->lessee_list);
  128. idr_init(&master->leases);
  129. idr_init_base(&master->lessee_idr, 1);
  130. return master;
  131. }
  132. static void drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
  133. bool new_master)
  134. {
  135. dev->master = drm_master_get(fpriv->master);
  136. if (dev->driver->master_set)
  137. dev->driver->master_set(dev, fpriv, new_master);
  138. fpriv->was_master = true;
  139. }
  140. static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
  141. {
  142. struct drm_master *old_master;
  143. struct drm_master *new_master;
  144. lockdep_assert_held_once(&dev->master_mutex);
  145. WARN_ON(fpriv->is_master);
  146. old_master = fpriv->master;
  147. new_master = drm_master_create(dev);
  148. if (!new_master)
  149. return -ENOMEM;
  150. spin_lock(&fpriv->master_lookup_lock);
  151. fpriv->master = new_master;
  152. spin_unlock(&fpriv->master_lookup_lock);
  153. fpriv->is_master = 1;
  154. fpriv->authenticated = 1;
  155. drm_set_master(dev, fpriv, true);
  156. if (old_master)
  157. drm_master_put(&old_master);
  158. return 0;
  159. }
  160. /*
  161. * In the olden days the SET/DROP_MASTER ioctls used to return EACCES when
  162. * CAP_SYS_ADMIN was not set. This was used to prevent rogue applications
  163. * from becoming master and/or failing to release it.
  164. *
  165. * At the same time, the first client (for a given VT) is _always_ master.
  166. * Thus in order for the ioctls to succeed, one had to _explicitly_ run the
  167. * application as root or flip the setuid bit.
  168. *
  169. * If the CAP_SYS_ADMIN was missing, no other client could become master...
  170. * EVER :-( Leading to a) the graphics session dying badly or b) a completely
  171. * locked session.
  172. *
  173. *
  174. * As some point systemd-logind was introduced to orchestrate and delegate
  175. * master as applicable. It does so by opening the fd and passing it to users
  176. * while in itself logind a) does the set/drop master per users' request and
  177. * b) * implicitly drops master on VT switch.
  178. *
  179. * Even though logind looks like the future, there are a few issues:
  180. * - some platforms don't have equivalent (Android, CrOS, some BSDs) so
  181. * root is required _solely_ for SET/DROP MASTER.
  182. * - applications may not be updated to use it,
  183. * - any client which fails to drop master* can DoS the application using
  184. * logind, to a varying degree.
  185. *
  186. * * Either due missing CAP_SYS_ADMIN or simply not calling DROP_MASTER.
  187. *
  188. *
  189. * Here we implement the next best thing:
  190. * - ensure the logind style of fd passing works unchanged, and
  191. * - allow a client to drop/set master, iff it is/was master at a given point
  192. * in time.
  193. *
  194. * Note: DROP_MASTER cannot be free for all, as an arbitrator user could:
  195. * - DoS/crash the arbitrator - details would be implementation specific
  196. * - open the node, become master implicitly and cause issues
  197. *
  198. * As a result this fixes the following when using root-less build w/o logind
  199. * - startx
  200. * - weston
  201. * - various compositors based on wlroots
  202. */
  203. static int
  204. drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
  205. {
  206. if (file_priv->pid == task_pid(current) && file_priv->was_master)
  207. return 0;
  208. if (!capable(CAP_SYS_ADMIN))
  209. return -EACCES;
  210. return 0;
  211. }
  212. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  213. struct drm_file *file_priv)
  214. {
  215. int ret;
  216. mutex_lock(&dev->master_mutex);
  217. ret = drm_master_check_perm(dev, file_priv);
  218. if (ret)
  219. goto out_unlock;
  220. if (drm_is_current_master_locked(file_priv))
  221. goto out_unlock;
  222. if (dev->master) {
  223. ret = -EBUSY;
  224. goto out_unlock;
  225. }
  226. if (!file_priv->master) {
  227. ret = -EINVAL;
  228. goto out_unlock;
  229. }
  230. if (!file_priv->is_master) {
  231. ret = drm_new_set_master(dev, file_priv);
  232. goto out_unlock;
  233. }
  234. if (file_priv->master->lessor != NULL) {
  235. drm_dbg_lease(dev,
  236. "Attempt to set lessee %d as master\n",
  237. file_priv->master->lessee_id);
  238. ret = -EINVAL;
  239. goto out_unlock;
  240. }
  241. drm_set_master(dev, file_priv, false);
  242. out_unlock:
  243. mutex_unlock(&dev->master_mutex);
  244. return ret;
  245. }
  246. static void drm_drop_master(struct drm_device *dev,
  247. struct drm_file *fpriv)
  248. {
  249. if (dev->driver->master_drop)
  250. dev->driver->master_drop(dev, fpriv);
  251. drm_master_put(&dev->master);
  252. }
  253. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  254. struct drm_file *file_priv)
  255. {
  256. int ret;
  257. mutex_lock(&dev->master_mutex);
  258. ret = drm_master_check_perm(dev, file_priv);
  259. if (ret)
  260. goto out_unlock;
  261. if (!drm_is_current_master_locked(file_priv)) {
  262. ret = -EINVAL;
  263. goto out_unlock;
  264. }
  265. if (!dev->master) {
  266. ret = -EINVAL;
  267. goto out_unlock;
  268. }
  269. if (file_priv->master->lessor != NULL) {
  270. drm_dbg_lease(dev,
  271. "Attempt to drop lessee %d as master\n",
  272. file_priv->master->lessee_id);
  273. ret = -EINVAL;
  274. goto out_unlock;
  275. }
  276. drm_drop_master(dev, file_priv);
  277. out_unlock:
  278. mutex_unlock(&dev->master_mutex);
  279. return ret;
  280. }
  281. int drm_master_open(struct drm_file *file_priv)
  282. {
  283. struct drm_device *dev = file_priv->minor->dev;
  284. int ret = 0;
  285. /* if there is no current master make this fd it, but do not create
  286. * any master object for render clients
  287. */
  288. mutex_lock(&dev->master_mutex);
  289. if (!dev->master) {
  290. ret = drm_new_set_master(dev, file_priv);
  291. } else {
  292. spin_lock(&file_priv->master_lookup_lock);
  293. file_priv->master = drm_master_get(dev->master);
  294. spin_unlock(&file_priv->master_lookup_lock);
  295. }
  296. mutex_unlock(&dev->master_mutex);
  297. return ret;
  298. }
  299. void drm_master_release(struct drm_file *file_priv)
  300. {
  301. struct drm_device *dev = file_priv->minor->dev;
  302. struct drm_master *master;
  303. mutex_lock(&dev->master_mutex);
  304. master = file_priv->master;
  305. if (file_priv->magic)
  306. idr_remove(&file_priv->master->magic_map, file_priv->magic);
  307. if (!drm_is_current_master_locked(file_priv))
  308. goto out;
  309. drm_legacy_lock_master_cleanup(dev, master);
  310. if (dev->master == file_priv->master)
  311. drm_drop_master(dev, file_priv);
  312. out:
  313. if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
  314. /* Revoke any leases held by this or lessees, but only if
  315. * this is the "real" master
  316. */
  317. drm_lease_revoke(master);
  318. }
  319. /* drop the master reference held by the file priv */
  320. if (file_priv->master)
  321. drm_master_put(&file_priv->master);
  322. mutex_unlock(&dev->master_mutex);
  323. }
  324. /**
  325. * drm_master_get - reference a master pointer
  326. * @master: &struct drm_master
  327. *
  328. * Increments the reference count of @master and returns a pointer to @master.
  329. */
  330. struct drm_master *drm_master_get(struct drm_master *master)
  331. {
  332. kref_get(&master->refcount);
  333. return master;
  334. }
  335. EXPORT_SYMBOL(drm_master_get);
  336. /**
  337. * drm_file_get_master - reference &drm_file.master of @file_priv
  338. * @file_priv: DRM file private
  339. *
  340. * Increments the reference count of @file_priv's &drm_file.master and returns
  341. * the &drm_file.master. If @file_priv has no &drm_file.master, returns NULL.
  342. *
  343. * Master pointers returned from this function should be unreferenced using
  344. * drm_master_put().
  345. */
  346. struct drm_master *drm_file_get_master(struct drm_file *file_priv)
  347. {
  348. struct drm_master *master = NULL;
  349. spin_lock(&file_priv->master_lookup_lock);
  350. if (!file_priv->master)
  351. goto unlock;
  352. master = drm_master_get(file_priv->master);
  353. unlock:
  354. spin_unlock(&file_priv->master_lookup_lock);
  355. return master;
  356. }
  357. EXPORT_SYMBOL(drm_file_get_master);
  358. static void drm_master_destroy(struct kref *kref)
  359. {
  360. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  361. struct drm_device *dev = master->dev;
  362. if (drm_core_check_feature(dev, DRIVER_MODESET))
  363. drm_lease_destroy(master);
  364. drm_legacy_master_rmmaps(dev, master);
  365. idr_destroy(&master->magic_map);
  366. idr_destroy(&master->leases);
  367. idr_destroy(&master->lessee_idr);
  368. kfree(master->unique);
  369. kfree(master);
  370. }
  371. /**
  372. * drm_master_put - unreference and clear a master pointer
  373. * @master: pointer to a pointer of &struct drm_master
  374. *
  375. * This decrements the &drm_master behind @master and sets it to NULL.
  376. */
  377. void drm_master_put(struct drm_master **master)
  378. {
  379. kref_put(&(*master)->refcount, drm_master_destroy);
  380. *master = NULL;
  381. }
  382. EXPORT_SYMBOL(drm_master_put);
  383. /* Used by drm_client and drm_fb_helper */
  384. bool drm_master_internal_acquire(struct drm_device *dev)
  385. {
  386. mutex_lock(&dev->master_mutex);
  387. if (dev->master) {
  388. mutex_unlock(&dev->master_mutex);
  389. return false;
  390. }
  391. return true;
  392. }
  393. EXPORT_SYMBOL(drm_master_internal_acquire);
  394. /* Used by drm_client and drm_fb_helper */
  395. void drm_master_internal_release(struct drm_device *dev)
  396. {
  397. mutex_unlock(&dev->master_mutex);
  398. }
  399. EXPORT_SYMBOL(drm_master_internal_release);