From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E2E2C64EC4 for ; Sat, 18 Feb 2023 11:13:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229521AbjBRLNI (ORCPT ); Sat, 18 Feb 2023 06:13:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbjBRLNH (ORCPT ); Sat, 18 Feb 2023 06:13:07 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA3FF1A958 for ; Sat, 18 Feb 2023 03:13:06 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 466D460A05 for ; Sat, 18 Feb 2023 11:13:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E338C433EF; Sat, 18 Feb 2023 11:13:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1676718785; bh=Rg7aju2pPws5hO70XKDsuk0apFqw3ngfpAfrqk+PvpY=; h=Subject:To:Cc:From:Date:From; b=sYc32h2U5aPvW9MtSL0U6Cz7iJPLb8aj4CS9ecmBugFgQgff2qZuy/7SOgvUu1G2u n1u5xXqVwBEjKdXulPwCq0yL/nNaI1HZLaPgE/I3vo8Mn9EKcNtka49eLv2Zr4Itpg lsbER81cTt8nTRXupugEE5q2E/bfsOIfIDRyitdc= Subject: FAILED: patch "[PATCH] net/sched: tcindex: update imperfect hash filters respecting" failed to apply to 4.19-stable tree To: pctammela@mojatatu.com, jhs@mojatatu.com, kuba@kernel.org, sec@valis.email Cc: From: Date: Sat, 18 Feb 2023 12:12:52 +0100 Message-ID: <1676718772139185@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org The patch below does not apply to the 4.19-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . Possible dependencies: ee059170b1f7 ("net/sched: tcindex: update imperfect hash filters respecting rcu") 304e024216a8 ("net_sched: add a temporary refcnt for struct tcindex_data") 599be01ee567 ("net_sched: fix an OOB access in cls_tcindex") 14215108a1fd ("net_sched: initialize net pointer inside tcf_exts_init()") 51dcb69de67a ("net_sched: fix a memory leak in cls_tcindex") 3d210534cc93 ("net_sched: fix a race condition in tcindex_destroy()") 12db03b65c2b ("net: sched: extend proto ops to support unlocked classifiers") 7d5509fa0d3d ("net: sched: extend proto ops with 'put' callback") 726d061286ce ("net: sched: prevent insertion of new classifiers during chain flush") 8b64678e0af8 ("net: sched: refactor tp insert/delete for concurrent execution") fe2923afc124 ("net: sched: traverse classifiers in chain with tcf_get_next_proto()") 4dbfa766440c ("net: sched: introduce reference counting for tcf_proto") ed76f5edccc9 ("net: sched: protect filter_chain list with filter_chain_lock mutex") a5654820bb4b ("net: sched: protect chain template accesses with block lock") bbf73830cd48 ("net: sched: traverse chains in block with tcf_get_next_chain()") 165f01354c52 ("net: sched: protect block->chain0 with block->lock") c266f64dbfa2 ("net: sched: protect block state with mutex") a030598690c6 ("net: sched: cls_u32: simplify the hell out u32_delete() emptiness check") 787ce6d02d95 ("net: sched: use reference counting for tcf blocks on rules update") 0607e439943b ("net: sched: implement tcf_block_refcnt_{get|put}()") thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From ee059170b1f7e94e55fa6cadee544e176a6e59c2 Mon Sep 17 00:00:00 2001 From: Pedro Tammela Date: Thu, 9 Feb 2023 11:37:39 -0300 Subject: [PATCH] net/sched: tcindex: update imperfect hash filters respecting rcu The imperfect hash area can be updated while packets are traversing, which will cause a use-after-free when 'tcf_exts_exec()' is called with the destroyed tcf_ext. CPU 0: CPU 1: tcindex_set_parms tcindex_classify tcindex_lookup tcindex_lookup tcf_exts_change tcf_exts_exec [UAF] Stop operating on the shared area directly, by using a local copy, and update the filter with 'rcu_replace_pointer()'. Delete the old filter version only after a rcu grace period elapsed. Fixes: 9b0d4446b569 ("net: sched: avoid atomic swap in tcf_exts_change") Reported-by: valis Suggested-by: valis Signed-off-by: Jamal Hadi Salim Signed-off-by: Pedro Tammela Link: https://lore.kernel.org/r/20230209143739.279867-1-pctammela@mojatatu.com Signed-off-by: Jakub Kicinski diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c index ee2a050c887b..ba7f22a49397 100644 --- a/net/sched/cls_tcindex.c +++ b/net/sched/cls_tcindex.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -339,6 +340,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, struct tcf_result cr = {}; int err, balloc = 0; struct tcf_exts e; + bool update_h = false; err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); if (err < 0) @@ -456,10 +458,13 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, } } - if (cp->perfect) + if (cp->perfect) { r = cp->perfect + handle; - else - r = tcindex_lookup(cp, handle) ? : &new_filter_result; + } else { + /* imperfect area is updated in-place using rcu */ + update_h = !!tcindex_lookup(cp, handle); + r = &new_filter_result; + } if (r == &new_filter_result) { f = kzalloc(sizeof(*f), GFP_KERNEL); @@ -485,7 +490,28 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, rcu_assign_pointer(tp->root, cp); - if (r == &new_filter_result) { + if (update_h) { + struct tcindex_filter __rcu **fp; + struct tcindex_filter *cf; + + f->result.res = r->res; + tcf_exts_change(&f->result.exts, &r->exts); + + /* imperfect area bucket */ + fp = cp->h + (handle % cp->hash); + + /* lookup the filter, guaranteed to exist */ + for (cf = rcu_dereference_bh_rtnl(*fp); cf; + fp = &cf->next, cf = rcu_dereference_bh_rtnl(*fp)) + if (cf->key == handle) + break; + + f->next = cf->next; + + cf = rcu_replace_pointer(*fp, f, 1); + tcf_exts_get_net(&cf->result.exts); + tcf_queue_work(&cf->rwork, tcindex_destroy_fexts_work); + } else if (r == &new_filter_result) { struct tcindex_filter *nfp; struct tcindex_filter __rcu **fp;