diff mbox series

[v19,19/27] x86/sgx: ptrace() support for the SGX driver

Message ID 20190317211456.13927-20-jarkko.sakkinen@linux.intel.com (mailing list archive)
State New, archived
Headers show
Series Intel SGX1 support | expand

Commit Message

Jarkko Sakkinen March 17, 2019, 9:14 p.m. UTC
Add VMA callbacks for ptrace() that can be used with debug enclaves.
With debug enclaves data can be read and write the memory word at a time
by using ENCLS(EDBGRD) and ENCLS(EDBGWR) leaf instructions.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 arch/x86/kernel/cpu/sgx/encl.c | 97 ++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

Comments

Sean Christopherson March 19, 2019, 10:22 p.m. UTC | #1
On Sun, Mar 17, 2019 at 11:14:48PM +0200, Jarkko Sakkinen wrote:
> Add VMA callbacks for ptrace() that can be used with debug enclaves.
> With debug enclaves data can be read and write the memory word at a time
> by using ENCLS(EDBGRD) and ENCLS(EDBGWR) leaf instructions.
> 
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> ---
>  arch/x86/kernel/cpu/sgx/encl.c | 97 ++++++++++++++++++++++++++++++++++
>  1 file changed, 97 insertions(+)
> 
> diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
> index 1b8874699dd3..5b5fc933ee19 100644
> --- a/arch/x86/kernel/cpu/sgx/encl.c
> +++ b/arch/x86/kernel/cpu/sgx/encl.c
> @@ -256,10 +256,107 @@ static unsigned int sgx_vma_fault(struct vm_fault *vmf)
>  	return ret;
>  }
>  
> +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page,
> +		      unsigned long addr, void *data)
> +{
> +	unsigned long offset;
> +	int ret;
> +
> +	offset = addr & ~PAGE_MASK;
> +
> +	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
> +	    offset > offsetof(struct sgx_tcs, gs_limit))
> +		return -ECANCELED;
> +
> +	ret = __edbgrd(sgx_epc_addr(page->epc_page) + offset, data);
> +	if (ret)
> +		return -EIO;
> +
> +	return 0;
> +}
> +
> +static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page,
> +		      unsigned long addr, void *data)
> +{
> +	unsigned long offset;
> +	int ret;
> +
> +	offset = addr & ~PAGE_MASK;
> +
> +	/* Writing anything else than flags will cause #GP */
> +	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
> +	    offset != offsetof(struct sgx_tcs, flags))
> +		return -ECANCELED;
> +
> +	ret = __edbgwr(sgx_epc_addr(page->epc_page) + offset, data);
> +	if (ret)
> +		return -EIO;
> +
> +	return 0;
> +}
> +
> +static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
> +			  void *buf, int len, int write)
> +{
> +	struct sgx_encl *encl = vma->vm_private_data;
> +	struct sgx_encl_page *entry = NULL;
> +	unsigned long align;
> +	char data[sizeof(unsigned long)];
> +	int offset;
> +	int cnt;
> +	int ret = 0;
> +	int i;
> +
> +	/* If process was forked, VMA is still there but vm_private_data is set
> +	 * to NULL.
> +	 */
> +	if (!encl)
> +		return -EFAULT;
> +
> +	if (!(encl->flags & SGX_ENCL_DEBUG) ||
> +	    !(encl->flags & SGX_ENCL_INITIALIZED) ||
> +	    (encl->flags & SGX_ENCL_DEAD))
> +		return -EFAULT;
> +
> +	for (i = 0; i < len; i += cnt) {
> +		entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK);

Taking a lock indirectly via sgx_encl_reserve_page() and releasing it
directly via mutex_unlock() is a bit difficult to follow, having a helper
to pair up with sgx_encl_reserve_page() would be more readable.  Open
coding sgx_encl_reserve_page() would probably be even better since this
is the only user AFAICT.

All that being said, I prefer the old approach of marking the page RESERVED
instead of holding the enclave's lock, even though it was slightly more
complex.

> +		if (IS_ERR(entry)) {
> +			ret = PTR_ERR(entry);
> +			break;
> +		}
> +
> +		align = ALIGN_DOWN(addr + i, sizeof(unsigned long));
> +		offset = (addr + i) & (sizeof(unsigned long) - 1);
> +		cnt = sizeof(unsigned long) - offset;
> +		cnt = min(cnt, len - i);
> +
> +		ret = sgx_edbgrd(encl, entry, align, data);
> +		if (ret)
> +			goto out;
> +
> +		if (write) {
> +			memcpy(data + offset, buf + i, cnt);
> +			ret = sgx_edbgwr(encl, entry, align, data);
> +			if (ret)
> +				goto out;
> +		} else
> +			memcpy(buf + i, data + offset, cnt);
> +
> +out:
> +		mutex_unlock(&encl->lock);
> +
> +		if (ret)
> +			break;
> +	}
> +
> +	return ret < 0 ? ret : i;
> +}
> +
>  const struct vm_operations_struct sgx_vm_ops = {
>  	.close = sgx_vma_close,
>  	.open = sgx_vma_open,
>  	.fault = sgx_vma_fault,
> +	.access = sgx_vma_access,
>  };
>  EXPORT_SYMBOL_GPL(sgx_vm_ops);
>  
> -- 
> 2.19.1
>
Jarkko Sakkinen March 21, 2019, 3:02 p.m. UTC | #2
On Tue, Mar 19, 2019 at 03:22:01PM -0700, Sean Christopherson wrote:
> On Sun, Mar 17, 2019 at 11:14:48PM +0200, Jarkko Sakkinen wrote:
> > Add VMA callbacks for ptrace() that can be used with debug enclaves.
> > With debug enclaves data can be read and write the memory word at a time
> > by using ENCLS(EDBGRD) and ENCLS(EDBGWR) leaf instructions.
> > 
> > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> > ---
> >  arch/x86/kernel/cpu/sgx/encl.c | 97 ++++++++++++++++++++++++++++++++++
> >  1 file changed, 97 insertions(+)
> > 
> > diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
> > index 1b8874699dd3..5b5fc933ee19 100644
> > --- a/arch/x86/kernel/cpu/sgx/encl.c
> > +++ b/arch/x86/kernel/cpu/sgx/encl.c
> > @@ -256,10 +256,107 @@ static unsigned int sgx_vma_fault(struct vm_fault *vmf)
> >  	return ret;
> >  }
> >  
> > +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page,
> > +		      unsigned long addr, void *data)
> > +{
> > +	unsigned long offset;
> > +	int ret;
> > +
> > +	offset = addr & ~PAGE_MASK;
> > +
> > +	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
> > +	    offset > offsetof(struct sgx_tcs, gs_limit))
> > +		return -ECANCELED;
> > +
> > +	ret = __edbgrd(sgx_epc_addr(page->epc_page) + offset, data);
> > +	if (ret)
> > +		return -EIO;
> > +
> > +	return 0;
> > +}
> > +
> > +static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page,
> > +		      unsigned long addr, void *data)
> > +{
> > +	unsigned long offset;
> > +	int ret;
> > +
> > +	offset = addr & ~PAGE_MASK;
> > +
> > +	/* Writing anything else than flags will cause #GP */
> > +	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
> > +	    offset != offsetof(struct sgx_tcs, flags))
> > +		return -ECANCELED;
> > +
> > +	ret = __edbgwr(sgx_epc_addr(page->epc_page) + offset, data);
> > +	if (ret)
> > +		return -EIO;
> > +
> > +	return 0;
> > +}
> > +
> > +static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
> > +			  void *buf, int len, int write)
> > +{
> > +	struct sgx_encl *encl = vma->vm_private_data;
> > +	struct sgx_encl_page *entry = NULL;
> > +	unsigned long align;
> > +	char data[sizeof(unsigned long)];
> > +	int offset;
> > +	int cnt;
> > +	int ret = 0;
> > +	int i;
> > +
> > +	/* If process was forked, VMA is still there but vm_private_data is set
> > +	 * to NULL.
> > +	 */
> > +	if (!encl)
> > +		return -EFAULT;
> > +
> > +	if (!(encl->flags & SGX_ENCL_DEBUG) ||
> > +	    !(encl->flags & SGX_ENCL_INITIALIZED) ||
> > +	    (encl->flags & SGX_ENCL_DEAD))
> > +		return -EFAULT;
> > +
> > +	for (i = 0; i < len; i += cnt) {
> > +		entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK);
> 
> Taking a lock indirectly via sgx_encl_reserve_page() and releasing it
> directly via mutex_unlock() is a bit difficult to follow, having a helper
> to pair up with sgx_encl_reserve_page() would be more readable.  Open
> coding sgx_encl_reserve_page() would probably be even better since this
> is the only user AFAICT.
> 
> All that being said, I prefer the old approach of marking the page RESERVED
> instead of holding the enclave's lock, even though it was slightly more
> complex.

For me the big rationale is to minimize the amount of state in the
context of this patch set. And this approach should work just fine.

/Jarkko
diff mbox series

Patch

diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index 1b8874699dd3..5b5fc933ee19 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -256,10 +256,107 @@  static unsigned int sgx_vma_fault(struct vm_fault *vmf)
 	return ret;
 }
 
