From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CD801E489; Wed, 17 Sep 2025 12:44:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758113056; cv=none; b=Kt0P6aqDjHzBWV8Sa0NbsDYpGBuOaQB+PzqdAa2Qu1p1n/FuIGUwiskfkNg0ebRr4Z9CdpE6Sxw9yTVp95Xg2FuhCUX7aRiL7f4S+VmGvJ/YWBnUfnSVhrAp9SFlGaZgLMy4blYQ/Kh3ouAO4yOcFeBUTWN114YMZYKwLVoGcME= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758113056; c=relaxed/simple; bh=a+PcUFfjfRXqjx8DogzpTvLAWdSEkmkTYeV6E+PNwgU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CotcJOWtiOVio/XCAlkqV3qh6oh2gliDi8t0aR8aqgnk+mINML8Qq++jwwDvZhsj3qYAL512443m7YQmwLva7ifS4BwjjZB1cU5fI81mXbACYUKCJN/gs3aEPw0XDWPavTxEBN2Hyli/EqFdzOb8yVkgMyotg8UF5dgpo7fLDcg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Yk51TUHh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Yk51TUHh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFEFCC4CEF0; Wed, 17 Sep 2025 12:44:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1758113056; bh=a+PcUFfjfRXqjx8DogzpTvLAWdSEkmkTYeV6E+PNwgU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yk51TUHhHQP/RfUWKrUE7oOox216hEY0f3Pq4azWp4VLjnpU7XgiqptCPgyVcm/Ao /WcgW9BfPbnMY+czHs0ffCOqQ/W4uE75CgwKR/BhGzuLh16K1RBnN3J23U7uYSBUEL 64yQUgbQJYoHLB5Ti1RbD+1n8OOB1AJL4LdOFAMQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Anssi Hannula , Marc Kleine-Budde , Sasha Levin Subject: [PATCH 6.16 151/189] can: xilinx_can: xcan_write_frame(): fix use-after-free of transmitted SKB Date: Wed, 17 Sep 2025 14:34:21 +0200 Message-ID: <20250917123355.559830251@linuxfoundation.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250917123351.839989757@linuxfoundation.org> References: <20250917123351.839989757@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Anssi Hannula [ Upstream commit ef79f00be72bd81d2e1e6f060d83cf7e425deee4 ] can_put_echo_skb() takes ownership of the SKB and it may be freed during or after the call. However, xilinx_can xcan_write_frame() keeps using SKB after the call. Fix that by only calling can_put_echo_skb() after the code is done touching the SKB. The tx_lock is held for the entire xcan_write_frame() execution and also on the can_get_echo_skb() side so the order of operations does not matter. An earlier fix commit 3d3c817c3a40 ("can: xilinx_can: Fix usage of skb memory") did not move the can_put_echo_skb() call far enough. Signed-off-by: Anssi Hannula Fixes: 1598efe57b3e ("can: xilinx_can: refactor code in preparation for CAN FD support") Link: https://patch.msgid.link/20250822095002.168389-1-anssi.hannula@bitwise.fi [mkl: add "commit" in front of sha1 in patch description] [mkl: fix indention] Signed-off-by: Marc Kleine-Budde Signed-off-by: Sasha Levin --- drivers/net/can/xilinx_can.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/net/can/xilinx_can.c b/drivers/net/can/xilinx_can.c index 3f2e378199abb..5abe4af61655c 100644 --- a/drivers/net/can/xilinx_can.c +++ b/drivers/net/can/xilinx_can.c @@ -690,14 +690,6 @@ static void xcan_write_frame(struct net_device *ndev, struct sk_buff *skb, dlc |= XCAN_DLCR_EDL_MASK; } - if (!(priv->devtype.flags & XCAN_FLAG_TX_MAILBOXES) && - (priv->devtype.flags & XCAN_FLAG_TXFEMP)) - can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max, 0); - else - can_put_echo_skb(skb, ndev, 0, 0); - - priv->tx_head++; - priv->write_reg(priv, XCAN_FRAME_ID_OFFSET(frame_offset), id); /* If the CAN frame is RTR frame this write triggers transmission * (not on CAN FD) @@ -730,6 +722,14 @@ static void xcan_write_frame(struct net_device *ndev, struct sk_buff *skb, data[1]); } } + + if (!(priv->devtype.flags & XCAN_FLAG_TX_MAILBOXES) && + (priv->devtype.flags & XCAN_FLAG_TXFEMP)) + can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max, 0); + else + can_put_echo_skb(skb, ndev, 0, 0); + + priv->tx_head++; } /** -- 2.51.0