Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3732495imm; Mon, 8 Oct 2018 08:39:55 -0700 (PDT) X-Google-Smtp-Source: ACcGV61haUdBlgMDo9K8uyE50rrBCp/MhocsvLkg8fO6kFDVaRbKAeLCetDRjke5bbZsOxfj031+ X-Received: by 2002:a62:3402:: with SMTP id b2-v6mr26404494pfa.65.1539013195184; Mon, 08 Oct 2018 08:39:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539013195; cv=none; d=google.com; s=arc-20160816; b=ubKyQq2FI7SbatYIqv7nnQP3BZPQVjG1FrTFaRDGSYfGOv4nTbyfRvrmLJSQEAOtCl wJLIN+sBjDaDPNlGid31U4Y0rxHfjo2o48IjLQUQ23BK9EYlmSRN6WcJq2iWUbm1T5JE 8JJIb3byclAApq5LPmIX6U4gjZ0FfYQaLl8SM+Q0/0MNjZbF7K4D2B5YCN+/P4B2ss+m oB6VAq6rY9tETdU0H0W0exYy2jDFR3+0IIDyAApZRo6sdinzdHkxsVOBBB+TN6xuGsbH FGYJsyZWZ1nReJywVO/8JnzTWTFhDgIr5vjoCQayISbA3k6+LYYB/bvPMufEq6vaQAX4 yZ0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=fw9H4sfZL2wjIq0CIyX6byjKvdsVrClcmU+yAnN16oA=; b=LjrqFTfkhRDJMaFdlrqhCY/z0lYP41vjqvCHQVAgCqpiILx1QY/9twTOT1WHT9Dvrf L5aMO7HNxKvsVf65a4B6l3ScdbpNeStbB70VwlxNpdR6JC1hFL/3KKb0JCn57nM8oOCV fEt5hCat+BemHcuxGL3/AQheCfQ49rrdoqB3mKFx5Z6vfhZh4rz5+4PE4aoGGPTsrC8F qlENUQzT20S++rN7Kl/AUxfEMRy5W1nxNd/7Gf9dfevnPZpy/QyhnqZueWfQeHm8uhJS 3xGe/lIGf1qgvrTj/VGvJjd9J8hHIP0acGdPwwjncrW41U6GB7FnfTKrXiVO81qN9k2H CV2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CQykFgr1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k8-v6si17721677plt.176.2018.10.08.08.39.40; Mon, 08 Oct 2018 08:39:55 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=CQykFgr1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726910AbeJHWhx (ORCPT + 99 others); Mon, 8 Oct 2018 18:37:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:55944 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726754AbeJHWhw (ORCPT ); Mon, 8 Oct 2018 18:37:52 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 50DDD213A2; Mon, 8 Oct 2018 15:25:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539012337; bh=DJraqWQv7w2D12YbzFAN7EO8Eg8DLbEeRTn52nNQHyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CQykFgr1sm1CBvu0hEi3/8QRI2hPyFxhPmBrcdcOLolsLKADYLzMCXf7TOFmaa4OJ zMdVQ6q8gj9GkVTAIGWPPKsCdx3/88IqoGUIMvebtoxoHy3azxDv9sKsXbwwg4yvbV 4d5TVN4bV6wM5VVxlrSPVClsjJS1qPCjz8o/MGOQ= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sven Eckelmann , Simon Wunderlich , Sasha Levin Subject: [PATCH AUTOSEL 4.18 11/58] batman-adv: Prevent duplicated softif_vlan entry Date: Mon, 8 Oct 2018 11:24:36 -0400 Message-Id: <20181008152523.70705-11-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181008152523.70705-1-sashal@kernel.org> References: <20181008152523.70705-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sven Eckelmann [ Upstream commit 94cb82f594ed86be303398d6dfc7640a6f1d45d4 ] The function batadv_softif_vlan_get is responsible for adding new softif_vlan to the softif_vlan_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: 5d2c05b21337 ("batman-adv: add per VLAN interface attribute framework") Signed-off-by: Sven Eckelmann Signed-off-by: Simon Wunderlich Signed-off-by: Sasha Levin --- net/batman-adv/soft-interface.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/net/batman-adv/soft-interface.c b/net/batman-adv/soft-interface.c index 1485263a348b..626ddca332db 100644 --- a/net/batman-adv/soft-interface.c +++ b/net/batman-adv/soft-interface.c @@ -574,15 +574,20 @@ int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid) struct batadv_softif_vlan *vlan; int err; + spin_lock_bh(&bat_priv->softif_vlan_list_lock); + vlan = batadv_softif_vlan_get(bat_priv, vid); if (vlan) { batadv_softif_vlan_put(vlan); + spin_unlock_bh(&bat_priv->softif_vlan_list_lock); return -EEXIST; } vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC); - if (!vlan) + if (!vlan) { + spin_unlock_bh(&bat_priv->softif_vlan_list_lock); return -ENOMEM; + } vlan->bat_priv = bat_priv; vlan->vid = vid; @@ -590,17 +595,23 @@ int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid) atomic_set(&vlan->ap_isolation, 0); + kref_get(&vlan->refcount); + hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list); + spin_unlock_bh(&bat_priv->softif_vlan_list_lock); + + /* batadv_sysfs_add_vlan cannot be in the spinlock section due to the + * sleeping behavior of the sysfs functions and the fs_reclaim lock + */ err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan); if (err) { - kfree(vlan); + /* ref for the function */ + batadv_softif_vlan_put(vlan); + + /* ref for the list */ + batadv_softif_vlan_put(vlan); return err; } - spin_lock_bh(&bat_priv->softif_vlan_list_lock); - kref_get(&vlan->refcount); - hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list); - spin_unlock_bh(&bat_priv->softif_vlan_list_lock); - /* add a new TT local entry. This one will be marked with the NOPURGE * flag */ -- 2.17.1