Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6011246ybc; Wed, 27 Nov 2019 13:21:31 -0800 (PST) X-Google-Smtp-Source: APXvYqwHa/IpqbINZ7eQmI5dAwRq98H/fnS0mMFiWG9QU/uT3aAicOIO085NOVulZMK0tSsbPNyf X-Received: by 2002:a05:6402:714:: with SMTP id w20mr30406297edx.93.1574889691170; Wed, 27 Nov 2019 13:21:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574889691; cv=none; d=google.com; s=arc-20160816; b=DP53POtNrjOMxWqy5/9XY2whz1Ik+/4/U0Uj4OKZyLAJsVCCweU4vzLE44tM82huIY i4Tc91EDtT7EjaBxufDe7yu98GmFBovV/4WecRVkcMYx0SNO8A61eWm8/WCIkD1cf64g lwJv0toSNVxETdUczp+krzMYWHKufN1IHOL1t+zdkE48CFp/rEf8lG6fe8zJ0uyTL8Xm 9/VqpKBSqp3dZrea8qYAygH2aR2DzAeP4MfUCDGgw+4KASJ+A8ROC/JQuANQPh0C0uXh g+C7sJc3Ls3U5U0DvgLeBFxJZWAq5GbM1AtTwupWnThWlqpKdMENwRW1qmno7CE3+F/U pcoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1icoaX0FUjh62u2dSChCZ13MclUI17UeNRYbMtamrpQ=; b=d7MC6FKvWI2+27oOdITN+iDeaTB0z13yv+lApJSFaZLXkfoFATQG67gJOIf8f6kear vKEDZV1tOHTP5Fd4XdTAYEO5EeykEyKRDn7pdgRCPF5KZa1LLEw8WMX/8cEwwZfyYBlP TaXPnnwMXx0DzzeKvS6SZYtC9lWbx8o+C48ApktxZ6AEpGw95qgVorInuoroV5wl93Li NFrhrr61a+gqeQZgLcVMiRFnOOVoBMe9RdyGu4TWb5PnrpdlHF3oWcMr+JCNEQKn7IiT BepKt/4q7ooQQY8viw8q3i2jRh2/GqtBAE4rxm0hwXCJwSBTbicglO8etiKelKKz5xUG nLHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="LqM/9xfm"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w21si3893163edx.425.2019.11.27.13.21.07; Wed, 27 Nov 2019 13:21:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="LqM/9xfm"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733073AbfK0VQq (ORCPT + 99 others); Wed, 27 Nov 2019 16:16:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:46108 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727139AbfK0VNl (ORCPT ); Wed, 27 Nov 2019 16:13:41 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4B2202086A; Wed, 27 Nov 2019 21:13:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574889220; bh=oSmQcau+Dz+wLRGRWwAQ5Ji3N0rAjF1AMGTR5Sm7Omk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LqM/9xfmWqkuZtiaGNV7wBSc0Tb4gqrmlzQ4GrxBWYdJlC3nVZ8TKAAE5hID0Lob5 RJoS7K6zvF58FJl2Cfew+OsanLb1RIOakXhJK6FaLSPJ1lGBPInZ2MkMd7z1nhkoPP PnJuhDRQJzKqPJQ8I9wQ0bNqPMVNlxH8jPcfthVY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH 5.4 34/66] media: usbvision: Fix races among open, close, and disconnect Date: Wed, 27 Nov 2019 21:32:29 +0100 Message-Id: <20191127202713.864639036@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127202632.536277063@linuxfoundation.org> References: <20191127202632.536277063@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alan Stern commit 9e08117c9d4efc1e1bc6fce83dab856d9fd284b6 upstream. Visual inspection of the usbvision driver shows that it suffers from three races between its open, close, and disconnect handlers. In particular, the driver is careful to update its usbvision->user and usbvision->remove_pending flags while holding the private mutex, but: usbvision_v4l2_close() and usbvision_radio_close() don't hold the mutex while they check the value of usbvision->remove_pending; usbvision_disconnect() doesn't hold the mutex while checking the value of usbvision->user; and also, usbvision_v4l2_open() and usbvision_radio_open() don't check whether the device has been unplugged before allowing the user to open the device files. Each of these can potentially lead to usbvision_release() being called twice and use-after-free errors. This patch fixes the races by reading the flags while the mutex is still held and checking for pending removes before allowing an open to succeed. Signed-off-by: Alan Stern CC: Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Greg Kroah-Hartman --- drivers/media/usb/usbvision/usbvision-video.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) --- a/drivers/media/usb/usbvision/usbvision-video.c +++ b/drivers/media/usb/usbvision/usbvision-video.c @@ -314,6 +314,10 @@ static int usbvision_v4l2_open(struct fi if (mutex_lock_interruptible(&usbvision->v4l2_lock)) return -ERESTARTSYS; + if (usbvision->remove_pending) { + err_code = -ENODEV; + goto unlock; + } if (usbvision->user) { err_code = -EBUSY; } else { @@ -377,6 +381,7 @@ unlock: static int usbvision_v4l2_close(struct file *file) { struct usb_usbvision *usbvision = video_drvdata(file); + int r; PDEBUG(DBG_IO, "close"); @@ -391,9 +396,10 @@ static int usbvision_v4l2_close(struct f usbvision_scratch_free(usbvision); usbvision->user--; + r = usbvision->remove_pending; mutex_unlock(&usbvision->v4l2_lock); - if (usbvision->remove_pending) { + if (r) { printk(KERN_INFO "%s: Final disconnect\n", __func__); usbvision_release(usbvision); return 0; @@ -1064,6 +1070,11 @@ static int usbvision_radio_open(struct f if (mutex_lock_interruptible(&usbvision->v4l2_lock)) return -ERESTARTSYS; + + if (usbvision->remove_pending) { + err_code = -ENODEV; + goto out; + } err_code = v4l2_fh_open(file); if (err_code) goto out; @@ -1096,6 +1107,7 @@ out: static int usbvision_radio_close(struct file *file) { struct usb_usbvision *usbvision = video_drvdata(file); + int r; PDEBUG(DBG_IO, ""); @@ -1109,9 +1121,10 @@ static int usbvision_radio_close(struct usbvision_audio_off(usbvision); usbvision->radio = 0; usbvision->user--; + r = usbvision->remove_pending; mutex_unlock(&usbvision->v4l2_lock); - if (usbvision->remove_pending) { + if (r) { printk(KERN_INFO "%s: Final disconnect\n", __func__); v4l2_fh_release(file); usbvision_release(usbvision); @@ -1543,6 +1556,7 @@ err_usb: static void usbvision_disconnect(struct usb_interface *intf) { struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf)); + int u; PDEBUG(DBG_PROBE, ""); @@ -1559,13 +1573,14 @@ static void usbvision_disconnect(struct v4l2_device_disconnect(&usbvision->v4l2_dev); usbvision_i2c_unregister(usbvision); usbvision->remove_pending = 1; /* Now all ISO data will be ignored */ + u = usbvision->user; usb_put_dev(usbvision->dev); usbvision->dev = NULL; /* USB device is no more */ mutex_unlock(&usbvision->v4l2_lock); - if (usbvision->user) { + if (u) { printk(KERN_INFO "%s: In use, disconnect pending\n", __func__); wake_up_interruptible(&usbvision->wait_frame);