Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3605257pxb; Tue, 19 Apr 2022 06:27:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyUJALNxgtFbo2g7yhImyNB2C2sSgCdWaaGsANiILL2grkLttBcgpzwWOKnz/83gMsNjXX X-Received: by 2002:a65:6753:0:b0:385:fa8a:188f with SMTP id c19-20020a656753000000b00385fa8a188fmr14751586pgu.499.1650374865564; Tue, 19 Apr 2022 06:27:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650374865; cv=none; d=google.com; s=arc-20160816; b=k65HqVKrlIR9jWCvlPBb7AMeLiboGekJNTsKeFCbBml7G9ixf/RPtxeHvdWpexMHqe 35jV5iKU2qHOVwoHIPNPNuLuHTtulkKtuGJ/UQJF8Q+rLJMtSuDEB2BLk5Aa9f2Y4M67 SCxyrcjXF3ufqbrTLPH3htc03w8fj8kdiGXEFk9taMa5iTts5nWoh9Rlz+e1wMKvcU8F aWe/kUq9EubzkzHjhx3z7cwyl7Q5ittGn7iPDvdmm4qYiXsbem0dThSSRvRhfKgxOeCr d9xUyC/qDfppKVCYvMo4fH/G17NXeeBuDSQ1oou4m3DWXlyiBM+slbV+HqQyir70loEn Rw3A== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QbRIg2rjadXfPpWMRNYZceDsVbPz/UUbvnI9tlM7sM8=; b=ARHxaSgkhE6z1zNZyxV4oZK0TCIdNF+YT7JjLGCCuECqmObp1hMEkPWlORiNmvn42K SE/+Qj6l1BAyq1CCF0TlrfeVp2SezNMIFhONQMUfhYP6QDMVJK5vHFurohglFFc3IzEF sZJZ+/Ohj0d8t7FMxKx8yiHKoqYMs9+TKlprZsWNR7HEa9a/CZXKhIZpcVcgQ0faD133 TpB6APczJrSUd2miW7bQ/iyHILGpdq3f6x0itIhZWYY7Bkr3lcHxg0OPie0moCfK/p8q QL8b98jM57fJrOk4wOXqFbvDvd+n33j24h05mYlLLh7KTGJgat+JJWnx2P5r2ZeueMh5 wBBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=obCwdtsE; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a7-20020a056a000c8700b0050a92f7d228si2365282pfv.143.2022.04.19.06.27.28; Tue, 19 Apr 2022 06:27:45 -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=@linuxfoundation.org header.s=korg header.b=obCwdtsE; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243118AbiDRNTo (ORCPT + 99 others); Mon, 18 Apr 2022 09:19:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238720AbiDRNAd (ORCPT ); Mon, 18 Apr 2022 09:00:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B37153191A; Mon, 18 Apr 2022 05:41:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3EC46B80EC3; Mon, 18 Apr 2022 12:41:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F6CCC385A1; Mon, 18 Apr 2022 12:41:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650285716; bh=ssH+O5U6Zc+kQBJr13MbIlhhEg3a9HKg/eM3zo7oJFg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=obCwdtsEz7pC+NOg0TF/9fSK4ckWG0NYXIeh2vSDsiyzEo2HVZWU1v0+2DG6fFfgh r0iw1VoUI+YxvckvtufgFlZJf72w/k/81xzr2afODjDrZ8/ume5icM8OSTrJjGGb4h zynOmeH0q/byEctpW4nCURVB4YA7nZmrve/TNGmE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Duoming Zhou , "David S. Miller" , Ovidiu Panait Subject: [PATCH 5.10 100/105] ax25: fix UAF bugs of net_device caused by rebinding operation Date: Mon, 18 Apr 2022 14:13:42 +0200 Message-Id: <20220418121149.477791070@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121145.140991388@linuxfoundation.org> References: <20220418121145.140991388@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Duoming Zhou commit feef318c855a361a1eccd880f33e88c460eb63b4 upstream. The ax25_kill_by_device() will set s->ax25_dev = NULL and call ax25_disconnect() to change states of ax25_cb and sock, if we call ax25_bind() before ax25_kill_by_device(). However, if we call ax25_bind() again between the window of ax25_kill_by_device() and ax25_dev_device_down(), the values and states changed by ax25_kill_by_device() will be reassigned. Finally, ax25_dev_device_down() will deallocate net_device. If we dereference net_device in syscall functions such as ax25_release(), ax25_sendmsg(), ax25_getsockopt(), ax25_getname() and ax25_info_show(), a UAF bug will occur. One of the possible race conditions is shown below: (USE) | (FREE) ax25_bind() | | ax25_kill_by_device() ax25_bind() | ax25_connect() | ... | ax25_dev_device_down() | ... | dev_put_track(dev, ...) //FREE ax25_release() | ... ax25_send_control() | alloc_skb() //USE | the corresponding fail log is shown below: =============================================================== BUG: KASAN: use-after-free in ax25_send_control+0x43/0x210 ... Call Trace: ... ax25_send_control+0x43/0x210 ax25_release+0x2db/0x3b0 __sock_release+0x6d/0x120 sock_close+0xf/0x20 __fput+0x11f/0x420 ... Allocated by task 1283: ... __kasan_kmalloc+0x81/0xa0 alloc_netdev_mqs+0x5a/0x680 mkiss_open+0x6c/0x380 tty_ldisc_open+0x55/0x90 ... Freed by task 1969: ... kfree+0xa3/0x2c0 device_release+0x54/0xe0 kobject_put+0xa5/0x120 tty_ldisc_kill+0x3e/0x80 ... In order to fix these UAF bugs caused by rebinding operation, this patch adds dev_hold_track() into ax25_bind() and corresponding dev_put_track() into ax25_kill_by_device(). Signed-off-by: Duoming Zhou Signed-off-by: David S. Miller [OP: backport to 5.10: adjust dev_put_track()->dev_put() and dev_hold_track()->dev_hold()] Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman --- net/ax25/af_ax25.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/net/ax25/af_ax25.c +++ b/net/ax25/af_ax25.c @@ -98,6 +98,7 @@ again: spin_unlock_bh(&ax25_list_lock); lock_sock(sk); s->ax25_dev = NULL; + dev_put(ax25_dev->dev); ax25_dev_put(ax25_dev); release_sock(sk); ax25_disconnect(s, ENETUNREACH); @@ -1122,8 +1123,10 @@ static int ax25_bind(struct socket *sock } } - if (ax25_dev != NULL) + if (ax25_dev) { ax25_fillin_cb(ax25, ax25_dev); + dev_hold(ax25_dev->dev); + } done: ax25_cb_add(ax25);