If the device sends a CAN error frame and the SKB allocation fails, the CAN state is not updated. To avoid losing CAN state updates, analyze CAN error frames before allocating the skb. Update gs_update_state() to work on a struct gs_host_frame instead of a struct can_frame. Check length of received struct gs_host_frame::data before accessing it. Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices") Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261130-gs_usb-fixes-v12-2-9ba1f84c179a@pengutronix.de Signed-off-by: Marc Kleine-Budde --- drivers/net/can/usb/gs_usb.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c index 965151ab50a1..996029853235 100644 --- a/drivers/net/can/usb/gs_usb.c +++ b/drivers/net/can/usb/gs_usb.c @@ -495,23 +495,24 @@ static void gs_usb_timestamp_stop(struct gs_usb *parent) cancel_delayed_work_sync(&parent->timestamp); } -static void gs_update_state(struct gs_can *dev, struct can_frame *cf) +static void gs_update_state(struct gs_can *dev, const struct gs_host_frame *hf, + unsigned int data_length) { struct can_device_stats *can_stats = &dev->can.can_stats; - if (cf->can_id & CAN_ERR_RESTARTED) { + if (hf->can_id & cpu_to_le32(CAN_ERR_RESTARTED)) { dev->can.state = CAN_STATE_ERROR_ACTIVE; can_stats->restarts++; - } else if (cf->can_id & CAN_ERR_BUSOFF) { + } else if (hf->can_id & cpu_to_le32(CAN_ERR_BUSOFF)) { dev->can.state = CAN_STATE_BUS_OFF; can_stats->bus_off++; - } else if (cf->can_id & CAN_ERR_CRTL) { - if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || - (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { + } else if (hf->can_id & cpu_to_le32(CAN_ERR_CRTL) && data_length > 1) { + if ((hf->classic_can->data[1] & CAN_ERR_CRTL_TX_WARNING) || + (hf->classic_can->data[1] & CAN_ERR_CRTL_RX_WARNING)) { dev->can.state = CAN_STATE_ERROR_WARNING; can_stats->error_warning++; - } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || - (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { + } else if ((hf->classic_can->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || + (hf->classic_can->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { dev->can.state = CAN_STATE_ERROR_PASSIVE; can_stats->error_passive++; } else { @@ -685,6 +686,10 @@ static void gs_usb_receive_bulk_callback(struct urb *urb) memcpy(cfd->data, hf->canfd->data, data_length); } else { + /* ERROR frames tell us information about the controller */ + if (hf->can_id & cpu_to_le32(CAN_ERR_FLAG)) + gs_update_state(dev, hf, data_length); + skb = alloc_can_skb(netdev, &cf); if (!skb) goto resubmit_urb; @@ -693,10 +698,6 @@ static void gs_usb_receive_bulk_callback(struct urb *urb) can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode); memcpy(cf->data, hf->classic_can->data, data_length); - - /* ERROR frames tell us information about the controller */ - if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG) - gs_update_state(dev, cf); } gs_usb_rx_offload(dev, skb, hf); -- 2.53.0