Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp2470401rdg; Mon, 14 Aug 2023 03:47:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHq9ldzsFugGt2E4b24DrWk+6JlwLLaZpLC2L5ykOEuRP3TLQGEbfgOPqwRxtHu9XlS2nOs X-Received: by 2002:a17:906:5dc1:b0:99b:499d:4635 with SMTP id p1-20020a1709065dc100b0099b499d4635mr6678650ejv.75.1692010061094; Mon, 14 Aug 2023 03:47:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692010061; cv=none; d=google.com; s=arc-20160816; b=utmfcBk3llkLekyykrNKPk4QW+2d/adHH/C8yWyet6gWNVhplAgh96inn9bAu5yEiY goXtEeaJwHpiG2qWWFcfD2pSxAWy5e+y4BgSi67n3vZHkwYpMQ/9lAQ/uPxIPbFV0KPd R6Oi/Hr4xG2lU27ZPV5FHaVcfVCI3fMfCP6R9vTY1MovjkKcCrGxfuXoU37SpAqhk6re qOmXdIry+ZNkHxbqbBDk/6NQQHk1VvH7xPgbQO9Aob35z8BNZ1vJffjQ/C7+P/IoA+Is RIdR+Ozr7UNDb8t3or02FBso59/yiojklu6j+BxIC/7MOpP70ECA00+Ofxy1813iPfUE VIHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=SuEKwaGjW8+cSqdbg0dyLaUTG7P+lpITUCzW6X8m/mU=; fh=kG69bQSXTN93g8RmSGG/G0qB3KLLnwaQLzyuWr6jiuc=; b=HB3NhtOpqjONhyjjOiVdTgYYK8wgXYXJa+xlejEUAT7A0MwUeNaexo0Lh4i4V7XfJv vLyujjTXefAgtKYsM25E5C8W2inCuus6nJFhWFgT+ynvDsRaY2vdZy9zSoPcBRmG1FA7 w7ZlFR7uf7KScjNS8r9Cc/hdBV3iLwT+BRFjztKGj5stbEhP3p9zkhe/T7ypjhu8GS5y BDHR/53iCawswHYwsa4KO53jESPQMpIBLatjuEicCgRzGUgu/i6IUd06KxJIz8vskWNw MwQ4hw5E9cACWOCsGrcpygdQ645hD0lXjvA5i0m9wZ60qCM0dFMrhV/jhmdEjKcxbDYM RDKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hn30rE1O; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt24-20020a170906b79800b00989027eb30bsi7348125ejb.610.2023.08.14.03.47.16; Mon, 14 Aug 2023 03:47:41 -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=@kernel.org header.s=k20201202 header.b=hn30rE1O; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235123AbjHNKK4 (ORCPT + 99 others); Mon, 14 Aug 2023 06:10:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235792AbjHNKKg (ORCPT ); Mon, 14 Aug 2023 06:10:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B26A10C1 for ; Mon, 14 Aug 2023 03:10:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CA0B661190 for ; Mon, 14 Aug 2023 10:10:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88466C433C8; Mon, 14 Aug 2023 10:10:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692007817; bh=bSsdTL71z4hGrSCltN3Y+AqF61F+d6Q3fdKuz/7vM9c=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=hn30rE1OHQgjLAlP5+msmRlW5EYFw0eFluVuWY4hzDe/U/9Oju19LSQzb8BQWAunu fNWN3r4A+/iA7OnJXsiDsGJIxeZzUKHrMjC3LMn9qZDI6w5PkdSyFFHJGm0Q0UeAs+ PZhpZO2nit1KP1T3pP41jPTCdr8jQgMyRXrhoTIhYh/Jfn3InFMxQgJNRMFZRgbSXi FrLXVBNPLK58102Xmle3dC42yL4QUj9048QZU/UHn1bQwh9472cqdtoazf939gq45X z0fnqLH7ppkCHbGysqAZSxvaYUmA3VNFJ2rixJ2b9xlaAw1esW9KDOOxMunq3N8zDd it/vEu5HDEpFQ== Date: Mon, 14 Aug 2023 13:10:13 +0300 From: Leon Romanovsky To: Dong Chenchen Cc: steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, fw@strlen.de, timo.teras@iki.fi, yuehaibing@huawei.com, weiyongjun1@huawei.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net] net: xfrm: skip policies marked as dead while reinserting policies Message-ID: <20230814101013.GC3921@unreal> References: <20230814013352.2771452-1-dongchenchen2@huawei.com> <20230814070349.GA3921@unreal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230814070349.GA3921@unreal> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 On Mon, Aug 14, 2023 at 04:58:46PM +0800, Dong Chenchen wrote: > On Mon, Aug 14, 2023 at 09:33:52AM +0800, Dong Chenchen wrote: > >> 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. > >> > >> It can be fixed by skip such "policies" with walk.dead set to 1. > > > >But where in the xfrm_dump_policy() flow, these policies are becoming to > >be walk.dead == 1? > > > >Thanks > > > user will use xfrm_dispatch[XFRM_MSG_GETPOLICY] ops to get xfrm policy. > > .start = xfrm_dump_policy_start > > xfrm_dump_policy_start() will set walk.dead to 1 by call > xfrm_policy_walk_init(). Thanks > > Thanks > >> > >> 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 > >> --- > >> net/xfrm/xfrm_policy.c | 3 +++ > >> 1 file changed, 3 insertions(+) > >> > >> diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c > >> index d6b405782b63..5b56faad78e0 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; > >> > >> -- > >> 2.25.1 > >> > >> >