From: Oliver Hartkopp 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 Closes: https://sashiko.dev/#/bug/linux-d1d3ef49-c24e-4f3a-91ce-20f8fb222684 Cc: stable@vger.kernel.org Signed-off-by: Oliver Hartkopp Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-7-f6b84f5257b1@hartkopp.net Signed-off-by: Marc Kleine-Budde --- 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