FAILED: patch "[PATCH] can: replace ARPHRD_CAN type checks with can_get_ml_priv()" failed to apply to 5.10-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 962947b69589881167054bae5accc8abfd5aa4b6
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 962947b69589881167054bae5accc8abfd5aa4b6 Mon Sep 17 00:00:00 2001
From: Oliver Hartkopp <[email protected]>
Date: Tue, 27 Oct 2026 15:31:05 +0100
Subject: [PATCH] can: replace ARPHRD_CAN type checks with can_get_ml_priv()
Commit 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. A valid pointer returned by can_get_ml_priv() therefore identifies a real CAN device.
The referenced commit only converted the j1939 and proc code to this check, while the other parts of the CAN subsystem still rely on dev->type == ARPHRD_CAN.
The TUNSETLINK ioctl allows a down TUN/TAP device to set its hardware type to ARPHRD_CAN while dev->ml_priv remains NULL. Such a repurposed netdevice passes the ARPHRD_CAN checks.
Commit 8aa59e355949 ("can: af_can: fix NULL pointer dereference in can_rx_register()") already made can_rx_register() reject such devices. But can_rx_unregister() still only checks dev->type and dereferences the NULL ml_priv when it walks the receive lists of the device.
CAN_ISOTP reaches this path: isotp_bind() ignores the return values of can_rx_register() and binds the socket anyway. When the socket is released or the device is unregistered, isotp_release() and isotp_notify() remove the filters that have never been registered and trigger the NULL pointer dereference in can_rx_unregister(). This only requires owning a TUN/TAP device or CAP_NET_ADMIN in an unprivileged user namespace.
Convert the remaining ARPHRD_CAN checks in af_can, bcm, gw, isotp and raw to can_get_ml_priv().
--- net/can/af_can.c | 15 ++++++--------- 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(+), 19 deletions(-)
net/can/af_can.c +6 -9
@@ -226,7 +226,7 @@ int can_send(struct sk_buff *skb, int loop) goto inval_skb; } -if (unlikely (skb->dev->type !=ARPHRD_CAN )) { +if (unlikely (!can_get_ml_priv (skb->dev))) { err = -EPERM ; goto inval_skb; }
@@ -452,7 +452,7 @@ int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,/* 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)))
@@ -519,7 +519,7 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,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)))
@@ -684,8 +684,7 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev, {struct can_skb_ext *csx =can_skb_ext_find (skb); -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || - !csx || !can_is_can_skb (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);
@@ -710,8 +709,7 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev, {struct can_skb_ext *csx =can_skb_ext_find (skb); -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || - !csx || !can_is_canfd_skb (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);
@@ -736,8 +734,7 @@ static int canxl_rcv(struct sk_buff *skb, struct net_device *dev, {struct can_skb_ext *csx =can_skb_ext_find (skb); -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || - !csx || !can_is_canxl_skb (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);
net/can/bcm.c +4 -3
@@ -54,6 +54,7 @@#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>
@@ -1719,7 +1720,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) goto out_release; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) {dev_put (dev); ret = -ENODEV ; goto out_release;
@@ -1867,7 +1868,7 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
@@ -2024,7 +2025,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int 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
@@ -52,6 +52,7 @@#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>
@@ -612,7 +613,7 @@ static int cgw_notifier(struct notifier_block *nb,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 ) {
@@ -1163,7 +1164,7 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,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);
@@ -1171,7 +1172,7 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,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? */
net/can/isotp.c +3 -2
@@ -65,6 +65,7 @@#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>
@@ -1618,7 +1619,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }
@@ -1905,7 +1906,7 @@ static int isotp_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
net/can/raw.c +2 -2
@@ -348,7 +348,7 @@ static int raw_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
@@ -491,7 +491,7 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }
Signature · 1 line
2.53.0
Oliver Hartkopp
maintainerPATCH 1/4 can: raw: fix receiver memory leak
From: Ziyang Xuan <[email protected]>
commit ee8b94c8510ce64afe0b87ef548d23e00915fb10 upstream.
Got kmemleak errors with the following ltp can_filter testcase:
for ((i=1; i<=100; i++))
do
./can_filter &
sleep 0.1done
==============================================================
[<00000000db4a4943>] can_rx_register+0x147/0x360 [can] [<00000000a289549d>] raw_setsockopt+0x5ef/0x853 [can_raw] [<000000006d3d9ebd>] __sys_setsockopt+0x173/0x2c0 [<00000000407dbfec>] __x64_sys_setsockopt+0x61/0x70 [<00000000fd468496>] do_syscall_64+0x33/0x40 [<00000000b7e47d51>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
It's a bug in the concurrent scenario of unregister_netdevice_many() and raw_release() as following:
cpu0 cpu1unregister_netdevice_many(can_dev)
unlist_netdevice(can_dev) // dev_get_by_index() return NULL after this net_set_todo(can_dev)
raw_release(can_socket)
dev = dev_get_by_index(, ro->ifindex); // dev == NULL
if (dev) { // receivers in dev_rcv_lists not free because dev is NULL
raw_disable_allfilters(, dev, );
dev_put(dev);
}
...
ro->bound = 0;
...call_netdevice_notifiers(NETDEV_UNREGISTER, )
raw_notify(, NETDEV_UNREGISTER, )
if (ro->bound) // invalid because ro->bound has been set 0
raw_disable_allfilters(, dev, ); // receivers in dev_rcv_lists will never be freed
Add a net_device pointer member in struct raw_sock to record bound can_dev, and use rtnl_lock to serialize raw_socket members between raw_bind(), raw_release(), raw_setsockopt() and raw_notify(). Use ro->dev to decide whether to free receivers in dev_rcv_lists.
Stable-dep-of: 962947b69589 ("can: replace ARPHRD_CAN type checks with can_get_ml_priv()") [ backport to 5.10: Adapt the raw_release() context as
free_percpu(ro->uniq) has already been moved out of raw_release() in
this tree. ]
--- net/can/raw.c | 57 ++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 33 deletions(-)
net/can/raw.c +24 -33
@@ -83,6 +83,7 @@ struct raw_sock {struct sock sk;int bound;int ifindex; +struct net_device *dev;struct list_head notifier;int loopback;int recv_own_msgs;
@@ -275,7 +276,7 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,if (!net_eq (dev_net (dev),sock_net (sk)))return ; -if (ro->ifindex != dev->ifindex) +if (ro->dev != dev)return ;switch (msg) {
@@ -290,6 +291,7 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg, ro->ifindex =0 ; ro->bound =0 ; + ro->dev =NULL ; ro->count =0 ;release_sock (sk);
@@ -343,6 +345,7 @@ static int raw_init(struct sock *sk) ro->bound =0 ; ro->ifindex =0 ; + ro->dev =NULL ;/* set default filter to single entry dfilter */ ro->dfilter.can_id =0 ;
@@ -392,19 +395,13 @@ static int raw_release(struct socket *sock)lock_sock (sk); +rtnl_lock ();/* remove current filters & unregister */ if (ro->bound) { -if (ro->ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (sock_net (sk), ro->ifindex); -if (dev) { -raw_disable_allfilters (dev_net (dev), dev, sk); -dev_put (dev); - } - }else { +if (ro->dev) +raw_disable_allfilters (dev_net (ro->dev), ro->dev, sk); +else raw_disable_allfilters (sock_net (sk),NULL , sk); - } }if (ro->count >1 )
@@ -412,7 +409,9 @@ static int raw_release(struct socket *sock) ro->ifindex =0 ; ro->bound =0 ; + ro->dev =NULL ; ro->count =0 ; +rtnl_unlock ();sock_orphan (sk); sock->sk =NULL ;
@@ -428,6 +427,7 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;struct sock *sk = sock->sk;struct raw_sock *ro =raw_sk (sk); +struct net_device *dev =NULL ;int ifindex;int err =0 ;int notify_enetdown =0 ;
@@ -437,14 +437,13 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)if (addr->can_family !=AF_CAN )return -EINVAL ; +rtnl_lock ();lock_sock (sk);if (ro->bound && addr->can_ifindex == ro->ifindex) goto out;if (addr->can_ifindex) { -struct net_device *dev; - dev =dev_get_by_index (sock_net (sk), addr->can_ifindex);if (!dev) { err = -ENODEV ;
@@ -473,26 +472,20 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)if (!err) {if (ro->bound) {/* unregister old filters */ -if (ro->ifindex) { -struct net_device *dev; - - dev =dev_get_by_index (sock_net (sk), - ro->ifindex); -if (dev) { -raw_disable_allfilters (dev_net (dev), - dev, sk); -dev_put (dev); - } - }else { +if (ro->dev) +raw_disable_allfilters (dev_net (ro->dev), + ro->dev, sk); +else raw_disable_allfilters (sock_net (sk),NULL , sk); - } } ro->ifindex = ifindex; ro->bound =1 ; + ro->dev = dev; } out:release_sock (sk); +rtnl_unlock ();if (notify_enetdown) { sk->sk_err =ENETDOWN ;
@@ -558,9 +551,9 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,rtnl_lock ();lock_sock (sk); -if (ro->bound && ro->ifindex) { - dev =dev_get_by_index (sock_net (sk), ro->ifindex); -if (!dev) { + dev = ro->dev; +if (ro->bound && dev) { +if (dev->reg_state !=NETREG_REGISTERED ) {if (count >1 )kfree (filter); err = -ENODEV ;
@@ -601,7 +594,6 @@ static int raw_setsockopt(struct socket *sock, int level, int optname, ro->count = count; out_fil: -dev_put (dev);release_sock (sk);rtnl_unlock ();
@@ -619,9 +611,9 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,rtnl_lock ();lock_sock (sk); -if (ro->bound && ro->ifindex) { - dev =dev_get_by_index (sock_net (sk), ro->ifindex); -if (!dev) { + dev = ro->dev; +if (ro->bound && dev) { +if (dev->reg_state !=NETREG_REGISTERED ) { err = -ENODEV ; goto out_err; }
@@ -645,7 +637,6 @@ static int raw_setsockopt(struct socket *sock, int level, int optname, ro->err_mask = err_mask; out_err: -dev_put (dev);release_sock (sk);rtnl_unlock ();
Oliver Hartkopp
maintainerPATCH 2/4 can: raw: fix lockdep issue in raw_release()
From: Eric Dumazet <[email protected]>
commit 11c9027c983e9e4b408ee5613b6504d24ebd85be upstream.
syzbot complained about a lockdep issue [1]
Since raw_bind() and raw_setsockopt() first get RTNL
before locking the socket, we must adopt the same order in raw_release()
[1]
WARNING: possible circular locking dependency detected 6.5.0-rc1-syzkaller-00192-g78adb4bcf99e #0 Not tainted
------------------------------------------------------
syz-executor.0/14110 is trying to acquire lock:ffff88804e4b6130 (sk_lock-AF_CAN){+.+.}-{0:0}, at: lock_sock include/net/sock.h:1708 [inline] ffff88804e4b6130 (sk_lock-AF_CAN){+.+.}-{0:0}, at: raw_bind+0xb1/0xab0 net/can/raw.c:435
but task is already holding lock:ffffffff8e3df368 (rtnl_mutex){+.+.}-{3:3}, at: raw_bind+0xa7/0xab0 net/can/raw.c:434
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (rtnl_mutex){+.+.}-{3:3}:
__mutex_lock_common kernel/locking/mutex.c:603 [inline] __mutex_lock+0x181/0x1340 kernel/locking/mutex.c:747 raw_release+0x1c6/0x9b0 net/can/raw.c:391 __sock_release+0xcd/0x290 net/socket.c:654 sock_close+0x1c/0x20 net/socket.c:1386 __fput+0x3fd/0xac0 fs/file_table.c:384 task_work_run+0x14d/0x240 kernel/task_work.c:179 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] exit_to_user_mode_loop kernel/entry/common.c:171 [inline] exit_to_user_mode_prepare+0x210/0x240 kernel/entry/common.c:204 __syscall_exit_to_user_mode_work kernel/entry/common.c:286 [inline] syscall_exit_to_user_mode+0x1d/0x50 kernel/entry/common.c:297 do_syscall_64+0x44/0xb0 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x63/0xcd
-> #0 (sk_lock-AF_CAN){+.+.}-{0:0}:
check_prev_add kernel/locking/lockdep.c:3142 [inline] check_prevs_add kernel/locking/lockdep.c:3261 [inline] validate_chain kernel/locking/lockdep.c:3876 [inline] __lock_acquire+0x2e3d/0x5de0 kernel/locking/lockdep.c:5144 lock_acquire kernel/locking/lockdep.c:5761 [inline] lock_acquire+0x1ae/0x510 kernel/locking/lockdep.c:5726 lock_sock_nested+0x3a/0xf0 net/core/sock.c:3492 lock_sock include/net/sock.h:1708 [inline] raw_bind+0xb1/0xab0 net/can/raw.c:435 __sys_bind+0x1ec/0x220 net/socket.c:1792 __do_sys_bind net/socket.c:1803 [inline] __se_sys_bind net/socket.c:1801 [inline] __x64_sys_bind+0x72/0xb0 net/socket.c:1801 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(rtnl_mutex);
lock(sk_lock-AF_CAN);
lock(rtnl_mutex);lock(sk_lock-AF_CAN);
*** DEADLOCK ***
1 lock held by syz-executor.0/14110:
stack backtrace: CPU: 0 PID: 14110 Comm: syz-executor.0 Not tainted 6.5.0-rc1-syzkaller-00192-g78adb4bcf99e #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/03/2023 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xd9/0x1b0 lib/dump_stack.c:106 check_noncircular+0x311/0x3f0 kernel/locking/lockdep.c:2195 check_prev_add kernel/locking/lockdep.c:3142 [inline] check_prevs_add kernel/locking/lockdep.c:3261 [inline] validate_chain kernel/locking/lockdep.c:3876 [inline] __lock_acquire+0x2e3d/0x5de0 kernel/locking/lockdep.c:5144 lock_acquire kernel/locking/lockdep.c:5761 [inline] lock_acquire+0x1ae/0x510 kernel/locking/lockdep.c:5726 lock_sock_nested+0x3a/0xf0 net/core/sock.c:3492 lock_sock include/net/sock.h:1708 [inline] raw_bind+0xb1/0xab0 net/can/raw.c:435 __sys_bind+0x1ec/0x220 net/socket.c:1792 __do_sys_bind net/socket.c:1803 [inline] __se_sys_bind net/socket.c:1801 [inline] __x64_sys_bind+0x72/0xb0 net/socket.c:1801 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7fd89007cb29 Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 e1 20 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fd890d2a0c8 EFLAGS: 00000246 ORIG_RAX: 0000000000000031 RAX: ffffffffffffffda RBX: 00007fd89019bf80 RCX: 00007fd89007cb29 RDX: 0000000000000010 RSI: 0000000020000040 RDI: 0000000000000003 RBP: 00007fd8900c847a R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 000000000000000b R14: 00007fd89019bf80 R15: 00007ffebf8124f8 </TASK>
Stable-dep-of: 962947b69589 ("can: replace ARPHRD_CAN type checks with can_get_ml_priv()") [ backport to 5.10: Adapt the raw_release() context as
free_percpu(ro->uniq) has already been moved out of raw_release() in
this tree. ]
--- net/can/raw.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/can/raw.c +3 -2
@@ -393,9 +393,9 @@ static int raw_release(struct socket *sock)list_del (&ro->notifier);spin_unlock (&raw_notifier_lock); +rtnl_lock ();lock_sock (sk); -rtnl_lock ();/* remove current filters & unregister */ if (ro->bound) {if (ro->dev)
@@ -411,12 +411,13 @@ static int raw_release(struct socket *sock) ro->bound =0 ; ro->dev =NULL ; ro->count =0 ; -rtnl_unlock ();sock_orphan (sk); sock->sk =NULL ;release_sock (sk); +rtnl_unlock (); +sock_put (sk);return 0 ;
Oliver Hartkopp
maintainerPATCH 3/4 can: raw: add missing refcount for memory leak fix
In reply to Oliver Hartkoppcommit c275a176e4b69868576e543409927ae75e3a3288 upstream.
Commit ee8b94c8510c ("can: raw: fix receiver memory leak") introduced a new reference to the CAN netdevice that has assigned CAN filters. But this new ro->dev reference did not maintain its own refcount which lead to another KASAN use-after-free splat found by Eric Dumazet.
This patch ensures a proper refcount for the CAN nedevice.
Stable-dep-of: 962947b69589 ("can: replace ARPHRD_CAN type checks with can_get_ml_priv()") [ backport to 5.10: Use dev_hold()/dev_put() as in the 5.15-stable
backport, as this tree has no netdevice reference tracker. ]
--- net/can/raw.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)
net/can/raw.c +25 -9
@@ -283,8 +283,10 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,case NETDEV_UNREGISTER :lock_sock (sk);/* remove current filters & unregister */ -if (ro->bound) +if (ro->bound) {raw_disable_allfilters (dev_net (dev), dev, sk); +dev_put (dev); + }if (ro->count >1 )kfree (ro->filter);
@@ -398,10 +400,12 @@ static int raw_release(struct socket *sock)/* remove current filters & unregister */ if (ro->bound) { -if (ro->dev) +if (ro->dev) {raw_disable_allfilters (dev_net (ro->dev), ro->dev, sk); -else +dev_put (ro->dev); + }else {raw_disable_allfilters (sock_net (sk),NULL , sk); + } }if (ro->count >1 )
@@ -451,10 +455,10 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) goto out; }if (dev->type !=ARPHRD_CAN ) { -dev_put (dev); err = -ENODEV ; - goto out; + goto out_put_dev; } +if (!(dev->flags &IFF_UP )) notify_enetdown =1 ;
@@ -462,7 +466,9 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)/* filters set by default/setsockopt */ err =raw_enable_allfilters (sock_net (sk), dev, sk); -dev_put (dev); +if (err) + goto out_put_dev; + }else { ifindex =0 ;
@@ -473,18 +479,28 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)if (!err) {if (ro->bound) {/* unregister old filters */ -if (ro->dev) +if (ro->dev) {raw_disable_allfilters (dev_net (ro->dev), ro->dev, sk); -else +/* drop reference to old ro->dev */ +dev_put (ro->dev); + }else {raw_disable_allfilters (sock_net (sk),NULL , sk); + } } ro->ifindex = ifindex; ro->bound =1 ; +/* bind() ok -> hold a reference for new ro->dev */ ro->dev = dev; +if (ro->dev) +dev_hold (ro->dev); } - out: +out_put_dev: +/* remove potential reference from dev_get_by_index() */ +if (dev) +dev_put (dev); +out:release_sock (sk);rtnl_unlock ();
Oliver Hartkopp
maintainerPATCH 4/4 can: replace ARPHRD_CAN type checks with can_get_ml_priv()
In reply to Oliver Hartkoppcommit 962947b69589881167054bae5accc8abfd5aa4b6 upstream.
Commit 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. A valid pointer returned by can_get_ml_priv() therefore identifies a real CAN device.
The referenced commit only converted the j1939 and proc code to this check, while the other parts of the CAN subsystem still rely on dev->type == ARPHRD_CAN.
The TUNSETLINK ioctl allows a down TUN/TAP device to set its hardware type to ARPHRD_CAN while dev->ml_priv remains NULL. Such a repurposed netdevice passes the ARPHRD_CAN checks.
Commit 8aa59e355949 ("can: af_can: fix NULL pointer dereference in can_rx_register()") already made can_rx_register() reject such devices. But can_rx_unregister() still only checks dev->type and dereferences the NULL ml_priv when it walks the receive lists of the device.
CAN_ISOTP reaches this path: isotp_bind() ignores the return values of can_rx_register() and binds the socket anyway. When the socket is released or the device is unregistered, isotp_release() and isotp_notify() remove the filters that have never been registered and trigger the NULL pointer dereference in can_rx_unregister(). This only requires owning a TUN/TAP device or CAP_NET_ADMIN in an unprivileged user namespace.
Convert the remaining ARPHRD_CAN checks in af_can, bcm, gw, isotp and raw to can_get_ml_priv().
[ backport to 5.10: This tree has no can_skb_ext, so only the ARPHRD_CAN
check is removed from the conditions in can_rcv() and canfd_rcv(). There is no canxl_rcv() in this tree. Add the <linux/can/can-ml.h> include to raw.c, which upstream got with commit faba5860fcf9 ("can:
raw: instantly reject disabled CAN frames"). ]
--- net/can/af_can.c | 10 +++++----- net/can/bcm.c | 7 ++++--- net/can/gw.c | 7 ++++--- net/can/isotp.c | 5 +++-- net/can/raw.c | 5 +++-- 5 files changed, 19 insertions(+), 15 deletions(-)
net/can/af_can.c +5 -5
@@ -225,7 +225,7 @@ int can_send(struct sk_buff *skb, int loop) goto inval_skb; } -if (unlikely (skb->dev->type !=ARPHRD_CAN )) { +if (unlikely (!can_get_ml_priv (skb->dev))) { err = -EPERM ; goto inval_skb; }
@@ -452,7 +452,7 @@ int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,/* 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)))
@@ -519,7 +519,7 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,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)))
@@ -687,7 +687,7 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev, {struct canfd_frame *cfd = (struct canfd_frame *)skb->data; -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || skb->len !=CAN_MTU )) { +if (unlikely (!can_get_ml_priv (dev) || skb->len !=CAN_MTU )) {pr_warn_once ("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d\n" , dev->type, skb->len); goto free_skb;
@@ -713,7 +713,7 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev, {struct canfd_frame *cfd = (struct canfd_frame *)skb->data; -if (unlikely (dev->type !=ARPHRD_CAN || !can_get_ml_priv (dev) || skb->len !=CANFD_MTU )) { +if (unlikely (!can_get_ml_priv (dev) || skb->len !=CANFD_MTU )) {pr_warn_once ("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n" , dev->type, skb->len); goto free_skb;
net/can/bcm.c +4 -3
@@ -54,6 +54,7 @@#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>
@@ -1663,7 +1664,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) goto out_release; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) {dev_put (dev); ret = -ENODEV ; goto out_release;
@@ -1811,7 +1812,7 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
@@ -1967,7 +1968,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len, 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
@@ -52,6 +52,7 @@#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>
@@ -541,7 +542,7 @@ static int cgw_notifier(struct notifier_block *nb,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 ) {
@@ -1092,7 +1093,7 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,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);
@@ -1100,7 +1101,7 @@ static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,if (!gwj->dst.dev) goto out; -if (gwj->dst.dev->type !=ARPHRD_CAN ) +if (!can_get_ml_priv (gwj->dst.dev)) goto out;ASSERT_RTNL ();
net/can/isotp.c +3 -2
@@ -65,6 +65,7 @@#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>
@@ -1550,7 +1551,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len) err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }
@@ -1850,7 +1851,7 @@ static int isotp_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
net/can/raw.c +3 -2
@@ -49,6 +49,7 @@#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/raw.h>
@@ -315,7 +316,7 @@ static int raw_notifier(struct notifier_block *nb, unsigned long msg, {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 ;
@@ -454,7 +455,7 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) err = -ENODEV ; goto out; } -if (dev->type !=ARPHRD_CAN ) { +if (!can_get_ml_priv (dev)) { err = -ENODEV ; goto out_put_dev; }
Re: [PATCH 5.10.y 1/4] can: raw: fix receiver memory leak
In reply to Oliver HartkoppQuoted 1 line
can: raw: fix receiver memory leak
Queued the series for 5.10, thanks.
Signature · 2 lines
Thanks, Sasha