Skip to content

Commit 3088f23

Browse files
committed
Use shared Serial object
Although this is not ideal from a security perspective, for this example we want to share the Serial object to make sure that the app log does not get messed up by multiple threads trying to write to the serial port. For this purpose, we explicitly instantiated a `shared_pc` Serial object, instead of silently using `printf` as-is.
1 parent 2f27a07 commit 3088f23

File tree

5 files changed

+17
-37
lines changed

5 files changed

+17
-37
lines changed

source/client_a.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
struct box_context {
2424
uint32_t number;
25-
RawSerial * pc;
2625
};
2726

2827
static const UvisorBoxAclItem acl[] = {
@@ -56,8 +55,8 @@ static void box_async_runner(void)
5655
while (1) {
5756
uint32_t ret;
5857
int status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
59-
uvisor_ctx->pc->printf("client_a: Attempt to write 0x%08X (%s)\r\n",
60-
(unsigned int) number, (ret == 0) ? "granted" : "denied");
58+
shared_pc.printf("client_a: Attempt to write 0x%08X (%s)\r\n",
59+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
6160
/* FIXME: Add better error handling. */
6261
if (!status) {
6362
break;
@@ -73,21 +72,14 @@ static void box_sync_runner(void)
7372
while (1) {
7473
/* Synchronous access to the number. */
7574
const uint32_t number = secure_number_get_number();
76-
uvisor_ctx->pc->printf("client_a: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
75+
shared_pc.printf("client_a: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
7776

7877
Thread::wait(7000);
7978
}
8079
}
8180

8281
static void client_a_main(const void *)
8382
{
84-
/* Allocate serial port to ensure that code in this secure box won't touch
85-
* the handle in the default security context when printing. */
86-
uvisor_ctx->pc = new RawSerial(USBTX, USBRX);
87-
if (!uvisor_ctx->pc) {
88-
return;
89-
}
90-
9183
/* Create new threads. */
9284
/* Note: The stack must be at least 1kB since threads will use printf. */
9385
Thread sync(osPriorityNormal, 1024, NULL);

source/client_b.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
struct box_context {
2424
uint32_t number;
25-
RawSerial * pc;
2625
};
2726

2827
static const UvisorBoxAclItem acl[] = {
@@ -45,13 +44,6 @@ static uint32_t get_a_number()
4544

4645
static void client_b_main(const void *)
4746
{
48-
/* Allocate serial port to ensure that code in this secure box won't touch
49-
* the handle in the default security context when printing. */
50-
uvisor_ctx->pc = new RawSerial(USBTX, USBRX);
51-
if (!uvisor_ctx->pc) {
52-
return;
53-
}
54-
5547
/* The entire box code runs in its main thread. */
5648
while (1) {
5749
uvisor_rpc_result_t result;
@@ -65,16 +57,16 @@ static void client_b_main(const void *)
6557
while (1) {
6658
uint32_t ret;
6759
int status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
68-
uvisor_ctx->pc->printf("client_b: Attempt to write 0x%08X (%s)\r\n",
69-
(unsigned int) number, (ret == 0) ? "granted" : "denied");
60+
shared_pc.printf("client_b: Attempt to write 0x%08X (%s)\r\n",
61+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
7062
if (!status) {
7163
break;
7264
}
7365
}
7466

7567
/* Synchronous access to the number. */
7668
number = secure_number_get_number();
77-
uvisor_ctx->pc->printf("client_b: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
69+
shared_pc.printf("client_b: Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
7870

7971
Thread::wait(3000);
8072
}

source/main-hw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern DigitalOut led_red;
2121
extern DigitalOut led_green;
2222
extern DigitalOut led_blue;
2323

24+
extern Serial shared_pc;
25+
2426
#if defined(TARGET_K64F)
2527

2628
#define LED_ON false

source/main.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ DigitalOut led_red(LED1);
3232
DigitalOut led_green(LED2);
3333
DigitalOut led_blue(LED3);
3434

35+
Serial shared_pc(USBTX, USBRX);
36+
3537
static uint32_t get_a_number()
3638
{
3739
static uint32_t number = 425;
@@ -53,8 +55,8 @@ static void main_async_runner(void)
5355
/* TODO typesafe return codes */
5456
uint32_t ret;
5557
status = rpc_fncall_wait(result, UVISOR_WAIT_FOREVER, &ret);
56-
printf("public : Attempt to write 0x%08X (%s)\r\n",
57-
(unsigned int) number, (ret == 0) ? "granted" : "denied");
58+
shared_pc.printf("public : Attempt to write 0x%08X (%s)\r\n",
59+
(unsigned int) number, (ret == 0) ? "granted" : "denied");
5860
if (!status) {
5961
break;
6062
}
@@ -69,15 +71,15 @@ static void main_sync_runner(void)
6971
while (1) {
7072
/* Synchronous access to the number. */
7173
const uint32_t number = secure_number_get_number();
72-
printf("public : Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
74+
shared_pc.printf("public : Attempt to read : 0x%08X (granted)\r\n", (unsigned int) number);
7375

7476
Thread::wait(11000);
7577
}
7678
}
7779

7880
int main(void)
7981
{
80-
printf("\r\n***** uVisor secure number store example *****\r\n");
82+
shared_pc.printf("\r\n***** uVisor secure number store example *****\r\n");
8183
led_red = LED_OFF;
8284
led_blue = LED_OFF;
8385
led_green = LED_OFF;

source/secure_number.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ struct box_context {
2424
int trusted_id;
2525
int previous_box_caller;
2626
int caller_id;
27-
RawSerial * pc;
2827
};
2928

3029
static const UvisorBoxAclItem acl[] = {
@@ -103,13 +102,6 @@ static int set_number(uint32_t number)
103102

104103
static void number_store_main(const void *)
105104
{
106-
/* Allocate serial port to ensure that code in this secure box won't touch
107-
* the handle in the default security context when printing. */
108-
uvisor_ctx->pc = new RawSerial(USBTX, USBRX);
109-
if (!uvisor_ctx->pc) {
110-
return;
111-
}
112-
113105
/* Today we only allow client a to write to the number. */
114106
uvisor_ctx->trusted_id = -1;
115107

@@ -119,16 +111,16 @@ static void number_store_main(const void *)
119111
(TFN_Ptr) set_number
120112
};
121113

122-
uvisor_ctx->pc->printf("vault : Only client_a can write into the vault\r\n");
123-
uvisor_ctx->pc->printf("vault : All clients can read the vault\r\n");
114+
shared_pc.printf("vault : Only client_a can write into the vault\r\n");
115+
shared_pc.printf("vault : All clients can read the vault\r\n");
124116
while (1) {
125117
int status;
126118

127119
/* NOTE: This serializes all access to the number store! */
128120
status = rpc_fncall_waitfor(my_fn_array, 2, &uvisor_ctx->caller_id, UVISOR_WAIT_FOREVER);
129121

130122
if (status) {
131-
uvisor_ctx->pc->printf("Failure is not an option.\r\n");
123+
shared_pc.printf("Failure is not an option.\r\n");
132124
uvisor_error(USER_NOT_ALLOWED);
133125
}
134126
}

0 commit comments

Comments
 (0)