Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3032488pxb; Mon, 18 Oct 2021 07:00:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzElox/xixMaWmy0Kd4nO7wVjAkFFDDMMPdd+DLaPgEFOrUKgRQVlugXsFhI1m0crz9Ro2+ X-Received: by 2002:a63:e64a:: with SMTP id p10mr24110687pgj.263.1634565607587; Mon, 18 Oct 2021 07:00:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634565607; cv=none; d=google.com; s=arc-20160816; b=Yos7LIcEO3bqoRg4GD6YPpMJ3+kd1K+2Sv6cACHV1XsMYi+wmQV7dia5PlBKYwzzvQ 2AhBWLhnoyLO79g8SguRa748ir/R34zWpu1dlw8SUJzNoJgW+XBhpTsmb1vr5NVhXK1M UXZw8oePUrUKWoCqchp9RkY+9mrFLwznpDvpV3Li4e0nzTZl7EB+JycGVsUrvyyHa4lp 9VJhKE6wwcCKTO0clJTYtxt2QM8SE80VsD2XkaDekMBAMmNHdvS5O50a0OUs0WjeRGF+ o0QweZbmXYyj3vefrYTbXzj4oitQiJn1stiOyxXSnR94kSboIDluLk2y0EXrlGS5lhY/ 1sVg== 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=ol2UCw0nZmNUmBz258L7o3/CjxgRLgp2QKZJMy69dbw=; b=PNBaLGMrwxX9IMepONH8492IhDLU8QOm4epv3tcu6fI6JTA1qydLDaH3dnzsDJGywH Ox4pv97l2hwgiZo7WI3UKtIuECW/E+jp0MrQ/4jxECmst98D2g3ajYXZxVuEJaCBNYyL 9Fk0hAWnrkv75DIXSHP48J1j+zWTXv+0HwTO1JQdcxKqkEz+gPQOicIwFDh78ETXSxyw 3Em0NgfVx/UawUPoJ59aNn+YRqwpm2HUtiyLlnVCTkdUd+tiRTt3HFv2O8lGJj6IJG2R 1uzJswBy7Jojq23okeAbmcsB0H9831yBC8MsSKs8sRVO67C+cOO3eWqLTDPvw1Zs3G+7 kEPA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j28si200089pgl.0.2021.10.18.06.59.54; Mon, 18 Oct 2021 07:00:07 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233488AbhJRN7i (ORCPT + 99 others); Mon, 18 Oct 2021 09:59:38 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:13765 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234216AbhJRN5j (ORCPT ); Mon, 18 Oct 2021 09:57:39 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HXyyW1K8yzZcJQ; Mon, 18 Oct 2021 21:53:39 +0800 (CST) Received: from kwepemm600001.china.huawei.com (7.193.23.3) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Mon, 18 Oct 2021 21:55:23 +0800 Received: from huawei.com (10.175.104.82) by kwepemm600001.china.huawei.com (7.193.23.3) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Mon, 18 Oct 2021 21:55:22 +0800 From: Wang Hai To: , , , , , CC: , Subject: [PATCH] drm: fix null-ptr-deref in drm_minor_alloc_release Date: Mon, 18 Oct 2021 21:54:28 +0800 Message-ID: <20211018135428.3971329-1-wanghai38@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600001.china.huawei.com (7.193.23.3) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I got a null-ptr-deref report: BUG: kernel NULL pointer dereference, address: 0000000000000030 ... RIP: 0010:kobject_put+0x2a/0x180 ... Call Trace: put_device+0x25/0x30 drm_minor_alloc_release.cold+0x45/0x7f [drm] drm_managed_release+0x158/0x2d0 [drm] drm_dev_init+0x3a7/0x4a0 [drm] __devm_drm_dev_alloc+0x55/0xd0 [drm] mi0283qt_probe+0x8a/0x2b5 [mi0283qt] spi_probe+0xeb/0x130 ... entry_SYSCALL_64_after_hwframe+0x44/0xae If drm_sysfs_minor_alloc() fails in drm_minor_alloc(), minor->kdev will point to PTR_ERR(...) instead of NULL. This will result in null-ptr-deref when put_device(minor->kdev) is called. drm_dev_init() drm_minor_alloc() drm_sysfs_minor_alloc(); // fail, minor->kdev = PTR_ERR(...) drm_managed_release() drm_minor_alloc_release() put_device(minor->kdev); // access non-existent kdev Define a temp variable and assign it to minor->kdev if the temp variable is not PTR_ERR. Fixes: f96306f9892b ("drm: manage drm_minor cleanup with drmm_") Reported-by: Hulk Robot Signed-off-by: Wang Hai --- drivers/gpu/drm/drm_drv.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index b3a1636d1b98..b302536edbce 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -112,6 +112,7 @@ static int drm_minor_alloc(struct drm_device *dev, unsigned int type) { struct drm_minor *minor; unsigned long flags; + struct device *kdev; int r; minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL); @@ -140,9 +141,11 @@ static int drm_minor_alloc(struct drm_device *dev, unsigned int type) if (r) return r; - minor->kdev = drm_sysfs_minor_alloc(minor); - if (IS_ERR(minor->kdev)) - return PTR_ERR(minor->kdev); + kdev = drm_sysfs_minor_alloc(minor); + if (IS_ERR(kdev)) + return PTR_ERR(kdev); + + minor->kdev = kdev; *drm_minor_get_slot(dev, type) = minor; return 0; -- 2.25.1