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 D1FB6EC9 for ; Thu, 11 May 2023 03:44:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD1A6C433EF; Thu, 11 May 2023 03:44:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1683776678; bh=0znUxwXk2K0JJbiEiLhhTK64Sd0XVZz9CaoB7WaSTOQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Sk8439F0iNdydOVyHAr9ets543Vgaznl/4yqDjkXoVux9ZjftvyQTZKHy/3L7kEoV OGbQmqdKeom9HSU1lSxUVe+c4t8x2H7VtWNr+/CSk1hjtrzqE/JMN8SQEH9HlRDGaC UgfyfVLeoAhSW7y0DKufd0mGLED6PyZ5cwOPuEgk= Date: Thu, 11 May 2023 12:44:32 +0900 From: Greg Kroah-Hartman To: Zhangfei Gao Cc: Arnd Bergmann , Herbert Xu , jean-philippe , Wangzhou , Jonathan Cameron , linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux.dev, acc@lists.linaro.org, Weili Qian Subject: Re: [PATCH] uacce: use filep->f_mapping to replace inode->i_mapping Message-ID: <2023051110-jelly-barricade-d737@gregkh> References: <20230511021553.44318-1-zhangfei.gao@linaro.org> Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230511021553.44318-1-zhangfei.gao@linaro.org> On Thu, May 11, 2023 at 10:15:53AM +0800, Zhangfei Gao wrote: > The inode can be different in a container, for example, a docker and host > both open the same uacce parent device, which uses the same uacce struct > but different inode, so uacce->inode is not enough. > > What's worse, when docker stops, the inode will be destroyed as well, > causing use-after-free in uacce_remove. > > So use q->filep->f_mapping to replace uacce->inode->i_mapping. > > Signed-off-by: Weili Qian > Signed-off-by: Zhangfei Gao > --- > drivers/misc/uacce/uacce.c | 16 +++++++++------- > include/linux/uacce.h | 4 ++-- > 2 files changed, 11 insertions(+), 9 deletions(-) > > diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c > index 346bd7cf2e94..740ace422baa 100644 > --- a/drivers/misc/uacce/uacce.c > +++ b/drivers/misc/uacce/uacce.c > @@ -166,8 +166,8 @@ static int uacce_fops_open(struct inode *inode, struct file *filep) > > init_waitqueue_head(&q->wait); > filep->private_data = q; > - uacce->inode = inode; > q->state = UACCE_Q_INIT; > + q->private_data = filep; > mutex_init(&q->mutex); > list_add(&q->list, &uacce->queues); > mutex_unlock(&uacce->mutex); > @@ -574,12 +574,6 @@ void uacce_remove(struct uacce_device *uacce) > > if (!uacce) > return; > - /* > - * unmap remaining mapping from user space, preventing user still > - * access the mmaped area while parent device is already removed > - */ > - if (uacce->inode) > - unmap_mapping_range(uacce->inode->i_mapping, 0, 0, 1); > > /* > * uacce_fops_open() may be running concurrently, even after we remove > @@ -589,6 +583,8 @@ void uacce_remove(struct uacce_device *uacce) > mutex_lock(&uacce->mutex); > /* ensure no open queue remains */ > list_for_each_entry_safe(q, next_q, &uacce->queues, list) { > + struct file *filep = q->private_data; > + > /* > * Taking q->mutex ensures that fops do not use the defunct > * uacce->ops after the queue is disabled. > @@ -597,6 +593,12 @@ void uacce_remove(struct uacce_device *uacce) > uacce_put_queue(q); > mutex_unlock(&q->mutex); > uacce_unbind_queue(q); > + > + /* > + * unmap remaining mapping from user space, preventing user still > + * access the mmaped area while parent device is already removed > + */ > + unmap_mapping_range(filep->f_mapping, 0, 0, 1); > } > > /* disable sva now since no opened queues */ > diff --git a/include/linux/uacce.h b/include/linux/uacce.h > index 0a81c3dfd26c..64b800b74436 100644 > --- a/include/linux/uacce.h > +++ b/include/linux/uacce.h > @@ -86,6 +86,7 @@ enum uacce_q_state { > * @state: queue state machine > * @pasid: pasid associated to the mm > * @handle: iommu_sva handle returned by iommu_sva_bind_device() > + * @private_data: private data for saving filep > */ > struct uacce_queue { > struct uacce_device *uacce; > @@ -97,6 +98,7 @@ struct uacce_queue { > enum uacce_q_state state; > u32 pasid; > struct iommu_sva *handle; > + void *private_data; Make this a real pointer to the inode, no need to make this "void *", right? thanks, greg k-h