Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2970892imu; Sun, 9 Dec 2018 13:59:19 -0800 (PST) X-Google-Smtp-Source: AFSGD/X3WFlqqzzUpHahBFcXEyDcPctW3hFhcBnh8JC/EvfGEvdZe2R32rM8mD0ovIuxaFZP5LVU X-Received: by 2002:a63:2222:: with SMTP id i34mr8082398pgi.83.1544392759388; Sun, 09 Dec 2018 13:59:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544392759; cv=none; d=google.com; s=arc-20160816; b=Q0YP4pg2XG2rB99bzVzvE1pp3QVVFrpIrlKIH1A8N+Mjt7ie9q9APY1drgN5Ku8XGZ tmJ4oxln0c4r5YbN6XV+DiAVNazWRUQE6yV7QENugaJvqXveZsBbn8qc/QY/XTYQ5A6O TyfijwqgqxeS3y8jI0tdF+9r3tutXTUlNHq1KB4quXnIPCtyyZOe0CBnaK/M7Pv1QLk8 eW5khhjPOdcKEA9p1iPMrpWT3rdMXgCiDhTSykfSVy3ClFP6xBi+neTdngUedrkzNKUS wb2j0ixhTKXN8FMZBR1aQpEoyWLSOvHkjI06iCmZD7vcFpfBLpBJ3CExH8TwWuiG5+Cd 6Mzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=s7ogoKSm0JYdbF/C+Uwoy79kCj1yVd7OA8oXZAl2FkU=; b=JriM/gT8vZaUfAJ9T8hC7o+nV8eecah+gT8FWAso7sovGVlF6zqoKEA4zsKZH6CwKG sZ1BdkUY68aaiGgeH4N7cjZNDzd4KA2AHOByzbOlbdWl8ft/3rYf1obIuoAr9d89Rv4n e5FRVt4CPGoiELOYLtnaCH3JvamApATVQ66GXMdqAKVJR1ngKndLqbIATaUnsFm6OKUG gByNutxS+3/gVG2cBqyYWx7/s9YWCEZSGZsTSiDVIO4gPobxSRFrWN9OKns+EW8DPeW+ n9Hg2TiBlbfNLl90zILGFJddh1g0y21PASMwXfE1F6KvOMGYeMzVF3RQPnkTUuCeDOZh GWfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g9si9058549plb.54.2018.12.09.13.59.04; Sun, 09 Dec 2018 13:59:19 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727038AbeLIV5k (ORCPT + 99 others); Sun, 9 Dec 2018 16:57:40 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:36336 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726783AbeLIVzk (ORCPT ); Sun, 9 Dec 2018 16:55:40 -0500 Received: from pub.yeoldevic.com ([81.174.156.145] helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gW734-0002pr-HO; Sun, 09 Dec 2018 21:55:38 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72g-0003WD-OG; Sun, 09 Dec 2018 21:55:14 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Sven Eckelmann" , "Simon Wunderlich" , "Marek Lindner" Date: Sun, 09 Dec 2018 21:50:33 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 213/328] batman-adv: Prevent duplicated gateway_node entry In-Reply-To: X-SA-Exim-Connect-IP: 81.174.156.145 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.62-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Sven Eckelmann commit dff9bc42ab0b2d38c5e90ddd79b238fed5b4c7ad upstream. The function batadv_gw_node_add is responsible for adding new gw_node to the gateway_list. It is expecting that the caller already checked that there is not already an entry with the same key or not. 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: c6c8fea29769 ("net: Add batman-adv meshing protocol") Signed-off-by: Sven Eckelmann Acked-by: Marek Lindner Signed-off-by: Simon Wunderlich [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- net/batman-adv/gateway_client.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) --- a/net/batman-adv/gateway_client.c +++ b/net/batman-adv/gateway_client.c @@ -27,6 +27,7 @@ #include #include #include +#include /* These are the offsets of the "hw type" and "hw address length" in the dhcp * packet starting at the beginning of the dhcp header @@ -398,6 +399,9 @@ out: * @bat_priv: the bat priv with all the soft interface information * @orig_node: originator announcing gateway capabilities * @gateway: announced bandwidth information + * + * Has to be called with the appropriate locks being acquired + * (gw.list_lock). */ static void batadv_gw_node_add(struct batadv_priv *bat_priv, struct batadv_orig_node *orig_node, @@ -405,6 +409,8 @@ static void batadv_gw_node_add(struct ba { struct batadv_gw_node *gw_node; + lockdep_assert_held(&bat_priv->gw.list_lock); + if (gateway->bandwidth_down == 0) return; @@ -421,9 +427,7 @@ static void batadv_gw_node_add(struct ba gw_node->orig_node = orig_node; atomic_set(&gw_node->refcount, 1); - spin_lock_bh(&bat_priv->gw.list_lock); hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); - spin_unlock_bh(&bat_priv->gw.list_lock); batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", @@ -479,11 +483,14 @@ void batadv_gw_node_update(struct batadv { struct batadv_gw_node *gw_node, *curr_gw = NULL; + spin_lock_bh(&bat_priv->gw.list_lock); gw_node = batadv_gw_node_get(bat_priv, orig_node); if (!gw_node) { batadv_gw_node_add(bat_priv, orig_node, gateway); + spin_unlock_bh(&bat_priv->gw.list_lock); goto out; } + spin_unlock_bh(&bat_priv->gw.list_lock); if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) && (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))