Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3118829pxx; Sun, 1 Nov 2020 23:46:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJzCBRTcXj1+mq/zz8cEC13z8APiubdK396dSvM5AfI335KrYLyX56vQx4o5c34HZdA5Y/7V X-Received: by 2002:a17:906:3087:: with SMTP id 7mr14017662ejv.375.1604303206077; Sun, 01 Nov 2020 23:46:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604303206; cv=none; d=google.com; s=arc-20160816; b=EiFhWhMxqkhR9Bt5jhbinrWoSEY2Y2d+Ef9avXKSaoNeLzWvdjAdvcGdEL/xMKEtNk i2pBI97rQom0SLwyc5F1PNjLKDpZsVk8Sby9vpKhs/W0PUH/bAWwFff4bN82QmU7Cowz Jk/94Amz2WTLjvKGGOdrkvdLpsFk78dFsi9xElsZpCp9HPRAIvE7BSDFz1/BLe+H9eij lmisunP+fyhl3bfFDpa/Fc4cLROh6vX/Ed+lyHBVbDtaWCvobKaH8mrIyXT4YPB2kkGP 7jv7923II5HV1EipW0Kxz97/669KmToDhoBv5DhYeNeSSs7Dn2RqdqMIczAyDuJQEYsQ wgEg== 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=DYeOZV+rg4Uez0Hs3MnzN/cMrbuYTbfEUJq3uSIEmfM=; b=FGqJhtrqIjRTshJri4BM38ZQg2wD9sScCkpZuR67LAme64oTDq5PdrzjhX7Dbkd5/o 1tfihV3XRgszcLV/GOMtXQYpfMPKAT4oaEUmHPwN4jt347IfvTcRgwk2tXi+qIZGSjHU u8ykxURdlQ0eWkq6e5+sRIu9LcX0VVReQ0u1w+/6tPVDwdZHyqawaB/FTtbayjim706d MOZpXp29HIo6cimOlmLY7vdV4JvuKR0twktW96Uy+DfaGdcx7nAGx1o3Xr4lgvmrikOr Qxi2+yScVx/A5YcMNqj6/frQugXM1n9emrUunOw8vVD1M+KDiBwGyqaLyMssbhLzWxsx 775w== 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 n24si9554222ejz.56.2020.11.01.23.46.23; Sun, 01 Nov 2020 23:46:46 -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 S1728156AbgKBHoh (ORCPT + 99 others); Mon, 2 Nov 2020 02:44:37 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:7396 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726819AbgKBHog (ORCPT ); Mon, 2 Nov 2020 02:44:36 -0500 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4CPlL76NNBz7231; Mon, 2 Nov 2020 15:44:31 +0800 (CST) Received: from huawei.com (10.175.112.208) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.487.0; Mon, 2 Nov 2020 15:44:22 +0800 From: Wang Wensheng To: , , , CC: , Subject: [PATCH -next] watchdog: Fix potential dereferencing of null pointer Date: Mon, 2 Nov 2020 07:44:13 +0000 Message-ID: <20201102074413.69552-1-wangwensheng4@huawei.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.112.208] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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. Fixes: d0684c8a9354 ("watchdog: Make stop function optional") Signed-off-by: Wang Wensheng --- drivers/watchdog/watchdog_core.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c index 423844757812..5269761ba072 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. */ @@ -267,15 +265,17 @@ 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", wdd->id, ret); - watchdog_dev_unregister(wdd); - ida_simple_remove(&watchdog_ida, id); - return ret; + goto dev_unregister; } } @@ -289,6 +289,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; } /** -- 2.25.0