Skip to contentSkip to conversation

platform/chrome: wilco_ec: Reject out-of-bounds property length

PATCH In review Maoyi Xie 3 participants 3 messages Nov 18 – Nov 20

Subsystems (3)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/1 platform/chrome: wilco_ec: Reject out-of-bounds property length

wilco_ec_get_property() copies rs.length bytes from rs.data into prop_msg->data without checking rs.length, which comes from the EC. Both buffers hold WILCO_EC_PROPERTY_MAX_SIZE (4) bytes, so a larger length reads past rs on the stack and writes past the caller's message.

Return -EBADMSG for such a length, as the file does for other bad responses.

Found with a CodeQL-based static analysis tool that we are developing. I used an LLM to help write this patch and a userspace harness. The harness runs the unmodified properties.c against a model of the EC under AddressSanitizer. It reports the copy for lengths 5 to 255, and nothing with this patch. Not tested on hardware, only compile-tested with W=1.

Fixes 0c0b7ea23aed ("platform/chrome: wilco_ec: Add property helper library")
---
 drivers/platform/chrome/wilco_ec/properties.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/chrome/wilco_ec/properties.c +2 -0
index 9951c8db04da065..4ca7e874804c28e 100644
@@ -70,6 +70,8 @@ int wilco_ec_get_property(struct wilco_ec_device *ec,
 	ret = send_property_msg(ec, &rq, &rs);
 	if (ret < 0)
 		return ret;
+	if (rs.length > WILCO_EC_PROPERTY_MAX_SIZE)
+		return -EBADMSG;
 
 	prop_msg->length = rs.length;
 	memcpy(prop_msg->data, rs.data, rs.length);

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea

Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Maoyi,

Quoted 15 lines · Maoyi Xie

wilco_ec_get_property() copies rs.length bytes from rs.data into prop_msg->data without checking rs.length, which comes from the EC. Both buffers hold WILCO_EC_PROPERTY_MAX_SIZE (4) bytes, so a larger length reads past rs on the stack and writes past the caller's message.

Return -EBADMSG for such a length, as the file does for other bad responses.

Found with a CodeQL-based static analysis tool that we are developing. I used an LLM to help write this patch and a userspace harness. The harness runs the unmodified properties.c against a model of the EC under AddressSanitizer. It reports the copy for lengths 5 to 255, and nothing with this patch. Not tested on hardware, only compile-tested with W=1.

Quoted 3 lines
Fixes 0c0b7ea23aed ("platform/chrome: wilco_ec: Add property helper library")

Thanks,
Benson

Quoted 22 lines
---
 drivers/platform/chrome/wilco_ec/properties.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/chrome/wilco_ec/properties.c +2 -0
index 9951c8db04da065..4ca7e874804c28e 100644
@@ -70,6 +70,8 @@ int wilco_ec_get_property(struct wilco_ec_device *ec,
 	ret = send_property_msg(ec, &rq, &rs);
 	if (ret < 0)
 		return ret;
+	if (rs.length > WILCO_EC_PROPERTY_MAX_SIZE)
+		return -EBADMSG;
 
 	prop_msg->length = rs.length;
 	memcpy(prop_msg->data, rs.data, rs.length);

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea

Signature · 1 line
2.34.1
maintainer
#2 lore raw
In reply to Maoyi Xie
Quoted 9 lines · Maoyi Xie

wilco_ec_get_property() copies rs.length bytes from rs.data into prop_msg->data without checking rs.length, which comes from the EC. Both buffers hold WILCO_EC_PROPERTY_MAX_SIZE (4) bytes, so a larger length reads past rs on the stack and writes past the caller's message.

Return -EBADMSG for such a length, as the file does for other bad responses.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux.git for-next

[1/1] platform/chrome: wilco_ec: Reject out-of-bounds property length

      commit: 1491040b5f396b23021f2905d18599e2e0bd5908

Thanks!