Skip to contentSkip to conversation

can: af_can: fix NULL pointer dereference in can_rcv_filter

PATCH In review Oliver Hartkopp 1 participant 2 messages Dec 9, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 can: af_can: fix NULL pointer dereference in can_rcv_filter

Analogue to commit 8aa59e355949 ("can: af_can: fix NULL pointer dereference in can_rx_register()") we need to check for a missing initialization of ml_priv in the receive path of CAN frames.

Since commit 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device") the check for dev->type to be ARPHRD_CAN is not sufficient anymore since bonding or tun netdevices claim to be CAN devices but do not initialize ml_priv accordingly.

Upstream commit 0acc442309a0 ("can: af_can: fix NULL pointer dereference in can_rcv_filter")

Fixes 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device")
Reported-by [email protected]
Cc [email protected] # 5.12 .. 6.0
---
 net/can/af_can.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/can/af_can.c +2 -2
index 1fb49d51b25d..4392f1d9c027 100644
@@ -678,11 +678,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 canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CAN_MTU)) {
+	if (unlikely(dev->type != ARPHRD_CAN || !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;
 	}
 
@@ -704,11 +704,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 canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
+	if (unlikely(dev->type != ARPHRD_CAN || !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;
 	}
 
Signature · 1 line
2.30.2
#1 lore raw

Just checked that the stable patch for 8aa59e355949 ("can: af_can: fix NULL pointer dereference in can_rx_register()") has been applied down to linux-5.4.y

Same works with this patch.

Just the current 6.1-rc has some changes in this section which makes this backport stable patch necessary.

Best,
Oliver

Quoted 51 lines · Oliver Hartkopp

Analogue to commit 8aa59e355949 ("can: af_can: fix NULL pointer dereference in can_rx_register()") we need to check for a missing initialization of ml_priv in the receive path of CAN frames.

Since commit 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device") the check for dev->type to be ARPHRD_CAN is not sufficient anymore since bonding or tun netdevices claim to be CAN devices but do not initialize ml_priv accordingly.

Upstream commit 0acc442309a0 ("can: af_can: fix NULL pointer dereference in can_rcv_filter")

Fixes 4e096a18867a ("net: introduce CAN specific pointer in the struct net_device")
Reported-by [email protected]
Cc [email protected] # 5.12 .. 6.0
---
  net/can/af_can.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/can/af_can.c +2 -2
index 1fb49d51b25d..4392f1d9c027 100644
@@ -678,11 +678,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 canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CAN_MTU)) {
+	if (unlikely(dev->type != ARPHRD_CAN || !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;
  	}
 
@@ -704,11 +704,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 canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
+	if (unlikely(dev->type != ARPHRD_CAN || !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;
  	}