Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2278836imb; Mon, 4 Mar 2019 00:35:26 -0800 (PST) X-Google-Smtp-Source: APXvYqw8wnBzh6EEvcqryACiCi5DG4W9CETSwQfX/M22Rzi3LpF+7lsn3SXZdKn3MZtOcpbxFYTA X-Received: by 2002:a17:902:9893:: with SMTP id s19mr19345931plp.165.1551688526909; Mon, 04 Mar 2019 00:35:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551688526; cv=none; d=google.com; s=arc-20160816; b=jiWopmii0Oagqa9W0xRyRUSy7uh6URSOvPBKJ7kll7OS8TNBq69NHyQAzmsTq5FIl0 QhSFPo7yy7YDJM2a6HBqga/F330Ag3qacFj9fcTOD69LkmPpn+itnaYTZVvJ0/KgYhWJ ro03TBLuAm6PEwG3ckmeiQWPHp5Fo1ZMNswbEO++vV94Mp3VHQW4npppO9e3DF5WwcVB lKW2bC77CcD4pluLR17kz83mep+9rwzSwelW5mJjDhJf9yjrdB39C7bDZSAV1QIdya0J lG22OGe8+c81kZRzfAHKhbmBE/JdbBW2I5ooLj7ojTIZjo3ek1Onz4zfhmCdGkJOVYwh 0x2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aTL+P1+bKGzvVo2K5o3BFPaJnrROYURr3zL7s0FTah8=; b=HalfKYQBsYsGYnx6ZZQbZ/JJrOIhnR7/D3zkAETBQoKN16iBDDgTM73RLJhCEkqhDc 5CoE4rNrO5Ki9mJZArH2uN4UCV89UYvgyC7OOqMeDJibepeTKNB269U9tm9hzjSYfOpK 3FKqdXndThnQ48Yz0iq6a2j6hiR/vj4V35IgY83KQW3p0axO8xCJLcNpk1kjFp4mOU1q P3VAuOawvYTO+fADNvgPCwc3vaXCtweJ5LmAnEpv+bM8yTQM+h2cykGnRgZzib7JOn9U uFRBKQQmkpqZAuqu1XqVeo3+2gYghL2cO5aKjojMDyGHusPsnI9HhnAMNBOG/VleLE0e iiVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=B4WXknp1; 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 l15si4879718pfb.146.2019.03.04.00.35.09; Mon, 04 Mar 2019 00:35:26 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=B4WXknp1; 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 S1726425AbfCDIdZ (ORCPT + 99 others); Mon, 4 Mar 2019 03:33:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:37768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727158AbfCDIdT (ORCPT ); Mon, 4 Mar 2019 03:33:19 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C61B920836; Mon, 4 Mar 2019 08:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551688398; bh=HnowgTxvf6LwJixNEYHbOuccYswNsOaex2mZ+HxcaP0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B4WXknp18rWDR9S9qjyMfJVJZAL35iKyhxlmKKkPEfZdV8fzQpMZktYmBcUo05hoa 9MABTWQZ2ZAG3UBhtA+V/gFjIezRQVX6nAuIIsGTjmOZNp6ggO62eaRM0Y7PDfJsTh rnsAz0buQYxsD+hZPmzxyWF7jSQjjvwss0ZlmnAo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Aaron Hill , Zhang Rui , Sasha Levin Subject: [PATCH 4.20 32/88] drivers: thermal: int340x_thermal: Fix sysfs race condition Date: Mon, 4 Mar 2019 09:22:15 +0100 Message-Id: <20190304081631.867882705@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190304081630.610632175@linuxfoundation.org> References: <20190304081630.610632175@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 129699bb8c7572106b5bbb2407c2daee4727ccad ] Changes since V1: * Use dev_info instead of printk * Use dev_warn instead of BUG_ON Previously, sysfs_create_group was called before all initialization had fully run - specifically, before pci_set_drvdata was called. Since the sysctl group is visible to userspace as soon as sysfs_create_group returns, a small window of time existed during which a process could read from an uninitialized/partially-initialized device. This commit moves the creation of the sysctl group to after all initialized is completed. This ensures that it's impossible for userspace to read from a sysctl file before initialization has fully completed. To catch any future regressions, I've added a check to ensure that proc_thermal_emum_mode is never PROC_THERMAL_NONE when a process tries to read from a sysctl file. Previously, the aforementioned race condition could result in the 'else' branch running while PROC_THERMAL_NONE was set, leading to a null pointer deference. Signed-off-by: Aaron Hill Signed-off-by: Zhang Rui Signed-off-by: Sasha Levin --- .../processor_thermal_device.c | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/thermal/int340x_thermal/processor_thermal_device.c b/drivers/thermal/int340x_thermal/processor_thermal_device.c index 8e0f665cf06f8..8e1cf4d789be1 100644 --- a/drivers/thermal/int340x_thermal/processor_thermal_device.c +++ b/drivers/thermal/int340x_thermal/processor_thermal_device.c @@ -84,7 +84,12 @@ static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \ struct pci_dev *pci_dev; \ struct platform_device *pdev; \ struct proc_thermal_device *proc_dev; \ -\ + \ + if (proc_thermal_emum_mode == PROC_THERMAL_NONE) { \ + dev_warn(dev, "Attempted to get power limit before device was initialized!\n"); \ + return 0; \ + } \ + \ if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \ pdev = to_platform_device(dev); \ proc_dev = platform_get_drvdata(pdev); \ @@ -298,11 +303,6 @@ static int proc_thermal_add(struct device *dev, *priv = proc_priv; ret = proc_thermal_read_ppcc(proc_priv); - if (!ret) { - ret = sysfs_create_group(&dev->kobj, - &power_limit_attribute_group); - - } if (ret) return ret; @@ -316,8 +316,7 @@ static int proc_thermal_add(struct device *dev, proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops); if (IS_ERR(proc_priv->int340x_zone)) { - ret = PTR_ERR(proc_priv->int340x_zone); - goto remove_group; + return PTR_ERR(proc_priv->int340x_zone); } else ret = 0; @@ -331,9 +330,6 @@ static int proc_thermal_add(struct device *dev, remove_zone: int340x_thermal_zone_remove(proc_priv->int340x_zone); -remove_group: - sysfs_remove_group(&proc_priv->dev->kobj, - &power_limit_attribute_group); return ret; } @@ -364,7 +360,10 @@ static int int3401_add(struct platform_device *pdev) platform_set_drvdata(pdev, proc_priv); proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV; - return 0; + dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PLATFORM_DEV\n"); + + return sysfs_create_group(&pdev->dev.kobj, + &power_limit_attribute_group); } static int int3401_remove(struct platform_device *pdev) @@ -441,7 +440,10 @@ static int proc_thermal_pci_probe(struct pci_dev *pdev, dev_err(&pdev->dev, "No auxiliary DTSs enabled\n"); } - return 0; + dev_info(&pdev->dev, "Creating sysfs group for PROC_THERMAL_PCI\n"); + + return sysfs_create_group(&pdev->dev.kobj, + &power_limit_attribute_group); } static void proc_thermal_pci_remove(struct pci_dev *pdev) -- 2.19.1