* [PATCH] staging: nvec: fix out-of-bounds string copies
[not found] <CAObkL7P+PnkEBpav6AVEPTyjiWsoOEBobUT2hrR=fay23FyO6w@mail.gmail.com>
@ 2026-07-30 4:12 ` Dang Vu Duc Hien
2026-07-30 7:35 ` Greg KH
0 siblings, 1 reply; 2+ messages in thread
From: Dang Vu Duc Hien @ 2026-07-30 4:12 UTC (permalink / raw)
To: marvin24, gregkh
Cc: ac100, linux-tegra, linux-staging, linux-kernel, luquijeffrey,
Dang Vu Duc Hien
The function `nvec_power_bat_notifier()` assumes that `res->length` is
both >= 2 and within the battery info buffers.
Technically, `res->length` is provided by the embedded controller (EC)
and is unverified (ie. a malformed response). If `res->length` < 2, the
expression `res->length - 2` evaluates to a massive size_t value which
is passed to `memcpy()`, causing severe memory corruption. Conversely,
`res->length - 2` being larger than the buffer also causes an
out-of-bounds write.
Introduce helper function `nvec_bat_str_copy()` to safely handle these
string copies by:
1. Ensuring that it returns an error to the upper layer along with a
`dev_warn()` message when `res->length` < 2.
2. Ensuring that it provides toleration by copying as much as it can
along with a `dev_warn()` when `res->length - 2` is too big.
3. Guaranteeing a properly sized, null-terminated destination string if
the copy succeeds.
Signed-off-by: Dang Vu Duc Hien <dvdh12707@gmail.com>
---
Note: Coincidentally, I have been working on this exact issue earlier
and had this patch prepared just before Lucas Jeffrey submitted his RFC.
I am sending it here to answer his protocol validation question with code.
---
drivers/staging/nvec/nvec_power.c | 41 ++++++++++++++++++++++++++-----
1 file changed, 35 insertions(+), 6 deletions(-)
diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_power.c
index c514d51a9e57..12699515356d 100644
--- a/drivers/staging/nvec/nvec_power.c
+++ b/drivers/staging/nvec/nvec_power.c
@@ -117,6 +117,32 @@ static void get_bat_mfg_data(struct nvec_power *power)
}
}
+static int nvec_bat_str_copy(struct nvec_power *power,
+ struct bat_response *res, char *dst,
+ unsigned int dst_size, const char *prop_name)
+{
+ unsigned int len;
+
+ if (res->length < 2) {
+ dev_warn(power->nvec->dev,
+ "battery %s response length too short (%u)\n",
+ prop_name, res->length);
+ return -EINVAL;
+ }
+
+ len = res->length - 2;
+ if (len >= dst_size) {
+ dev_warn(power->nvec->dev,
+ "battery %s string too long (%u)\n",
+ prop_name, len);
+ len = dst_size - 1;
+ }
+
+ memcpy(dst, &res->plc, len);
+ dst[len] = '\0';
+ return 0;
+}
+
static int nvec_power_bat_notifier(struct notifier_block *nb,
unsigned long event_type, void *data)
{
@@ -193,16 +219,19 @@ static int nvec_power_bat_notifier(struct notifier_block *nb,
power->bat_temperature = res->plu - 2732;
break;
case MANUFACTURER:
- memcpy(power->bat_manu, &res->plc, res->length - 2);
- power->bat_manu[res->length - 2] = '\0';
+ if (nvec_bat_str_copy(power, res, power->bat_manu,
+ sizeof(power->bat_manu), "manufacturer") < 0)
+ break;
break;
case MODEL:
- memcpy(power->bat_model, &res->plc, res->length - 2);
- power->bat_model[res->length - 2] = '\0';
+ if (nvec_bat_str_copy(power, res, power->bat_model,
+ sizeof(power->bat_model), "model") < 0)
+ break;
break;
case TYPE:
- memcpy(power->bat_type, &res->plc, res->length - 2);
- power->bat_type[res->length - 2] = '\0';
+ if (nvec_bat_str_copy(power, res, power->bat_type,
+ sizeof(power->bat_type), "type") < 0)
+ break;
/*
* This differs a little from the spec fill in more if you find
* some.
--
2.55.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] staging: nvec: fix out-of-bounds string copies
2026-07-30 4:12 ` [PATCH] staging: nvec: fix out-of-bounds string copies Dang Vu Duc Hien
@ 2026-07-30 7:35 ` Greg KH
0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-07-30 7:35 UTC (permalink / raw)
To: Dang Vu Duc Hien
Cc: marvin24, ac100, linux-tegra, linux-staging, linux-kernel, luquijeffrey
On Thu, Jul 30, 2026 at 11:12:20AM +0700, Dang Vu Duc Hien wrote:
> The function `nvec_power_bat_notifier()` assumes that `res->length` is
> both >= 2 and within the battery info buffers.
> Technically, `res->length` is provided by the embedded controller (EC)
> and is unverified (ie. a malformed response). If `res->length` < 2, the
> expression `res->length - 2` evaluates to a massive size_t value which
> is passed to `memcpy()`, causing severe memory corruption. Conversely,
> `res->length - 2` being larger than the buffer also causes an
> out-of-bounds write.
>
> Introduce helper function `nvec_bat_str_copy()` to safely handle these
> string copies by:
> 1. Ensuring that it returns an error to the upper layer along with a
> `dev_warn()` message when `res->length` < 2.
> 2. Ensuring that it provides toleration by copying as much as it can
> along with a `dev_warn()` when `res->length - 2` is too big.
> 3. Guaranteeing a properly sized, null-terminated destination string if
> the copy succeeds.
>
> Signed-off-by: Dang Vu Duc Hien <dvdh12707@gmail.com>
> ---
> Note: Coincidentally, I have been working on this exact issue earlier
> and had this patch prepared just before Lucas Jeffrey submitted his RFC.
> I am sending it here to answer his protocol validation question with code.
> ---
> drivers/staging/nvec/nvec_power.c | 41 ++++++++++++++++++++++++++-----
> 1 file changed, 35 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/staging/nvec/nvec_power.c b/drivers/staging/nvec/nvec_power.c
> index c514d51a9e57..12699515356d 100644
> --- a/drivers/staging/nvec/nvec_power.c
> +++ b/drivers/staging/nvec/nvec_power.c
> @@ -117,6 +117,32 @@ static void get_bat_mfg_data(struct nvec_power *power)
> }
> }
>
> +static int nvec_bat_str_copy(struct nvec_power *power,
> + struct bat_response *res, char *dst,
> + unsigned int dst_size, const char *prop_name)
> +{
> + unsigned int len;
> +
> + if (res->length < 2) {
> + dev_warn(power->nvec->dev,
> + "battery %s response length too short (%u)\n",
> + prop_name, res->length);
So you are allowing a malicious device to spam the kernel log? If you
really worry about malicious devices, this isn't a good idea :)
But this is a platform device, built into the hardware, so you have to
assume that it is NOT malicious and we can trust it, as we trust all
platform devices these days. If we wish to change that model, then we
need to do more work on Linux than just stuff like this.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-30 7:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CAObkL7P+PnkEBpav6AVEPTyjiWsoOEBobUT2hrR=fay23FyO6w@mail.gmail.com>
2026-07-30 4:12 ` [PATCH] staging: nvec: fix out-of-bounds string copies Dang Vu Duc Hien
2026-07-30 7:35 ` Greg KH
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®