Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1417896lqd; Thu, 25 Apr 2024 15:17:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXrNC+YzdQlTN4q1n/tiaeFZWm45l345hLyf8krnMBFJrsfbrZYiPFcVfCx/rJgARnxnGqLO2R/MQ5udQeZwyZQfHA4FNvFtGX/r0ccbg== X-Google-Smtp-Source: AGHT+IEY0cWmtbR5J6YxQxKOMd+WZQnwsnyIFQ6PhF3jiwa8BoVcJ+4Ox0wwFUQv5cjD499H8Zw+ X-Received: by 2002:a50:d69b:0:b0:56d:c928:ad76 with SMTP id r27-20020a50d69b000000b0056dc928ad76mr560382edi.26.1714083469519; Thu, 25 Apr 2024 15:17:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714083469; cv=pass; d=google.com; s=arc-20160816; b=kWujz7acFeIuF25IVoqR1NpmtYbRNFK5d+buib89udD4qyORUWVTAWL9+a2U9q9oCL Zm88iX8dVu1pT0VfvxJ5uezxbvAjdv87kBBR28KCLWYqHVuUhEbUOEKcblAklDX7Px8b 7ESuHWLe3PxdfAmjxs1J9XQuojfoeVit3K1ZSJGXKCl6K9Al2qqflFsJHfTELKBCe/q5 bfdAomX3zXrhVK+aMm/aa0LfH5v8edyoZNSwvsGL+C8a4Ty2sKVwL1DDakMV6uTdnYDV ZV78+Z/+o6NTVdTqm4ZBq2UQigyf+eBKzGOrKvRUpglb+NtufRjldQ1Sl6TBv//gIBoj pSxg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=meHN1gLIkCqL01jNf82QWeZGDGQXZJLxNOBmleDpNho=; fh=td+0v3trrmPV4qP41iyJFurZmnin3cs+QQ90oVx+Zok=; b=cBV7/mAjcYjPFItDFAcoKXJw7muUYUOVh9vnnI6Dk6hSHer+QhxVQdCMLV3G2EDXg8 gNVATDxpT4X63SmGG8a20zKxyo8+V5cl/cIvg3ryQHTJ253ZySCF+egh2ENlQiTIdz6Y iqXqc03BjA7Nud5WxlXE1gyGrtDL7scp0SuQMLMktdMh44Ezw/TvrbcSaAxzF5b6bZGl /G4Xe1ZmKXBc99LXOxxY6gToJprWYTrKHOZjI/zSFPcGWktTXW0sStOmcOW0zm7eubce Xw2Diqyu5xEpafPSxXUGbgmHC2jDEJhGgdiBUkV4bV++0S4WsCuUsjH6qfBXhDx1Kdh8 Z0yQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-159296-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159296-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z91-20020a509e64000000b00570242da2c2si10448081ede.535.2024.04.25.15.17.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 15:17:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159296-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-159296-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159296-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9422E1F29D3A for ; Thu, 25 Apr 2024 22:11:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 82915174EDE; Thu, 25 Apr 2024 22:05:13 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C9A0C15665A; Thu, 25 Apr 2024 22:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714082713; cv=none; b=pqF9ZCaPrBXjtngELU50XdW745AhdT0b2Y2xbvIjn2Q2Mwd91D/zjfKZsTehxxwcsJ2ZhjKiXVJaC4/45DXyzmccjB3qMZybi9ACd1+aCPOaYpdzOAJw0fZuFBzEo78buQ14B5nIGR1/KaB13c+YmtJ5UfEKcLHDHTLr6q2fHZ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714082713; c=relaxed/simple; bh=rzOpat6m+ksSiEzPcmXF9k/1yjbfIuVAexjK08+zs2I=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=YoDAbehfQXQXQA2XYYAPqVBMvoJZiTEoFaVGSptL2DofDgCS19g/qnaWTxVjMiuhcsaMtjjOegSf/UOPG63Zqb239HxXZSqBuuWH3LopZ9EarHsxXOa4+nc5wM6NDdO2Zt0yV3hdJB7TpH8ZhqgrV8ZfZ6n9W1biFQE1S444aK0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 568602F; Thu, 25 Apr 2024 15:05:38 -0700 (PDT) Received: from [10.57.64.58] (unknown [10.57.64.58]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1993F3F73F; Thu, 25 Apr 2024 15:05:08 -0700 (PDT) Message-ID: Date: Thu, 25 Apr 2024 23:05:07 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 3/3] thermal/debugfs: Prevent use-after-free from occurring after cdev removal To: "Rafael J. Wysocki" Cc: Linux PM , LKML , "Rafael J. Wysocki" , Daniel Lezcano References: <12427744.O9o76ZdvQC@kreacher> <13503555.uLZWGnKmhe@kreacher> Content-Language: en-US From: Lukasz Luba In-Reply-To: <13503555.uLZWGnKmhe@kreacher> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/25/24 14:57, Rafael J. Wysocki wrote: > From: Rafael J. Wysocki > > Since thermal_debug_cdev_remove() does not run under cdev->lock, it can > run in parallel with thermal_debug_cdev_state_update() and it may free > the struct thermal_debugfs object used by the latter after it has been > checked against NULL. > > If that happens, thermal_debug_cdev_state_update() will access memory > that has been freed already causing the kernel to crash. > > Address this by using cdev->lock in thermal_debug_cdev_remove() around > the cdev->debugfs value check (in case the same cdev is removed at the > same time in two differet threads) and its reset to NULL. s/differet/different/ > > Fixes: 755113d76786 ("thermal/debugfs: Add thermal cooling device debugfs information") > Cc :6.8+ # 6.8+ > Signed-off-by: Rafael J. Wysocki > --- > drivers/thermal/thermal_debugfs.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > Index: linux-pm/drivers/thermal/thermal_debugfs.c > =================================================================== > --- linux-pm.orig/drivers/thermal/thermal_debugfs.c > +++ linux-pm/drivers/thermal/thermal_debugfs.c > @@ -503,15 +503,21 @@ void thermal_debug_cdev_add(struct therm > */ > void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) > { > - struct thermal_debugfs *thermal_dbg = cdev->debugfs; > + struct thermal_debugfs *thermal_dbg; > > + mutex_lock(&cdev->lock); > + > + thermal_dbg = cdev->debugfs; > if (!thermal_dbg) mutex_unlock(&cdev->lock) missing here > return; > > + cdev->debugfs = NULL; > + > + mutex_unlock(&cdev->lock); > + > mutex_lock(&thermal_dbg->lock); > > thermal_debugfs_cdev_clear(&thermal_dbg->cdev_dbg); > - cdev->debugfs = NULL; > > mutex_unlock(&thermal_dbg->lock); > > > > >