Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp46748ybz; Tue, 21 Apr 2020 14:57:43 -0700 (PDT) X-Google-Smtp-Source: APiQypLaqvxjz/ZXJwjY1fLwEUNomL8V/KRPmFJDcEcK1CHHLoiNrkHjnkG1UJteoKZBeIJbUife X-Received: by 2002:a17:906:bfc9:: with SMTP id us9mr9016375ejb.84.1587506263455; Tue, 21 Apr 2020 14:57:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587506263; cv=none; d=google.com; s=arc-20160816; b=TVHruw5DjbRkWF8wyTEMyrOZ1Jvrp3UxbFU+zyQ+E72+Z8N1BStbQY8n25sqO3woBI fIfZSK61ETiBRrGqNoXQg9GUYp2hdAjm8WN3j6bj9zUqM/LO3AHNeeP/7MHE0jgD+V56 cl7Tgyd+e/YFWwX74Z78ZuVZ8M3wJ8Fu6j6/ZKKHzBYhTVUhs4BbcQooNyQbl6rtpKH5 AbgR8ec0xYlTrAcifg8WcYCAos5fhh5lNiukEt3vX6r686Pu7z6elWUXFfKXxrx/P7eO XWP/vamcch82xvwRCBQW1eAaCydGIaZxlgiaXZGI2wOKwBYGVVK4n3/P3nBNiMu+1kUc SLxQ== 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 :ironport-sdr:ironport-sdr; bh=toFOxo7of7bXlCAwoKwz6sypVefXpYNHj4lfIuSZQFo=; b=si60mxGGai2Om/nWESLNYUo6a5qgIWmrFoAhU0BamoKGFinECLTxM4wPv1IfQYJYMv r4N6cbUDl938XLWIRmDjKfKGOJiyn0Llzwj6ngaJajaSz+xUutGraRDWmKzcfr5wCN20 apXJKP3uyc9M5JZaUmbDqijFaXdFPw3Fg/lwTz6Nm36gMaKjLp5MqKsNporcS2piRZ7z bdHuTJz5AauoOKxMQGfde3MmvIAacHrEuLJNdcU/s3EN3LnBDCY5gOFPVpUpfi74sOC+ hlSdVNgw9FIu2lJ5VjO4zKUo6aSzUY94IwnQyVP+gmNZICUzS5bQeV3eHkthJCbgAr2T ApGg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id cm18si2182504edb.130.2020.04.21.14.57.20; Tue, 21 Apr 2020 14:57:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726661AbgDUVys (ORCPT + 99 others); Tue, 21 Apr 2020 17:54:48 -0400 Received: from mga12.intel.com ([192.55.52.136]:30191 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726648AbgDUVyp (ORCPT ); Tue, 21 Apr 2020 17:54:45 -0400 IronPort-SDR: 8arrQmvauPpIybtcLQi8Ybv3IwOqaw5VuFDMnJSoDW96E4cT8f3YzxYYFZdbZ5+SLK8u4A1Ave b84Y/56fQK+A== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2020 14:54:44 -0700 IronPort-SDR: CWDRjm4I3cnvaBEAki0NW5SQ2mA51g3xTb+TTOM4psprfSh+BLp9lF+dbvNzyuFgLKE67A2c5Z dOGOr8v7lKqw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,411,1580803200"; d="scan'208";a="456260785" Received: from mnchalux-mobl2.gar.corp.intel.com (HELO localhost) ([10.252.44.234]) by fmsmga005.fm.intel.com with ESMTP; 21 Apr 2020 14:54:40 -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, 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 v29 14/20] x86/sgx: ptrace() support for the SGX driver Date: Wed, 22 Apr 2020 00:53:10 +0300 Message-Id: <20200421215316.56503-15-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200421215316.56503-1-jarkko.sakkinen@linux.intel.com> References: <20200421215316.56503-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 fe7dbca40bb4..0c5ea2968868 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -326,6 +326,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) { @@ -333,10 +334,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.1