staging: vc04_services: vchiq-mmal: fix multiple memory safety issues
cover staging: vc04_services: vchiq-mmal: fix multiple memory safety issues
This series fixes four memory safety issues in the MMAL VCHIQ driver which handles multimedia message passing between the ARM CPU and the VideoCore GPU on all Raspberry Pi models.
Patch 1: OOB array access in event_to_host_cb() Patch 2: Missing buffer size check in inline_receive() Patch 3: Missing bounds check in port_parameter_set() Patch 4: Integer underflow in port_parameter_get()
All four issues affect both ARM32 and ARM64 Raspberry Pi kernels. Found through manual source code auditing.
I would like to request separate CVE assignments for each independently exploitable vulnerability.
PATCH 1/4 staging: vc04_services: vchiq-mmal: fix OOB array access in event_to_host_cb()
From: Sebastián Alba Vives <[email protected]>
event_to_host_cb() uses msg->u.event_to_host.client_component as an index into the instance->component[] array (size VCHIQ_MMAL_MAX_COMPONENTS = 64) without any bounds validation. The client_component value comes from the VideoCore GPU firmware via VCHIQ message passing.
A malicious or buggy GPU firmware could send a crafted MMAL_MSG_TYPE_EVENT_TO_HOST message with client_component >= 64 (or negative), causing an out-of-bounds array access in kernel memory. This results in reading/dereferencing a bogus vchiq_mmal_component structure from memory beyond the array, which can lead to kernel crashes or potentially arbitrary kernel memory access.
Add a bounds check on comp_idx before using it as an array index. Move the component pointer assignment after the validation.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +9 -2
@@ -477,12 +477,19 @@ static void event_to_host_cb(struct vchiq_mmal_instance *instance, struct mmal_msg *msg, u32 msg_len) { int comp_idx = msg->u.event_to_host.client_component; -struct vchiq_mmal_component *component = - &instance->component[comp_idx]; +struct vchiq_mmal_component *component;struct vchiq_mmal_port *port =NULL ;struct mmal_msg_context *msg_context;u32 port_num = msg->u.event_to_host.port_num; +if (comp_idx <0 || comp_idx >=VCHIQ_MMAL_MAX_COMPONENTS ) { +pr_err ("%s: component index %d out of range\n" , + __func__, comp_idx); +return ; + } + + component = &instance->component[comp_idx]; +if (msg->u.buffer_from_host.drvbuf.magic ==MMAL_MAGIC ) {pr_err ("%s: MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n" , __func__);
Signature · 1 line
2.43.0
Greg Kroah-Hartman
maintainerRe: [PATCH 1/4] staging: vc04_services: vchiq-mmal: fix OOB array access in event_to_host_cb()
Quoted 7 lines · Sebastian Josue Alba Vives
From: Sebastián Alba Vives <[email protected]>
event_to_host_cb()usesmsg->u.event_to_host.client_componentas an index into the instance->component[] array (sizeVCHIQ_MMAL_MAX_COMPONENTS= 64) without any bounds validation. Theclient_componentvalue comes from the VideoCore GPU firmware via VCHIQ message passing.
Quoted 6 lines
A malicious or buggy GPU firmware could send a crafted
MMAL_MSG_TYPE_EVENT_TO_HOSTmessage withclient_component>= 64 (or negative), causing an out-of-bounds array access in kernel memory. This results in reading/dereferencing a bogusvchiq_mmal_componentstructure from memory beyond the array, which can lead to kernel crashes or potentially arbitrary kernel memory access.
The kernel trusts the hardware the driver is bound to, so this shouldn't be happening ever, right?
Quoted 6 lines
Add a bounds check on
comp_idxbefore using it as an array index. Move the component pointer assignment after the validation.Fixes b18ee53ad297 ("staging: bcm2835: Break MMAL support out from camera")Signed-off-by Sebastián Alba Vives <[email protected]>
No cc: stable?
Quoted 12 lines
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +0
-0@@ -477,12 +477,19 @@ static void event_to_host_cb(struct vchiq_mmal_instance *instance, struct mmal_msg *msg, u32 msg_len) {int comp_idx = msg->u.event_to_host.client_component;
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c static void event_to_host_cb(struct vchiq_mmal_instance *instance, · line 487
Quoted 10 lines
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +4
-2-struct vchiq_mmal_component *component = - &instance->component[comp_idx]; +struct vchiq_mmal_component *component;struct vchiq_mmal_port *port =NULL ;struct mmal_msg_context *msg_context;u32 port_num = msg->u.event_to_host.port_num; +if (comp_idx <0 || comp_idx >=VCHIQ_MMAL_MAX_COMPONENTS ) { +pr_err ("%s: component index %d out of range\n" , + __func__, comp_idx);
dev_err() is best, right?
And are you going to allow a malicious hardware device to spam the kernel log? :)
thanks,
greg k-h
Re: [PATCH 1/4] staging: vc04_services: vchiq-mmal: fix OOB array access in event_to_host_cb()
Hi Greg,
Quoted 2 lines
The kernel trusts the hardware the driver is bound to, so this shouldn't be happening ever, right?
You're right that the kernel generally trusts hardware it's bound to. This is a defensive hardening patch - a malformed index from buggy firmware would currently cause an uncontrolled OOB access, while with the check we get a clean error return. Happy to reframe the commit message as hardening rather than a security fix if you prefer.
Quoted 1 line
No cc: stable?
Will add Cc: [email protected] in v2.
Quoted 1 line
dev_err()is best, right?
Agreed, will switch to dev_err() in v2. I'll need to thread the struct device through - will check how other functions in this file handle it.
Quoted 2 lines
And are you going to allow a malicious hardware device to spam the kernel log? :)
Good point, will switch to dev_err_ratelimited() in v2.
I'll send a v2 addressing all of these. Thanks for the review.
Sebastián
Quoted 59 lines · Greg Kroah-Hartman
Quoted 7 lines · Sebastian Josue Alba Vives
From: Sebastián Alba Vives <[email protected]>
event_to_host_cb()usesmsg->u.event_to_host.client_componentas an index into the instance->component[] array (sizeVCHIQ_MMAL_MAX_COMPONENTS= 64) without any bounds validation. Theclient_componentvalue comes from the VideoCore GPU firmware via VCHIQ message passing.Quoted 6 lines
A malicious or buggy GPU firmware could send a crafted
MMAL_MSG_TYPE_EVENT_TO_HOSTmessage withclient_component>= 64 (or negative), causing an out-of-bounds array access in kernel memory. This results in reading/dereferencing a bogusvchiq_mmal_componentstructure from memory beyond the array, which can lead to kernel crashes or potentially arbitrary kernel memory access.The kernel trusts the hardware the driver is bound to, so this shouldn't be happening ever, right?
Quoted 6 lines
Add a bounds check on
comp_idxbefore using it as an array index. Move the component pointer assignment after the validation.Fixes b18ee53ad297 ("staging: bcm2835: Break MMAL support out from camera")Signed-off-by Sebastián Alba Vives <[email protected]>No cc: stable?
Quoted 12 lines
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +0
-0@@ -477,12 +477,19 @@ static void event_to_host_cb(struct vchiq_mmal_instance *instance, struct mmal_msg *msg, u32 msg_len) {int comp_idx = msg->u.event_to_host.client_component;drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c static void event_to_host_cb(struct vchiq_mmal_instance *instance, · line 487
Quoted 10 lines
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +4
-2-struct vchiq_mmal_component *component = - &instance->component[comp_idx]; +struct vchiq_mmal_component *component;struct vchiq_mmal_port *port =NULL ;struct mmal_msg_context *msg_context;u32 port_num = msg->u.event_to_host.port_num; +if (comp_idx <0 || comp_idx >=VCHIQ_MMAL_MAX_COMPONENTS ) { +pr_err ("%s: component index %d out of range\n" , + __func__, comp_idx);
dev_err()is best, right?And are you going to allow a malicious hardware device to spam the kernel log? :)
thanks,
greg k-h
Signature · 1 line
Sebastián Alba
PATCH 2/4 staging: vc04_services: vchiq-mmal: add buffer size check in inline_receive()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
inline_receive() copies payload data from a VCHIQ message into a destination buffer using payload_in_message as the copy length, but never validates that this length fits within the destination buffer (msg_context->u.bulk.buffer->buffer_size).
While the caller validates payload_in_message <= MMAL_VC_SHORT_DATA
(128) to prevent overreading the source, the destination buffer may be smaller than 128 bytes. This is inconsistent with bulk_receive() which does check buffer_size before copying. A VideoCore GPU sending a short inline payload to a smaller destination buffer would cause a heap buffer overflow in kernel memory.
Add a bounds check against buffer_size and truncate the copy length if it exceeds the destination capacity, matching the defensive pattern used in bulk_receive().
--- .../vc04_services/vchiq-mmal/mmal-vchiq.c | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +17 -3
@@ -368,12 +368,26 @@ static int inline_receive(struct vchiq_mmal_instance *instance,struct mmal_msg *msg, struct mmal_msg_context *msg_context) { + u32 payload_len = msg->u.buffer_from_host.payload_in_message; + +/* +* Ensure the payload fits within the destination buffer. +* The caller already validates payload_len <= MMAL_VC_SHORT_DATA +* against the source, but the destination buffer may be smaller. +* bulk_receive() performs this check; inline_receive() must too. +*/ +if (payload_len > msg_context->u.bulk.buffer->buffer_size) { + payload_len = msg_context->u.bulk.buffer->buffer_size; +pr_warn ("inline_receive: payload truncated (%u > %lu)\n" , + msg->u.buffer_from_host.payload_in_message, + msg_context->u.bulk.buffer->buffer_size); + } +memcpy (msg_context->u.bulk.buffer->buffer, msg->u.buffer_from_host.short_data, - msg->u.buffer_from_host.payload_in_message); + payload_len); - msg_context->u.bulk.buffer_used = - msg->u.buffer_from_host.payload_in_message; + msg_context->u.bulk.buffer_used = payload_len;return 0 ; }
Signature · 1 line
2.43.0
PATCH 3/4 staging: vc04_services: vchiq-mmal: prevent stack overflow in port_parameter_set()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
port_parameter_set() copies value_size bytes from the caller-supplied value buffer into the stack-allocated struct mmal_msg's port_parameter_set.value field, which is u32[96] (384 bytes). There is no bounds check on value_size before the memcpy.
While current in-tree callers pass small fixed-size structures, the function is exported via EXPORT_SYMBOL_GPL and accessible to any GPL kernel module. A caller passing value_size > 384 would overflow the stack-allocated mmal_msg structure, potentially leading to stack corruption and code execution.
Add a bounds check rejecting value_size larger than the value field.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +8 -0
@@ -1361,6 +1361,14 @@ static int port_parameter_set(struct vchiq_mmal_instance *instance,struct mmal_msg *rmsg;struct vchiq_header *rmsg_handle; +if (value_size > +sizeof (m.u.port_parameter_set.value)) { +pr_err ("port_parameter_set: value_size %u exceeds max %zu\n" , + value_size, +sizeof (m.u.port_parameter_set.value)); +return -EINVAL ; + } + m.h.type =MMAL_MSG_TYPE_PORT_PARAMETER_SET ; m.u.port_parameter_set.component_handle = port->component->handle;
Signature · 1 line
2.43.0
PATCH 4/4 staging: vc04_services: vchiq-mmal: fix integer underflow in port_parameter_get()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
port_parameter_get() subtracts 2 * sizeof(u32) from the GPU firmware's reply size field to compute the parameter value size. The reply size is a u32 provided by the VideoCore firmware. If the firmware returns a size smaller than 8, the subtraction wraps around to a large value due to unsigned integer underflow.
The underflowed size is then:
1) Used in a comparison that selects the wrong copy path
2) Stored back to the caller via *value_size, propagating a bogus size (up to ~4GB) to subsequent operations
Add a minimum size check before the subtraction and return -EPROTO if the reply is malformed.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +4 -0
@@ -1436,6 +1436,10 @@ static int port_parameter_get(struct vchiq_mmal_instance *instance,/* port_parameter_get_reply.size includes the header, * whilst *value_size doesn't. */ +if (rmsg->u.port_parameter_get_reply.size < (2 *sizeof (u32))) { + ret = -EPROTO ; + goto release_msg; + } rmsg->u.port_parameter_get_reply.size -= (2 *sizeof (u32));if (ret || rmsg->u.port_parameter_get_reply.size > *value_size) {
Signature · 1 line
2.43.0
v2 cover staging: vc04_services: vchiq-mmal: fix multiple memory safety issues
In reply to Sebastian Josue Alba VivesChanges since v1:
- Reframed patch 1 as hardening rather than security fix (Greg)
- Switched pr_err/pr_warn to ratelimited variants (Greg)
- Added Cc: stable to all patches (Greg)
- Note: dev_err() not used as this file has no struct device access
This series adds defensive bounds checks to the MMAL VCHIQ driver which handles multimedia message passing between the ARM CPU and the VideoCore GPU on all Raspberry Pi models.
PATCH v2 1/4 staging: vc04_services: vchiq-mmal: validate component index in event_to_host_cb()
From: Sebastián Alba Vives <[email protected]>
event_to_host_cb() uses msg->u.event_to_host.client_component as an index into the instance->component[] array (size VCHIQ_MMAL_MAX_COMPONENTS = 64) without bounds validation. While the kernel generally trusts the hardware it is bound to, a bounds check here hardens the driver against potential firmware bugs that could otherwise cause an uncontrolled out-of-bounds array access and kernel crash.
Add a bounds check on comp_idx before using it as an array index and move the component pointer assignment after the validation. Use pr_err_ratelimited() to avoid log flooding. Note: this file does not currently have access to a struct device, so dev_err() is not available.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +9 -2
@@ -477,12 +477,19 @@ static void event_to_host_cb(struct vchiq_mmal_instance *instance, struct mmal_msg *msg, u32 msg_len) { int comp_idx = msg->u.event_to_host.client_component; -struct vchiq_mmal_component *component = - &instance->component[comp_idx]; +struct vchiq_mmal_component *component;struct vchiq_mmal_port *port =NULL ;struct mmal_msg_context *msg_context;u32 port_num = msg->u.event_to_host.port_num; +if (comp_idx <0 || comp_idx >=VCHIQ_MMAL_MAX_COMPONENTS ) { +pr_err_ratelimited ("%s: component index %d out of range\n" , + __func__, comp_idx); +return ; + } + + component = &instance->component[comp_idx]; +if (msg->u.buffer_from_host.drvbuf.magic ==MMAL_MAGIC ) {pr_err ("%s: MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n" , __func__);
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] staging: vc04_services: vchiq-mmal: validate component index in event_to_host_cb()
Quoted 9 lines · Sebastian Josue Alba Vives
From: Sebastián Alba Vives <[email protected]>
event_to_host_cb()usesmsg->u.event_to_host.client_componentas an index into the instance->component[] array (sizeVCHIQ_MMAL_MAX_COMPONENTS= 64) without bounds validation. While the kernel generally trusts the hardware it is bound to, a bounds check here hardens the driver against potential firmware bugs that could otherwise cause an uncontrolled out-of-bounds array access and kernel crash.
Quoted 7 lines
Add a bounds check on
comp_idxbefore using it as an array index and move the component pointer assignment after the validation. Usepr_err_ratelimited()to avoid log flooding. Note: this file does not currently have access to a struct device, sodev_err()is not available.Fixes b18ee53ad297 ("staging: bcm2835: Break MMAL support out from camera")
This fixes tag is wrong. That patch just moves code around.
I can't apply this patch to linux-next. Is this another out of tree bug?
regards,
dan carpenter
PATCH v2 2/4 staging: vc04_services: vchiq-mmal: add buffer size check in inline_receive()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
inline_receive() copies payload data from a VCHIQ message into a destination buffer using payload_in_message as the copy length, but never validates that this length fits within the destination buffer (msg_context->u.bulk.buffer->buffer_size).
While the caller validates payload_in_message <= MMAL_VC_SHORT_DATA
(128) to prevent overreading the source, the destination buffer may be smaller than 128 bytes. This is inconsistent with bulk_receive() which does check buffer_size before copying.
Add a bounds check against buffer_size and truncate the copy length if it exceeds the destination capacity, matching the defensive pattern used in bulk_receive(). Use pr_warn_ratelimited() for the truncation warning.
--- .../vc04_services/vchiq-mmal/mmal-vchiq.c | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +17 -3
@@ -368,12 +368,26 @@ static int inline_receive(struct vchiq_mmal_instance *instance,struct mmal_msg *msg, struct mmal_msg_context *msg_context) { + u32 payload_len = msg->u.buffer_from_host.payload_in_message; + +/* +* Ensure the payload fits within the destination buffer. +* The caller already validates payload_len <= MMAL_VC_SHORT_DATA +* against the source, but the destination buffer may be smaller. +* bulk_receive() performs this check; inline_receive() must too. +*/ +if (payload_len > msg_context->u.bulk.buffer->buffer_size) { + payload_len = msg_context->u.bulk.buffer->buffer_size; +pr_warn_ratelimited ("inline_receive: payload truncated (%u > %lu)\n" , + msg->u.buffer_from_host.payload_in_message, + msg_context->u.bulk.buffer->buffer_size); + } +memcpy (msg_context->u.bulk.buffer->buffer, msg->u.buffer_from_host.short_data, - msg->u.buffer_from_host.payload_in_message); + payload_len); - msg_context->u.bulk.buffer_used = - msg->u.buffer_from_host.payload_in_message; + msg_context->u.bulk.buffer_used = payload_len;return 0 ; }
Signature · 1 line
2.43.0
PATCH v2 3/4 staging: vc04_services: vchiq-mmal: prevent stack overflow in port_parameter_set()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
port_parameter_set() copies value_size bytes from the caller-supplied value buffer into the stack-allocated struct mmal_msg's port_parameter_set.value field, which is u32[96] (384 bytes). There is no bounds check on value_size before the memcpy.
While current in-tree callers pass small fixed-size structures, the function is exported via EXPORT_SYMBOL_GPL and accessible to any GPL kernel module. A caller passing value_size > 384 would overflow the stack-allocated mmal_msg structure.
Add a bounds check rejecting value_size larger than the value field.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +8 -0
@@ -1361,6 +1361,14 @@ static int port_parameter_set(struct vchiq_mmal_instance *instance,struct mmal_msg *rmsg;struct vchiq_header *rmsg_handle; +if (value_size > +sizeof (m.u.port_parameter_set.value)) { +pr_err_ratelimited ("port_parameter_set: value_size %u exceeds max %zu\n" , + value_size, +sizeof (m.u.port_parameter_set.value)); +return -EINVAL ; + } + m.h.type =MMAL_MSG_TYPE_PORT_PARAMETER_SET ; m.u.port_parameter_set.component_handle = port->component->handle;
Signature · 1 line
2.43.0
PATCH v2 4/4 staging: vc04_services: vchiq-mmal: fix integer underflow in port_parameter_get()
In reply to Sebastian Josue Alba VivesFrom: Sebastián Alba Vives <[email protected]>
port_parameter_get() subtracts 2 * sizeof(u32) from the VideoCore firmware's reply size field to compute the parameter value size. If the firmware returns a size smaller than 8, the subtraction wraps around to a large value due to unsigned integer underflow.
The underflowed size is then used in a comparison that selects the wrong copy path and stored back to the caller via *value_size, propagating a bogus size to subsequent operations.
Add a minimum size check before the subtraction and return -EPROTO if the reply is malformed.
--- drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +4 -0
@@ -1436,6 +1436,10 @@ static int port_parameter_get(struct vchiq_mmal_instance *instance,/* port_parameter_get_reply.size includes the header, * whilst *value_size doesn't. */ +if (rmsg->u.port_parameter_get_reply.size < (2 *sizeof (u32))) { + ret = -EPROTO ; + goto release_msg; + } rmsg->u.port_parameter_get_reply.size -= (2 *sizeof (u32));if (ret || rmsg->u.port_parameter_get_reply.size > *value_size) {
Signature · 1 line
2.43.0