From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-vk1-f170.google.com (mail-vk1-f170.google.com [209.85.221.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D0CA36BCF2 for ; Mon, 1 Jun 2026 15:15:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780326917; cv=none; b=C6+liRFgPwGOaAcq+jIDwTs+rgH6w9zTYAzPr4vVVgoGZdmL4QTRgrT5qW6CzcYwUtKryfmm4QTrLVb/Syp4TFil0D7S75BqOvbxjUm54zJHx7clSIEBDW8K4ldvz6b4BRhd8e9csRAEIis6kVxGX+XfXKJxRbWwCUfUV4i6Fh8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780326917; c=relaxed/simple; bh=2MXUrqP50B7XcVz861ZemOFtYgIly9nH4PrO6Ic6ym8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SGM0LOz8wVpc4ewXbzkJcxO+F+XoAv0uUYi0OKNxSfFm2NllLqfYkF8Q8vCFYyCmT+QDPfZ4EPveHCFJFC5OoAiWPxoz690GfBT7cSCOa+A4r434Icy6r1f5iy4GSKmy248brrjQbQ1iX69yY8B6GNwnmliG0GxrPTrQXDujzcM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mojatatu.com; spf=none smtp.mailfrom=mojatatu.com; dkim=pass (1024-bit key) header.d=mojatatu.com header.i=@mojatatu.com header.b=y0Vbt2pO; arc=none smtp.client-ip=209.85.221.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mojatatu.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=mojatatu.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mojatatu.com header.i=@mojatatu.com header.b="y0Vbt2pO" Received: by mail-vk1-f170.google.com with SMTP id 71dfb90a1353d-5a0e9e798e4so351237e0c.1 for ; Mon, 01 Jun 2026 08:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mojatatu.com; s=google; t=1780326912; x=1780931712; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=FPTNIzliAheYDBgMd3OesTJXCtynKcxw/qSbdwpq3CA=; b=y0Vbt2pOvxdqxpI+hzyaT9AZasgy+TKjH8r12s8uerDzhO+lzRltZ0dkteH2qOx64s /U2JSGMSm27D+7eh9g9xVQVpD5zXYzrkEamKzDVjQy42jwV3Nh1JhRBI7oylGbVOlh9E WCb6GFs4aayoqoNDvINj7sGSJdSGwH42rqTY4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780326912; x=1780931712; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FPTNIzliAheYDBgMd3OesTJXCtynKcxw/qSbdwpq3CA=; b=fZzuaZMf7JOnk71IK072HDsqhrg/I46J88TS8usjwsyHabNkEcb1mUTr5bnK7Er7Ar Ib8Hl0L16H/CDQxVo/yDkmHzKDP37GAHzR2G/uozlg8iGMpvI+v+q1w/TbaWvaA8zzkE uLSD7HIXASmLkrR375Qhkvmzgxkv8jjE58tDhwQKmHXDY/1llNwTQpaBFueBRYEn5xkG PI69a1cjqhNrubQ4DAL5OWvTu6Me0ERNUMcePr4iVYsHtKpiCaljTOA5FJSf/ssGwqy1 42VOv09sm+fUOFA+LNFNw9XAgjEbcwdJTOInyACh7hRYi28MPZ8KI8ofTQo+TufagqDo H0tw== X-Forwarded-Encrypted: i=1; AFNElJ9Dmn00AiQOBMyDRcw6aPGNsgrm31LHDwK836XUQjIqoExA7NMnPkjOyUyw2F/3xeDS/iTfOU68cUMgUgk=@vger.kernel.org X-Gm-Message-State: AOJu0YzwN1F6HrnQelDrp+YbECSiXo4JsYifQL694YNZK1xfl9AIMB7i foIKHuJZ2mYSxLe6BAkndfNYoTNNSHO4UOzddO9jfiOOPCt9t051FDKvipi/2789nEk4/FlKRsi yBNs= X-Gm-Gg: Acq92OFnes5IMrJMpvvaj98AguOtkH1ds7BaQMALMBlWgxUBNlKot3rPsMmKN0b4M08 iWIrgZSrVVKYFhqIXfijoyGcv+i8i7uCoOvl+oMszd2oct5x3T0OFUvraIvFXzqtyVsRk72YEDH 4LPnlg8Qc6TM1e1UxXmFoCWDYROnSvbDtfTxhNwvL9Q10AEs1fXRnsRjfcV91zDLlMVH8AmT1ad GEWrYcKoXP8xhhhDwRXQjobfhL33y7kkGcUIczsSF2y9bi+r+RM+AofsdACV9K9K/XajyNlHMko Sx6ZGhnZegCYqaa25w/vGJ+36F82eZRLjIcb8uOniXSbTY35LJje84PJljNnRrs+C2rcmXS0XCJ KeySX43pPhT8d48srO8MeZWRBpPvr+hxFgPp7L2p43L+G40xuAK5xhkAGgE4JLykkrSWxPxwSty FtWrTb1BT5F7CjejXECSBq4SwCt7xmIGem3ccavkxARw== X-Received: by 2002:a05:6122:489a:b0:575:36c4:8900 with SMTP id 71dfb90a1353d-59bf56cc72dmr5061994e0c.14.1780326912250; Mon, 01 Jun 2026 08:15:12 -0700 (PDT) Received: from [192.168.50.25] ([179.218.14.134]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-599d4754259sm7314403e0c.7.2026.06.01.08.15.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Jun 2026 08:15:11 -0700 (PDT) Message-ID: Date: Mon, 1 Jun 2026 12:15:04 -0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH net v2 1/1] net/sched: act_api: use RCU with deferred freeing for action lifecycle To: Jamal Hadi Salim , netdev@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, victor@mojatatu.com, kylebot@openai.com, jiri@resnulli.us, vladbu@nvidia.com, linux-kernel@vger.kernel.org, security@kernel.org, stable@kernel.org, syzbot@syzkaller.appspotmail.com References: <20260531160812.68020-1-jhs@mojatatu.com> Content-Language: en-US From: Pedro Tammela In-Reply-To: <20260531160812.68020-1-jhs@mojatatu.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 31/05/2026 13:08, Jamal Hadi Salim wrote: > When NEWTFILTER and DELFILTER are run concurrently it is possible to create a > race with an associated action. > > Let's illustrate with CPU0 running NEWTFILTER and CPU1 running DELFILTER: > > 0: mutex_lock() <-- holds the idr lock > 0: rcu_read_lock() > 0: p = idr_find(idr, index) <-- action p is valid (RCU protects IDR) > 0: mutex_unlock() <-- releases the idr lock > 1: refcount_dec_and_mutex_lock() <-- refcnt 1->0, mutex held > 1: idr_remove(idr, index) <-- Action removed from IDR > 1: mutex_unlock() <-- mutex released allowing us to delete the action > 1: tcf_action_cleanup(p); kfree(p) <-- Kfrees p immediately, no deferral > 0: refcount_inc_not_zero(&p->tcfa_refcnt) <-- ouch, UAF p points to freed memory > > This patch fixes the race condition between NEWTFILTER and DELFILTER by > adding struct rcu_head to tc_action used in the deferral and introducing a > call_rcu() in the delete path to defer the final kfree(). > > Note: this is a revert of commit d7fb60b9cafb ("net_sched: get rid of tcfa_rcu") > but also modernization/simplification to directly use kfree_rcu(). > > Let's illustrate the new restored code path: > > 0: rcu_read_lock() > 1: refcount_dec_and_mutex_lock() <-- refcnt 1->0, mutex held > 1: idr_remove(idr, index) > 1: mutex_unlock() > 1: call_rcu(&p->tcfa_rcu, tcf_action_rcu_free) <-- defer kfree after grace period > 0: p = idr_find(idr, index) > 0: refcount_inc_not_zero(&p->tcfa_refcnt) <-- fails, refcnt already 0 > 1: rcu_read_unlock() <-- release so freeing can run after grace period > > After CPU1 calls idr_remove(), the object is no longer reachable through the IDR. > CPU0's subsequent idr_find() will return NULL, and even if it still held a > stale pointer, the immediate kfree() is now deferred until after the RCU grace > period, so no UAF can occur. > > Fixes: d7fb60b9cafb ("net_sched: get rid of tcfa_rcu") > Suggested-by: Jakub Kicinski > Reported-by: Kyle Zeng > Tested-by: Victor Nogueira > Tested-by: syzbot@syzkaller.appspotmail.com > Signed-off-by: Jamal Hadi Salim Reviewed-by: Pedro Tammela > --- > v1->v2 > 1) syzbot ci found revealed that the cleanup code could be called under > softirq/atomic context. Because tcf_action_cleanup() calls __tcf_chain_put() > which grabs a mutex (which can sleep). Fix is to move tcf_action_cleanup() > to be invoked before call to rcu. > 2) And while looking closely i noticed a bad comment on action code which > claims that "actions are always connected to filters" ;-> Tracing back > on git i noticed that infact we did have exactly this new change circa 2017 > that was erronously removed by commit d7fb60b9cafb. Well, at least I > am certain what the "Fixes" tag should be now ;-> > 3) Patch is much much simpler now than it was in v1 > --- > include/net/act_api.h | 1 + > net/sched/act_api.c | 7 +------ > 2 files changed, 2 insertions(+), 6 deletions(-) > > diff --git a/include/net/act_api.h b/include/net/act_api.h > index d11b79107930..fd2967ee08f7 100644 > --- a/include/net/act_api.h > +++ b/include/net/act_api.h > @@ -45,6 +45,7 @@ struct tc_action { > struct tc_cookie __rcu *user_cookie; > struct tcf_chain __rcu *goto_chain; > u32 tcfa_flags; > + struct rcu_head tcfa_rcu; > u8 hw_stats; > u8 used_hw_stats; > bool used_hw_stats_valid; > diff --git a/net/sched/act_api.c b/net/sched/act_api.c > index 332fd9695e54..04ea11c90e03 100644 > --- a/net/sched/act_api.c > +++ b/net/sched/act_api.c > @@ -112,11 +112,6 @@ struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action, > } > EXPORT_SYMBOL(tcf_action_set_ctrlact); > > -/* XXX: For standalone actions, we don't need a RCU grace period either, because > - * actions are always connected to filters and filters are already destroyed in > - * RCU callbacks, so after a RCU grace period actions are already disconnected > - * from filters. Readers later can not find us. > - */ > static void free_tcf(struct tc_action *p) > { > struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1); > @@ -129,7 +124,7 @@ static void free_tcf(struct tc_action *p) > if (chain) > tcf_chain_put_by_act(chain); > > - kfree(p); > + kfree_rcu(p, tcfa_rcu); > } > > static void offload_action_hw_count_set(struct tc_action *act,