diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c index 190d4808ffd90a..d1dc509b9b223a 100644 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c @@ -369,12 +369,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; } @@ -478,12 +492,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__); @@ -1342,6 +1363,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; @@ -1409,6 +1438,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) {