Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp2600865rdg; Mon, 14 Aug 2023 07:40:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBKphs7PGYobtYfrO4DHhAK9aGdhtMOdRcor8nvcNC9l5DJQnVfyMamenA4JSV3cnDTA3+ X-Received: by 2002:a05:6870:c1d3:b0:1bf:d5fd:618 with SMTP id i19-20020a056870c1d300b001bfd5fd0618mr7990769oad.34.1692024016620; Mon, 14 Aug 2023 07:40:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692024016; cv=none; d=google.com; s=arc-20160816; b=uwTRzC7HSuhVeIIPWC/SKmDfmxvvXD+AhaBuzgonTZ0IytFJ71wXdiArhCG6RiylFL NkDvUxrRY640UCqsJoKYq2PV8OExv1lvsiAjGz8Qcf74xFz65iCptn9gj4Gi2Zu3H4rP ir9L1/8POX+1a/wkMpncsNAQbr9JVfERmphjpJLnCj3b44zFfFXkJSF6aqkidfrsGwPP S1C7HzVno2kewVZVbvaEgiDfJNEViMmX2M24q7zgWobTuWQiz9qDP6olRc+fyLkG3KLK vi2/iItwNhlfx5SA8eeZuERIbAL8xxYNeERw5WQewqvWYsopnvygIHa1hY7QWTgotR5g MZ/A== 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 :message-id:date:subject:cc:to:from; bh=O5YJ0jHNIHsjeHXbDWMko3VRv19XLF3bkuK5CHsHeVo=; fh=B+2PY0lNTtJDKcbqGLZ1CQ2Mt3Id8r0xXrrTGjS3jc4=; b=tGwy6epkrD8SXRpKpH15D967bDpkbUh+vFZ/+NcSFEwqV7mVk17pUz+dnIpE5oVTkb CrCznbWDqUm+VlZ4O0EK6EepaunbsjikerrPKsLLEBphtmKOx9PyRhlUa/n8Dtexj7nS Euz3Oolt+x7yUe41j8loZT+vwCT4OHroPgkrNDQHRclDymYt09QW/rujNjFGqmLRukAE ImyWzLMY0i8EhPn7e1mkLNlFDTcKuytsApiy+cV1XuUYf+5Py47rPn7oQKhQ7jePMmnW +TnN1X/YwLmIPVQ7cGaCS4iLIJUjaXGABJIveObl1Q3RJX0dyy1MamII9Z3v/Y0u0vgj 0w3A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e2-20020a17090a804200b0025ebac2314asi10046819pjw.180.2023.08.14.07.39.53; Mon, 14 Aug 2023 07:40: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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229927AbjHNOBG (ORCPT + 99 others); Mon, 14 Aug 2023 10:01:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231857AbjHNOAr (ORCPT ); Mon, 14 Aug 2023 10:00:47 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91C6C10DE; Mon, 14 Aug 2023 07:00:45 -0700 (PDT) Received: from kwepemi500026.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4RPbYc00WpztS1G; Mon, 14 Aug 2023 21:57:07 +0800 (CST) Received: from localhost.localdomain (10.175.104.82) by kwepemi500026.china.huawei.com (7.221.188.247) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Mon, 14 Aug 2023 22:00:41 +0800 From: Dong Chenchen To: , , , , CC: , , , , , , , , Dong Chenchen Subject: [Patch net, v2] net: xfrm: skip policies marked as dead while reinserting policies Date: Mon, 14 Aug 2023 22:00:13 +0800 Message-ID: <20230814140013.712001-1-dongchenchen2@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemi500026.china.huawei.com (7.221.188.247) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,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 BUG: KASAN: slab-use-after-free in xfrm_policy_inexact_list_reinsert+0xb6/0x430 Read of size 1 at addr ffff8881051f3bf8 by task ip/668 CPU: 2 PID: 668 Comm: ip Not tainted 6.5.0-rc5-00182-g25aa0bebba72-dirty #64 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13 04/01/2014 Call Trace: dump_stack_lvl+0x72/0xa0 print_report+0xd0/0x620 kasan_report+0xb6/0xf0 xfrm_policy_inexact_list_reinsert+0xb6/0x430 xfrm_policy_inexact_insert_node.constprop.0+0x537/0x800 xfrm_policy_inexact_alloc_chain+0x23f/0x320 xfrm_policy_inexact_insert+0x6b/0x590 xfrm_policy_insert+0x3b1/0x480 xfrm_add_policy+0x23c/0x3c0 xfrm_user_rcv_msg+0x2d0/0x510 netlink_rcv_skb+0x10d/0x2d0 xfrm_netlink_rcv+0x49/0x60 netlink_unicast+0x3fe/0x540 netlink_sendmsg+0x528/0x970 sock_sendmsg+0x14a/0x160 ____sys_sendmsg+0x4fc/0x580 ___sys_sendmsg+0xef/0x160 __sys_sendmsg+0xf7/0x1b0 do_syscall_64+0x3f/0x90 entry_SYSCALL_64_after_hwframe+0x73/0xdd The root cause is: cpu 0 cpu1 xfrm_dump_policy xfrm_policy_walk list_move_tail xfrm_add_policy ... ... xfrm_policy_inexact_list_reinsert list_for_each_entry_reverse if (!policy->bydst_reinsert) //read non-existent policy xfrm_dump_policy_done xfrm_policy_walk_done list_del(&walk->walk.all); If dump_one_policy() returns err (triggered by netlink socket), xfrm_policy_walk() will move walk initialized by socket to list net->xfrm.policy_all. so this socket becomes visible in the global policy list. The head *walk can be traversed when users add policies with different prefixlen and trigger xfrm_policy node merge. The issue can also be triggered by policy list traversal while rehashing and flushing policies. It can be fixed by skip such "policies" with walk.dead set to 1. Fixes: 9cf545ebd591 ("xfrm: policy: store inexact policies in a tree ordered by destination address") Fixes: 12a169e7d8f4 ("ipsec: Put dumpers on the dump list") Signed-off-by: Dong Chenchen --- v2: fix similiar similar while rehashing and flushing policies --- net/xfrm/xfrm_policy.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index d6b405782b63..33efd46fb291 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -848,6 +848,9 @@ static void xfrm_policy_inexact_list_reinsert(struct net *net, matched_d = 0; list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) { + if (policy->walk.dead) + continue; + struct hlist_node *newpos = NULL; bool matches_s, matches_d; @@ -1253,11 +1256,14 @@ static void xfrm_hash_rebuild(struct work_struct *work) * we start with destructive action. */ list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) { + if (policy->walk.dead) + continue; + struct xfrm_pol_inexact_bin *bin; u8 dbits, sbits; dir = xfrm_policy_id2dir(policy->index); - if (policy->walk.dead || dir >= XFRM_POLICY_MAX) + if (dir >= XFRM_POLICY_MAX) continue; if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) { @@ -1823,9 +1829,11 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) again: list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) { + if (pol->walk.dead) + continue; + dir = xfrm_policy_id2dir(pol->index); - if (pol->walk.dead || - dir >= XFRM_POLICY_MAX || + if (dir >= XFRM_POLICY_MAX || pol->type != type) continue; @@ -1862,9 +1870,11 @@ int xfrm_dev_policy_flush(struct net *net, struct net_device *dev, again: list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) { + if (pol->walk.dead) + continue; + dir = xfrm_policy_id2dir(pol->index); - if (pol->walk.dead || - dir >= XFRM_POLICY_MAX || + if (dir >= XFRM_POLICY_MAX || pol->xdo.dev != dev) continue; -- 2.25.1