drm_auth.c 13 KB

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