Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp672365ybz; Wed, 22 Apr 2020 05:59:07 -0700 (PDT) X-Google-Smtp-Source: APiQypI414VwQGj8+J2cYvNlhy6pQDXwcOpM0DiF5fsXRVvQ0t5A+KjZVZj9y+x7btF0gDX1xyMl X-Received: by 2002:a50:c389:: with SMTP id h9mr23402241edf.1.1587560347106; Wed, 22 Apr 2020 05:59:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587560347; cv=none; d=google.com; s=arc-20160816; b=uWHBoGzkgxCpxas0pQWceYGqjZbOWdBbSGrpZcHmhdHcmJ2xsttwNjtOaC1G4NvhAq BVHbDdFH9oORmkDyImKhLrc/O4scFM7G3VYn4StzEahSexWxc1SWbY/aCxlhxhSsxj6Z Fqib6PgAsv1wKBCzTi2W6TwESNvN8xzpqYdfyTrDYVdQY/5oktphgfiGMXSC3iD9Fwnm RhtrzQOavCC6EE0r+jP2ekppSLsz9hfbIpvolFf68pT8wfuW2yQAggKPUNhbo5nopVAT wRr4XzGFOV49uty8rqO2UKNMTsO8Dy+7OViuCQMGq8rxUOXN+mUiGYXk2kdI36iPhYzV HnDw== 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=GWYRccDiQYM1Z4UyKxLB106wueTlPBVNv//lLN63CCE=; b=vcPqHIx6OGVj2wz90sEM+9Ao7P/4mww43N2N3i6KDAPnYEzsIe1O8kzUbNwihudR3+ RyeB8yvs7EP+gd8kn1rLkJ/HQWLXAQ5jDd9jzRxmnYJl5tUrO6UK5y71NwbYLgHC0UN6 oejGG7dN7cKSn331wA3JktIsLIl3LNxh4zQ84iwNiMYuYlAQ+P7o+db1ml7xx64flR2A hjKcPe+FaN34A+/MfjLQ9l9Q85SYxScZB66kraycXK9JwysCaKCjIqLgwUI6kVAdL41I wI6Nte57T4FcyK+XFxKV4DwjHFG4wt6grBGXtQJfZGTyGIJyBr456mUlACFf03qZcQFj YO/w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k2si3862884edl.244.2020.04.22.05.58.44; Wed, 22 Apr 2020 05:59:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726629AbgDVMyu (ORCPT + 99 others); Wed, 22 Apr 2020 08:54:50 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:34236 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725895AbgDVMyu (ORCPT ); Wed, 22 Apr 2020 08:54:50 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id BF8904839555C70BC3B1; Wed, 22 Apr 2020 20:54:46 +0800 (CST) Received: from localhost (10.166.215.154) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Wed, 22 Apr 2020 20:54:37 +0800 From: YueHaibing To: , , , CC: , , , YueHaibing Subject: [PATCH v2] xfrm: policy: Fix xfrm policy match Date: Wed, 22 Apr 2020 20:53:46 +0800 Message-ID: <20200422125346.27756-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 In-Reply-To: <20200421143149.45108-1-yuehaibing@huawei.com> References: <20200421143149.45108-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.166.215.154] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While update xfrm policy as follow: ip -6 xfrm policy update src fd00::1/128 dst fd00::2/128 dir in \ priority 1 mark 0 mask 0x10 ip -6 xfrm policy update src fd00::1/128 dst fd00::2/128 dir in \ priority 2 mark 0 mask 0x00 ip -6 xfrm policy update src fd00::1/128 dst fd00::2/128 dir in \ priority 2 mark 0 mask 0x10 We get this warning: WARNING: CPU: 0 PID: 4808 at net/xfrm/xfrm_policy.c:1548 Kernel panic - not syncing: panic_on_warn set ... CPU: 0 PID: 4808 Comm: ip Not tainted 5.7.0-rc1+ #151 Call Trace: RIP: 0010:xfrm_policy_insert_list+0x153/0x1e0 xfrm_policy_inexact_insert+0x70/0x330 xfrm_policy_insert+0x1df/0x250 xfrm_add_policy+0xcc/0x190 [xfrm_user] xfrm_user_rcv_msg+0x1d1/0x1f0 [xfrm_user] netlink_rcv_skb+0x4c/0x120 xfrm_netlink_rcv+0x32/0x40 [xfrm_user] netlink_unicast+0x1b3/0x270 netlink_sendmsg+0x350/0x470 sock_sendmsg+0x4f/0x60 Policy C and policy A has the same mark.v and mark.m, so policy A is matched in first round lookup while updating C. However policy C and policy B has same mark and priority, which also leads to matched. So the WARN_ON is triggered. xfrm policy lookup should only be matched if the found policy has the same lookup keys (mark.v & mark.m) and priority. Fixes: 7cb8a93968e3 ("xfrm: Allow inserting policies with matching mark and different priorities") Signed-off-by: YueHaibing --- v2: policy matched while have same mark and priority --- net/xfrm/xfrm_policy.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index 297b2fdb3c29..2a0d7f5e6545 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -1436,12 +1436,7 @@ static void xfrm_policy_requeue(struct xfrm_policy *old, static bool xfrm_policy_mark_match(struct xfrm_policy *policy, struct xfrm_policy *pol) { - u32 mark = policy->mark.v & policy->mark.m; - - if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m) - return true; - - if ((mark & pol->mark.m) == pol->mark.v && + if ((policy->mark.v & policy->mark.m) == (pol->mark.v & pol->mark.m) && policy->priority == pol->priority) return true; @@ -1628,7 +1623,7 @@ __xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id, hlist_for_each_entry(pol, chain, bydst) { if (pol->type == type && pol->if_id == if_id && - (mark & pol->mark.m) == pol->mark.v && + mark == (pol->mark.m & pol->mark.v) && !selector_cmp(sel, &pol->selector) && xfrm_sec_ctx_match(ctx, pol->security)) return pol; @@ -1726,7 +1721,7 @@ struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, hlist_for_each_entry(pol, chain, byidx) { if (pol->type == type && pol->index == id && pol->if_id == if_id && - (mark & pol->mark.m) == pol->mark.v) { + mark == (pol->mark.m & pol->mark.v)) { xfrm_pol_hold(pol); if (delete) { *err = security_xfrm_policy_delete( @@ -1898,7 +1893,7 @@ static int xfrm_policy_match(const struct xfrm_policy *pol, if (pol->family != family || pol->if_id != if_id || - (fl->flowi_mark & pol->mark.m) != pol->mark.v || + fl->flowi_mark != (pol->mark.m & pol->mark.v) || pol->type != type) return ret; @@ -2177,7 +2172,7 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, match = xfrm_selector_match(&pol->selector, fl, family); if (match) { - if ((sk->sk_mark & pol->mark.m) != pol->mark.v || + if (sk->sk_mark != (pol->mark.m & pol->mark.v) || pol->if_id != if_id) { pol = NULL; goto out; -- 2.17.1