From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20758F504; Tue, 23 Jan 2024 00:04:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705968254; cv=none; b=pYFQXkT4zB9HWmsIzFlhVVUj501I5ocqrPBsriy/c3q7tPI8REm1YwKTMbqEL90LXZEL73p7T2omd+NQSUq3kHaLkO2aTyGZQSRaNJiM/TbHH4P/Qw/FeJnGLIs7m/t27hv0BJeOOWqt2OFQcx5eMk+RecvX7cy7IoujP+sDaSI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705968254; c=relaxed/simple; bh=ZH0qaRItczQagGvOHVLOqxTthUemlEAByzIMUlZa3K0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gsLA/4+eYz/j/yU2UaUOcl2z1yAYQRRav5roy/Zw119Qeb7FeiCyWoszrYHP56kcVKMBt7XOvUNt7IyCzwaKFXaHKRXrm96hCzRjrG2NNSzpzaQSI6ArRttuIjOE+UESlgt/eBwroHdQREn/JgONxt8o8mmgU5zfq+WBC/3mNDA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=yetxnN9x; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="yetxnN9x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02AEEC433F1; Tue, 23 Jan 2024 00:04:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1705968254; bh=ZH0qaRItczQagGvOHVLOqxTthUemlEAByzIMUlZa3K0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yetxnN9xLkF+drFsqvW0Ez5vyUDJcqex/2JPNeqtTu2OWSjgg4b0hBz4NgepPpb9Q GtQ2JW3jqFLb5mBl7zJfdTcDL5yLlmsAmqWIG0PCA4BnCYVJJVevrPMGtBTG0MHM3y XtUYmGzXqT05HBYGfXh9nKdQZCjDxm+QTyDTxXC4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, stable , Guanghui Feng , Baolin Wang Subject: [PATCH 4.19 025/148] uio: Fix use-after-free in uio_open Date: Mon, 22 Jan 2024 15:56:21 -0800 Message-ID: <20240122235713.441195414@linuxfoundation.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240122235712.442097787@linuxfoundation.org> References: <20240122235712.442097787@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Guanghui Feng commit 0c9ae0b8605078eafc3bea053cc78791e97ba2e2 upstream. core-1 core-2 ------------------------------------------------------- uio_unregister_device uio_open idev = idr_find() device_unregister(&idev->dev) put_device(&idev->dev) uio_device_release get_device(&idev->dev) kfree(idev) uio_free_minor(minor) uio_release put_device(&idev->dev) kfree(idev) ------------------------------------------------------- In the core-1 uio_unregister_device(), the device_unregister will kfree idev when the idev->dev kobject ref is 1. But after core-1 device_unregister, put_device and before doing kfree, the core-2 may get_device. Then: 1. After core-1 kfree idev, the core-2 will do use-after-free for idev. 2. When core-2 do uio_release and put_device, the idev will be double freed. To address this issue, we can get idev atomic & inc idev reference with minor_lock. Fixes: 57c5f4df0a5a ("uio: fix crash after the device is unregistered") Cc: stable Signed-off-by: Guanghui Feng Reviewed-by: Baolin Wang Link: https://lore.kernel.org/r/1703152663-59949-1-git-send-email-guanghuifeng@linux.alibaba.com Signed-off-by: Greg Kroah-Hartman --- drivers/uio/uio.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) --- a/drivers/uio/uio.c +++ b/drivers/uio/uio.c @@ -464,13 +464,13 @@ static int uio_open(struct inode *inode, mutex_lock(&minor_lock); idev = idr_find(&uio_idr, iminor(inode)); - mutex_unlock(&minor_lock); if (!idev) { ret = -ENODEV; + mutex_unlock(&minor_lock); goto out; } - get_device(&idev->dev); + mutex_unlock(&minor_lock); if (!try_module_get(idev->owner)) { ret = -ENODEV; @@ -1019,9 +1019,8 @@ void uio_unregister_device(struct uio_in idev->info = NULL; mutex_unlock(&idev->info_lock); - device_unregister(&idev->dev); - uio_free_minor(minor); + device_unregister(&idev->dev); return; }