Skip to contentSkip to conversation

staging: batman-adv: check kmalloc() return value

PATCH In review Vasiliy Kulikov 3 participants 5 messages Sep 12, 2010

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

PATCH 1/1 staging: batman-adv: check kmalloc() return value

kmalloc() may fail, if so drop current packet.

---
Compile tested.

 drivers/staging/batman-adv/routing.c |    6 ++++--
 drivers/staging/batman-adv/unicast.c |    8 ++++++--
 drivers/staging/batman-adv/unicast.h |    2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
drivers/staging/batman-adv/routing.c +4 -2
index e12fd99..e545260 100644
@@ -1232,8 +1232,10 @@ int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
 
 		orig_node->last_frag_packet = jiffies;
 
-		if (list_empty(&orig_node->frag_list))
-			create_frag_buffer(&orig_node->frag_list);
+		if (list_empty(&orig_node->frag_list)) {
+			if (create_frag_buffer(&orig_node->frag_list))
+				return NET_RX_DROP;
+		}
 
 		tmp_frag_entry  			search_frag_packet(&orig_node->frag_list,
drivers/staging/batman-adv/unicast.c +6 -2
index f951abc..f7d2206 100644
@@ -78,7 +78,7 @@ void create_frag_entry(struct list_head *head, struct sk_buff *skb)
 	return;
 }
 
-void create_frag_buffer(struct list_head *head)
+int create_frag_buffer(struct list_head *head)
 {
 	int i;
 	struct frag_packet_list_entry *tfp;
@@ -86,13 +86,17 @@ void create_frag_buffer(struct list_head *head)
 	for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
 		tfp = kmalloc(sizeof(struct frag_packet_list_entry),
 			GFP_ATOMIC);
+		if (tfp = NULL) {
+			frag_list_free(head);
+			return -ENOMEM;
+		}
 		tfp->skb = NULL;
 		tfp->seqno = 0;
 		INIT_LIST_HEAD(&tfp->list);
 		list_add(&tfp->list, head);
 	}
 
-	return;
+	return 0;
 }
 
 struct frag_packet_list_entry *search_frag_packet(struct list_head *head,
drivers/staging/batman-adv/unicast.h +1 -1
index 1d5cbeb..7973697 100644
@@ -30,7 +30,7 @@ struct sk_buff *merge_frag_packet(struct list_head *head,
 	struct sk_buff *skb);
 
 void create_frag_entry(struct list_head *head, struct sk_buff *skb);
-void create_frag_buffer(struct list_head *head);
+int create_frag_buffer(struct list_head *head);
 struct frag_packet_list_entry *search_frag_packet(struct list_head *head,
 	struct unicast_frag_packet *up);
 void frag_list_free(struct list_head *head);
Signature · 1 line
1.7.0.4
#2 lore raw
In reply to Vasiliy Kulikov
Quoted 1 line · Vasiliy Kulikov

kmalloc() may fail, if so drop current packet.

Thanks for your patch. The indention is right, but it adds just another bug (locking related).

Quoted 26 lines

---
Compile tested.

 drivers/staging/batman-adv/routing.c |    6 ++++--
 drivers/staging/batman-adv/unicast.c |    8 ++++++--
 drivers/staging/batman-adv/unicast.h |    2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/batman-adv/routing.c index e12fd99..e545260 100644

drivers/staging/batman-adv/routing.c +4 -2
@@ -1232,8 +1232,10 @@ int recv_ucast_frag_packet(struct sk_buff *skb,
 struct batman_if *recv_if)
 
 		orig_node->last_frag_packet = jiffies;
 
-		if (list_empty(&orig_node->frag_list))
-			create_frag_buffer(&orig_node->frag_list);
+		if (list_empty(&orig_node->frag_list)) {
+			if (create_frag_buffer(&orig_node->frag_list))
+				return NET_RX_DROP;
+		}
 
 		tmp_frag_entry =
Quoted 1 line

search_frag_packet(&orig_node->frag_list,

You must spin_unlock_irqrestore before you return from that function.

Best regards,
Sven

#3 lore raw

PATCH 1/1 batman-adv: Unlock on failure during fragmentation rx

When we detect a failure in create_frag_buffer we must drop the packet and unlock the orig_has which was locked before. 69d187ffb8e22024a87d8457e5dcb10e297108e9 forgot that and created a potential deadlock of the complete system.

---
 routing.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
routing.c +5 -3
index 23a12cd..a07e0e0 100644
@@ -1233,9 +1233,11 @@ int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
 
 		orig_node->last_frag_packet = jiffies;
 
-		if (list_empty(&orig_node->frag_list)) {
-			if (create_frag_buffer(&orig_node->frag_list))
-				return NET_RX_DROP;
+		if (list_empty(&orig_node->frag_list) &&
+			create_frag_buffer(&orig_node->frag_list)) {
+			spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
+					       flags);
+			return NET_RX_DROP;
 		}
 
 		tmp_frag_entry =
Signature · 1 line
1.7.2.3
#4 lore raw

Re: [B.A.T.M.A.N.] [PATCH] batman-adv: Unlock on failure during fragmentation rx

Quoted 6 lines · On Sunday 12 September 2010 23:00:42 Sven Eckelmann wrote:

When we detect a failure in create_frag_buffer we must drop the packet and unlock the orig_has which was locked before. 69d187ffb8e22024a87d8457e5dcb10e297108e9 forgot that and created a potential deadlock of the complete system.

Good catch - applied in revision 1791.

Thanks,
Marek