From patchwork Wed Dec 4 16:28:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marcelo Henrique Cerri X-Patchwork-Id: 1204268 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47SknY1llTz9sPf; Thu, 5 Dec 2019 03:29:05 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1icXWO-0005WW-PE; Wed, 04 Dec 2019 16:29:00 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1icXWD-0005Ui-TC for kernel-team@lists.ubuntu.com; Wed, 04 Dec 2019 16:28:49 +0000 Received: from mail-qt1-f197.google.com ([209.85.160.197]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1icXWD-0006qc-A7 for kernel-team@lists.ubuntu.com; Wed, 04 Dec 2019 16:28:49 +0000 Received: by mail-qt1-f197.google.com with SMTP id p12so251483qtu.6 for ; Wed, 04 Dec 2019 08:28:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g1L/pzu5J/trQDf1TbCKza9/rgMcsQbFrQaau0SijDk=; b=X920/vY4hIJansu0xx6CLQ3HbmV1kLTXd169C9g99pt1v4AZkLZyaskVdU1BHAS2hB J4iUdXu7Kz6dS8Fj1Gj/s+XzSsMeNzcz8aHHz56LHYp9dZf6xwuV35HhGHWm1Z2tS/Id gpshfJofiacn30S0C7iHeqfzogCqUuBhjIpny0lqwrCDalnpIiNBYC5Yudm52xBQRNg6 1bFTiJK/kpuKxunBgndh+11DT4TwYGqX9plrCabOaK1tPR+mpa8B4HOEh6aTKgZT0lF3 Ay9+2vF05iLudjxyNcDa2KRJ77fMtdFO8/yqiscNvHjuDt+Pgs1N21LRacV5oBFunqMD QMfA== X-Gm-Message-State: APjAAAX3hS8M/kbruTvD9dnBwzDk/TjiKm0U2cbObomPzUAK0Qm4M4tu h+cA1NLC1pT3vSsQ03TfIyXin0GdBmK7ibVc8FPhc/iA6SSds4JXh9gsIG4WVsjLWClRMPiU37a +MJu0N81FdYrBxcnxiDTlX7jc+nC/0tgGYrQbEGJ4 X-Received: by 2002:a37:688c:: with SMTP id d134mr3870055qkc.288.1575476926176; Wed, 04 Dec 2019 08:28:46 -0800 (PST) X-Google-Smtp-Source: APXvYqwbFApQI30bklwvlxRisjbVuj+SMEBP2gBFxVTH040jZjzBnM6QWNpzXucU/toVY1zs4McmtA== X-Received: by 2002:a37:688c:: with SMTP id d134mr3869704qkc.288.1575476922595; Wed, 04 Dec 2019 08:28:42 -0800 (PST) Received: from localhost.localdomain ([2804:14c:4e6:1bc:c996:3e2:8668:ca74]) by smtp.gmail.com with ESMTPSA id t15sm3865432qkt.30.2019.12.04.08.28.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Dec 2019 08:28:41 -0800 (PST) From: Marcelo Henrique Cerri To: kernel-team@lists.ubuntu.com Subject: [{eoan, disco, xenial}:linux-azure PATCH 1/2] UBUNTU: SAUCE: linux-azure: Include Intel SGX driver to the main modules package Date: Wed, 4 Dec 2019 13:28:34 -0300 Message-Id: <20191204162835.28282-2-marcelo.cerri@canonical.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191204162835.28282-1-marcelo.cerri@canonical.com> References: <20191204162437.25733-1-marcelo.cerri@canonical.com> <20191204162835.28282-1-marcelo.cerri@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" BugLink: https://bugs.launchpad.net/bugs/1844245 This version corresponds to commit id a6f2f2a8600ca67b61daa13dc36442d3747bd3e9 from https://github.com/haimc-intel/SGXDataCenterAttestationPrimitives inker-ph1 Signed-off-by: Marcelo Henrique Cerri --- arch/x86/include/asm/sgx.h | 233 +++++ arch/x86/include/asm/sgx_arch.h | 278 ++++++ arch/x86/include/asm/sgx_pr.h | 80 ++ arch/x86/include/uapi/asm/sgx.h | 154 ++++ debian.azure/control.d/azure.inclusion-list | 1 + ubuntu/Makefile | 4 + ubuntu/sgx/License.txt | 46 + ubuntu/sgx/Makefile | 79 ++ ubuntu/sgx/README.md | 114 +++ ubuntu/sgx/sgx.h | 286 ++++++ ubuntu/sgx/sgx_driver_info.h | 62 ++ ubuntu/sgx/sgx_encl.c | 974 ++++++++++++++++++++ ubuntu/sgx/sgx_ioctl.c | 332 +++++++ ubuntu/sgx/sgx_main.c | 498 ++++++++++ ubuntu/sgx/sgx_page_cache.c | 596 ++++++++++++ ubuntu/sgx/sgx_util.c | 382 ++++++++ ubuntu/sgx/sgx_version.h | 60 ++ ubuntu/sgx/sgx_vma.c | 242 +++++ ubuntu/sgx/sgx_wl.h | 82 ++ 19 files changed, 4503 insertions(+) create mode 100644 arch/x86/include/asm/sgx.h create mode 100755 arch/x86/include/asm/sgx_arch.h create mode 100644 arch/x86/include/asm/sgx_pr.h create mode 100644 arch/x86/include/uapi/asm/sgx.h create mode 100644 ubuntu/sgx/License.txt create mode 100644 ubuntu/sgx/Makefile create mode 100644 ubuntu/sgx/README.md create mode 100644 ubuntu/sgx/sgx.h create mode 100644 ubuntu/sgx/sgx_driver_info.h create mode 100644 ubuntu/sgx/sgx_encl.c create mode 100644 ubuntu/sgx/sgx_ioctl.c create mode 100644 ubuntu/sgx/sgx_main.c create mode 100644 ubuntu/sgx/sgx_page_cache.c create mode 100644 ubuntu/sgx/sgx_util.c create mode 100644 ubuntu/sgx/sgx_version.h create mode 100644 ubuntu/sgx/sgx_vma.c create mode 100644 ubuntu/sgx/sgx_wl.h diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h new file mode 100644 index 000000000000..1daf4e4c0790 --- /dev/null +++ b/arch/x86/include/asm/sgx.h @@ -0,0 +1,233 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha + +#ifndef _ASM_X86_SGX_H +#define _ASM_X86_SGX_H + +#include +#include +#include +#include +#include + +#define SGX_CPUID 0x12 + +enum sgx_cpuid { + SGX_CPUID_CAPABILITIES = 0, + SGX_CPUID_ATTRIBUTES = 1, + SGX_CPUID_EPC_BANKS = 2, +}; + +enum sgx_commands { + ECREATE = 0x0, + EADD = 0x1, + EINIT = 0x2, + EREMOVE = 0x3, + EDGBRD = 0x4, + EDGBWR = 0x5, + EEXTEND = 0x6, + ELDU = 0x8, + EBLOCK = 0x9, + EPA = 0xA, + EWB = 0xB, + ETRACK = 0xC, + EAUG = 0xD, + EMODPR = 0xE, + EMODT = 0xF, +}; + +#ifdef CONFIG_X86_64 +#define XAX "%%rax" +#else +#define XAX "%%eax" +#endif + +#define __encls_ret(rax, rbx, rcx, rdx) \ + ({ \ + int ret; \ + asm volatile( \ + "1: .byte 0x0f, 0x01, 0xcf;\n\t" \ + "2:\n" \ + ".section .fixup,\"ax\"\n" \ + "3: mov $-14,"XAX"\n" \ + " jmp 2b\n" \ + ".previous\n" \ + _ASM_EXTABLE(1b, 3b) \ + : "=a"(ret) \ + : "a"(rax), "b"(rbx), "c"(rcx), "d"(rdx) \ + : "memory"); \ + ret; \ + }) + +#define __encls(rax, rbx, rcx, rdx...) \ + ({ \ + int ret; \ + asm volatile( \ + "1: .byte 0x0f, 0x01, 0xcf;\n\t" \ + " xor "XAX","XAX"\n" \ + "2:\n" \ + ".section .fixup,\"ax\"\n" \ + "3: mov $-14,"XAX"\n" \ + " jmp 2b\n" \ + ".previous\n" \ + _ASM_EXTABLE(1b, 3b) \ + : "=a"(ret), "=b"(rbx), "=c"(rcx) \ + : "a"(rax), "b"(rbx), "c"(rcx), rdx \ + : "memory"); \ + ret; \ + }) + +static inline unsigned long __ecreate(struct sgx_pageinfo *pginfo, void *secs) +{ + return __encls(ECREATE, pginfo, secs, "d"(0)); +} + +static inline int __eextend(void *secs, void *epc) +{ + return __encls(EEXTEND, secs, epc, "d"(0)); +} + +static inline int __eadd(struct sgx_pageinfo *pginfo, void *epc) +{ + return __encls(EADD, pginfo, epc, "d"(0)); +} + +static inline int __einit(void *sigstruct, struct sgx_einittoken *einittoken, + void *secs) +{ + return __encls_ret(EINIT, sigstruct, secs, einittoken); +} + +static inline int __eremove(void *epc) +{ + unsigned long rbx = 0; + unsigned long rdx = 0; + + return __encls_ret(EREMOVE, rbx, epc, rdx); +} + +static inline int __edbgwr(unsigned long addr, unsigned long *data) +{ + return __encls(EDGBWR, *data, addr, "d"(0)); +} + +static inline int __edbgrd(unsigned long addr, unsigned long *data) +{ + unsigned long rbx = 0; + int ret; + + ret = __encls(EDGBRD, rbx, addr, "d"(0)); + if (!ret) + *(unsigned long *) data = rbx; + + return ret; +} + +static inline int __etrack(void *epc) +{ + unsigned long rbx = 0; + unsigned long rdx = 0; + + return __encls_ret(ETRACK, rbx, epc, rdx); +} + +static inline int __eldu(unsigned long rbx, unsigned long rcx, + unsigned long rdx) +{ + return __encls_ret(ELDU, rbx, rcx, rdx); +} + +static inline int __eblock(void *epc) +{ + unsigned long rbx = 0; + unsigned long rdx = 0; + + return __encls_ret(EBLOCK, rbx, epc, rdx); +} + +static inline int __epa(void *epc) +{ + unsigned long rbx = SGX_PAGE_TYPE_VA; + + return __encls(EPA, rbx, epc, "d"(0)); +} + +static inline int __ewb(struct sgx_pageinfo *pginfo, void *epc, void *va) +{ + return __encls_ret(EWB, pginfo, epc, va); +} + +static inline int __eaug(struct sgx_pageinfo *pginfo, void *epc) +{ + return __encls(EAUG, pginfo, epc, "d"(0)); +} + +static inline int __emodpr(struct sgx_secinfo *secinfo, void *epc) +{ + unsigned long rdx = 0; + + return __encls_ret(EMODPR, secinfo, epc, rdx); +} + +static inline int __emodt(struct sgx_secinfo *secinfo, void *epc) +{ + unsigned long rdx = 0; + + return __encls_ret(EMODT, secinfo, epc, rdx); +} + +extern bool sgx_enabled; + +#endif /* _ASM_X86_SGX_H */ diff --git a/arch/x86/include/asm/sgx_arch.h b/arch/x86/include/asm/sgx_arch.h new file mode 100755 index 000000000000..9cddf83967d1 --- /dev/null +++ b/arch/x86/include/asm/sgx_arch.h @@ -0,0 +1,278 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha + +#ifndef _ASM_X86_SGX_ARCH_H +#define _ASM_X86_SGX_ARCH_H + +#include + +#define SGX_SSA_GPRS_SIZE 182 +#define SGX_SSA_MISC_EXINFO_SIZE 16 + +enum sgx_misc { + SGX_MISC_EXINFO = 0x01, +}; + +#define SGX_MISC_RESERVED_MASK 0xFFFFFFFFFFFFFFFEL + +enum sgx_attribute { + SGX_ATTR_INIT = BIT(0), + SGX_ATTR_DEBUG = BIT(1), + SGX_ATTR_MODE64BIT = BIT(2), + SGX_ATTR_PROVISIONKEY = BIT(4), + SGX_ATTR_EINITTOKENKEY = BIT(5), + SGX_ATTR_KSS = BIT(7) +}; + +#define SGX_ATTR_RESERVED_MASK (BIT_ULL(0) | BIT_ULL(3) | BIT_ULL(6) | GENMASK_ULL(63, 8)) +#define SGX_ATTR_ALLOWED_MASK (SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT | SGX_ATTR_KSS) + + +#define SGX_SECS_RESERVED1_SIZE 24 +#define SGX_SECS_RESERVED2_SIZE 32 +#define SGX_SECS_RESERVED3_SIZE 32 +#define SGX_SECS_RESERVED4_SIZE 3834 + +struct sgx_secs { + uint64_t size; + uint64_t base; + uint32_t ssaframesize; + uint32_t miscselect; + uint8_t reserved1[SGX_SECS_RESERVED1_SIZE]; + uint64_t attributes; + uint64_t xfrm; + uint32_t mrenclave[8]; + uint8_t reserved2[SGX_SECS_RESERVED2_SIZE]; + uint32_t mrsigner[8]; + uint8_t reserved3[SGX_SECS_RESERVED3_SIZE]; + uint32_t configid[16]; + uint16_t isvvprodid; + uint16_t isvsvn; + uint16_t configsvn; + uint8_t reserved4[SGX_SECS_RESERVED4_SIZE]; +} __attribute__((__packed__)); + +enum sgx_tcs_flags { + SGX_TCS_DBGOPTIN = 0x01, /* cleared on EADD */ +}; + +#define SGX_TCS_RESERVED_MASK 0xFFFFFFFFFFFFFFFEL + +struct sgx_tcs { + uint64_t state; + uint64_t flags; + uint64_t ossa; + uint32_t cssa; + uint32_t nssa; + uint64_t oentry; + uint64_t aep; + uint64_t ofsbase; + uint64_t ogsbase; + uint32_t fslimit; + uint32_t gslimit; + uint64_t reserved[503]; +} __attribute__((__packed__)); + +struct sgx_pageinfo { + uint64_t linaddr; + uint64_t srcpge; + union { + uint64_t secinfo; + uint64_t pcmd; + }; + uint64_t secs; +} __attribute__((packed, aligned(32))); + + +#define SGX_SECINFO_PERMISSION_MASK 0x0000000000000007L +#define SGX_SECINFO_PAGE_TYPE_MASK 0x000000000000FF00L +#define SGX_SECINFO_RESERVED_MASK 0xFFFFFFFFFFFF00F8L + +enum sgx_page_type { + SGX_PAGE_TYPE_SECS = 0x00, + SGX_PAGE_TYPE_TCS = 0x01, + SGX_PAGE_TYPE_REG = 0x02, + SGX_PAGE_TYPE_VA = 0x03, +}; + +enum sgx_secinfo_flags { + SGX_SECINFO_R = 0x01, + SGX_SECINFO_W = 0x02, + SGX_SECINFO_X = 0x04, + SGX_SECINFO_SECS = (SGX_PAGE_TYPE_SECS << 8), + SGX_SECINFO_TCS = (SGX_PAGE_TYPE_TCS << 8), + SGX_SECINFO_REG = (SGX_PAGE_TYPE_REG << 8), +}; + +struct sgx_secinfo { + uint64_t flags; + uint64_t reserved[7]; +} __attribute__((__packed__, aligned(64))); + +struct sgx_pcmd { + struct sgx_secinfo secinfo; + uint64_t enclave_id; + uint8_t reserved[40]; + uint8_t mac[16]; +} __attribute__((__packed__, aligned(64))); + +#define SGX_MODULUS_SIZE 384 + +struct sgx_sigstruct_header { + uint64_t header1[2]; + uint32_t vendor; + uint32_t date; + uint64_t header2[2]; + uint32_t swdefined; + uint8_t reserved1[84]; +} __attribute__((__packed__)); + +struct sgx_sigstruct_body { + uint32_t miscselect; + uint32_t miscmask; + uint8_t reserved2[4]; + uint8_t isvfamilyid[16]; + uint64_t attributes; + uint64_t xfrm; + uint64_t attributesmask; + uint64_t xfrmmask; + uint8_t mrenclave[32]; + uint8_t reserved3[16]; + uint8_t isvextprodid[16]; + uint16_t isvprodid; + uint16_t isvsvn; +} __attribute__((__packed__)); + +struct sgx_sigstruct { + struct sgx_sigstruct_header header; + uint8_t modulus[SGX_MODULUS_SIZE]; + uint32_t exponent; + uint8_t signature[SGX_MODULUS_SIZE]; + struct sgx_sigstruct_body body; + uint8_t reserved4[12]; + uint8_t q1[SGX_MODULUS_SIZE]; + uint8_t q2[SGX_MODULUS_SIZE]; +} __attribute__((__packed__)); + +struct sgx_sigstruct_payload { + struct sgx_sigstruct_header header; + struct sgx_sigstruct_body body; +} __attribute__((__packed__)); + +struct sgx_einittoken_payload { + uint32_t valid; + uint32_t reserved1[11]; + uint64_t attributes; + uint64_t xfrm; + uint8_t mrenclave[32]; + uint8_t reserved2[32]; + uint8_t mrsigner[32]; + uint8_t reserved3[32]; +} __attribute__((__packed__)); + +struct sgx_einittoken { + struct sgx_einittoken_payload payload; + uint8_t cpusvnle[16]; + uint16_t isvprodidle; + uint16_t isvsvnle; + uint8_t reserved2[24]; + uint32_t maskedmiscselectle; + uint64_t maskedattributesle; + uint64_t maskedxfrmle; + uint8_t keyid[32]; + uint8_t mac[16]; +} __attribute__((__packed__)); + +struct sgx_report { + uint8_t cpusvn[16]; + uint32_t miscselect; + uint8_t reserved1[28]; + uint64_t attributes; + uint64_t xfrm; + uint8_t mrenclave[32]; + uint8_t reserved2[32]; + uint8_t mrsigner[32]; + uint8_t reserved3[96]; + uint16_t isvprodid; + uint16_t isvsvn; + uint8_t reserved4[60]; + uint8_t reportdata[64]; + uint8_t keyid[32]; + uint8_t mac[16]; +} __attribute__((__packed__)); + +struct sgx_targetinfo { + uint8_t mrenclave[32]; + uint64_t attributes; + uint64_t xfrm; + uint8_t reserved1[4]; + uint32_t miscselect; + uint8_t reserved2[456]; +} __attribute__((__packed__)); + +struct sgx_keyrequest { + uint16_t keyname; + uint16_t keypolicy; + uint16_t isvsvn; + uint16_t reserved1; + uint8_t cpusvn[16]; + uint64_t attributemask; + uint64_t xfrmmask; + uint8_t keyid[32]; + uint32_t miscmask; + uint8_t reserved2[436]; +} __attribute__((__packed__)); + +#endif /* _ASM_X86_SGX_ARCH_H */ diff --git a/arch/x86/include/asm/sgx_pr.h b/arch/x86/include/asm/sgx_pr.h new file mode 100644 index 000000000000..9479a6c6b3ca --- /dev/null +++ b/arch/x86/include/asm/sgx_pr.h @@ -0,0 +1,80 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir + +#ifndef _ASM_X86_SGX_PR_H +#define _ASM_X86_SGX_PR_H + +#undef pr_fmt +#define pr_fmt(fmt) "intel_sgx: " fmt + +#define sgx_pr_ratelimited(level, encl, fmt, ...) \ + pr_ ## level ## _ratelimited("[%d:0x%p] " fmt, \ + pid_nr((encl)->tgid), \ + (void *)(encl)->base, ##__VA_ARGS__) + +#define sgx_dbg(encl, fmt, ...) \ + sgx_pr_ratelimited(debug, encl, fmt, ##__VA_ARGS__) +#define sgx_info(encl, fmt, ...) \ + sgx_pr_ratelimited(info, encl, fmt, ##__VA_ARGS__) +#define sgx_warn(encl, fmt, ...) \ + sgx_pr_ratelimited(warn, encl, fmt, ##__VA_ARGS__) +#define sgx_err(encl, fmt, ...) \ + sgx_pr_ratelimited(err, encl, fmt, ##__VA_ARGS__) +#define sgx_crit(encl, fmt, ...) \ + sgx_pr_ratelimited(crit, encl, fmt, ##__VA_ARGS__) + +#endif /* _ASM_X86_SGX_PR_H */ diff --git a/arch/x86/include/uapi/asm/sgx.h b/arch/x86/include/uapi/asm/sgx.h new file mode 100644 index 000000000000..9a50f0bdd0be --- /dev/null +++ b/arch/x86/include/uapi/asm/sgx.h @@ -0,0 +1,154 @@ +/* + * This file is provided under a dual BSD/GPLv2 license. When using or + * redistributing this file, you may do so under either license. + * + * GPL LICENSE SUMMARY + * + * Copyright(c) 2016-2018 Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * Contact Information: + * Jarkko Sakkinen + * Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo + * + * BSD LICENSE + * + * Copyright(c) 2016-2018 Intel Corporation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: + * + * Jarkko Sakkinen + * Suresh Siddha + */ + +#ifndef _UAPI_ASM_X86_SGX_H +#define _UAPI_ASM_X86_SGX_H + +#include +#include + +#define SGX_MAGIC 0xA4 + +#define SGX_IOC_ENCLAVE_CREATE \ + _IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create) +#define SGX_IOC_ENCLAVE_ADD_PAGE \ + _IOW(SGX_MAGIC, 0x01, struct sgx_enclave_add_page) +#define SGX_IOC_ENCLAVE_INIT \ + _IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init) +#define SGX_IOC_ENCLAVE_SET_ATTRIBUTE \ + _IOW(SGX_MAGIC, 0x03, struct sgx_enclave_set_attribute) + +/* SGX leaf instruction return values */ +#define SGX_SUCCESS 0 +#define SGX_INVALID_SIG_STRUCT 1 +#define SGX_INVALID_ATTRIBUTE 2 +#define SGX_BLKSTATE 3 +#define SGX_INVALID_MEASUREMENT 4 +#define SGX_NOTBLOCKABLE 5 +#define SGX_PG_INVLD 6 +#define SGX_LOCKFAIL 7 +#define SGX_INVALID_SIGNATURE 8 +#define SGX_MAC_COMPARE_FAIL 9 +#define SGX_PAGE_NOT_BLOCKED 10 +#define SGX_NOT_TRACKED 11 +#define SGX_VA_SLOT_OCCUPIED 12 +#define SGX_CHILD_PRESENT 13 +#define SGX_ENCLAVE_ACT 14 +#define SGX_ENTRYEPOCH_LOCKED 15 +#define SGX_INVALID_EINITTOKEN 16 +#define SGX_PREV_TRK_INCMPL 17 +#define SGX_PG_IS_SECS 18 +#define SGX_INVALID_CPUSVN 32 +#define SGX_INVALID_ISVSVN 64 +#define SGX_UNMASKED_EVENT 128 +#define SGX_INVALID_KEYNAME 256 + +/* IOCTL return values */ +#define SGX_POWER_LOST_ENCLAVE 0x40000000 +#define SGX_LE_ROLLBACK 0x40000001 +#define SGX_INVALID_PRIVILEGE 0x40000002 +#define SGX_UNEXPECTED_ERROR 0x40000003 + +/** + * struct sgx_enclave_create - parameter structure for the + * %SGX_IOC_ENCLAVE_CREATE ioctl + * @src: address for the SECS page data + */ +struct sgx_enclave_create { + __u64 src; +} __attribute__((__packed__)); + +/** + * struct sgx_enclave_add_page - parameter structure for the + * %SGX_IOC_ENCLAVE_ADD_PAGE ioctl + * @addr: address within the ELRANGE + * @src: address for the page data + * @secinfo: address for the SECINFO data + * @mrmask: bitmask for the measured 256 byte chunks + */ +struct sgx_enclave_add_page { + __u64 addr; + __u64 src; + __u64 secinfo; + __u16 mrmask; +} __attribute__((__packed__)); + + +/** + * struct sgx_enclave_init - parameter structure for the + * %SGX_IOC_ENCLAVE_INIT ioctl + * @addr: address within the ELRANGE + * @sigstruct: address for the SIGSTRUCT data + */ +struct sgx_enclave_init { + __u64 addr; + __u64 sigstruct; +} __attribute__((__packed__)); + +/** + * struct sgx_enclave_set_attribute - parameter structure for the + * %SGX_IOC_ENCLAVE_SET_ATTRIBUTE ioctl + * @addr: address within the ELRANGE + * @attribute_fd: file handle of the attribute file in the securityfs + */ +struct sgx_enclave_set_attribute { + __u64 addr; + __u64 attribute_fd; +} __attribute__((__packed__));; + + +#endif /* _UAPI_ASM_X86_SGX_H */ diff --git a/debian.azure/control.d/azure.inclusion-list b/debian.azure/control.d/azure.inclusion-list index 693e58e66f8e..003d0a0cbc9f 100644 --- a/debian.azure/control.d/azure.inclusion-list +++ b/debian.azure/control.d/azure.inclusion-list @@ -264,6 +264,7 @@ net/xfrm/* sound/drivers/pcsp/snd-pcsp.ko sound/pci/snd-ens1370.ko sound/soundcore.ko +ubuntu/sgx/* ubuntu/vbox/vboxguest/vboxguest.ko ubuntu/vbox/vboxsf/vboxsf.ko zfs/* diff --git a/ubuntu/Makefile b/ubuntu/Makefile index c499b2112dec..3230b018df8c 100644 --- a/ubuntu/Makefile +++ b/ubuntu/Makefile @@ -31,6 +31,10 @@ endif ## ## ## +obj-y += sgx/ +## +## +## ## ## ## diff --git a/ubuntu/sgx/License.txt b/ubuntu/sgx/License.txt new file mode 100644 index 000000000000..947cefddbefd --- /dev/null +++ b/ubuntu/sgx/License.txt @@ -0,0 +1,46 @@ +Copyright (C) 2018 Intel Corporation +  +This software is licensed under +(a) a 3-clause BSD license; or alternatively +(b) the GPL v2 license +  +-- A. BSD-3-Clause ---------------------------- +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, +   this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, +   this list of conditions and the following disclaimer in the documentation +   and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its contributors +   may be used to endorse or promote products derived from this software +   without specific prior written permission. +  +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +  +-- B. GPL-2.0 ---------------------------- +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License, as published +by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. +  +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +GNU General Public License for more details. +  +You should have received a copy of the GNU General Public License +along with this program; if not, see . +------------------------------ + +SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0) diff --git a/ubuntu/sgx/Makefile b/ubuntu/sgx/Makefile new file mode 100644 index 000000000000..9dcb2b159657 --- /dev/null +++ b/ubuntu/sgx/Makefile @@ -0,0 +1,79 @@ +# This file is provided under a dual BSD/GPLv2 license. When using or +# redistributing this file, you may do so under either license. +# +# GPL LICENSE SUMMARY +# +# Copyright(c) 2016-2018 Intel Corporation. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of version 2 of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# Contact Information: +# Jarkko Sakkinen +# Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +# +# BSD LICENSE +# +# Copyright(c) 2016-2018 Intel Corporation. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# +# Intel SGX +# + +ifneq ($(KERNELRELEASE),) + +obj-m += intel_sgx.o +intel_sgx-y := \ + sgx_ioctl.o \ + sgx_encl.o \ + sgx_main.o \ + sgx_page_cache.o \ + sgx_util.o \ + sgx_vma.o + +else + +KDIR := /lib/modules/$(shell uname -r)/build +PWD := $(shell pwd) + +default: + $(MAKE) -C $(KDIR) M=$(PWD) CFLAGS_MODULE="-I$(PWD) -I$(PWD)/include" modules + +endif + +clean: + rm -vrf *.o *.ko *.order *.symvers *.mod.c .tmp_versions .*.cmd *.o.ur-safe + + diff --git a/ubuntu/sgx/README.md b/ubuntu/sgx/README.md new file mode 100644 index 000000000000..e459167a8937 --- /dev/null +++ b/ubuntu/sgx/README.md @@ -0,0 +1,114 @@ +Intel(R) Software Guard Extensions for Linux\* OS +================================================ + +# SGX Linux Driver with Launch Enclave(LE) for Intel(R) SGX DCAP + +Introduction +------------ +This Intel(R) SGX driver package is for Intel(R) SGX DCAP and is derived from the upstream version of the SGX driver, including the in-driver Launch Enclave. + + +Documentation +------------- +- [Intel(R) SGX for Linux\* OS](https://01.org/intel-softwareguard-extensions) project home page on [01.org](http://01.org) +- [Intel(R) SGX Programming Reference](https://software.intel.com/sites/default/files/managed/48/88/329298-002.pdf) + + +Build and Install the Intel(R) SGX Driver +----------------------------------------- +### Prerequisites +- Ensure that you have the following required operating systems: + * Ubuntu* 16.04 LTS Desktop 64bits - minimal kernel 4.10 + * Ubuntu* 16.04 LTS Server 64bits - minimal kernel 4.10 + * Ubuntu* 18.04 LTS Desktop 64bits + * Ubuntu* 18.04 LTS Server 64bits +- Ensure that you have the following required hardware: + * 8th Generation Intel(R) Core(TM) Processor or newer with **Flexible Launch Control** and **Intel(R) AES New Instructions** support* + * Intel(R) Atom(TM) Processor with **Flexible Launch Control** and **Intel(R) AES New Instructions** support* +- Configure the system with the **SGX hardware enabled** option. +- Ensure that the version of installed kernel headers matches the active kernel version on the system. + * To check if matching kernel headers are installed: + ``` + $ dpkg-query -s linux-headers-$(uname -r) + ``` + * To install matching headers: + ``` + $ sudo apt-get install linux-headers-$(uname -r) + ``` +- OpenSSL is required for the Launch Enclave signing. + * To install OpenSSl: + ``` + $ sudo apt-get install libssl-dev + ``` + +**Note:** Refer to the *"Intel® SGX Resource Enumeration Leaves"* section in the [Intel SGX Programming reference guide](https://software.intel.com/sites/default/files/managed/48/88/329298-002.pdf) to make sure your cpu has the SGX feature. + + +### Build the Intel(R) SGX Driver +The driver build process is also building and integrating the Launch Enclave as part of the build process. +As the Launch Enclave must be signed, the driver build process supports two methods of build: +- Single step / debug: + In this method the LE is built and signed as part of the driver build process, the private key may be provided to the build command or auto-generated during the build +- Two steps / production: + In this method the first step builds the Launch Enclave and generates the signing materials, which should be signed by some signing entity separately. In the second step the signature and the public key are used to continue the build and generate the driver. + +These build options are provided to support the above two build methods: +- sign: + Build option for the single step build process. It builds the Launch Enclave, signs it and integrate it into the driver. The private key may be provided to the build command by specifying ```SGX_LE_SIGNING_KEY_PATH=``` (default: ./sgx_signing_key.pem), if the key does not exist it will be generated. +- gendata: + Build option for the first step in the two steps process. It builds the Launch Enclave and prepared the signing materials for it. The output of the build may be defined by specifying ```SGX_LE_SIGNING_MATERIAL=``` (default: ./signing_material.bin). +- usesig: + Build option for the second step in the two steps process. It gets the signature file and the public key, and uses them to integrate with the driver build. + The signature file **must** be provided by specifying ```SIG_FILE=```. + In addition the public key file may be specified by using ```SGX_LE_PUBLIC_KEY_PATH=``` (default: ./sgx_public_key.pem). + +#### Build Intel(R) SGX Driver Using Single Step Process + +The following is an example for a single step make command: +``` +$ make sign SGX_LE_SIGNING_KEY_PATH=~/my_private_key.pem +``` +**Note:** The **SGX_LE_SIGNING_KEY_PATH** is NOT a mandatory parameter. + +#### Build Intel(R) SGX Driver Using Two Steps Process +The following lines are an example for two steps make process: +``` +$ make gendata SGX_LE_SIGNING_MATERIAL=~/signing_material.bin +$ [sign the generated signing material] +$ make usesig SIG_FILE=~/signature_file.bin SGX_LE_PUBLIC_KEY_PATH=~/my_public_key.pem +``` +**Note:** The **SGX_LE_SIGNING_MATERIAL** and **SGX_LE_PUBLIC_KEY_PATH** are NOT mandatory parameters. +**Note:** To generate "Intel signed" compatible sigstruct file, add **INTEL_SIGNED=1** for each of the make commands. + +#### Build Intel(R) SGX Driver using a pre-built Permissive LE +The Permissive LE (PLE) includes two headers representing the binary content of the PLE (sgx_le_blob.h) and the sigstruct (sgx_le_ss.h) located in the ```driver/le/enclave``` directory. +If these files exist, the PLE will not be built and they will be integrated into the driver build. Use a single step make command to complete the driver build and integrate the PLE into it: +``` +$ make +``` + +**Note:** To ensure execution of the PLE build when **NOT** using pre-built PLE, clean all the previously built content before any other build command: +``` +$ make clean +``` + +### Install the Intel(R) SGX Driver +The Intel(R) SGX driver supports DKMS installation, to install the driver follow the following steps: +- Ensure that the DKMS package is installed, or install it using: + ``` $ sudo apt-get install dkms ``` +- With root priviledge, copy the sources to ``/usr/src/sgx-/`` + - ```` should match the version specified in the dkms.conf file +- Follow the following steps to add and install the driver into the DKMS tree: +``` +$ sudo dkms add -m sgx -v +$ sudo dkms build -m sgx -v +$ sudo dkms install -m sgx -v +$ sudo /sbin/modprobe intel_sgx +``` +### Uninstall the Intel(R) SGX Driver +To uninstall the Intel(R) SGX driver, enter the following commands with root privilege: +``` +$ sudo /sbin/modprobe -r intel_sgx +$ sudo dkms remove -m sgx -v --all +``` +You should also remove the sources from ``/usr/src/sgx-/`` diff --git a/ubuntu/sgx/sgx.h b/ubuntu/sgx/sgx.h new file mode 100644 index 000000000000..69503db5399f --- /dev/null +++ b/ubuntu/sgx/sgx.h @@ -0,0 +1,286 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir + +#ifndef __ARCH_INTEL_SGX_H__ +#define __ARCH_INTEL_SGX_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SGX_MAX_EPC_BANKS 8 + +#ifndef X86_FEATURE_SGX + #define X86_FEATURE_SGX (9 * 32 + 2) +#endif + +#define FEATURE_CONTROL_SGX_ENABLE (1<<18) + +#ifndef MSR_IA32_FEATURE_CONTROL + #define MSR_IA32_FEATURE_CONTROL 0x0000003a +#endif + +#ifndef FEATURE_CONTROL_SGX_LE_WR + #define FEATURE_CONTROL_SGX_LE_WR (1<<17) +#endif + +#ifndef X86_FEATURE_SGX_LC + #define X86_FEATURE_SGX_LC (16*32+30) /* supports SGX launch configuration */ +#endif + +#ifndef MSR_IA32_FEATURE_CONFIG + #define MSR_IA32_FEATURE_CONFIG 0x0000013C +#endif + +#ifndef FEATURE_CONFIG_LOCKED + #define FEATURE_CONFIG_LOCKED (1<<0) +#endif + +#ifndef FEATURE_CONFIG_AES_DISABLE + #define FEATURE_CONFIG_AES_DISABLE (1<<1) +#endif + +#define FEATURE_CONFIG_AES_DISABLE_LOCKED (FEATURE_CONFIG_AES_DISABLE | FEATURE_CONFIG_LOCKED) + + +/* Intel SGX MSRs */ +#ifndef MSR_IA32_SGXLEPUBKEYHASH0 + #define MSR_IA32_SGXLEPUBKEYHASH0 0x0000008C + #define MSR_IA32_SGXLEPUBKEYHASH1 0x0000008D + #define MSR_IA32_SGXLEPUBKEYHASH2 0x0000008E + #define MSR_IA32_SGXLEPUBKEYHASH3 0x0000008F +#endif + +#define SGX_EINIT_SPIN_COUNT 20 +#define SGX_EINIT_SLEEP_COUNT 50 +#define SGX_EINIT_SLEEP_TIME 20 + +#define SGX_VA_SLOT_COUNT 512 +#define SGX_VA_OFFSET_MASK ((SGX_VA_SLOT_COUNT - 1) << 3) + +#define SGX_EPC_BANK(epc_page) \ + (&sgx_epc_banks[(unsigned long)(epc_page) & ~PAGE_MASK]) +#define SGX_EPC_PFN(epc_page) PFN_DOWN((unsigned long)(epc_page)) +#define SGX_EPC_ADDR(epc_page) ((unsigned long)(epc_page) & PAGE_MASK) + +enum sgx_alloc_flags { + SGX_ALLOC_ATOMIC = BIT(0), +}; + +struct sgx_va_page { + void *epc_page; + DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT); + struct list_head list; +}; + +static inline unsigned int sgx_alloc_va_slot(struct sgx_va_page *page) +{ + int slot = find_first_zero_bit(page->slots, SGX_VA_SLOT_COUNT); + + if (slot < SGX_VA_SLOT_COUNT) + set_bit(slot, page->slots); + + return slot << 3; +} + +static inline void sgx_free_va_slot(struct sgx_va_page *page, + unsigned int offset) +{ + clear_bit(offset >> 3, page->slots); +} + +static inline bool sgx_va_page_full(struct sgx_va_page *page) +{ + int slot = find_first_zero_bit(page->slots, SGX_VA_SLOT_COUNT); + + return slot == SGX_VA_SLOT_COUNT; +} + +enum sgx_encl_page_flags { + SGX_ENCL_PAGE_TCS = BIT(0), + SGX_ENCL_PAGE_RESERVED = BIT(1), + SGX_ENCL_PAGE_LOADED = BIT(2), +}; + +#define SGX_ENCL_PAGE_ADDR(encl_page) ((encl_page)->desc & PAGE_MASK) +#define SGX_ENCL_PAGE_VA_OFFSET(encl_page) \ + ((encl_page)->desc & SGX_VA_OFFSET_MASK) +#define SGX_ENCL_PAGE_PCMD_OFFSET(encl_page) \ + ((PFN_DOWN((encl_page)->desc) & 31) * 128) + +struct sgx_encl_page { + unsigned long desc; + union { + void *epc_page; + struct sgx_va_page *va_page; + }; + struct sgx_encl *encl; + struct list_head list; +}; + +enum sgx_encl_flags { + SGX_ENCL_INITIALIZED = BIT(0), + SGX_ENCL_DEBUG = BIT(1), + SGX_ENCL_SECS_EVICTED = BIT(2), + SGX_ENCL_SUSPEND = BIT(3), + SGX_ENCL_DEAD = BIT(4), +}; + +struct sgx_encl { + unsigned int flags; + uint64_t attributes; + uint64_t allowed_attributes; + uint64_t xfrm; + unsigned int page_cnt; + unsigned int secs_child_cnt; + struct mutex lock; + struct mm_struct *mm; + struct file *backing; + struct file *pcmd; + struct list_head load_list; + struct kref refcount; + unsigned long base; + unsigned long size; + unsigned long ssaframesize; + struct list_head va_pages; + struct radix_tree_root page_tree; + struct list_head add_page_reqs; + struct work_struct add_page_work; + struct sgx_encl_page secs; + struct pid *tgid; + struct list_head encl_list; + struct mmu_notifier mmu_notifier; +}; + +extern struct workqueue_struct *sgx_add_page_wq; +extern u64 sgx_encl_size_max_32; +extern u64 sgx_encl_size_max_64; +extern u64 sgx_xfrm_mask; +extern u32 sgx_misc_reserved; +extern u32 sgx_xsave_size_tbl[64]; +extern bool sgx_unlocked_msrs; + +extern const struct file_operations sgx_fops; +extern const struct vm_operations_struct sgx_vm_ops; +extern const struct file_operations sgx_provision_fops; + +int sgx_encl_find(struct mm_struct *mm, unsigned long addr, + struct vm_area_struct **vma); +struct sgx_encl *sgx_encl_alloc(struct sgx_secs *secs); +int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs); +int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, void *data, + struct sgx_secinfo *secinfo, unsigned int mrmask); +int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct, + struct sgx_einittoken *einittoken); +void sgx_encl_release(struct kref *ref); + +long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg); +#ifdef CONFIG_COMPAT +long sgx_compat_ioctl(struct file *filep, unsigned int cmd, unsigned long arg); +#endif + +/* Utility functions */ +int sgx_test_and_clear_young(struct sgx_encl_page *page); +struct page *sgx_get_backing(struct sgx_encl *encl, + struct sgx_encl_page *entry, + bool pcmd); +void sgx_put_backing(struct page *backing, bool write); +void sgx_insert_pte(struct sgx_encl *encl, + struct sgx_encl_page *encl_page, + void *epc_page, + struct vm_area_struct *vma); +int sgx_eremove(void *epc_page); +void sgx_zap_tcs_ptes(struct sgx_encl *encl, + struct vm_area_struct *vma); +void sgx_invalidate(struct sgx_encl *encl, bool flush_cpus); +void sgx_flush_cpus(struct sgx_encl *encl); + +enum sgx_fault_flags { + SGX_FAULT_RESERVE = BIT(0), +}; + +struct sgx_encl_page *sgx_fault_page(struct vm_area_struct *vma, + unsigned long addr, + unsigned int flags); + +int sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus, void *hash); +int sgx_get_key_hash_simple(const void *modulus, void *hash); + +extern struct mutex sgx_encl_list_lock; +extern struct list_head sgx_encl_list; +extern atomic_t sgx_va_pages_cnt; + +int sgx_add_epc_bank(resource_size_t start, unsigned long size, int bank); +int sgx_page_cache_init(struct device *parent); +void sgx_page_cache_teardown(void); +void *sgx_alloc_page(unsigned int flags); +void sgx_free_page(void *page, struct sgx_encl *encl); +void *sgx_get_page(void *page); +void sgx_put_page(void *ptr); + + +#endif /* __ARCH_X86_INTEL_SGX_H__ */ diff --git a/ubuntu/sgx/sgx_driver_info.h b/ubuntu/sgx/sgx_driver_info.h new file mode 100644 index 000000000000..72719d6357d0 --- /dev/null +++ b/ubuntu/sgx/sgx_driver_info.h @@ -0,0 +1,62 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// + +#ifndef _SGX_DRIVER_INFO_H +#define _SGX_DRIVER_INFO_H + +#define SGX_DRIVER_INFO_OOT 0x80000000 +#define SGX_DRIVER_INFO_DCAP 0x40000000 + +#define SGX_DRIVER_INFO_FEATURE_SGX2 0x00000001 + +#endif /* _SGX_DRIVER_INFO_H */ diff --git a/ubuntu/sgx/sgx_encl.c b/ubuntu/sgx/sgx_encl.c new file mode 100644 index 000000000000..99b89657ca70 --- /dev/null +++ b/ubuntu/sgx/sgx_encl.c @@ -0,0 +1,974 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include "sgx.h" +#include "sgx_wl.h" + + +struct sgx_add_page_req { + struct sgx_encl *encl; + struct sgx_encl_page *encl_page; + struct sgx_secinfo secinfo; + u16 mrmask; + struct list_head list; +}; + +/* A per-cpu cache for the last known values of IA32_SGXLEPUBKEYHASHx MSRs. */ +static DEFINE_PER_CPU(u64 [4], sgx_lepubkeyhash_cache); + +/** + * sgx_encl_find - find an enclave + * @mm: mm struct of the current process + * @addr: address in the ELRANGE + * @vma: the resulting VMA + * + * Finds an enclave identified by the given address. Gives back the VMA, that + * is part of the enclave, located in that address. The VMA is given back if it + * is a proper enclave VMA even if a &struct sgx_encl instance does not exist + * yet (enclave creation has not been performed). + * + * Return: + * 0 on success, + * -EINVAL if an enclave was not found, + * -ENOENT if the enclave has not been created yet + */ +int sgx_encl_find(struct mm_struct *mm, unsigned long addr, + struct vm_area_struct **vma) +{ + struct vm_area_struct *result; + struct sgx_encl *encl; + + result = find_vma(mm, addr); + if (!result || result->vm_ops != &sgx_vm_ops || addr < result->vm_start) + return -EINVAL; + + encl = result->vm_private_data; + *vma = result; + + return encl ? 0 : -ENOENT; +} + +static int sgx_measure(void *secs_page, + void *epc_page, + u16 mrmask) +{ + int ret = 0; + void *secs; + void *epc; + int i; + int j; + + for (i = 0, j = 1; i < 0x1000 && !ret; i += 0x100, j <<= 1) { + if (!(j & mrmask)) + continue; + + secs = sgx_get_page(secs_page); + epc = sgx_get_page(epc_page); + + ret = __eextend(secs, (void *)((unsigned long)epc + i)); + + sgx_put_page(epc); + sgx_put_page(secs); + } + + return ret; +} + +static int sgx_eadd(void *secs_page, + void *epc_page, + unsigned long linaddr, + struct sgx_secinfo *secinfo, + struct page *backing) +{ + struct sgx_pageinfo pginfo; + void *epc_page_vaddr; + int ret; + + pginfo.srcpge = (unsigned long)kmap_atomic(backing); + pginfo.secs = (unsigned long)sgx_get_page(secs_page); + epc_page_vaddr = sgx_get_page(epc_page); + + pginfo.linaddr = linaddr; + pginfo.secinfo = (unsigned long)secinfo; + ret = __eadd(&pginfo, epc_page_vaddr); + + sgx_put_page(epc_page_vaddr); + sgx_put_page((void *)(unsigned long)pginfo.secs); + kunmap_atomic((void *)(unsigned long)pginfo.srcpge); + + return ret; +} + +static bool sgx_process_add_page_req(struct sgx_add_page_req *req, + void *epc_page) +{ + struct sgx_encl_page *encl_page = req->encl_page; + struct sgx_encl *encl = req->encl; + struct vm_area_struct *vma; + struct page *backing; + unsigned long addr; + int ret; + + if (encl->flags & (SGX_ENCL_SUSPEND | SGX_ENCL_DEAD)) + return false; + + addr = SGX_ENCL_PAGE_ADDR(encl_page); + ret = sgx_encl_find(encl->mm, addr, &vma); + if (ret) + return false; + + backing = sgx_get_backing(encl, encl_page, false); + if (IS_ERR(backing)) + return false; + + /* Do not race with do_exit() */ + if (!atomic_read(&encl->mm->mm_users)) { + sgx_put_backing(backing, 0); + return false; + } + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)) + ret = vmf_insert_pfn(vma, addr, SGX_EPC_PFN(epc_page)); + if (ret != VM_FAULT_NOPAGE) { +#else + ret = vm_insert_pfn(vma, addr, SGX_EPC_PFN(epc_page)); + if (ret) { +#endif + sgx_put_backing(backing, 0); + return false; + } + + ret = sgx_eadd(encl->secs.epc_page, epc_page, addr, &req->secinfo, + backing); + + sgx_put_backing(backing, 0); + if (ret) { + sgx_warn(encl, "EADD returned %d\n", ret); + zap_vma_ptes(vma, addr, PAGE_SIZE); + return false; + } + + encl->secs_child_cnt++; + + ret = sgx_measure(encl->secs.epc_page, epc_page, req->mrmask); + if (ret) { + sgx_warn(encl, "EEXTEND returned %d\n", ret); + zap_vma_ptes(vma, addr, PAGE_SIZE); + return false; + } + + encl_page->encl = encl; + encl_page->epc_page = epc_page; + encl_page->desc |= SGX_ENCL_PAGE_LOADED; + sgx_test_and_clear_young(encl_page); + list_add_tail(&encl_page->list, &encl->load_list); + + return true; +} + +static void sgx_add_page_worker(struct work_struct *work) +{ + struct sgx_add_page_req *req; + bool skip_rest = false; + bool is_empty = false; + struct sgx_encl *encl; + void *epc_page; + + encl = container_of(work, struct sgx_encl, add_page_work); + + do { + schedule(); + + if (encl->flags & SGX_ENCL_DEAD) + skip_rest = true; + + mutex_lock(&encl->lock); + req = list_first_entry(&encl->add_page_reqs, + struct sgx_add_page_req, list); + list_del(&req->list); + is_empty = list_empty(&encl->add_page_reqs); + mutex_unlock(&encl->lock); + + if (skip_rest) + goto next; + + epc_page = sgx_alloc_page(0); + if (IS_ERR(epc_page)) { + skip_rest = true; + goto next; + } + + down_read(&encl->mm->mmap_sem); + mutex_lock(&encl->lock); + + if (!sgx_process_add_page_req(req, epc_page)) { + sgx_free_page(epc_page, encl); + skip_rest = true; + } + + mutex_unlock(&encl->lock); + up_read(&encl->mm->mmap_sem); + +next: + kfree(req); + } while (!kref_put(&encl->refcount, sgx_encl_release) && !is_empty); +} + +static u32 sgx_calc_ssaframesize(u32 miscselect, u64 xfrm) +{ + u32 size_max = PAGE_SIZE; + u32 size; + int i; + + for (i = 2; i < 64; i++) { + if (!((1 << i) & xfrm)) + continue; + + size = SGX_SSA_GPRS_SIZE + sgx_xsave_size_tbl[i]; + if (miscselect & SGX_MISC_EXINFO) + size += SGX_SSA_MISC_EXINFO_SIZE; + + if (size > size_max) + size_max = size; + } + + return (size_max + PAGE_SIZE - 1) >> PAGE_SHIFT; +} + +static int sgx_validate_secs(const struct sgx_secs *secs, + unsigned long ssaframesize) +{ + int i; + + if (secs->size < (2 * PAGE_SIZE) || + (secs->size & (secs->size - 1)) != 0) + return -EINVAL; + + if (secs->base & (secs->size - 1)) + return -EINVAL; + + if (secs->attributes & SGX_ATTR_RESERVED_MASK || + secs->miscselect & sgx_misc_reserved) + return -EINVAL; + + if (secs->attributes & SGX_ATTR_MODE64BIT) { +#ifdef CONFIG_X86_64 + if (secs->size > sgx_encl_size_max_64) + return -EINVAL; +#else + return -EINVAL; +#endif + } else { + /* On 64-bit architecture allow 32-bit encls only in + * the compatibility mode. + */ +#ifdef CONFIG_X86_64 + if (!test_thread_flag(TIF_ADDR32)) + return -EINVAL; +#endif + if (secs->size > sgx_encl_size_max_32) + return -EINVAL; + } + + if ((secs->xfrm & 0x3) != 0x3 || (secs->xfrm & ~sgx_xfrm_mask)) + return -EINVAL; + + /* Check that BNDREGS and BNDCSR are equal. */ + if (((secs->xfrm >> 3) & 1) != ((secs->xfrm >> 4) & 1)) + return -EINVAL; + + if (!secs->ssaframesize || ssaframesize > secs->ssaframesize) + return -EINVAL; + + for (i = 0; i < SGX_SECS_RESERVED1_SIZE; i++) + if (secs->reserved1[i]) + return -EINVAL; + + for (i = 0; i < SGX_SECS_RESERVED2_SIZE; i++) + if (secs->reserved2[i]) + return -EINVAL; + + for (i = 0; i < SGX_SECS_RESERVED3_SIZE; i++) + if (secs->reserved3[i]) + return -EINVAL; + + for (i = 0; i < SGX_SECS_RESERVED4_SIZE; i++) + if (secs->reserved4[i]) + return -EINVAL; + + return 0; +} + +static void sgx_mmu_notifier_release(struct mmu_notifier *mn, + struct mm_struct *mm) +{ + struct sgx_encl *encl = + container_of(mn, struct sgx_encl, mmu_notifier); + + mutex_lock(&encl->lock); + encl->flags |= SGX_ENCL_DEAD; + mutex_unlock(&encl->lock); +} + +static const struct mmu_notifier_ops sgx_mmu_notifier_ops = { + .release = sgx_mmu_notifier_release, +}; + +static int sgx_init_page(struct sgx_encl *encl, struct sgx_encl_page *entry, + unsigned long addr) +{ + struct sgx_va_page *va_page; + void *epc_page = NULL; + void *ptr; + int ret = 0; + + /* fast path */ + mutex_lock(&encl->lock); + if (encl->page_cnt % SGX_VA_SLOT_COUNT) + goto out; + mutex_unlock(&encl->lock); + + /* slow path */ + epc_page = sgx_alloc_page(0); + if (IS_ERR(epc_page)) + return PTR_ERR(epc_page); + + mutex_lock(&encl->lock); + if (encl->page_cnt % SGX_VA_SLOT_COUNT) { + sgx_free_page(epc_page, encl); + goto out; + } + + ptr = sgx_get_page(epc_page); + ret = __epa(ptr); + sgx_put_page(ptr); + if (ret) { + sgx_crit(encl, "EPA returned %d\n", ret); + sgx_free_page(epc_page, encl); + ret = -EFAULT; + goto out; + } + + va_page = kzalloc(sizeof(*va_page), GFP_KERNEL); + if (!va_page) { + sgx_free_page(epc_page, encl); + ret = -ENOMEM; + goto out; + } + + atomic_inc(&sgx_va_pages_cnt); + va_page->epc_page = epc_page; + list_add(&va_page->list, &encl->va_pages); + +out: + if (!ret) { + entry->desc = addr; + encl->page_cnt++; + } + mutex_unlock(&encl->lock); + return ret; +} + +/** + * sgx_encl_alloc - allocate memory for an enclave and set attributes + * + * @secs: SECS data (must be page aligned) + * + * Allocates a new &struct sgx_encl instance. Validates SECS attributes, creates + * backing storage for the enclave and sets enclave attributes to sane initial + * values. + * + * Return: + * &struct sgx_encl instance on success, + * system error on failure + */ +struct sgx_encl *sgx_encl_alloc(struct sgx_secs *secs) +{ + unsigned long ssaframesize; + struct sgx_encl *encl; + struct file *backing; + struct file *pcmd; + + ssaframesize = sgx_calc_ssaframesize(secs->miscselect, secs->xfrm); + if (sgx_validate_secs(secs, ssaframesize)) + return ERR_PTR(-EINVAL); + + backing = shmem_file_setup("[dev/sgx]", secs->size + PAGE_SIZE, + VM_NORESERVE); + if (IS_ERR(backing)) + return (void *)backing; + + pcmd = shmem_file_setup("[dev/sgx]", (secs->size + PAGE_SIZE) >> 5, + VM_NORESERVE); + if (IS_ERR(pcmd)) { + fput(backing); + return (void *)pcmd; + } + + encl = kzalloc(sizeof(*encl), GFP_KERNEL); + if (!encl) { + fput(backing); + fput(pcmd); + return ERR_PTR(-ENOMEM); + } + + encl->attributes = secs->attributes; + encl->allowed_attributes = SGX_ATTR_ALLOWED_MASK; + encl->xfrm = secs->xfrm; + + kref_init(&encl->refcount); + INIT_LIST_HEAD(&encl->add_page_reqs); + INIT_LIST_HEAD(&encl->va_pages); + INIT_RADIX_TREE(&encl->page_tree, GFP_KERNEL); + INIT_LIST_HEAD(&encl->load_list); + INIT_LIST_HEAD(&encl->encl_list); + mutex_init(&encl->lock); + INIT_WORK(&encl->add_page_work, sgx_add_page_worker); + + encl->mm = current->mm; + encl->base = secs->base; + encl->size = secs->size; + encl->ssaframesize = secs->ssaframesize; + encl->backing = backing; + encl->pcmd = pcmd; + + return encl; +} + +/** + * sgx_encl_create - create an enclave + * + * @encl: an enclave + * @secs: page aligned SECS data + * + * Validates SECS attributes, allocates an EPC page for the SECS and creates + * the enclave by performing ECREATE. + * + * Return: + * 0 on success, + * system error on failure + */ +int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) +{ + struct vm_area_struct *vma; + struct sgx_pageinfo pginfo; + struct sgx_secinfo secinfo; + void *secs_epc; + void *secs_vaddr; + long ret; + + secs_epc = sgx_alloc_page(0); + if (IS_ERR(secs_epc)) { + ret = PTR_ERR(secs_epc); + return ret; + } + + encl->secs.epc_page = secs_epc; + encl->tgid = get_pid(task_tgid(current)); + + ret = sgx_init_page(encl, &encl->secs, encl->base + encl->size); + if (ret) + return ret; + + secs_vaddr = sgx_get_page(secs_epc); + + pginfo.srcpge = (unsigned long)secs; + pginfo.linaddr = 0; + pginfo.secinfo = (unsigned long)&secinfo; + pginfo.secs = 0; + memset(&secinfo, 0, sizeof(secinfo)); + ret = __ecreate((void *)&pginfo, secs_vaddr); + + sgx_put_page(secs_vaddr); + + if (ret) { + sgx_dbg(encl, "ECREATE returned %ld\n", ret); + ret = -EFAULT; + return ret; + } + + if (secs->attributes & SGX_ATTR_DEBUG) + encl->flags |= SGX_ENCL_DEBUG; + + encl->mmu_notifier.ops = &sgx_mmu_notifier_ops; + ret = mmu_notifier_register(&encl->mmu_notifier, encl->mm); + if (ret) { + if (ret == -EINTR) + ret = -ERESTARTSYS; + encl->mmu_notifier.ops = NULL; + return ret; + } + + down_read(¤t->mm->mmap_sem); + ret = sgx_encl_find(current->mm, secs->base, &vma); + if (ret != -ENOENT) { + if (!ret) + ret = -EINVAL; + up_read(¤t->mm->mmap_sem); + return ret; + } + + if (vma->vm_start != secs->base || + vma->vm_end != (secs->base + secs->size) || + vma->vm_pgoff != 0) { + ret = -EINVAL; + up_read(¤t->mm->mmap_sem); + return ret; + } + + vma->vm_private_data = encl; + up_read(¤t->mm->mmap_sem); + + mutex_lock(&sgx_encl_list_lock); + list_add_tail(&encl->encl_list, &sgx_encl_list); + mutex_unlock(&sgx_encl_list_lock); + + return 0; +} + +static int sgx_validate_secinfo(struct sgx_secinfo *secinfo) +{ + u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK; + u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK; + int i; + + if ((secinfo->flags & SGX_SECINFO_RESERVED_MASK) || + ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R)) || + (page_type != SGX_SECINFO_TCS && + page_type != SGX_SECINFO_REG)) + return -EINVAL; + + for (i = 0; i < sizeof(secinfo->reserved) / sizeof(u64); i++) + if (secinfo->reserved[i]) + return -EINVAL; + + return 0; +} + +static bool sgx_validate_offset(struct sgx_encl *encl, unsigned long offset) +{ + if (offset & (PAGE_SIZE - 1)) + return false; + + if (offset >= encl->size) + return false; + + return true; +} + +static int sgx_validate_tcs(struct sgx_encl *encl, struct sgx_tcs *tcs) +{ + int i; + + if (tcs->flags & SGX_TCS_RESERVED_MASK) { + sgx_dbg(encl, "%s: invalid TCS flags = 0x%lx\n", + __func__, (unsigned long)tcs->flags); + return -EINVAL; + } + + if (tcs->flags & SGX_TCS_DBGOPTIN) { + sgx_dbg(encl, "%s: DBGOPTIN TCS flag is set, EADD will clear it\n", + __func__); + return -EINVAL; + } + + if (!sgx_validate_offset(encl, tcs->ossa)) { + sgx_dbg(encl, "%s: invalid OSSA: 0x%lx\n", __func__, + (unsigned long)tcs->ossa); + return -EINVAL; + } + + if (!sgx_validate_offset(encl, tcs->ofsbase)) { + sgx_dbg(encl, "%s: invalid OFSBASE: 0x%lx\n", __func__, + (unsigned long)tcs->ofsbase); + return -EINVAL; + } + + if (!sgx_validate_offset(encl, tcs->ogsbase)) { + sgx_dbg(encl, "%s: invalid OGSBASE: 0x%lx\n", __func__, + (unsigned long)tcs->ogsbase); + return -EINVAL; + } + + if ((tcs->fslimit & 0xFFF) != 0xFFF) { + sgx_dbg(encl, "%s: invalid FSLIMIT: 0x%x\n", __func__, + tcs->fslimit); + return -EINVAL; + } + + if ((tcs->gslimit & 0xFFF) != 0xFFF) { + sgx_dbg(encl, "%s: invalid GSLIMIT: 0x%x\n", __func__, + tcs->gslimit); + return -EINVAL; + } + + for (i = 0; i < sizeof(tcs->reserved) / sizeof(u64); i++) + if (tcs->reserved[i]) + return -EINVAL; + + return 0; +} + +static int __sgx_encl_add_page(struct sgx_encl *encl, + struct sgx_encl_page *encl_page, + unsigned long addr, + void *data, + struct sgx_secinfo *secinfo, + unsigned int mrmask) +{ + u64 page_type = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK; + struct sgx_add_page_req *req = NULL; + struct page *backing; + void *backing_ptr; + int ret; + int empty; + + if (sgx_validate_secinfo(secinfo)) + return -EINVAL; + + if (page_type == SGX_SECINFO_TCS) { + ret = sgx_validate_tcs(encl, data); + if (ret) + return ret; + } + + ret = sgx_init_page(encl, encl_page, addr); + if (ret) + return ret; + + mutex_lock(&encl->lock); + + if (encl->flags & (SGX_ENCL_INITIALIZED | SGX_ENCL_DEAD)) { + ret = -EINVAL; + goto out; + } + + if (radix_tree_lookup(&encl->page_tree, addr >> PAGE_SHIFT)) { + ret = -EEXIST; + goto out; + } + + req = kzalloc(sizeof(*req), GFP_KERNEL); + if (!req) { + ret = -ENOMEM; + goto out; + } + + backing = sgx_get_backing(encl, encl_page, false); + if (IS_ERR((void *)backing)) { + ret = PTR_ERR((void *)backing); + goto out; + } + + ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc), + encl_page); + if (ret) { + sgx_put_backing(backing, false /* write */); + goto out; + } + + backing_ptr = kmap(backing); + memcpy(backing_ptr, data, PAGE_SIZE); + kunmap(backing); + + if (page_type == SGX_SECINFO_TCS) + encl_page->desc |= SGX_ENCL_PAGE_TCS; + + memcpy(&req->secinfo, secinfo, sizeof(*secinfo)); + + req->encl = encl; + req->encl_page = encl_page; + req->mrmask = mrmask; + empty = list_empty(&encl->add_page_reqs); + kref_get(&encl->refcount); + list_add_tail(&req->list, &encl->add_page_reqs); + if (empty) + queue_work(sgx_add_page_wq, &encl->add_page_work); + + sgx_put_backing(backing, true /* write */); + + mutex_unlock(&encl->lock); + return 0; +out: + kfree(req); + mutex_unlock(&encl->lock); + return ret; +} + + +/** + * sgx_encl_add_page - add a page to the enclave + * + * @encl: an enclave + * @addr: page address in the ELRANGE + * @data: page data + * @secinfo: page permissions + * @mrmask: bitmask to select the 256 byte chunks to be measured + * + * Creates a new enclave page and enqueues an EADD operation that will be + * processed by a worker thread later on. + * + * Return: + * 0 on success, + * system error on failure + */ +int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, void *data, + struct sgx_secinfo *secinfo, unsigned int mrmask) +{ + struct sgx_encl_page *page; + int ret; + + page = kzalloc(sizeof(*page), GFP_KERNEL); + if (!page) + return -ENOMEM; + + ret = __sgx_encl_add_page(encl, page, addr, data, secinfo, mrmask); + + if (ret) + kfree(page); + + return ret; +} + + +static void sgx_update_lepubkeyhash_msrs(u64 *lepubkeyhash, bool enforce) +{ + u64 *cache; + int i; + + cache = per_cpu(sgx_lepubkeyhash_cache, smp_processor_id()); + for (i = 0; i < 4; i++) { + if (enforce || (lepubkeyhash[i] != cache[i])) { + wrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, lepubkeyhash[i]); + cache[i] = lepubkeyhash[i]; + } + } +} + + +static int sgx_einit(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct, + struct sgx_einittoken *token, u64 *lepubkeyhash) +{ + void *secs_epc = encl->secs.epc_page; + void *secs_va; + int ret; + + secs_va = sgx_get_page(secs_epc); + + if (!boot_cpu_has(X86_FEATURE_SGX_LC)) { + ret = __einit(sigstruct, token, secs_va); + goto out; + } + + token->payload.valid = 0; + + preempt_disable(); + sgx_update_lepubkeyhash_msrs(lepubkeyhash, false); + ret = __einit(sigstruct, token, secs_va); + if (ret == SGX_INVALID_EINITTOKEN) { + sgx_update_lepubkeyhash_msrs(lepubkeyhash, true); + ret = __einit(sigstruct, token, secs_va); + } + preempt_enable(); + +out: + sgx_put_page(secs_va); + + return ret; +} + +/** + * sgx_encl_init - perform EINIT for the given enclave + * + * @encl: an enclave + * @sigstruct: SIGSTRUCT for the enclave + * @token: EINITTOKEN for the enclave + * + * Retries a few times in order to perform EINIT operation on an enclave + * because there could be potentially an interrupt storm. + * + * Return: + * 0 on success, + * -FAULT on a CPU exception during EINIT, + * SGX error code + */ +int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct, + struct sgx_einittoken *token) +{ + u64 mrsigner[4]; + int ret; + int i; + int j; + + ret = sgx_get_key_hash_simple(sigstruct->modulus, mrsigner); + if (ret) + return ret; + + if((encl->attributes & ~encl->allowed_attributes) && (encl->attributes & SGX_ATTR_PROVISIONKEY)) { + for(i = 0; i < (sizeof(G_SERVICE_ENCLAVE_MRSIGNER) / sizeof(G_SERVICE_ENCLAVE_MRSIGNER[0])); i++) { + if(0 == memcmp(&G_SERVICE_ENCLAVE_MRSIGNER[i], mrsigner, sizeof(G_SERVICE_ENCLAVE_MRSIGNER[0]))) { + encl->allowed_attributes |= SGX_ATTR_PROVISIONKEY; + break; + } + } + } + + /* Check that the required attributes have been authorized. */ + if (encl->attributes & ~encl->allowed_attributes) + return -EINVAL; + + flush_work(&encl->add_page_work); + + mutex_lock(&encl->lock); + + if (encl->flags & SGX_ENCL_INITIALIZED) { + mutex_unlock(&encl->lock); + return 0; + } + + for (i = 0; i < SGX_EINIT_SLEEP_COUNT; i++) { + for (j = 0; j < SGX_EINIT_SPIN_COUNT; j++) { + ret = sgx_einit(encl, sigstruct, token, mrsigner); + if (ret == SGX_UNMASKED_EVENT) + continue; + else + break; + } + + if (ret != SGX_UNMASKED_EVENT) + break; + + msleep_interruptible(SGX_EINIT_SLEEP_TIME); + + if (signal_pending(current)) { + mutex_unlock(&encl->lock); + return -ERESTARTSYS; + } + } + + mutex_unlock(&encl->lock); + + if (ret) { + if (ret > 0) + sgx_dbg(encl, "EINIT returned %d\n", ret); + return ret; + } + + encl->flags |= SGX_ENCL_INITIALIZED; + return 0; +} + +void sgx_encl_release(struct kref *ref) +{ + struct sgx_encl *encl = container_of(ref, struct sgx_encl, refcount); + struct sgx_encl_page *entry; + struct sgx_va_page *va_page; + struct radix_tree_iter iter; + void **slot; + + mutex_lock(&sgx_encl_list_lock); + if (!list_empty(&encl->encl_list)) + list_del(&encl->encl_list); + mutex_unlock(&sgx_encl_list_lock); + + put_pid(encl->tgid); + + if (encl->mmu_notifier.ops) + mmu_notifier_unregister_no_release(&encl->mmu_notifier, + encl->mm); + + list_for_each_entry(entry, &encl->load_list, list) + sgx_free_page(entry->epc_page, encl); + + radix_tree_for_each_slot(slot, &encl->page_tree, &iter, 0) { + entry = *slot; + radix_tree_delete(&encl->page_tree, PFN_DOWN(entry->desc)); + kfree(entry); + } + + while (!list_empty(&encl->va_pages)) { + va_page = list_first_entry(&encl->va_pages, + struct sgx_va_page, list); + list_del(&va_page->list); + sgx_free_page(va_page->epc_page, encl); + kfree(va_page); + atomic_dec(&sgx_va_pages_cnt); + } + + if (!(encl->flags & SGX_ENCL_SECS_EVICTED)) + sgx_free_page(encl->secs.epc_page, encl); + + if (encl->backing) + fput(encl->backing); + + if (encl->pcmd) + fput(encl->pcmd); + + kfree(encl); +} diff --git a/ubuntu/sgx/sgx_ioctl.c b/ubuntu/sgx/sgx_ioctl.c new file mode 100644 index 000000000000..e8267f9da317 --- /dev/null +++ b/ubuntu/sgx/sgx_ioctl.c @@ -0,0 +1,332 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sgx.h" + +static int sgx_encl_get(unsigned long addr, struct sgx_encl **encl) +{ + struct mm_struct *mm = current->mm; + struct vm_area_struct *vma; + int ret; + + if (addr & (PAGE_SIZE - 1)) + return -EINVAL; + + down_read(&mm->mmap_sem); + + ret = sgx_encl_find(mm, addr, &vma); + if (!ret) { + *encl = vma->vm_private_data; + + if ((*encl)->flags & SGX_ENCL_SUSPEND) + ret = SGX_POWER_LOST_ENCLAVE; + else + kref_get(&(*encl)->refcount); + } + + up_read(&mm->mmap_sem); + return ret; +} + +/** + * sgx_ioc_enclave_create - handler for %SGX_IOC_ENCLAVE_CREATE + * @filep: open file to /dev/sgx + * @cmd: the command value + * @arg: pointer to the &struct sgx_enclave_create + * + * Validates SECS attributes, allocates an EPC page for the SECS and performs + * ECREATE. + * + * Return: + * 0 on success, + * system error on failure + */ +static long sgx_ioc_enclave_create(struct file *filep, unsigned int cmd, + unsigned long arg) +{ + struct sgx_enclave_create *createp = (struct sgx_enclave_create *)arg; + struct sgx_secs *secs; + struct sgx_encl *encl; + int ret; + + secs = kzalloc(sizeof(*secs), GFP_KERNEL); + if (!secs) + return -ENOMEM; + + ret = copy_from_user(secs, (void __user *)createp->src, sizeof(*secs)); + if (ret) + goto out; + + encl = sgx_encl_alloc(secs); + if (IS_ERR(encl)) { + ret = PTR_ERR(encl); + goto out; + } + + ret = sgx_encl_create(encl, secs); + if (ret) + kref_put(&encl->refcount, sgx_encl_release); + +out: + kfree(secs); + return ret; +} + +/** + * sgx_ioc_enclave_add_page - handler for %SGX_IOC_ENCLAVE_ADD_PAGE + * + * @filep: open file to /dev/sgx + * @cmd: the command value + * @arg: pointer to the &struct sgx_enclave_add_page + * + * Creates a new enclave page and enqueues an EADD operation that will be + * processed by a worker thread later on. + * + * Return: + * 0 on success, + * system error on failure + */ +static long sgx_ioc_enclave_add_page(struct file *filep, unsigned int cmd, + unsigned long arg) +{ + struct sgx_enclave_add_page *addp = (void *)arg; + struct sgx_secinfo secinfo; + struct sgx_encl *encl; + struct page *data_page; + void *data; + int ret; + + ret = sgx_encl_get(addp->addr, &encl); + if (ret) + return ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo, + sizeof(secinfo))) { + kref_put(&encl->refcount, sgx_encl_release); + return -EFAULT; + } + + data_page = alloc_page(GFP_HIGHUSER); + if (!data_page) { + kref_put(&encl->refcount, sgx_encl_release); + return -ENOMEM; + } + + data = kmap(data_page); + + ret = copy_from_user((void *)data, (void __user *)addp->src, PAGE_SIZE); + if (ret) + goto out; + + ret = sgx_encl_add_page(encl, addp->addr, data, &secinfo, addp->mrmask); + if (ret) + goto out; + +out: + kref_put(&encl->refcount, sgx_encl_release); + kunmap(data_page); + __free_page(data_page); + return ret; +} + +/** + * sgx_ioc_enclave_init - handler for %SGX_IOC_ENCLAVE_INIT + * + * @filep: open file to /dev/sgx + * @cmd: the command value + * @arg: pointer to the &struct sgx_enclave_init + * + * Flushes the remaining enqueued EADD operations and performs EINIT. + * + * Return: + * 0 on success, + * system error on failure + */ +static long sgx_ioc_enclave_init(struct file *filep, unsigned int cmd, + unsigned long arg) +{ + struct sgx_enclave_init *initp = (struct sgx_enclave_init *)arg; + struct sgx_sigstruct *sigstruct; + struct sgx_einittoken *einittoken; + struct sgx_encl *encl; + struct page *initp_page; + int ret; + + initp_page = alloc_page(GFP_HIGHUSER); + if (!initp_page) + return -ENOMEM; + + sigstruct = kmap(initp_page); + einittoken = (struct sgx_einittoken *) + ((unsigned long)sigstruct + PAGE_SIZE / 2); + + ret = copy_from_user(sigstruct, (void __user *)initp->sigstruct, + sizeof(*sigstruct)); + if (ret) + goto out; + + ret = sgx_encl_get(initp->addr, &encl); + if (ret) + goto out; + + ret = sgx_encl_init(encl, sigstruct, einittoken); + + kref_put(&encl->refcount, sgx_encl_release); + +out: + kunmap(initp_page); + __free_page(initp_page); + return ret; +} + +/** + * sgx_ioc_enclave_set_attribute - handler for %SGX_IOC_ENCLAVE_SET_ATTRIBUTE + * @filep: open file to /dev/sgx + * @cmd: the command value + * @arg: pointer to a struct sgx_enclave_set_attribute instance + * + * Mark the enclave as being allowed to access a restricted attribute bit. + * The requested attribute is specified via the attribute_fd field in the + * provided struct sgx_enclave_set_attribute. The attribute_fd must be a + * handle to an SGX attribute file, e.g. “/dev/sgx/provision". + * + * Failure to explicitly request access to a restricted attribute will cause + * sgx_ioc_enclave_init() to fail. Currently, the only restricted attribute + * is access to the PROVISION_KEY. + * + * Note, access to the EINITTOKEN_KEY is disallowed entirely. + * + * Return: 0 on success, -errno otherwise + */ +static long sgx_ioc_enclave_set_attribute(struct file *filep, unsigned int cmd, + unsigned long arg) +{ + struct sgx_enclave_set_attribute *params = (void *)arg; + struct sgx_encl *encl; + struct file *attribute_file; + int ret; + + attribute_file = fget(params->attribute_fd); + if (!attribute_file || !attribute_file->f_op) + return -EINVAL; + + if (attribute_file->f_op != &sgx_provision_fops) { + ret = -EINVAL; + goto out; + } + + ret = sgx_encl_get(params->addr, &encl); + if (ret) + goto out; + + encl->allowed_attributes |= SGX_ATTR_PROVISIONKEY; + + kref_put(&encl->refcount, sgx_encl_release); + +out: + fput(attribute_file); + return ret; +} + +typedef long (*sgx_ioc_t)(struct file *filep, unsigned int cmd, + unsigned long arg); + +long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) +{ + char data[256]; + sgx_ioc_t handler = NULL; + long ret; + + switch (cmd) { + case SGX_IOC_ENCLAVE_CREATE: + handler = sgx_ioc_enclave_create; + break; + case SGX_IOC_ENCLAVE_ADD_PAGE: + handler = sgx_ioc_enclave_add_page; + break; + case SGX_IOC_ENCLAVE_INIT: + handler = sgx_ioc_enclave_init; + break; + case SGX_IOC_ENCLAVE_SET_ATTRIBUTE: + handler = sgx_ioc_enclave_set_attribute; + break; + default: + return -ENOIOCTLCMD; + } + + if (copy_from_user(data, (void __user *)arg, _IOC_SIZE(cmd))) + return -EFAULT; + + ret = handler(filep, cmd, (unsigned long)((void *)data)); + if (!ret && (cmd & IOC_OUT)) { + if (copy_to_user((void __user *)arg, data, _IOC_SIZE(cmd))) + return -EFAULT; + } + + return ret; +} diff --git a/ubuntu/sgx/sgx_main.c b/ubuntu/sgx/sgx_main.c new file mode 100644 index 000000000000..65e0bbe2af1a --- /dev/null +++ b/ubuntu/sgx/sgx_main.c @@ -0,0 +1,498 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include +#include + +#include "sgx.h" +#include "sgx_version.h" +#include "sgx_driver_info.h" + + +MODULE_DESCRIPTION(DRV_DESCRIPTION); +MODULE_AUTHOR("Jarkko Sakkinen "); +MODULE_LICENSE("Dual BSD/GPL"); +MODULE_VERSION(DRV_VERSION); + + +/* + * Global data. + */ + +struct workqueue_struct *sgx_add_page_wq; +u64 sgx_encl_size_max_32; +u64 sgx_encl_size_max_64; +u64 sgx_xfrm_mask = 0x3; +u32 sgx_misc_reserved; +u32 sgx_xsave_size_tbl[64]; + + +// From intel_sgx.c +bool sgx_enabled = false; + +static bool sgx_is_enabled(void) +{ + unsigned int eax; + unsigned int ebx; + unsigned int ecx; + unsigned int edx; + unsigned long fc; + + if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) { + pr_err("intel_sgx: Not an Intel CPU vendor!\n"); + return false; + } + + if (!boot_cpu_has(X86_FEATURE_SGX)) { + pr_err("intel_sgx: SGX is not supported on the platform!\n"); + return false; + } + + if (!boot_cpu_has(X86_FEATURE_SGX_LC)) { + pr_err("intel_sgx: FLC feature is not supported on the platform!\n"); + return false; + } + + if (!boot_cpu_has(X86_FEATURE_AES)) { + pr_err("intel_sgx: AES-NI instructions are not supported on the platform!\n"); + return false; + } + + rdmsrl(MSR_IA32_FEATURE_CONTROL, fc); + if (!(fc & FEATURE_CONTROL_LOCKED)) { + pr_err("intel_sgx: FEATURE_CONTROL MSR is not locked!\n"); + return false; + } + + if (!(fc & FEATURE_CONTROL_SGX_ENABLE)) { + pr_err("intel_sgx: SGX is not enalbed in FEATURE_CONTROL MSR!\n"); + return false; + } + + if (!(fc & FEATURE_CONTROL_SGX_LE_WR)) { + pr_err("intel_sgx: FLC feature is not enalbed in FEATURE_CONTROL MSR!\n"); + return false; + } + + cpuid(0, &eax, &ebx, &ecx, &edx); + if (eax < SGX_CPUID) { + pr_err("intel_sgx: SGX CPUID leaf is not supported!\n"); + return false; + } + + cpuid_count(SGX_CPUID, SGX_CPUID_CAPABILITIES, &eax, &ebx, &ecx, &edx); + + /* The first bit indicates support for SGX1 instruction set. */ + if (!(eax & 1)) { + pr_err("intel_sgx: Platform does not support SGX!\n"); + return false; + } + + return true; +} + +static int sgx_init(void) +{ + sgx_enabled = sgx_is_enabled(); + return 0; +} + + +static DECLARE_RWSEM(sgx_file_sem); + + +#ifdef CONFIG_COMPAT +long sgx_compat_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) +{ + return sgx_ioctl(filep, cmd, arg); +} +#endif + +static int sgx_mmap(struct file *file, struct vm_area_struct *vma) +{ + vma->vm_ops = &sgx_vm_ops; + vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO | + VM_DONTCOPY; + + return 0; +} + +static unsigned long sgx_get_unmapped_area(struct file *file, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags) +{ + if (len < 2 * PAGE_SIZE || (len & (len - 1)) || flags & MAP_PRIVATE) + return -EINVAL; + + /* On 64-bit architecture, allow mmap() to exceed 32-bit encl + * limit only if the task is not running in 32-bit compatibility + * mode. + */ + if (len > sgx_encl_size_max_32) +#ifdef CONFIG_X86_64 + if (test_thread_flag(TIF_ADDR32)) + return -EINVAL; +#else + return -EINVAL; +#endif + +#ifdef CONFIG_X86_64 + if (len > sgx_encl_size_max_64) + return -EINVAL; +#endif + + addr = current->mm->get_unmapped_area(file, addr, 2 * len, pgoff, + flags); + if (IS_ERR_VALUE(addr)) + return addr; + + addr = (addr + (len - 1)) & ~(len - 1); + + return addr; +} + +const struct file_operations sgx_fops = { + .owner = THIS_MODULE, + .unlocked_ioctl = sgx_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sgx_compat_ioctl, +#endif + .mmap = sgx_mmap, + .get_unmapped_area = sgx_get_unmapped_area, +}; + +const struct file_operations sgx_provision_fops = { + .owner = THIS_MODULE, +}; + +static int sgx_pm_suspend(struct device *dev) +{ + struct sgx_encl *encl; + + list_for_each_entry(encl, &sgx_encl_list, encl_list) { + sgx_invalidate(encl, false); + encl->flags |= SGX_ENCL_SUSPEND; + flush_work(&encl->add_page_work); + } + + return 0; +} + +static SIMPLE_DEV_PM_OPS(sgx_drv_pm, sgx_pm_suspend, NULL); + + +static struct bus_type sgx_bus_type = { + .name = "sgx", +}; + +struct sgx_context { + struct device dev; + struct cdev cdev; + struct device provision_dev; + struct cdev provision_cdev; + struct kobject *kobj_dir; +}; + +static dev_t sgx_devt; + +static void sgx_dev_release(struct device *dev) +{ + struct sgx_context *ctx = container_of(dev, struct sgx_context, dev); + + kfree(ctx); +} + +static struct sgx_context *sgx_ctx_alloc(struct device *parent) +{ + struct sgx_context *ctx; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return ERR_PTR(-ENOMEM); + + // /dev/sgx + device_initialize(&ctx->dev); + + ctx->dev.bus = &sgx_bus_type; + ctx->dev.parent = parent; + ctx->dev.devt = MKDEV(MAJOR(sgx_devt), 0); + ctx->dev.release = sgx_dev_release; + + dev_set_name(&ctx->dev, "sgx"); + + cdev_init(&ctx->cdev, &sgx_fops); + ctx->cdev.owner = THIS_MODULE; + + // /dev/sgx_prv + device_initialize(&ctx->provision_dev); + + ctx->provision_dev.bus = &sgx_bus_type; + ctx->provision_dev.parent = parent; + ctx->provision_dev.devt = MKDEV(MAJOR(sgx_devt), 1); + ctx->provision_dev.release = sgx_dev_release; + + dev_set_name(&ctx->provision_dev, "sgx_prv"); + + cdev_init(&ctx->provision_cdev, &sgx_provision_fops); + ctx->provision_cdev.owner = THIS_MODULE; + + // device + dev_set_drvdata(parent, ctx); + + return ctx; +} + +static struct sgx_context *sgxm_ctx_alloc(struct device *parent) +{ + struct sgx_context *ctx; + int rc; + + ctx = sgx_ctx_alloc(parent); + if (IS_ERR(ctx)) + return ctx; + + rc = devm_add_action_or_reset(parent, (void (*)(void *))put_device, + &ctx->dev); + if (rc) { + kfree(ctx); + return ERR_PTR(rc); + } + + return ctx; +} + +static ssize_t info_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sprintf(buf, "0x%08X\n", SGX_DRIVER_INFO_DCAP); +} + +static ssize_t version_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sprintf(buf, "v" DRV_VERSION "\n"); +} + +struct kobj_attribute info_attr = __ATTR_RO(info); +struct kobj_attribute version_attr = __ATTR_RO(version); + +static int sgx_dev_init(struct device *parent) +{ + struct sgx_context *sgx_dev; + unsigned int eax; + unsigned int ebx; + unsigned int ecx; + unsigned int edx; + int ret; + int i; + + pr_info("intel_sgx: " DRV_DESCRIPTION " v" DRV_VERSION "\n"); + + sgx_dev = sgxm_ctx_alloc(parent); + + cpuid_count(SGX_CPUID, SGX_CPUID_CAPABILITIES, &eax, &ebx, &ecx, &edx); + /* Only allow misc bits supported by the driver. */ + sgx_misc_reserved = ~ebx | SGX_MISC_RESERVED_MASK; +#ifdef CONFIG_X86_64 + sgx_encl_size_max_64 = 1ULL << ((edx >> 8) & 0xFF); +#endif + sgx_encl_size_max_32 = 1ULL << (edx & 0xFF); + + if (boot_cpu_has(X86_FEATURE_OSXSAVE)) { + cpuid_count(SGX_CPUID, SGX_CPUID_ATTRIBUTES, &eax, &ebx, &ecx, + &edx); + sgx_xfrm_mask = (((u64)edx) << 32) + (u64)ecx; + + for (i = 2; i < 64; i++) { + cpuid_count(0x0D, i, &eax, &ebx, &ecx, &edx); + if ((1 << i) & sgx_xfrm_mask) + sgx_xsave_size_tbl[i] = eax + ebx; + } + } + + ret = sgx_page_cache_init(parent); + if (ret) + return ret; + + sgx_add_page_wq = alloc_workqueue("intel_sgx-add-page-wq", + WQ_UNBOUND | WQ_FREEZABLE, 1); + if (!sgx_add_page_wq) { + pr_err("intel_sgx: alloc_workqueue() failed\n"); + ret = -ENOMEM; + goto out_page_cache; + } + + ret = cdev_device_add(&sgx_dev->cdev, &sgx_dev->dev); + if (ret) + goto out_workqueue; + + ret = cdev_device_add(&sgx_dev->provision_cdev, &sgx_dev->provision_dev); + if (ret) + goto out_workqueue; + + sgx_dev->kobj_dir = kobject_create_and_add("sgx", kernel_kobj); + sysfs_create_file(sgx_dev->kobj_dir, &info_attr.attr); + sysfs_create_file(sgx_dev->kobj_dir, &version_attr.attr); + + return 0; + +out_workqueue: + destroy_workqueue(sgx_add_page_wq); +out_page_cache: + sgx_page_cache_teardown(); + return ret; +} + + +static int sgx_drv_probe(struct platform_device *pdev) +{ + sgx_init(); + + if (!sgx_enabled) + return -ENODEV; + + return sgx_dev_init(&pdev->dev); +} + +static int sgx_drv_remove(struct platform_device *pdev) +{ + struct sgx_context *ctx = dev_get_drvdata(&pdev->dev); + + sysfs_remove_file(ctx->kobj_dir, &info_attr.attr); + sysfs_remove_file(ctx->kobj_dir, &version_attr.attr); + kobject_put(ctx->kobj_dir); + + cdev_device_del(&ctx->cdev, &ctx->dev); + cdev_device_del(&ctx->provision_cdev, &ctx->provision_dev); + + destroy_workqueue(sgx_add_page_wq); + sgx_page_cache_teardown(); + + return 0; +} + +#ifdef CONFIG_ACPI +static struct acpi_device_id sgx_device_ids[] = { + {"INT0E0C", 0}, + {"", 0}, +}; +MODULE_DEVICE_TABLE(acpi, sgx_device_ids); +#endif + +static struct platform_driver sgx_drv = { + .probe = sgx_drv_probe, + .remove = sgx_drv_remove, + .driver = { + .name = "intel_sgx", + .pm = &sgx_drv_pm, + .acpi_match_table = ACPI_PTR(sgx_device_ids), + }, +}; + +static int __init sgx_drv_subsys_init(void) +{ + int ret; + + ret = bus_register(&sgx_bus_type); + if (ret) + return ret; + + ret = alloc_chrdev_region(&sgx_devt, 0, 1, "sgx"); + if (ret < 0) { + bus_unregister(&sgx_bus_type); + return ret; + } + + return 0; +} + +static void sgx_drv_subsys_exit(void) +{ + bus_unregister(&sgx_bus_type); + unregister_chrdev_region(sgx_devt, 1); +} + +static int __init sgx_drv_init(void) +{ + int ret; + + ret = sgx_drv_subsys_init(); + if (ret) + return ret; + + ret = platform_driver_register(&sgx_drv); + if (ret) + sgx_drv_subsys_exit(); + + return ret; +} +module_init(sgx_drv_init); + +static void __exit sgx_drv_exit(void) +{ + platform_driver_unregister(&sgx_drv); + sgx_drv_subsys_exit(); +} +module_exit(sgx_drv_exit); + diff --git a/ubuntu/sgx/sgx_page_cache.c b/ubuntu/sgx/sgx_page_cache.c new file mode 100644 index 000000000000..751893d2f688 --- /dev/null +++ b/ubuntu/sgx/sgx_page_cache.c @@ -0,0 +1,596 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include +#include +#include +#include +#include "sgx.h" + +#define SGX_NR_LOW_PAGES 32 +#define SGX_NR_HIGH_PAGES 64 +#define SGX_NR_TO_SCAN 16 + +LIST_HEAD(sgx_encl_list); +DEFINE_MUTEX(sgx_encl_list_lock); +atomic_t sgx_va_pages_cnt = ATOMIC_INIT(0); + +struct sgx_epc_bank { + unsigned long pa; + unsigned long va; + unsigned long size; + void **pages; + atomic_t free_cnt; + struct rw_semaphore lock; +}; + +static struct sgx_epc_bank sgx_epc_banks[SGX_MAX_EPC_BANKS]; +static int sgx_nr_epc_banks; +static unsigned int sgx_nr_total_pages; +static atomic_t sgx_nr_free_pages = ATOMIC_INIT(0); +static struct task_struct *ksgxswapd_tsk; +static DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq); + +static int sgx_test_and_clear_young_cb(pte_t *ptep, +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 3, 0)) + pgtable_t token, +#endif + unsigned long addr, void *data) +{ + pte_t pte; + int ret; + + ret = pte_young(*ptep); + if (ret) { + pte = pte_mkold(*ptep); + set_pte_at((struct mm_struct *)data, addr, ptep, pte); + } + + return ret; +} + +/** + * sgx_test_and_clear_young() - Test and reset the accessed bit + * @page: enclave page to be tested for recent access + * + * Checks the Access (A) bit from the PTE corresponding to the + * enclave page and clears it. Returns 1 if the page has been + * recently accessed and 0 if not. + */ +int sgx_test_and_clear_young(struct sgx_encl_page *page) +{ + unsigned long addr = SGX_ENCL_PAGE_ADDR(page); + struct sgx_encl *encl = page->encl; + struct vm_area_struct *vma; + int ret; + + ret = sgx_encl_find(encl->mm, addr, &vma); + if (ret) + return 0; + + if (encl != vma->vm_private_data) + return 0; + + return apply_to_page_range(vma->vm_mm, addr, PAGE_SIZE, + sgx_test_and_clear_young_cb, vma->vm_mm); +} + +static struct sgx_encl *sgx_isolate_encl(void) +{ + struct sgx_encl *encl = NULL; + int i; + + mutex_lock(&sgx_encl_list_lock); + + if (list_empty(&sgx_encl_list)) { + mutex_unlock(&sgx_encl_list_lock); + return NULL; + } + + for (i = 0; i < SGX_NR_TO_SCAN; i++) { + encl = list_first_entry(&sgx_encl_list, struct sgx_encl, + encl_list); + + list_move_tail(&encl->encl_list, &sgx_encl_list); + + /* Select a victim with faulted pages and a valid refcount. */ + if (!list_empty(&encl->load_list) && + kref_get_unless_zero(&encl->refcount)) + break; + + encl = NULL; + } + + mutex_unlock(&sgx_encl_list_lock); + + return encl; +} + +static void sgx_isolate_pages(struct sgx_encl *encl, + struct sgx_encl_page **cluster) +{ + struct sgx_encl_page *entry; + int i; + + mutex_lock(&encl->lock); + + if (encl->flags & SGX_ENCL_DEAD) + goto out; + + for (i = 0; i < SGX_NR_TO_SCAN; i++) { + if (list_empty(&encl->load_list)) + break; + + entry = list_first_entry(&encl->load_list, struct sgx_encl_page, + list); + + if (!sgx_test_and_clear_young(entry) && + !(entry->desc & SGX_ENCL_PAGE_RESERVED)) { + entry->desc |= SGX_ENCL_PAGE_RESERVED; + list_del(&entry->list); + entry->desc &= ~SGX_ENCL_PAGE_LOADED; + *cluster++ = entry; + } else { + list_move_tail(&entry->list, &encl->load_list); + } + } +out: + *cluster = NULL; + mutex_unlock(&encl->lock); +} + +static int __sgx_ewb(struct sgx_encl *encl, + struct sgx_encl_page *encl_page, + struct sgx_va_page *va_page, + unsigned int va_offset) +{ + unsigned long pcmd_offset = SGX_ENCL_PAGE_PCMD_OFFSET(encl_page); + struct sgx_pageinfo pginfo; + struct page *backing; + struct page *pcmd; + void *epc; + void *va; + int ret; + + backing = sgx_get_backing(encl, encl_page, false); + if (IS_ERR(backing)) { + ret = PTR_ERR(backing); + sgx_warn(encl, "pinning the backing page for EWB failed with %d\n", + ret); + return ret; + } + + pcmd = sgx_get_backing(encl, encl_page, true); + if (IS_ERR(pcmd)) { + ret = PTR_ERR(pcmd); + sgx_warn(encl, "pinning the pcmd page for EWB failed with %d\n", + ret); + goto out; + } + + epc = sgx_get_page(encl_page->epc_page); + va = sgx_get_page(va_page->epc_page); + + pginfo.srcpge = (unsigned long)kmap_atomic(backing); + pginfo.pcmd = (unsigned long)kmap_atomic(pcmd) + pcmd_offset; + pginfo.linaddr = 0; + pginfo.secs = 0; + ret = __ewb(&pginfo, epc, (void *)((unsigned long)va + va_offset)); + kunmap_atomic((void *)(unsigned long)(pginfo.pcmd - pcmd_offset)); + kunmap_atomic((void *)(unsigned long)pginfo.srcpge); + + sgx_put_page(va); + sgx_put_page(epc); + sgx_put_backing(pcmd, true); + +out: + sgx_put_backing(backing, true); + return ret; +} + +static void sgx_eblock(struct sgx_encl *encl, struct sgx_encl_page **cluster) +{ + struct vm_area_struct *vma; + unsigned long addr; + void *ptr; + int ret; + + for ( ; *cluster; cluster++) { + addr = SGX_ENCL_PAGE_ADDR(*cluster); + + ret = sgx_encl_find(encl->mm, addr, &vma); + if (!ret && encl == vma->vm_private_data) + zap_vma_ptes(vma, addr, PAGE_SIZE); + + ptr = sgx_get_page((*cluster)->epc_page); + ret = __eblock(ptr); + sgx_put_page(ptr); + if (ret) { + sgx_crit(encl, "EBLOCK returned %d\n", ret); + sgx_invalidate(encl, true); + } + } +} + +static void sgx_etrack(struct sgx_encl *encl) +{ + void *ptr; + int ret; + + ptr = sgx_get_page(encl->secs.epc_page); + ret = __etrack(ptr); + sgx_put_page(ptr); + if (ret) { + sgx_crit(encl, "ETRACK returned %d\n", ret); + sgx_invalidate(encl, true); + } +} + +static void sgx_ewb(struct sgx_encl *encl, struct sgx_encl_page *entry) +{ + struct sgx_va_page *va_page; + unsigned int va_offset; + int ret; + + va_page = list_first_entry(&encl->va_pages, struct sgx_va_page, list); + va_offset = sgx_alloc_va_slot(va_page); + if (sgx_va_page_full(va_page)) + list_move_tail(&va_page->list, &encl->va_pages); + + ret = __sgx_ewb(encl, entry, va_page, va_offset); + if (ret == SGX_NOT_TRACKED) { + /* slow path, IPI needed */ + sgx_flush_cpus(encl); + ret = __sgx_ewb(encl, entry, va_page, va_offset); + } + + if (ret) { + sgx_invalidate(encl, true); + if (ret > 0) + sgx_err(encl, "EWB returned %d, enclave invalidated\n", + ret); + } + + sgx_free_page(entry->epc_page, encl); + entry->desc |= va_offset; + entry->va_page = va_page; + entry->desc &= ~SGX_ENCL_PAGE_RESERVED; +} + +static void sgx_write_pages(struct sgx_encl *encl, + struct sgx_encl_page **cluster) +{ + if (!*cluster) + return; + + mutex_lock(&encl->lock); + + sgx_eblock(encl, cluster); + sgx_etrack(encl); + + for ( ; *cluster; cluster++) { + sgx_ewb(encl, *cluster); + encl->secs_child_cnt--; + } + + if (!encl->secs_child_cnt && (encl->flags & SGX_ENCL_INITIALIZED)) { + sgx_ewb(encl, &encl->secs); + encl->flags |= SGX_ENCL_SECS_EVICTED; + } + + mutex_unlock(&encl->lock); +} + +static void sgx_swap_pages(void) +{ + struct sgx_encl *encl; + struct sgx_encl_page *cluster[SGX_NR_TO_SCAN + 1]; + + encl = sgx_isolate_encl(); + if (!encl) + return; + + down_read(&encl->mm->mmap_sem); + sgx_isolate_pages(encl, cluster); + sgx_write_pages(encl, cluster); + up_read(&encl->mm->mmap_sem); + + kref_put(&encl->refcount, sgx_encl_release); +} + +static int ksgxswapd(void *p) +{ + set_freezable(); + + while (!kthread_should_stop()) { + if (try_to_freeze()) + continue; + + wait_event_freezable(ksgxswapd_waitq, kthread_should_stop() || + atomic_read(&sgx_nr_free_pages) < + SGX_NR_HIGH_PAGES); + + if (atomic_read(&sgx_nr_free_pages) < SGX_NR_HIGH_PAGES) + sgx_swap_pages(); + } + + pr_info("%s: done\n", __func__); + return 0; +} + +static int sgx_init_epc_bank(unsigned long addr, unsigned long size, + unsigned long index, struct sgx_epc_bank *bank) +{ + unsigned long nr_pages = size >> PAGE_SHIFT; + unsigned long i; + void *va; + + if (IS_ENABLED(CONFIG_X86_64)) { + va = ioremap_cache(addr, size); + if (!va) + return -ENOMEM; + } + + bank->pages = kzalloc(nr_pages * sizeof(void *), GFP_KERNEL); + if (!bank->pages) { + if (IS_ENABLED(CONFIG_X86_64)) + iounmap(va); + + return -ENOMEM; + } + + for (i = 0; i < nr_pages; i++) + bank->pages[i] = (void *)((addr + (i << PAGE_SHIFT)) | index); + + bank->pa = addr; + bank->size = size; + + if (IS_ENABLED(CONFIG_X86_64)) + bank->va = (unsigned long)va; + + atomic_set(&bank->free_cnt, nr_pages); + + init_rwsem(&bank->lock); + + sgx_nr_total_pages += nr_pages; + atomic_add(nr_pages, &sgx_nr_free_pages); + return 0; +} + +int sgx_page_cache_init(struct device *parent) +{ + struct task_struct *tsk; + unsigned long size; + unsigned int eax = 0; + unsigned int ebx = 0; + unsigned int ecx = 0; + unsigned int edx = 0; + unsigned long pa; + int i; + int ret; + + for (i = 0; i < SGX_MAX_EPC_BANKS; i++) { + cpuid_count(SGX_CPUID, i + SGX_CPUID_EPC_BANKS, &eax, &ebx, + &ecx, &edx); + if (!(eax & 0xf)) + break; + + pa = ((u64)(ebx & 0xfffff) << 32) + (u64)(eax & 0xfffff000); + size = ((u64)(edx & 0xfffff) << 32) + (u64)(ecx & 0xfffff000); + + dev_info(parent, "EPC bank 0x%lx-0x%lx\n", pa, pa + size); + + ret = sgx_init_epc_bank(pa, size, i, &sgx_epc_banks[i]); + if (ret) + return ret; + + sgx_nr_epc_banks++; + } + + tsk = kthread_run(ksgxswapd, NULL, "ksgxswapd"); + if (IS_ERR(tsk)) { + sgx_page_cache_teardown(); + return PTR_ERR(tsk); + } + + ksgxswapd_tsk = tsk; + return 0; +} + +void sgx_page_cache_teardown(void) +{ + struct sgx_epc_bank *bank; + int i; + + if (ksgxswapd_tsk) { + kthread_stop(ksgxswapd_tsk); + ksgxswapd_tsk = NULL; + } + + for (i = 0; i < sgx_nr_epc_banks; i++) { + bank = &sgx_epc_banks[i]; + + if (IS_ENABLED(CONFIG_X86_64)) + iounmap((void *)bank->va); + + kfree(bank->pages); + } +} + +static void *sgx_try_alloc_page(void) +{ + struct sgx_epc_bank *bank; + void *page = NULL; + int i; + + for (i = 0; i < sgx_nr_epc_banks; i++) { + bank = &sgx_epc_banks[i]; + + down_write(&bank->lock); + + if (atomic_read(&bank->free_cnt)) + page = bank->pages[atomic_dec_return(&bank->free_cnt)]; + + up_write(&bank->lock); + + if (page) + break; + } + + if (page) + atomic_dec(&sgx_nr_free_pages); + + return page; +} + +/** + * sgx_alloc_page - allocate an EPC page + * @flags: allocation flags + * + * Try to grab a page from the free EPC page list. If there is a free page + * available, it is returned to the caller. If called with SGX_ALLOC_ATOMIC, + * the function will return immediately if the list is empty. Otherwise, it + * will swap pages up until there is a free page available. Before returning + * the low watermark is checked and ksgxswapd is waken up if we are below it. + * + * Return: an EPC page or a system error code + */ +void *sgx_alloc_page(unsigned int flags) +{ + void *entry; + + for ( ; ; ) { + entry = sgx_try_alloc_page(); + if (entry) + break; + + /* We need at minimum two pages for the #PF handler. */ + if (atomic_read(&sgx_va_pages_cnt) > (sgx_nr_total_pages - 2)) + return ERR_PTR(-ENOMEM); + + if (flags & SGX_ALLOC_ATOMIC) { + entry = ERR_PTR(-EBUSY); + break; + } + + if (signal_pending(current)) { + entry = ERR_PTR(-ERESTARTSYS); + break; + } + + sgx_swap_pages(); + schedule(); + } + + if (atomic_read(&sgx_nr_free_pages) < SGX_NR_LOW_PAGES) + wake_up(&ksgxswapd_waitq); + + return entry; +} + +/** + * sgx_free_page - free an EPC page + * + * EREMOVE an EPC page and insert it back to the list of free pages. + * If EREMOVE fails, the error is printed out loud as a critical error. + * It is an indicator of a driver bug if that would happen. + * + * @page: any EPC page + * @encl: enclave that owns the given EPC page + */ +void sgx_free_page(void *page, struct sgx_encl *encl) +{ + struct sgx_epc_bank *bank = SGX_EPC_BANK(page); + void *va; + int ret; + + va = sgx_get_page(page); + ret = __eremove(va); + sgx_put_page(va); + + if (ret) + sgx_crit(encl, "EREMOVE returned %d\n", ret); + + down_read(&bank->lock); + bank->pages[atomic_inc_return(&bank->free_cnt) - 1] = page; + up_read(&bank->lock); + + atomic_inc(&sgx_nr_free_pages); +} + +void *sgx_get_page(void *page) +{ + struct sgx_epc_bank *bank = SGX_EPC_BANK(page); + + if (IS_ENABLED(CONFIG_X86_64)) + return (void *)(bank->va + SGX_EPC_ADDR(page) - bank->pa); + + return kmap_atomic_pfn(SGX_EPC_PFN(page)); +} + +void sgx_put_page(void *ptr) +{ + if (IS_ENABLED(CONFIG_X86_64)) + return; + + kunmap_atomic(ptr); +} diff --git a/ubuntu/sgx/sgx_util.c b/ubuntu/sgx/sgx_util.c new file mode 100644 index 000000000000..4ff8b13ebbf1 --- /dev/null +++ b/ubuntu/sgx/sgx_util.c @@ -0,0 +1,382 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include "sgx.h" + +struct page *sgx_get_backing(struct sgx_encl *encl, + struct sgx_encl_page *entry, + bool pcmd) +{ + struct address_space *mapping; + struct inode *inode; + gfp_t gfpmask; + pgoff_t index; + + if (pcmd) + inode = encl->pcmd->f_path.dentry->d_inode; + else + inode = encl->backing->f_path.dentry->d_inode; + + mapping = inode->i_mapping; + gfpmask = mapping_gfp_mask(mapping); + + if (pcmd) + index = PFN_DOWN(entry->desc - encl->base) >> 5; + else + index = PFN_DOWN(entry->desc - encl->base); + + return shmem_read_mapping_page_gfp(mapping, index, gfpmask); +} + +void sgx_put_backing(struct page *backing_page, bool write) +{ + if (write) + set_page_dirty(backing_page); + + put_page(backing_page); +} + +void sgx_zap_tcs_ptes(struct sgx_encl *encl, struct vm_area_struct *vma) +{ + struct sgx_encl_page *entry; + unsigned long addr; + + list_for_each_entry(entry, &encl->load_list, list) { + addr = SGX_ENCL_PAGE_ADDR(entry); + if ((entry->desc & SGX_ENCL_PAGE_TCS) && + addr >= vma->vm_start && addr < vma->vm_end) + zap_vma_ptes(vma, addr, PAGE_SIZE); + } +} + +void sgx_invalidate(struct sgx_encl *encl, bool flush_cpus) +{ + struct vm_area_struct *vma; + unsigned long addr; + int ret; + + for (addr = encl->base; addr < (encl->base + encl->size); + addr = vma->vm_end) { + ret = sgx_encl_find(encl->mm, addr, &vma); + if (!ret && encl == vma->vm_private_data) + sgx_zap_tcs_ptes(encl, vma); + else + break; + } + + encl->flags |= SGX_ENCL_DEAD; + + if (flush_cpus) + sgx_flush_cpus(encl); +} + +static void sgx_ipi_cb(void *info) +{ +} + +void sgx_flush_cpus(struct sgx_encl *encl) +{ + on_each_cpu_mask(mm_cpumask(encl->mm), sgx_ipi_cb, NULL, 1); +} + +static int sgx_eldu(struct sgx_encl *encl, + struct sgx_encl_page *encl_page, + void *epc_page, + bool is_secs) +{ + struct sgx_pageinfo pginfo; + unsigned long pcmd_offset; + unsigned long va_offset; + void *secs_ptr = NULL; + struct page *backing; + struct page *pcmd; + void *epc_ptr; + void *va_ptr; + int ret; + + pcmd_offset = SGX_ENCL_PAGE_PCMD_OFFSET(encl_page); + va_offset = SGX_ENCL_PAGE_VA_OFFSET(encl_page); + + backing = sgx_get_backing(encl, encl_page, false); + if (IS_ERR(backing)) { + ret = PTR_ERR(backing); + sgx_warn(encl, "pinning the backing page for ELDU failed with %d\n", + ret); + return ret; + } + + pcmd = sgx_get_backing(encl, encl_page, true); + if (IS_ERR(pcmd)) { + ret = PTR_ERR(pcmd); + sgx_warn(encl, "pinning the pcmd page for EWB failed with %d\n", + ret); + goto out; + } + + if (!is_secs) + secs_ptr = sgx_get_page(encl->secs.epc_page); + + epc_ptr = sgx_get_page(epc_page); + va_ptr = sgx_get_page(encl_page->va_page->epc_page); + pginfo.srcpge = (unsigned long)kmap_atomic(backing); + pginfo.pcmd = (unsigned long)kmap_atomic(pcmd) + pcmd_offset; + pginfo.linaddr = is_secs ? 0 : SGX_ENCL_PAGE_ADDR(encl_page); + pginfo.secs = (unsigned long)secs_ptr; + + ret = __eldu((unsigned long)&pginfo, (unsigned long)epc_ptr, + (unsigned long)va_ptr + va_offset); + if (ret) { + sgx_err(encl, "ELDU returned %d\n", ret); + ret = -EFAULT; + } + + kunmap_atomic((void *)(unsigned long)(pginfo.pcmd - pcmd_offset)); + kunmap_atomic((void *)(unsigned long)pginfo.srcpge); + sgx_put_page(va_ptr); + sgx_put_page(epc_ptr); + + if (!is_secs) + sgx_put_page(secs_ptr); + + sgx_put_backing(pcmd, false); + +out: + sgx_put_backing(backing, false); + + if (!ret) { + sgx_free_va_slot(encl_page->va_page, va_offset); + list_move(&encl_page->va_page->list, &encl->va_pages); + encl_page->desc &= ~SGX_VA_OFFSET_MASK; + } + + return ret; +} + +static struct sgx_encl_page *sgx_do_fault(struct vm_area_struct *vma, + unsigned long addr, + unsigned int flags) +{ + bool reserve = (flags & SGX_FAULT_RESERVE) != 0; + struct sgx_encl *encl = vma->vm_private_data; + struct sgx_encl_page *entry; + void *secs_epc_page = NULL; + void *epc_page = NULL; + int rc = 0; + + /* If process was forked, VMA is still there but vm_private_data is set + * to NULL. + */ + if (!encl) + return ERR_PTR(-EFAULT); + + mutex_lock(&encl->lock); + + entry = radix_tree_lookup(&encl->page_tree, addr >> PAGE_SHIFT); + if (!entry) { + rc = -EFAULT; + goto out; + } + + if (encl->flags & SGX_ENCL_DEAD) { + rc = -EFAULT; + goto out; + } + + if (!(encl->flags & SGX_ENCL_INITIALIZED)) { + sgx_dbg(encl, "cannot fault, unitialized\n"); + rc = -EFAULT; + goto out; + } + + if (reserve && (entry->desc & SGX_ENCL_PAGE_RESERVED)) { + sgx_dbg(encl, "cannot fault, 0x%p is reserved\n", + (void *)SGX_ENCL_PAGE_ADDR(entry)); + rc = -EBUSY; + goto out; + } + + /* Legal race condition, page is already faulted. */ + if (entry->desc & SGX_ENCL_PAGE_LOADED) { + if (reserve) + entry->desc |= SGX_ENCL_PAGE_RESERVED; + goto out; + } + + epc_page = sgx_alloc_page(SGX_ALLOC_ATOMIC); + if (IS_ERR(epc_page)) { + rc = PTR_ERR(epc_page); + epc_page = NULL; + goto out; + } + + /* If SECS is evicted then reload it first */ + if (encl->flags & SGX_ENCL_SECS_EVICTED) { + secs_epc_page = sgx_alloc_page(SGX_ALLOC_ATOMIC); + if (IS_ERR(secs_epc_page)) { + rc = PTR_ERR(secs_epc_page); + secs_epc_page = NULL; + goto out; + } + + rc = sgx_eldu(encl, &encl->secs, secs_epc_page, true); + if (rc) + goto out; + + encl->secs.epc_page = secs_epc_page; + encl->flags &= ~SGX_ENCL_SECS_EVICTED; + + /* Do not free */ + secs_epc_page = NULL; + } + + rc = sgx_eldu(encl, entry, epc_page, false /* is_secs */); + if (rc) + goto out; + + /* Track the EPC page even if vm_insert_pfn fails; we need to ensure + * the EPC page is properly freed and we can't do EREMOVE right away + * because EREMOVE may fail due to an active cpu in the enclave. We + * can't call vm_insert_pfn before sgx_eldu because SKL signals #GP + * instead of #PF if the EPC page is invalid. + */ + encl->secs_child_cnt++; + + entry->epc_page = epc_page; + entry->desc |= SGX_ENCL_PAGE_LOADED; + + if (reserve) + entry->desc |= SGX_ENCL_PAGE_RESERVED; + + /* Do not free */ + epc_page = NULL; + list_add_tail(&entry->list, &encl->load_list); + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)) + rc = vmf_insert_pfn(vma, addr, SGX_EPC_PFN(entry->epc_page)); + if (rc != VM_FAULT_NOPAGE) { +#else + rc = vm_insert_pfn(vma, addr, SGX_EPC_PFN(entry->epc_page)); + if (rc) { +#endif + /* Kill the enclave if vm_insert_pfn fails; failure only occurs + * if there is a driver bug or an unrecoverable issue, e.g. OOM. + */ + sgx_crit(encl, "vm_insert_pfn returned %d\n", rc); + sgx_invalidate(encl, true); + goto out; + } + rc = 0; + + sgx_test_and_clear_young(entry); +out: + mutex_unlock(&encl->lock); + if (epc_page) + sgx_free_page(epc_page, encl); + if (secs_epc_page) + sgx_free_page(secs_epc_page, encl); + return rc ? ERR_PTR(rc) : entry; +} + +struct sgx_encl_page *sgx_fault_page(struct vm_area_struct *vma, + unsigned long addr, + unsigned int flags) +{ + struct sgx_encl_page *entry; + + do { + entry = sgx_do_fault(vma, addr, flags); + if (!(flags & SGX_FAULT_RESERVE)) + break; + } while (PTR_ERR(entry) == -EBUSY); + + return entry; +} + +int sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus, void *hash) +{ + SHASH_DESC_ON_STACK(shash, tfm); + + shash->tfm = tfm; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)) + shash->tfm->base.crt_flags = CRYPTO_TFM_REQ_MAY_SLEEP; +#else + shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP; +#endif + + return crypto_shash_digest(shash, modulus, SGX_MODULUS_SIZE, hash); +} + +int sgx_get_key_hash_simple(const void *modulus, void *hash) +{ + struct crypto_shash *tfm; + int ret; + + tfm = crypto_alloc_shash("sha256", 0, CRYPTO_ALG_ASYNC); + if (IS_ERR(tfm)) + return PTR_ERR(tfm); + + ret = sgx_get_key_hash(tfm, modulus, hash); + + crypto_free_shash(tfm); + return ret; +} diff --git a/ubuntu/sgx/sgx_version.h b/ubuntu/sgx/sgx_version.h new file mode 100644 index 000000000000..eb550a6e6627 --- /dev/null +++ b/ubuntu/sgx/sgx_version.h @@ -0,0 +1,60 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// + +#ifndef _SGX_VERSION_H +#define _SGX_VERSION_H + +#define DRV_DESCRIPTION "Intel SGX Driver" +#define DRV_VERSION "1.20" + +#endif /* _SGX_VERSION_H */ diff --git a/ubuntu/sgx/sgx_vma.c b/ubuntu/sgx/sgx_vma.c new file mode 100644 index 000000000000..cff5082e0add --- /dev/null +++ b/ubuntu/sgx/sgx_vma.c @@ -0,0 +1,242 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// +// Jarkko Sakkinen +// Suresh Siddha +// Serge Ayoun +// Shay Katz-zamir +// Sean Christopherson + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sgx.h" + +#ifndef ALIGN_DOWN +#define ALIGN_DOWN(x, align_to) ((x) & ~((align_to)-1)) +#endif + +static void sgx_vma_open(struct vm_area_struct *vma) +{ + struct sgx_encl *encl = vma->vm_private_data; + + if (!encl) + return; + + /* kref cannot underflow because ECREATE ioctl checks that there is only + * one single VMA for the enclave before proceeding. + */ + kref_get(&encl->refcount); +} + +static void sgx_vma_close(struct vm_area_struct *vma) +{ + struct sgx_encl *encl = vma->vm_private_data; + + if (!encl) + return; + + mutex_lock(&encl->lock); + zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); + encl->flags |= SGX_ENCL_DEAD; + mutex_unlock(&encl->lock); + kref_put(&encl->refcount, sgx_encl_release); +} + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)) +static unsigned int sgx_vma_fault(struct vm_fault *vmf) +#else +static int sgx_vma_fault(struct vm_fault *vmf) +#endif +{ + struct vm_area_struct *vma = vmf->vma; + unsigned long addr = (unsigned long)vmf->address; + struct sgx_encl_page *entry; + + entry = sgx_fault_page(vma, addr, 0); + + if (!IS_ERR(entry) || PTR_ERR(entry) == -EBUSY) + return VM_FAULT_NOPAGE; + else + return VM_FAULT_SIGBUS; +} + +static int sgx_edbgrd(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset; + void *ptr; + int ret; + + offset = addr & ~PAGE_MASK; + + if ((page->desc & SGX_ENCL_PAGE_TCS) && + (offset + sizeof(unsigned long)) > + offsetof(struct sgx_tcs, reserved)) + return -ECANCELED; + + ptr = sgx_get_page(page->epc_page); + ret = __edbgrd((unsigned long)ptr + offset, data); + sgx_put_page(ptr); + if (ret) { + sgx_dbg(encl, "EDBGRD returned %d\n", ret); + return -EFAULT; + } + + return 0; +} + +static int sgx_edbgwr(struct sgx_encl *encl, struct sgx_encl_page *page, + unsigned long addr, void *data) +{ + unsigned long offset; + void *ptr; + 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; + + ptr = sgx_get_page(page->epc_page); + ret = __edbgwr((unsigned long)ptr + offset, data); + sgx_put_page(ptr); + if (ret) { + sgx_dbg(encl, "EDBGWR returned %d\n", ret); + return -EFAULT; + } + + 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) { + if (!entry || !((addr + i) & (PAGE_SIZE - 1))) { + if (entry) + entry->desc &= ~SGX_ENCL_PAGE_RESERVED; + + entry = sgx_fault_page(vma, (addr + i) & PAGE_MASK, + SGX_FAULT_RESERVE); + if (IS_ERR(entry)) { + ret = PTR_ERR(entry); + entry = NULL; + break; + } + } + + /* Locking is not needed because only immutable fields of the + * page are accessed and page itself is reserved so that it + * cannot be swapped out in the middle. + */ + + 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) + break; + + if (write) { + memcpy(data + offset, buf + i, cnt); + ret = sgx_edbgwr(encl, entry, align, data); + if (ret) + break; + } + else + memcpy(buf + i,data + offset, cnt); + } + + if (entry) + entry->desc &= ~SGX_ENCL_PAGE_RESERVED; + + return (ret < 0 && ret != -ECANCELED) ? 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, +}; diff --git a/ubuntu/sgx/sgx_wl.h b/ubuntu/sgx/sgx_wl.h new file mode 100644 index 000000000000..5217cb9d6154 --- /dev/null +++ b/ubuntu/sgx/sgx_wl.h @@ -0,0 +1,82 @@ +// This file is provided under a dual BSD/GPLv2 license. When using or +// redistributing this file, you may do so under either license. +// +// GPL LICENSE SUMMARY +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of version 2 of the GNU General Public License as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// Contact Information: +// Jarkko Sakkinen +// Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +// +// BSD LICENSE +// +// Copyright(c) 2016-2018 Intel Corporation. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Intel Corporation nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Authors: +// + +#ifndef _WHITELIST_H_ +#define _WHITELIST_H_ + + +uint8_t G_SERVICE_ENCLAVE_MRSIGNER[][32] = +{ + {//MR_SIGNER of PvE provided + 0XEC, 0X15, 0XB1, 0X07, 0X87, 0XD2, 0XF8, 0X46, + 0X67, 0XCE, 0XB0, 0XB5, 0X98, 0XFF, 0XC4, 0X4A, + 0X1F, 0X1C, 0XB8, 0X0F, 0X67, 0X0A, 0XAE, 0X5D, + 0XF9, 0XE8, 0XFA, 0X9F, 0X63, 0X76, 0XE1, 0XF8 + }, + {//MR_SIGNER of PCE provided + 0xC5, 0x4A, 0x62, 0xF2, 0xBE, 0x9E, 0xF7, 0x6E, + 0xFB, 0x1F, 0x39, 0x30, 0xAD, 0x81, 0xEA, 0x7F, + 0x60, 0xDE, 0xFC, 0x1F, 0x5F, 0x25, 0xE0, 0x9B, + 0x7C, 0x06, 0x7A, 0x81, 0x5A, 0xE0, 0xC6, 0xCB + }, + {//MR_SIGNER of ECDSA/QE + 0X8C, 0X4F, 0X57, 0X75, 0XD7, 0X96, 0X50, 0X3E, + 0X96, 0X13, 0X7F, 0X77, 0XC6, 0X8A, 0X82, 0X9A, + 0X00, 0X56, 0XAC, 0X8D, 0XED, 0X70, 0X14, 0X0B, + 0X08, 0X1B, 0X09, 0X44, 0X90, 0XC5, 0X7B, 0XFF + } +}; + + +#endif //_WHITELIST_H_ +