Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp899617pxb; Wed, 6 Apr 2022 03:41:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpEJRqi6pQPD3opdlHCUKe9weu+S+eeIRImgJT+S/xsOPWrYwzBQqLyYuq0lk/vd2ZHe/d X-Received: by 2002:a05:6a00:999:b0:4fa:964f:9021 with SMTP id u25-20020a056a00099900b004fa964f9021mr8271708pfg.34.1649241693021; Wed, 06 Apr 2022 03:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649241693; cv=none; d=google.com; s=arc-20160816; b=igogeRieZ531j9UAAPatgNo+VwpNQmWEQjbHFEnxsUaOLGSkVTjPChpOKvh57SXP0e JHN7yvshrlS9HIKVoGycvFrYqd9cP/adzl6bYc7O3Qwrvhrn7mPg1I3/uyyhOUjP/aBJ 3dejKFrnq4P1lwYf6pEMqYnY29awT+e0mC9K5vMj/N3yzfoulqnq7IH9KwCXF2bP5/CE vbdG8y2vBBI6RXbfGWoSaStAqle9OmdoG7BG8ZvtsbKB9FY3f725GuflmzIWg55cnsH1 IfBJYiofyq4WRLYDG+9WJFXdxligZspQjwYI46yJtXEmbzmjsr7QL2q07EGWOSzS5Ohi CHrA== 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=dDhYTY2WhS8SdK326TEpgiIGudxITFmKJlPF0DXJM6s=; b=mUJR8JnCjOEhfHveL2OClxdWZ9/PZ6TsWw7L4itMMWm+NWzNdZbzZ3YAofx0eEHYUg vfrF9hgbDuUV0jaBnxL1iMLWet704BVPjY7XcS2WNPjViRKrQFcuWfkmEDhjtMDKHhnb 1DMBoeMLn003Ge7Pmnh0dulxDF7gTPb8z27pAPKAILghue5Tx05c+QHqMuqribG9e0sP sM0sEn0y0z+p1uqe+GJ3cFC/pQiFBG8OzW6yoGIUfNMoP+5wrPjUjboVMeUtVHdmRhzA z/Bmw/vMOkvKMM6JjSuDxKICH3r340C7bqjBJkOXGG+rVfr3gY01JdVejcyVEXqAfBFj 330Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xRA2GAmk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l14-20020a170903120e00b00153b2d165c3si16549196plh.459.2022.04.06.03.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 03:41:33 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xRA2GAmk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 806D542D6B5; Wed, 6 Apr 2022 01:59:03 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1843782AbiDFBmD (ORCPT + 99 others); Tue, 5 Apr 2022 21:42:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355232AbiDEKSh (ORCPT ); Tue, 5 Apr 2022 06:18:37 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40D52D93; Tue, 5 Apr 2022 03:04:33 -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 EFB86B81C86; Tue, 5 Apr 2022 10:04:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65696C385A1; Tue, 5 Apr 2022 10:04:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649153070; bh=Y44RZZtZoW5GEwA5ktM/IoJ5Tnlzo8j8Re6ddG35/IE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xRA2GAmk89KJENgJ2KGbAOfr+vwO3x+FSmkbhiTMJQcv+wyX9MSZ0bN1M5Go9LYLc qczhe1FuiISYolCCdA7vQpw/Y+1OEpRJe9Z1hfn1DKa1FRND8HGP3QBM9Z6knffBTh OebHQbwEY8+TlQM7Wj4GUuc30mH2D4chzRJwD0es= 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.10 092/599] drivers: hamradio: 6pack: fix UAF bug caused by mod_timer() Date: Tue, 5 Apr 2022 09:26:26 +0200 Message-Id: <20220405070301.566657774@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 @@ -674,14 +674,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. */