Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5383341rwb; Sun, 4 Dec 2022 20:42:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf7lzzNkV/lCCjtSearUbvuuzCKnjnv3endp4Q69Q4+LpvooRFBV2s5tSyhNPMTkcrwEtF0d X-Received: by 2002:a17:906:658:b0:7ae:df97:9ff4 with SMTP id t24-20020a170906065800b007aedf979ff4mr19186143ejb.762.1670215366261; Sun, 04 Dec 2022 20:42:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670215366; cv=none; d=google.com; s=arc-20160816; b=LU7EM4OCVPo0nEF8B9tL2pMb/HzxaUXCZw0Rp/VWRCJUcxcMfZBRlo2N5mobZXVXj2 KfP+37MfEnLswHlxukbEIntyosBG1VIY7BvZrn5YopUcDoGJUM4tv13ZuyqVOAs8VpVt Y83O98NppL7saTc3XnID+BDhHlQqWNRKkgKBOS7n769x89VIN00wY0A/+iAW3LE6zn/F am1e3Y9uwd7t/GT8fwXEXX1FP2kzwU+YSUzlalRRuGeyWX35Sstqtd7fuHjhKP7wrMqr 8p4j8dLTB4q6YQsVUlyw1Tru6PAJqnxlOjbBm5sv2eseOk1x3Whb98tMczc7TKyTYeia s8HQ== 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=cWc7NzfGpCCmo+Fr59wJuyNI83ZfhqlOcN+smZku2ts=; b=VJgENYaKtWMOquTG1KxV8t2HjCOuoMQ6wXIGZKNbLy6+hTd1l/WahgZ4hreIRmi0Dt hFGWstpwL1L3zGYphoKLvR4BBGeNzaCxOB7rmFIhqe90fhQHJnC2jIGL4Vk4Apn2DhLf j/ym7cSDqjTglxQAoO8vjh6uKsG589+TTnffPeV67HzgkXZN6ZBbP/H4Y13PhlnsbHnB t0z3VAP9RT+MSSRF8kSw9PmMsjyhNB3i6CPupTVIy8Now6bfZgvldv87J15ccWnwyrBs zEOPyaX7Qf/aJmJFsv+UYd56KnLwWFNKb+Li9XH/dEUh0wxzTO9YGFZJujhk0nd0kcPD 9YgQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go19-20020a1709070d9300b007c0ea5a7ca4si2387021ejc.858.2022.12.04.20.42.27; Sun, 04 Dec 2022 20:42:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231393AbiLEDvW (ORCPT + 82 others); Sun, 4 Dec 2022 22:51:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231161AbiLEDvT (ORCPT ); Sun, 4 Dec 2022 22:51:19 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58114FCFA for ; Sun, 4 Dec 2022 19:51:17 -0800 (PST) Received: from dggpemm500007.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4NQV1x3F51z15N58; Mon, 5 Dec 2022 11:50:29 +0800 (CST) Received: from huawei.com (10.175.103.91) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 5 Dec 2022 11:51:15 +0800 From: Yang Yingliang To: CC: , , Subject: [PATCH] driver core: fix potential null-ptr-deref in device_add() Date: Mon, 5 Dec 2022 11:49:04 +0800 Message-ID: <20221205034904.2077765-1-yangyingliang@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.103.91] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I got the following null-ptr-deref report while doing fault injection test: BUG: kernel NULL pointer dereference, address: 0000000000000058 CPU: 2 PID: 278 Comm: 37-i2c-ds2482 Tainted: G B W N 6.1.0-rc3+ RIP: 0010:klist_put+0x2d/0xd0 Call Trace: klist_remove+0xf1/0x1c0 device_release_driver_internal+0x196/0x210 bus_remove_device+0x1bd/0x240 device_add+0xd3d/0x1100 w1_add_master_device+0x476/0x490 [wire] ds2482_probe+0x303/0x3e0 [ds2482] This is how it happened: w1_alloc_dev() // The dev->driver is set to w1_master_driver. memcpy(&dev->dev, device, sizeof(struct device)); device_add() bus_add_device() dpm_sysfs_add() // It fails, calls bus_remove_device. // error path bus_remove_device() // The dev->driver is not null, but driver is not bound. __device_release_driver() klist_remove(&dev->p->knode_driver) <-- It causes null-ptr-deref. // normal path bus_probe_device() // It's not called yet. device_bind_driver() If dev->driver is set, in the error path after calling bus_add_device() in device_add(), bus_remove_device() is called, then the device will be detached from driver. But device_bind_driver() is not called yet, so it causes null-ptr-deref while access the 'knode_driver'. To fix this, set dev->driver to null in the error path before calling bus_remove_device(). Fixes: 57eee3d23e88 ("Driver core: Call device_pm_add() after bus_add_device() in device_add()") Signed-off-by: Yang Yingliang --- drivers/base/core.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index d02501933467..e5c15061070b 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3551,6 +3551,7 @@ int device_add(struct device *dev) device_pm_remove(dev); dpm_sysfs_remove(dev); DPMError: + dev->driver = NULL; bus_remove_device(dev); BusError: device_remove_attrs(dev); -- 2.25.1