aboutsummaryrefslogtreecommitdiffstats
path: root/net/batman-adv/network-coding.c
diff options
context:
space:
mode:
authorSven Eckelmann <sven@narfation.org>2018-08-12 15:04:42 -0400
committerSimon Wunderlich <sw@simonwunderlich.de>2018-09-06 07:55:58 -0400
commitfa122fec8640eb7186ce5a41b83a4c1744ceef8f (patch)
tree6f5c2c76e2cf3ca47a54d444e9af0f0320cc30c1 /net/batman-adv/network-coding.c
parentdff9bc42ab0b2d38c5e90ddd79b238fed5b4c7ad (diff)
batman-adv: Prevent duplicated nc_node entry
The function batadv_nc_get_nc_node is responsible for adding new nc_nodes to the in_coding_list and out_coding_list. It first checks whether the entry already is in the list or not. If it is, then the creation of a new entry is aborted. But the lock for the list is only held when the list is really modified. This could lead to duplicated entries because another context could create an entry with the same key between the check and the list manipulation. The check and the manipulation of the list must therefore be in the same locked code section. Fixes: d56b1705e28c ("batman-adv: network coding - detect coding nodes and remove these after timeout") Signed-off-by: Sven Eckelmann <sven@narfation.org> Acked-by: Marek Lindner <mareklindner@neomailbox.ch> Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
Diffstat (limited to 'net/batman-adv/network-coding.c')
-rw-r--r--net/batman-adv/network-coding.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/net/batman-adv/network-coding.c b/net/batman-adv/network-coding.c
index c3578444f3cb..34caf129a9bf 100644
--- a/net/batman-adv/network-coding.c
+++ b/net/batman-adv/network-coding.c
@@ -854,16 +854,27 @@ batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
854 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */ 854 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
855 struct list_head *list; 855 struct list_head *list;
856 856
857 /* Select ingoing or outgoing coding node */
858 if (in_coding) {
859 lock = &orig_neigh_node->in_coding_list_lock;
860 list = &orig_neigh_node->in_coding_list;
861 } else {
862 lock = &orig_neigh_node->out_coding_list_lock;
863 list = &orig_neigh_node->out_coding_list;
864 }
865
866 spin_lock_bh(lock);
867
857 /* Check if nc_node is already added */ 868 /* Check if nc_node is already added */
858 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding); 869 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
859 870
860 /* Node found */ 871 /* Node found */
861 if (nc_node) 872 if (nc_node)
862 return nc_node; 873 goto unlock;
863 874
864 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC); 875 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
865 if (!nc_node) 876 if (!nc_node)
866 return NULL; 877 goto unlock;
867 878
868 /* Initialize nc_node */ 879 /* Initialize nc_node */
869 INIT_LIST_HEAD(&nc_node->list); 880 INIT_LIST_HEAD(&nc_node->list);
@@ -872,22 +883,14 @@ batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
872 kref_get(&orig_neigh_node->refcount); 883 kref_get(&orig_neigh_node->refcount);
873 nc_node->orig_node = orig_neigh_node; 884 nc_node->orig_node = orig_neigh_node;
874 885
875 /* Select ingoing or outgoing coding node */
876 if (in_coding) {
877 lock = &orig_neigh_node->in_coding_list_lock;
878 list = &orig_neigh_node->in_coding_list;
879 } else {
880 lock = &orig_neigh_node->out_coding_list_lock;
881 list = &orig_neigh_node->out_coding_list;
882 }
883
884 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n", 886 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
885 nc_node->addr, nc_node->orig_node->orig); 887 nc_node->addr, nc_node->orig_node->orig);
886 888
887 /* Add nc_node to orig_node */ 889 /* Add nc_node to orig_node */
888 spin_lock_bh(lock);
889 kref_get(&nc_node->refcount); 890 kref_get(&nc_node->refcount);
890 list_add_tail_rcu(&nc_node->list, list); 891 list_add_tail_rcu(&nc_node->list, list);
892
893unlock:
891 spin_unlock_bh(lock); 894 spin_unlock_bh(lock);
892 895
893 return nc_node; 896 return nc_node;