Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp2522235rdg; Mon, 14 Aug 2023 05:28:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWnHqeDwF96GnepFGN4POOsfqhJkCorw/QOuKBifEoNJ2M8NGcp3nFX9ilW3BD1b7HxJG4 X-Received: by 2002:a05:6a20:7d88:b0:13f:c159:63ec with SMTP id v8-20020a056a207d8800b0013fc15963ecmr13389551pzj.24.1692016092003; Mon, 14 Aug 2023 05:28:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692016091; cv=none; d=google.com; s=arc-20160816; b=QZmGtgSqXcsN7g4z+uXHSxhELrEHgVvswvYVy0qdleCfTqTUHdFgZKfr8uYwtAIwGn EPrc+L8Ll3z2zFuWBL9WfrXdYtvQGEPl+ytpuGMOkWiB0c1I0750TTDKSt61PbmtvVvw 86uTNa4/vstuDSwDrIRXRZ6McEccDvdxV+ZEGCc3oQoP9XjtdtQs9KhQVudriaE/qJf2 wP44xIN1RZULsq/EdebkzLpfvmIff/vkBs4zUY9HLZ6ZjCO3A1t5c75gsZnb/mqOTUzw 9I9pAEAkostEYBjdcIc30jEsLfDDNvFrTiQQ9yxzvGm2/gu+XT4NCRl4M5Az874DuaHK nCnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=JGW5g2K/uzMBrdskopAy5z+SLiOOVJlx4OEbuyVf274=; fh=+I/qrd3kwIOBb+3lIQA0jaSPInUthNkPR78Sh/1d99c=; b=T+xLcasaI5yPHIsAa37JzJOTU2ohbgfSU0+Uc2cJv9GPfxWpXifAhC46ICTEdHXbyb 57/JxFxjNqJJ1Eo+mNGYw0qLkLWS+92sfm5ST43qmoyP43b8aX3fAADVgSNK3ghWkEGT 98pq9qaH95BW4cOmFPxupVnr/9FGk0BtRrCoANPpnDh3T48TZdAKg4psfpnm7OlUPtUU wrmF2MfYte0ECrWqm6v7uUf8x1fryOHNbz1qnone8r34/0Fv7JI+0ymvCiGXw5wOkZgl d4P1AA/b7LR8Ao7xZSlyglkpbkIKLRbnnl3MQ8mKBE1aOCQ4LwbT+SvL12vtX8QZ0Le2 T/kA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r135-20020a632b8d000000b0055b635b788fsi7901262pgr.199.2023.08.14.05.27.59; Mon, 14 Aug 2023 05:28: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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233966AbjHNLb2 (ORCPT + 99 others); Mon, 14 Aug 2023 07:31:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230112AbjHNLa5 (ORCPT ); Mon, 14 Aug 2023 07:30:57 -0400 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [IPv6:2a0a:51c0:0:237:300::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F55CFA; Mon, 14 Aug 2023 04:30:56 -0700 (PDT) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1qVVmM-00024k-7v; Mon, 14 Aug 2023 13:30:34 +0200 Date: Mon, 14 Aug 2023 13:30:34 +0200 From: Florian Westphal 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: <20230814113034.GB7324@breakpoint.cc> References: <20230814013352.2771452-1-dongchenchen2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230814013352.2771452-1-dongchenchen2@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS,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 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. > > 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; > + Looks like we have other places that might trigger a splat, e.g.: 1816 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) .. 1827 again: 1828 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) { 1829 dir = xfrm_policy_id2dir(pol->index); 1830 if (pol->walk.dead || 1831 dir >= XFRM_POLICY_MAX || 'walker' has no pol->index, so I suspect this would trigger a kasan splat as well, this needs to check walk.dead before access to pol->index.