Skip to content

Commit 997d1e3

Browse files
committed
fix(nebula_core_hw_interfaces): add bounds checking for CAN FD frame reception
Signed-off-by: David Wong <david.wong@tier4.jp>
1 parent bd8c595 commit 997d1e3

File tree

1 file changed

+15
-2
lines changed
  • src/nebula_core/nebula_core_hw_interfaces/include/nebula_core_hw_interfaces/nebula_hw_interfaces_common/connections

1 file changed

+15
-2
lines changed

src/nebula_core/nebula_core_hw_interfaces/include/nebula_core_hw_interfaces/nebula_hw_interfaces_common/connections/can.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,21 @@ class CanSocket
255255
if (status == -1) throw SocketError(errno);
256256
if (status == 0) return false;
257257

258-
ssize_t recv_result = receive_frame_with_metadata(&frame, sizeof(frame), metadata);
259-
if (static_cast<size_t>(recv_result) < sizeof(frame)) {
258+
size_t recv_result = receive_frame_with_metadata(&frame, sizeof(frame), metadata);
259+
260+
if (recv_result < sizeof(frame.can_id) + sizeof(frame.len)) {
261+
throw SocketError("Corrupted CAN frame received");
262+
}
263+
264+
if (frame.len > CANFD_MAX_DLEN) {
265+
throw SocketError("Frame length is larger than max allowed CAN FD payload length");
266+
}
267+
268+
const auto data_length = static_cast<size_t>(frame.len);
269+
// some CAN FD frames are shorter than 64 bytes
270+
const auto expected_length = sizeof(frame) - sizeof(frame.data) + data_length;
271+
272+
if (recv_result < expected_length) {
260273
throw SocketError("Incomplete CAN FD frame received");
261274
}
262275
return true;

0 commit comments

Comments
 (0)