Skip to contentSkip to conversation

can: ensure an initialized headroom in outgoing CAN sk_buffs

PATCH In review Oliver Hartkopp 2 participants 4 messages Dec 7, 2019 – Dec 9, 2019

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

PATCH 1/1 can: ensure an initialized headroom in outgoing CAN sk_buffs

KMSAN sysbot detected a read access to an untinitialized value in the headroom of an outgoing CAN related sk_buff. When using CAN sockets this area is filled appropriately - but when using a packet socket this initialization is missing.

The problematic read access occurs in the CAN receive path which can only be triggered when the sk_buff is sent through a (virtual) CAN interface. So we check in the sending path whether we need to perform the missing initializations.

Fixes d3b58c47d330d ("can: replace timestamp as unique skb attribute")
Reported-by [email protected]
---
 include/linux/can/dev.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
include/linux/can/dev.h +35 -0
index 9b3c720a31b1..8f86e7a1f8e9 100644
@@ -18,6 +18,7 @@
 #include <linux/can/error.h>
 #include <linux/can/led.h>
 #include <linux/can/netlink.h>
+#include <linux/can/skb.h>
 #include <linux/netdevice.h>
 
 /*
@@ -91,6 +92,37 @@ struct can_priv {
 #define get_can_dlc(i)		(min_t(__u8, (i), CAN_MAX_DLC))
 #define get_canfd_dlc(i)	(min_t(__u8, (i), CANFD_MAX_DLC))
 
+/* Check for outgoing skbs that have not been created by the CAN subsystem */
+static inline bool can_check_skb_headroom(struct net_device *dev,
+					  struct sk_buff *skb)
+{
+	/* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
+	if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
+		return true;
+
+	/* af_packet does not apply CAN skb specific settings */
+	if (skb->ip_summed == CHECKSUM_NONE) {
+
+		/* init headroom */
+		can_skb_prv(skb)->ifindex = dev->ifindex;
+		can_skb_prv(skb)->skbcnt = 0;
+
+		skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+		/* preform proper loopback on capable devices */
+		if (dev->flags & IFF_ECHO)
+			skb->pkt_type = PACKET_LOOPBACK;
+		else
+			skb->pkt_type = PACKET_HOST;
+
+		skb_reset_mac_header(skb);
+		skb_reset_network_header(skb);
+		skb_reset_transport_header(skb);
+	}
+
+	return false;
+}
+
 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
 static inline bool can_dropped_invalid_skb(struct net_device *dev,
 					  struct sk_buff *skb)
@@ -108,6 +140,9 @@ static inline bool can_dropped_invalid_skb(struct net_device *dev,
 	} else
 		goto inval_skb;
 
+	if (can_check_skb_headroom(dev, skb))
+		goto inval_skb;
+
 	return false;
 
 inval_skb:
Signature · 1 line
2.20.1
#1 lore raw
Quoted 29 lines · Oliver Hartkopp

KMSAN sysbot detected a read access to an untinitialized value in the headroom of an outgoing CAN related sk_buff. When using CAN sockets this area is filled appropriately - but when using a packet socket this initialization is missing.

The problematic read access occurs in the CAN receive path which can only be triggered when the sk_buff is sent through a (virtual) CAN interface. So we check in the sending path whether we need to perform the missing initializations.

Fixes d3b58c47d330d ("can: replace timestamp as unique skb attribute")
Reported-by [email protected]
---
 include/linux/can/dev.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
include/linux/can/dev.h +1 -0
index 9b3c720a31b1..8f86e7a1f8e9 100644
@@ -18,6 +18,7 @@
 #include <linux/can/error.h>
 #include <linux/can/led.h>
 #include <linux/can/netlink.h>
+#include <linux/can/skb.h>
 #include <linux/netdevice.h>
 
 /*

include/linux/can/dev.h struct can_priv { · line 97

Quoted 6 lines
include/linux/can/dev.h +3 -0
@@ -91,6 +92,37 @@ struct can_priv {
 #define get_can_dlc(i)		(min_t(__u8, (i), CAN_MAX_DLC))
 #define get_canfd_dlc(i)	(min_t(__u8, (i), CANFD_MAX_DLC))
 
+/* Check for outgoing skbs that have not been created by the CAN subsystem */
+static inline bool can_check_skb_headroom(struct net_device *dev,
+					  struct sk_buff *skb)

