[v2] batman-adv: Clone shared bat packets before modifying them

Message ID 1268586955-29220-1-git-send-email-sven.eckelmann@gmx.de (mailing list archive)
State Accepted, archived
Headers

Commit Message

Sven Eckelmann March 14, 2010, 5:15 p.m. UTC
  "tcpdump" and "batctl td" will receive packets with a wrong sequence
number on systems with a different endianess than network byte order.
This happens due to the reordering of bytes in the function which
handles aggregated bat packets. The function which receives the bat
packets must ensure that these buffers aren't shared with anything else
before that function tries to write into it. Otherwise it has to copy
the buffers so it is save again to change them.

Reported-by: Kevin Steen <batman@kevinsteen.net>
Signed-off-by: Sven Eckelmann <sven.eckelmann@gmx.de>
---
 batman-adv-kernelland/routing.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
  

Comments

Marek Lindner March 14, 2010, 5:52 p.m. UTC | #1
On Monday 15 March 2010 01:15:55 Sven Eckelmann wrote:
> "tcpdump" and "batctl td" will receive packets with a wrong sequence
> number on systems with a different endianess than network byte order.
> This happens due to the reordering of bytes in the function which
> handles aggregated bat packets. The function which receives the bat
> packets must ensure that these buffers aren't shared with anything else
> before that function tries to write into it. Otherwise it has to copy
> the buffers so it is save again to change them.

Applied in rev 1598.

Thanks for this quick fix!

Cheers,
Marek
  
Kevin Steen March 14, 2010, 7:33 p.m. UTC | #2
Works correctly on my systems now - thanks.

-Kevin
  

Patch

diff --git a/batman-adv-kernelland/routing.c b/batman-adv-kernelland/routing.c
index 8610b22..0051259 100644
--- a/batman-adv-kernelland/routing.c
+++ b/batman-adv-kernelland/routing.c
@@ -680,6 +680,7 @@  int recv_bat_packet(struct sk_buff *skb,
 {
 	struct ethhdr *ethhdr;
 	unsigned long flags;
+	struct sk_buff *skb_old;
 
 	/* drop packet if it has not necessary minimum size */
 	if (skb_headlen(skb) < sizeof(struct batman_packet))
@@ -695,12 +696,19 @@  int recv_bat_packet(struct sk_buff *skb,
 	if (is_bcast(ethhdr->h_source))
 		return NET_RX_DROP;
 
-	spin_lock_irqsave(&orig_hash_lock, flags);
 	/* TODO: we use headlen instead of "length", because
 	 * only this data is paged in. */
-	/* TODO: is another skb_copy needed here? there will be
-	 * written on the data, but nobody (?) should further use
-	 * this data */
+
+	/* create a copy of the skb, if needed, to modify it. */
+	if (!skb_clone_writable(skb, skb_headlen(skb))) {
+		skb_old = skb;
+		skb = skb_copy(skb, GFP_ATOMIC);
+		if (!skb)
+			return NET_RX_DROP;
+		kfree_skb(skb_old);
+	}
+
+	spin_lock_irqsave(&orig_hash_lock, flags);
 	receive_aggr_bat_packet(ethhdr,
 				skb->data,
 				skb_headlen(skb),