Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2290095pxb; Wed, 9 Feb 2022 15:17:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZ0pxVajD5JP1OpJAVTnXWvMSsl0kuTSf6ww7ezj4CcqskMmMqXvP4frzF9zalJ6ZIhbhJ X-Received: by 2002:a63:8a:: with SMTP id 132mr3821633pga.338.1644448664337; Wed, 09 Feb 2022 15:17:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644448664; cv=none; d=google.com; s=arc-20160816; b=kaL2ySqtFSEsS44aMlqxYumc+JX4NDq4Kj/kMAkQ2FOFaYkMrwaKvWxueE0kbpYkH0 uJJmUbwQnv9sqx18JZhaO1icJ8eykwjNXbInOY2Hjnzz69PUXLMqOoj7JbuzdH3L0IvP 9JxOro+ZXkeeFR4/fk1MvxtLeJWyslUqBONveHj+1V22JsLfHlphsT/EZjbruMjkjzBt cI/VXA9yispR5EZ9n+Z+52Ck25Qz6IjVBTVfXMATv5jGpl5CmeiB2w+zE4BunkOHAOZD 9Vl9Fy+s6XbSnwCVs91GuZVay8Udz+kq2KhDjzFL1lNIZm+jl4RnUAq6Uu4kC3YlTX+B 0Z5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=vnKaJ3fYeFxAypTgfkLzFAKETIOGKeliSE47QqKxh2M=; b=0vgv3p6KPRfvmYqK9xTN2D+UEXeyaG8mwbLE4VQDOg6aHoTJT7oHNzH3dBl50qYonL oh1GTz3jpIqrua0YkXxpcbXz64d0hRFkgJVRhJxVkVyVs5MSpDqpZLvlXCrvUGR/emtQ XRXyiOwvijSMWJi0w7vzlXUVqcfIJ5Ox8n1LVqEJuuwbl9jy/ya3Wi93Vjvt3L8RmHsM 8cnbJ7cyPdkCeEQ1INOwU6Ac3qhAFZZH0NMZc2bSW/atgl7i9ZLR1amTNq0sdxy7PQBH 4S2t6IJFWdytrlO1bOPhV/GZy7rOQXQQP8wT9myGVF9GhoHPL6FhtaE2VtaBRA2mRPCf pzJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=I7zcBZ11; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m3si302481pls.185.2022.02.09.15.17.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 15:17:44 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=I7zcBZ11; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8A578E053832; Wed, 9 Feb 2022 15:15:44 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233621AbiBIVlp (ORCPT + 99 others); Wed, 9 Feb 2022 16:41:45 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:52030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233627AbiBIVll (ORCPT ); Wed, 9 Feb 2022 16:41:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9F7D1DD94E68 for ; Wed, 9 Feb 2022 13:41:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644442902; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vnKaJ3fYeFxAypTgfkLzFAKETIOGKeliSE47QqKxh2M=; b=I7zcBZ11yet4j91X0OvgspsezIIZ7u5UNpnIjyJntp266UY3QD78LAlbn2fkevgDEoEqyo IG2ldO0C9OmVnm3bjuwTxHg0tMv7joY/GiKvM5xrCSbqVDAz4GznPaYOEbobA41DuDlGHQ 4v6M5bmaMF5CbR1MO4gue+2maXECCgI= Received: from mail-io1-f69.google.com (mail-io1-f69.google.com [209.85.166.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-283-vibPio9HOsmuHmCiOVXAmg-1; Wed, 09 Feb 2022 16:41:41 -0500 X-MC-Unique: vibPio9HOsmuHmCiOVXAmg-1 Received: by mail-io1-f69.google.com with SMTP id h1-20020a056602008100b0061152382337so2662003iob.18 for ; Wed, 09 Feb 2022 13:41:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:organization:mime-version:content-transfer-encoding; bh=vnKaJ3fYeFxAypTgfkLzFAKETIOGKeliSE47QqKxh2M=; b=pQPVJvjQkjjFE1gi+MS7H0+h63ZS7Gva+1hHvsgULa1rVQ+gTxYqTY3NIYDKYzqBwI fzRaI4PNNk+2cp6UMT1Lu4uTyFGbP3XbwOevdkU/8JLQP3xSlCkIosJklode4suF/p03 oqsENECnxQgTnPbDX1ZAHKiGZImcoQW4CAAH5lbV17b7XbwgACLieYgNXUxW4tR96C4A 6Xgqa6AEj5k9HFnU3FJwZ9cL+KjbwQT0kNFTdlwEu/htMS7jCiH3EkYB6tBEtb6pLqnu C9SsjZMw9keRkNP9DLXKKzU2UoueHO9wi1npFU9PUtY8vZDlCqLn1QE5T0DapFck7GMp Ns8w== X-Gm-Message-State: AOAM533z8XytEFGg+WchASYik1tjDFw5uVKTfv+hi6mXaKfqg7a2y45O GgPjm3PeLpSufe186b6PRxQYqDry9tArcwLyWi9hOGuGMffXu4tN4VdFGOMZ/4cX5aDFWbwgVJ0 5YWgXlz7lLGJ9OnSSoiaFVzyc X-Received: by 2002:a5d:9151:: with SMTP id y17mr2084031ioq.38.1644442901150; Wed, 09 Feb 2022 13:41:41 -0800 (PST) X-Received: by 2002:a5d:9151:: with SMTP id y17mr2084013ioq.38.1644442900953; Wed, 09 Feb 2022 13:41:40 -0800 (PST) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id a6sm10552396iow.22.2022.02.09.13.41.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 13:41:40 -0800 (PST) Date: Wed, 9 Feb 2022 14:41:37 -0700 From: Alex Williamson To: Shameer Kolothum Cc: , , , , , , , , , , , Subject: Re: [RFC v4 5/8] hisi_acc_vfio_pci: Restrict access to VF dev BAR2 migration region Message-ID: <20220209144137.3770d914.alex.williamson@redhat.com> In-Reply-To: <20220208133425.1096-6-shameerali.kolothum.thodi@huawei.com> References: <20220208133425.1096-1-shameerali.kolothum.thodi@huawei.com> <20220208133425.1096-6-shameerali.kolothum.thodi@huawei.com> Organization: Red Hat MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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-kernel@vger.kernel.org On Tue, 8 Feb 2022 13:34:22 +0000 Shameer Kolothum wrote: > HiSilicon ACC VF device BAR2 region consists of both functional > register space and migration control register space. From a > security point of view, it's not advisable to export the migration > control region to Guest. > > Hence, override the ioctl/read/write/mmap methods to hide the > migration region and limit the access only to the functional register > space. > > Signed-off-by: Shameer Kolothum > --- > drivers/vfio/pci/hisi_acc_vfio_pci.c | 122 ++++++++++++++++++++++++++- > 1 file changed, 118 insertions(+), 4 deletions(-) > > diff --git a/drivers/vfio/pci/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisi_acc_vfio_pci.c > index 8b59e628110e..563ed2cc861f 100644 > --- a/drivers/vfio/pci/hisi_acc_vfio_pci.c > +++ b/drivers/vfio/pci/hisi_acc_vfio_pci.c > @@ -13,6 +13,120 @@ > #include > #include > > +static int hisi_acc_pci_rw_access_check(struct vfio_device *core_vdev, > + size_t count, loff_t *ppos, > + size_t *new_count) > +{ > + unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); > + struct vfio_pci_core_device *vdev = > + container_of(core_vdev, struct vfio_pci_core_device, vdev); > + > + if (index == VFIO_PCI_BAR2_REGION_INDEX) { > + loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; > + resource_size_t end = pci_resource_len(vdev->pdev, index) / 2; Be careful here, there are nested assignment use cases. This can only survive one level of assignment before we've restricted more than we intended. If migration support is dependent on PF access, can we use that to determine when to when to expose only half the BAR and when to expose the full BAR? We should also follow the mlx5 lead to use a vendor sub-directory below drivers/vfio/pci/ Thanks, Alex > + > + /* Check if access is for migration control region */ > + if (pos >= end) > + return -EINVAL; > + > + *new_count = min(count, (size_t)(end - pos)); > + } > + > + return 0; > +} > + > +static int hisi_acc_vfio_pci_mmap(struct vfio_device *core_vdev, > + struct vm_area_struct *vma) > +{ > + struct vfio_pci_core_device *vdev = > + container_of(core_vdev, struct vfio_pci_core_device, vdev); > + unsigned int index; > + > + index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); > + if (index == VFIO_PCI_BAR2_REGION_INDEX) { > + u64 req_len, pgoff, req_start; > + resource_size_t end = pci_resource_len(vdev->pdev, index) / 2; > + > + req_len = vma->vm_end - vma->vm_start; > + pgoff = vma->vm_pgoff & > + ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); > + req_start = pgoff << PAGE_SHIFT; > + > + if (req_start + req_len > end) > + return -EINVAL; > + } > + > + return vfio_pci_core_mmap(core_vdev, vma); > +} > + > +static ssize_t hisi_acc_vfio_pci_write(struct vfio_device *core_vdev, > + const char __user *buf, size_t count, > + loff_t *ppos) > +{ > + size_t new_count = count; > + int ret; > + > + ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count); > + if (ret) > + return ret; > + > + return vfio_pci_core_write(core_vdev, buf, new_count, ppos); > +} > + > +static ssize_t hisi_acc_vfio_pci_read(struct vfio_device *core_vdev, > + char __user *buf, size_t count, > + loff_t *ppos) > +{ > + size_t new_count = count; > + int ret; > + > + ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count); > + if (ret) > + return ret; > + > + return vfio_pci_core_read(core_vdev, buf, new_count, ppos); > +} > + > +static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int cmd, > + unsigned long arg) > +{ > + struct vfio_pci_core_device *vdev = > + container_of(core_vdev, struct vfio_pci_core_device, vdev); > + > + if (cmd == VFIO_DEVICE_GET_REGION_INFO) { > + struct pci_dev *pdev = vdev->pdev; > + struct vfio_region_info info; > + unsigned long minsz; > + > + minsz = offsetofend(struct vfio_region_info, offset); > + > + if (copy_from_user(&info, (void __user *)arg, minsz)) > + return -EFAULT; > + > + if (info.argsz < minsz) > + return -EINVAL; > + > + if (info.index == VFIO_PCI_BAR2_REGION_INDEX) { > + info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); > + > + /* > + * ACC VF dev BAR2 region consists of both functional > + * register space and migration control register space. > + * Report only the functional region to Guest. > + */ > + info.size = pci_resource_len(pdev, info.index) / 2; > + > + info.flags = VFIO_REGION_INFO_FLAG_READ | > + VFIO_REGION_INFO_FLAG_WRITE | > + VFIO_REGION_INFO_FLAG_MMAP; > + > + return copy_to_user((void __user *)arg, &info, minsz) ? > + -EFAULT : 0; > + } > + } > + return vfio_pci_core_ioctl(core_vdev, cmd, arg); > +} > + > static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev) > { > struct vfio_pci_core_device *vdev = > @@ -32,10 +146,10 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { > .name = "hisi-acc-vfio-pci", > .open_device = hisi_acc_vfio_pci_open_device, > .close_device = vfio_pci_core_close_device, > - .ioctl = vfio_pci_core_ioctl, > - .read = vfio_pci_core_read, > - .write = vfio_pci_core_write, > - .mmap = vfio_pci_core_mmap, > + .ioctl = hisi_acc_vfio_pci_ioctl, > + .read = hisi_acc_vfio_pci_read, > + .write = hisi_acc_vfio_pci_write, > + .mmap = hisi_acc_vfio_pci_mmap, > .request = vfio_pci_core_request, > .match = vfio_pci_core_match, > };