Skip to content

Commit c67732d

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: annotate mtu accesses with READ_ONCE()
As hinted in commit 501a90c ("inet: protect against too small mtu values."), net_device->mtu is vulnerable to race conditions if it is written and read without holding the RTNL. At the moment, all the writes are done while the interface is down, either in the devices' probe() function or in can_changelink(). So there are no such issues yet. But upcoming changes will allow to modify the MTU while the CAN XL devices are up. In preparation to the introduction of CAN XL, annotate all the net_device->mtu accesses which are not yet guarded by the RTNL with a READ_ONCE(). Note that all the write accesses are already either guarded by the RTNL or are already annotated and thus need no changes. Signed-off-by: Vincent Mailhol <mailhol@kernel.org> Link: https://patch.msgid.link/20250923-can-fix-mtu-v3-1-581bde113f52@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent c0b5952 commit c67732d

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

net/can/af_can.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ int can_send(struct sk_buff *skb, int loop)
221221
}
222222

223223
/* Make sure the CAN frame can pass the selected CAN netdevice. */
224-
if (unlikely(skb->len > skb->dev->mtu)) {
224+
if (unlikely(skb->len > READ_ONCE(skb->dev->mtu))) {
225225
err = -EMSGSIZE;
226226
goto inval_skb;
227227
}

net/can/isotp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
13131313
err = -ENODEV;
13141314
goto out;
13151315
}
1316-
if (dev->mtu < so->ll.mtu) {
1316+
if (READ_ONCE(dev->mtu) < so->ll.mtu) {
13171317
dev_put(dev);
13181318
err = -EINVAL;
13191319
goto out;

net/can/raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
961961
err = -EINVAL;
962962

963963
/* check for valid CAN (CC/FD/XL) frame content */
964-
txmtu = raw_check_txframe(ro, skb, dev->mtu);
964+
txmtu = raw_check_txframe(ro, skb, READ_ONCE(dev->mtu));
965965
if (!txmtu)
966966
goto free_skb;
967967

0 commit comments

Comments
 (0)