batman-adv: reject unrepresentable multicast TVLV offsets
Subsystems (4)
PATCH v2 1/1 batman-adv: reject unrepresentable multicast TVLV offsets
From: Kyle Zeng <[email protected]>
The network and transport header fields in struct sk_buff are 16-bit offsets from skb->head, and U16_MAX is reserved as the unset transport header value. batadv_tvlv_call_handler() sets both fields from a received multicast TVLV without checking whether the TVLV end is representable.
If the end offset exceeds the field's range, skb_set_transport_header() truncates it so that the transport header precedes the network header. The negative difference is then returned by skb_network_header_len() as a large u32. batadv_mcast_forw_packet() consequently accepts an oversized multicast tracker and accesses memory beyond the skb data.
Add skb_set_transport_header_careful(), an offset-aware counterpart to skb_reset_transport_header_careful(), which validates the final head-relative offset before assigning it. Use the new helper in batadv_tvlv_call_handler() and reject unrepresentable TVLVs before setting the network header.
---
Changes in v2:
- Add an offset-aware careful transport-header setter and make batman-adv handle its failure, as suggested by Sven Eckelmann.
- Restore Kyle Zeng as the patch author and correct the sign-off chain.
- Move the research credit below the commit-message separator.
v1: https://lore.kernel.org/all/[email protected]/
Bug found and triaged by OpenAI Security Research and validated by Trail of Bits.
Trail of Bits has a reproducer for this bug that triggers a KASAN use-after-free and can share if needed.
include/linux/skbuff.h | 26 ++++++++++++++++++++++++++ net/batman-adv/tvlv.c | 5 ++++- 2 files changed, 30 insertions(+), 1 deletion(-)
include/linux/skbuff.h +26 -0
@@ -3126,6 +3126,32 @@ static inline void skb_set_transport_header(struct sk_buff *skb, skb->transport_header += offset; } +/** +* skb_set_transport_header_careful - conditionally set transport header +* @skb: buffer to alter +* @offset: offset to add to skb->data +* +* Hardened version of skb_set_transport_header(). +* +* Returns: true if the operation was a success. +*/ +static inline bool __must_check +skb_set_transport_header_careful (struct sk_buff *skb,const int offset) +{ +long transport_offset = skb->data - skb->head + offset; + +if (unlikely (transport_offset != + (typeof(skb->transport_header))transport_offset)) +return false; + +if (unlikely (transport_offset == + (typeof(skb->transport_header))~0U)) +return false; + + skb->transport_header = transport_offset; +return true; +} +static inline unsigned char *skb_network_header (const struct sk_buff *skb) {return skb->head + skb->network_header;
net/batman-adv/tvlv.c +4 -1
@@ -433,8 +433,11 @@ static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,return NET_RX_SUCCESS ; tvlv_offset = (unsigned char *)tvlv_value - skb->data; +if (!skb_set_transport_header_careful (skb, + tvlv_offset + tvlv_value_len)) +return -EINVAL ; +skb_set_network_header (skb, tvlv_offset); -skb_set_transport_header (skb, tvlv_offset + tvlv_value_len);return tvlv_handler->mcast_handler (bat_priv, skb); }
Signature · 1 line
2.53.0
Quoted 13 lines
include/linux/skbuff.h +7
-0@@ -3126,6 +3126,32 @@ static inline void skb_set_transport_header(struct sk_buff *skb +static inline bool __must_check +skb_set_transport_header_careful (struct sk_buff *skb,const int offset) +{ +long transport_offset = skb->data - skb->head + offset; + +if (unlikely (transport_offset != + (typeof(skb->transport_header))transport_offset))
include/linux/skbuff.h static inline void skb_set_transport_header(struct sk_buff *skb · line 3154
Quoted 10 lines
include/linux/skbuff.h +10
-0+return false; + +if (unlikely (transport_offset == + (typeof(skb->transport_header))~0U)) +return false; + + skb->transport_header = transport_offset; +return true; +} +
I personally don't like the way the lines are broken down. The 80 character limit was replaced by a 100 character limit since commit bdc48fa11e46 ("checkpatch/coding-style: deprecate 80-column warning"). And it seems like there are already multiple lines in this file which are longer than 80 characters. So it should be possible not to add a line break right after a comparison operator.
But I leave the judgement about this part to the netdev maintainers.
Quoted 7 lines
net/batman-adv/tvlv.c +0
-0@@ -438,8 +438,11 @@ static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,return NET_RX_SUCCESS ;
net/batman-adv/tvlv.c static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv, · line 448
Quoted 10 lines
net/batman-adv/tvlv.c +4
-1tvlv_offset = (unsigned char *)tvlv_value - skb->data; +if (!skb_set_transport_header_careful (skb, + tvlv_offset + tvlv_value_len)) +return -EINVAL ; +skb_set_network_header (skb, tvlv_offset); -skb_set_transport_header (skb, tvlv_offset + tvlv_value_len);return tvlv_handler->mcast_handler (bat_priv, skb); }
For the batman-adv part:
Signature · 1 line
Sven Eckelmann <[email protected]>
Jakub Kicinski
maintainerinclude/linux/skbuff.h static inline void skb_set_transport_header(struct sk_buff *skb, · line 3152
Quoted 12 lines · On Mon, 10 Aug 2026 14:57:51 +0000 David Lee wrote:
include/linux/skbuff.h +12
-0+long transport_offset = skb->data - skb->head + offset; + +if (unlikely (transport_offset != + (typeof(skb->transport_header))transport_offset)) +return false; + +if (unlikely (transport_offset == + (typeof(skb->transport_header))~0U)) +return false; + + skb->transport_header = transport_offset; +return true;
Find a better name for the variable instead of wrapping the lines.