Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp658642yba; Wed, 24 Apr 2019 07:35:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqwdCZu8oNNOC3R6OTB7dO+R2f0Qwn8+rbH3KaZ2UX4I2sF8XGfR55vQS1tqV19iipYE0ynj X-Received: by 2002:a65:5286:: with SMTP id y6mr30364310pgp.79.1556116527770; Wed, 24 Apr 2019 07:35:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556116527; cv=none; d=google.com; s=arc-20160816; b=sZMn0fNHCIQgD1g5ud6hANsZLjieDlOJOHEUt7ZKzjFIP874BdZIqBGk2J0OHdXqKi jIDjvJqiwaVdzzQzaHX0jHYm6kGuMW9o1nipgeu40TUp4ipzTdZrp4d8CZt9A8PDkUKe z6hHAxD6jx6k2bk7LIZLMrqMoOlOs1hAes9Fj2YCPwe1hJu94FYzvnbmHOuEZtKjIBmk 6KS0pdI7/AGRtzLrivgBodReKNFiffAJ1loep9oZ9XwGL/NKX82GFOw8/b98V8h94lni UuKbBye7i+jBeNslnlf4hYOUtSmiIqRwQadwtwLXU+KPEVTHdlajAN3/J0edrGOeUyPX QXAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8KRzAP1IGHCzC51FwXD36J8M8L7+ZhLMs1kf/xWHcKI=; b=spOnlkpBniWzRIIvrky32Js2EmyjZPu8/M3wfgyXSCWhZxB0A3IToOwT2psLVFDzsn n29tzqjQzxToo4BGtnnekSzvAWaByqxcQPwez/NRe75AJNzVAocHI7lkTdkuLN40vgq4 jmeeI70VK9t1+MYAXDjZotNZS+BrrcDuDawPCKwiRb2BQyLKqGVYwNam0WJUL5V2PU5y Hf+P7rSBuRmeIUq5zqwnIfJQ+DzUZmIMNaBO4fBIbNi5GJY7rcx+2LeqMc4kUG4RCy2x cj6+xWP0afQiws2GtxsTpN9t7nMPYVrUL7xvoYGc0XC5E1W3dZjUIFO0X8OXVGUVRJTs p+Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ruSSNcsq; 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 p14si190854pll.184.2019.04.24.07.35.11; Wed, 24 Apr 2019 07:35:27 -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=ruSSNcsq; 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 S1730844AbfDXOeE (ORCPT + 99 others); Wed, 24 Apr 2019 10:34:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:37936 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730808AbfDXOeB (ORCPT ); Wed, 24 Apr 2019 10:34:01 -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 319D4218FE; Wed, 24 Apr 2019 14:33:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556116440; bh=2RhSs4CrWgP480evom6V30diwMWsfMYFPrBxNCehjFA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ruSSNcsqisqDDtzp9M4tR2V8+rtEjpIbXERFJ6R4E5fAxtsZBQpEdq6YxKZhNj5HV TP0hbNwEq5wTsqAOvNYqP0VEsaT/8bCyn/CrVCHfoIMSrNfdctJJTMlji5R6fGS9HF bJ0V/GQ65N3R3QQPi6Yik5cCZ2W0Ss0uQHAGGvHE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sven Eckelmann , Simon Wunderlich , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.0 08/66] batman-adv: Reduce claim hash refcnt only for removed entry Date: Wed, 24 Apr 2019 10:32:42 -0400 Message-Id: <20190424143341.27665-8-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190424143341.27665-1-sashal@kernel.org> References: <20190424143341.27665-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sven Eckelmann [ Upstream commit 4ba104f468bbfc27362c393815d03aa18fb7a20f ] The batadv_hash_remove is a function which searches the hashtable for an entry using a needle, a hashtable bucket selection function and a compare function. It will lock the bucket list and delete an entry when the compare function matches it with the needle. It returns the pointer to the hlist_node which matches or NULL when no entry matches the needle. The batadv_bla_del_claim is not itself protected in anyway to avoid that any other function is modifying the hashtable between the search for the entry and the call to batadv_hash_remove. It can therefore happen that the entry either doesn't exist anymore or an entry was deleted which is not the same object as the needle. In such an situation, the reference counter (for the reference stored in the hashtable) must not be reduced for the needle. Instead the reference counter of the actually removed entry has to be reduced. Otherwise the reference counter will underflow and the object might be freed before all its references were dropped. The kref helpers reported this problem as: refcount_t: underflow; use-after-free. Fixes: 23721387c409 ("batman-adv: add basic bridge loop avoidance code") Signed-off-by: Sven Eckelmann Signed-off-by: Simon Wunderlich Signed-off-by: Sasha Levin (Microsoft) --- net/batman-adv/bridge_loop_avoidance.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/net/batman-adv/bridge_loop_avoidance.c b/net/batman-adv/bridge_loop_avoidance.c index 5fdde2947802..cf2bcea7df82 100644 --- a/net/batman-adv/bridge_loop_avoidance.c +++ b/net/batman-adv/bridge_loop_avoidance.c @@ -803,6 +803,8 @@ static void batadv_bla_del_claim(struct batadv_priv *bat_priv, const u8 *mac, const unsigned short vid) { struct batadv_bla_claim search_claim, *claim; + struct batadv_bla_claim *claim_removed_entry; + struct hlist_node *claim_removed_node; ether_addr_copy(search_claim.addr, mac); search_claim.vid = vid; @@ -813,10 +815,18 @@ static void batadv_bla_del_claim(struct batadv_priv *bat_priv, batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__, mac, batadv_print_vid(vid)); - batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim, - batadv_choose_claim, claim); - batadv_claim_put(claim); /* reference from the hash is gone */ + claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash, + batadv_compare_claim, + batadv_choose_claim, claim); + if (!claim_removed_node) + goto free_claim; + /* reference from the hash is gone */ + claim_removed_entry = hlist_entry(claim_removed_node, + struct batadv_bla_claim, hash_entry); + batadv_claim_put(claim_removed_entry); + +free_claim: /* don't need the reference from hash_find() anymore */ batadv_claim_put(claim); } -- 2.19.1