Skip to content

Commit

Permalink
isotp: isotp_sendmsg(): fix TX buffer concurrent access in isotp_send…
Browse files Browse the repository at this point in the history
…msg()

When isotp_sendmsg() concurrent, tx.state of all TX processes can be
ISOTP_IDLE. The conditions so->tx.state != ISOTP_IDLE and
wq_has_sleeper(&so->wait) can not protect TX buffer from being
accessed by multiple TX processes.

We can use cmpxchg() to try to modify tx.state to ISOTP_SENDING firstly.
If the modification of the previous process succeed, the later process
must wait tx.state to ISOTP_IDLE firstly. Thus, we can ensure TX buffer
is accessed by only one process at the same time. And we should also
restore the original tx.state at the subsequent error processes.

Upstream commit 43a08c3bdac4

Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Link: https://lore.kernel.org/all/c2517874fbdf4188585cf9ddf67a8fa74d5dbde5.1633764159.git.william.xuanziyang@huawei.com
Cc: stable@vger.kernel.org
Signed-off-by: Ziyang Xuan <william.xuanziyang@huawei.com>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
  • Loading branch information
hartkopp committed Dec 1, 2021
1 parent 52a002c commit aaa0516
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions net/can/isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ enum {
struct tpcon {
int idx;
int len;
u8 state;
u32 state;
u8 bs;
u8 sn;
u8 ll_dl;
Expand Down Expand Up @@ -875,6 +875,7 @@ static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
{
struct sock *sk = sock->sk;
struct isotp_sock *so = isotp_sk(sk);
u32 old_state = so->tx.state;
struct sk_buff *skb;
struct net_device *dev;
struct canfd_frame *cf;
Expand All @@ -887,49 +888,57 @@ static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
return -EADDRNOTAVAIL;

/* we do not support multiple buffers - for now */
if (so->tx.state != ISOTP_IDLE) {
if (msg->msg_flags & MSG_DONTWAIT)
return -EAGAIN;
if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE ||
wq_has_sleeper(&so->wait)) {
if (msg->msg_flags & MSG_DONTWAIT) {
err = -EAGAIN;
goto err_out;
}

/* wait for complete transmission of current pdu */
err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
if (err)
return err;
goto err_out;
}

if (!size || size > MAX_MSG_LENGTH)
return -EINVAL;
if (!size || size > MAX_MSG_LENGTH) {
err = -EINVAL;
goto err_out;
}

/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;

/* does the given data fit into a single frame for SF_BROADCAST? */
if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) &&
(size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off))
return -EINVAL;
(size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
err = -EINVAL;
goto err_out;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,19,0)
err = memcpy_from_msg(so->tx.buf, msg, size);
#else
err = memcpy_fromiovec(so->tx.buf, msg->msg_iov, size);
#endif
if (err < 0)
return err;
goto err_out;

dev = dev_get_by_index(sock_net(sk), so->ifindex);
if (!dev)
return -ENXIO;
if (!dev) {
err = -ENXIO;
goto err_out;
}

skb = sock_alloc_send_skb(sk, so->ll.mtu + CAN_SKBRES,
msg->msg_flags & MSG_DONTWAIT, &err);
if (!skb) {
dev_put(dev);
return err;
goto err_out;
}

isotp_skb_reserve(skb, dev);

so->tx.state = ISOTP_SENDING;
so->tx.len = size;
so->tx.idx = 0;

Expand Down Expand Up @@ -987,7 +996,7 @@ static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
if (err) {
printk_once(KERN_NOTICE "can-isotp: %s: can_send_ret %d\n",
__func__, err);
return err;
goto err_out;
}

if (wait_tx_done) {
Expand All @@ -996,6 +1005,13 @@ static int isotp_sendmsg(struct kiocb *iocb, struct socket *sock,
}

return size;

err_out:
so->tx.state = old_state;
if (so->tx.state == ISOTP_IDLE)
wake_up_interruptible(&so->wait);

return err;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
Expand Down

0 comments on commit aaa0516

Please sign in to comment.