Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8232011rwr; Wed, 10 May 2023 21:00:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7resXEP9SQGADLXP9HeHcoo1Hvvfl4A5wxUVugivJDBPRebMPI57hhe0mWzUaa/byeIDIo X-Received: by 2002:a17:90a:7e13:b0:252:89bc:1cd9 with SMTP id i19-20020a17090a7e1300b0025289bc1cd9mr1327244pjl.20.1683777630552; Wed, 10 May 2023 21:00:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683777630; cv=none; d=google.com; s=arc-20160816; b=xtpQ3W81GXthmHxzB2tDPBJN9ToAx0N2FgCw7H4sNz0VIyzYdlT52/389cu6d7keup q35vG2iqpYQ8Mxo+0qAypi2Gi+Lv5VQM0mpl+j3Pa118qZ/olNP7XskMyN43XtuFWPf2 8UTBsgo+Xaa4UN7aMatrj2NQsV1j+Zt5cKIuOWJGYXC2+7Jols4FdFfkfCkav1pkfGFo jY0b6toWj3q1RVgqQG/9PmDciGI8rFUg2mfoqEasGQSFLZ7RT2LJ2GqmE6KZ+EaqWhGz ZiKutO6DR9c4wisg1qhnhdiiSEAvnJ0+5hcJ+QDsAY7HTXwK5znblKMJwarHoW10eB8o WBrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=kbD9nuSzqdSx2eNo0wx5cH9TfhKrMZrDnVFwEPg1pDM=; b=CJ7EPLrl9f/Ws2ivzJCe1TcsuR/GCskAXc+Wd7m5kFtAHxz61ScSctrkyYhIX3x8y9 dePWsHehRslpr/QMdMV8S6hIYP//BvTLxZyCrpzeevzVI59lNUD6CWFyabUaQ7fzOfPD ygCuJQ9PeK6uRCU/e8ip7PkiEH/HC+OCXONRyHxe8qbwdACINU43enmGqT2PIaxfv2A4 xV7KCAjOEy/BxftNAS8xCW2a8qM3FbYdtSzzyHSTBbPJDEJ2MAmCsKMUe18/LPf9Z5nF PHlMr8qsBlqd4EAJEsHirtwqPmdcLTQP4Tqp9ifhlyKq7jenbHC+krRH2KlMxvO3eudJ n/6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Sk8439F0; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x26-20020a63b21a000000b0052cb473f95csi5694024pge.211.2023.05.10.21.00.13; Wed, 10 May 2023 21:00:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=pass header.i=@linuxfoundation.org header.s=korg header.b=Sk8439F0; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230316AbjEKDol (ORCPT + 99 others); Wed, 10 May 2023 23:44:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230205AbjEKDok (ORCPT ); Wed, 10 May 2023 23:44:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AF8E30FB; Wed, 10 May 2023 20:44:39 -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 1730B64160; Thu, 11 May 2023 03:44:39 +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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230511021553.44318-1-zhangfei.gao@linaro.org> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.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