From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mo4-p01-ob.smtp.rzone.de (mo4-p01-ob.smtp.rzone.de [85.215.255.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D82FA44781E for ; Fri, 16 Oct 2026 09:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.53 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792143342; cv=pass; b=AOU7uWmQf0cdGQ8HeqoaNODSkFRWRxrvkFa0dbfud3NLl8x6Pd9nNHzqAMPsGIUVEqT8DgxBO39ALqGgukVfrst2rrTPrBjC1o7ArXbqHWLFJfpsTp5hUTaP8LuB5FydvyLVK2yY5hs7efL1JeqeF2rqAM2Ih6+04f2Y+twasFg= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792143342; c=relaxed/simple; bh=ioEKN4XwsXbBg6dcz7uSRwZMvxm32xUkGn/NEpXUt6M=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=bZiTx6LsiUap5qxEYtEz0SUdK/KYFYz2HXK22CA84uxXde3YLUpbvxQwvcqf/Wu4TM9F8Fz72wm9pnvd7wCD1JI77w+tB94/i5V/sVhfEVZi+R4wAb/wE0A+AjW2OIv/m3FNPIjQsx6FPAnh10gSBanoJ7P6l0G6bWEic8tHWy4= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=hartkopp.net; spf=fail smtp.mailfrom=hartkopp.net; dkim=pass (2048-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b=KF2uFRKJ; dkim=permerror (0-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b=E2XwaiUs; arc=pass smtp.client-ip=85.215.255.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=hartkopp.net Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=hartkopp.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b="KF2uFRKJ"; dkim=permerror (0-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b="E2XwaiUs" ARC-Seal: i=1; a=rsa-sha256; t=1792143331; cv=none; d=strato.com; s=strato-dkim-0002; b=aqqWHOJaqEMVflHWxFUecs6u6AvWcD42e5aX1l3++qxXKSAL5Q5ATP2vmRzZ5sLVKO iVBZ5dwAJ4Z72jr5oLB/x1FmmM/EbufUgK3Ppu37Ku1XR8gczFhTGZE+8eamOPccsBjF QpuXJnIxxNcCCJLmsSGnoHtngU58oLG+Z96AjDMzex8c2Q43aY9fIDT9cZujFmAQnYpX XzfV8he4DzSptENqYKdQX1SmwRSwze29Yx9ZxWTOXQvDgLl4V40nN55khOImfEaUvDt2 FswpQj1LrV/iTNtBg8EdA6kRNetOtbTdVFDTnUl8cA5phQE1VAxMe4Z1fy0WhsOanQx7 GKKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1792143331; s=strato-dkim-0002; d=strato.com; h=Message-ID:Date:Subject:Cc:To:From:Cc:Date:From:Subject:Sender; bh=DKjl75GjhFkI3dR76Uf/6lyhDWT+Ww5DsaYRh3x+GZo=; b=AqiLgYseurEcBGWUcJwe9mTPLlDNQ3QGvBlIw2Hlk14qWp4gnnE5P/7tS/sINjLtZL 0y4RE4OkVQSQpjXUYKQ+6cSU/LkRCuAXtNCD2ZtSCOHK66O+VMKMKsLVyQhNvJ4+tHrq Ib8gnKLiZPeFinL8I4l6y1Yvd0Z1XoMj2NoYd33dTbKfimhX4mBJXT4xv08YBKjypfAe FPnDCnvqzME4CXwUABpqr4bJ46YhiNSWqbf1KgUSHVbuXy/CY2aRDJt3Hxt9Bsg89Sb8 N/EQZd54255bdKHNwXDDHbzkfPDlmhBYTr+EvO6u17K5T5SBl8yq8XefvyDtdLVCyexT acdw== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1792143331; s=strato-dkim-0002; d=hartkopp.net; h=Message-ID:Date:Subject:Cc:To:From:Cc:Date:From:Subject:Sender; bh=DKjl75GjhFkI3dR76Uf/6lyhDWT+Ww5DsaYRh3x+GZo=; b=KF2uFRKJ3XRP08fzDvhttzZCCuNgZDsJIMvkePtv88Mpp29PQ4YS6TXKKEJQIzJWvW hrRMkIghfPDn2hG1aK3oCmUCZDVpz++LjpGt7amLovSwk8rKtHfH2fT/8kf8IxD/Eb2N HNtl2WH/wWiYTk9MhL7EAatYEiLJ6OrnBbiD8ybtE29dSlc17XlsU+bNc5Nk9DgY59Nx bho5s4YPBgZD44scDU18OEBdyDrRua4EIK23zTSeJE1836QNTcwN4wj9riupcLsYOJs2 RCX+crqZjSNIP8kPPK6FEs2erb/HgxH3dmo0K6frlz11VMXMVD2Q1eFvvGCxM0B/L4uu /a7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1792143331; s=strato-dkim-0003; d=hartkopp.net; h=Message-ID:Date:Subject:Cc:To:From:Cc:Date:From:Subject:Sender; bh=DKjl75GjhFkI3dR76Uf/6lyhDWT+Ww5DsaYRh3x+GZo=; b=E2XwaiUsAvzOksYkWJB9KOIYFAjhOO0KSq6icGA6ZhadtUFaQXkb4/ni/fvRJQ1vyo dXe0XZ2EVq8c99B93ODA== X-RZG-AUTH: ":P2MHfkW8eP4Mre39l357AZT/I7AY/7nT2yrDxb8mjH4JKvMdQv2tTUsMrZpkO3Mw3lZ/t54cFxeEQ7s8bDup0Q==" Received: from vivo.lan by smtp.strato.de (RZmta 55.6.3 AUTH) with ESMTPSA id Kfdf2a29G9ZVLRP (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 16 Oct 2026 11:35:31 +0200 (CEST) From: Oliver Hartkopp To: linux-can@vger.kernel.org Cc: Oliver Hartkopp , stable@kernel.org, Oleksij Rempel , Marc Kleine-Budde Subject: [PATCH can-next v2] can: replace ARPHRD_CAN type checks with can_get_ml_priv() Date: Fri, 16 Oct 2026 11:35:25 +0200 Message-ID: <20261016093525.327367-1-socketcan@hartkopp.net> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-can@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="us-ascii" 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 in the CAN subsystem, which can lead to a NULL pointer dereference of the CAN specific ml_priv. This only requires owning a TUN/TAP device or CAP_NET_ADMIN in an unprivileged user namespace. can_rx_register() already rejects such a device since commit 8aa59e355949 ("can: af_can: prevent NULL pointer dereference in can_rx_register()"), but can_rx_unregister() only checks dev->type and dereferences the NULL ml_priv in can_dev_rcv_lists_find(). This path is reachable via isotp: isotp_bind() only checked for ARPHRD_CAN and ignored the -ENODEV from can_rx_register(), so the socket was marked as bound to the TUN device. A later isotp_release() or NETDEV_UNREGISTER notification then crashes in can_rx_unregister(). Replace all remaining ARPHRD_CAN checks in net/can with can_get_ml_priv(). For can_rx_register() and the can*_rcv() functions this is a simplification without functional change. Fixes: 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device") Cc: stable@kernel.org Cc: Oleksij Rempel Signed-off-by: Oliver Hartkopp Link: https://patch.msgid.link/20261012164802.94113-1-socketcan@hartkopp.net Signed-off-by: Marc Kleine-Budde --- 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(-) diff --git a/net/can/af_can.c b/net/can/af_can.c index 34fe3b28d576..dc27ace43719 100644 --- a/net/can/af_can.c +++ b/net/can/af_can.c @@ -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; @@ -682,12 +682,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) { 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); kfree_skb_reason(skb, SKB_DROP_REASON_CAN_RX_INVALID_FRAME); return NET_RX_DROP; @@ -708,12 +707,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) { 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); kfree_skb_reason(skb, SKB_DROP_REASON_CANFD_RX_INVALID_FRAME); return NET_RX_DROP; @@ -734,12 +732,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) { 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); kfree_skb_reason(skb, SKB_DROP_REASON_CANXL_RX_INVALID_FRAME); return NET_RX_DROP; diff --git a/net/can/bcm.c b/net/can/bcm.c index 3d637a1e0ac1..60406439a13f 100644 --- a/net/can/bcm.c +++ b/net/can/bcm.c @@ -52,10 +52,11 @@ #include #include #include #include #include +#include #include #include #include #include #include @@ -1717,11 +1718,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; } @@ -1865,11 +1866,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; @@ -2022,11 +2023,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; } diff --git a/net/can/gw.c b/net/can/gw.c index 5793cb2420ed..54bb5bd3242a 100644 --- a/net/can/gw.c +++ b/net/can/gw.c @@ -50,10 +50,11 @@ #include #include #include #include #include +#include #include #include #include #include #include @@ -610,11 +611,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; @@ -1161,19 +1162,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)) { diff --git a/net/can/isotp.c b/net/can/isotp.c index f5dc9d04bd68..6c28802c0605 100644 --- a/net/can/isotp.c +++ b/net/can/isotp.c @@ -63,10 +63,11 @@ #include #include #include #include #include +#include #include #include #include #include #include @@ -1608,11 +1609,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; @@ -1895,11 +1896,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; diff --git a/net/can/raw.c b/net/can/raw.c index 0acd4f6c6dd6..e1d741a25be3 100644 --- a/net/can/raw.c +++ b/net/can/raw.c @@ -346,11 +346,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; @@ -489,11 +489,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)) -- 2.53.0