Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2276018imb; Mon, 4 Mar 2019 00:30:15 -0800 (PST) X-Google-Smtp-Source: APXvYqy7jvTKkxwhcVoCVtGBPbw9otKQwLUYrwWgJfR36gHKGK3IXaVihYeke5714SLn6MB86ndq X-Received: by 2002:a17:902:b492:: with SMTP id y18mr18970782plr.175.1551688215403; Mon, 04 Mar 2019 00:30:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551688215; cv=none; d=google.com; s=arc-20160816; b=mBPHop9oqZYzIAO1nHFyfoUoATlQAQ+FFxWsOInEZqosQyw1Lp0jrW+zvJZpJa4w5L CDJQiOcyyQg9BrX3R+piS6Yzl8HqqjpFL8maNEwalewpckRuYXi5YrFhyuYwHevV/XZP WGHmgine179BV4fScswHdx8ODss6x+0Bk9ZB3WPCDNCmoPPSzaEYSpR5y4mDs4Kp3lDH /p/i2suigkhVgL68wDjI0M+q09z4crolaqitJukgneQ+0dDyt8SddFemZWKPtDZ2E8Pp T+AGQA2MIIEZqqhq1ggFt4P5fBH4HHJC2x6RzuuLsUS6StFFw8YFClvHJNG0AljciaP9 FPJg== 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=6luaPyNG8FH9Je7/Z6Hwp7CS/6ADsjo0Tbnt7H6GDB8=; b=z1NznF0o8dA3KR0eEyXMngLYst+3EO8b2gTlKA+3ciI+G4H9p1Sd9CPWWb0eeO0+Tt ZpTc++5VHRBYprqcgWT5zxe2wtq+lGGongAJJCX5xcOwStlvJ4jdJUjoivpkQsVBX7+a zmS/wh/kKxY4tL7S9fb+KrYAkBYK/hm1t1xblgvE6v/DyUacZERZFfoUuYwF5xm+UQAX ZLDEdiXQZ1t0pRGp0OrYk5np8tTfhRTkQos9SV0k3A1haYzLBLyLb+L8Rg5fPRk/Tf3R Gn/clF6HFlhOhgjOHw/cfutLX4tN+9nqCbkoC1//K2FU1a+OWVR9s5qQGTQx2TUDB6t5 uy3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XH+Dhsqe; 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 x9si4877176pfe.254.2019.03.04.00.30.00; Mon, 04 Mar 2019 00:30:15 -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=XH+Dhsqe; 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 S1727521AbfCDI3O (ORCPT + 99 others); Mon, 4 Mar 2019 03:29:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:55202 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726724AbfCDI3I (ORCPT ); Mon, 4 Mar 2019 03:29:08 -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 77C0920823; Mon, 4 Mar 2019 08:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551688147; bh=t3RocPeqG0hItxg2AS11CR1o7xL6T6FgOYlkUfdvCCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XH+DhsqeRih+gNdMyfVynhFuZvVThBSKn44aDnOLrMFoNyKN1JgjUTqNj+KAq7vzS n93vMLEVpYOtivkVlk7SVZ/w7a2qRdBY/Blo9f0gsoJj/ba2WW05CFXbb7WQujLB23 pP77lfEUbKn9gAEwKZlbzKjlQb/yn8l0SMxqkfg4= 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.19 32/78] drivers: thermal: int340x_thermal: Fix sysfs race condition Date: Mon, 4 Mar 2019 09:22:15 +0100 Message-Id: <20190304081626.944115594@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190304081625.508788074@linuxfoundation.org> References: <20190304081625.508788074@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.19-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