batman-adv: Refresh patches
[feed/routing.git] / batman-adv / patches / 0018-batman-adv-Prevent-duplicated-nc_node-entry.patch
1 From: Sven Eckelmann <sven@narfation.org>
2 Date: Thu, 6 Sep 2018 14:35:25 +0200
3 Subject: batman-adv: Prevent duplicated nc_node entry
4
5 The function batadv_nc_get_nc_node is responsible for adding new nc_nodes
6 to the in_coding_list and out_coding_list. It first checks whether the
7 entry already is in the list or not. If it is, then the creation of a new
8 entry is aborted.
9
10 But the lock for the list is only held when the list is really modified.
11 This could lead to duplicated entries because another context could create
12 an entry with the same key between the check and the list manipulation.
13
14 The check and the manipulation of the list must therefore be in the same
15 locked code section.
16
17 Fixes: 3ed7ada3f0bb ("batman-adv: network coding - detect coding nodes and remove these after timeout")
18 Signed-off-by: Sven Eckelmann <sven@narfation.org>
19 Acked-by: Marek Lindner <mareklindner@neomailbox.ch>
20
21 Origin: upstream, https://git.open-mesh.org/batman-adv.git/commit/bab8447ad1850b25188f9652c0c52f8e58acd656
22
23 diff --git a/net/batman-adv/network-coding.c b/net/batman-adv/network-coding.c
24 index c3578444f3cbe759a5385ac460ccb9d41ae1c4de..34caf129a9bf5531360f798be6a7059bad26a50f 100644
25 --- a/net/batman-adv/network-coding.c
26 +++ b/net/batman-adv/network-coding.c
27 @@ -854,24 +854,6 @@ batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
28 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
29 struct list_head *list;
30
31 - /* Check if nc_node is already added */
32 - nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
33 -
34 - /* Node found */
35 - if (nc_node)
36 - return nc_node;
37 -
38 - nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
39 - if (!nc_node)
40 - return NULL;
41 -
42 - /* Initialize nc_node */
43 - INIT_LIST_HEAD(&nc_node->list);
44 - kref_init(&nc_node->refcount);
45 - ether_addr_copy(nc_node->addr, orig_node->orig);
46 - kref_get(&orig_neigh_node->refcount);
47 - nc_node->orig_node = orig_neigh_node;
48 -
49 /* Select ingoing or outgoing coding node */
50 if (in_coding) {
51 lock = &orig_neigh_node->in_coding_list_lock;
52 @@ -881,13 +863,34 @@ batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
53 list = &orig_neigh_node->out_coding_list;
54 }
55
56 + spin_lock_bh(lock);
57 +
58 + /* Check if nc_node is already added */
59 + nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
60 +
61 + /* Node found */
62 + if (nc_node)
63 + goto unlock;
64 +
65 + nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
66 + if (!nc_node)
67 + goto unlock;
68 +
69 + /* Initialize nc_node */
70 + INIT_LIST_HEAD(&nc_node->list);
71 + kref_init(&nc_node->refcount);
72 + ether_addr_copy(nc_node->addr, orig_node->orig);
73 + kref_get(&orig_neigh_node->refcount);
74 + nc_node->orig_node = orig_neigh_node;
75 +
76 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
77 nc_node->addr, nc_node->orig_node->orig);
78
79 /* Add nc_node to orig_node */
80 - spin_lock_bh(lock);
81 kref_get(&nc_node->refcount);
82 list_add_tail_rcu(&nc_node->list, list);
83 +
84 +unlock:
85 spin_unlock_bh(lock);
86
87 return nc_node;