Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2865967pxb; Mon, 18 Apr 2022 09:52:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9eQ/kdTbRZcdI+nRrUPHTtbTtJbkcEL9eqEL5iHkr4zf5spw+jHER1eFAcfL2DnILZTsh X-Received: by 2002:a17:906:4787:b0:6e8:9252:5bef with SMTP id cw7-20020a170906478700b006e892525befmr9999950ejc.679.1650300760548; Mon, 18 Apr 2022 09:52:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650300760; cv=none; d=google.com; s=arc-20160816; b=mtgyeWpIAhOrxilJCU3aFulJPBNBpcisi8jpnYNnfXdVC56GzoqL39T682AkKXqg83 WHXn4eCrh5ctqQSw06Dr8YC6wQLq8cAmluikKjC3E6ieb2Om1384ddfJP4thcIaHM4GJ Tpah7SDxTknImuiLk4iz3t2VCb0ZWbX4ueWivDchRLdqZT3zJOIl79DLgfPZB/ikrtX4 YsMA2PcLUlYsUTLY0ydHBRbHKU8PCCLfMpMVUbExQuRzP6lcVFBa+j/HDCDiFXJE9ov+ k8BjkE/wdw1RZiD5emIAJs+AmiT8wOFzpB6i6iySrfQAsjzzxOcNKP0BjGoeKZXgms0b zr5g== 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=RTDcVPlJix+SBKd0yUVX5hrb1umhCmzMrAVcuy3GWTY=; b=m5xCTKoSYqOsXgJBI5VjWTHCIOYGHimzDwep7hDg/yngO+W8ujFBNZPH5KhjWgJjnC Y79HzKQA30vxYMDGdP3iX+Fizfq+GglbHk/JDJYHIHtIfn7Mhb97JymtkY2nosw9giuu SRPx9xaRjjEY342K9UTZnRQA8QL7ANHF/+FLdbcj592pPL86onvvITcOd0MAW3tdSjPf bipc8zYx15biVDohppgc+tBYM0sui4tbr56fbaZejAsvUa1B/16hH9sTNhFSav2ilccr jzQEHafSA0WEL9txU/yVQxPesyyJsLQ+ZV9w2yO4SH34YZan+8k7WTnqwsam4kTis1Gd saow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xx0w9ZDL; 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 g27-20020a1709063b1b00b006e87fcf4cc5si6027181ejf.734.2022.04.18.09.52.16; Mon, 18 Apr 2022 09:52:40 -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=Xx0w9ZDL; 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 S242863AbiDRNgN (ORCPT + 99 others); Mon, 18 Apr 2022 09:36:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243422AbiDRNKH (ORCPT ); Mon, 18 Apr 2022 09:10:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4745737BDA; Mon, 18 Apr 2022 05:49:36 -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 ECB9FB80EDB; Mon, 18 Apr 2022 12:49:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23B99C385A1; Mon, 18 Apr 2022 12:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286153; bh=FjNegvRkVwiBOy2n8U5PjkNWCM1IxJIfAj3VatT+cxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xx0w9ZDLyasvlHQI+qyvc5U1og+0CSQQJhnQu7rb4FIMyEEbY8rtktXa17Dbp6yCe RQ5LQYVxzyNwarxv9La2ULjlPGdTi3WcTIsa5Yfet7iIvTranIJZN13NQ/P+uxPma9 dzQgZb5k8eIqH6ZJdFHeGvsywMl7z1lvez8fppIU= 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" Subject: [PATCH 4.14 038/284] drivers: hamradio: 6pack: fix UAF bug caused by mod_timer() Date: Mon, 18 Apr 2022 14:10:19 +0200 Message-Id: <20220418121211.778389648@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@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 efe4186e6a1b54bf38b9e05450d43b0da1fd7739 upstream. 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: Greg Kroah-Hartman --- drivers/net/hamradio/6pack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/drivers/net/hamradio/6pack.c +++ b/drivers/net/hamradio/6pack.c @@ -685,14 +685,14 @@ static void sixpack_close(struct tty_str */ netif_stop_queue(sp->dev); + unregister_netdev(sp->dev); + del_timer_sync(&sp->tx_t); del_timer_sync(&sp->resync_t); /* Free all 6pack frame buffers. */ kfree(sp->rbuff); kfree(sp->xbuff); - - unregister_netdev(sp->dev); } /* Perform I/O control on an active 6pack channel. */