CAN netlayer fixes for stable
Subsystems (4)
Oliver Hartkopp
maintainerv2 cover CAN netlayer fixes for stable
This patch set aggregates the open stable fixes for the CAN netlayer.
v2: restore non-zero UID value generation for isotp cfecho handling
Oliver Hartkopp (3):
can: remove CAN filters independent from namespace
can: convert unreliable ARPHRD_CAN type checks to robust can_get_ml_priv()
can: fix unique skb identifier regression under RPS
include/linux/can/core.h | 3 +- include/linux/can/skb.h | 4 +- include/net/can.h | 3 ++ net/can/af_can.c | 35 ++++++++----- net/can/bcm.c | 107 +++++++++++++++++++++++++++++---------- net/can/gw.c | 10 ++-- net/can/isotp.c | 16 +++--- net/can/raw.c | 7 +-- 8 files changed, 126 insertions(+), 59 deletions(-)
Signature · 1 line
2.53.0
Oliver Hartkopp
maintainerPATCH v2 1/3 can: remove CAN filters independent from namespace
When the devices namespace is changed the socket namespace and the device namespace might differ. The net_eq(dev_net(dev), sock_net(sk)) check in the CAN protocols netdev notifiers therefore led to skipping the required removal of the CAN filters from the (namespace changed) CAN devices.
This patch removes the namespace equality check in the netdev notifiers for BCM, ISOTP and RAW sockets. Since the struct net_device pointer is globally unique, the notifier should always process the unregister event and remove the CAN filters if it matches the original socket's bound device pointer.
In bcm.c netdevice comparisons were performed by checking the interface index (bo->ifindex and op->ifindex) which is not namespace-safe either. Introduce tracked netdevice pointers (bo->dev and op->tx_dev) for these referenced devices to enable namespace-save device comparisons. Additional put all bo->dev accesses in bcm_notify() under lock_sock().
In isotp.c the two missing can_rx_unregister() calling sites are converted to use dev_net(dev) instead of sock_net(sk) to get the correct namespace.
--- net/can/bcm.c | 100 ++++++++++++++++++++++++++++++++++++------------ net/can/isotp.c | 7 +--- net/can/raw.c | 3 -- 3 files changed, 78 insertions(+), 32 deletions(-)
net/can/bcm.c +76 -24
@@ -128,18 +128,22 @@ struct bcm_op {struct canfd_frame sframe;struct canfd_frame last_sframe;struct sock *sk;struct net_device *rx_reg_dev;netdevice_tracker rx_reg_dev_tracker; +struct net_device *tx_dev; +netdevice_tracker tx_dev_tracker;spinlock_t bcm_tx_lock;/* protect tx data and timer updates */ spinlock_t bcm_rx_update_lock;/* protect filter/timer data updates */ };struct bcm_sock {struct sock sk;int bound;int ifindex; +struct net_device *dev; +netdevice_tracker dev_tracker;struct list_head notifier;struct list_head rx_ops;struct list_head tx_ops;unsigned long dropped_usr_msgs;struct proc_dir_entry *bcm_proc_read;
@@ -933,10 +937,13 @@ static void bcm_free_op_work(struct work_struct *work)kfree (op->frames);if ((op->last_frames) && (op->last_frames != &op->last_sframe))kfree (op->last_frames); +if (op->tx_dev) +netdev_put (op->tx_dev, &op->tx_dev_tracker); +/* the last possible access to op->timer/op->thrtimer has now * happened above via hrtimer_cancel() - op->sk is no longer * needed by any pending timer callback, so drop our reference */ sock_put (op->sk);
@@ -1072,10 +1079,11 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, int ifindex, struct sock *sk) { struct bcm_sock *bo = bcm_sk(sk);struct bcm_op *op;struct canfd_frame *cf; +struct net_device *tx_dev;bool add_op_to_list = false;unsigned int i;int err;/* we need a real device to send frames */
@@ -1103,10 +1111,26 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,* therefore (complexity / locking) it is not supported. */ if (msg_head->nframes > op->nframes)return -E2BIG ; +/* Re-resolve and re-hold the target device if a concurrent +* NETDEV_UNREGISTER already cleared it (see bcm_notify()). +* op->ifindex and sock_net(sk) is unchanged. +*/ +if (!op->tx_dev) { + tx_dev =dev_get_by_index (sock_net (sk), ifindex); +if (tx_dev) { + op->tx_dev = tx_dev; +netdev_hold (tx_dev, &op->tx_dev_tracker, +GFP_KERNEL ); +dev_put (tx_dev); + }else { +return -ENODEV ; + } + } +/* get new CAN frames content into a staging buffer before * locking: validate and normalize the frames there so that * bcm_can_tx() / bcm_tx_timeout_handler() never observe a * partially updated or unvalidated frame in op->frames */
@@ -1169,10 +1193,22 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, op =kzalloc (OPSIZ ,GFP_KERNEL );if (!op)return -ENOMEM ; + tx_dev =dev_get_by_index (sock_net (sk), ifindex); +if (tx_dev) { + op->tx_dev = tx_dev; +netdev_hold (tx_dev, &op->tx_dev_tracker,GFP_KERNEL ); +dev_put (tx_dev); + }else { +/* prepare op->frames for goto free_op */ + op->frames = &op->sframe; + err = -ENODEV ; + goto free_op; + } +spin_lock_init (&op->bcm_tx_lock); op->can_id = msg_head->can_id; op->cfsiz =CFSIZ (msg_head->flags); op->flags = msg_head->flags; op->nframes = msg_head->nframes;
@@ -1184,12 +1220,14 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,if (msg_head->nframes >1 ) { op->frames =kmalloc_array (msg_head->nframes, op->cfsiz,GFP_KERNEL );if (!op->frames) { -kfree (op); -return -ENOMEM ; +/* prepare op->frames for goto free_op */ + op->frames = &op->sframe; + err = -ENOMEM ; + goto free_op; } }else op->frames = &op->sframe;for (i =0 ; i < msg_head->nframes; i++) {
@@ -1267,10 +1305,13 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,bcm_tx_start_timer (op);return msg_head->nframes * op->cfsiz +MHSIZ ; free_op: +if (op->tx_dev) +netdev_put (op->tx_dev, &op->tx_dev_tracker); +if (op->frames != &op->sframe)kfree (op->frames);kfree (op);return err; }
@@ -1790,46 +1831,47 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)static void bcm_notify (struct bcm_sock *bo,unsigned long msg,struct net_device *dev) {struct sock *sk = &bo->sk;struct bcm_op *op; -int notify_enodev =0 ; +int sk_err =0 ; -if (!net_eq (dev_net (dev),sock_net (sk))) -return ; +lock_sock (sk);switch (msg) {case NETDEV_UNREGISTER : -lock_sock (sk);/* rx_ops: remove device specific receive entries */ list_for_each_entry (op, &bo->rx_ops, list) {if (op->rx_reg_dev == dev)bcm_rx_unreg (dev, op);/* release an ANYDEV op's claim (see bcm_rx_handler()) * on this now confirmed-gone interface. */ -if (!op->ifindex) { +if (!op->ifindex &&net_eq (dev_net (dev),sock_net (sk))) {spin_lock_bh (&op->bcm_rx_update_lock);if (op->if_detected == dev->ifindex) op->if_detected =0 ;spin_unlock_bh (&op->bcm_rx_update_lock); } }/* tx_ops: stop device specific cyclic transmissions on the -* vanishing ifindex. Cancelling the timer is enough to stop +* vanishing device. Cancelling the timer is enough to stop * cyclic bcm_can_tx() calls as there is no re-arming. */ list_for_each_entry (op, &bo->tx_ops, list) -if (op->ifindex == dev->ifindex) +if (op->tx_dev == dev) {hrtimer_cancel (&op->timer); +netdev_put (op->tx_dev, &op->tx_dev_tracker); + op->tx_dev =NULL ; + }/* remove device reference, if this is our bound device */ -if (bo->bound && bo->ifindex == dev->ifindex) { +if (bo->bound && bo->dev == dev) {#if IS_ENABLED (CONFIG_PROC_FS )if (sock_net (sk)->can.bcmproc_dir && bo->bcm_proc_read) {remove_proc_entry (bo->procname,sock_net (sk)->can.bcmproc_dir); bo->bcm_proc_read =NULL ; }
@@ -1839,28 +1881,27 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,* accessed under lock_sock() so it needs no * annotation. */ WRITE_ONCE (bo->bound,0 ); bo->ifindex =0 ; - notify_enodev =1 ; - } - -release_sock (sk); - -if (notify_enodev) { - sk->sk_err =ENODEV ; -if (!sock_flag (sk,SOCK_DEAD )) -sk_error_report (sk); +netdev_put (bo->dev, &bo->dev_tracker); + bo->dev =NULL ; + sk_err =ENODEV ; }break ;case NETDEV_DOWN : -if (bo->bound && bo->ifindex == dev->ifindex) { - sk->sk_err =ENETDOWN ; -if (!sock_flag (sk,SOCK_DEAD )) -sk_error_report (sk); - } +if (bo->bound && bo->dev == dev) + sk_err =ENETDOWN ; + } + +release_sock (sk); + +if (sk_err) { + sk->sk_err = sk_err; +if (!sock_flag (sk,SOCK_DEAD )) +sk_error_report (sk); } }static int bcm_notifier (struct notifier_block *nb,unsigned long msg,void *ptr)
@@ -1982,10 +2023,14 @@ static int bcm_release(struct socket *sock)/* remove device reference */ if (bo->bound) {WRITE_ONCE (bo->bound,0 ); bo->ifindex =0 ; +if (bo->dev) { +netdev_put (bo->dev, &bo->dev_tracker); + bo->dev =NULL ; + } }sock_orphan (sk); sock->sk =NULL ;
@@ -2029,25 +2074,32 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int ret = -ENODEV ; goto fail; } bo->ifindex = dev->ifindex; + bo->dev = dev; +netdev_hold (dev, &bo->dev_tracker,GFP_KERNEL );dev_put (dev); }else {/* no interface reference for ifindex = 0 ('any' CAN device) */ bo->ifindex =0 ; + bo->dev =NULL ; }#if IS_ENABLED (CONFIG_PROC_FS )if (net->can.bcmproc_dir) {/* unique socket address as filename */ sprintf (bo->procname,"%llu" ,sock_i_ino (sk)); bo->bcm_proc_read =proc_create_net_single (bo->procname,0644 , net->can.bcmproc_dir, bcm_proc_show, sk);if (!bo->bcm_proc_read) { +if (bo->dev) { +netdev_put (bo->dev, &bo->dev_tracker); + bo->dev =NULL ; + } ret = -ENOMEM ; goto fail; } }#endif /* CONFIG_PROC_FS */
net/can/isotp.c +2 -5
@@ -1490,15 +1490,15 @@ static int isotp_release(struct socket *sock)/* remove current filters & unregister * tracked reference so->dev is taken at bind() time with rtnl_lock */ if (so->bound && so->dev) {if (isotp_register_rxid (so)) -can_rx_unregister (net, so->dev, so->rxid, +can_rx_unregister (dev_net (so->dev), so->dev, so->rxid,SINGLE_MASK (so->rxid), isotp_rcv, sk); -can_rx_unregister (net, so->dev, so->txid, +can_rx_unregister (dev_net (so->dev), so->dev, so->txid,SINGLE_MASK (so->txid), isotp_rcv_echo, sk);netdev_put (so->dev, &so->dev_tracker); }
@@ -1846,13 +1846,10 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,static void isotp_notify (struct isotp_sock *so,unsigned long msg,struct net_device *dev) {struct sock *sk = &so->sk; -if (!net_eq (dev_net (dev),sock_net (sk))) -return ; -if (so->dev != dev)return ;switch (msg) {case NETDEV_UNREGISTER :
net/can/raw.c +0 -3
@@ -300,13 +300,10 @@ static int raw_enable_allfilters(struct net *net, struct net_device *dev,static void raw_notify (struct raw_sock *ro,unsigned long msg,struct net_device *dev) {struct sock *sk = &ro->sk; -if (!net_eq (dev_net (dev),sock_net (sk))) -return ; -if (ro->dev != dev)return ;switch (msg) {case NETDEV_UNREGISTER :
Signature · 1 line
2.53.0
Oliver Hartkopp
maintainerPATCH v2 2/3 can: convert unreliable ARPHRD_CAN type checks to robust can_get_ml_priv()
In reply to Oliver HartkoppCommit 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device") introduced an explicit way to assign the midlayer private pointer (dev->ml_priv) to named users like ML_PRIV_CAN.
With this extension the CAN device specific ml_priv assignment became a robust indicator to identify a valid CAN device, when can_get_ml_priv() returns a valid pointer.
This has been used directly by the referenced commit in the CAN specific j1939 and proc code but not in the other parts of the CAN subsystem.
With the TUN/TAP driver a device's ARPHRD type can be controlled by userspace independently of its midlayer private data (ml_priv). The TUNSETLINK ioctl allows a down TUN/TAP device to overwrite its hardware type to become ARPHRD_CAN while dev->ml_priv remains NULL (uninitialized).
Instead of checking dev->type being the unreliable ARPHRD_CAN value convert the missing "valid CAN devices" checks to can_get_ml_priv().
--- net/can/af_can.c | 12 ++++++------ net/can/bcm.c | 7 ++++--- net/can/gw.c | 7 ++++--- net/can/isotp.c | 5 +++-- net/can/raw.c | 4 ++-- 5 files changed, 19 insertions(+), 16 deletions(-)
net/can/af_can.c +6 -6
@@ -224,11 +224,11 @@ int can_send(struct sk_buff *skb, int loop)if (unlikely (skb->len >READ_ONCE (skb->dev->mtu))) { err = -EMSGSIZE ; goto inval_skb; } -if (unlikely (skb->dev->type !=ARPHRD_CAN )) { +if (unlikely (!can_get_ml_priv (skb->dev))) { err = -EPERM ; goto inval_skb; }if (unlikely (!(skb->dev->flags &IFF_UP ))) {
@@ -450,11 +450,11 @@ int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,struct can_dev_rcv_lists *dev_rcv_lists;struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;/* insert new receiver (dev,canid,mask) -> (func,data) */ -if (dev && (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev))) +if (dev && !can_get_ml_priv (dev))return -ENODEV ;if (dev && !net_eq (net,dev_net (dev)))return -ENODEV ;
@@ -517,11 +517,11 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,struct receiver *rcv =NULL ;struct hlist_head *rcv_list;struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;struct can_dev_rcv_lists *dev_rcv_lists; -if (dev && dev->type !=ARPHRD_CAN ) +if (dev && !can_get_ml_priv (dev))return ;if (dev && !net_eq (net,dev_net (dev)))return ;
@@ -685,11 +685,11 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev) }static int can_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || +if (unlikely (!can_get_ml_priv (dev) || !can_skb_ext_find (skb) || !can_is_can_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CAN_RX_INVALID_FRAME );
@@ -701,11 +701,11 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev, }static int canfd_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || +if (unlikely (!can_get_ml_priv (dev) || !can_skb_ext_find (skb) || !can_is_canfd_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CANFD_RX_INVALID_FRAME );
@@ -717,11 +717,11 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev, }static int canxl_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || +if (unlikely (!can_get_ml_priv (dev) || !can_skb_ext_find (skb) || !can_is_canxl_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN XL skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CANXL_RX_INVALID_FRAME );
net/can/bcm.c +4 -3
@@ -52,10 +52,11 @@#include <linux/netdevice.h> #include <linux/socket.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <linux/can.h> +#include <linux/can/can-ml.h> #include <linux/can/core.h> #include <linux/can/skb.h> #include <linux/can/bcm.h> #include <linux/slab.h> #include <linux/workqueue.h>
@@ -1758,11 +1759,11 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)if (!dev) { ret = -ENODEV ; goto out_release; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) {dev_put (dev); ret = -ENODEV ; goto out_release; }
@@ -1906,11 +1907,11 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,static int bcm_notifier (struct notifier_block *nb,unsigned long msg,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); -if (dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (dev))return NOTIFY_DONE ;if (msg !=NETDEV_UNREGISTER && msg !=NETDEV_DOWN )return NOTIFY_DONE ;if (unlikely (bcm_busy_notifier))/* Check for reentrant bug. */ return NOTIFY_DONE ;
@@ -2067,11 +2068,11 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int dev =dev_get_by_index (net, addr->can_ifindex);if (!dev) { ret = -ENODEV ; goto fail; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) {dev_put (dev); ret = -ENODEV ; goto fail; }
net/can/gw.c +4 -3
@@ -50,10 +50,11 @@#include <linux/net.h> #include <linux/netdevice.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <linux/can.h> +#include <linux/can/can-ml.h> #include <linux/can/core.h> #include <linux/can/skb.h> #include <linux/can/gw.h> #include <net/can.h> #include <net/rtnetlink.h>
@@ -607,11 +608,11 @@ static int cgw_notifier(struct notifier_block *nb, unsigned long msg, void *ptr) { struct net_device *dev = netdev_notifier_info_to_dev(ptr);struct net *net =dev_net (dev); -if (dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (dev))return NOTIFY_DONE ;if (msg ==NETDEV_UNREGISTER ) {struct cgw_job *gwj =NULL ;struct hlist_node *nx;
@@ -1158,19 +1159,19 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh, gwj->src.dev =__dev_get_by_index (net, gwj->ccgw.src_idx);if (!gwj->src.dev) goto out; -if (gwj->src.dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (gwj->src.dev)) goto out; gwj->dst.dev =__dev_get_by_index (net, gwj->ccgw.dst_idx);if (!gwj->dst.dev) goto out; -if (gwj->dst.dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (gwj->dst.dev)) goto out;/* is sending the skb back to the incoming interface intended? */ if (gwj->src.dev == gwj->dst.dev && !(gwj->flags &CGW_FLAGS_CAN_IIF_TX_OK )) {
net/can/isotp.c +3 -2
@@ -63,10 +63,11 @@#include <linux/netdevice.h> #include <linux/socket.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <linux/can.h> +#include <linux/can/can-ml.h> #include <linux/can/core.h> #include <linux/can/skb.h> #include <linux/can/isotp.h> #include <linux/slab.h> #include <net/can.h>
@@ -1604,11 +1605,11 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l dev =dev_get_by_index (net, addr->can_ifindex);if (!dev) { err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }if (READ_ONCE (dev->mtu) < so->ll.mtu) { err = -EINVAL ;
@@ -1888,11 +1889,11 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,static int isotp_notifier (struct notifier_block *nb,unsigned long msg,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); -if (dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (dev))return NOTIFY_DONE ;if (msg !=NETDEV_UNREGISTER && msg !=NETDEV_DOWN )return NOTIFY_DONE ;if (unlikely (isotp_busy_notifier))/* Check for reentrant bug. */ return NOTIFY_DONE ;
net/can/raw.c +2 -2
@@ -339,11 +339,11 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,static int raw_notifier (struct notifier_block *nb,unsigned long msg,void *ptr) {struct net_device *dev =netdev_notifier_info_to_dev (ptr); -if (dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (dev))return NOTIFY_DONE ;if (msg !=NETDEV_UNREGISTER && msg !=NETDEV_DOWN )return NOTIFY_DONE ;if (unlikely (raw_busy_notifier))/* Check for reentrant bug. */ return NOTIFY_DONE ;
@@ -482,11 +482,11 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len dev =dev_get_by_index (sock_net (sk), addr->can_ifindex);if (!dev) { err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }if (!(dev->flags &IFF_UP ))
Signature · 1 line
2.53.0
Oliver Hartkopp
maintainerPATCH v2 3/3 can: fix unique skb identifier regression under RPS
In reply to Oliver HartkoppCommit d4fb6514ff8e ("can: use skb hash instead of private variable in headroom") introduced a regression when Receive Packet Steering (RPS) is enabled.
When RPS is active, the network stack calculates a software hash via skb_get_hash() before the frame reaches the CAN subsystem. Because the flow dissector finds no L3/L4 headers in CAN frames, it generates a static software hash for every CAN packet and sets skb->sw_hash to 1.
Since can_set_skb_uid() originally skipped generating a unique identifier if skb->hash was already non-zero, it preserved this colliding RPS hash. If the SLAB allocator subsequently reused the same memory address for a different skb pointer, raw_rcv() falsely discarded the new, legitimate frame as a duplicate.
Fix this by moving the unique identification decision logic into the CAN skb extension (struct can_skb_ext). Introduce a new flag CAN_EXT_UID in can_ext_flags to explicitly track whether a unique CAN skb identifier has been assigned to the frame. can_set_skb_uid() now unconditionally overwrites any pre-calculated network layer hashes with the unique CAN UID if the flag is not yet set.
Additionally force the generation of a new CAN UID for CAN skbs routed via can-gw.
---
v2: We need a non-zero UID for the cfecho checks in isotp.c
So restore the former while (!(skb->hash)) statement --- include/linux/can/core.h | 3 ++- include/linux/can/skb.h | 4 +++- include/net/can.h | 3 +++ net/can/af_can.c | 29 +++++++++++++++++++---------- net/can/gw.c | 3 +++ net/can/isotp.c | 4 ++-- 6 files changed, 32 insertions(+), 14 deletions(-)
include/linux/can/core.h +2 -1
@@ -15,10 +15,11 @@#define _CAN_CORE_H#include <linux/can.h> #include <linux/skbuff.h> #include <linux/netdevice.h> +#include <net/can.h> #define DNAME (dev) ((dev) ? (dev)->name : "any")/** * struct can_proto - CAN protocol structure
@@ -56,9 +57,9 @@ extern void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id, canid_t mask, void (*func)(struct sk_buff *, void *), void *data);extern int can_send (struct sk_buff *skb,int loop); -void can_set_skb_uid (struct sk_buff *skb); +void can_set_skb_uid (struct sk_buff *skb,struct can_skb_ext *csx);void can_sock_destruct (struct sock *sk);#endif /* !_CAN_CORE_H */
include/linux/can/skb.h +3 -1
@@ -41,12 +41,14 @@ bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb);static inline struct can_skb_ext *can_skb_ext_add (struct sk_buff *skb) {struct can_skb_ext *csx =skb_ext_add (skb,SKB_EXT_CAN );/* skb_ext_add() returns uninitialized space */ -if (csx) +if (csx) { csx->can_gw_hops =0 ; + csx->can_ext_flags =0 ; + }return csx; }static inline struct can_skb_ext *can_skb_ext_find (struct sk_buff *skb)
include/net/can.h +3 -0
@@ -9,10 +9,13 @@*/ #ifndef _NET_CAN_H#define _NET_CAN_H +/* flags for struct can_skb_ext::can_ext_flags */ +#define CAN_EXT_UID BIT(0)/* skb->hash contains a valid CAN skb UID */ +/** * struct can_skb_ext - skb extensions for CAN specific content * @can_iif: ifindex of the first interface the CAN frame appeared on * @can_framelen: cached echo CAN frame length for bql * @can_gw_hops: can-gw CAN frame time-to-live counter
net/can/af_can.c +19 -10
@@ -639,17 +639,22 @@ static int can_rcv_filter(struct can_dev_rcv_lists *dev_rcv_lists, struct sk_buf }return matches; } -void can_set_skb_uid (struct sk_buff *skb) +void can_set_skb_uid (struct sk_buff *skb,struct can_skb_ext *csx) { -/* create non-zero unique skb identifier together with *skb */ +if (csx->can_ext_flags &CAN_EXT_UID ) +return ; + +/* Overwrite pre-calculated network hashes with a unique CAN UID */ + skb->hash =0 ;while (!(skb->hash)) skb->hash =atomic_inc_return (&skbcounter); skb->sw_hash =1 ; + csx->can_ext_flags |=CAN_EXT_UID ; }EXPORT_SYMBOL (can_set_skb_uid);static void can_receive (struct sk_buff *skb,struct net_device *dev) {
@@ -660,12 +665,10 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev)/* update statistics */ atomic_long_inc (&pkg_stats->rx_frames);atomic_long_inc (&pkg_stats->rx_frames_delta); -can_set_skb_uid (skb); -rcu_read_lock ();/* deliver the packet to sockets listening on all devices */ matches =can_rcv_filter (net->can.rx_alldev_list, skb);
@@ -685,51 +688,57 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev) }static int can_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (!can_get_ml_priv (dev) || - !can_skb_ext_find (skb) || !can_is_can_skb (skb))) { +struct can_skb_ext *csx =can_skb_ext_find (skb); + +if (unlikely (!can_get_ml_priv (dev) || !csx || !can_is_can_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CAN_RX_INVALID_FRAME );return NET_RX_DROP ; } +can_set_skb_uid (skb, csx);can_receive (skb, dev);return NET_RX_SUCCESS ; }static int canfd_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (!can_get_ml_priv (dev) || - !can_skb_ext_find (skb) || !can_is_canfd_skb (skb))) { +struct can_skb_ext *csx =can_skb_ext_find (skb); + +if (unlikely (!can_get_ml_priv (dev) || !csx || !can_is_canfd_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CANFD_RX_INVALID_FRAME );return NET_RX_DROP ; } +can_set_skb_uid (skb, csx);can_receive (skb, dev);return NET_RX_SUCCESS ; }static int canxl_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (!can_get_ml_priv (dev) || - !can_skb_ext_find (skb) || !can_is_canxl_skb (skb))) { +struct can_skb_ext *csx =can_skb_ext_find (skb); + +if (unlikely (!can_get_ml_priv (dev) || !csx || !can_is_canxl_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN XL skbuff: dev type %d, len %d\n" , dev->type, skb->len);kfree_skb_reason (skb,SKB_DROP_REASON_CANXL_RX_INVALID_FRAME );return NET_RX_DROP ; } +can_set_skb_uid (skb, csx);can_receive (skb, dev);return NET_RX_SUCCESS ; }/* af_can protocol functions */
net/can/gw.c +3 -0
@@ -526,10 +526,13 @@ static void can_can_gw_rcv(struct sk_buff *skb, void *data) }/* put the incremented hop counter in the cloned skb */ ncsx->can_gw_hops = csx->can_gw_hops +1 ; +/* force a new CAN UID generation for the routed frame */ + ncsx->can_ext_flags = csx->can_ext_flags & ~CAN_EXT_UID ; +/* first processing of this CAN frame -> adjust to private hop limit */ if (gwj->limit_hops && ncsx->can_gw_hops ==1 ) ncsx->can_gw_hops = max_hops - gwj->limit_hops +1 ; nskb->dev = gwj->dst.dev;
net/can/isotp.c +2 -2
@@ -890,11 +890,11 @@ static void isotp_send_cframe(struct isotp_sock *so) } csx->can_iif = dev->ifindex;/* set uid in tx skb to identify CF echo frames */ -can_set_skb_uid (skb); +can_set_skb_uid (skb, csx); cf = (struct canfd_frame *)skb->data;skb_put_zero (skb, so->ll.mtu);/* create consecutive frame */
@@ -1221,11 +1221,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) } csx->can_iif = dev->ifindex;/* set uid in tx skb to identify CF echo frames */ -can_set_skb_uid (skb); +can_set_skb_uid (skb, csx); so->tx.len = size; so->tx.idx =0 ; cf = (struct canfd_frame *)skb->data;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 3/3] can: fix unique skb identifier regression under RPS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] can: silent packet drops due to shared state mutation in skb extension --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] can: silent packet drops due to shared state mutation in skb extension --
commit c8004f984caec4e36211ec16983dd32a18d7f101
Author: Oliver Hartkopp <[email protected]>
can: fix unique skb identifier regression under RPS
This commit addresses an RPS regression where statically generated hashes by the flow dissector cause legitimate frames to be discarded as duplicates in raw_rcv(). It attempts to fix this by explicitly tracking the CAN skb UID via a new CAN_EXT_UID flag in the skb extension (struct can_skb_ext), ensuring that skb->hash is overwritten only if the flag is not set.
Quoted 5 lines
net/can/af_can.c +0
-0
net/can/af_can.c static int can_rcv_filter(struct can_dev_rcv_lists *dev_rcv_lists, struct sk_buf · line 647
Quoted 9 lines
net/can/af_can.c +3
-2@@ -641,13 +641,18 @@ static int can_rcv_filter(struct can_dev_rcv_lists *dev_rcv_lists, struct sk_bufreturn matches; } -void can_set_skb_uid (struct sk_buff *skb) +void can_set_skb_uid (struct sk_buff *skb,struct can_skb_ext *csx) { -/* create non-zero unique skb identifier together with *skb */ +if (csx->can_ext_flags &CAN_EXT_UID ) +return ;
[Severity: High]
Could reading this shared flag to conditionally initialize a per-SKB local field (skb->hash) cause legitimate frames to be discarded?
If a CAN frame is cloned on the egress path (e.g., via tc mirred mirroring to another CAN interface), the original and the clone share the same skb_ext structure. Both devices call can_put_echo_skb() to create loopback echoes, which inherit the shared extension.
When these echoes arrive concurrently at can_rcv(), the first CPU sets CAN_EXT_UID in the shared extension.
The second CPU sees the flag already set and returns without assigning a unique skb->hash. Because the second clone retains the static RPS software hash, if the SLAB allocator reuses the memory address of a previously received frame, raw_rcv()'s per-CPU uniqframe check will falsely identify the new frame as a duplicate and drop it.
This appears to revive the RPS duplicate-drop regression this patch intended to fix.
net/can/af_can.c static int can_rcv_filter(struct can_dev_rcv_lists *dev_rcv_lists, struct sk_buf · line 655
Quoted 8 lines
net/can/af_can.c +4
-0+ +/* Overwrite pre-calculated network hashes with a unique CAN UID */ + skb->hash =0 ;while (!(skb->hash)) skb->hash =atomic_inc_return (&skbcounter); skb->sw_hash =1 ; + csx->can_ext_flags |=CAN_EXT_UID ;
[Severity: High]
Can this mutate the shared SKB extension in the RX softirq path without locking or unsharing, causing a data race and altering state for other clones?
Additionally, can_put_echo_skb() mutates csx->can_framelen on the TX path:
drivers/net/can/dev/skb.c:can_put_echo_skb() {
...
csx->can_framelen = frame_len;
...
}Because can_framelen and can_ext_flags share the same 4-byte memory region in struct can_skb_ext, does this race with can_set_skb_uid() during concurrent processing of mirrored clones?
Quoted 2 lines
net/can/af_can.c +0
-0}EXPORT_SYMBOL (can_set_skb_uid);
[ ... ]
net/can/af_can.c static void can_receive(struct sk_buff *skb, struct net_device *dev) · line 693
Quoted 7 lines
net/can/af_can.c +1
-2@@ -687,8 +690,9 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev)static int can_rcv (struct sk_buff *skb,struct net_device *dev,struct packet_type *pt,struct net_device *orig_dev) { -if (unlikely (!can_get_ml_priv (dev) || - !can_skb_ext_find (skb) || !can_is_can_skb (skb))) { +struct can_skb_ext *csx =can_skb_ext_find (skb);
[Severity: High]
Might retrieving the SKB extension here without ensuring exclusive ownership (e.g., via skb_ext_maybe_cow()) pass a potentially shared structure to a mutator?
Quoted 4 lines
net/can/af_can.c +2
-0+ +if (unlikely (!can_get_ml_priv (dev) || !csx || !can_is_can_skb (skb))) {pr_warn_once ("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d\n" , dev->type, skb->len);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Marc Kleine-Budde
maintainerQuoted 3 lines · Oliver Hartkopp
This patch set aggregates the open stable fixes for the CAN netlayer.
v2: restore non-zero UID value generation for isotp cfecho handling
I'll send a PR soonish. Once this series is ready, I'll send another PR.
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung Nürnberg | Phone: +49-5121-206917-129 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Oliver Hartkopp
maintainerQuoted 6 lines · Marc Kleine-Budde
Quoted 3 lines · Oliver Hartkopp
This patch set aggregates the open stable fixes for the CAN netlayer.
v2: restore non-zero UID value generation for isotp cfecho handling
I'll send a PR soonish. Once this series is ready, I'll send another PR.
Thanks Marc!
I've updated a v3 - maybe sashiko is fine now :-D
Best regards,
Oliver
Oliver Hartkopp
maintainerBtw. the first two patches of this patch set might go in immediately.
Quoted 18 lines · Oliver Hartkopp
Quoted 6 lines · Marc Kleine-Budde
Quoted 3 lines · Oliver Hartkopp
This patch set aggregates the open stable fixes for the CAN netlayer.
v2: restore non-zero UID value generation for isotp cfecho handling
I'll send a PR soonish. Once this series is ready, I'll send another PR.
Thanks Marc!
I've updated a v3 - maybe sashiko is fine now :-D
Best regards,
Oliver
Oliver Hartkopp
maintainerv5 is ready - Re: [PATCH v2 0/3] CAN netlayer fixes for stable
In reply to Oliver HartkoppHello Marc, hello Jörg,
Quoted 2 lines · Marc Kleine-Budde
Quoted 1 line
I'll send a PR soonish. Once this series is ready, I'll send another PR.
Done!
https://sashiko.dev/#/patchset/20260929163424.16382-1-socketcan%40hartkopp.net
The three patches are now ready for upstream.
Can you send the follow-up PR tomorrow, so that we can meet the net-PR window this Thursday?
The patch that addresses the RPS regression reported by Jörg is now also ready.
I was able to follow the bug report from Jörg:
# echo 1 > /sys/class/net/can0/queues/rx-0/rps_cpus
$ candump -n30000 any
$ cangen can1 -x -L 1 -D 00 -I 123 -g 0.3 -n30000
Before the fix cangen returned but candump still was waiting which indicates, that candump did not receive 30000 frames.
After the fix both tools quit at the same time. I therefore added my Tested-by tag too.
Best regards,
Oliver
Marc Kleine-Budde
maintainerQuoted 1 line · Oliver Hartkopp
This patch set aggregates the open stable fixes for the CAN netlayer.
pw-bot: cr
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung Nürnberg | Phone: +49-5121-206917-129 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |