Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp4067253pxp; Tue, 15 Mar 2022 11:47:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxC9K7lUanwwRKVAqakI8+H0OWto076T2ih0RZ7HVT4m7IHK8icOMQKcr4lHFHHwDx9xSI0 X-Received: by 2002:a17:90a:9106:b0:1b9:115a:a2c1 with SMTP id k6-20020a17090a910600b001b9115aa2c1mr6259251pjo.80.1647370066174; Tue, 15 Mar 2022 11:47:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647370066; cv=none; d=google.com; s=arc-20160816; b=n2ef+WeCQzc3Yh1chiECg16wVqR2lHxUSOOg/MF9cCvRPAaedszYIOV8qqogHAi51p CDrd+GtJSzyZLs876p2sR1Y4sacSSJjUQWv32FCU+UP0jGzs5t3ykIkQnACAzmE5mitX 5eATsia6MjYciO0i1DM/3Y9TSg47fqk7pD/FcDg1vuTLJ7g2+bNrw8+6kHb+cgYFC9kj Dxi2mgzVXiRYODPc+ibl2EJY/NrpPoc2vwOTAlk0K5N6FzTYD658jjLysGVopDucJ1QV Jn/RRG0gNakPhS4yQ4RMnfBMpbRH68rchFJL8TRixA5pSfyg+vgvoxejELR1J00IYzW9 Dhkg== 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=8IPxGhA8b3fzSUGu+SkgFo7OErBWdHDHzy3XCTGSW8I=; b=H4QjAyf0NixoYxa7WyULKLn+V7066fnCH33PG3bDeYQdl9cbmvN7xuWvXx0TF6ya0c mn2E0IuLCFf5k0bdIbs08TDNKXiHWHOSA2c9WH6yFYx2qfZ0DinPg6uUdKlSCo7Kz1FP wpOzjMRx10HLYlY1x5f0UM/zh3ZGGAWt1zE2oDh2kmhVZC2qjBwzOugbgnO2FshRpCjx Cu+5ddeQnN04ZPywBb+g8kLte3mMzrhCconRQu9zzubaPLZMZwM2ygukzk0xcs3JkGdh 2dl23DMtz5D4W/BM64rQm45yC/EyXR6bioe9Gi5AnbMkvOytUmFDAhyDhLRsiwTMXeHK fdDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fkvkhLru; 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 r32-20020a63fc60000000b00364f31fb850si18032959pgk.843.2022.03.15.11.47.29; Tue, 15 Mar 2022 11:47:46 -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=fkvkhLru; 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 S245080AbiCNMco (ORCPT + 99 others); Mon, 14 Mar 2022 08:32:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243927AbiCNMVY (ORCPT ); Mon, 14 Mar 2022 08:21:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 885F013CE3; Mon, 14 Mar 2022 05:18:30 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 1D20460DBB; Mon, 14 Mar 2022 12:18:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E5E9C340E9; Mon, 14 Mar 2022 12:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647260309; bh=q+lhG1LbNNKewQbXq+2r/4a0Q5Up4I/iuuciJ9Y6sGM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fkvkhLruCmAGlcmSZHYckWMyCt8KkM1oCvo46Fos0ltRUSDJ7WnNq33jFgMJG3yDO Xr8KHp5ay/nZmr4mZ2OcxFm5z72QbBPiiZdLl+XWxDizGUzGDSZ3Xi/+hM61ZcvNyH JXzGX5sKzjEoqPUaCu+CTEgcLxad9HcaUzwdzmHY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Duoming Zhou , Lin Ma , "David S. Miller" , Sasha Levin Subject: [PATCH 5.16 071/121] drivers: hamradio: 6pack: fix UAF bug caused by mod_timer() Date: Mon, 14 Mar 2022 12:54:14 +0100 Message-Id: <20220314112746.104980888@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112744.120491875@linuxfoundation.org> References: <20220314112744.120491875@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=-8.6 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 [ Upstream commit efe4186e6a1b54bf38b9e05450d43b0da1fd7739 ] When a 6pack device is detaching, the sixpack_close() will act to cleanup necessary resources. Although del_timer_sync() in sixpack_close() won't return if there is an active timer, one could use mod_timer() in sp_xmit_on_air() to wake up timer again by calling userspace syscall such as ax25_sendmsg(), ax25_connect() and ax25_ioctl(). This unexpected waked handler, sp_xmit_on_air(), realizes nothing about the undergoing cleanup and may still call pty_write() to use driver layer resources that have already been released. One of the possible race conditions is shown below: (USE) | (FREE) ax25_sendmsg() | ax25_queue_xmit() | ... | sp_xmit() | sp_encaps() | sixpack_close() sp_xmit_on_air() | del_timer_sync(&sp->tx_t) mod_timer(&sp->tx_t,...) | ... | unregister_netdev() | ... (wait a while) | tty_release() | tty_release_struct() | release_tty() sp_xmit_on_air() | tty_kref_put(tty_struct) //FREE pty_write(tty_struct) //USE | ... The corresponding fail log is shown below: =============================================================== BUG: KASAN: use-after-free in __run_timers.part.0+0x170/0x470 Write of size 8 at addr ffff88800a652ab8 by task swapper/2/0 ... Call Trace: ... queue_work_on+0x3f/0x50 pty_write+0xcd/0xe0pty_write+0xcd/0xe0 sp_xmit_on_air+0xb2/0x1f0 call_timer_fn+0x28/0x150 __run_timers.part.0+0x3c2/0x470 run_timer_softirq+0x3b/0x80 __do_softirq+0xf1/0x380 ... This patch reorders the del_timer_sync() after the unregister_netdev() to avoid UAF bugs. Because the unregister_netdev() is well synchronized, it flushs out any pending queues, waits the refcount of net_device decreases to zero and removes net_device from kernel. There is not any running routines after executing unregister_netdev(). Therefore, we could not arouse timer from userspace again. Signed-off-by: Duoming Zhou Reviewed-by: Lin Ma Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/hamradio/6pack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/hamradio/6pack.c b/drivers/net/hamradio/6pack.c index 8a19a06b505d..ff2bb3d80fac 100644 --- a/drivers/net/hamradio/6pack.c +++ b/drivers/net/hamradio/6pack.c @@ -668,11 +668,11 @@ static void sixpack_close(struct tty_struct *tty) */ netif_stop_queue(sp->dev); + unregister_netdev(sp->dev); + del_timer_sync(&sp->tx_t); del_timer_sync(&sp->resync_t); - unregister_netdev(sp->dev); - /* Free all 6pack frame buffers after unreg. */ kfree(sp->rbuff); kfree(sp->xbuff); -- 2.34.1