mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-23 09:19:51 +00:00
![Sergio Correia](/assets/img/avatar_default.png)
When drm_new_set_master() fails, set is_master to 0, to prevent a possible NULL pointer deref. Here is a problematic flow: we check is_master in drm_is_current_master(), then proceed to call drm_lease_owner() passing master. If we do not restore is_master status when drm_new_set_master() fails, we may have a situation in which is_master will be 1 and master itself, NULL, leading to the deref of a NULL pointer in drm_lease_owner(). This fixes the following OOPS, observed on an ArchLinux running a 4.19.2 kernel: [ 97.804282] BUG: unable to handle kernel NULL pointer dereference at 0000000000000080 [ 97.807224] PGD 0 P4D 0 [ 97.807224] Oops: 0000 [#1] PREEMPT SMP NOPTI [ 97.807224] CPU: 0 PID: 1348 Comm: xfwm4 Tainted: P OE 4.19.2-arch1-1-ARCH #1 [ 97.807224] Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./AB350 Pro4, BIOS P5.10 10/16/2018 [ 97.807224] RIP: 0010:drm_lease_owner+0xd/0x20 [drm] [ 97.807224] Code: 83 c4 18 5b 5d c3 b8 ea ff ff ff eb e2 b8 ed ff ff ff eb db e8 b4 ca 68 fb 0f 1f 40 00 0f 1f 44 00 00 48 89 f8 eb 03 48 89 d0 <48> 8b 90 80 00 00 00 48 85 d2 75 f1 c3 66 0f 1f 44 00 00 0f 1f 44 [ 97.807224] RSP: 0018:ffffb8cf08e07bb0 EFLAGS: 00010202 [ 97.807224] RAX: 0000000000000000 RBX: ffff9cf0f2586c00 RCX: ffff9cf0f2586c88 [ 97.807224] RDX: ffff9cf0ddbd8000 RSI: 0000000000000000 RDI: 0000000000000000 [ 97.807224] RBP: ffff9cf1040e9800 R08: 0000000000000000 R09: 0000000000000000 [ 97.807224] R10: ffffdeb30fd5d680 R11: ffffdeb30f5d6808 R12: ffff9cf1040e9888 [ 97.807224] R13: 0000000000000000 R14: dead000000000200 R15: ffff9cf0f2586cc8 [ 97.807224] FS: 00007f4145513180(0000) GS:ffff9cf10ea00000(0000) knlGS:0000000000000000 [ 97.807224] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 97.807224] CR2: 0000000000000080 CR3: 00000003d7548000 CR4: 00000000003406f0 [ 97.807224] Call Trace: [ 97.807224] drm_is_current_master+0x1a/0x30 [drm] [ 97.807224] drm_master_release+0x3e/0x130 [drm] [ 97.807224] drm_file_free.part.0+0x2be/0x2d0 [drm] [ 97.807224] drm_open+0x1ba/0x1e0 [drm] [ 97.807224] drm_stub_open+0xaf/0xe0 [drm] [ 97.807224] chrdev_open+0xa3/0x1b0 [ 97.807224] ? cdev_put.part.0+0x20/0x20 [ 97.807224] do_dentry_open+0x132/0x340 [ 97.807224] path_openat+0x2d1/0x14e0 [ 97.807224] ? mem_cgroup_commit_charge+0x7a/0x520 [ 97.807224] do_filp_open+0x93/0x100 [ 97.807224] ? __check_object_size+0x102/0x189 [ 97.807224] ? _raw_spin_unlock+0x16/0x30 [ 97.807224] do_sys_open+0x186/0x210 [ 97.807224] do_syscall_64+0x5b/0x170 [ 97.807224] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 97.807224] RIP: 0033:0x7f4147b07976 [ 97.807224] Code: 89 54 24 08 e8 7b f4 ff ff 8b 74 24 0c 48 8b 3c 24 41 89 c0 44 8b 54 24 08 b8 01 01 00 00 89 f2 48 89 fe bf 9c ff ff ff 0f 05 <48> 3d 00 f0 ff ff 77 30 44 89 c7 89 44 24 08 e8 a6 f4 ff ff 8b 44 [ 97.807224] RSP: 002b:00007ffcced96ca0 EFLAGS: 00000293 ORIG_RAX: 0000000000000101 [ 97.807224] RAX: ffffffffffffffda RBX: 00005619d5037f80 RCX: 00007f4147b07976 [ 97.807224] RDX: 0000000000000002 RSI: 00005619d46b969c RDI: 00000000ffffff9c [ 98.040039] RBP: 0000000000000024 R08: 0000000000000000 R09: 0000000000000000 [ 98.040039] R10: 0000000000000000 R11: 0000000000000293 R12: 0000000000000024 [ 98.040039] R13: 0000000000000012 R14: 00005619d5035950 R15: 0000000000000012 [ 98.040039] Modules linked in: nct6775 hwmon_vid algif_skcipher af_alg nls_iso8859_1 nls_cp437 vfat fat uvcvideo videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 videobuf2_common arc4 videodev media snd_usb_audio snd_hda_codec_hdmi snd_usbmidi_lib snd_rawmidi snd_seq_device mousedev input_leds iwlmvm mac80211 snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_codec edac_mce_amd kvm_amd snd_hda_core kvm iwlwifi snd_hwdep r8169 wmi_bmof cfg80211 snd_pcm irqbypass snd_timer snd libphy soundcore pinctrl_amd rfkill pcspkr sp5100_tco evdev gpio_amdpt k10temp mac_hid i2c_piix4 wmi pcc_cpufreq acpi_cpufreq vboxnetflt(OE) vboxnetadp(OE) vboxpci(OE) vboxdrv(OE) msr sg crypto_user ip_tables x_tables ext4 crc32c_generic crc16 mbcache jbd2 fscrypto uas usb_storage dm_crypt hid_generic usbhid hid [ 98.040039] dm_mod raid1 md_mod sd_mod crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel pcbc ahci libahci aesni_intel aes_x86_64 libata crypto_simd cryptd glue_helper ccp xhci_pci rng_core scsi_mod xhci_hcd nvidia_drm(POE) drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops drm agpgart nvidia_uvm(POE) nvidia_modeset(POE) nvidia(POE) ipmi_devintf ipmi_msghandler [ 98.040039] CR2: 0000000000000080 [ 98.040039] ---[ end trace 3b65093b6fe62b2f ]--- [ 98.040039] RIP: 0010:drm_lease_owner+0xd/0x20 [drm] [ 98.040039] Code: 83 c4 18 5b 5d c3 b8 ea ff ff ff eb e2 b8 ed ff ff ff eb db e8 b4 ca 68 fb 0f 1f 40 00 0f 1f 44 00 00 48 89 f8 eb 03 48 89 d0 <48> 8b 90 80 00 00 00 48 85 d2 75 f1 c3 66 0f 1f 44 00 00 0f 1f 44 [ 98.040039] RSP: 0018:ffffb8cf08e07bb0 EFLAGS: 00010202 [ 98.040039] RAX: 0000000000000000 RBX: ffff9cf0f2586c00 RCX: ffff9cf0f2586c88 [ 98.040039] RDX: ffff9cf0ddbd8000 RSI: 0000000000000000 RDI: 0000000000000000 [ 98.040039] RBP: ffff9cf1040e9800 R08: 0000000000000000 R09: 0000000000000000 [ 98.040039] R10: ffffdeb30fd5d680 R11: ffffdeb30f5d6808 R12: ffff9cf1040e9888 [ 98.040039] R13: 0000000000000000 R14: dead000000000200 R15: ffff9cf0f2586cc8 [ 98.040039] FS: 00007f4145513180(0000) GS:ffff9cf10ea00000(0000) knlGS:0000000000000000 [ 98.040039] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 98.040039] CR2: 0000000000000080 CR3: 00000003d7548000 CR4: 00000000003406f0 Signed-off-by: Sergio Correia <sergio@correia.cc> Cc: stable@vger.kernel.org Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: https://patchwork.freedesktop.org/patch/msgid/20181122053329.2692-1-sergio@correia.cc Signed-off-by: Sean Paul <seanpaul@chromium.org>
370 lines
9.8 KiB
C
370 lines
9.8 KiB
C
/*
|
|
* Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
|
|
*
|
|
* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
|
|
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
|
|
* All Rights Reserved.
|
|
*
|
|
* Author Rickard E. (Rik) Faith <faith@valinux.com>
|
|
* Author Gareth Hughes <gareth@valinux.com>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
* to deal in the Software without restriction, including without limitation
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice (including the next
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
* Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include <drm/drmP.h>
|
|
#include "drm_internal.h"
|
|
#include "drm_legacy.h"
|
|
#include <drm/drm_lease.h>
|
|
|
|
/**
|
|
* DOC: master and authentication
|
|
*
|
|
* &struct drm_master is used to track groups of clients with open
|
|
* primary/legacy device nodes. For every &struct drm_file which has had at
|
|
* least once successfully became the device master (either through the
|
|
* SET_MASTER IOCTL, or implicitly through opening the primary device node when
|
|
* no one else is the current master that time) there exists one &drm_master.
|
|
* This is noted in &drm_file.is_master. All other clients have just a pointer
|
|
* to the &drm_master they are associated with.
|
|
*
|
|
* In addition only one &drm_master can be the current master for a &drm_device.
|
|
* It can be switched through the DROP_MASTER and SET_MASTER IOCTL, or
|
|
* implicitly through closing/openeing the primary device node. See also
|
|
* drm_is_current_master().
|
|
*
|
|
* Clients can authenticate against the current master (if it matches their own)
|
|
* using the GETMAGIC and AUTHMAGIC IOCTLs. Together with exchanging masters,
|
|
* this allows controlled access to the device for an entire group of mutually
|
|
* trusted clients.
|
|
*/
|
|
|
|
int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
|
|
{
|
|
struct drm_auth *auth = data;
|
|
int ret = 0;
|
|
|
|
mutex_lock(&dev->master_mutex);
|
|
if (!file_priv->magic) {
|
|
ret = idr_alloc(&file_priv->master->magic_map, file_priv,
|
|
1, 0, GFP_KERNEL);
|
|
if (ret >= 0)
|
|
file_priv->magic = ret;
|
|
}
|
|
auth->magic = file_priv->magic;
|
|
mutex_unlock(&dev->master_mutex);
|
|
|
|
DRM_DEBUG("%u\n", auth->magic);
|
|
|
|
return ret < 0 ? ret : 0;
|
|
}
|
|
|
|
int drm_authmagic(struct drm_device *dev, void *data,
|
|
struct drm_file *file_priv)
|
|
{
|
|
struct drm_auth *auth = data;
|
|
struct drm_file *file;
|
|
|
|
DRM_DEBUG("%u\n", auth->magic);
|
|
|
|
mutex_lock(&dev->master_mutex);
|
|
file = idr_find(&file_priv->master->magic_map, auth->magic);
|
|
if (file) {
|
|
file->authenticated = 1;
|
|
idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
|
|
}
|
|
mutex_unlock(&dev->master_mutex);
|
|
|
|
return file ? 0 : -EINVAL;
|
|
}
|
|
|
|
struct drm_master *drm_master_create(struct drm_device *dev)
|
|
{
|
|
struct drm_master *master;
|
|
|
|
master = kzalloc(sizeof(*master), GFP_KERNEL);
|
|
if (!master)
|
|
return NULL;
|
|
|
|
kref_init(&master->refcount);
|
|
spin_lock_init(&master->lock.spinlock);
|
|
init_waitqueue_head(&master->lock.lock_queue);
|
|
idr_init(&master->magic_map);
|
|
master->dev = dev;
|
|
|
|
/* initialize the tree of output resource lessees */
|
|
master->lessor = NULL;
|
|
master->lessee_id = 0;
|
|
INIT_LIST_HEAD(&master->lessees);
|
|
INIT_LIST_HEAD(&master->lessee_list);
|
|
idr_init(&master->leases);
|
|
idr_init(&master->lessee_idr);
|
|
|
|
return master;
|
|
}
|
|
|
|
static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
|
|
bool new_master)
|
|
{
|
|
int ret = 0;
|
|
|
|
dev->master = drm_master_get(fpriv->master);
|
|
if (dev->driver->master_set) {
|
|
ret = dev->driver->master_set(dev, fpriv, new_master);
|
|
if (unlikely(ret != 0)) {
|
|
drm_master_put(&dev->master);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
|
|
{
|
|
struct drm_master *old_master;
|
|
int ret;
|
|
|
|
lockdep_assert_held_once(&dev->master_mutex);
|
|
|
|
WARN_ON(fpriv->is_master);
|
|
old_master = fpriv->master;
|
|
fpriv->master = drm_master_create(dev);
|
|
if (!fpriv->master) {
|
|
fpriv->master = old_master;
|
|
return -ENOMEM;
|
|
}
|
|
|
|
if (dev->driver->master_create) {
|
|
ret = dev->driver->master_create(dev, fpriv->master);
|
|
if (ret)
|
|
goto out_err;
|
|
}
|
|
fpriv->is_master = 1;
|
|
fpriv->authenticated = 1;
|
|
|
|
ret = drm_set_master(dev, fpriv, true);
|
|
if (ret)
|
|
goto out_err;
|
|
|
|
if (old_master)
|
|
drm_master_put(&old_master);
|
|
|
|
return 0;
|
|
|
|
out_err:
|
|
/* drop references and restore old master on failure */
|
|
drm_master_put(&fpriv->master);
|
|
fpriv->master = old_master;
|
|
fpriv->is_master = 0;
|
|
|
|
return ret;
|
|
}
|
|
|
|
int drm_setmaster_ioctl(struct drm_device *dev, void *data,
|
|
struct drm_file *file_priv)
|
|
{
|
|
int ret = 0;
|
|
|
|
mutex_lock(&dev->master_mutex);
|
|
if (drm_is_current_master(file_priv))
|
|
goto out_unlock;
|
|
|
|
if (dev->master) {
|
|
ret = -EINVAL;
|
|
goto out_unlock;
|
|
}
|
|
|
|
if (!file_priv->master) {
|
|
ret = -EINVAL;
|
|
goto out_unlock;
|
|
}
|
|
|
|
if (!file_priv->is_master) {
|
|
ret = drm_new_set_master(dev, file_priv);
|
|
goto out_unlock;
|
|
}
|
|
|
|
if (file_priv->master->lessor != NULL) {
|
|
DRM_DEBUG_LEASE("Attempt to set lessee %d as master\n", file_priv->master->lessee_id);
|
|
ret = -EINVAL;
|
|
goto out_unlock;
|
|
}
|
|
|
|
ret = drm_set_master(dev, file_priv, false);
|
|
out_unlock:
|
|
mutex_unlock(&dev->master_mutex);
|
|
return ret;
|
|
}
|
|
|
|
static void drm_drop_master(struct drm_device *dev,
|
|
struct drm_file *fpriv)
|
|
{
|
|
if (dev->driver->master_drop)
|
|
dev->driver->master_drop(dev, fpriv);
|
|
drm_master_put(&dev->master);
|
|
}
|
|
|
|
int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
|
|
struct drm_file *file_priv)
|
|
{
|
|
int ret = -EINVAL;
|
|
|
|
mutex_lock(&dev->master_mutex);
|
|
if (!drm_is_current_master(file_priv))
|
|
goto out_unlock;
|
|
|
|
if (!dev->master)
|
|
goto out_unlock;
|
|
|
|
if (file_priv->master->lessor != NULL) {
|
|
DRM_DEBUG_LEASE("Attempt to drop lessee %d as master\n", file_priv->master->lessee_id);
|
|
ret = -EINVAL;
|
|
goto out_unlock;
|
|
}
|
|
|
|
ret = 0;
|
|
drm_drop_master(dev, file_priv);
|
|
out_unlock:
|
|
mutex_unlock(&dev->master_mutex);
|
|
return ret;
|
|
}
|
|
|
|
int drm_master_open(struct drm_file *file_priv)
|
|
{
|
|
struct drm_device *dev = file_priv->minor->dev;
|
|
int ret = 0;
|
|
|
|
/* if there is no current master make this fd it, but do not create
|
|
* any master object for render clients */
|
|
mutex_lock(&dev->master_mutex);
|
|
if (!dev->master)
|
|
ret = drm_new_set_master(dev, file_priv);
|
|
else
|
|
file_priv->master = drm_master_get(dev->master);
|
|
mutex_unlock(&dev->master_mutex);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void drm_master_release(struct drm_file *file_priv)
|
|
{
|
|
struct drm_device *dev = file_priv->minor->dev;
|
|
struct drm_master *master = file_priv->master;
|
|
|
|
mutex_lock(&dev->master_mutex);
|
|
if (file_priv->magic)
|
|
idr_remove(&file_priv->master->magic_map, file_priv->magic);
|
|
|
|
if (!drm_is_current_master(file_priv))
|
|
goto out;
|
|
|
|
if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
|
|
/*
|
|
* Since the master is disappearing, so is the
|
|
* possibility to lock.
|
|
*/
|
|
mutex_lock(&dev->struct_mutex);
|
|
if (master->lock.hw_lock) {
|
|
if (dev->sigdata.lock == master->lock.hw_lock)
|
|
dev->sigdata.lock = NULL;
|
|
master->lock.hw_lock = NULL;
|
|
master->lock.file_priv = NULL;
|
|
wake_up_interruptible_all(&master->lock.lock_queue);
|
|
}
|
|
mutex_unlock(&dev->struct_mutex);
|
|
}
|
|
|
|
if (dev->master == file_priv->master)
|
|
drm_drop_master(dev, file_priv);
|
|
out:
|
|
if (drm_core_check_feature(dev, DRIVER_MODESET) && file_priv->is_master) {
|
|
/* Revoke any leases held by this or lessees, but only if
|
|
* this is the "real" master
|
|
*/
|
|
drm_lease_revoke(master);
|
|
}
|
|
|
|
/* drop the master reference held by the file priv */
|
|
if (file_priv->master)
|
|
drm_master_put(&file_priv->master);
|
|
mutex_unlock(&dev->master_mutex);
|
|
}
|
|
|
|
/**
|
|
* drm_is_current_master - checks whether @priv is the current master
|
|
* @fpriv: DRM file private
|
|
*
|
|
* Checks whether @fpriv is current master on its device. This decides whether a
|
|
* client is allowed to run DRM_MASTER IOCTLs.
|
|
*
|
|
* Most of the modern IOCTL which require DRM_MASTER are for kernel modesetting
|
|
* - the current master is assumed to own the non-shareable display hardware.
|
|
*/
|
|
bool drm_is_current_master(struct drm_file *fpriv)
|
|
{
|
|
return fpriv->is_master && drm_lease_owner(fpriv->master) == fpriv->minor->dev->master;
|
|
}
|
|
EXPORT_SYMBOL(drm_is_current_master);
|
|
|
|
/**
|
|
* drm_master_get - reference a master pointer
|
|
* @master: &struct drm_master
|
|
*
|
|
* Increments the reference count of @master and returns a pointer to @master.
|
|
*/
|
|
struct drm_master *drm_master_get(struct drm_master *master)
|
|
{
|
|
kref_get(&master->refcount);
|
|
return master;
|
|
}
|
|
EXPORT_SYMBOL(drm_master_get);
|
|
|
|
static void drm_master_destroy(struct kref *kref)
|
|
{
|
|
struct drm_master *master = container_of(kref, struct drm_master, refcount);
|
|
struct drm_device *dev = master->dev;
|
|
|
|
if (drm_core_check_feature(dev, DRIVER_MODESET))
|
|
drm_lease_destroy(master);
|
|
|
|
if (dev->driver->master_destroy)
|
|
dev->driver->master_destroy(dev, master);
|
|
|
|
drm_legacy_master_rmmaps(dev, master);
|
|
|
|
idr_destroy(&master->magic_map);
|
|
idr_destroy(&master->leases);
|
|
idr_destroy(&master->lessee_idr);
|
|
|
|
kfree(master->unique);
|
|
kfree(master);
|
|
}
|
|
|
|
/**
|
|
* drm_master_put - unreference and clear a master pointer
|
|
* @master: pointer to a pointer of &struct drm_master
|
|
*
|
|
* This decrements the &drm_master behind @master and sets it to NULL.
|
|
*/
|
|
void drm_master_put(struct drm_master **master)
|
|
{
|
|
kref_put(&(*master)->refcount, drm_master_destroy);
|
|
*master = NULL;
|
|
}
|
|
EXPORT_SYMBOL(drm_master_put);
|