Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1236075ybh; Thu, 16 Jul 2020 06:58:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfQwOXnttFmCY4yPlJzG/WJ0hSJ21XAsScGfoBNYctJGVRRD9u8jNvnhS7nkdJVdnTntrm X-Received: by 2002:a17:906:8595:: with SMTP id v21mr3947971ejx.333.1594907909898; Thu, 16 Jul 2020 06:58:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594907909; cv=none; d=google.com; s=arc-20160816; b=qy6fMIqlyE8kPgiUrvOYWrtjvZ8eddPnriIDOPzLuAfO4e19o9fmuaT/7MBDEUc2tB 5eLdNB0Yw8OvjbJ/r7EFnQEyBqhNMQEuzFXKw7i8YeKmmDxstataf9IoHtuTbnb1d/AZ Ip0wjWfXP/1G6lQpryCqZ2n1C4aRPJ9zK8bl9jwho+NyILFgklU+kTsggjaxWoVUhjgc CLnIAHCdBwm+i7yhW8cqmlk6I1fv04UBx3Wb28x0QrF6KxkuJURpm+6DRZz21/Pm0TYd mpPk2gRPOG5B7GP3Fia81zRcvaSKwaCxevF5AhNOjXM+6uCy3RBGH1PLJmrg0YimzO5m N9Ww== 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=fo+R5KIBUVdPei9mCSZ808zCJXMqb++mMTI3DtQggrY=; b=mjrOlOhgJAPi9vW4/89Wezbq8leK++oMycCORoonHdJH4nKO0sCv35jhaW5rV4CkjZ QWxxbEmHJJXaqXFx8UNP32PCqKA7c++zz2tXQ5ziQZHh4coYk7xp+sEXDp6KJTulpFy9 lwBujhNMfw08q92S1SWu05/R5i75qTkjAvRD+Rx5Kz9+EGqhhHpYkt5z046N8UxtQhXy XEJtzS3WGqabzv7uvqqEqHKHhKyjYUGfUZnfNkaRnW+dlhJbAv4YiJiWWg5FowlyaO4E R21DwmraPzatayXZurOLilv8/a9tKO7bM51u3SzX4N1rFBd3OuMXnuXoNY40AX4UX+Lp Gefw== 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 z7si3126281ejf.367.2020.07.16.06.58.07; Thu, 16 Jul 2020 06:58:29 -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 S1728927AbgGPN5d (ORCPT + 99 others); Thu, 16 Jul 2020 09:57:33 -0400 Received: from mga17.intel.com ([192.55.52.151]:45684 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728093AbgGPN5d (ORCPT ); Thu, 16 Jul 2020 09:57:33 -0400 IronPort-SDR: uR83mvQR0BDb3xukE/99dXB6i/Dsq6wgalzrlVS51fA+bAl7zN4SjVydFZvASw/p9Pdft8ev0y 36WzxJe7eJ3Q== X-IronPort-AV: E=McAfee;i="6000,8403,9683"; a="129449677" X-IronPort-AV: E=Sophos;i="5.75,359,1589266800"; d="scan'208";a="129449677" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jul 2020 06:57:32 -0700 IronPort-SDR: X5qChEt8+NPOsxAyVJwnFY8igk+FDvTYMwr3hcRrmzO60jyutURIeFFgHwuPGQ9oxKzWZT5sWC 4SwhQzEJfxwQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,359,1589266800"; d="scan'208";a="325150689" Received: from lettner-mobl.ger.corp.intel.com (HELO localhost) ([10.252.32.212]) by FMSMGA003.fm.intel.com with ESMTP; 16 Jul 2020 06:57:21 -0700 From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , Jethro Beekman , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, josh@joshtriplett.org, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, npmccallum@redhat.com, puiterwijk@redhat.com, rientjes@google.com, sean.j.christopherson@intel.com, tglx@linutronix.de, yaozhangx@google.com Subject: [PATCH v36 17/24] x86/sgx: ptrace() support for the SGX driver Date: Thu, 16 Jul 2020 16:52:56 +0300 Message-Id: <20200716135303.276442-18-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200716135303.276442-1-jarkko.sakkinen@linux.intel.com> References: <20200716135303.276442-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. Acked-by: Jethro Beekman Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/encl.c | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 27a7904d7f5a..0ea4701adad5 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -335,10 +335,97 @@ static int sgx_vma_mprotect(struct vm_area_struct *vma, return mprotect_fixup(vma, pprev, start, end, newflags); } +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_get_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_get_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, .mprotect = sgx_vma_mprotect, + .access = sgx_vma_access, }; /** -- 2.25.1