+static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page,
+		      unsigned long addr, void *data)
+{
+	unsigned long offset;
+	int ret;
+
+	offset = addr & ~PAGE_MASK;
+
+	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
+	    offset > offsetof(struct sgx_tcs, gs_limit))
+		return -ECANCELED;
+
+	ret = __edbgrd(sgx_epc_addr(page->epc_page) + offset, data);
+	if (ret)
+		return -EIO;
+
+	return 0;
+}
+
+static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page,
+		      unsigned long addr, void *data)
+{
+	unsigned long offset;
+	int ret;
+
+	offset = addr & ~PAGE_MASK;
+
+	/* Writing anything else than flags will cause #GP */
+	if ((page->desc & SGX_ENCL_PAGE_TCS) &&
+	    offset != offsetof(struct sgx_tcs, flags))
+		return -ECANCELED;
+
+	ret = __edbgwr(sgx_epc_addr(page->epc_page) + offset, data);
+	if (ret)
+		return -EIO;
+
+	return 0;
+}
+
+static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
+			  void *buf, int len, int write)
+{
+	struct sgx_encl *encl = vma->vm_private_data;
+	struct sgx_encl_page *entry = NULL;
+	unsigned long align;
+	char data[sizeof(unsigned long)];
+	int offset;
+	int cnt;
+	int ret = 0;
+	int i;
+
+	/* If process was forked, VMA is still there but vm_private_data is set
+	 * to NULL.
+	 */
+	if (!encl)
+		return -EFAULT;
+
+	if (!(encl->flags & SGX_ENCL_DEBUG) ||
+	    !(encl->flags & SGX_ENCL_INITIALIZED) ||
+	    (encl->flags & SGX_ENCL_DEAD))
+		return -EFAULT;
+
+	for (i = 0; i < len; i += cnt) {
+		entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK);
+		if (IS_ERR(entry)) {
+			ret = PTR_ERR(entry);
+			break;
+		}
+
+		align = ALIGN_DOWN(addr + i, sizeof(unsigned long));
+		offset = (addr + i) & (sizeof(unsigned long) - 1);
+		cnt = sizeof(unsigned long) - offset;
+		cnt = min(cnt, len - i);
+
+		ret = sgx_edbgrd(encl, entry, align, data);
+		if (ret)
+			goto out;
+
+		if (write) {
+			memcpy(data + offset, buf + i, cnt);
+			ret = sgx_edbgwr(encl, entry, align, data);
+			if (ret)
+				goto out;
+		} else
+			memcpy(buf + i, data + offset, cnt);
+
+out:
+		mutex_unlock(&encl->lock);
+
+		if (ret)
+			break;
+	}
+
+	return ret < 0 ? ret : i;
+}
+
 const struct vm_operations_struct sgx_vm_ops = {
 	.close = sgx_vma_close,
 	.open = sgx_vma_open,
 	.fault = sgx_vma_fault,
+	.access = sgx_vma_access,
 };
 EXPORT_SYMBOL_GPL(sgx_vm_ops);