Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp36716imn; Thu, 28 Jul 2022 21:26:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u8Tl3N5ui/t6MZFHIohClkdm1/1ZOWqz2UnRNJPUyhdeHzkqYDRSmKcvydnmhC2QfMYd8e X-Received: by 2002:a05:6402:b84:b0:43c:1ab7:9544 with SMTP id cf4-20020a0564020b8400b0043c1ab79544mr1807595edb.53.1659068819526; Thu, 28 Jul 2022 21:26:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659068819; cv=none; d=google.com; s=arc-20160816; b=qf6cnKZB/ULIf8Wbo2PRfLmirYHBxwebk4I5gNIezOGhsVp2jZ3LHsTcIi+Nj53vx8 3LNN6Qkl6tShfvrTy4+0wA1a503C8SgN6lWhRSKSXai7WW/BuDflrzasgV7oLrQiqvff aZYhsYlyIr6iNZXYxkgamLkeLzc3rjKxJhuJRU/PwEcpZ9XMzis4aLi3jeOPtvJEroEz w3Tc7lXvDLv3jVQs4CifJ+grxHxVTes4hhc3ovH/DdIBfewTYaIqio713mH0ZOK/PAth Hd2FkNo+Ehf5rthilxviDJBF4D+XxuWteCiS3ToQgun/tdmeqFJA3cthVRtP8pfu1e4A Jyww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=AZ60qKuT5a/7sHrJ+bYYeIs9sYIXGTIGO+9ScjR0i7A=; b=m2zGxXbHFQivbo8RL6KAWUH3BxykVRZAiYuE6U6xzIuCjDnWDmNZTQf/xa7Ye5M4kP yVCHV/TmGc7lq9lsVkX31lwr+5nEy6Jo7wzl2PQgy30NP9ULAl7+D8/JU8BiWcUgxkR6 0ZMXhx07iQvCLNT1lzxMSQHJvSJyQhBOyK9Vd5QkILQWo2guQqvZGFcaomht/rCPspA3 sGiQFnsEHs9TEY/DxaTpoh8MPJ9nGrzMw5wPpzP3GUPHd0sCDmKhWAXgBYhYa8uifvf6 wiRo/NKMoBh62XyEkpY8bdENdEBwOuWKOADIV544p4XZgb80/cg/Rij8yyfRXlLdxGxQ nh1Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w9-20020a05640234c900b0043bdd032e2esi2583024edc.473.2022.07.28.21.26.35; Thu, 28 Jul 2022 21:26:59 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233832AbiG2EXH (ORCPT + 99 others); Fri, 29 Jul 2022 00:23:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232910AbiG2EXE (ORCPT ); Fri, 29 Jul 2022 00:23:04 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19EE677A51 for ; Thu, 28 Jul 2022 21:23:03 -0700 (PDT) Received: from ptx.hi.pengutronix.de ([2001:67c:670:100:1d::c0]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oHHWS-0001Up-I9; Fri, 29 Jul 2022 06:22:48 +0200 Received: from ore by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1oHHWP-0002bH-0o; Fri, 29 Jul 2022 06:22:45 +0200 Date: Fri, 29 Jul 2022 06:22:44 +0200 From: Oleksij Rempel To: Fedor Pchelkin Cc: Robin van der Gracht , Oleksij Rempel , kernel@pengutronix.de, Oliver Hartkopp , Marc Kleine-Budde , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, ldv-project@linuxtesting.org, Alexey Khoroshilov Subject: Re: [PATCH] can: j1939: fix memory leak of skbs Message-ID: <20220729042244.GC30201@pengutronix.de> References: <20220708175949.539064-1-pchelkin@ispras.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220708175949.539064-1-pchelkin@ispras.ru> X-Sent-From: Pengutronix Hildesheim X-URL: http://www.pengutronix.de/ X-Accept-Language: de,en X-Accept-Content-Type: text/plain User-Agent: Mutt/1.10.1 (2018-07-13) X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c0 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 Hi Fedor, thank you for work. On Fri, Jul 08, 2022 at 08:59:49PM +0300, Fedor Pchelkin wrote: > Syzkaller reported memory leak of skbs introduced with the commit > 2030043e616c ("can: j1939: fix Use-after-Free, hold skb ref while in use"). > > Link to Syzkaller info and repro: https://forge.ispras.ru/issues/11743 > > The suggested solution was tested on the new memory-leak Syzkaller repro > and on the old use-after-free repro (that use-after-free bug was solved > with aforementioned commit). Although there can probably be another > situations when the numbers of skb_get() and skb_unref() calls don't match > and I don't see it in right way. > > Moreover, skb_unref() call can be harmlessly removed from line 338 in > j1939_session_skb_drop_old() (/net/can/j1939/transport.c). But then I > assume this removal ruins the whole reference counts logic... > > Overall, there is definitely something not clear in skb reference counts > management with skb_get() and skb_unref(). The solution we suggested fixes > the leaks and use-after-free's induced by Syzkaller but perhaps the origin > of the problem can be somewhere else. > > Found by Linux Verification Center (linuxtesting.org) with Syzkaller. > Signed-off-by: Fedor Pchelkin > Signed-off-by: Alexey Khoroshilov > --- > net/can/j1939/transport.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c > index 307ee1174a6e..9600b339cbf8 100644 > --- a/net/can/j1939/transport.c > +++ b/net/can/j1939/transport.c > @@ -356,7 +356,6 @@ void j1939_session_skb_queue(struct j1939_session *session, > > skcb->flags |= J1939_ECU_LOCAL_SRC; > > - skb_get(skb); > skb_queue_tail(&session->skb_queue, skb); > } This skb_get() is counter part of skb_unref() j1939_session_skb_drop_old(). Initial issue can be reproduced by using real (slow) CAN with j1939cat[1] tool. Both parts should be started to make sure the j1939_session_tx_dat() will actually start using the queue. After pushing about 100K of data, application will try to close the socket and exit. After socket is closed, all skb related to this socket will be freed and j1939_session_tx_dat() will use freed skbs. NACK for this patch. 1. https://github.com/linux-can/can-utils/blob/master/j1939cat.c -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |