Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp670292pxb; Tue, 5 Apr 2022 18:06:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzW/8tYkmb0MrSjw0Q/YFpbTEnh7NCRZiqVo9oHEcdiup7yoc7MOUUJDe3afnQIFkiVHEB+ X-Received: by 2002:a17:906:dc8e:b0:6e7:1713:5de9 with SMTP id cs14-20020a170906dc8e00b006e717135de9mr6093580ejc.71.1649207184955; Tue, 05 Apr 2022 18:06:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649207184; cv=none; d=google.com; s=arc-20160816; b=CRH4JKPW5qX9RtDapfP3lSqyJ11TdNJPcizlscFDzz01kuzfFh54+3AXfgYMRYXK5D Naqe8CEtXDIIVWEK9zBGkkN6BICxDDhhSVaU/2k62r71czJTSnTEMQrajOS4/g8uhuyY MHG3uoCLYRf9cNBKqB8Zf/XxBTp8YA7kmXTd9+z2bZgkJlS3ew70jLnUp9q5fg7XwSq5 NNZGyNkqOUVzmy+nG52Jzq+NdrYYYhBCeyVjG/RddWipN8oW/1l7sTIiFMdAmL4Z94jM rZSmO+fjU9XVtlATgCe5z9kKt4bgNydkz54DVqkMcNMq/7yxSCRW6x4EO3g9QBx7omDu bqjA== 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=W+MPkEO5Dh0wfpLEeepIDlLUPwV96oH4tScLAvMs5qg=; b=tIs8Bo09Q8DjGnxuK0ZC9YwlKXN+8eQfYfnbMZeSVWXkJr65iNwC8y5kGHvZvmyz/X BG64Qn5qn4At3kHfD8T7//wfftg2OCoCui1pwZWoG/fXmapZucK8fN03K9xZM7jgiAOR d5tb7lhVcNBTJsadRDvWG5QiQkZ+gyw3LE8WZ/SihwRM/fmkMCDLxSOtznSq5J+tmOAz WpVxZpc5w2F5BRLCF9LfCPKceAhFQZP6kgKOrVvY7o6AfGKcn0r4E3wWVfe4t5xriPBt 5JhvfqKr/3iD4pixNaX73ZrtHDEo/8bteB/NjKKq/ngOj+O+Lh3NVfOhFOuPp3A9ZJq3 OCaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rzx2Em2c; 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 w22-20020aa7cb56000000b00418c2b5be29si11387648edt.267.2022.04.05.18.06.00; Tue, 05 Apr 2022 18:06:24 -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=rzx2Em2c; 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 S1377687AbiDEOiO (ORCPT + 99 others); Tue, 5 Apr 2022 10:38:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242232AbiDEJg1 (ORCPT ); Tue, 5 Apr 2022 05:36:27 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B807972F7; Tue, 5 Apr 2022 02:24:22 -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 12792B81C99; Tue, 5 Apr 2022 09:24:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F142C385A0; Tue, 5 Apr 2022 09:24:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150659; bh=fo8p2+ttd+ffwyrKTGB2OgmH97OwcdKTt3Yz9iCK+aU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rzx2Em2c0cig403k2LFOSYazTP2da+EFyPGXpZ2V1lHowK2oqsJXa0A0ENVpCCUPK 3UHWvX+y5WC6I+OwQTH9lUaSWfNxEmkiy7MLPygw29Ige93zQu/EtEmceOFZdDvYr9 WOi60e+GBa816SEMgQNsNahuqizVgL4nQigUzy7w= 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 5.15 135/913] drivers: hamradio: 6pack: fix UAF bug caused by mod_timer() Date: Tue, 5 Apr 2022 09:19:57 +0200 Message-Id: <20220405070343.877315234@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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.1 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 @@ -669,14 +669,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. */