Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1190018pxu; Fri, 27 Nov 2020 01:42:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJwyfYWV79qYjAU2QvbXu6GXBHCR/xOxPx/0qTDgUd6vClMAKmhbwZl44njNVq41FW0yKPVj X-Received: by 2002:aa7:d711:: with SMTP id t17mr6617439edq.83.1606470168300; Fri, 27 Nov 2020 01:42:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606470168; cv=none; d=google.com; s=arc-20160816; b=beKF8zrMBxrhca4JG3bPlisIojB9ADfC/VYz5kC6IfvF1HqbrT1v2EZK1wP8gORM9o qNSE8UL/k+Pv+ciZ3ZxHmXEtGmOj7/VYjC7hjY0/3GHE1pzY/VcG5ItHYhocRARDZWJ6 7wpuKrRG2FDmQ3MusS18X2pEUNQva8M1PdBSdW6wlr79ZRhMg85cbZzZLgefdYvvZ1PQ LnuvRCtGsdQtxwIzL3y/M7MqQ7Y6EiVlFDBzqTbhlYDHdYQKtSjfwKeUCBx7i0vMk5sO 3ZlFrRBy+Czse2nH7HXmQTk/3iRp55t0LZdO+0UQzkuK4MNYFZVdaZpQQW3MNa839dSY zGpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=K9Qli0sVuZzgxwpQEIK6GFFcq5J7BDIUS4Vog+mKqzs=; b=KtvzL2qaL8YK2loWo4uKakxbs/NEJpOROrJD0EjmVfqQhsBysQMhIqsZf5QkHAUzXN zzxCxlZMveX1NDiFnCMXPG2D8mwEOscuGwvyKflQbs9cOg9M6YgctWDY//tHcn8yoTNV EIexcn8RWNHbzYENQEKkK9fKC9xnrqOds6xCLHaPkjnXPpUG1Z0SJ/kaGd2WwHLlhZBx pZkGqoL5yofEW3jf0azsO75zFRbb6z0bAL2ODRlD1mh7fboUiW3mpIwsb7WgVbCjrI2J bkzO38OIM0n90eEstJVBH3vQqu65IuBDU377NcOct9icviUCQjr85vUbH8I3LbbwbXev IU9g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gl10si4674939ejb.138.2020.11.27.01.42.25; Fri, 27 Nov 2020 01:42:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726248AbgK0Jjx (ORCPT + 99 others); Fri, 27 Nov 2020 04:39:53 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:8046 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725616AbgK0Jjw (ORCPT ); Fri, 27 Nov 2020 04:39:52 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Cj8jC4pSwzhjJL; Fri, 27 Nov 2020 17:39:27 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Fri, 27 Nov 2020 17:39:39 +0800 From: Qinglang Miao To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter CC: , , Qinglang Miao Subject: [PATCH] drm/dev: Fix NULL pointer dereference in drm_minor_alloc Date: Fri, 27 Nov 2020 17:43:55 +0800 Message-ID: <20201127094355.120520-1-miaoqinglang@huawei.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037] CPU: 0 PID: 18491 Comm: syz-executor.0 Tainted: G C 5.10.0-rc4+ #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1 04/01/2014 RIP: 0010:kobject_put+0x2f/0x140 Call Trace: put_device+0x20/0x40 drm_minor_alloc_release+0x60/0xe0 [drm] drm_managed_release+0x1b6/0x440 [drm] drm_dev_init+0x50b/0x8e0 [drm] __devm_drm_dev_alloc+0x50/0x160 [drm] vgem_init+0x15c/0x1000 [vgem] do_one_initcall+0x149/0x7e0 do_init_module+0x1ef/0x700 load_module+0x3467/0x4140 __do_sys_finit_module+0x10d/0x1a0 do_syscall_64+0x34/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 kfree(minor->kdev) in put_device would raise a null-ptr-deref bug when minor->kdev is null or error pointer. So do check before put_device in drm_minor_alloc_release and prohibit minor->kdev becoming an error pointer. Fixes: f96306f9892b ("drm: manage drm_minor cleanup with drmm_") Signed-off-by: Qinglang Miao --- drivers/gpu/drm/drm_drv.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index cd162d406..c253d3cd4 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -100,7 +100,8 @@ static void drm_minor_alloc_release(struct drm_device *dev, void *data) WARN_ON(dev != minor->dev); - put_device(minor->kdev); + if (minor->kdev) + put_device(minor->kdev); spin_lock_irqsave(&drm_minor_lock, flags); idr_remove(&drm_minors_idr, minor->index); @@ -140,8 +141,11 @@ static int drm_minor_alloc(struct drm_device *dev, unsigned int type) return r; minor->kdev = drm_sysfs_minor_alloc(minor); - if (IS_ERR(minor->kdev)) - return PTR_ERR(minor->kdev); + if (IS_ERR(minor->kdev)) { + r = PTR_ERR(minor->kdev); + minor->kdev = NULL; + return r; + } *drm_minor_get_slot(dev, type) = minor; return 0; -- 2.23.0