Received: by 10.213.65.68 with SMTP id h4csp213552imn; Fri, 23 Mar 2018 03:03:07 -0700 (PDT) X-Google-Smtp-Source: AG47ELu1Bn2Nhq6H/fzkoRnNNgd5wtSSXUww6mSYUI4Z7a0ZPbhvpGi48TIx/kPmKnYhmm1J0iLm X-Received: by 10.99.127.89 with SMTP id p25mr19562531pgn.293.1521799387227; Fri, 23 Mar 2018 03:03:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521799387; cv=none; d=google.com; s=arc-20160816; b=Abdun5B7OtO1egFRJu/A73g9TH/gd0ykmbE2M/dWINFMArFpOjfuZe+QwWTOa7+3Ax AYD9z+a2dBcrN6C8jVEpMnHTKwNI/KZMIiH4W2lFRrpJjcC5n5LbhkYejzWx84jfmO+2 ppdvyjQonrQvubkD1/lzUFxTGUO6U3yQ5zR+ozUDRbE1PEgVexMFFMKxoabozMU/wjIu AQPvMOHQs60q58QZd2qpqy1SAI36LT8TY6euWjm40pKP9fK3HhU5fkvkxUH/yheCLZBh l0hmUyhHsk2CA2FljjQKzav6oPgw4U061CaMaPKBn4/OC7s23VBTjDPKo3PW4O+AXVYY LTdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=qmvxrf9OopyHZubcjUAOqpdxNr+SmUH+A6BOnfE7h74=; b=Gshc8aBTrPnVpUKdZwjyWpqYZt4Ky8HkfOP5AHyAdh1MOgh5r8hTg4efbaL5iqw+jE tBrzyy32h7vz26bv0lrwj8XSnBhH0j5G7WPPG4P5LzLNhMIcu1k1+bp3Kuh20nIdBNI3 bEua+qRZyZWsuwkEb43b1rLZbFyipQzr2NENU0CUhYDm0izptDNguslJoL19ONeRexvh LOGy5T7s/z8A3yzBPeY3pYZSvYcqHZ+9zQ/iuNGF3Q/G7XuZ9Rw9Wqz9F3gfKAaG1RJz hY5rcgvzePOkix4qV6wNXI+E2XebYYceYqkYrJ9jVeQFJ4gwZbC4tQJL7fntqcBiRKbX 0WkA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 19si5844990pgg.395.2018.03.23.03.02.52; Fri, 23 Mar 2018 03:03:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753983AbeCWKBn (ORCPT + 99 others); Fri, 23 Mar 2018 06:01:43 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:37988 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751966AbeCWKBk (ORCPT ); Fri, 23 Mar 2018 06:01:40 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 5E34E1071; Fri, 23 Mar 2018 10:01:39 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guenter Roeck , Wim Van Sebroeck , Sasha Levin Subject: [PATCH 4.14 22/77] watchdog: Fix potential kref imbalance when opening watchdog Date: Fri, 23 Mar 2018 10:53:56 +0100 Message-Id: <20180323094143.684670232@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180323094142.260022880@linuxfoundation.org> References: <20180323094142.260022880@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Guenter Roeck [ Upstream commit 4bcd615fad6adddc68b058d498b30a9e0e0db77a ] If a watchdog driver's open function sets WDOG_HW_RUNNING with the expectation that the watchdog can not be stopped, but then stops the watchdog anyway in its stop function, kref_get() wil not be called in watchdog_open(). If the watchdog then stops on close, WDOG_HW_RUNNING will be cleared and kref_put() will be called, causing a kref imbalance. As result the character device data structure will be released, which in turn will cause the system to crash on the next call to watchdog_open(). Fixes: ee142889e32f5 ("watchdog: Introduce WDOG_HW_RUNNING flag") Signed-off-by: Guenter Roeck Signed-off-by: Wim Van Sebroeck Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/watchdog/watchdog_dev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/drivers/watchdog/watchdog_dev.c +++ b/drivers/watchdog/watchdog_dev.c @@ -768,6 +768,7 @@ static int watchdog_open(struct inode *i { struct watchdog_core_data *wd_data; struct watchdog_device *wdd; + bool hw_running; int err; /* Get the corresponding watchdog device */ @@ -787,7 +788,8 @@ static int watchdog_open(struct inode *i * If the /dev/watchdog device is open, we don't want the module * to be unloaded. */ - if (!watchdog_hw_running(wdd) && !try_module_get(wdd->ops->owner)) { + hw_running = watchdog_hw_running(wdd); + if (!hw_running && !try_module_get(wdd->ops->owner)) { err = -EBUSY; goto out_clear; } @@ -798,7 +800,7 @@ static int watchdog_open(struct inode *i file->private_data = wd_data; - if (!watchdog_hw_running(wdd)) + if (!hw_running) kref_get(&wd_data->kref); /* dev/watchdog is a virtual (and thus non-seekable) filesystem */