diff mbox series

[V3,10/30] x86/sgx: Create utility to validate user provided offset and length

Message ID 7ebe0aa8cc6ce11eaf032c3796823bcc08df27fe.1648847675.git.reinette.chatre@intel.com (mailing list archive)
State New, archived
Headers show
Series x86/sgx and selftests/sgx: Support SGX2 | expand

Commit Message

Reinette Chatre April 4, 2022, 4:49 p.m. UTC
User provided offset and length is validated when parsing the parameters
of the SGX_IOC_ENCLAVE_ADD_PAGES ioctl(). Extract this validation
into a utility that can be used by the SGX2 ioctl()s that will
also provide these values.

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
---
No changes since V2

Changes since V1:
- New patch

 arch/x86/kernel/cpu/sgx/ioctl.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

Comments

Jarkko Sakkinen April 5, 2022, 7 a.m. UTC | #1
On Mon, Apr 04, 2022 at 09:49:18AM -0700, Reinette Chatre wrote:
> User provided offset and length is validated when parsing the parameters
> of the SGX_IOC_ENCLAVE_ADD_PAGES ioctl(). Extract this validation
> into a utility that can be used by the SGX2 ioctl()s that will
> also provide these values.
> 
> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
> ---
> No changes since V2
> 
> Changes since V1:
> - New patch
> 
>  arch/x86/kernel/cpu/sgx/ioctl.c | 28 ++++++++++++++++++++++------
>  1 file changed, 22 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
> index 83df20e3e633..f487549bccba 100644
> --- a/arch/x86/kernel/cpu/sgx/ioctl.c
> +++ b/arch/x86/kernel/cpu/sgx/ioctl.c
> @@ -372,6 +372,26 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
>  	return ret;
>  }
>  
> +/*
> + * Ensure user provided offset and length values are valid for
> + * an enclave.
> + */
> +static int sgx_validate_offset_length(struct sgx_encl *encl,
> +				      unsigned long offset,
> +				      unsigned long length)
> +{
> +	if (!IS_ALIGNED(offset, PAGE_SIZE))
> +		return -EINVAL;
> +
> +	if (!length || length & (PAGE_SIZE - 1))
> +		return -EINVAL;
> +
> +	if (offset + length - PAGE_SIZE >= encl->size)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
>  /**
>   * sgx_ioc_enclave_add_pages() - The handler for %SGX_IOC_ENCLAVE_ADD_PAGES
>   * @encl:       an enclave pointer
> @@ -425,14 +445,10 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)
>  	if (copy_from_user(&add_arg, arg, sizeof(add_arg)))
>  		return -EFAULT;
>  
> -	if (!IS_ALIGNED(add_arg.offset, PAGE_SIZE) ||
> -	    !IS_ALIGNED(add_arg.src, PAGE_SIZE))
> -		return -EINVAL;
> -
> -	if (!add_arg.length || add_arg.length & (PAGE_SIZE - 1))
> +	if (!IS_ALIGNED(add_arg.src, PAGE_SIZE))
>  		return -EINVAL;
>  
> -	if (add_arg.offset + add_arg.length - PAGE_SIZE >= encl->size)
> +	if (sgx_validate_offset_length(encl, add_arg.offset, add_arg.length))
>  		return -EINVAL;
>  
>  	if (copy_from_user(&secinfo, (void __user *)add_arg.secinfo,
> -- 
> 2.25.1
> 


Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko
diff mbox series

Patch

diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index 83df20e3e633..f487549bccba 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -372,6 +372,26 @@  static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 	return ret;
 }
 
+/*
+ * Ensure user provided offset and length values are valid for
+ * an enclave.
+ */
+static int sgx_validate_offset_length(struct sgx_encl *encl,
+				      unsigned long offset,
+				      unsigned long length)
+{
+	if (!IS_ALIGNED(offset, PAGE_SIZE))
+		return -EINVAL;
+
+	if (!length || length & (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	if (offset + length - PAGE_SIZE >= encl->size)
+		return -EINVAL;
+
+	return 0;
+}
+
 /**
  * sgx_ioc_enclave_add_pages() - The handler for %SGX_IOC_ENCLAVE_ADD_PAGES
  * @encl:       an enclave pointer
@@ -425,14 +445,10 @@  static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)
 	if (copy_from_user(&add_arg, arg, sizeof(add_arg)))
 		return -EFAULT;
 
-	if (!IS_ALIGNED(add_arg.offset, PAGE_SIZE) ||
-	    !IS_ALIGNED(add_arg.src, PAGE_SIZE))
-		return -EINVAL;
-
-	if (!add_arg.length || add_arg.length & (PAGE_SIZE - 1))
+	if (!IS_ALIGNED(add_arg.src, PAGE_SIZE))
 		return -EINVAL;
 
-	if (add_arg.offset + add_arg.length - PAGE_SIZE >= encl->size)
+	if (sgx_validate_offset_length(encl, add_arg.offset, add_arg.length))
 		return -EINVAL;
 
 	if (copy_from_user(&secinfo, (void __user *)add_arg.secinfo,