From patchwork Mon Dec 5 03:49:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yingliang X-Patchwork-Id: 29509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2054158wrr; Sun, 4 Dec 2022 19:55:30 -0800 (PST) X-Google-Smtp-Source: AA0mqf4L83csBp1qGxV1abob8aak/+S/5Q0S0OmEE3akcCzEhRvMkAJwCU0nfePZ9Ye/Ji+8vJUV X-Received: by 2002:a17:907:9c0a:b0:7ae:1e53:8dd4 with SMTP id ld10-20020a1709079c0a00b007ae1e538dd4mr25243827ejc.42.1670212530437; Sun, 04 Dec 2022 19:55:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670212530; cv=none; d=google.com; s=arc-20160816; b=XtenVmvDgxn596n0lYFtgHbXsPjhaOPCB58dPnW+a9et1nfw3SbiM3zMnQOMiieGv8 nznw3mJlwn3T/xtjipPfLU9sVUIxzmwPc8w1ZS0tUXkU1VmLLE03QnntFT776xABV2Q6 DkkfiFbiCPPkQjM5nITLxkUQ4ftsSD9sWV+fLx8zWAdhYOA97MtVhSC1wm7Z/f/ywelU CZn1Dyf2Ku74YKFjen/eZZ1dB4O+NKkjNMJCWWagtYDMp+DEac/Ih/sjdskpz9mGLt/A RN3jyKBhPZ1w1xRifiDkiVXa3JdJQ5Yra2ZGnNJJzm/ym9zc6ykibybkhdgLwFOHl5YS BbsQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c6-20020a05640227c600b0046a222b78a5si11913282ede.249.2022.12.04.19.54.42; Sun, 04 Dec 2022 19:55:30 -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 + 99 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751344774332038516?= X-GMAIL-MSGID: =?utf-8?q?1751344774332038516?= 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);