Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp408297imn; Wed, 27 Jul 2022 09:26:16 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sRqbwB4l0fCR/R3Zzqn0vyZCo+szFKXvSQQPgA1vOyw8a/RBkbKzxPzd8kjT3+2ktXCuzt X-Received: by 2002:a05:6a00:b8c:b0:52c:4a92:d133 with SMTP id g12-20020a056a000b8c00b0052c4a92d133mr2896967pfj.74.1658939176297; Wed, 27 Jul 2022 09:26:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658939176; cv=none; d=google.com; s=arc-20160816; b=X8jClfPbHAkL5gvmnXToXJXptGsT2jzz5c1WUB35pz20j5WOVpAli06/bDsy7YtWH1 wgp4AMUpCsXWWx4mJEvJAFQ5UhsVcgiGJW9Ci3II78qfY4NTXRH+9CgsH74reG3xk+7p n2QIYO1ozcWm86i1+j3oo8ZuzRhNmRx25luo/vJFVTrNDIVA5RgN8ZEHIuE5g6fKActn BkAIa4NjAyri6AeayvnapAAtcxIqy8dXKEULa6ZE16Vt/QyV5ma+K3MLrNNA5UQvEqF+ q2dmjOio/jAeX501spDfkdWJBo+x1It7oy/m3dnjtTHLxs7KKwzKCCsmVGJwOXppz9RQ rpyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t4eVRjffqOXBFJPeuz5CtcCYdyANM35WGnXcmILUOL8=; b=HEH4pQcw4N2e8gPBHoYjZg91n2b3aRkqdK0YH5YiRDec6GwFh3zPthUhq2wKxzbYQU hGIlQeVEg4BwqJaUX8Z2LU6TPVUZkNAgi+qVR9w6qQbG0nIbdU/WbiMWdKeY3l/YQFdB 2iwO/Tcngu3D401zfXW84BIkPlUTY1ahz1gvr/yNVLMv4AgehYEKDlZVkOQjEY7mPSsA Y2p4uX7vxysjOYUopkABAlKRx8UlKdUdE5rzsS2IFUTTa+OahODd8FnJ2Xo0ppQsMVIc SWCOIEf/KhMjp12URQqx6qW/AnL6P6kC0Q6Fd8m6caoSw9kmQzKhdXTwZm7Pbp30sJfM xNog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZKa4Nvu8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cp18-20020a056a00349200b0050dd86727b8si19360706pfb.349.2022.07.27.09.26.00; Wed, 27 Jul 2022 09:26:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZKa4Nvu8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234667AbiG0QXH (ORCPT + 99 others); Wed, 27 Jul 2022 12:23:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235148AbiG0QW1 (ORCPT ); Wed, 27 Jul 2022 12:22:27 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C97654C610; Wed, 27 Jul 2022 09:22:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 67430619C0; Wed, 27 Jul 2022 16:22:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EB61C433D6; Wed, 27 Jul 2022 16:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658938945; bh=LU/6JYDmJVCAIJc5hn/I2FwrmPcI5S2waUtTQhLrexc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZKa4Nvu8Or5wp11BxNsgYkSdbIOPuPaHrKPbr5kM2wTubAKTZpgle/cHW+J9Nz9lX C5PA/nVh5XUiJMFFgNgbogM9n39F/GScBDBeUrcLXAbKi9hCr1sa1ZSBpCNL8E4MxD Z9X3V//ozgRbcHwoI/umUv1KumXQ2Z82fipjdK20= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hangyu Hua , Steffen Klassert , Sasha Levin Subject: [PATCH 4.9 06/26] xfrm: xfrm_policy: fix a possible double xfrm_pols_put() in xfrm_bundle_lookup() Date: Wed, 27 Jul 2022 18:10:35 +0200 Message-Id: <20220727160959.403628194@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727160959.122591422@linuxfoundation.org> References: <20220727160959.122591422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hangyu Hua [ Upstream commit f85daf0e725358be78dfd208dea5fd665d8cb901 ] xfrm_policy_lookup() will call xfrm_pol_hold_rcu() to get a refcount of pols[0]. This refcount can be dropped in xfrm_expand_policies() when xfrm_expand_policies() return error. pols[0]'s refcount is balanced in here. But xfrm_bundle_lookup() will also call xfrm_pols_put() with num_pols == 1 to drop this refcount when xfrm_expand_policies() return error. This patch also fix an illegal address access. pols[0] will save a error point when xfrm_policy_lookup fails. This lead to xfrm_pols_put to resolve an illegal address in xfrm_bundle_lookup's error path. Fix these by setting num_pols = 0 in xfrm_expand_policies()'s error path. Fixes: 80c802f3073e ("xfrm: cache bundles instead of policies for outgoing flows") Signed-off-by: Hangyu Hua Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- net/xfrm/xfrm_policy.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index 9179b47e8b61..0894108f561c 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -1819,8 +1819,10 @@ static int xfrm_expand_policies(const struct flowi *fl, u16 family, *num_xfrms = 0; return 0; } - if (IS_ERR(pols[0])) + if (IS_ERR(pols[0])) { + *num_pols = 0; return PTR_ERR(pols[0]); + } *num_xfrms = pols[0]->xfrm_nr; @@ -1834,6 +1836,7 @@ static int xfrm_expand_policies(const struct flowi *fl, u16 family, if (pols[1]) { if (IS_ERR(pols[1])) { xfrm_pols_put(pols, *num_pols); + *num_pols = 0; return PTR_ERR(pols[1]); } (*num_pols)++; -- 2.35.1