From patchwork Sat May 13 17:57:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mauro Carvalho Chehab X-Patchwork-Id: 93554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp5902708vqo; Sat, 13 May 2023 10:59:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6MOI8OMXBRzHQbD74vdmypaVM5a0bsqBFZD35mkd4Z+n9sfqYvKn8/BrFjU0RFFSGHTD81 X-Received: by 2002:a05:6a00:2392:b0:645:ac97:5295 with SMTP id f18-20020a056a00239200b00645ac975295mr28522001pfc.9.1684000795056; Sat, 13 May 2023 10:59:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684000795; cv=none; d=google.com; s=arc-20160816; b=RFvCvmAyKB0qAzzxGMRAvHpylL64fR7BzFinAO8ON/PVjyEiKIQ5ZxieG2cs3rRo+K paK+cARJ0kHX7AlXbHNARSX4ertDchASIOTQDKZRHIa7KEL87UAlW9hppiHPthuhqYny VBWvFIlyFqyGuclergnxy+P9/O6XIJ6W9dP+BfSJP+lGZWLzoRR6/vF+PYFYs9hZsLw7 L0VBQU68NqxSR3LvXYAv83ULgmdKjFCD7NABJrFyEY6hFTW7jKbjMVmoeXdwzgRDJ25g VNZPMRguNAh9uVQiBIG82t6UbvCzUvwPxrg5lID32ccRGpyiTBaxnsBfvI6GgsBpK8Ka d1Pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=ZAKhxKMR+me0xcW0oBKSrM7ljPprBw3qEMtn5ImN6yU=; b=m5BjP5mqn2c7RJ4sYtJ3Jxzc3nL1I6X8ZeBh8Viv33QTMve/3J4Hzc5/BHS05uyYV0 SgvB58yHaQMueDJRuuHe7c1F4RSKqGHadwoSuDjpqrPMC2L+h66sPk+SMYeey2tSRxBJ U++Ay6b4EASeCoJGEZOErMs0GjXPMs4wxFJ6ljYXbrOcGoffI4Ktj2yXMNXm5mp6IZZe 6oOGMJLtNlWv7j2zmCVwul92r/o8IihZOQQ0LbvL2fGxtQjahMB2pElF1GXW/O9VN77q W7Tr/g+3afU7GNqpopIJ2DbPiTrQ4oqQBuytckT0sX7W0GHZFAEduCKE6f/ZBwAbzh9E Eq6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@kernel.org header.s=k20201202 header.b="csGG/ewU"; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i125-20020a636d83000000b0052c30fc9fd9si11330874pgc.125.2023.05.13.10.59.40; Sat, 13 May 2023 10:59:55 -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=fail header.i=@kernel.org header.s=k20201202 header.b="csGG/ewU"; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237462AbjEMR6n (ORCPT + 99 others); Sat, 13 May 2023 13:58:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233655AbjEMR5w (ORCPT ); Sat, 13 May 2023 13:57:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 515F7171E; Sat, 13 May 2023 10:57:50 -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 dfw.source.kernel.org (Postfix) with ESMTPS id D592761D18; Sat, 13 May 2023 17:57:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A555C43321; Sat, 13 May 2023 17:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684000666; bh=PXB8g8OwRVp5vTtA+di89yda0JZvQHWqqIcZtmTI8Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=csGG/ewUYTt9aedR/ML62VAPd2h9WyQnXTnYgQTfBw5QoJtR/u2cA4SmkfKZbm00i 2Gi8UH6YigeS9I8r4rrK+kM6wwRMRAIEBL1SDqfWpdPBMSk+/mPZhe/9tSax0YDQcL NcJoW+567qPtMJx/ObJXB7pixDObMGdfYcw+b7q5Bs0axkg1RKTq7pTWugeeRwA9KO h7IXTC9VPeQulLgmKQP8K1q83kB7aKbpBXddgvzZIlm7Iv7hn5KXThRpxRSUF37mMq Zcg6lRbkYJ4c/nkNY0gMVeDx8Tspa7m6SndqUZgN0WalqdqT7oRJZu+toW+Bo5+RvW nPIhEvemRFEgQ== Received: from mchehab by mail.kernel.org with local (Exim 4.96) (envelope-from ) id 1pxtV1-001ty8-1e; Sat, 13 May 2023 18:57:43 +0100 From: Mauro Carvalho Chehab Cc: Hyunwoo Kim , Mauro Carvalho Chehab , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org Subject: [PATCH 15/24] media: dvb-core: Fix use-after-free due to race condition occurring in dvb_net Date: Sat, 13 May 2023 18:57:32 +0100 Message-Id: <1ae42fd55196b955283f6884f87d77607f588d45.1684000646.git.mchehab@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <53558de2b5c4f4ee6bfcfbe34e27071c2d0073d5.1684000646.git.mchehab@kernel.org> References: <53558de2b5c4f4ee6bfcfbe34e27071c2d0073d5.1684000646.git.mchehab@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765802817262320103?= X-GMAIL-MSGID: =?utf-8?q?1765802817262320103?= From: Hyunwoo Kim A race condition may occur between the .disconnect function, which is called when the device is disconnected, and the dvb_device_open() function, which is called when the device node is open()ed. This results in several types of UAFs. The root cause of this is that you use the dvb_device_open() function, which does not implement a conditional statement that checks 'dvbnet->exit'. So, add 'remove_mutex` to protect 'dvbnet->exit' and use locked_dvb_net_open() function to check 'dvbnet->exit'. [mchehab: fix a checkpatch warning] Link: https://lore.kernel.org/linux-media/20221117045925.14297-3-imv4bel@gmail.com Signed-off-by: Hyunwoo Kim Signed-off-by: Mauro Carvalho Chehab --- drivers/media/dvb-core/dvb_net.c | 38 +++++++++++++++++++++++++++++--- include/media/dvb_net.h | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/drivers/media/dvb-core/dvb_net.c b/drivers/media/dvb-core/dvb_net.c index 8a2febf33ce2..8bb8dd34c223 100644 --- a/drivers/media/dvb-core/dvb_net.c +++ b/drivers/media/dvb-core/dvb_net.c @@ -1564,15 +1564,43 @@ static long dvb_net_ioctl(struct file *file, return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl); } +static int locked_dvb_net_open(struct inode *inode, struct file *file) +{ + struct dvb_device *dvbdev = file->private_data; + struct dvb_net *dvbnet = dvbdev->priv; + int ret; + + if (mutex_lock_interruptible(&dvbnet->remove_mutex)) + return -ERESTARTSYS; + + if (dvbnet->exit) { + mutex_unlock(&dvbnet->remove_mutex); + return -ENODEV; + } + + ret = dvb_generic_open(inode, file); + + mutex_unlock(&dvbnet->remove_mutex); + + return ret; +} + static int dvb_net_close(struct inode *inode, struct file *file) { struct dvb_device *dvbdev = file->private_data; struct dvb_net *dvbnet = dvbdev->priv; + mutex_lock(&dvbnet->remove_mutex); + dvb_generic_release(inode, file); - if(dvbdev->users == 1 && dvbnet->exit == 1) + if (dvbdev->users == 1 && dvbnet->exit == 1) { + mutex_unlock(&dvbnet->remove_mutex); wake_up(&dvbdev->wait_queue); + } else { + mutex_unlock(&dvbnet->remove_mutex); + } + return 0; } @@ -1580,7 +1608,7 @@ static int dvb_net_close(struct inode *inode, struct file *file) static const struct file_operations dvb_net_fops = { .owner = THIS_MODULE, .unlocked_ioctl = dvb_net_ioctl, - .open = dvb_generic_open, + .open = locked_dvb_net_open, .release = dvb_net_close, .llseek = noop_llseek, }; @@ -1599,10 +1627,13 @@ void dvb_net_release (struct dvb_net *dvbnet) { int i; + mutex_lock(&dvbnet->remove_mutex); dvbnet->exit = 1; + mutex_unlock(&dvbnet->remove_mutex); + if (dvbnet->dvbdev->users < 1) wait_event(dvbnet->dvbdev->wait_queue, - dvbnet->dvbdev->users==1); + dvbnet->dvbdev->users == 1); dvb_unregister_device(dvbnet->dvbdev); @@ -1621,6 +1652,7 @@ int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet, int i; mutex_init(&dvbnet->ioctl_mutex); + mutex_init(&dvbnet->remove_mutex); dvbnet->demux = dmx; for (i=0; i