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