Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp204043rwb; Mon, 28 Nov 2022 19:21:31 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ap6/9k2+FnW7hLrQ7Tddu5W8LOQa6vj7IOZN7XwqcEwdY39/bDbNoDTKYx5GgZeieFcMi X-Received: by 2002:a17:902:7d94:b0:187:4920:3a7e with SMTP id a20-20020a1709027d9400b0018749203a7emr38748435plm.171.1669692091617; Mon, 28 Nov 2022 19:21:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669692091; cv=none; d=google.com; s=arc-20160816; b=lWD5lfRla89/2Ict1NpihQ3Z68SZIptPxVOrxLuKxSZu7Fh8icXqxyBHskGOirRoHK l8Vv6uE56ftDG7kPb00eHSt3/Q0GPrwWq5Y6KG+a5cHC14sfn7vRW3ExnElf8r+kjwre LPM+hp+U/K91Lr6JgZmiR1EMTTQFuRg1CVdivs0ZqBYuhhucpcoCsrvDG6tayLLBQ4vK 29Cq61BQUw5U9mX+0UpeTNahLtFmzUeUtqUfssicTfpMfsXQ4ywAK1X0feubkWF3z7Sg cLPBX8SUGSUh4Tj+tvKn5ezReJ7GxivuBrNGfrrUdr2dJTLtZqfTpOSjDx4zQvfIdYxN ZsKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=35p5Pzvij06883L5xcotqkJm3JL5Gnq7PcCbaLDkUh8=; b=OfNAMhTfQX2Zl6r0ceoR4m/Vl4cYc+BfCFI66FiEF+XX6pEOrhsxtozhPoYjadR53Z FtuRwmV7vefRpfTg1HlfwedzahqnSF0dnzcLQoWUpUhNbb/+3vGCl61Kzj1oLt2KWeeU f7wzqhnGgUioYiYs1gLlbOfdhvNuf2kPq/xTnEIdljTINiXyKEbdo1I71tK8h3M+4qGa Sf/yaQDQrUnCRJypccnRTXi7CzH0xrRavpiB6uIgGvz3FVGINaMALkG9B4APNl7tqvgk suyud2SMQq65C3hH2VAxmbUvcAzZhBhl95AjIf2uFwIpZx7oQYIh20l0wAcTjCZ4h0Zx aIyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Y7b9duHr; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c20-20020a6566d4000000b00478446557bdsi126721pgw.18.2022.11.28.19.21.20; Mon, 28 Nov 2022 19:21:31 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=Y7b9duHr; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235266AbiK2CxG (ORCPT + 82 others); Mon, 28 Nov 2022 21:53:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235206AbiK2CxA (ORCPT ); Mon, 28 Nov 2022 21:53:00 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDBDF2A428; Mon, 28 Nov 2022 18:52:57 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id 82so4278797pgc.0; Mon, 28 Nov 2022 18:52:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=35p5Pzvij06883L5xcotqkJm3JL5Gnq7PcCbaLDkUh8=; b=Y7b9duHr6K89L/ohp2M0hPG3B89OYr6vF9bFrfIiCWJBLHgcnXkP9P1WwonJ2c//51 XKIIu36s6NdzF4oIuGKkjPxIMjGaK5aZfnAqlwWqnVmuhFiNGCzMedTU7ZRjP2NSqt8w JEOpANuinneiS1fYdV3K0TjMkeuCPBMc9DCoPYox/4Lp812J+9vbKQJndbflPdeM0vHX kpMJNSvcOOepdfEZ4cyCLp9zyScncvgNtpSE788m1zhCckPRO3An4UZdM0hnZcwaW2M0 hoSbv2BCs2mAciVXknzwAynj8LLHl3mfMBLZ1XZgwsprWd2h/R1kC9OVLRgHVuMtonsd wNFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=35p5Pzvij06883L5xcotqkJm3JL5Gnq7PcCbaLDkUh8=; b=ZbqBevg3VloFVrujeQ31ijYaoW75FeB/E2QtVexgvpdobN74uk4//MEW4HixXH9Mt1 JN5vIgYqm7PKOAYG3QlstHJgdL7HmUeGRk5YChJllf5/UNdh+IHFHDE+4M2NAD3VMNZN 9iJp2TGI/LmLxnUH+5VqE+wALIW7IOTRcjie7r/xFSBZWU4jSRHxt2TBf60mJR23HknD mkxVq/bKmPZQCHIbdEPfxOUOn4zzDch/C+xQf01vNnOWRC+3NefcSaxGHQRXSXJD4HhT xCbx3ESTnjJu9JBcV+MmP33MBzeDDKINClpw3eUafQuHA1cqnUbApwvM0eXId+9ly0bd FLiA== X-Gm-Message-State: ANoB5plmi034WwBbeayD4bxQOV1Pt4sGuEj4nFHhyh58f9LNvOPoiXep jkgoUcNSlCkSpEnAMIYJRLo= X-Received: by 2002:a65:4c85:0:b0:46f:59bd:6125 with SMTP id m5-20020a654c85000000b0046f59bd6125mr48407772pgt.147.1669690376990; Mon, 28 Nov 2022 18:52:56 -0800 (PST) Received: from localhost ([183.242.254.166]) by smtp.gmail.com with ESMTPSA id p21-20020a631e55000000b004597e92f99dsm7233338pgm.66.2022.11.28.18.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 18:52:55 -0800 (PST) From: Hawkins Jiawei To: yin31149@gmail.com, Jamal Hadi Salim , Cong Wang , Jiri Pirko , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: 18801353760@163.com, syzbot+232ebdbd36706c965ebf@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Cong Wang , Dmitry Vyukov , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] net: sched: fix memory leak in tcindex_set_parms Date: Tue, 29 Nov 2022 10:52:49 +0800 Message-Id: <20221129025249.463833-1-yin31149@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,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 Syzkaller reports a memory leak as follows: ==================================== BUG: memory leak unreferenced object 0xffff88810c287f00 (size 256): comm "syz-executor105", pid 3600, jiffies 4294943292 (age 12.990s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] kmalloc_trace+0x20/0x90 mm/slab_common.c:1046 [] kmalloc include/linux/slab.h:576 [inline] [] kmalloc_array include/linux/slab.h:627 [inline] [] kcalloc include/linux/slab.h:659 [inline] [] tcf_exts_init include/net/pkt_cls.h:250 [inline] [] tcindex_set_parms+0xa7/0xbe0 net/sched/cls_tcindex.c:342 [] tcindex_change+0xdf/0x120 net/sched/cls_tcindex.c:553 [] tc_new_tfilter+0x4f2/0x1100 net/sched/cls_api.c:2147 [] rtnetlink_rcv_msg+0x4dc/0x5d0 net/core/rtnetlink.c:6082 [] netlink_rcv_skb+0x87/0x1d0 net/netlink/af_netlink.c:2540 [] netlink_unicast_kernel net/netlink/af_netlink.c:1319 [inline] [] netlink_unicast+0x397/0x4c0 net/netlink/af_netlink.c:1345 [] netlink_sendmsg+0x396/0x710 net/netlink/af_netlink.c:1921 [] sock_sendmsg_nosec net/socket.c:714 [inline] [] sock_sendmsg+0x56/0x80 net/socket.c:734 [] ____sys_sendmsg+0x178/0x410 net/socket.c:2482 [] ___sys_sendmsg+0xa8/0x110 net/socket.c:2536 [] __sys_sendmmsg+0x105/0x330 net/socket.c:2622 [] __do_sys_sendmmsg net/socket.c:2651 [inline] [] __se_sys_sendmmsg net/socket.c:2648 [inline] [] __x64_sys_sendmmsg+0x24/0x30 net/socket.c:2648 [] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 [] entry_SYSCALL_64_after_hwframe+0x63/0xcd ==================================== Kernel uses tcindex_change() to change an existing filter properties. During the process of changing, kernel uses tcindex_alloc_perfect_hash() to newly allocate filter results, uses tcindex_filter_result_init() to clear the old filter result. Yet the problem is that, kernel clears the old filter result, without destroying its tcf_exts structure, which triggers the above memory leak. Considering that there already extis a tc_filter_wq workqueue to destroy the old tcindex_data by tcindex_partial_destroy_work() at the end of tcindex_set_parms(), this patch solves this memory leak bug by removing this old filter result clearing part, and delegating it to the tc_filter_wq workqueue. [Thanks to the suggestion from Jakub Kicinski, Cong Wang, Paolo Abeni and Dmitry Vyukov] Fixes: b9a24bb76bf6 ("net_sched: properly handle failure case of tcf_exts_init()") Link: https://lore.kernel.org/all/0000000000001de5c505ebc9ec59@google.com/ Reported-by: syzbot+232ebdbd36706c965ebf@syzkaller.appspotmail.com Tested-by: syzbot+232ebdbd36706c965ebf@syzkaller.appspotmail.com Cc: Cong Wang Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Dmitry Vyukov Signed-off-by: Hawkins Jiawei --- v3: - refactor the commit message - delegate the tcf_exts_destroy() to tc_filter_wq workqueue, suggested by Paolo Abeni and Dmitry Vyukov v2: https://lore.kernel.org/all/20221113170507.8205-1-yin31149@gmail.com/ v1: https://lore.kernel.org/all/20221031060835.11722-1-yin31149@gmail.com/ net/sched/cls_tcindex.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c index 1c9eeb98d826..3f4e7a6cdd96 100644 --- a/net/sched/cls_tcindex.c +++ b/net/sched/cls_tcindex.c @@ -478,14 +478,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, tcf_bind_filter(tp, &cr, base); } - if (old_r && old_r != r) { - err = tcindex_filter_result_init(old_r, cp, net); - if (err < 0) { - kfree(f); - goto errout_alloc; - } - } - oldp = p; r->res = cr; tcf_exts_change(&r->exts, &e); -- 2.34.1