Skip to contentSkip to conversation

batman-adv: fix fragment reassembly length accounting

PATCH Superseded Ren Wei 2 participants 2 messages May 11 1 message not in the archive Batman advanced

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ren Wei

author
#1 lore raw

PATCH 1/1 batman-adv: fix fragment reassembly length accounting

In reply to a message not in the archive

From: Ruide Cao <[email protected]>

batman-adv keeps a running payload length for queued fragments and uses it to validate a fragment chain before reassembly.

That accounting currently uses a type that can truncate the accumulated value. As a result, malformed fragment chains can bypass the intended validation and drive reassembly with inconsistent length state, leading to a local denial of service.

Fix the accounting by using a non-truncating type for the accumulated fragment length so the existing validation logic always operates on the real value.

The fix was verified against the original reproducer and against valid fragment reassembly paths.

Fixes 610bfc6bc99b ("batman-adv: Receive fragmented packets and merge")
---
 net/batman-adv/fragmentation.c | 12 +++++++-----
 net/batman-adv/types.h         |  2 +-
 2 files changed, 8 insertions(+), 6 deletions(-)
net/batman-adv/fragmentation.c +9 -7
index f4e45cc25816..e4472dbef4d9 100644
@@ -80,9 +80,9 @@ void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
  *
  * Return: the maximum size of payload that can be fragmented.
  */
-static int batadv_frag_size_limit(void)
+static u32 batadv_frag_size_limit(void)
 {
-	int limit = BATADV_FRAG_MAX_FRAG_SIZE;
+	u32 limit = BATADV_FRAG_MAX_FRAG_SIZE;
 
 	limit -= sizeof(struct batadv_frag_packet);
 	limit *= BATADV_FRAG_MAX_FRAGMENTS;
@@ -141,6 +141,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 	struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
 	struct batadv_frag_list_entry *frag_entry_last = NULL;
 	struct batadv_frag_packet *frag_packet;
-	u8 bucket;
-	u16 seqno, hdr_size = sizeof(struct batadv_frag_packet);
+	u32 data_len;
+	u16 seqno, hdr_size = sizeof(struct batadv_frag_packet);
+	u8 bucket;
 	bool ret = false;
@@ -153,6 +154,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 		goto err;
 
 	frag_packet = (struct batadv_frag_packet *)skb->data;
+	data_len = skb->len - hdr_size;
 	seqno = ntohs(frag_packet->seqno);
 	bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
 
@@ -171,7 +173,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 	spin_lock_bh(&chain->lock);
 	if (batadv_frag_init_chain(chain, seqno)) {
 		hlist_add_head(&frag_entry_new->list, &chain->fragment_list);
-		chain->size = skb->len - hdr_size;
+		chain->size = data_len;
 		chain->timestamp = jiffies;
 		chain->total_size = ntohs(frag_packet->total_size);
 		ret = true;
@@ -188,7 +190,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 		if (frag_entry_curr->no < frag_entry_new->no) {
 			hlist_add_before(&frag_entry_new->list,
 					 &frag_entry_curr->list);
-			chain->size += skb->len - hdr_size;
+			chain->size += data_len;
 			chain->timestamp = jiffies;
 			ret = true;
 			goto out;
@@ -201,7 +203,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 	/* Reached the end of the list, so insert after 'frag_entry_last'. */
 	if (likely(frag_entry_last)) {
 		hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
-		chain->size += skb->len - hdr_size;
+		chain->size += data_len;
 		chain->timestamp = jiffies;
 		ret = true;
 	}
net/batman-adv/types.h +1 -1
index 8fc5fe0e9b05..96ea4c70fd59 100644
@@ -300,7 +300,7 @@ struct batadv_frag_table_entry {
 	u16 seqno;
 
 	/** @size: accumulated size of packets in list */
-	u16 size;
+	u32 size;
 
 	/** @total_size: expected size of the assembled packet */
 	u16 total_size;
Signature · 1 line
2.34.1
maintainer
#2 lore raw

net/batman-adv/fragmentation.c void batadv_frag_purge_orig(struct batadv_orig_node *orig_node, · line 85

Quoted 13 lines · Ren Wei
net/batman-adv/fragmentation.c +2 -2
index f4e45cc2..c6c25be7 100644
@@ -80,9 +80,9 @@ void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
  *
  * Return: the maximum size of payload that can be fragmented.
  */
-static int batadv_frag_size_limit(void)
+static u32 batadv_frag_size_limit(void)
 {
-	int limit = BATADV_FRAG_MAX_FRAG_SIZE;
+	u32 limit = BATADV_FRAG_MAX_FRAG_SIZE;

size_t

net/batman-adv/fragmentation.c static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node, · line 144

Quoted 6 lines
net/batman-adv/fragmentation.c +1 -1
@@ -141,8 +141,9 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 	struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
 	struct batadv_frag_list_entry *frag_entry_last = NULL;
 	struct batadv_frag_packet *frag_packet;
-	u8 bucket;
+	u32 data_len;

size_t

net/batman-adv/fragmentation.c static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node, · line 193

Quoted 6 lines
net/batman-adv/fragmentation.c +1 -1
@@ -188,7 +190,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 		if (frag_entry_curr->no < frag_entry_new->no) {
 			hlist_add_before(&frag_entry_new->list,
 					 &frag_entry_curr->list);
-			chain->size += skb->len - hdr_size;
+			chain->size += data_len;

Let us ignore for the moment the reality of wifi/ethernet an widen our scope:
The sk_buff len is an unsigned int (equal to u32 for all we care). To make the "non-truncating type" true, you would need to use a much larger type. Or you can add a check for the overflow:

bool overflow = false;
...

if (check_add_overflow(chain->size, data_len, &chain->size)) {
	overflow = true;
	goto out;
}

...

if (overflow || chain->size > batadv_frag_size_limit() ||

	...
	/* Clear chain if total size of either the list or the packet
	...

Don't forget to include linux/overflow.h

net/batman-adv/fragmentation.c static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node, · line 206

Quoted 6 lines
net/batman-adv/fragmentation.c +1 -1
@@ -201,7 +203,7 @@ static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
 	/* Reached the end of the list, so insert after 'frag_entry_last'. */
 	if (likely(frag_entry_last)) {
 		hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
-		chain->size += skb->len - hdr_size;
+		chain->size += data_len;

See above

net/batman-adv/types.h struct batadv_frag_table_entry { · line 303

Quoted 11 lines
net/batman-adv/types.h +1 -1
index 8fc5fe0e..96ea4c70 100644
@@ -300,7 +300,7 @@ struct batadv_frag_table_entry {
 	u16 seqno;
 
 	/** @size: accumulated size of packets in list */
-	u16 size;
+	u32 size;

size_t

Signature · 1 line
Sven Eckelmann <[email protected]>