Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp720858img; Wed, 20 Mar 2019 09:27:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqze82c7C4Zdheq1TaYDl5V2OCQ8wnNWQVXAyI5ThCnYFfPyqALHMXDwrJshsCG4UqW1geHb X-Received: by 2002:a65:4608:: with SMTP id v8mr29806124pgq.9.1553099220242; Wed, 20 Mar 2019 09:27:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553099220; cv=none; d=google.com; s=arc-20160816; b=gmUqpzuKC1yWafyksWCRv7aT9Hl6DXRT/P5x6QETBNBKuBkI3eaGOLwsRrzjuBBJyI cdL/bNtqJxmgtMO9Q7TRPXFvSJDuoIKvr8cSXZ7ND8y6DtCjhDAeOk3hYmn+H3PNIGc+ CW0ZpOAdGdhQDGyP2hShOPuFlM1XKPRXu3xWS2c5yNAi2QqLd2P3JW3pJYsLWlp7nT5U v1o2yi/hBmIgyLOT3wfAzsuMqT/0RnZDjNYpD83TBAv9TTjv6xxUbytfqDkAX6OWY8nf 3pl6YLxlpyq1zdwXTGp5qfjTacLwVTDWRJWZHNSS/nfqncX00OCP1/vwSVvsE9gUlIIO /fcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BWGgVA8wfV8IsyDPwudh0PG78JbYDRsdRiYvEHSn/9Q=; b=Bkp/B80jLcMAcejokdWAhdm0Y/x3M6230UTfkJS5ZTFJIXFE/yyh6nsLCKAWbWHE+/ Qn+FHY43g57shBhpkCXcJ0ATfjFNSyYv/p1/y6uLZDcNorWJazY+vsQR3YMHJVYvfu+b Ok2HcKX4E22ehMUDa8XXiyWr8jQXROwShpYFxhtqxXLsnN3kSNmzA/nNk9c9QACga/kH pIvHdWmKIwXha1RxuEWy7wkVqBHLcD8vFqJdIYOkpfjyQ7dIZt/OzKTme1YL+y7eUhJU 6wdYlgpAaIrunnLM4Nvl+tdHAS3SKkGzAEldaJfy88R4gWFv+NiKn4Bu7tgFEa3rRPGP A4Mw== 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 p24si782149pfd.288.2019.03.20.09.26.44; Wed, 20 Mar 2019 09:27:00 -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 S1727836AbfCTQZN (ORCPT + 99 others); Wed, 20 Mar 2019 12:25:13 -0400 Received: from mga14.intel.com ([192.55.52.115]:52857 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726860AbfCTQZN (ORCPT ); Wed, 20 Mar 2019 12:25:13 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Mar 2019 09:25:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,249,1549958400"; d="scan'208";a="135715626" Received: from sorenthe-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.254.203]) by orsmga003.jf.intel.com with ESMTP; 20 Mar 2019 09:25:02 -0700 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org Cc: akpm@linux-foundation.org, dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, Jarkko Sakkinen Subject: [PATCH v19,RESEND 19/27] x86/sgx: ptrace() support for the SGX driver Date: Wed, 20 Mar 2019 18:21:11 +0200 Message-Id: <20190320162119.4469-20-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190320162119.4469-1-jarkko.sakkinen@linux.intel.com> References: <20190320162119.4469-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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); + 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