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.54]) (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 E84D94E36FD for ; Mon, 28 Sep 2026 17:03:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=85.215.255.54 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790614987; cv=pass; b=tJ2m6+16D+rnJXMoCFu4UpK6rEK/8gO0RScib+picHWq9dfxEfMbdUcKwENZpNyPpAa2UjHLVE+R2jcqJwYMxa4mwRiQMtsXi3dhzGbW2ULe0Hv+AosPsE7mqI5jaZHWMBwCYfCRieMLqbY+8sTZBIkiBqrWhMLMVTGwjEv4tIw= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790614987; c=relaxed/simple; bh=ptqkqXfF2DcrmahEmA4fL3Lw92/sEik7vu1G3oQtGi8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=q0U8CsXxeCwgQ2nY0U0Ko0rk3pZih7PqXEsRMlhTV3CU0rFFfB8ot/Uc8ptEn8+9NwsaIBuVfITlla4Q/3fMj4iebnWVgEXBl/Mw8S1070Tk+SZMpdU+pK6w3BhNDEmoD+zPKwrgWe5kPqhmKN33BXPG24F6nMIsx3pQZDwkqh0= 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=X4muRBo6; dkim=permerror (0-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b=yzkljZZ+; arc=pass smtp.client-ip=85.215.255.54 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="X4muRBo6"; dkim=permerror (0-bit key) header.d=hartkopp.net header.i=@hartkopp.net header.b="yzkljZZ+" ARC-Seal: i=1; a=rsa-sha256; t=1790614804; cv=none; d=strato.com; s=strato-dkim-0002; b=q4BjQ1vSz9xpRx2DQ9Jfd0k5Nuh9bx5FCWrJpedOGjxxTQqJtJ9s3wiG7EHKUOWB3u kqqporYAqY+P+K/HXGHSnzD8HdImXjU8T8fJ9WEaFcfIsBsjPnafx6w8OsChyMorNeDe 2HQREjDVa01SQp3MehlDfgOJ8WJ6d/hqhfVsdZx5phIEnaqIRGeDBCzb2FGehZbiptWB X3PH+TI2Wk58cXXDgrjB477ngYkiIX8ijDu0e6LxYZw+TbHZG3FbXYAi8EQWFO5bPcmY KjyxsXy40UEAlnkViqBeUqRMVknpXbpQ750desU1LoVv1x/iU9ePX/sb+smwjjSsqEVf keAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1790614804; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=2ssivLe5HAtDONaKIlRuSposwTJpUwI+9sfyOC2UrFc=; b=TMptfl5hLo6Eidi0tvseDXmED0MOyGs9Z685No6CGsEZLy92WGN8Cbv9VlkGdeHqqh 6pgFU7JCHxcHOuPvi1IlG/YHusKhp3H0qB5AbBsfqAODx5zXFIzhgfVZ/xEtTIC5rWSe nAlwB6lsIZCpH7XLtVMyBXTRwPgknE6StKf+Z6Df2rf9y36hwJ8Bom3//XHSqLrXjzYH uCcit162eFUSbl6JwedkHXP3nuyXalNbSULyzZi7/GuwKRa7GgF6tu5FQPWobi6EOUnf F2jMhoI11+TJBR/W/hHaSrHUhCYN9T6UGgtJlanOCy2YpZVFDKQ4946AJtfNFRbFts76 JSHw== 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=1790614804; s=strato-dkim-0002; d=hartkopp.net; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=2ssivLe5HAtDONaKIlRuSposwTJpUwI+9sfyOC2UrFc=; b=X4muRBo6ZcdDpdmrY5dic31k6YSHYvmB6LyXnsFK6ikdgNwlUcN1SnLCs46T0swzAu a+kEh+sfyelNTwpKBd8shmh7NAqM3nZYrwpK8f9+q+N5zti8EtFOar+eHSVCYjhZtJR4 SgqNYq8UrK6Z8dwrtDpazSDVmPWBTd9L5zo/Ah8r4sqI3L5FJWgRkou0S6EZmqt3xLAd 7cj9Dpa3w4/zUSk0Of5VXUXKCcUwJ/0Bzk5EdsHaUlp8iX0Ic5Db/JY4DLtZQ/EGh0cp 8ZYxgcQBAMwlDite783neh+2E5awFhdsG+y9gtfj09v0q44deClqbz8cwzv4+uGl3sBo gTZA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1790614804; s=strato-dkim-0003; d=hartkopp.net; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=2ssivLe5HAtDONaKIlRuSposwTJpUwI+9sfyOC2UrFc=; b=yzkljZZ+PUpJp+gGoaKdum7YFivGuokU/r8ecNXSuJOwVXJRAaxlcrGLjhP9CL7rEq Nmi0Q6cvgSKsLBRRPHDg== X-RZG-AUTH: ":P2MHfkW8eP4Mre39l357AZT/I7AY/7nT2yrDxb8mjH4JKvMdQv2tTUsMrZpkO3Mw3lZ/t54cFxeEQ7s8bDup0Q==" Received: from vivo.lan by smtp.strato.de (RZmta 55.6.2 AUTH) with ESMTPSA id K04b9a28SH04Ofi (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Mon, 28 Sep 2026 19:00:04 +0200 (CEST) From: Oliver Hartkopp To: linux-can@vger.kernel.org Cc: Oliver Hartkopp , stable@kernel.org, Oleksij Rempel Subject: [PATCH 2/3] can: convert unreliable ARPHRD_CAN type checks to robust can_get_ml_priv() Date: Mon, 28 Sep 2026 18:59:26 +0200 Message-ID: <20260928165927.31667-3-socketcan@hartkopp.net> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260928165927.31667-1-socketcan@hartkopp.net> References: <20260928165927.31667-1-socketcan@hartkopp.net> 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. 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(). Fixes: 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device") Cc: stable@kernel.org Cc: Oleksij Rempel Signed-off-by: Oliver Hartkopp --- 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(-) diff --git a/net/can/af_can.c b/net/can/af_can.c index 7bc86b176b4d..ef435f22ac93 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; @@ -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); diff --git a/net/can/bcm.c b/net/can/bcm.c index cd3522ec32c0..940b917e3830 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 @@ -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; } diff --git a/net/can/gw.c b/net/can/gw.c index 0ec99f68aa45..d9912dea738b 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 @@ -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)) { diff --git a/net/can/isotp.c b/net/can/isotp.c index 0835a4758a72..d3f79efc9c1e 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 @@ -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; diff --git a/net/can/raw.c b/net/can/raw.c index c5596fc9aac5..7c48ff36e6cd 100644 --- a/net/can/raw.c +++ b/net/can/raw.c @@ -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)) -- 2.53.0