]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
can: annotate mtu accesses with READ_ONCE()
authorVincent Mailhol <mailhol@kernel.org>
Tue, 23 Sep 2025 06:37:08 +0000 (15:37 +0900)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Tue, 23 Sep 2025 08:04:58 +0000 (10:04 +0200)
As hinted in commit 501a90c94510 ("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>
net/can/af_can.c
net/can/isotp.c
net/can/raw.c

index b2387a46794a576973f3d865a5ca8e2ba696d167..770173d8db42813d5c085248d1bcf5fbe717955b 100644 (file)
@@ -221,7 +221,7 @@ int can_send(struct sk_buff *skb, int loop)
        }
 
        /* Make sure the CAN frame can pass the selected CAN netdevice. */
-       if (unlikely(skb->len > skb->dev->mtu)) {
+       if (unlikely(skb->len > READ_ONCE(skb->dev->mtu))) {
                err = -EMSGSIZE;
                goto inval_skb;
        }
index dee1412b3c9c1ffcfc43a109b448701459fcf8b9..74ee1e52249b232813a06c5d2c6e404a38dce990 100644 (file)
@@ -1313,7 +1313,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
                err = -ENODEV;
                goto out;
        }
-       if (dev->mtu < so->ll.mtu) {
+       if (READ_ONCE(dev->mtu) < so->ll.mtu) {
                dev_put(dev);
                err = -EINVAL;
                goto out;
index bf65d67b5df0135bf02d90c5e1070898a4f5592e..a53853f5e9afc57404132f9acb246e8af2ba0d9c 100644 (file)
@@ -961,7 +961,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
        err = -EINVAL;
 
        /* check for valid CAN (CC/FD/XL) frame content */
-       txmtu = raw_check_txframe(ro, skb, dev->mtu);
+       txmtu = raw_check_txframe(ro, skb, READ_ONCE(dev->mtu));
        if (!txmtu)
                goto free_skb;