Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2279170pxb; Fri, 8 Oct 2021 04:50:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhuaRytZxbA4uwaNLL0IN5DT6wM0b17c7Qtw6pVl8xeiRB27F2J1/wFYSXdZYK0BWo/W6m X-Received: by 2002:a17:906:d96a:: with SMTP id rp10mr3657702ejb.497.1633693815175; Fri, 08 Oct 2021 04:50:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633693815; cv=none; d=google.com; s=arc-20160816; b=HiDpfPjTbQZHrKmqZjZZWogIgcAUtWwl8xWYQ3PiGtnhExSDWNdXaSTvjhKnsgPQrS vLh9Th92xyV7RFZdouQeVKMY8IMEIqt5aa/Zf8vjU7alv+QF/5j1h7SGEoac1AQPsKO0 M0Ry17SiawylrAcoFHDYNAtf65rFePChVzFQxVBHxQrcJW9Fr509NeCskTU8TV0f5FDR gq2C9x80uFqzisEdvCCMkDShPPekGV/R6zjj6ULux2hgGv03E6XttTmIzh81K40KYMja X7Ot3i1Tj7A4I3zm446ue0zgLblNNcOoI1qXr3MKDiMC/wYO8tsER9t8gul7sGarTSoB 5HyA== 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=4n1OQe4qLIcwZ947ZDqfQlC709bHgYEiQhOYNBx1oLs=; b=xP6vO26MKqQg020bBOkGEugvq8EI26FlRNjz4XmHaUCGfTm1ZQL2jkS3z/3Y3iUcgG qYLWwdO9KLHxWd92JsqfLN268VbgW5dKkh+T/HSGh5Iu91j+tR7FmN0q472J8SyNBnBC Dew64MRuZw00T2fzhkDmxxHzQf/pYiYGFtjJGoBW+gdmPePoENvIvnVt60hWtfgPM6Zj NfLohF97jV/m0Fin2hsbDGhMBoHOBzAkzTSkBnVS1b7luNRhPSQDBRJ62xreafNMnirA 63MTrwkP3VtBHDbgAeCHm/fZsag6jHLIYOORRR3AtP71Ng28JZvfIHZi6B71vA6Jt206 mMrQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l4si4665684ejo.650.2021.10.08.04.49.51; Fri, 08 Oct 2021 04:50:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243078AbhJHLtf (ORCPT + 99 others); Fri, 8 Oct 2021 07:49:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243517AbhJHLtG (ORCPT ); Fri, 8 Oct 2021 07:49:06 -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 E34C1C02B841 for ; Fri, 8 Oct 2021 04:34:27 -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 1mYo8o-0007LY-9v; Fri, 08 Oct 2021 13:34:18 +0200 Received: from ore by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1mYo8l-0005QK-Ta; Fri, 08 Oct 2021 13:34:15 +0200 Date: Fri, 8 Oct 2021 13:34:15 +0200 From: Oleksij Rempel To: Ziyang Xuan Cc: robin@protonic.nl, linux@rempel-privat.de, socketcan@hartkopp.net, mkl@pengutronix.de, davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net] can: j1939: fix UAF for rx_kref of j1939_priv Message-ID: <20211008113415.GG29653@pengutronix.de> References: <20210926104757.2021540-1-william.xuanziyang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210926104757.2021540-1-william.xuanziyang@huawei.com> X-Sent-From: Pengutronix Hildesheim X-URL: http://www.pengutronix.de/ X-IRC: #ptxdist @freenode X-Accept-Language: de,en X-Accept-Content-Type: text/plain X-Uptime: 13:32:28 up 232 days, 14:56, 143 users, load average: 0.08, 0.14, 0.16 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Sun, Sep 26, 2021 at 06:47:57PM +0800, Ziyang Xuan wrote: > It will trigger UAF for rx_kref of j1939_priv as following. > > cpu0 cpu1 > j1939_sk_bind(socket0, ndev0, ...) > j1939_netdev_start > j1939_sk_bind(socket1, ndev0, ...) > j1939_netdev_start > j1939_priv_set > j1939_priv_get_by_ndev_locked > j1939_jsk_add > ..... > j1939_netdev_stop > kref_put_lock(&priv->rx_kref, ...) > kref_get(&priv->rx_kref, ...) > REFCOUNT_WARN("addition on 0;...") > > ==================================================== > refcount_t: addition on 0; use-after-free. > WARNING: CPU: 1 PID: 20874 at lib/refcount.c:25 refcount_warn_saturate+0x169/0x1e0 > RIP: 0010:refcount_warn_saturate+0x169/0x1e0 > Call Trace: > j1939_netdev_start+0x68b/0x920 > j1939_sk_bind+0x426/0xeb0 > ? security_socket_bind+0x83/0xb0 > > The rx_kref's kref_get() and kref_put() should use j1939_netdev_lock to > protect. > > Fixes: 9d71dd0c70099 ("can: add support of SAE J1939 protocol") > Reported-by: syzbot+85d9878b19c94f9019ad@syzkaller.appspotmail.com > Signed-off-by: Ziyang Xuan Thank you! Acked-by: Oleksij Rempel > --- > net/can/j1939/main.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c > index 08c8606cfd9c..9bc55ecb37f9 100644 > --- a/net/can/j1939/main.c > +++ b/net/can/j1939/main.c > @@ -249,11 +249,14 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) > struct j1939_priv *priv, *priv_new; > int ret; > > - priv = j1939_priv_get_by_ndev(ndev); > + spin_lock(&j1939_netdev_lock); > + priv = j1939_priv_get_by_ndev_locked(ndev); > if (priv) { > kref_get(&priv->rx_kref); > + spin_unlock(&j1939_netdev_lock); > return priv; > } > + spin_unlock(&j1939_netdev_lock); > > priv = j1939_priv_create(ndev); > if (!priv) > @@ -269,10 +272,10 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) > /* Someone was faster than us, use their priv and roll > * back our's. > */ > + kref_get(&priv_new->rx_kref); > spin_unlock(&j1939_netdev_lock); > dev_put(ndev); > kfree(priv); > - kref_get(&priv_new->rx_kref); > return priv_new; > } > j1939_priv_set(ndev, priv); > -- > 2.25.1 > > -- 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 |