Received: by 2002:a4a:301c:0:0:0:0:0 with SMTP id q28-v6csp905933oof; Tue, 25 Sep 2018 06:15:01 -0700 (PDT) X-Google-Smtp-Source: ACcGV60N6WJEoElsQ8t8S4rnUaV2ftXKZF7jhR1LRPWCpDVrD9Y1eRfZIw2/xOICa2N7QUo0IWQx X-Received: by 2002:a17:902:4324:: with SMTP id i33-v6mr1166594pld.43.1537881301717; Tue, 25 Sep 2018 06:15:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537881301; cv=none; d=google.com; s=arc-20160816; b=CDG4oZCQaWEnLwaD5s8Nn2nCLSiH/gwjWviZBmrvll0uteioe10NJSnFMf0kHxz28L oXfTWuNPHx9D8/NrW8TIzcVftWUHRUDIiUntXRsUXWPUF2Ux7hSsGGUhsWjpLR1jjrKC UwI/iYWZQQtwra6Qw9e83Oo0iAzp27YmsT6SqqL9Jwwvzvw9dzVGkXFsU/ZBTlE5zZbe x2oKxY1sP9vciGhPhSNqOHxzkzZnw5uGp+QeVS44Q/XGGz6p5qRq0FqNEhb43j5dsgom sHlxWvGqDRuM9smIxRjeTmLRc9NLmX9H5br9WPd03Z2uX1+IWMYemGdhIgCZJt+oRT0r 25TQ== 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; bh=U09yHV65FRIpzRxRz30rTQm0cxquI+2+jnc4mbUK8K8=; b=pjTzBVqepEqO/QmmvsoMV/oQli65DBh5DOu+SG13EwZjFg6tqFiMxeRPWzQaRX7ZyJ 0iRh7s57d6qkOkdOj7YkzQY2URS7LVs30OAQ4Oeb1NX+i8hklht9oCIBE7UC8x5tZedR FQSMJzitvD7YABkEt5tzM+BL9ZzcE85dD63eb17qIOkFBooWnCX0QsU0IL2nE8XV3nje +SBKRRo1dd20A4/p1fgVKe+lIVUbdcpmf4SMfnmCUKWHyUVUSBbAo9PSLsF3EjKsTORl LJIGrAZqprKNKMOGsGuLmfbd6Gao/j6oTHO5uzkvozkb7eJ7xJVEl+pR9+k2bM2/zwWf sZhw== 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 q8-v6si2235029pfj.88.2018.09.25.06.14.45; Tue, 25 Sep 2018 06:15:01 -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 S1729466AbeIYTV3 (ORCPT + 99 others); Tue, 25 Sep 2018 15:21:29 -0400 Received: from mga09.intel.com ([134.134.136.24]:26431 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729111AbeIYTV1 (ORCPT ); Tue, 25 Sep 2018 15:21:27 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2018 06:13:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,302,1534834800"; d="scan'208";a="93547061" Received: from thomasvo-mobl2.ger.corp.intel.com (HELO localhost) ([10.252.53.212]) by orsmga001.jf.intel.com with ESMTP; 25 Sep 2018 06:13:20 -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, serge.ayoun@intel.com, shay.katz-zamir@intel.com, linux-sgx@vger.kernel.org, andriy.shevchenko@linux.intel.com, Jarkko Sakkinen , Darren Hart , Andy Shevchenko , Suresh Siddha , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v14 18/19] platform/x86: ptrace() support for the SGX driver Date: Tue, 25 Sep 2018 16:06:55 +0300 Message-Id: <20180925130845.9962-19-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180925130845.9962-1-jarkko.sakkinen@linux.intel.com> References: <20180925130845.9962-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 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 --- drivers/platform/x86/intel_sgx/sgx_vma.c | 109 +++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/drivers/platform/x86/intel_sgx/sgx_vma.c b/drivers/platform/x86/intel_sgx/sgx_vma.c index cc0993b4fd40..df604e4d0d0a 100644 --- a/drivers/platform/x86/intel_sgx/sgx_vma.c +++ b/drivers/platform/x86/intel_sgx/sgx_vma.c @@ -51,8 +51,117 @@ 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; + 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) { + 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; + 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) { + 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 : 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.1