mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Niklas Söderlund" <niklas.soderlund@ragnatech.se>
To: Li Ximo <lixicaicai@gmail.com>
Cc: "Rafael J . Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@kernel.org>,
	Zhang Rui <rui.zhang@intel.com>,
	Lukasz Luba <lukasz.luba@arm.com>,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	Magnus Damm <magnus.damm@gmail.com>,
	linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Wang Haoran <haoranwangsec@gmail.com>
Subject: Re: [PATCH] thermal/drivers/rcar_thermal: fix list_first_entry on possibly empty sensor list in suspend/resume
Date: Mon, 26 Oct 2026 15:07:42 +0100	[thread overview]
Message-ID: <20261026140742.GB1959885@ragnatech.se> (raw)
In-Reply-To: <20261026115018.3227372-1-lixicaicai@gmail.com>

Hello Li,

Thanks for your work.

On 2026-10-26 19:50:18 +0800, Li Ximo wrote:
> From: Wang Haoran <haoranwangsec@gmail.com>
> 
> rcar_thermal_suspend() and rcar_thermal_resume() call
> list_first_entry(&common->head, ...) without checking whether the list
> is empty. list_first_entry() does not return NULL for an empty list; it
> returns the container_of() of the list head pointer, i.e. a wild pointer
> into struct rcar_thermal_common. Dereferencing it at
> priv->chip->needs_suspend_resume (and the MMIO accesses through
> priv->base that follow) is undefined behavior and can corrupt memory or
> crash the kernel.
> 
> The list can indeed be empty: rcar_thermal_probe() only adds a sensor to
> common->head after platform_get_resource() succeeds, and it returns
> success even when zero sensors were probed (no i == 0 check before
> "return 0"). With a malformed DT node (no MEM resource) the driver binds
> successfully with an empty list, and the PM core then unconditionally
> invokes the suspend/resume callbacks. This was found by code audit; we
> do not have R-Car hardware to reproduce it at runtime.

Could you please audit the code some more, preferably without an LLM.  
While I agree the issue should be solved this is not the fix.

Nacked-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>

Does it make sens to allow probe to succeed if we haver a malformed DT 
node? Would a nicer fix not be to fail probe with -ENODEV in that case?

> 
> Fix both callbacks by switching to list_first_entry_or_null() and
> returning 0 early when the sensor list is empty. This matches how the
> rest of the driver already tolerates an empty list via the
> list_for_each_entry()-based rcar_thermal_for_each_priv() iterator used
> in remove and IRQ handling, and does not change behavior on the normal
> non-empty path.
> 
> Reported-by: Wang Haoran <haoranwangsec@gmail.com>
> Reported-by: Li Ximo <lixicaicai@gmail.com>
> Signed-off-by: Wang Haoran <haoranwangsec@gmail.com>
> ---
>  drivers/thermal/renesas/rcar_thermal.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_thermal.c b/drivers/thermal/renesas/rcar_thermal.c
> index 9a9038671..65dba6354 100644
> --- a/drivers/thermal/renesas/rcar_thermal.c
> +++ b/drivers/thermal/renesas/rcar_thermal.c
> @@ -530,8 +530,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)
>  static int rcar_thermal_suspend(struct device *dev)
>  {
>  	struct rcar_thermal_common *common = dev_get_drvdata(dev);
> -	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
> -							  typeof(*priv), list);
> +	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
> +								  typeof(*priv), list);
> +
> +	if (!priv)
> +		return 0;
>  
>  	if (priv->chip->needs_suspend_resume) {
>  		rcar_thermal_common_write(common, ENR, 0);
> @@ -545,10 +548,13 @@ static int rcar_thermal_suspend(struct device *dev)
>  static int rcar_thermal_resume(struct device *dev)
>  {
>  	struct rcar_thermal_common *common = dev_get_drvdata(dev);
> -	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
> -							  typeof(*priv), list);
> +	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
> +								  typeof(*priv), list);
>  	int ret;
>  
> +	if (!priv)
> +		return 0;
> +
>  	if (priv->chip->needs_suspend_resume) {
>  		ret = rcar_thermal_update_temp(priv);
>  		if (ret < 0)
> -- 
> 2.30.2
> 

-- 
Kind Regards,
Niklas Söderlund

      reply	other threads:[~2026-10-26 14:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 11:50 Li Ximo
2026-10-26 14:07 ` Niklas Söderlund [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026140742.GB1959885@ragnatech.se \
    --to=niklas.soderlund@ragnatech.se \
    --cc=daniel.lezcano@kernel.org \
    --cc=geert+renesas@glider.be \
    --cc=haoranwangsec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=lixicaicai@gmail.com \
    --cc=lukasz.luba@arm.com \
    --cc=magnus.damm@gmail.com \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®