Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp510292rdb; Tue, 15 Aug 2023 04:00:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHgDG509UKC46VQgyKSqnGplw+JFAu84yyObi5jOhkPUq7UWKdI3GZ85agNl4VgQuQnyhJ1 X-Received: by 2002:a05:6402:3c9:b0:523:d1ab:b2ed with SMTP id t9-20020a05640203c900b00523d1abb2edmr9104527edw.29.1692097225243; Tue, 15 Aug 2023 04:00:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692097225; cv=none; d=google.com; s=arc-20160816; b=LCuxyziJjpBO2ezkgUMK5UAVezb7zLLC59il+r50P8qOxIMTZ2y0h2nXH3n7FOIXO0 riBUinOaE0mB7Megij6+VLrOmVGp9SrIl/7YfN65KWbiFnKxEyH6V32IXny2NMY1K5v3 2CI6Mz2K7JLexJlf3v7UPr+sLUV135ASP/X5gkd1weJ9ISpzd9DDNQ6BqYOt7GlOF6Dq xIp7ZDeAEI7Xrfl21CUqcek1u+82lQHgWjEHJUsHpF7gpz9GXbA+fzV2ycRdWYJLoAPz SjdXdMdOgPp2+XOfCySCTpHIgrlzB9A1YqdIQ/vdyBYTOyHI+gTBVs89oOs4pZdBp9+U uy8Q== 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=khZxzAUrjWwCJQYkzDSeZVdP3INZCzgK41zRm57PyIk=; fh=5BxXQpFdwEIxxtciG3Kz0fqVDKCrsRqPOpnP42CzmaY=; b=vQH3bQj2vjKNnKnffFD900Xh+5NFSbEVF/gNhpvgjZ/BgqLstcOGnnc657omLOi5UX NtgDoVjzGG2gF3MPLW8pRWbWDnKqseZTxifSst9Zq0n3qya+mE5DkA3PGmWqjmhhy+KS XNieQg1jNTceyRfCSPl7/dtM6MU5Y70mkJhtJVDWqljBigJIxj2Q3uNyjBOuBbagzUCX eHm19s0SabmlvEEH7BLPRiZYXgynur21zEjXNLRopwa3CeCJGNrdDKv1RNT9L/EAYNtJ saLX8O5kA+os3PlAu6nZKrO732j/9ui8i759EVYrtaxCZue508V5S5QyvE8EhgQm+NJt Ra8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AUcnopSO; 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 d2-20020a50fb02000000b005231f2d8973si9246744edq.365.2023.08.15.03.59.56; Tue, 15 Aug 2023 04:00:25 -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=AUcnopSO; 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 S234761AbjHOGA6 (ORCPT + 99 others); Tue, 15 Aug 2023 02:00:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234251AbjHOGAd (ORCPT ); Tue, 15 Aug 2023 02:00:33 -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 E2FCD93 for ; Mon, 14 Aug 2023 23:00:31 -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 7255561913 for ; Tue, 15 Aug 2023 06:00:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1F65C433C8; Tue, 15 Aug 2023 06:00:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692079230; bh=lE/p8zYP1Dnrd2GfRHhm178K3wSG7PAmcbJdgZN3zMY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AUcnopSOQuAtcEZlQxH0wEAzQNwtpTFHBjHO0qzwHexcD1KPwRnvaBVgRk9iBYl6+ ndhp9UXjqKFHApQTQ1DAKoJ2gmqPCBnxr9Yp8EfXqDihGe5HOHEGmT4SrdOd4ywCgN DHix8isRSjA+rv2MXJFW1mhfduvafwq/QQ24SI/yb6aIY+tI+ucEqJ1DesoV5I4onO wh+A4y/UoS42bxmMWpMkqcRSM5Pek9HPpH9GWWNALX2BJDcKm4iOmKzYKrZD44Mg/a AXipfeLMZmmpD8rDkpbZDS6McPDU29CQsb9UDnxftvoPdFi2GjeZHP3dOsZv6YUUd7 NhcW8AAz7Hn+A== Date: Tue, 15 Aug 2023 09:00:26 +0300 From: Leon Romanovsky To: Dong Chenchen Cc: steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, fw@strlen.de, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, timo.teras@iki.fi, yuehaibing@huawei.com, weiyongjun1@huawei.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [Patch net, v2] net: xfrm: skip policies marked as dead while reinserting policies Message-ID: <20230815060026.GE22185@unreal> References: <20230814140013.712001-1-dongchenchen2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230814140013.712001-1-dongchenchen2@huawei.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 10:00:13PM +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. > > 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; You can't declare new variables in the middle of execution scope in C. > > @@ -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; Same comment as above. > > dir = xfrm_policy_id2dir(policy->index); > - if (policy->walk.dead || dir >= XFRM_POLICY_MAX) > + if (dir >= XFRM_POLICY_MAX) This change is unnecessary, previous code was perfectly fine. > 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 || This change is unnecessary, previous code was perfectly fine. > 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; Ditto. > > -- > 2.25.1 >