Do we want to have such a big function as a static inline?

include/linux/can/dev.h struct can_priv { · line 104

Quoted 7 lines
include/linux/can/dev.h +7 -0
+{
+	/* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
+	if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
+		return true;
+
+	/* af_packet does not apply CAN skb specific settings */
+	if (skb->ip_summed == CHECKSUM_NONE) {

Is it possible to set the ip_summed via the packet socket or is it always 0 (== CHECKSUM_NONE)?

include/linux/can/dev.h struct can_priv { · line 105

Quoted 1 line
include/linux/can/dev.h +1 -0
+

Please remove that empty line.

Quoted 24 lines
include/linux/can/dev.h +20 -0
+		/* init headroom */
+		can_skb_prv(skb)->ifindex = dev->ifindex;
+		can_skb_prv(skb)->skbcnt = 0;
+
+		skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+		/* preform proper loopback on capable devices */
+		if (dev->flags & IFF_ECHO)
+			skb->pkt_type = PACKET_LOOPBACK;
+		else
+			skb->pkt_type = PACKET_HOST;
+
+		skb_reset_mac_header(skb);
+		skb_reset_network_header(skb);
+		skb_reset_transport_header(skb);
+	}
+
+	return false;
+}
+
 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
 static inline bool can_dropped_invalid_skb(struct net_device *dev,
 					  struct sk_buff *skb)

include/linux/can/dev.h static inline bool can_dropped_invalid_skb(struct net_device *dev, · line 143

Quoted 4 lines
include/linux/can/dev.h +1 -0
@@ -108,6 +140,9 @@ static inline bool can_dropped_invalid_skb(struct net_device *dev,
 	} else
 		goto inval_skb;
 
+	if (can_check_skb_headroom(dev, skb))

Can you rename the function, so that it's clear that returning false means it's an invalid skb?

include/linux/can/dev.h static inline bool can_dropped_invalid_skb(struct net_device *dev, · line 148

Quoted 6 lines
include/linux/can/dev.h +2 -0
+		goto inval_skb;
+
 	return false;
 
 inval_skb:

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#2 lore raw
Quoted 6 lines · Marc Kleine-Budde
Quoted 3 lines · Oliver Hartkopp

KMSAN sysbot detected a read access to an untinitialized value in the headroom of an outgoing CAN related sk_buff. When using CAN sockets this area is filled appropriately - but when using a packet socket this initialization is missing.

Quoted 6 lines

include/linux/can/dev.h struct can_priv { · line 97

Quoted 3 lines
include/linux/can/dev.h +3 -0
+/* Check for outgoing skbs that have not been created by the CAN subsystem */
+static inline bool can_check_skb_headroom(struct net_device *dev,
+					  struct sk_buff *skb)

Do we want to have such a big function as a static inline?

No. Usually not. can_dropped_invalid_skb() has the same problem IMO.

This additional inline function approach was the only way to ensure simple backwards portability for stable kernels.

I would suggest to clean this up once this patch went into mainline.

Quoted 10 lines

include/linux/can/dev.h struct can_priv { · line 104

Quoted 7 lines
include/linux/can/dev.h +7 -0
+{
+	/* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
+	if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
+		return true;
+
+	/* af_packet does not apply CAN skb specific settings */
+	if (skb->ip_summed == CHECKSUM_NONE) {

Is it possible to set the ip_summed via the packet socket or is it always 0 (== CHECKSUM_NONE)?

af_packet only reads ip_summed in the receive path. It is not set when creating the outgoing skb where the struct skb is mainly zero'ed.

Quoted 5 lines
Quoted 2 lines

+

Please remove that empty line.

ok.

Quoted 27 lines
Quoted 27 lines
include/linux/can/dev.h +20 -0
+		/* init headroom */
+		can_skb_prv(skb)->ifindex = dev->ifindex;
+		can_skb_prv(skb)->skbcnt = 0;
+
+		skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+		/* preform proper loopback on capable devices */
+		if (dev->flags & IFF_ECHO)
+			skb->pkt_type = PACKET_LOOPBACK;
+		else
+			skb->pkt_type = PACKET_HOST;
+
+		skb_reset_mac_header(skb);
+		skb_reset_network_header(skb);
+		skb_reset_transport_header(skb);
+	}
+
+	return false;
+}
+
  /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
  static inline bool can_dropped_invalid_skb(struct net_device *dev,
  					  struct sk_buff *skb)
@@ -108,6 +140,9 @@ static inline bool can_dropped_invalid_skb(struct net_device *dev,
  	} else
  		goto inval_skb;
 
Quoted 5 lines

include/linux/can/dev.h static inline bool can_dropped_invalid_skb(struct net_device *dev, · line 143

Quoted 1 line
include/linux/can/dev.h +1 -0
+	if (can_check_skb_headroom(dev, skb))

Can you rename the function, so that it's clear that returning false means it's an invalid skb?

Returning 'false' is *good* like in can_dropped_invalid_skb(). What naming would you suggest?

Regards,
Oliver

#3 lore raw
Quoted 16 lines · Oliver Hartkopp
Quoted 6 lines · Marc Kleine-Budde
Quoted 3 lines · Oliver Hartkopp

KMSAN sysbot detected a read access to an untinitialized value in the headroom of an outgoing CAN related sk_buff. When using CAN sockets this area is filled appropriately - but when using a packet socket this initialization is missing.

Quoted 6 lines

include/linux/can/dev.h struct can_priv { · line 97

Quoted 3 lines
include/linux/can/dev.h +3 -0
+/* Check for outgoing skbs that have not been created by the CAN subsystem */
+static inline bool can_check_skb_headroom(struct net_device *dev,
+					  struct sk_buff *skb)

Do we want to have such a big function as a static inline?

Quoted 4 lines

No. Usually not. can_dropped_invalid_skb() has the same problem IMO.

This additional inline function approach was the only way to ensure simple backwards portability for stable kernels.

A "normal" function called from the can_dropped_invalid_skb() works aswell.

Quoted 51 lines

I would suggest to clean this up once this patch went into mainline.

Quoted 10 lines

include/linux/can/dev.h struct can_priv { · line 104

Quoted 7 lines
include/linux/can/dev.h +7 -0
+{
+	/* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
+	if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
+		return true;
+
+	/* af_packet does not apply CAN skb specific settings */
+	if (skb->ip_summed == CHECKSUM_NONE) {

Is it possible to set the ip_summed via the packet socket or is it always 0 (== CHECKSUM_NONE)?

af_packet only reads ip_summed in the receive path. It is not set when creating the outgoing skb where the struct skb is mainly zero'ed.

Quoted 5 lines
Quoted 2 lines

+

Please remove that empty line.

ok.

Quoted 27 lines
Quoted 27 lines
include/linux/can/dev.h +20 -0
+		/* init headroom */
+		can_skb_prv(skb)->ifindex = dev->ifindex;
+		can_skb_prv(skb)->skbcnt = 0;
+
+		skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+		/* preform proper loopback on capable devices */
+		if (dev->flags & IFF_ECHO)
+			skb->pkt_type = PACKET_LOOPBACK;
+		else
+			skb->pkt_type = PACKET_HOST;
+
+		skb_reset_mac_header(skb);
+		skb_reset_network_header(skb);
+		skb_reset_transport_header(skb);
+	}
+
+	return false;
+}
+
  /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
  static inline bool can_dropped_invalid_skb(struct net_device *dev,
  					  struct sk_buff *skb)
@@ -108,6 +140,9 @@ static inline bool can_dropped_invalid_skb(struct net_device *dev,
  	} else
  		goto inval_skb;
 
Quoted 8 lines
Quoted 5 lines

include/linux/can/dev.h static inline bool can_dropped_invalid_skb(struct net_device *dev, · line 143

Quoted 1 line
include/linux/can/dev.h +1 -0
+	if (can_check_skb_headroom(dev, skb))

Can you rename the function, so that it's clear that returning false means it's an invalid skb?

Returning 'false' is *good* like in can_dropped_invalid_skb(). What naming would you suggest?

Something like: can_skb_headroom_valid().

See v2 I just sent around.

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |