Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1124557imm; Fri, 8 Jun 2018 10:23:28 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKoaqGoNG7oSOARDeiobOkd/JX3q60rW0Z1BCZeQ548lEw6KTx76tMUF5SkNBBAYsZP114i X-Received: by 2002:a17:902:8a4:: with SMTP id 33-v6mr7485976pll.343.1528478608448; Fri, 08 Jun 2018 10:23:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528478608; cv=none; d=google.com; s=arc-20160816; b=nqgh2P9hapk7+I3MzQIrr1G0G+iFAZt6+v5RZN8Ss5Ww9QpYWVfVi/k8A6QYPmSp7b zAgJn614PJGU40Z+ybKA4mSGNueMVSmMuMvv3o6OBo1KD+Sf5WfmyCYRQapp/0qNTYaw aYov6EH9N4SAt6ZDb3XlMTnBFixKPTuCGmyZReeTv5cBaWnTy+Hr3ZUskpDk9xNfrXW8 Qf58tvBlnwhr1u72Al+ZTzycSSoKBbrvv/YBa9eApwoIaISzZUdlU5d3tw6y5zj5PzA5 Jg8gXnZQ6G5TJ61JXF6R+row8SzbA1T3rg7TvzRx1Si71EjwgmJedrvNPMjQZj1XSow6 b1IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=kNaeVrcmwkYW/n+39egOWl5/4qgKobIXBmor7Uq6J3U=; b=a+4D1xcuFaSESo9CE9remV/Q46yZs1CCGhK4mEwUJaLw1JTAiD5nCm1nZXvzJC97Zl qCc4nFILxkcQK2n/vhhuu1v1VBo1CWHi5ikIcnI0HpjJJnmkgqam9hqGzv4064b/NtO7 0vkBYoMFdTdO21TgzOMkN937MkTwBrLpxINZdrjAQdQiSlTRaIl3JojTnp04JKXsfO0/ CuWJZiGNOv2/OpT8cEkpiivlto98XzDtZR8tQkaFcTY6MabtfLfJ2+WVYi9FYpKbxi4r QtmzZlOvmGXjHUfCZ99nULJ0R5kxAtsFE/KNrYHDOkliYNiWDPNFWUVqmVxd1FI6dJTp JyHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6-v6si30416684pgf.378.2018.06.08.10.23.14; Fri, 08 Jun 2018 10:23:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753224AbeFHRWM (ORCPT + 99 others); Fri, 8 Jun 2018 13:22:12 -0400 Received: from mga06.intel.com ([134.134.136.31]:7770 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753071AbeFHRWK (ORCPT ); Fri, 8 Jun 2018 13:22:10 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Jun 2018 10:22:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,490,1520924400"; d="scan'208";a="231035629" Received: from nzou1-mobl1.ccr.corp.intel.com (HELO localhost) ([10.249.254.60]) by orsmga005.jf.intel.com with ESMTP; 08 Jun 2018 10:22:03 -0700 From: Jarkko Sakkinen To: x86@kernel.org, platform-driver-x86@vger.kernel.org Cc: dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, Jarkko Sakkinen , Darren Hart , Andy Shevchenko , intel-sgx-kernel-dev@lists.01.org (open list:INTEL SGX), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v11 11/13] intel_sgx: ptrace() support Date: Fri, 8 Jun 2018 19:09:46 +0200 Message-Id: <20180608171216.26521-12-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180608171216.26521-1-jarkko.sakkinen@linux.intel.com> References: <20180608171216.26521-1-jarkko.sakkinen@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implemented VMA callbacks in order to ptrace() 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 Tested-by: Serge Ayoun --- drivers/platform/x86/intel_sgx/sgx_encl.c | 2 +- drivers/platform/x86/intel_sgx/sgx_vma.c | 116 ++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/drivers/platform/x86/intel_sgx/sgx_encl.c b/drivers/platform/x86/intel_sgx/sgx_encl.c index 562d4ce412d4..35436497530b 100644 --- a/drivers/platform/x86/intel_sgx/sgx_encl.c +++ b/drivers/platform/x86/intel_sgx/sgx_encl.c @@ -945,7 +945,7 @@ int sgx_encl_load_page(struct sgx_encl_page *encl_page, ret = __eldu(&pginfo, epc_ptr, va_ptr + va_offset); if (ret) { sgx_err(encl, "ELDU returned %d\n", ret); - ret = -EFAULT; + ret = ENCLS_TO_ERR(ret); } kunmap_atomic((void *)(unsigned long)(pginfo.pcmd - pcmd_offset)); diff --git a/drivers/platform/x86/intel_sgx/sgx_vma.c b/drivers/platform/x86/intel_sgx/sgx_vma.c index ad47383ea7f5..afc02d70c618 100644 --- a/drivers/platform/x86/intel_sgx/sgx_vma.c +++ b/drivers/platform/x86/intel_sgx/sgx_vma.c @@ -59,8 +59,124 @@ static int sgx_vma_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; } +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset; + void *ptr; + int ret; + + offset = addr & ~PAGE_MASK; + + if ((page->desc & SGX_ENCL_PAGE_TCS) && + offset > offsetof(struct sgx_tcs, gslimit)) + return -ECANCELED; + + ptr = sgx_get_page(page->epc_page); + ret = __edbgrd((unsigned long)ptr + offset, data); + sgx_put_page(ptr); + if (ret) { + sgx_dbg(encl, "EDBGRD returned %d\n", ret); + return ENCLS_TO_ERR(ret); + } + + return 0; +} + +static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset; + void *ptr; + 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; + + ptr = sgx_get_page(page->epc_page); + ret = __edbgwr((unsigned long)ptr + offset, data); + sgx_put_page(ptr); + if (ret) { + sgx_dbg(encl, "EDBGWR returned %d\n", ret); + return ENCLS_TO_ERR(ret); + } + + 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) { + if (!entry || !((addr + i) & (PAGE_SIZE - 1))) { + if (entry) + entry->desc &= ~SGX_ENCL_PAGE_RESERVED; + + entry = sgx_fault_page(vma, (addr + i) & PAGE_MASK, + true); + if (IS_ERR(entry)) { + ret = PTR_ERR(entry); + entry = NULL; + break; + } + } + + /* Locking is not needed because only immutable fields of the + * page are accessed and page itself is reserved so that it + * cannot be swapped out in the middle. + */ + + 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) + break; + if (write) { + memcpy(data + offset, buf + i, cnt); + ret = sgx_edbgwr(encl, entry, align, data); + if (ret) + break; + } + else + memcpy(buf + i,data + offset, cnt); + } + + if (entry) + entry->desc &= ~SGX_ENCL_PAGE_RESERVED; + + return (ret < 0 && ret != -ECANCELED) ? 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, }; -- 2.17.0