Skip to contentSkip to conversation

staging: nvec: possible missing bounds check in nvec_power_bat response handling

RFC In review Lucas Jeffrey 3 participants 3 messages Jul 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hello Marc, Greg,
While reviewing drivers/staging/nvec/nvec_power.c I noticed that the battery response handler uses the length field from struct bat_response without validating it against the destination buffer size:
nvec_power.c between lines 120 and 220:

static int nvec_power_bat_notifier(struct notifier_block *nb, unsigned long event_type, void *data)
{
struct nvec_power *power =
container_of(nb, struct nvec_power, notifier); struct bat_response *res = data;
(...)

switch (res->sub_type) { (...)
case TYPE:
memcpy(power->bat_type, &res->plc, res->length - 2); //res->length is never validated that is in a valid range, //according to the plc field in the bat_response struct it should be smaller than 30 (...)

}

return NOTIFY_STOP;
}

The same patterns patterns exist for MANUFACTURER and MODEL. My understanding is that res->length comes from the NVEC embedded controller response. If a malformed response contains a length larger than the destination buffer, this could result in an out-of-bounds write.
I would like to confirm whether the NVEC protocol guarantees these length values, or whether the driver should validate them before copying.

Thanks, Lucas

#1 lore raw

PATCH 1/1 staging: nvec: fix out-of-bounds string copies

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.

---
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(-)
drivers/staging/nvec/nvec_power.c +35 -6
index c514d51a9e57..12699515356d 100644
@@ -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.
Signature · 1 line
2.55.0
maintainer
#2 lore raw

Re: [PATCH] staging: nvec: fix out-of-bounds string copies

Quoted 35 lines · 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.

---
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(-)
drivers/staging/nvec/nvec_power.c +0 -0
index c514d51a9e57..12699515356d 100644
@@ -117,6 +117,32 @@ static void get_bat_mfg_data(struct nvec_power *power)
 	}
 }
 

drivers/staging/nvec/nvec_power.c static void get_bat_mfg_data(struct nvec_power *power) · line 129

Quoted 10 lines
drivers/staging/nvec/nvec_power.c +10 -0
+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