From patchwork Tue Nov 3 06:52:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Wensheng X-Patchwork-Id: 11876005 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3278892C for ; Tue, 3 Nov 2020 06:53:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2068422277 for ; Tue, 3 Nov 2020 06:53:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726109AbgKCGxB (ORCPT ); Tue, 3 Nov 2020 01:53:01 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:7405 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727794AbgKCGxB (ORCPT ); Tue, 3 Nov 2020 01:53:01 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4CQL7l2Db1z6xy6; Tue, 3 Nov 2020 14:52:35 +0800 (CST) Received: from huawei.com (10.175.112.208) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Tue, 3 Nov 2020 14:52:27 +0800 From: Wang Wensheng To: , , , CC: , Subject: [PATCH -next v2 1/2] watchdog: Clean up error handlings of __watchdog_register_device Date: Tue, 3 Nov 2020 06:52:16 +0000 Message-ID: <20201103065217.67876-1-wangwensheng4@huawei.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 X-Originating-IP: [10.175.112.208] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Signed-off-by: Wang Wensheng --- drivers/watchdog/watchdog_core.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c index 423844757812..c73871f41142 100644 --- a/drivers/watchdog/watchdog_core.c +++ b/drivers/watchdog/watchdog_core.c @@ -252,10 +252,8 @@ static int __watchdog_register_device(struct watchdog_device *wdd) wdd->id = id; ret = watchdog_dev_register(wdd); - if (ret) { - ida_simple_remove(&watchdog_ida, id); - return ret; - } + if (ret) + goto id_remove; } /* Module parameter to force watchdog policy on reboot. */ @@ -273,9 +271,7 @@ static int __watchdog_register_device(struct watchdog_device *wdd) if (ret) { pr_err("watchdog%d: Cannot register reboot notifier (%d)\n", wdd->id, ret); - watchdog_dev_unregister(wdd); - ida_simple_remove(&watchdog_ida, id); - return ret; + goto dev_unregister; } } @@ -289,6 +285,13 @@ static int __watchdog_register_device(struct watchdog_device *wdd) } return 0; + +dev_unregister: + watchdog_dev_unregister(wdd); +id_remove: + ida_simple_remove(&watchdog_ida, id); + + return ret; } /** From patchwork Tue Nov 3 06:52:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Wensheng X-Patchwork-Id: 11876003 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D89266A2 for ; Tue, 3 Nov 2020 06:52:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C635D223AC for ; Tue, 3 Nov 2020 06:52:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725968AbgKCGwl (ORCPT ); Tue, 3 Nov 2020 01:52:41 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:7406 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgKCGwl (ORCPT ); Tue, 3 Nov 2020 01:52:41 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4CQL7l2PZHz7223; Tue, 3 Nov 2020 14:52:35 +0800 (CST) Received: from huawei.com (10.175.112.208) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Tue, 3 Nov 2020 14:52:28 +0800 From: Wang Wensheng To: , , , CC: , Subject: [PATCH -next v2 2/2] watchdog: Fix potential dereferencing of null pointer Date: Tue, 3 Nov 2020 06:52:17 +0000 Message-ID: <20201103065217.67876-2-wangwensheng4@huawei.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20201103065217.67876-1-wangwensheng4@huawei.com> References: <20201103065217.67876-1-wangwensheng4@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.208] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org A reboot notifier, which stops the WDT by calling the stop hook without any check, would be registered when we set WDOG_STOP_ON_REBOOT flag. Howerer we allow the WDT driver to omit the stop hook since commit "d0684c8a93549" ("watchdog: Make stop function optional") and provide a module parameter for user that controls the WDOG_STOP_ON_REBOOT flag in commit 9232c80659e94 ("watchdog: Add stop_on_reboot parameter to control reboot policy"). Together that commits make user potential to insert a watchdog driver that don't provide a stop hook but with the stop_on_reboot parameter set, then dereferencing of null pointer occurs on system reboot. Check the stop hook before registering the reboot notifier to fix the issue. Signed-off-by: Wang Wensheng --- drivers/watchdog/watchdog_core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c index c73871f41142..5269761ba072 100644 --- a/drivers/watchdog/watchdog_core.c +++ b/drivers/watchdog/watchdog_core.c @@ -265,8 +265,12 @@ static int __watchdog_register_device(struct watchdog_device *wdd) } if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) { - wdd->reboot_nb.notifier_call = watchdog_reboot_notifier; + if (!wdd->ops->stop) { + ret = -EINVAL; + goto dev_unregister; + } + wdd->reboot_nb.notifier_call = watchdog_reboot_notifier; ret = register_reboot_notifier(&wdd->reboot_nb); if (ret) { pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",