From patchwork Mon Jan 23 14:21:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Duoming Zhou X-Patchwork-Id: 646379 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B91F2C38142 for ; Mon, 23 Jan 2023 14:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232253AbjAWOVm (ORCPT ); Mon, 23 Jan 2023 09:21:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230377AbjAWOVl (ORCPT ); Mon, 23 Jan 2023 09:21:41 -0500 X-Greylist: delayed 47329 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 23 Jan 2023 06:21:38 PST Received: from zg8tmtyylji0my4xnjeumjiw.icoremail.net (zg8tmtyylji0my4xnjeumjiw.icoremail.net [162.243.161.220]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 8BF2D2004D; Mon, 23 Jan 2023 06:21:38 -0800 (PST) Received: from ubuntu.localdomain (unknown [112.254.167.99]) by mail-app4 (Coremail) with SMTP id cS_KCgDnet3ml85jwadpCg--.20831S2; Mon, 23 Jan 2023 22:21:34 +0800 (CST) From: Duoming Zhou To: linux-media@vger.kernel.org, sean@mess.org Cc: maximlevitsky@gmail.com, mchehab@kernel.org, linux-kernel@vger.kernel.org, Duoming Zhou Subject: [PATCH v2] media: rc: Fix use-after-free bugs caused by ene_tx_irqsim() Date: Mon, 23 Jan 2023 22:21:23 +0800 Message-Id: <20230123142123.58870-1-duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: cS_KCgDnet3ml85jwadpCg--.20831S2 X-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ZdtdU8lAAjsg Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org When the ene device is detaching, function ene_remove() will be called. But there is no function to cancel tx_sim_timer in ene_remove(), the timer handler ene_tx_irqsim() could race with ene_remove(). As a result, the UAF bugs could happen, the process is shown below. (cleanup routine) | (timer routine) | mod_timer(&dev->tx_sim_timer, ..) ene_remove() | (wait a time) kfree(dev) //FREE | | ene_tx_irqsim() | dev->hw_lock //USE | ene_tx_sample(dev) //USE Fix by adding del_timer_sync(&dev->tx_sim_timer) in ene_remove(), The tx_sim_timer could stop before ene device is deallocated. What's more, the timer can call ene_tx_sample() which can write to the io ports. So we should put rc_unregister_device() and del_timer_sync(&dev->tx_sim_timer) before release_region() in ene_remove(). Fixes: 9ea53b74df9c ("V4L/DVB: STAGING: remove lirc_ene0100 driver") Signed-off-by: Duoming Zhou --- Changes in v2: - Move rc_unregister_device() and del_timer_sync() before release_region(). drivers/media/rc/ene_ir.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/media/rc/ene_ir.c b/drivers/media/rc/ene_ir.c index e09270916fb..2864360af9e 100644 --- a/drivers/media/rc/ene_ir.c +++ b/drivers/media/rc/ene_ir.c @@ -1112,8 +1112,9 @@ static void ene_remove(struct pnp_dev *pnp_dev) spin_unlock_irqrestore(&dev->hw_lock, flags); free_irq(dev->irq, dev); - release_region(dev->hw_io, ENE_IO_SIZE); rc_unregister_device(dev->rdev); + del_timer_sync(&dev->tx_sim_timer); + release_region(dev->hw_io, ENE_IO_SIZE); kfree(dev); }