Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp594615rwd; Sat, 27 May 2023 03:22:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7nOplKOd/QLsOCf+0TollmEZAn5Zrqmna60WUk9Rcy7S9SdUmQVV+JSFvI8/p19eU+7okj X-Received: by 2002:a17:902:d50d:b0:1ad:cef8:f913 with SMTP id b13-20020a170902d50d00b001adcef8f913mr6796505plg.44.1685182973909; Sat, 27 May 2023 03:22:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685182973; cv=none; d=google.com; s=arc-20160816; b=yGp7MLPRTDTcY2H2jhJtKtr2ClRfwCjAKrutCgL8ENCIConBJ5uAAbNd8a7963dL1l 8xljUsBTtS04AWqwTmJXAfFuiYJ1f2ErN6hCRI7iYV+7RRTy7kqEBaUVdMosUsHu7qDv ZFbRAZJKJNUdLPhQGvsDahhKqMishyJUL6alOvGJcTB1uY6xF28cEStuqxu5dyX4w9z3 jqUeTxBuCjQIbnxaQhra8naWFuEzUT1aSboJroJEYiYS4g8RLbs4j47UYoFdix7h1Nq0 qhU1OCEHBOhukV25tySUYEohTkiYrzXBs44MMvxb5L+0lWIDHuFied4N6RPQ3NfYgr3Z bgHQ== 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 :dkim-filter; bh=OUAbxv5dy46DdMVoI/PW+9M3g+qUfLsq4S2IhDgEP04=; b=YSSaNDguXvNNhKkCMOZG2nmcK4m82xV5e6fWZ3unsbwLHCW8xLx07+WCISLS4Ufi95 ackZZ04xlgG3lAf0OEqt1mmiYoTKyV2zsGgTlLNUqz6YNXLQg3SshL0MgRtfEWFaZMmG Q04YycixTN70LCG7VjV2RhPrdSZnyYaN76ds8DZA3N9FCpEBASCqkifgb0qrYr5uODZO DTdMh2/yOOKNoKIrDx6qW/j7/uX0+uyOCcJUNtHbMNnS6KqsjPJB8ooakl0EB0ORSqwr 3aCXYlUPvHf/o+qR4FDK7wHXysoJAethCoccGEdNbxLH0IhpxgvaMR4B3iPKIE68bw3W L1Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=cMn5ILoR; 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=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g2-20020a170902740200b001a92a507187si5761985pll.80.2023.05.27.03.22.41; Sat, 27 May 2023 03:22:53 -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=@ispras.ru header.s=default header.b=cMn5ILoR; 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=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232077AbjE0KFy (ORCPT + 99 others); Sat, 27 May 2023 06:05:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231620AbjE0KFx (ORCPT ); Sat, 27 May 2023 06:05:53 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E91DDF7; Sat, 27 May 2023 03:05:51 -0700 (PDT) Received: from fpc (unknown [10.10.165.8]) by mail.ispras.ru (Postfix) with ESMTPSA id 2D90344C101E; Sat, 27 May 2023 10:05:48 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 2D90344C101E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1685181948; bh=OUAbxv5dy46DdMVoI/PW+9M3g+qUfLsq4S2IhDgEP04=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=cMn5ILoRw60U0Z4pA7QcgcYL7c7jCLUS76ujzSzXzOQHxT4YO3Wo391AyDlEG89P7 0KOts/v9GGj1XCi5coAeKRgDDt8nfxY6f+ykeo7sOezEQRolgSOiLWi+pdSUDPsgQi TECqMrQYUsIZkXEwSqV7eKrtbW22IsQpOifKWHOc= Date: Sat, 27 May 2023 13:05:44 +0300 From: Fedor Pchelkin To: Oleksij Rempel Cc: Marc Kleine-Budde , kernel@pengutronix.de, Robin van der Gracht , Oliver Hartkopp , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kurt Van Dijck , linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Khoroshilov , lvc-project@linuxtesting.org Subject: Re: [PATCH 2/2] can: j1939: avoid possible use-after-free when j1939_can_rx_register fails Message-ID: <20230527100544.cf4nyq33e24atmv4@fpc> References: <20230526171910.227615-1-pchelkin@ispras.ru> <20230526171910.227615-3-pchelkin@ispras.ru> <20230526181500.GA26860@pengutronix.de> <20230526185026.33pcjvoyq5jzlnxk@fpc> <20230527055704.GA17237@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230527055704.GA17237@pengutronix.de> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 Sat, May 27, 2023 at 07:57:04AM +0200, Oleksij Rempel wrote: > Hi Fedor, > > On Fri, May 26, 2023 at 09:50:26PM +0300, Fedor Pchelkin wrote: > > Hi Oleksij, > > > > thanks for the reply! > > > > On Fri, May 26, 2023 at 08:15:00PM +0200, Oleksij Rempel wrote: > > > Hi Fedor, > > > > > > On Fri, May 26, 2023 at 08:19:10PM +0300, Fedor Pchelkin wrote: > > > > > > > > > Thank you for your investigation. How about this change? > > > --- a/net/can/j1939/main.c > > > +++ b/net/can/j1939/main.c > > > @@ -285,8 +285,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) > > > */ > > > kref_get(&priv_new->rx_kref); > > > spin_unlock(&j1939_netdev_lock); > > > - dev_put(ndev); > > > - kfree(priv); > > > + j1939_priv_put(priv); > > > > I don't think that's good because the priv which is directly freed here is > > still local to the thread, and parallel threads don't have any access to > > it. j1939_priv_create() has allocated a fresh priv and called dev_hold() > > so dev_put() and kfree() here are okay. > > > > > return priv_new; > > > } > > > j1939_priv_set(ndev, priv); > > > @@ -300,8 +299,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) > > > > > > out_priv_put: > > > j1939_priv_set(ndev, NULL); > > > - dev_put(ndev); > > > - kfree(priv); > > > + j1939_priv_put(priv); > > > > > > return ERR_PTR(ret); > > > } > > > > > > If I see it correctly, the problem is kfree() which is called without respecting > > > the ref counting. If CPU1 has priv_new, refcounting is increased. The priv will > > > not be freed on this place. > > > > With your suggestion, I think it doesn't work correctly if > > j1939_can_rx_register() fails and we go to out_priv_put. The priv is kept > > but the parallel thread which may have already grabbed it thinks that > > j1939_can_rx_register() has succeeded when actually it hasn't succeed. > > Moreover, j1939_priv_set() makes it NULL on error path so that priv cannot > > be accessed from ndev. > > > > I also considered the alternatives where we don't have to serialize access > > to j1939_can_rx_register() and subsequently introduce mutex. But with > > current j1939_netdev_start() implementation I can't see how to fix the > > racy bug without it. > > Ok, it make sense. > > I'll try to do some testing next week. If i'll forget it, please feel > free to ping me. Got it, thank you. > > Regards, > Oleksij > -- > 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 |