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 || !IS_ALIGNED(length, PAGE_SIZE))
+               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
        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,