Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp693426ybt; Mon, 6 Jul 2020 20:44:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyr7ZAu0N+x65J5tkNLBIMgk80MKT0teyamPTCNlwcezgZ6VO4ZNsscm0Ypk+jU8Ii1XQSH X-Received: by 2002:a17:906:1414:: with SMTP id p20mr44833089ejc.247.1594093487422; Mon, 06 Jul 2020 20:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594093487; cv=none; d=google.com; s=arc-20160816; b=taSVQ+FvkzoHPZ2F6ideAplc7L9/DmJQFzlZoo7orj9bjpIpUPTdiVvnEl7TxTGCSB rRek2CY1dVKyEJDQJmNn+GHJ2OGlST10fqjHU/2pR9WqD3RqxHvnDkmNdgo28D1ejk86 KmkiL4ae5m7NiU+mmFi9jmWTztYeL1pX8nT1qv3QiFDUe406fhFC3hx3FxuYCiv5P+WL BK5yCFGF/QRKIB2gaSbji7IsX4qW1FOdPUepCxsbDTquPClttOW/RUwpny/lr4Y8huhV niFyK3jAIlGL6pntYVxW9g7kFCdgzYKkuIhkOtJ8Tev/mhQsOeMlzvku5CxIbbpTYuQB 0DKw== 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=nl+XvRc7o6woLGo7QkzF8KWFRnoiMqVLxICzZjPYv/s=; b=H4QIAUTz3p4YTHzt5VV/PmgwiWy9fEHOkwVdxcKzKkkBDqGDddutmoVIhYc8L2N+S7 gLneaLc9gW7FrKc62pKhIywviD8DfMCfddT74YTOC3hovWe4WGMca50nK4xN+m/sZylD RE1JwUn3Xo6QNpjRpvAPObLKNP6kILdqDYGJrDaqJ009mpEdCUF20kWTYXepMtFCJdcL KBNV5JcOHQ7x/5QYu+D/WmfVlmhWATUurmufhd0juAXnFMXqbb+cJLhzdRJaeoz49aRQ JUPc1qfO5DHJlaU0iF5NosLxNOBLwmHruTAFhEwM/KpaKihhNK4pUE06c83dd0Zaydb+ bOPA== 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 g22si16293268edr.336.2020.07.06.20.44.23; Mon, 06 Jul 2020 20:44:47 -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 S1728418AbgGGDll (ORCPT + 99 others); Mon, 6 Jul 2020 23:41:41 -0400 Received: from mga04.intel.com ([192.55.52.120]:31688 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727092AbgGGDlj (ORCPT ); Mon, 6 Jul 2020 23:41:39 -0400 IronPort-SDR: 3Wx8anRhQoucOe3MJi9HJm/yrlBxU7d/BNpPr7I/SlEFaXMt6WECHRzGY3+bpm4RR9ywj+2ask dJ6J77UU5AUw== X-IronPort-AV: E=McAfee;i="6000,8403,9674"; a="145034056" X-IronPort-AV: E=Sophos;i="5.75,321,1589266800"; d="scan'208";a="145034056" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jul 2020 20:41:38 -0700 IronPort-SDR: WyVq7hkjfL0Z14JMdd+sU44L08lTqZs+erCnht/NQeMSVIM5qIxS5lIS5HfjPXJjySiCwHEGRo RaodmBoQE59Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,321,1589266800"; d="scan'208";a="427314807" Received: from apiccion-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.45.178]) by orsmga004.jf.intel.com with ESMTP; 06 Jul 2020 20:41:29 -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 v35 17/24] x86/sgx: ptrace() support for the SGX driver Date: Tue, 7 Jul 2020 06:37:40 +0300 Message-Id: <20200707033747.142828-18-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200707033747.142828-1-jarkko.sakkinen@linux.intel.com> References: <20200707033747.142828-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 b22e7187142d..a0c84a7fb6f9 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -334,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_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