Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2971505imu; Sun, 9 Dec 2018 14:00:23 -0800 (PST) X-Google-Smtp-Source: AFSGD/U0/nMtMwH3GwCiitgYDbgvbR6WNgzjJkSozc9jLB832v7QZoe3QWz6DEVa/VpBcci+dIll X-Received: by 2002:a17:902:584:: with SMTP id f4mr10190267plf.28.1544392823001; Sun, 09 Dec 2018 14:00:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544392822; cv=none; d=google.com; s=arc-20160816; b=dDJPrsjwJNcALn8ALvMxgphAUihM2sMHPTQu3RTJX8AZUP19R7xfyvxmMeLkSw/+hg S9+FJk/yHsOYRd4m2ENlEgCFBFDg1/d6wqzJ9hDW+9qEHv25GXkzj2rQ9vEsPWP8iIKX +t6rOVqX5dAqknM1BypS9B9xjdwWgjuWzqAnAxz/eYcSRdcWtEt0wU8v2VnUUaSAyy74 uXc0MFRtccKylTPgeslGSI+4ziLauu+MOu12kLWBxpH9SH7Yf5BPKRGoWUs4MeZAvy8K 1Fe7tNT5Q8F9G7YstDnQPkVxnHeYkugoAabr0i31RvnCwvxbCWIlzqbF9CDoEZilm7oo E52g== 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=u/4WeJi+wENBV/yoBuHogHjf0awKWO7n74u+ZBTBR1I=; b=cASl1WZ3DElud8BwLvgXWMk/K5djYk43FW8RKN/xM9hSzDsGAzehr8XF1uq4Y9BHcA 4IQAr+WPjxxHJ9LVY7qAzk1qMwxBgrTDDFXbgLy2Q84E8G15EfyqDPe3aZKMtsPwNTCu 38LBZJ4c7RB+19zcztFUFwSgGXCHrYm4ndtjAIKRjvEo8wpwNsvPl9pQibBaqAFrpfEE sRnTwXu9F5MLYP9cX9ndHCJbQjFj8DubNLnZbB7bnroelu0dzHdSveX9/H1us0zYB6ky WSl6lOYOpO2NNUPxxEXEeqvZwB0pUt+mybukxz988Yy462fSX8K/0nJ4+BA8fi9m1Oo3 Om3A== 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 e4si8078859pgk.127.2018.12.09.14.00.06; Sun, 09 Dec 2018 14:00:22 -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 S1726828AbeLIV6j (ORCPT + 99 others); Sun, 9 Dec 2018 16:58:39 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:36242 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726755AbeLIVzj (ORCPT ); Sun, 9 Dec 2018 16:55:39 -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 1gW730-0002il-UB; Sun, 09 Dec 2018 21:55:35 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72g-0003WV-Qo; 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" , "Marek Lindner" , "Simon Wunderlich" 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 215/328] batman-adv: Prevent duplicated nc_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 fa122fec8640eb7186ce5a41b83a4c1744ceef8f upstream. 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 Acked-by: Marek Lindner Signed-off-by: Simon Wunderlich [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- net/batman-adv/network-coding.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) --- a/net/batman-adv/network-coding.c +++ b/net/batman-adv/network-coding.c @@ -802,16 +802,27 @@ static struct batadv_nc_node spinlock_t *lock; /* Used to lock list selected by "int in_coding" */ struct list_head *list; + /* Select ingoing or outgoing coding node */ + if (in_coding) { + lock = &orig_neigh_node->in_coding_list_lock; + list = &orig_neigh_node->in_coding_list; + } else { + lock = &orig_neigh_node->out_coding_list_lock; + list = &orig_neigh_node->out_coding_list; + } + + spin_lock_bh(lock); + /* Check if nc_node is already added */ nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding); /* Node found */ if (nc_node) - return nc_node; + goto unlock; nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC); if (!nc_node) - return NULL; + goto unlock; WARN_ON_ONCE(atomic_inc_return(&orig_neigh_node->refcount) < 2); @@ -821,21 +832,13 @@ static struct batadv_nc_node nc_node->orig_node = orig_neigh_node; atomic_set(&nc_node->refcount, 2); - /* Select ingoing or outgoing coding node */ - if (in_coding) { - lock = &orig_neigh_node->in_coding_list_lock; - list = &orig_neigh_node->in_coding_list; - } else { - lock = &orig_neigh_node->out_coding_list_lock; - list = &orig_neigh_node->out_coding_list; - } - batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n", nc_node->addr, nc_node->orig_node->orig); /* Add nc_node to orig_node */ - spin_lock_bh(lock); list_add_tail_rcu(&nc_node->list, list); + +unlock: spin_unlock_bh(lock); return nc_node;