Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp3047619pxb; Sun, 26 Sep 2021 03:55:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwLh4Eqky+/msjm8FBMnfIgWyJ2p7shZpAniOkGiJbcFm+mDUAv0/dVUhh0G0Cok4ZLaX0g X-Received: by 2002:a92:4a10:: with SMTP id m16mr15647881ilf.91.1632653706249; Sun, 26 Sep 2021 03:55:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632653706; cv=none; d=google.com; s=arc-20160816; b=1Fq6aF05vmKcOfiQWiK133ONf/yCkL6fAvyXLvA1aTtyT+nYiwq8N47dBX/QGtdXY5 qy9HHK7qmzTbMeurCBQhdIT0fF4XdznXDXftPgyG0T8WPKSPTICaqAIVXRXSKMl10YXW FZuOgL7Sp7X/4Vc9uiu8ucRFTQCubi9vQb+1T3wTj+mvatuMfbuoZ0+1Fq7tGpfiu16y gxq7ED3dWZKUs/ZbYeCx+b/tLFzQrVW/eyRJZsZnxlKLYEVPP9ASKOqiJGTWMerepLiy aHJR1azODyb7D4DuOorwJdogiSwKKJGxJ3gUo5b0nZMVTNTIyIvsem/t8bBv4tXKnSIT qHTQ== 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; bh=dGlid76eFBDp075Pm7sweRuQ5et2Pv0ffef8ENB4VdA=; b=nN2WJuSkENz+M5wVbVhxmh9Wzk1azNG7bRYa9tAvg/6La10odLab7YhlpRB6h2gswy AxtWfAy6Bdk7S0XCG59AB3nuHWFjzXvPClit/qkHp63cCUf0HXG5p4iNrhV0OOxIjxIl MEgwyUhn+RabEkM76/jI+WSMb3dlnWPFKSIbcwPiBVe1nZJSeERd4nX7IJ0C0+51FQNY IbHrcoRGwA5zisjW49d7yi8LtSARYULwtDJ1MsnrXbGpLbXuKdbuWyDNsIkRmSiNylhp rL7xzEf81JQkOs7p0jC/sFdohZPen4dxhItZNjC5tEhu4Utjqav8bGh1B+d0baqaVpm9 0sxw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s18si15569438ilj.28.2021.09.26.03.54.41; Sun, 26 Sep 2021 03:55:06 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230169AbhIZKuj (ORCPT + 99 others); Sun, 26 Sep 2021 06:50:39 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:23953 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230025AbhIZKuf (ORCPT ); Sun, 26 Sep 2021 06:50:35 -0400 Received: from dggeml757-chm.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HHMpf11mPzbmp8; Sun, 26 Sep 2021 18:44:42 +0800 (CST) Received: from localhost.localdomain (10.175.104.82) by dggeml757-chm.china.huawei.com (10.1.199.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Sun, 26 Sep 2021 18:48:56 +0800 From: Ziyang Xuan To: CC: , , , , , , , Subject: [PATCH net] can: j1939: fix UAF for rx_kref of j1939_priv Date: Sun, 26 Sep 2021 18:47:57 +0800 Message-ID: <20210926104757.2021540-1-william.xuanziyang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggeml757-chm.china.huawei.com (10.1.199.137) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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