Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp156263lqf; Fri, 26 Apr 2024 02:42:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUXUYSP/dl4k+LEVQn0TvJ5L3gpnsNul9Y6MJKI6mgIaKBDAUZr5p1z9KynoYcZWs0oX5mCRvjmdJSnnNn8ZVx1I5Egm302DYJ3wP6IXw== X-Google-Smtp-Source: AGHT+IEaflUKo9KYtqEtPynb5EgU/qMApvVRcrpjE6iMZfGN/UPcwutZJXcxkkNmE5J8bHLJmZAk X-Received: by 2002:a17:90b:1208:b0:2a0:9b18:daf with SMTP id gl8-20020a17090b120800b002a09b180dafmr2104814pjb.42.1714124572064; Fri, 26 Apr 2024 02:42:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714124572; cv=pass; d=google.com; s=arc-20160816; b=0ebCUDVq9A93FQ6EDJHtluAMdMk8XdBf/opkuloWukupjDLbNhEhYvIT3hQqT7l2w4 WOPXAdHxDJjbx6IRHa/HCEY3ncUUfIAnXHzacJX8QAVkwn1bV8kG60xe+rKvrXhD29em nk4AdGjaMR8iNDcAnd1y4e63FTnhTROAc4YLF/aHVh8OwAX0EyqD6/LprOiKusxm2hqh 75YUBGUhZfjEQaNGiPtyLQFd9HxcMhDG30hHdseXpumtQ+FGbDlpkZw0ULahjQQFxHgj vZ9ivHt/fcx4EdKmNblmO1YsrK03MXn4iVFad3sQkSwYY0l53FSrPrf0spVkwBJpNSDB HXPw== 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=19c4V0YZG0Vqgd5jc0ZeZp5brqbrH/V3hztg92stStE=; fh=td+0v3trrmPV4qP41iyJFurZmnin3cs+QQ90oVx+Zok=; b=0BtE1yRa6inv8l9q2y0aTQYvRgKtUkSou9Uqg0hllXCWW1NoUwTrCXFeaQkRxgQyp5 GANv/lCUpRY9WMSnRQ7fye5+w3GV9Nl7/r1nCQ3KBm5YyBKu8IIsfaSjEpwoO1EdOoA1 JZQo3811xY2g9xh1K6raOyjlzYvQ6rOacRON/i+1NM/ph/JH909kxzWdR6nmvBZYxLVF B2C3S0CHFGGBDh9KmDdOB8KZr/3NnkzbaDK1KPiolhRHnYG3oYg3UQc8BlOz6Vpki1ih 2wUYf+5MnGoob8ycGYIaqqdFTAUeh7kvRMlfUHegwHR67smm8ypxerhAr36i/uO580ZX nv9w==; 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-159798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bg7-20020a17090b0d8700b002a2e8b8aefcsi16595789pjb.42.2024.04.26.02.42.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 02:42:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-159798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159798-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DCA9F2877EA for ; Fri, 26 Apr 2024 09:36:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9143F13F446; Fri, 26 Apr 2024 09:36:02 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ADC1813DDD5; Fri, 26 Apr 2024 09:35:59 +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=1714124162; cv=none; b=rRMMjeNSCpQ/V+lypd4hR+3qAvHISCuhW1EMT+IxKruApMWEO2Ivoc8okO+Td3RTLDUa/w5Coq10IcP2lS3JRwnmE0yJHl8gk8rKgTqL/cDuE/19MDhBtzeCevu5EZTqoyfxH9BUhe6289aBrkz/cTQzYIiqJXWiaOZqDtO4bnA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714124162; c=relaxed/simple; bh=vT9giKtFcg61enkBZkXpg92QD6V+SFbh+XlHX2mLqwk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=QwW2fj3QGqOjwsxweXGyxOuRRn1b5ujR07l8VLLbS0Nc1RAaYF+0taUptAt/KRvqZqUf8pFPIFDBtLzSQ9Tg3cP8f1YQflKyYpWBQszdGZx0wFR7DEsnBckxeZryM9rJYspboMYmG7l//mSp/oAZLMZcVofaEZNguJQu1VIJUuA= 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 B894F2F4; Fri, 26 Apr 2024 02:36:21 -0700 (PDT) Received: from [10.57.66.9] (unknown [10.57.66.9]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B89B33F64C; Fri, 26 Apr 2024 02:35:52 -0700 (PDT) Message-ID: <4416568e-79d4-465f-8d47-a199b3b69ca4@arm.com> Date: Fri, 26 Apr 2024 10:35:51 +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 v2 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> <2740417.mvXUDI8C0e@kreacher> Content-Language: en-US From: Lukasz Luba In-Reply-To: <2740417.mvXUDI8C0e@kreacher> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/26/24 10:28, 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 different threads) and its reset to NULL. > > Fixes: 755113d76786 ("thermal/debugfs: Add thermal cooling device debugfs information") > Cc :6.8+ # 6.8+ > Signed-off-by: Rafael J. Wysocki > --- > > v1 -> v2: Add missing mutex_unlock() (Lukasz). > > --- > drivers/thermal/thermal_debugfs.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > Index: linux-pm/drivers/thermal/thermal_debugfs.c > =================================================================== > --- linux-pm.orig/drivers/thermal/thermal_debugfs.c > +++ linux-pm/drivers/thermal/thermal_debugfs.c > @@ -505,15 +505,23 @@ 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; > > - if (!thermal_dbg) > + mutex_lock(&cdev->lock); > + > + thermal_dbg = cdev->debugfs; > + if (!thermal_dbg) { > + mutex_unlock(&cdev->lock); > 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); > > > > > It looks good now Reviewed-by: Lukasz Luba