Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

nitro_enclaves: Sanity check physical memory regions during merging

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.

Reviewed-by: Andra Paraschiv <andraprs@amazon.com>
Signed-off-by: Longpeng <longpeng2@huawei.com>
Link: https://lore.kernel.org/r/20211107140918.2106-3-longpeng2@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Longpeng and committed by
Greg Kroah-Hartman
090ce783 f6bdc0aa

+52 -25
+52 -25
drivers/virt/nitro_enclaves/ne_misc_dev.c
··· 836 836 } 837 837 838 838 /** 839 + * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size 840 + * of a physical memory region. 841 + * @phys_mem_region_paddr : Physical start address of the region to be sanity checked. 842 + * @phys_mem_region_size : Length of the region to be sanity checked. 843 + * 844 + * Context: Process context. This function is called with the ne_enclave mutex held. 845 + * Return: 846 + * * 0 on success. 847 + * * Negative return value on failure. 848 + */ 849 + static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr, 850 + u64 phys_mem_region_size) 851 + { 852 + if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) { 853 + dev_err_ratelimited(ne_misc_dev.this_device, 854 + "Physical mem region size is not multiple of 2 MiB\n"); 855 + 856 + return -EINVAL; 857 + } 858 + 859 + if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) { 860 + dev_err_ratelimited(ne_misc_dev.this_device, 861 + "Physical mem region address is not 2 MiB aligned\n"); 862 + 863 + return -EINVAL; 864 + } 865 + 866 + return 0; 867 + } 868 + 869 + /** 839 870 * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent 840 871 * regions if they are physically contiguous. 841 872 * @phys_contig_regions : Private data associated with the contiguous physical memory regions. ··· 874 843 * @page_size : Length of the region to be added. 875 844 * 876 845 * Context: Process context. This function is called with the ne_enclave mutex held. 846 + * Return: 847 + * * 0 on success. 848 + * * Negative return value on failure. 877 849 */ 878 - static void 850 + static int 879 851 ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions *phys_contig_regions, 880 852 u64 page_paddr, u64 page_size) 881 853 { 882 854 unsigned long num = phys_contig_regions->num; 855 + int rc = 0; 856 + 857 + rc = ne_sanity_check_phys_mem_region(page_paddr, page_size); 858 + if (rc < 0) 859 + return rc; 883 860 884 861 /* Physically contiguous, just merge */ 885 862 if (num && (phys_contig_regions->regions[num - 1].end + 1) == page_paddr) { 886 863 phys_contig_regions->regions[num - 1].end += page_size; 887 - 888 - return; 864 + } else { 865 + phys_contig_regions->regions[num].start = page_paddr; 866 + phys_contig_regions->regions[num].end = page_paddr + page_size - 1; 867 + phys_contig_regions->num++; 889 868 } 890 869 891 - phys_contig_regions->regions[num].start = page_paddr; 892 - phys_contig_regions->regions[num].end = page_paddr + page_size - 1; 893 - phys_contig_regions->num++; 870 + return 0; 894 871 } 895 872 896 873 /** ··· 978 939 if (rc < 0) 979 940 goto put_pages; 980 941 981 - ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions, 982 - page_to_phys(ne_mem_region->pages[i]), 983 - page_size(ne_mem_region->pages[i])); 942 + rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions, 943 + page_to_phys(ne_mem_region->pages[i]), 944 + page_size(ne_mem_region->pages[i])); 945 + if (rc < 0) 946 + goto put_pages; 984 947 985 948 memory_size += page_size(ne_mem_region->pages[i]); 986 949 ··· 1004 963 u64 phys_region_addr = phys_contig_mem_regions.regions[i].start; 1005 964 u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]); 1006 965 1007 - if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) { 1008 - dev_err_ratelimited(ne_misc_dev.this_device, 1009 - "Physical mem region size is not multiple of 2 MiB\n"); 1010 - 1011 - rc = -EINVAL; 1012 - 966 + rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size); 967 + if (rc < 0) 1013 968 goto put_pages; 1014 - } 1015 - 1016 - if (!IS_ALIGNED(phys_region_addr, NE_MIN_MEM_REGION_SIZE)) { 1017 - dev_err_ratelimited(ne_misc_dev.this_device, 1018 - "Physical mem region address is not 2 MiB aligned\n"); 1019 - 1020 - rc = -EINVAL; 1021 - 1022 - goto put_pages; 1023 - } 1024 969 } 1025 970 1026 971 ne_mem_region->memory_size = mem_region.memory_size;