mirror of
https://github.com/clearlinux/graphene.git
synced 2026-09-03 20:31:40 +00:00
This is the next part of the great loader rework, with a lot of breaking changes: - Complete removal of the "trusted children" thing - now children processes can be spawned arbitrarily and from arbitrary mountpoint types, without any additional configuration needed. - There's a new, required option in the manifest: `libos.entrypoint` - it specifies the URI to the entry binary in the first process. There's no need anymore to name the manifest and the first binary identically. - On SGX, the main binary is not measured in MRENCLAVE anymore - only PAL, LibOS and the manifest are measured. This is enough to bind MRENCLAVE to a specific entrypoint user executable if wanted - it just has to be mounted as a trusted file. - All Graphene SGX enclaves have now exactly the same MRENCLAVE. This is a hash of a "Graphene stub", which can "fork" into one of two states in runtime: initial process or child. The initial process creates a new "Graphene namespace" with a clean state, it can also be attested remotely (contrary to child processes). The initial process can spawn children processes by spawning a Graphene stub and directing it to start in the child mode. It then attests it locally, and if successful, establishes an encrypted pipe, "connects" to its own namespace and treats as trusted (including sending protected files key). - Now, there's only one, central manifest describing the initial state of a Graphene instance which can be spawned from it (previously, each process required a separate manifest which could have different configuration - which wasn't actually supported and didn't make sense design-wise). One downside of central manifests is that all processes require the same enclave configuration (e.g. size), but that was already the case so far because of broken checkpointing code. Also, this is only a temporary problem, which will cease to exist after the introduction of EDMM. - `sgx.static_address` was renamed to `sgx.nonpie_binary` and now has to be inserted manually by users (`sgx_sign` tools doesn't know about the binaries run inside, which can be even provided or generated in runtime by the user's workload). - Caveat: the memory gap for non-PIE executables was removed because it requires adding a new option to the manifest to be cleanly implemented. This is left for some future loader rework PR.
139 lines
4.1 KiB
Python
139 lines
4.1 KiB
Python
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
# Copyright (c) 2020 Intel Corporation
|
|
# Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
#
|
|
|
|
import contextlib
|
|
import os
|
|
import pathlib
|
|
import shlex
|
|
import string
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
class MesonTemplate(string.Template):
|
|
pattern = '''
|
|
@(?:
|
|
(?P<escaped>@) |
|
|
(?P<named>[A-Za-z0-9_]+)@ |
|
|
(?P<braced>[A-Za-z0-9_]+)@ |
|
|
(?P<invalid>)
|
|
)
|
|
'''
|
|
|
|
def which(command):
|
|
return subprocess.run('command -v {}'.format(shlex.quote(command)),
|
|
shell=True, check=True, stdout=subprocess.PIPE).stdout.strip().decode()
|
|
|
|
class Exec:
|
|
def __init__(self, executable, manifest_template, **kwds):
|
|
self.executable = pathlib.Path(executable)
|
|
self.manifest_template = manifest_template
|
|
self.template_vars = kwds
|
|
|
|
@property
|
|
def graphene_path(self):
|
|
return pathlib.Path(os.environ['ASV_BUILD_DIR'])
|
|
|
|
@property
|
|
def conf_path(self):
|
|
return pathlib.Path(os.environ['ASV_CONF_DIR']) / 'benchmarks'
|
|
|
|
@property
|
|
def benchmarks_path(self):
|
|
return self.graphene_path / 'tests/benchmarks'
|
|
|
|
@property
|
|
def manifest_path(self):
|
|
return (self.benchmarks_path / self.executable.name).with_suffix('.manifest')
|
|
|
|
@property
|
|
def manifest_sgx_path(self):
|
|
return self.manifest_path.with_suffix('.manifest.sgx')
|
|
|
|
@property
|
|
def executable_path(self):
|
|
return self.benchmarks_path / self.executable.name
|
|
|
|
|
|
def setup(self, *_args):
|
|
self.benchmarks_path.mkdir(parents=True, exist_ok=True)
|
|
try:
|
|
self.executable_path.unlink()
|
|
except FileNotFoundError:
|
|
pass
|
|
self.executable_path.symlink_to(self.conf_path / self.executable)
|
|
|
|
with open(self.conf_path / self.manifest_template) as file:
|
|
template = MesonTemplate(file.read())
|
|
|
|
with open(self.manifest_path, 'w') as file:
|
|
file.write(template.substitute(
|
|
GRAPHENEDIR=os.environ['ASV_BUILD_DIR'],
|
|
ARCH_LIBDIR='/lib/x86_64-linux-gnu',
|
|
**self.template_vars))
|
|
|
|
signer_path = self.graphene_path / 'Pal/src/host/Linux-SGX/signer'
|
|
|
|
subprocess.run([os.fspath(signer_path / 'pal-sgx-sign'),
|
|
'--manifest', os.fspath(self.manifest_path),
|
|
'--output', os.fspath(self.manifest_sgx_path),
|
|
'--key', os.fspath(signer_path / 'enclave-key.pem'),
|
|
'--libpal', os.fspath(self.graphene_path / 'Runtime/libpal-Linux-SGX.so'),
|
|
], check=True, stdout=subprocess.PIPE)
|
|
|
|
subprocess.run([os.fspath(signer_path / 'pal-sgx-get-token'),
|
|
'--sig', os.fspath(self.manifest_path.with_suffix('.sig')),
|
|
'--output', os.fspath(self.manifest_path.with_suffix('.token')),
|
|
], check=True, stdout=subprocess.PIPE)
|
|
|
|
def add_setup(self, func):
|
|
func.setup = self.setup
|
|
return func
|
|
|
|
@staticmethod
|
|
def _set_sgx(sgx):
|
|
if sgx:
|
|
os.environ['SGX'] = '1'
|
|
else:
|
|
os.environ.pop('SGX', None)
|
|
|
|
@property
|
|
def pal_loader(self):
|
|
return os.fspath(self.graphene_path / 'Runtime/pal_loader')
|
|
|
|
|
|
def run_in_graphene(self, *args, sgx=True):
|
|
self._set_sgx(sgx)
|
|
return subprocess.run([self.pal_loader, os.fspath(self.manifest_sgx_path), *args],
|
|
check=True, cwd=self.benchmarks_path)
|
|
|
|
@contextlib.contextmanager
|
|
def graphene_server(self, *args, sgx=True, sleep=30):
|
|
self._set_sgx(sgx)
|
|
|
|
try:
|
|
server = subprocess.Popen(
|
|
[self.pal_loader, os.fspath(self.manifest_sgx_path), *args],
|
|
cwd=self.benchmarks_path)
|
|
if sleep > 0:
|
|
time.sleep(sleep) # so the server starts
|
|
yield
|
|
|
|
finally:
|
|
server.kill()
|
|
server.wait()
|
|
|
|
@contextlib.contextmanager
|
|
def native_server(self, *args, sleep=5):
|
|
try:
|
|
server = subprocess.Popen([os.fspath(self.executable), *args])
|
|
if sleep > 0:
|
|
time.sleep(sleep) # so the server starts
|
|
yield
|
|
|
|
finally:
|
|
server.kill()
|
|
server.wait()
|