From patchwork Mon Oct 16 19:18:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 10009701 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C3FD160230 for ; Mon, 16 Oct 2017 19:19:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4F9C28505 for ; Mon, 16 Oct 2017 19:19:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA11D286A4; Mon, 16 Oct 2017 19:19:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5B6BB28505 for ; Mon, 16 Oct 2017 19:19:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id EEA32202E6180; Mon, 16 Oct 2017 12:16:01 -0700 (PDT) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received-SPF: None (no SPF record) identity=mailfrom; client-ip=134.134.136.31; helo=mga06.intel.com; envelope-from=jarkko.sakkinen@linux.intel.com; receiver=intel-sgx-kernel-dev@lists.01.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 29BBB202E6179 for ; Mon, 16 Oct 2017 12:16:01 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP; 16 Oct 2017 12:19:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.43,387,1503385200"; d="scan'208"; a="1231444268" Received: from mmalisze-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.254.122]) by fmsmga002.fm.intel.com with ESMTP; 16 Oct 2017 12:19:33 -0700 From: Jarkko Sakkinen To: intel-sgx-kernel-dev@lists.01.org Date: Mon, 16 Oct 2017 22:18:51 +0300 Message-Id: <20171016191855.16964-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171016191855.16964-1-jarkko.sakkinen@linux.intel.com> References: <20171016191855.16964-1-jarkko.sakkinen@linux.intel.com> Subject: [intel-sgx-kernel-dev] [PATCH v4 08/12] intel_sgx: ptrace() support X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: =?iso-8859-1?q?Project=3A_Intel=AE_Software_Guard_Extensions_for_Linux*=3A_https=3A//01=2Eorg/intel-software-guard-extensions?= List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org MIME-Version: 1.0 Errors-To: intel-sgx-kernel-dev-bounces@lists.01.org Sender: "intel-sgx-kernel-dev" X-Virus-Scanned: ClamAV using ClamSMTP Implemented VMA callbacks in order to ptrace() debug enclaves. Signed-off-by: Jarkko Sakkinen --- drivers/platform/x86/intel_sgx/sgx_vma.c | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/drivers/platform/x86/intel_sgx/sgx_vma.c b/drivers/platform/x86/intel_sgx/sgx_vma.c index 2b714daa6eb7..54b588ffa71c 100644 --- a/drivers/platform/x86/intel_sgx/sgx_vma.c +++ b/drivers/platform/x86/intel_sgx/sgx_vma.c @@ -110,8 +110,123 @@ static int sgx_vma_fault(struct vm_fault *vmf) return VM_FAULT_SIGBUS; } +static inline int sgx_vma_access_word(struct sgx_encl *encl, + unsigned long addr, + void *buf, + int len, + int write, + struct sgx_encl_page *encl_page, + int i) +{ + char data[sizeof(unsigned long)]; + int align, cnt, offset; + void *vaddr; + int ret; + + offset = ((addr + i) & (PAGE_SIZE - 1)) & ~(sizeof(unsigned long) - 1); + align = (addr + i) & (sizeof(unsigned long) - 1); + cnt = sizeof(unsigned long) - align; + cnt = min(cnt, len - i); + + if (write) { + if (encl_page->flags & SGX_ENCL_PAGE_TCS && + (offset < 8 || (offset + (len - i)) > 16)) + return -ECANCELED; + + if (align || (cnt != sizeof(unsigned long))) { + vaddr = sgx_get_page(encl_page->epc_page); + ret = __edbgrd((void *)((unsigned long)vaddr + offset), + (unsigned long *)data); + sgx_put_page(vaddr); + if (ret) { + sgx_dbg(encl, "EDBGRD returned %d\n", ret); + return -EFAULT; + } + } + + memcpy(data + align, buf + i, cnt); + vaddr = sgx_get_page(encl_page->epc_page); + ret = __edbgwr((void *)((unsigned long)vaddr + offset), + (unsigned long *)data); + sgx_put_page(vaddr); + if (ret) { + sgx_dbg(encl, "EDBGWR returned %d\n", ret); + return -EFAULT; + } + } else { + if (encl_page->flags & SGX_ENCL_PAGE_TCS && + (offset + (len - i)) > 72) + return -ECANCELED; + + vaddr = sgx_get_page(encl_page->epc_page); + ret = __edbgrd((void *)((unsigned long)vaddr + offset), + (unsigned long *)data); + sgx_put_page(vaddr); + if (ret) { + sgx_dbg(encl, "EDBGRD returned %d\n", ret); + return -EFAULT; + } + + memcpy(buf + i, data + align, cnt); + } + + return cnt; +} + +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; + const char *op_str = write ? "EDBGWR" : "EDBGRD"; + 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; + + sgx_dbg(encl, "%s addr=0x%lx, len=%d\n", op_str, addr, len); + + for (i = 0; i < len; i += ret) { + if (!entry || !((addr + i) & (PAGE_SIZE - 1))) { + if (entry) + entry->flags &= ~SGX_ENCL_PAGE_RESERVED; + + entry = sgx_fault_page(vma, (addr + i) & PAGE_MASK, + SGX_FAULT_RESERVE); + if (IS_ERR(entry)) { + ret = PTR_ERR(entry); + entry = NULL; + break; + } + } + + /* No locks are needed because used fields are immutable after + * intialization. + */ + ret = sgx_vma_access_word(encl, addr, buf, len, write, + entry, i); + if (ret < 0) + break; + } + + if (entry) + entry->flags &= ~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, };