Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3912881ybf; Tue, 3 Mar 2020 15:40:13 -0800 (PST) X-Google-Smtp-Source: ADFU+vtEB5a+96A4Z8e1jFvpDvmB/nYAbXNBKjR0zEvdThqLnGzO/Pqx35ycKrpsIsZuTNpwE3zY X-Received: by 2002:aca:b7d5:: with SMTP id h204mr21947oif.130.1583278813824; Tue, 03 Mar 2020 15:40:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583278813; cv=none; d=google.com; s=arc-20160816; b=hglHmP7LcoAbK90sYVaGn9Pc/B4Bo8dTuezs6QXVrLQIK2EPbRPCP9UAhpFgWO9Q9I EJp1TmMwREyo5TheC/1VcnuExo6NWJ1OUmSwK7T3chlWEOkVwtEF0U00YhQF5WWs3Pd9 LFPzLj69xtoHMXDguPhheKQLsnkdxZuFjKMY2/f9HaDjZZmSTHJWQraaYdBL47VeU3F0 U/c76Fi0Xy5jTus4qAjscBUX+dUjt9SWCXVQAUCHA4o0QUAsDIeM4TR09Wufvyql3OT3 8gQ4FtiaEffnx410lCEWbPzEXm3eqrIwCWDYc0BwGofo/Ef3qSHZHwMOhE4Z0x2AY27A 81nQ== 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=uQeqsZIhlxhQVDiwhX0G7ECMOibMhFS74+gnhicBM/w=; b=iSJWn62+rXFMGXPWvSbWNn/DZlhceA3KUHMui2JDgf6ANnIEcKDrRytWHxCGh1xmQh yx58+3pRpE5PkSAd9XVlzZCv5XPnln++YnDOD9yn6cysgTnH20MZJxaBk9Sqk10P9Giq Xa2jy5xC32nSzYNR40ZrVtNd4QgVZBnjKUCToNsB0R8d0H0mwikYzU6zLReBJ7npB4Rb byFjMEZSXjJxYIvY9NMkLeRWLU5q1vYdJF9VLnsFymUJoXWPvKLNcfAJeO+lUecGEW0+ eCmvlJ6aGlNqe7SYLXHT3hFaL9wNrJMLOVBMTsP2eQT+4m2c0UoVHU4PYGrFQRZuPuEs DsdA== 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 j20si167513oii.80.2020.03.03.15.40.02; Tue, 03 Mar 2020 15:40:13 -0800 (PST) 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 S1728692AbgCCXjj (ORCPT + 99 others); Tue, 3 Mar 2020 18:39:39 -0500 Received: from mga09.intel.com ([134.134.136.24]:14519 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728366AbgCCXjj (ORCPT ); Tue, 3 Mar 2020 18:39:39 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Mar 2020 15:39:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,511,1574150400"; d="scan'208";a="440790323" Received: from kwasilew-mobl.ger.corp.intel.com (HELO localhost) ([10.251.88.57]) by fmsmga006.fm.intel.com with ESMTP; 03 Mar 2020 15:39:30 -0800 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, 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, cedric.xing@intel.com, puiterwijk@redhat.com, Jarkko Sakkinen Subject: [PATCH v28 17/22] x86/sgx: ptrace() support for the SGX driver Date: Wed, 4 Mar 2020 01:36:04 +0200 Message-Id: <20200303233609.713348-18-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200303233609.713348-1-jarkko.sakkinen@linux.intel.com> References: <20200303233609.713348-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 | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index dc597fdfd0dd..d6a19bdd1921 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -327,6 +327,7 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start, return 0; } + static int sgx_vma_mprotect(struct vm_area_struct *vma, unsigned long start, unsigned long end, unsigned long prot) { @@ -334,10 +335,97 @@ static int sgx_vma_mprotect(struct vm_area_struct *vma, unsigned long start, calc_vm_prot_bits(prot, 0)); } +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset = addr & ~PAGE_MASK; + int ret; + + + 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 = addr & ~PAGE_MASK; + int ret; + + 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; + char data[sizeof(unsigned long)]; + unsigned long align; + unsigned int flags; + 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; + + flags = atomic_read(&encl->flags); + + if (!(flags & SGX_ENCL_DEBUG) || !(flags & SGX_ENCL_INITIALIZED) || + (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 = { .open = sgx_vma_open, .fault = sgx_vma_fault, .may_mprotect = sgx_vma_mprotect, + .access = sgx_vma_access, }; /** -- 2.25.0