Skip to content

Commit 9d250ab

Browse files
committed
gpu: nova-core: gsp: fix length of received messages
The size of messages' payload is miscalculated, leading to extra data passed to the message handler. While this is not a problem with our current set of commands, others with a variable-length payload may misbehave. Fix this by introducing a method returning the payload size and using it. Fixes: 75f6b1d ("gpu: nova-core: gsp: Add GSP command queue bindings and handling") Reviewed-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com> Reviewed-by: Alistair Popple <apopple@nvidia.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20251216-nova-fixes-v3-2-c7469a71f7c4@nvidia.com [acourbot@nvidia.com: update `PANIC:` comments as pointed out by Joel.] Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
1 parent b6c7651 commit 9d250ab

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

drivers/gpu/nova-core/gsp/cmdq.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,21 +588,23 @@ impl Cmdq {
588588
header.length(),
589589
);
590590

591+
let payload_length = header.payload_length();
592+
591593
// Check that the driver read area is large enough for the message.
592-
if slice_1.len() + slice_2.len() < header.length() {
594+
if slice_1.len() + slice_2.len() < payload_length {
593595
return Err(EIO);
594596
}
595597

596598
// Cut the message slices down to the actual length of the message.
597-
let (slice_1, slice_2) = if slice_1.len() > header.length() {
598-
// PANIC: we checked above that `slice_1` is at least as long as `msg_header.length()`.
599-
(slice_1.split_at(header.length()).0, &slice_2[0..0])
599+
let (slice_1, slice_2) = if slice_1.len() > payload_length {
600+
// PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
601+
(slice_1.split_at(payload_length).0, &slice_2[0..0])
600602
} else {
601603
(
602604
slice_1,
603605
// PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
604-
// large as `msg_header.length()`.
605-
slice_2.split_at(header.length() - slice_1.len()).0,
606+
// large as `payload_length`.
607+
slice_2.split_at(payload_length - slice_1.len()).0,
606608
)
607609
};
608610

drivers/gpu/nova-core/gsp/fw.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,16 @@ impl GspMsgElement {
853853
self.inner.checkSum = checksum;
854854
}
855855

856-
/// Returns the total length of the message.
856+
/// Returns the length of the message's payload.
857+
pub(crate) fn payload_length(&self) -> usize {
858+
// `rpc.length` includes the length of the RPC message header.
859+
num::u32_as_usize(self.inner.rpc.length)
860+
.saturating_sub(size_of::<bindings::rpc_message_header_v>())
861+
}
862+
863+
/// Returns the total length of the message, message and RPC headers included.
857864
pub(crate) fn length(&self) -> usize {
858-
// `rpc.length` includes the length of the GspRpcHeader but not the message header.
859-
size_of::<Self>() - size_of::<bindings::rpc_message_header_v>()
860-
+ num::u32_as_usize(self.inner.rpc.length)
865+
size_of::<Self>() + self.payload_length()
861866
}
862867

863868
// Returns the sequence number of the message.

0 commit comments

Comments
 (0)