Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3221148ybb; Sun, 22 Mar 2020 18:45:44 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtacxUHFtiCWWOu66HYxMq8OWnHoFZbdkO/JU0wNTUX803xuKXVf4Z1kSCeB4oc/bEcQO4q X-Received: by 2002:aca:c78e:: with SMTP id x136mr15143088oif.116.1584927944729; Sun, 22 Mar 2020 18:45:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584927944; cv=none; d=google.com; s=arc-20160816; b=i8WHn5uU2gGs7qgaofsrFcpbZXx54jneEL/O4prWEx5EijAobyszIQAkaM9bkpc6Dw PydJmJeJHRN1BBPytuTRYevqCCi67hGQ4wKc8xjOXf7E6b9JqV07NwaMvE8cvY62GGvZ LXuQRG5ArmLKqy+WErL5lAg6XnxKzQ18MD0bXt4xpXk/fpRTQcujWwCr9T77cbKlTQ6s 8ix5p4nA6oz9Wu9B3GhfGKwncuHxbFWyhv/7hwa9CMX929rMAQEf2pu/0R7nqFCB2GOc bl3YQBGU9HXQsWnk7bwjQ3uT7MONY0glnbaKXplomHJ89SemKAwXlJVKIOPEQI8hkn2x rWGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Ahk3qH2zMzuDZP7bGUR4vXUMXB2EEJz/0O9zcckCiiE=; b=qLSyGEqp/RxwK3qjfXGVFIdC8WnyEP4CimWdHBFlRgz4zneaaEEylIuKphni+2x9ci gqE+RqCG9I3qK3cjHjlFT+ieWw6cr2tM2C8eZ0r0AAaUwYlFuQLuMCOSYd+oQoWhE4cz 76iUHVl1W2F/1h1ugKQiJxZVvi72LYuxXAPI11Ctp9137KsNihULAary3+BQJOC/eP1Q nyzc+JZu0D/xDVW4974PG2jBxBn+Y7tP9OY9m0FpFbIlfsDm95jzS6DZ2XfCTjfqdHFw sXr5hrqvMbWN0L7bH59i7fc2P00CO1CgBMPJ2Jlk7jOr0urMaBcSp/CD00hdDIyZTOrW 0QIg== 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 109si1635209otv.36.2020.03.22.18.45.31; Sun, 22 Mar 2020 18:45:44 -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; 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 S1726991AbgCWBpJ (ORCPT + 99 others); Sun, 22 Mar 2020 21:45:09 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:12174 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726954AbgCWBpJ (ORCPT ); Sun, 22 Mar 2020 21:45:09 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id E2157334D816B813E5A9; Mon, 23 Mar 2020 09:45:03 +0800 (CST) Received: from localhost (10.173.223.234) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.487.0; Mon, 23 Mar 2020 09:44:57 +0800 From: YueHaibing To: , , , , CC: , , YueHaibing Subject: [PATCH v2] xfrm: policy: Fix doulbe free in xfrm_policy_timer Date: Mon, 23 Mar 2020 09:41:55 +0800 Message-ID: <20200323014155.56376-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 In-Reply-To: <20200318034839.57996-1-yuehaibing@huawei.com> References: <20200318034839.57996-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.173.223.234] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After xfrm_add_policy add a policy, its ref is 2, then xfrm_policy_timer read_lock xp->walk.dead is 0 .... mod_timer() xfrm_policy_kill policy->walk.dead = 1 .... del_timer(&policy->timer) xfrm_pol_put //ref is 1 xfrm_pol_put //ref is 0 xfrm_policy_destroy call_rcu xfrm_pol_hold //ref is 1 read_unlock xfrm_pol_put //ref is 0 xfrm_policy_destroy call_rcu xfrm_policy_destroy is called twice, which may leads to double free. Call Trace: RIP: 0010:refcount_warn_saturate+0x161/0x210 ... xfrm_policy_timer+0x522/0x600 call_timer_fn+0x1b3/0x5e0 ? __xfrm_decode_session+0x2990/0x2990 ? msleep+0xb0/0xb0 ? _raw_spin_unlock_irq+0x24/0x40 ? __xfrm_decode_session+0x2990/0x2990 ? __xfrm_decode_session+0x2990/0x2990 run_timer_softirq+0x5c5/0x10e0 Fix this by use write_lock_bh in xfrm_policy_kill. Fixes: ea2dea9dacc2 ("xfrm: remove policy lock when accessing policy->walk.dead") Signed-off-by: YueHaibing --- v2: Fix typo 'write_lock_bh'--> 'write_unlock_bh' while unlocking --- net/xfrm/xfrm_policy.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index dbda08ec566e..ae0689174bbf 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -434,6 +434,7 @@ EXPORT_SYMBOL(xfrm_policy_destroy); static void xfrm_policy_kill(struct xfrm_policy *policy) { + write_lock_bh(&policy->lock); policy->walk.dead = 1; atomic_inc(&policy->genid); @@ -445,6 +446,7 @@ static void xfrm_policy_kill(struct xfrm_policy *policy) if (del_timer(&policy->timer)) xfrm_pol_put(policy); + write_unlock_bh(&policy->lock); xfrm_pol_put(policy); } -- 2.17.1