Skip to content

Commit

Permalink
Fix broken sigining of EXT2 rootfs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikbras committed Feb 9, 2023
1 parent b4014d1 commit 4f81bb6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
56 changes: 56 additions & 0 deletions tests/sign/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
TOP=$(abspath ../..)
include $(TOP)/defs.mak

APPDIR = appdir
CFLAGS = -fPIC
LDFLAGS = -Wl,-rpath=$(MUSL_LIB)

all:
$(MAKE) myst
$(MAKE) rootfs

rootfs: hello.c
mkdir -p $(APPDIR)/bin
$(CC) $(CFLAGS) -o $(APPDIR)/bin/hello hello.c $(LDFLAGS)
$(MYST) mkext2 $(APPDIR) rootfs

OPTS =

ifdef STRACE
OPTS += --strace
endif

ifdef PERF
OPTS += --perf
endif

OPTS += --thread-stack-size=1048576

tests: all
$(RUNTEST) $(MYST_EXEC) rootfs /bin/hello $(OPTS)
$(MAKE) sign
$(MAKE) verify
$(MAKE) fail
@ echo "=== passed all tests"

sign:
$(MYST) fssig --roothash rootfs > roothash
rm -rf hello.signed
$(MYST) sign-sgx rootfs private.pem config.json --roothash=roothash

verify:
( cd hello.signed; ./bin/myst exec-sgx rootfs /bin/hello $(OPTS) )

myst:
$(MAKE) -C $(TOP)/tools/myst

clean:
rm -rf $(APPDIR) rootfs export ramfs hello.signed roothash

##
## Negative test to verify that hacking rootfs fails loading.
##
fail:
$(MAKE) sign
$(MYST) mkext2 --force $(APPDIR) hello.signed/rootfs
$(MAKE) verify 2> /dev/null; test $$? -eq 2
5 changes: 5 additions & 0 deletions tests/sign/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sign
====

This test verifies that a simple Mystikos application can be signed and
executed.
13 changes: 13 additions & 0 deletions tests/sign/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.1",
"Debug": 0,
"ProductID": 1,
"SecurityVersion": 1,
"MemorySize": "40m",
"ThreadStackSize": "16m",
"ApplicationPath": "/bin/hello",
"ApplicationParameters": [],
"HostApplicationParameters": false,
"EnvironmentVariables": [],
"HostEnvironmentVariables": []
}
17 changes: 17 additions & 0 deletions tests/sign/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, const char* argv[])
{
printf("Hello!\n");
printf("=== passed test (%s)\n", argv[0]);

return 0;
}
23 changes: 22 additions & 1 deletion tools/myst/host/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <myst/file.h>
#include <myst/strings.h>
#include <myst/types.h>
#include <myst/cpio.h>
#include <openenclave/host.h>
#include "../config.h"
#include "myst_u.h"
Expand Down Expand Up @@ -293,6 +294,7 @@ int _sign(int argc, const char* argv[])
const char* signing_engine_name = NULL;
const char* signing_engine_path = NULL;
myst_buf_t roothash_buf = MYST_BUF_INITIALIZER;
char rootfs_path[] = "/tmp/mystXXXXXX";

// We are in the right operation, right?
assert(
Expand Down Expand Up @@ -341,6 +343,7 @@ int _sign(int argc, const char* argv[])

const char* program_file = get_program_file();
const char* rootfs_file = argv[2];
const char* rootfs = argv[2];
const char* pem_file = argv[3];
const char* config_file = argv[4];
const char* target = NULL; // Extracted from config file
Expand Down Expand Up @@ -440,10 +443,28 @@ int _sign(int argc, const char* argv[])
assert(myst_validate_file_path(program_file));
assert(myst_validate_file_path(temp_oeconfig_file));

/* if not a CPIO archive, create a zero-filled file with one page */
if (myst_cpio_test(rootfs) == -ENOTSUP)
{
int fd;
uint8_t page[PAGE_SIZE];

if ((fd = mkstemp(rootfs_path)) < 0)
_err("failed to create temporary file");

memset(page, 0, sizeof(page));

if (write(fd, page, sizeof(page)) != sizeof(page))
_err("failed to create file");

close(fd);
rootfs = rootfs_path;
}

// Setup all the regions
if ((details = create_region_details_from_files(
target,
rootfs_file,
rootfs,
pubkeys_opt,
roothashes_opt,
config_file,
Expand Down

0 comments on commit 4f81bb6

Please sign in to comment.