Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp458957pxb; Wed, 3 Nov 2021 07:04:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/UgabkDiDb4errpjGki9/BKRxPS3XIWtQ7OuFCUHZfUkgf3WfgX7yXJZ+O2ocNEkuYGKU X-Received: by 2002:a05:6e02:2143:: with SMTP id d3mr13606051ilv.241.1635948297426; Wed, 03 Nov 2021 07:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635948297; cv=none; d=google.com; s=arc-20160816; b=qUdhA2e4VWMiTzZMa0SeCs2uMagD66yNoKx8FiqQ8yu0njnpWbujlHwiuWVYe7rIbV U1my2d3XvYLA/6VCRhtlKbZSOL5stcH007WWbWUVL1Scqqjzp8jAilxBHfP0tzisxthY 0iSmDwT1G/7WFvVt9dDRoK/p/ON0dFauTl35Ex0GPhuH8pQZa0mXZiMpS0iMPI/OW0wo A4fGrbQVI5PCcCMqcfbVJyeJpeFdH0UGj++XcQ8Gh5doiCEYu75lvLCzDeeVbDLjCMea onsb3n+41mSRhemkRI9M9KxY7QoXEVk9PKKWk7vaVsEjNYyiclBW07gSPAtyJcXWMvt4 PWFA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=EeDWaAm51W5HxNGV9yxjWP8VguR3w885SYqcINQeWlg=; b=m1gJRhmVnffX7PrffIu7bYP6gJLDsFPk1GnOQYb3dH41m3IPc3768yqQskDULBm9KI o6DSIvMDxMAM5iZ9DXvL1mEGirLtDXISdMFSFXZDkLX1RHght8676edG2bNHBZ+jZdYM dYx8pSp9eH2wEDBsmFt3tHOvMYFnWuMwB6AisQKft8Jxr2eZwRMQGRFkqt25lxxZd3cB GPYwbnvyiVNO2+Y5W4yKixz9hliDiW6Yv7M6StfYd76EZK6//ZS5u+69rzmBF7mcvFaP +7M0B0mqguOUpyCDyzdRxBVTULj4NYD4COdJd509uTHkt9RODOIG2FY2p8Wl1csNhBS7 hwsA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e9si2980309ilm.6.2021.11.03.07.04.37; Wed, 03 Nov 2021 07:04:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232065AbhKCODy (ORCPT + 99 others); Wed, 3 Nov 2021 10:03:54 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:25356 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232123AbhKCODf (ORCPT ); Wed, 3 Nov 2021 10:03:35 -0400 Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HkpG141WfzbhYV; Wed, 3 Nov 2021 21:56:09 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 3 Nov 2021 22:00:40 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 3 Nov 2021 22:00:39 +0800 From: "Longpeng(Mike)" To: , , CC: , , , , , , , , , Longpeng Subject: [PATCH v4 2/4] nitro_enclaves: Sanity check physical memory regions during merging Date: Wed, 3 Nov 2021 22:00:33 +0800 Message-ID: <20211103140035.2001-3-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211103140035.2001-1-longpeng2@huawei.com> References: <20211103140035.2001-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Longpeng Sanity check the physical memory regions during the merge of contiguous regions. Thus we can test the physical memory regions setup logic individually, including the error cases coming from the sanity checks. Signed-off-by: Longpeng --- Changes v3 -> v4: - add missing "Context" in the comments. [Andra] - move the comment in ne_merge_phys_contig_memory_regions() before the "if (...)". [Andra] Changes v2 -> v3: - update the commit title and commit message. [Andra] - add comments before the function definition. [Andra] - remove 'inline' attribute of ne_sanity_check_phys_mem_region. [Andra] - leave a blank line before return. [Andra] - move sanity check in ne_merge_phys_contig_memory_regions to the beginning of the function. [Andra] - double sanity checking after the merge of physical contiguous memory regions has been completed. [Andra] --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 77 +++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index b7d2116..3741ae7 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -836,6 +836,37 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, } /** + * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size + * of a physical memory region. + * @phys_mem_region_paddr : Physical start address of the region to be sanity checked. + * @phys_mem_region_size : Length of the region to be sanity checked. + * + * Context: Process context. This function is called with the ne_enclave mutex held. + * Return: + * * 0 on success. + * * Negative return value on failure. + */ +static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr, + u64 phys_mem_region_size) +{ + if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) { + dev_err_ratelimited(ne_misc_dev.this_device, + "Physical mem region size is not multiple of 2 MiB\n"); + + return -EINVAL; + } + + if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) { + dev_err_ratelimited(ne_misc_dev.this_device, + "Physical mem region address is not 2 MiB aligned\n"); + + return -EINVAL; + } + + return 0; +} + +/** * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent * regions if they are physically contiguous. * @phys_contig_regions : Private data associated with the contiguous physical memory regions. @@ -843,23 +874,31 @@ static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave, * @page_size : Length of the region to be added. * * Context: Process context. This function is called with the ne_enclave mutex held. + * Return: + * * 0 on success. + * * Negative return value on failure. */ -static void +static int ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions *phys_contig_regions, u64 page_paddr, u64 page_size) { unsigned long num = phys_contig_regions->num; + int rc = 0; + + rc = ne_sanity_check_phys_mem_region(page_paddr, page_size); + if (rc < 0) + return rc; /* Physically contiguous, just merge */ if (num && (phys_contig_regions->regions[num - 1].end + 1) == page_paddr) { phys_contig_regions->regions[num - 1].end += page_size; - - return; + } else { + phys_contig_regions->regions[num].start = page_paddr; + phys_contig_regions->regions[num].end = page_paddr + page_size - 1; + phys_contig_regions->num++; } - phys_contig_regions->regions[num].start = page_paddr; - phys_contig_regions->regions[num].end = page_paddr + page_size - 1; - phys_contig_regions->num++; + return 0; } /** @@ -938,9 +977,11 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, if (rc < 0) goto put_pages; - ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions, - page_to_phys(ne_mem_region->pages[i]), - page_size(ne_mem_region->pages[i])); + rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions, + page_to_phys(ne_mem_region->pages[i]), + page_size(ne_mem_region->pages[i])); + if (rc < 0) + goto put_pages; memory_size += page_size(ne_mem_region->pages[i]); @@ -962,23 +1003,9 @@ static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave, u64 phys_region_addr = phys_contig_mem_regions.regions[i].start; u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]); - if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) { - dev_err_ratelimited(ne_misc_dev.this_device, - "Physical mem region size is not multiple of 2 MiB\n"); - - rc = -EINVAL; - - goto put_pages; - } - - if (!IS_ALIGNED(phys_region_addr, NE_MIN_MEM_REGION_SIZE)) { - dev_err_ratelimited(ne_misc_dev.this_device, - "Physical mem region address is not 2 MiB aligned\n"); - - rc = -EINVAL; - + rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size); + if (rc < 0) goto put_pages; - } } ne_mem_region->memory_size = mem_region.memory_size; -- 1.8.3.1