From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
kernel@pengutronix.de, Oliver Hartkopp <socketcan@hartkopp.net>,
Sashiko <sashiko-bot@kernel.org>,
stable@vger.kernel.org, Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net 07/12] can: isotp: reject socket options while a transfer is active
Date: Tue, 24 Nov 2026 09:51:34 +0100 [thread overview]
Message-ID: <20261124085757.1093632-8-mkl@pengutronix.de> (raw)
In-Reply-To: <20261124085757.1093632-1-mkl@pengutronix.de>
From: Oliver Hartkopp <socketcan@hartkopp.net>
isotp_setsockopt_locked() only checks so->bound before it changes the
CAN_ISOTP socket options. When the bound CAN device is unregistered,
isotp_notify() clears so->bound, but a transmission or reception that
is already in progress is not stopped and may still be draining.
isotp_sendmsg() claims so->tx.state and rechecks so->bound. After that
it can block in sock_alloc_send_skb(), while the device is unregistered
and so->bound is cleared. A concurrent setsockopt() can then change
so->opt, so->ll and so->tx.ll_dl, before isotp_sendmsg() reads so->ll.mtu
and so->tx.ll_dl again to build the CAN frame for the skb that has been
allocated with the former so->ll.mtu.
isotp_bind() already rejects a new binding while so->tx.state or
so->rx.state are not ISOTP_IDLE for the same reason. Add this check to
isotp_setsockopt_locked() and return -EAGAIN, so the socket options can
only be changed when no transfer is active. As the transmission and
the reception always end by their timeouts at the latest, this state
is only temporary.
isotp_sendmsg() reads the CAN_ISOTP_EXTEND_ADDR and
CAN_ISOTP_WAIT_TX_DONE flags at its beginning, before the transfer is
claimed. Read them after the claim and the so->bound recheck instead,
so that all socket options used by isotp_sendmsg() are covered by this
check.
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-d1d3ef49-c24e-4f3a-91ce-20f8fb222684
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-7-f6b84f5257b1@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
net/can/isotp.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 0b7db1ead7a9..642fec500af9 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1128,8 +1128,8 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
struct can_skb_ext *csx;
struct net_device *dev;
struct canfd_frame *cf;
- int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
- int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
+ int ae;
+ int wait_tx_done;
s64 hrtimer_sec = ISOTP_ECHO_TIMEOUT;
struct hrtimer *tx_hrt = &so->echotimer;
u32 new_state = ISOTP_SENDING;
@@ -1188,6 +1188,10 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
goto err_out_drop;
}
+ /* read the options after the claim of the transfer */
+ ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
+ wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
+
/* PDU size > default => try max_pdu_size */
if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL);
@@ -1726,6 +1730,14 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
if (so->bound)
return -EISCONN;
+ /* A transmission or reception that outlived a previous binding
+ * (unbound by NETDEV_UNREGISTER) may still be draining and uses the
+ * socket options, see isotp_bind().
+ */
+ if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
+ READ_ONCE(so->rx.state) != ISOTP_IDLE)
+ return -EAGAIN;
+
switch (optname) {
case CAN_ISOTP_OPTS:
memcpy(&so->opt, val, sizeof(so->opt));
--
2.53.0
next prev parent reply other threads:[~2026-11-24 8:58 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 8:51 [PATCH net 0/12] pull-request: can 2026-11-24 Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 01/12] can: bcm: copy the CAN frames outside of lock_sock() Marc Kleine-Budde
2026-11-24 9:04 ` netdev-bot+sinfo
2026-11-24 8:51 ` [PATCH net 02/12] can: bcm: do not remove the procfs entry in bcm_notify() Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 03/12] can: raw: use a private mutex instead of lock_sock() Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 04/12] can: isotp: " Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 05/12] can: bcm: " Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 06/12] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state Marc Kleine-Budde
2026-11-24 8:51 ` Marc Kleine-Budde [this message]
2026-11-24 8:51 ` [PATCH net 08/12] can: netlink: fix static FD-NON-ISO mode validation Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 09/12] can: netlink: fix IFLA_CAN_BITRATE_MAX attribute sizing Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 10/12] can: netlink: fix CAN XL PWM " Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 11/12] can: gs_usb: gs_can_start_xmit(): don't leak uninitialized memory over USB Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 12/12] can: flexcan: Fix probing for m68k/coldfire Marc Kleine-Budde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124085757.1093632-8-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=davem@davemloft.net \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=sashiko-bot@kernel.org \
--cc=socketcan@hartkopp.net \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox