From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796253020; b=LeZQHNgSsGzKr2upQ2iBl12ixc1v0AtyzOzDq1bc7UXk6CiUEJFINrDKxibsvvUJW/Wy YCgBsHvEHVxNNpOtI2rtKLdQtTJMbjr8KkyNFdOnnkHm7NEP+5n3qjioD4M/teW3RTgAl KZnROvjdpc6JqgqIWB6oerVKrg+3iiXTbMWfBCwn6a/EIMRDj3JJYFTxGYpO+C+3lxd6n WNS4gQe3OXbAB7+9Lso5Nj/nIiaaV8TOATLZ5UVc18NZh52jcURV1qe+8C3WhQjgRN+Mn sRy6eI7DGx9+BzJkZw4PssFVsFY2jLnYCUyTEbXqBa/wGO1O8ejMKL2MB1ImVicTpcQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796253020; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=0kKzJ52bouFlTWNxN7p/vcAIQ8zGEFrnn6ugbjvkeG8=; b=13VOEBDqEn54+cFOclicxwxzNO02YAWw/g1T1A7MVaNXBeKq/rKmTZC3q5dFvmevRVW4 G2pfcU7Ce/5L1cEttjR4/QESggRpg5NxTF3F7cNkxvJ3Yn/VMjV+nTg/isH08Ce4yNZIm qVyCtQccXfFBNtJAUzFljF+HFLZMGdViTGjROk44t96QZKuuAMTLaTp4fe6qDQ7CEriHE 6g/WKuwGeDSPSoy2IyUta2fXKreuJwdn1wkXNW7NpfNYxCgmGiuYYg6MPDR2fDCquk0S5 Y4UkicjC4r1jcdbjxCUl79AJg6JQDGa2D/CrkJAR5qIkH+8mx64g3o7rgtvO39HWm4w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=J60EUBRe; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B030A606A32D; Wed, 2 Dec 2026 23:10:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B030A606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=J60EUBRe Received: from pengutronix.de (unknown [IPv6:2a0e:1580:1000::2dff:fe0e:e982]) (Authenticated sender: mkl@pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id B9216205BD6; Thu, 03 Dec 2026 00:10:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796253013; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0kKzJ52bouFlTWNxN7p/vcAIQ8zGEFrnn6ugbjvkeG8=; b=J60EUBRev6TZujhIuXivj6/SRCe1HhRPQdJSQ8gw8gJppFGX+P2C7qD/Zzj3OTPjXcFdm+ l5gfZkk46h5zZ9r8R6SWarJjFipQ2QO56bQbP7WjpAlx4xahr8N3VL5je2eUjyexTmrzbR x1YwPXDYxqatGd7M8em4FoBveHU+9iN35lI8+1VWIl9Hu4Z3vvPB2aUDSiIDfytQwtao3k JDejT81a6c4zoGWr76c/KXhhOejcCleO5XSEPlXXXQ6pdTTca4CAfsHjbb/IGsikED/cAL Z5ON+qre3fqd4yPyyWxF14drvibbS4at/CdfnRs9lsfXjwiuHZY1B+lKuJmc2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de; s=20260414; t=1796253013; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0kKzJ52bouFlTWNxN7p/vcAIQ8zGEFrnn6ugbjvkeG8=; b=Pk9LV4kfEkWiUDM8HWVgu31QiICB5ZB7G7DgW2lv1QdbdTJRNnWrnjA7rrLt2BuQ6jDzge d5JbLEZTpWNeaaSvjUC3jyXbSRk2py4orJ1t9nDoGVFjkb2of+ky/GqK5shBSpF/4jy1A2 TuoHIzYYMHhNR3PvIsUedWDM7NmJLT3ld8n7Tg7LqWgZg/t8aFgXBdGBwznbtfs8QkBu66 ehn7acCynzNFVBbUOVjgFr2WVdFrAylB+qSJhRjbu2+HN0QcyYo+BFFfVkkxrw86dbmfrw 4i495l1mYBvezZGH5kHnIvQyeJ8c/UjxiaI8bliThWmQE9U1eX/ThByHNkc2Mw== ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796253013; a=rsa-sha256; cv=none; b=MbiXp6e/iqzASI+yLlYLtqKcfd4IsOeRJLGi3AKlImZS+V6f+wdVsPhhY3BzfMlHS+plTM JIzV/m6aR4pnaknwe8eiIiQ96k5aY0R0lcRt3/qtT4e7UD39HfWsI3/+S8nE9EPwgWnRI1 f/It4kk90CwEEHwj37wmZvtoB1yAIOFIh2Uqwzuua4de4w3+1NAx1fIpTeeuOFnQ6vjSbE /nfseSEq9+PpncUKcwQiCexH2GmMKKAzyGhd3sug8S0pgWIFHKkY2kX5E76/qVznnSUexg Jm8MRKxDCiRUqhNLdtDAyCfHr724XrODlvUVDFY12EtvJ4TTAP0V7Y3V/BKDYg== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de From: Marc Kleine-Budde To: netdev@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, kernel@pengutronix.de, =?UTF-8?q?St=C3=A9phane=20Grosjean?= , stable@vger.kernel.org, Marc Kleine-Budde Subject: [PATCH net-next 17/23] can: peak_usb: validate the data length of PCAN-USB Pro CAN records Date: Thu, 3 Dec 2026 00:04:42 +0100 Message-ID: <20261202230853.2665144-18-mkl@pengutronix.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202230853.2665144-1-mkl@pengutronix.de> References: <20261202230853.2665144-1-mkl@pengutronix.de> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Stéphane Grosjean The PCAN-USB Pro decoder takes the data length of a received CAN frame from the 4-bit DLC field of the record, i.e. 0 to 15, and copies that many bytes from the record into can_frame->data. This length is checked neither against CAN_MAX_DLEN nor against the size of the record, which depends on its type: RXMSG8, RXMSG4 and RXMSG0/RXRTR records carry 8, 4 and 0 data bytes. pcan_usb_pro_decode_buf() only checks that the record fits in the URB, according to its type. A faulty or malicious device can therefore make the driver: - copy bytes located beyond the record into the frame, i.e. stale bytes of the RX buffer or, when the record ends the 1024-byte buffer, bytes beyond it, and deliver them to the CAN sockets; - write up to 7 bytes past can_frame->data[] with a DLC of 9 to 15; - deliver an RTR frame whose len is greater than CAN_MAX_DLEN. Convert the DLC with can_cc_dlc2len() and reject a data frame whose length exceeds the number of data bytes of its record type, as the other malformed records are. Found by Sashiko AI review. Fixes: d8a199355f8f ("can: usb: PEAK-System Technik PCAN-USB Pro specific part") Cc: stable@vger.kernel.org Signed-off-by: Stéphane Grosjean Link: https://patch.msgid.link/20261126-canfd_check_channel_idx-v9-8-abe0099b4585@peak-system.fr Signed-off-by: Marc Kleine-Budde --- drivers/net/can/usb/peak_usb/pcan_usb_pro.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c index 9e2f48683a9d..38307d452a5a 100644 --- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c +++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c @@ -534,6 +534,7 @@ static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if, struct pcan_usb_pro_rxmsg *rx) { const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f; + const u8 len = can_cc_dlc2len(rx->len & 0x0f); struct peak_usb_device *dev; struct net_device *netdev; struct can_frame *can_frame; @@ -543,6 +544,12 @@ static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if, if (ctrl_idx >= ARRAY_SIZE(usb_if->dev)) return -EINVAL; + /* the number of data bytes depends on the record type */ + if (!(rx->flags & PCAN_USBPRO_RTR) && + len > pcan_usb_pro_sizeof_rec[rx->data_type] - + offsetof(struct pcan_usb_pro_rxmsg, data)) + return -EINVAL; + dev = READ_ONCE(usb_if->dev[ctrl_idx]); /* reject records for a channel that is not (or no longer) set up */ @@ -556,7 +563,7 @@ static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if, return -ENOMEM; can_frame->can_id = le32_to_cpu(rx->id); - can_frame->len = rx->len & 0x0f; + can_frame->len = len; if (rx->flags & PCAN_USBPRO_EXT) can_frame->can_id |= CAN_EFF_FLAG; -- 2.53.0