From patchwork Thu May 11 09:59:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangfei Gao X-Patchwork-Id: 681325 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 041E8C7EE24 for ; Thu, 11 May 2023 10:00:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237370AbjEKKAC (ORCPT ); Thu, 11 May 2023 06:00:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237852AbjEKJ7u (ORCPT ); Thu, 11 May 2023 05:59:50 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1D373A82 for ; Thu, 11 May 2023 02:59:48 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3064099f9b6so5420692f8f.1 for ; Thu, 11 May 2023 02:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683799187; x=1686391187; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YviNjDFHhOqwkOhoUfFml83L4Enfc2RqdN30M/s6IK4=; b=aGrI7+aNFi4ljfy8dR3J49VBbZoM70kswN+XaFAVWv+4rpWu/UfZHTPJMy4NJQdHW7 VmN2CxovXzriyOKSBzoNIjeffT1d9k7WirSXXr6q0sTubF8BnyOpe2xR6hqsFCSRIsDZ J577lClwPZ5CTyTMwOgHdVzDgaVm7F2F904SH0OgAhTJ2uOATb+ApXJ4hZgk8wjmZJdj MdmK5lcJvYN1a2teXAKfVi7qjaEccBEn0sNUxaFjvQUWu6SCIYdxrzH5RxO4dZIPDWsX eKH+3PDzcjA65fRvSzAHBd32uAjnnnR7yCCwD3ZA+EVwJTF7zwnRQ6tB6vGstEsak6qL n+UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683799187; x=1686391187; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YviNjDFHhOqwkOhoUfFml83L4Enfc2RqdN30M/s6IK4=; b=DXzvX/khBn/qZbLTyTIlyjgSkBHuxvVpfLxDcE8iAPOetqx64Bb7d7Shb89hDpQigO IBcXZbrWg2RmHj98al3LYUhiBb/Qu8A0aI5gp0vhWABJAAi8W4cUU3wygVbf7xo1Q+9t UYQK1UkczdtbWgWgjqQwESITYnL932llFn1EfFw7x7gq3AND3xDMVslrL78ZdrZqdvgz d81wAOgm1mxDLLBmsC2vGYU9VVLClhtAhxpmhYX3O2QNFog14kvCf7hgnKmREsvVoecA jYutWcgVpO2hj9Foq8/XupG600V2ZDjkmhAiO91+jZkINq0zwe6F2aSWYdJHCRh80z92 BQog== X-Gm-Message-State: AC+VfDyg6qEPCS5T21/DLB/TkwIGg4S168OSkITLvy7ePnSR3ZbOHOmT 6I9UPOSVVbgbY9ZtzDJOOXBsFuSffsU6osz0oxiIbw== X-Google-Smtp-Source: ACHHUZ6Wbdt/vmNtSDQSPAiAevcSGV91xq3D2Xpdm/EOIJxZ4lFkHeSGe2T/08MoEZv8Nk3hgv86fg== X-Received: by 2002:adf:f30b:0:b0:2cd:bc79:5432 with SMTP id i11-20020adff30b000000b002cdbc795432mr15948521wro.25.1683799187295; Thu, 11 May 2023 02:59:47 -0700 (PDT) Received: from localhost.localdomain ([64.64.123.10]) by smtp.gmail.com with ESMTPSA id k9-20020a05600c1c8900b003f4283f5c1bsm12122791wms.2.2023.05.11.02.59.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 11 May 2023 02:59:46 -0700 (PDT) From: Zhangfei Gao To: Greg Kroah-Hartman , Arnd Bergmann , Herbert Xu , jean-philippe , Wangzhou , Jonathan Cameron , Al Viro Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux.dev, acc@lists.linaro.org, Zhangfei Gao , Weili Qian Subject: [PATCH v2 1/2] uacce: use q->mapping to replace inode->i_mapping Date: Thu, 11 May 2023 17:59:20 +0800 Message-Id: <20230511095921.9331-2-zhangfei.gao@linaro.org> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230511095921.9331-1-zhangfei.gao@linaro.org> References: <20230511021553.44318-1-zhangfei.gao@linaro.org> <20230511095921.9331-1-zhangfei.gao@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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->mapping to replace uacce->inode->i_mapping. Signed-off-by: Weili Qian Signed-off-by: Zhangfei Gao --- drivers/misc/uacce/uacce.c | 14 +++++++------- include/linux/uacce.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c index 346bd7cf2e94..338b59ef5493 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->mapping = filep->f_mapping; 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 @@ -597,6 +591,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(q->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..e290c0269944 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() + * @mapping: user space mapping of the queue */ 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; + struct address_space *mapping; }; /** @@ -114,7 +116,6 @@ struct uacce_queue { * @mutex: protects uacce operation * @priv: private pointer of the uacce * @queues: list of queues - * @inode: core vfs */ struct uacce_device { const char *algs; @@ -130,7 +131,6 @@ struct uacce_device { struct mutex mutex; void *priv; struct list_head queues; - struct inode *inode; }; #if IS_ENABLED(CONFIG_UACCE) From patchwork Thu May 11 09:59:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangfei Gao X-Patchwork-Id: 680980 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D457C77B7F for ; Thu, 11 May 2023 10:00:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237780AbjEKKAD (ORCPT ); Thu, 11 May 2023 06:00:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237889AbjEKJ76 (ORCPT ); Thu, 11 May 2023 05:59:58 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08FB41FF3 for ; Thu, 11 May 2023 02:59:57 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-3077d134028so4759606f8f.3 for ; Thu, 11 May 2023 02:59:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683799195; x=1686391195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7gMr5NOPVmuNsZ2AgAQsBS1s/tGTLr7USPVZEmTtLAM=; b=P69Q7bzbfakRA/K9JgmDghQPAMnRpt0SCpQ7ZEumIPlKWL6gppMxmleXCq84NRDQec L2leVzR/3FWZLsOj6Tl6Lex+dPFazIfbltuPkNdELguYK9TjZf/WA3SwYldnKNuD33cY i6uyQL+VVn9SJjeCEw1kfc9ZC3MC3bElmvRDJRLebt/SEChGfGw+F+z5Xcf2FixWs+Xp Sg7ShRary7Rz+qfF4Bwfz3G5Fgox00N/+dYFL+zNdz36daMbcPuLYtYVi8wEXEoFrAq3 tYrRDOVU9B+js8/ZnaxWh01dOtRU60ZLFi0ybj43WSF9n9IwXb3clh60dC0V+yh+OPsZ m9fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683799195; x=1686391195; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7gMr5NOPVmuNsZ2AgAQsBS1s/tGTLr7USPVZEmTtLAM=; b=k8LGHTmth72w/Fl3nUEkN7eUkhkn2LtkbHVPhempqvXGAEqB4nmmpfhJmLIt7vbK9L fMkXB3FC9LlzxhalR3tvHGDXcYcxeINaWUNpdcUGy2rJIw0ewcMKDBwIFrDEWpPGl7aq 2FPcmEeKguK+oY87y7AZRXWWURCgfZUvahKwANI1KobTF1NgxkCpqb0sDcW/Xlo1KO4r DnWQME91ARHvsR8KBrL63QLYUHj97dPBDQ3qGG1EuCfHvbX3M11CC4+1dCsXBL39meSF BzLZ2hz8vEl//94lHjFtXK96GhkxzUTv5N0N60TpmIUO2aYVqddYWlx3OsIoEbb4pQOm YDgA== X-Gm-Message-State: AC+VfDy1jhVrXdiOz1jbVWVlIN3qzkp5rGnFvMhxHTZpf0bZMH6G1ERt SXaGF8s76MBrIS31U54PcP21lQ== X-Google-Smtp-Source: ACHHUZ4pzscXK11l7u6GlPD+PahTw1+974nMATdOD8dNFVVaDttsw1qXyCihSWf8jbDdM1+W/oZSLg== X-Received: by 2002:adf:d0c7:0:b0:306:372d:7891 with SMTP id z7-20020adfd0c7000000b00306372d7891mr14334874wrh.59.1683799195475; Thu, 11 May 2023 02:59:55 -0700 (PDT) Received: from localhost.localdomain ([64.64.123.10]) by smtp.gmail.com with ESMTPSA id k9-20020a05600c1c8900b003f4283f5c1bsm12122791wms.2.2023.05.11.02.59.47 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 11 May 2023 02:59:55 -0700 (PDT) From: Zhangfei Gao To: Greg Kroah-Hartman , Arnd Bergmann , Herbert Xu , jean-philippe , Wangzhou , Jonathan Cameron , Al Viro Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, iommu@lists.linux.dev, acc@lists.linaro.org, Zhangfei Gao Subject: [PATCH v2 2/2] uacce: vma_close clears q->qfrs when freeing qfrs Date: Thu, 11 May 2023 17:59:21 +0800 Message-Id: <20230511095921.9331-3-zhangfei.gao@linaro.org> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230511095921.9331-1-zhangfei.gao@linaro.org> References: <20230511021553.44318-1-zhangfei.gao@linaro.org> <20230511095921.9331-1-zhangfei.gao@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org vma_close frees qfrs but not clears q->qfrs, which still points to the freed object, leading to subsequent mmap fail. So vma_close clears q->qfrs as well. Suggested-by: Al Viro Signed-off-by: Zhangfei Gao --- drivers/misc/uacce/uacce.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c index 338b59ef5493..930c252753a0 100644 --- a/drivers/misc/uacce/uacce.c +++ b/drivers/misc/uacce/uacce.c @@ -200,12 +200,15 @@ static int uacce_fops_release(struct inode *inode, struct file *filep) static void uacce_vma_close(struct vm_area_struct *vma) { struct uacce_queue *q = vma->vm_private_data; - struct uacce_qfile_region *qfr = NULL; - if (vma->vm_pgoff < UACCE_MAX_REGION) - qfr = q->qfrs[vma->vm_pgoff]; + if (vma->vm_pgoff < UACCE_MAX_REGION) { + struct uacce_qfile_region *qfr = q->qfrs[vma->vm_pgoff]; - kfree(qfr); + mutex_lock(&q->mutex); + q->qfrs[vma->vm_pgoff] = NULL; + mutex_unlock(&q->mutex); + kfree(qfr); + } } static const struct vm_operations_struct uacce_vm_ops = {