public inbox for linux-tegra@vger.kernel.org
 help / color / mirror / Atom feed
From: Marc Dietrich <marvin24@gmx.de>
To: Sebastian Josue Alba Vives <sebasjosue84@gmail.com>
Cc: ac100@lists.launchpad.net, linux-tegra@vger.kernel.org,
	 linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] staging: nvec: validate battery response length before memcpy
Date: Sun, 29 Mar 2026 22:48:24 +0200 (CEST)	[thread overview]
Message-ID: <b23eb8ca-ef38-6288-0891-fe91148e11cd@gmx.de> (raw)
In-Reply-To: <20260324195041.38343-1-sebasjosue84@gmail.com>

Hello Sebastian,

On Tue, 24 Mar 2026, Sebastian Josue Alba Vives wrote:

> In nvec_power_notifier(), the response length from the embedded
> controller is used directly as the size argument to memcpy() when
> copying battery manufacturer, model, and type strings. The
> destination buffers (bat_manu, bat_model, bat_type) are fixed at 30
> bytes, but res->length is a u8 that can be up to 255, allowing a
> heap buffer overflow.
>
> Additionally, if res->length is less than 2, the subtraction
> res->length - 2 wraps around as an unsigned value, resulting in a
> large copy that corrupts kernel heap memory.
>
> Add bounds checks before each memcpy to ensure the copy length does
> not exceed the destination buffer size, and that res->length is at
> least 2 to prevent unsigned integer underflow.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Sebastian Josue Alba Vives <sebasjosue84@gmail.com>
> ---
> drivers/staging/nvec/nvec_power.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_power.c
> index 2faab9fde..29beef0a7 100644
> --- a/drivers/staging/nvec/nvec_power.c
> +++ b/drivers/staging/nvec/nvec_power.c
> @@ -193,14 +193,20 @@ static int nvec_power_bat_notifier(struct notifier_block *nb,
> 		power->bat_temperature = res->plu - 2732;
> 		break;
> 	case MANUFACTURER:
> +		if (res->length < 2 || res->length - 2 > sizeof(power->bat_manu) - 1)
> +			break;

I think this check is a valid one, even if unlikely to fail. Could be a
bit nicer by replacing sizeof() with a contant and storing res->length-2 
to some variable, but I guess that's a matter of taste.

Tested-by: Marc Dietrich <marvin24@gmx.de>

Thanks,

Marc

> 		memcpy(power->bat_manu, &res->plc, res->length - 2);
> 		power->bat_manu[res->length - 2] = '\0';
> 		break;
> 	case MODEL:
> +		if (res->length < 2 || res->length - 2 > sizeof(power->bat_model) - 1)
> +			break;
> 		memcpy(power->bat_model, &res->plc, res->length - 2);
> 		power->bat_model[res->length - 2] = '\0';
> 		break;
> 	case TYPE:
> +		if (res->length < 2 || res->length - 2 > sizeof(power->bat_type) - 1)
> +			break;
> 		memcpy(power->bat_type, &res->plc, res->length - 2);
> 		power->bat_type[res->length - 2] = '\0';
> 		/*
> -- 
> 2.43.0
>
>

      reply	other threads:[~2026-03-29 20:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-24 19:50 [PATCH] staging: nvec: validate battery response length before memcpy Sebastian Josue Alba Vives
2026-03-29 20:48 ` Marc Dietrich [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=b23eb8ca-ef38-6288-0891-fe91148e11cd@gmx.de \
    --to=marvin24@gmx.de \
    --cc=ac100@lists.launchpad.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=sebasjosue84@gmail.com \
    --cc=stable@vger.kernel.org \
    /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