Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp485424imn; Wed, 27 Jul 2022 11:31:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tAwbOjTexUZDfagGWeg7XPwlC3Dvu5wC2VwyW/4c5JpDX9fjPsyzFi0XBJqHpqhJnCFyJS X-Received: by 2002:a17:906:cc0e:b0:72b:311b:8ed9 with SMTP id ml14-20020a170906cc0e00b0072b311b8ed9mr18789056ejb.186.1658946671847; Wed, 27 Jul 2022 11:31:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658946671; cv=none; d=google.com; s=arc-20160816; b=CPgYZME8rsjQrTiKVlrZwRkMRyULojDKnIrIG5Fuh+CKgDo8Ba3PPdmFz2pHraOVKR iL3AGHv82ilPP8Dx4ethXz+9aE56tpfKLSmFO4MrzOcnpMZMmn3ZPWgbD/cLxTPUH3xV h3NM/pwEGLqI7VbvnbzSguI9+s7YcUT3oGj9I3sdEM+2I+8TyUWEHaLdBi9IY6fqGBR4 esEV8yf2rIamJ1/Mi6RQTorIJd748GdtbFIZXmrDMLbd6AiWlsR0auNAtBUFHBtent18 CwmVnPkcyBGsKU4WxMCO/MFA87Gzeug9GBg6tuB9+Uxs9PLL1M4m73zWGufVbSUXKIu9 BhZg== 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=hy09fPwBAHzUdb/nAtsUOrdUd4rqDppTRTPIBtrZcCU=; b=Dd67fg4lLZ9/5WuuSrivA0KvAq7tDybHiDderm/kwh8jaF1MEjJyI/+/yO9QK6zyXH +5GrPXF9MWGl5Cx30c+KZw58Ysfz1YXVkrZgfB2cF9ygbsXfrH6Gy7ZfLWfinAhKFcI+ ONhylMSmwsnpqVP50sAYpm5llfv7wkrSd/NB07t+DoQ785HfeI+NKLGUhPvpuNF0SB1b 131z4SlMy7HsX2o14YTbeJkjneW9ALBT9khSIVFFx57D20Q7pWxkQuAqTWHQ++N2d6oR Hti46B8SfKqmGqx1mrGkyFg1lQX/vZYJAPZn+b15L4TQlRb+OiUj9MR8e8UmXO0i4oGd B7NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Hs+Kdlck; 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 qb27-20020a1709077e9b00b0072f9b440ffbsi20407358ejc.225.2022.07.27.11.30.46; Wed, 27 Jul 2022 11:31:11 -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=Hs+Kdlck; 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 S238913AbiG0Qmc (ORCPT + 99 others); Wed, 27 Jul 2022 12:42:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239590AbiG0Qlw (ORCPT ); Wed, 27 Jul 2022 12:41:52 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4ABDE5B7BE; Wed, 27 Jul 2022 09:29:52 -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 sin.source.kernel.org (Postfix) with ESMTPS id A83D4CE22FE; Wed, 27 Jul 2022 16:29:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1316C433C1; Wed, 27 Jul 2022 16:29:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658939389; bh=UjxZAk7k9pMqhRRjQXpw7vtqX5SDNcwJryCCR9a2Fd8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hs+Kdlcknb3Jwq0gi4goHR/8COtllEmbNvfMn78Uw7248d4VoD0DqELvW470QA0SG fVZIxaO5+0k6ej87FgijGUyMLGRuOJi3JlvLpkmTMvZ+zsNJMoNAk3gV8Ok4fefKCf wVpkbpTW+V9X3mL5m4RMYAIVZSklZ6TjbIPGCM3A= 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 5.4 11/87] xfrm: xfrm_policy: fix a possible double xfrm_pols_put() in xfrm_bundle_lookup() Date: Wed, 27 Jul 2022 18:10:04 +0200 Message-Id: <20220727161009.470491139@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727161008.993711844@linuxfoundation.org> References: <20220727161008.993711844@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 3ecb77c58c44..28a8cdef8e51 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -2679,8 +2679,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; @@ -2695,6 +2697,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