Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1643461pxy; Mon, 2 Aug 2021 06:57:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJypX5jEm54CK1LwKDP8Wol0NDvbKFrD9/Er7KglGOhMobRhXo6OoO8C13POMUOS6VuyEAte X-Received: by 2002:a05:6e02:b4b:: with SMTP id f11mr126268ilu.130.1627912678737; Mon, 02 Aug 2021 06:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912678; cv=none; d=google.com; s=arc-20160816; b=nD0cF/Pf5NKFM4k9faNe2KRAc788ZauJRxqK8arQT251MNP/TOmMnv3sVIaf5gsLtM hHkatJNUiEq93ej93U4We1TpxX7JyJDmPynRhE+9tAPKSqEnz9Qjs8q6NAu8myL/DwXY D7VlVQlyS5uj4JhxWThdF5QX/b9p0Wt+ah69kYe9hGjQTaxszbn/oVSOPsqKQlxMkeH4 TtStx2nEr50XhbqGtTz5uEDL0wVAW1vWkvcJcf//T2ulYKyz+TtqrbJQbsr/AdpcGFlV TBKczGJeqH583zo6OrDVCZC6ydDEbBmW+hNJvcZYBBEZBXTZVq6/ZAq3EUWIodlpHCSk uxkg== 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=XKUnTqVEJ90dw+emuBJpiSZxxHE8lTQnxSZwuyAmZHQ=; b=au4jn3i/oVmP9HPzkAoOmwryBQ8V+PqMSv8Gzz4UD5xgM7jvFFgc8t2Gwmyx8PvqJ/ yGEB4nQ0lqxlI9aAgzzenY1H2uX2BtGNKIyNjIzjh99WOqN4KRtu3MyU+FzUlQhVKSYV APSv0EXw6a+nFygD2+z2xKctom+RQv5QTehZyVZ92wktHPlHq85nwktkHcNGC1U41wEy BLF+vycJaHJmH3WE2TpLrBCHqMsUbLy8rMOtn2wkcoB2yl5sJVAHMg3peB8RjjRVyYE2 +AwG9A10K8YxQmSFKGT86KHBz6Thv8ObJiOPhnNmZI0c6IJj+rXjs+N+Lj+3Ly7wXVFs wG3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GENGJmGC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y143si12323202iof.80.2021.08.02.06.57.47; Mon, 02 Aug 2021 06:57:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GENGJmGC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S236206AbhHBN5S (ORCPT + 99 others); Mon, 2 Aug 2021 09:57:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:33086 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235117AbhHBNvc (ORCPT ); Mon, 2 Aug 2021 09:51:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 10D8D60F50; Mon, 2 Aug 2021 13:50:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912256; bh=1lXYdALOl7aLdgSDwhY3HsBeZnHOG09Cs7MGMZDAqrE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GENGJmGC/DWH7F2QS1gdjJAxJvaUyGM0Vsy7KoFEnHVc+68iRlRkApEgqX4n74wDf SjggWG69UxuvRAsbiiNFy27sCOQQXYND3/a3StP71lHB3k0pdB9hY80LAVpntJZ9Th Z6RQbCSxAyvyPD/0mJXbz8nlqN58A+AEtbi9OJVQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Krzysztof Kozlowski , "David S. Miller" Subject: [PATCH 5.4 18/40] nfc: nfcsim: fix use after free during module unload Date: Mon, 2 Aug 2021 15:44:58 +0200 Message-Id: <20210802134335.971241777@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134335.408294521@linuxfoundation.org> References: <20210802134335.408294521@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Krzysztof Kozlowski commit 5e7b30d24a5b8cb691c173b45b50e3ca0191be19 upstream. There is a use after free memory corruption during module exit: - nfcsim_exit() - nfcsim_device_free(dev0) - nfc_digital_unregister_device() This iterates over command queue and frees all commands, - dev->up = false - nfcsim_link_shutdown() - nfcsim_link_recv_wake() This wakes the sleeping thread nfcsim_link_recv_skb(). - nfcsim_link_recv_skb() Wake from wait_event_interruptible_timeout(), call directly the deb->cb callback even though (dev->up == false), - digital_send_cmd_complete() Dereference of "struct digital_cmd" cmd which was freed earlier by nfc_digital_unregister_device(). This causes memory corruption shortly after (with unrelated stack trace): nfc nfc0: NFC: nfcsim_recv_wq: Device is down llcp: nfc_llcp_recv: err -19 nfc nfc1: NFC: nfcsim_recv_wq: Device is down BUG: unable to handle page fault for address: ffffffffffffffed Call Trace: fsnotify+0x54b/0x5c0 __fsnotify_parent+0x1fe/0x300 ? vfs_write+0x27c/0x390 vfs_write+0x27c/0x390 ksys_write+0x63/0xe0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae KASAN report: BUG: KASAN: use-after-free in digital_send_cmd_complete+0x16/0x50 Write of size 8 at addr ffff88800a05f720 by task kworker/0:2/71 Workqueue: events nfcsim_recv_wq [nfcsim] Call Trace:  dump_stack_lvl+0x45/0x59  print_address_description.constprop.0+0x21/0x140  ? digital_send_cmd_complete+0x16/0x50  ? digital_send_cmd_complete+0x16/0x50  kasan_report.cold+0x7f/0x11b  ? digital_send_cmd_complete+0x16/0x50  ? digital_dep_link_down+0x60/0x60  digital_send_cmd_complete+0x16/0x50  nfcsim_recv_wq+0x38f/0x3d5 [nfcsim]  ? nfcsim_in_send_cmd+0x4a/0x4a [nfcsim]  ? lock_is_held_type+0x98/0x110  ? finish_wait+0x110/0x110  ? rcu_read_lock_sched_held+0x9c/0xd0  ? rcu_read_lock_bh_held+0xb0/0xb0  ? lockdep_hardirqs_on_prepare+0x12e/0x1f0 This flow of calling digital_send_cmd_complete() callback on driver exit is specific to nfcsim which implements reading and sending work queues. Since the NFC digital device was unregistered, the callback should not be called. Fixes: 204bddcb508f ("NFC: nfcsim: Make use of the Digital layer") Cc: Signed-off-by: Krzysztof Kozlowski Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- drivers/nfc/nfcsim.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) --- a/drivers/nfc/nfcsim.c +++ b/drivers/nfc/nfcsim.c @@ -192,8 +192,7 @@ static void nfcsim_recv_wq(struct work_s if (!IS_ERR(skb)) dev_kfree_skb(skb); - - skb = ERR_PTR(-ENODEV); + return; } dev->cb(dev->nfc_digital_dev, dev->arg, skb);