Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp1200651rdb; Wed, 16 Aug 2023 05:02:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6RIqRx169rl7RGyNfG0SRTel+/MDyhDcTOJEvKu1KwTSW7pUV3ZptpVrG4XBcqRDRN7+l X-Received: by 2002:a17:902:c3c1:b0:1bc:4171:67e3 with SMTP id j1-20020a170902c3c100b001bc417167e3mr1672614plj.31.1692187328065; Wed, 16 Aug 2023 05:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692187328; cv=none; d=google.com; s=arc-20160816; b=sCuxs5F0lC69cbxJrtI1+MBA5+AFg6xsstx2CHxzysTUJPlDFlHry6fxoW+O2EaA6U W1+PizAeSmHVELmIwEEU9+wTFfmZmq2VgTr80NJKKe4TZ7mKFffNVKkUF8FnfKEOEDXU QHyrEdyvmMM/XLWaT2W6IsWmZ168BGnW8ISzamHhIlyKzikcfecEKbnVW+eM0uKK/AHm 4sdCGJZrTmZaPeB0Qd67eIZFc6/mGcuEuVoVhROpfds32D25LAZ8GllVPQqKCYgxxXuY B4ZZ0L/57ZMnkqCR9d+I5ExOLhGDPbLT8rCH4JtsMgCv3lO7FkImMFRWE5s+nzXkPOMj 4dlw== 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=yH56Gm4YFZuCTxQcGi50rlZZL8Ff6EujChJKsQPDU4E=; fh=k1tuNqTCf//rvh9z5NPIAUd1fOWih+UpTTDJBzGudb8=; b=eqYCCkrDRXFAX6LQambYirq9gTnQ684vsjN0Rr1Kr4V2a6FxwC3Ki3bFk7YS4h548c Plp+avLOEZfG2orW5bjruYmHsSEiOQk46vpo+YxXMvs4kZZDlwnjlSFP+wCoNsrGL4R8 bpLYCgN1GT3VVfck/jhH93dexq6dMnUItfaSs35a0FOzXzFI7xh8qNRFUN40m/PjXJaG JUC0LmhBca1y9DM5KbSewyB2hU8BBEQ0SXzJMAxIm97Tvf1FmZVMIT8lMN1pyBjpeX2q kwi3rx00+ie20xTVzUNwwnKK4XEtcv2xXeU0TeuRtyfivKd3ex8jnnk1cgwo3/6LlG8n 7trg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TN2ea5ME; 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 s10-20020a170902ea0a00b001b9e38b8167si8500144plg.169.2023.08.16.05.01.44; Wed, 16 Aug 2023 05:02:08 -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=TN2ea5ME; 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 S235946AbjHOJPL (ORCPT + 99 others); Tue, 15 Aug 2023 05:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236080AbjHOJOE (ORCPT ); Tue, 15 Aug 2023 05:14:04 -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 32799E5F for ; Tue, 15 Aug 2023 02:13: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 2EBEC650CE for ; Tue, 15 Aug 2023 09:13:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 936E8C433C7; Tue, 15 Aug 2023 09:13:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692090809; bh=eV1YTMJMzlnIU4HCb9/vrK3w7/XNJSy/N3IcgaVrgEM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TN2ea5MEpMjTVPbWwxxp09WOwY2Uz2jgYf8okq7Ls2Y0S9sbyjC9b1ZGnLXNQu/lP qjMstjy+ahUvg29NCDV8cypxP4GuhOPGVbZ8DjzesBH2b3G3hNgsDgINb3TUtA7qWr nENpOilt6SQmt2Sbb66sYWBbFZuZms9750SSHYVPa/28UXEYv5aGUEYZAjQyvG6woa Tfq9J+Tm8yjBeTa2HoO2VG44Ym7BmubVWdi0RGZD+9RCsbAr+xzCZi3jQsezQ1UVMb nGtKXfC15QiXdiTSKg/glvwTNGWPkwyXXH6XaapU5uKO1Db6txwlOyGN8BWjct9GnH cGKg3UR1sgMqA== Date: Tue, 15 Aug 2023 12:13:24 +0300 From: Leon Romanovsky To: Dong Chenchen Cc: fw@strlen.de, steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, 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: <20230815091324.GL22185@unreal> References: <20230814140013.712001-1-dongchenchen2@huawei.com> <20230815060026.GE22185@unreal> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230815060026.GE22185@unreal> 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 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 Tue, Aug 15, 2023 at 04:47:58PM +0800, Dong Chenchen wrote: > 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(-) <...> > >> @@ -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. > > > The walker object initialized by xfrm_policy_walk_init() doesnt have policy. > list_for_each_entry() will use the walker offset to calculate policy address. > It's nonexistent and different from invalid dead policy. It will read memory > that doesnt belong to walker if dereference policy->index. > I think we should protect the memory. But all operations here are an outcome of "list_for_each_entry(policy, &net->xfrm.policy_all, walk.all)" which stores in policy iterator the pointer to struct xfrm_policy. How at the same time access to policy->walk.dead is valid while policy->index is not? Thanks