Skip to content

Commit d1b7f13

Browse files
pqcfoxalevy
authored andcommitted
tutorials: root_of_trust: add 'suspicious' SRAM dump demo application
1 parent 95f5fe8 commit d1b7f13

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../../
5+
6+
PACKAGE_NAME = org.tockos.tutorials.attestation.suspicious
7+
8+
# Which files to compile.
9+
C_SRCS := $(wildcard *.c)
10+
11+
# Include userland master makefile. Contains rules and flags for actually
12+
# building the application.
13+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Hardware Root of Trust (HWRoT) "Suspicious" Userspace Attack Application
2+
------------------------------------------------------------------------
3+
4+
This application implements a basic SRAM dump attack on the encryption servicde
5+
application in the Hardware Root of Trust demo.
6+
7+
This is part of a tutorial which improves the encryption application in multiple
8+
steps and mounts multiple attacks on it. A writeup for the tutorial is available
9+
at https://book.tockos.org/.
10+
11+
Specific abilities of this version (assuming `#define` constants set correctly):
12+
13+
* Allows dumping application's own SRAM
14+
* Allows attempting dumping of the encryption service application's SRAM
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "libtock/tock.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#include <libtock/kernel/ipc.h>
6+
7+
#define ENCRYPTION_SRAM_START 0x2000A000
8+
#define ENCRYPTION_SRAM_END 0x2000BFFF
9+
#define SELF_SRAM_START 0x2000C000
10+
#define SELF_SRAM_END 0x2000CFFF
11+
12+
#define LOG_WIDTH 32
13+
14+
bool started = false;
15+
bool log_done = false;
16+
size_t screen_service = -1;
17+
char log_buf[LOG_WIDTH] __attribute__((aligned(LOG_WIDTH)));
18+
19+
const char SCREEN_SERVICE_NAME[] = "org.tockos.tutorials.attestation.screen";
20+
21+
static void ipc_callback(__attribute__ ((unused)) int pid,
22+
__attribute__ ((unused)) int len,
23+
__attribute__ ((unused)) int arg2,
24+
__attribute__ ((unused)) void* ud) {
25+
started = true;
26+
}
27+
28+
static void log_done_callback(__attribute__ ((unused)) int pid,
29+
__attribute__ ((unused)) int len,
30+
__attribute__ ((unused)) int arg2,
31+
__attribute__ ((unused)) void* ud) {
32+
log_done = true;
33+
}
34+
35+
static void wait_for_start(void) {
36+
// Register an IPC callback and wait for it to be called by the
37+
// screen app based on the user's app selection.
38+
ipc_register_service_callback("org.tockos.tutorials.attestation.suspicious", ipc_callback,
39+
NULL);
40+
yield_for(&started);
41+
}
42+
43+
static int setup_logging() {
44+
returncode_t ret;
45+
46+
// Find the PID of the screen logging service
47+
ret = ipc_discover(SCREEN_SERVICE_NAME, &screen_service);
48+
if (ret != RETURNCODE_SUCCESS) {
49+
printf("Screen logging service not found.\n");
50+
return ret;
51+
}
52+
53+
// Set up a callback and share so we can supply log messages
54+
// and know when they've been completely logged.
55+
ipc_register_client_callback(screen_service, log_done_callback, NULL);
56+
ipc_share(screen_service, log_buf, LOG_WIDTH);
57+
58+
return 0;
59+
}
60+
61+
static int log_to_screen(const char* message) {
62+
returncode_t ret;
63+
64+
// Copy up to the log buffer's size of the message, with room for a null byte.
65+
uint16_t len = strnlen(message, sizeof(log_buf) - 1);
66+
memcpy(log_buf, message, len);
67+
68+
// Add the null byte.
69+
log_buf[len] = '\0';
70+
71+
// Start the logging process.
72+
ret = ipc_notify_service(screen_service);
73+
if (ret != RETURNCODE_SUCCESS) {
74+
printf("Failed to request a log to screen.\n");
75+
return ret;
76+
}
77+
78+
// Wait for the log to complete.
79+
yield_for(&log_done);
80+
log_done = false;
81+
82+
return 0;
83+
}
84+
85+
static void dump_memory(uint32_t *start, uint32_t *end, const char *label) {
86+
for (uint32_t *addr = start; addr < end; addr++) {
87+
printf("[%s] %p: %08lX\n", label, addr, *addr);
88+
}
89+
}
90+
91+
int main(void) {
92+
returncode_t ret;
93+
94+
// Wait to receive the signal to start from the app selector.
95+
wait_for_start();
96+
97+
// Set up logging service.
98+
ret = setup_logging();
99+
if (ret < 0) {
100+
printf("ERROR: cannot set up logging\n");
101+
}
102+
103+
// Dump our own SRAM.
104+
ret = log_to_screen("Dumping own SRAM...");
105+
if (ret < 0) {
106+
printf("ERROR: cannot log to screen\n");
107+
}
108+
dump_memory((uint32_t *)SELF_SRAM_START, (uint32_t *)SELF_SRAM_END, "SELF");
109+
ret = log_to_screen("Dumping own SRAM complete!");
110+
if (ret < 0) {
111+
printf("ERROR: cannot log to screen\n");
112+
}
113+
114+
// Dump the encryption service SRAM.
115+
ret = log_to_screen("Dumping encryption SRAM...");
116+
if (ret < 0) {
117+
printf("ERROR: cannot log to screen\n");
118+
}
119+
dump_memory((uint32_t *)ENCRYPTION_SRAM_START, (uint32_t *)ENCRYPTION_SRAM_END, "ENCRYPTION");
120+
ret = log_to_screen("Dumping encryption SRAM complete!");
121+
if (ret < 0) {
122+
printf("ERROR: cannot log to screen\n");
123+
}
124+
125+
return 0;
126+
}

0 commit comments

Comments
 (0)