Switch to the mbedtls code style

Signed-off-by: Gilles Peskine <[email protected]>
This commit is contained in:
Gilles Peskine
2024-04-16 12:10:44 +02:00
parent ce275a9702
commit 001c09ded9
9 changed files with 362 additions and 300 deletions
+4 -4
View File
@@ -27,10 +27,10 @@ extern "C" {
#define PSA_DROP_CONNECTION PSA_ERROR_PROGRAMMER_ERROR
/* PSA message handles */
#define PSA_NULL_HANDLE ((psa_handle_t)0)
#define PSA_NULL_HANDLE ((psa_handle_t) 0)
#define PSA_HANDLE_IS_VALID(handle) ((psa_handle_t)(handle) > 0)
#define PSA_HANDLE_TO_ERROR(handle) ((psa_status_t)(handle))
#define PSA_HANDLE_IS_VALID(handle) ((psa_handle_t) (handle) > 0)
#define PSA_HANDLE_TO_ERROR(handle) ((psa_status_t) (handle))
#define PSA_MAX_IOVEC (4u)
@@ -63,7 +63,7 @@ uint32_t psa_version(uint32_t sid);
psa_handle_t psa_connect(uint32_t sid, uint32_t version);
psa_status_t psa_call(psa_handle_t handle,
int32_t type,
int32_t type,
const psa_invec *in_vec,
size_t in_len,
psa_outvec *out_vec,
+21 -21
View File
@@ -10,27 +10,27 @@
#include <stdint.h>
typedef int32_t psa_status_t;
#define PSA_SUCCESS ((psa_status_t)0)
#define PSA_SUCCESS ((psa_status_t) 0)
#define PSA_ERROR_PROGRAMMER_ERROR ((psa_status_t)-129)
#define PSA_ERROR_CONNECTION_REFUSED ((psa_status_t)-130)
#define PSA_ERROR_CONNECTION_BUSY ((psa_status_t)-131)
#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
#define PSA_ERROR_SERVICE_FAILURE ((psa_status_t)-144)
#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
#define PSA_ERROR_PROGRAMMER_ERROR ((psa_status_t) -129)
#define PSA_ERROR_CONNECTION_REFUSED ((psa_status_t) -130)
#define PSA_ERROR_CONNECTION_BUSY ((psa_status_t) -131)
#define PSA_ERROR_GENERIC_ERROR ((psa_status_t) -132)
#define PSA_ERROR_NOT_PERMITTED ((psa_status_t) -133)
#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t) -134)
#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t) -135)
#define PSA_ERROR_INVALID_HANDLE ((psa_status_t) -136)
#define PSA_ERROR_BAD_STATE ((psa_status_t) -137)
#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t) -138)
#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t) -139)
#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t) -140)
#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t) -141)
#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t) -142)
#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t) -143)
#define PSA_ERROR_SERVICE_FAILURE ((psa_status_t) -144)
#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t) -145)
#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t) -146)
#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t) -147)
#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t) -149)
#endif
+2 -2
View File
@@ -10,6 +10,6 @@
void raise_signal(psa_signal_t signal);
void __init_psasim(const char **array,
int size,
const int allow_ns_clients_array[32],
const int allow_ns_clients_array[32],
const uint32_t versions[32],
const int strict_policy_array[32]);
const int strict_policy_array[32]);
+119 -99
View File
@@ -29,9 +29,10 @@ typedef struct internal_handle {
/* Access to this global is not thread safe */
#define MAX_HANDLES 32
static internal_handle_t handles[MAX_HANDLES] = {{ 0 }};
static internal_handle_t handles[MAX_HANDLES] = { { 0 } };
static int get_next_free_handle() {
static int get_next_free_handle()
{
/* Never return handle 0 as it's a special null handle */
for (int i = 1; i < MAX_HANDLES; i++) {
if (handles[i].valid == 0) {
@@ -41,7 +42,8 @@ static int get_next_free_handle() {
return -1;
}
static int handle_is_valid(psa_handle_t handle) {
static int handle_is_valid(psa_handle_t handle)
{
if (handle > 0 && handle < MAX_HANDLES) {
if (handles[handle].valid == 1) {
return 1;
@@ -51,25 +53,26 @@ static int handle_is_valid(psa_handle_t handle) {
return 0;
}
static int get_queue_info(char *path, int *cqid, int *sqid) {
static int get_queue_info(char *path, int *cqid, int *sqid)
{
key_t server_queue_key;
int rx_qid, server_qid;
INFO ("Attempting to contact a RoT service queue");
INFO("Attempting to contact a RoT service queue");
if ((rx_qid = msgget (IPC_PRIVATE, 0660)) == -1) {
INFO ("msgget: rx_qid");
if ((rx_qid = msgget(IPC_PRIVATE, 0660)) == -1) {
INFO("msgget: rx_qid");
return -1;
}
if ((server_queue_key = ftok (path, PROJECT_ID)) == -1) {
INFO ("ftok");
if ((server_queue_key = ftok(path, PROJECT_ID)) == -1) {
INFO("ftok");
return -2;
}
if ((server_qid = msgget (server_queue_key, 0)) == -1) {
INFO ("msgget: server_qid");
if ((server_qid = msgget(server_queue_key, 0)) == -1) {
INFO("msgget: server_qid");
return -3;
}
@@ -80,7 +83,8 @@ static int get_queue_info(char *path, int *cqid, int *sqid) {
}
static psa_status_t process_response(int rx_qid, vectors_t *vecs, int type,
int *internal_server_qid) {
int *internal_server_qid)
{
struct message response, request;
psa_status_t ret = PSA_ERROR_CONNECTION_REFUSED;
@@ -96,98 +100,103 @@ static psa_status_t process_response(int rx_qid, vectors_t *vecs, int type,
outvec = 0;
// read response from server
if (msgrcv (rx_qid, &response, sizeof (struct message_text), 0, 0) == -1) {
puts (" msgrcv failed");
if (msgrcv(rx_qid, &response, sizeof(struct message_text), 0, 0) == -1) {
puts(" msgrcv failed");
return ret;
}
// process return message from server
switch(response.message_type) {
case PSA_REPLY:
memcpy(&ret, response.message_text.buf, sizeof(psa_status_t));
printf (" Message received from server: %d\n", ret);
if (type == PSA_IPC_CONNECT && ret > 0) {
*internal_server_qid = ret;
INFO(" ASSSIGNED q ID %d", *internal_server_qid);
ret = PSA_SUCCESS;
}
return ret;
break;
case READ_REQUEST:
/* read data request */
request.message_type = READ_RESPONSE;
switch (response.message_type) {
case PSA_REPLY:
memcpy(&ret, response.message_text.buf, sizeof(psa_status_t));
printf(" Message received from server: %d\n", ret);
if (type == PSA_IPC_CONNECT && ret > 0) {
*internal_server_qid = ret;
INFO(" ASSSIGNED q ID %d", *internal_server_qid);
ret = PSA_SUCCESS;
}
return ret;
break;
case READ_REQUEST:
/* read data request */
request.message_type = READ_RESPONSE;
assert(vecs != 0);
assert(vecs != 0);
memcpy(&invec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf+sizeof(size_t), sizeof(size_t));
INFO(" Partition asked for %lu bytes from invec %d", data_size, invec);
memcpy(&invec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf+sizeof(size_t), sizeof(size_t));
INFO(" Partition asked for %lu bytes from invec %d", data_size, invec);
/* need to add more checks here */
assert(invec >= 0 && invec < PSA_MAX_IOVEC);
/* need to add more checks here */
assert(invec >= 0 && invec < PSA_MAX_IOVEC);
if (data_size > MAX_FRAGMENT_SIZE) {
data_size = MAX_FRAGMENT_SIZE;
}
if (data_size > MAX_FRAGMENT_SIZE) {
data_size = MAX_FRAGMENT_SIZE;
}
/* send response */
INFO (" invec_seek[invec] is %lu", invec_seek[invec]);
INFO (" Reading from offset %p", vecs->in_vec[invec].base + invec_seek[invec]);
memcpy(request.message_text.buf, (vecs->in_vec[invec].base + invec_seek[invec]), data_size);
/* send response */
INFO(" invec_seek[invec] is %lu", invec_seek[invec]);
INFO(" Reading from offset %p", vecs->in_vec[invec].base + invec_seek[invec]);
memcpy(request.message_text.buf,
(vecs->in_vec[invec].base + invec_seek[invec]),
data_size);
/* update invec base TODO: check me */
invec_seek[invec] = invec_seek[invec] + data_size;
/* update invec base TODO: check me */
invec_seek[invec] = invec_seek[invec] + data_size;
INFO(" Sending message of type %li", request.message_type);
INFO(" with content %s\n", request.message_text.buf);
INFO(" Sending message of type %li", request.message_type);
INFO(" with content %s\n", request.message_text.buf);
if (msgsnd (*internal_server_qid, &request, sizeof(int) + sizeof(uint32_t) + data_size, 0) == -1) {
INFO("Internal error: failed to respond to read request");
}
break;
case WRITE_REQUEST:
assert(vecs != 0);
if (msgsnd(*internal_server_qid, &request,
sizeof(int) + sizeof(uint32_t) + data_size, 0) == -1) {
INFO("Internal error: failed to respond to read request");
}
break;
case WRITE_REQUEST:
assert(vecs != 0);
request.message_type = WRITE_RESPONSE;
request.message_type = WRITE_RESPONSE;
memcpy(&outvec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf + sizeof(size_t), sizeof(size_t));
INFO(" Partition wants to write %lu bytes to outvec %d", data_size, outvec);
memcpy(&outvec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf + sizeof(size_t), sizeof(size_t));
INFO(" Partition wants to write %lu bytes to outvec %d", data_size, outvec);
assert(outvec >= 0 && outvec < PSA_MAX_IOVEC);
assert(outvec >= 0 && outvec < PSA_MAX_IOVEC);
/* copy memory into message and send back amount written */
size_t sofar = vecs->out_vec[outvec].len;
memcpy(vecs->out_vec[outvec].base + sofar, response.message_text.buf+(sizeof(size_t)*2), data_size);
INFO (" Data size is %lu\n", data_size);
vecs->out_vec[outvec].len += data_size;
/* copy memory into message and send back amount written */
size_t sofar = vecs->out_vec[outvec].len;
memcpy(vecs->out_vec[outvec].base + sofar,
response.message_text.buf+(sizeof(size_t)*2), data_size);
INFO(" Data size is %lu\n", data_size);
vecs->out_vec[outvec].len += data_size;
INFO(" Sending message of type %li\n", request.message_type);
INFO(" Sending message of type %li\n", request.message_type);
/* send response */
if (msgsnd (*internal_server_qid, &request, sizeof(int) + data_size, 0) == -1) {
INFO("Internal error: failed to respond to write request");
}
break;
case SKIP_REQUEST:
memcpy(&invec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf+sizeof(size_t), sizeof(size_t));
INFO(" Partition asked to skip %lu bytes in invec %d", data_size, invec);
assert(invec >= 0 && invec < PSA_MAX_IOVEC);
/* update invec base TODO: check me */
invec_seek[invec] = invec_seek[invec] + data_size;
break;
/* send response */
if (msgsnd(*internal_server_qid, &request, sizeof(int) + data_size, 0) == -1) {
INFO("Internal error: failed to respond to write request");
}
break;
case SKIP_REQUEST:
memcpy(&invec, response.message_text.buf, sizeof(psa_status_t));
memcpy(&data_size, response.message_text.buf+sizeof(size_t), sizeof(size_t));
INFO(" Partition asked to skip %lu bytes in invec %d", data_size, invec);
assert(invec >= 0 && invec < PSA_MAX_IOVEC);
/* update invec base TODO: check me */
invec_seek[invec] = invec_seek[invec] + data_size;
break;
default:
FATAL(" ERROR: unknown internal message type: %ld\n",
response.message_type);
return ret;
default:
FATAL(" ERROR: unknown internal message type: %ld\n",
response.message_type);
return ret;
}
}
}
static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
int32_t type, uint32_t minor_version, vectors_t *vecs) {
int32_t type, uint32_t minor_version, vectors_t *vecs)
{
{
psa_status_t ret = PSA_ERROR_CONNECTION_REFUSED;
size_t request_msg_size = (sizeof(int) + sizeof(long)); /* msg type plus queue id */
@@ -211,7 +220,7 @@ static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
if (type == PSA_IPC_CONNECT) {
memcpy(request.message_text.buf, &minor_version, sizeof(minor_version));
request_msg_size = request_msg_size + sizeof(minor_version);
INFO (" Request msg size is %lu", request_msg_size);
INFO(" Request msg size is %lu", request_msg_size);
} else {
assert(internal_server_qid > 0);
}
@@ -221,13 +230,13 @@ static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
bzero(&vec_sizes, sizeof(vec_sizes));
/* Copy invec sizes */
for (size_t i=0; i < (vecs->in_len); i++) {
for (size_t i = 0; i < (vecs->in_len); i++) {
vec_sizes.invec_sizes[i] = vecs->in_vec[i].len;
INFO(" Client sending vector %lu: %lu", i, vec_sizes.invec_sizes[i]);
}
/* Copy outvec sizes */
for (size_t i=0; i < (vecs->out_len); i++) {
for (size_t i = 0; i < (vecs->out_len); i++) {
vec_sizes.outvec_sizes[i] = vecs->out_vec[i].len;
/* Reset to 0 since we need to eventually fill in with bytes written */
@@ -241,8 +250,8 @@ static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
INFO(" Sending and then waiting");
// send message to server
if (msgsnd (server_qid, &request, request_msg_size, 0) == -1) {
puts (" msgsnd failed");
if (msgsnd(server_qid, &request, request_msg_size, 0) == -1) {
puts(" msgsnd failed");
return ret;
}
@@ -251,22 +260,24 @@ static psa_status_t send(int rx_qid, int server_qid, int *internal_server_qid,
}
uint32_t psa_framework_version(void) {
uint32_t psa_framework_version(void)
{
return PSA_FRAMEWORK_VERSION;
}
psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version) {
psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version)
{
int idx;
psa_status_t ret;
char pathname [PATHNAMESIZE] = { 0 };
char pathname[PATHNAMESIZE] = { 0 };
idx = get_next_free_handle();
/* if there's a free handle available */
if (idx >= 0) {
snprintf(pathname, PATHNAMESIZE - 1, "/tmp/psa_service_%u", sid);
INFO ("Attempting to contact RoT service at %s", pathname);
INFO("Attempting to contact RoT service at %s", pathname);
/* if communication is possible */
if (get_queue_info(pathname, &handles[idx].client_qid, &handles[idx].server_qid) >= 0) {
@@ -283,10 +294,10 @@ psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version) {
handles[idx].valid = 1;
return idx;
} else {
INFO ("Server didn't like you");
INFO("Server didn't like you");
}
} else {
INFO ("Couldn't contact RoT service. Does it exist?");
INFO("Couldn't contact RoT service. Does it exist?");
if (__psa_ff_client_security_state == 0) {
PROGRAMMER_ERROR("Invalid SID");
@@ -294,14 +305,15 @@ psa_handle_t psa_connect(uint32_t sid, uint32_t minor_version) {
}
}
INFO ("Couldn't obtain a free handle");
INFO("Couldn't obtain a free handle");
return PSA_ERROR_CONNECTION_REFUSED;
}
uint32_t psa_version(uint32_t sid) {
uint32_t psa_version(uint32_t sid)
{
int idx;
psa_status_t ret;
char pathname [PATHNAMESIZE] = { 0 };
char pathname[PATHNAMESIZE] = { 0 };
idx = get_next_free_handle();
@@ -329,7 +341,8 @@ psa_status_t psa_call(psa_handle_t handle,
const psa_invec *in_vec,
size_t in_len,
psa_outvec *out_vec,
size_t out_len) {
size_t out_len)
{
handle_is_valid(handle);
@@ -343,18 +356,25 @@ psa_status_t psa_call(psa_handle_t handle,
vecs.out_vec = out_vec;
vecs.out_len = out_len;
return send (handles[handle].client_qid, handles[handle].server_qid, &handles[handle].internal_server_qid, type, 0, &vecs);
return send(handles[handle].client_qid,
handles[handle].server_qid,
&handles[handle].internal_server_qid,
type,
0,
&vecs);
}
void psa_close(psa_handle_t handle) {
void psa_close(psa_handle_t handle)
{
handle_is_valid(handle);
if (send (handles[handle].client_qid, handles[handle].server_qid, &handles[handle].internal_server_qid, PSA_IPC_DISCONNECT, 0, NULL)) {
if (send(handles[handle].client_qid, handles[handle].server_qid,
&handles[handle].internal_server_qid, PSA_IPC_DISCONNECT, 0, NULL)) {
puts("ERROR: Couldn't send disconnect msg");
} else {
if (msgctl(handles[handle].client_qid, IPC_RMID, NULL) != 0) {
puts("ERROR: Failed to delete msg queue");
}
}
INFO ("Closing handle %u", handle);
INFO("Closing handle %u", handle);
handles[handle].valid = 0;
}
+3 -2
View File
@@ -9,8 +9,9 @@
int __psa_ff_client_security_state = NON_SECURE;
#if 0
static void _printbits(uint32_t num) {
for (int i=0; i<32; i++) {
static void _printbits(uint32_t num)
{
for (int i = 0; i < 32; i++) {
if ((num >> (31-i) & 0x1)) {
INFO("1");
} else {
+8 -5
View File
@@ -14,15 +14,18 @@
#endif
#define PRINT(...) \
do { if (DEBUG_TEST) fprintf(stderr, __VA_ARGS__); } while (0)
do { if (DEBUG_TEST) fprintf(stderr, __VA_ARGS__); } while (0)
#define INFO(...) \
do { if (DEBUG_TEST) { PRINT("%s", __FILE__); PRINT(" INFO: " __VA_ARGS__); printf("\n"); } } while (0)
do { if (DEBUG_TEST) { PRINT("%s", __FILE__); PRINT(" INFO: " __VA_ARGS__); printf("\n"); \
} } while (0)
#define PROGRAMMER_ERROR(...) \
do { if (DEBUG_TEST) { PRINT( "%s:%d:%s(): PROGRAMMER ERROR", __FILE__, __LINE__, __func__ ); PRINT(__VA_ARGS__);} abort();} while (0)
do { if (DEBUG_TEST) { PRINT("%s:%d:%s(): PROGRAMMER ERROR", __FILE__, __LINE__, __func__); \
PRINT(__VA_ARGS__); } abort(); } while (0)
#define FATAL(...) \
do { if (DEBUG_TEST) { PRINT( "%s:%d:%s(): INTERNAL ERROR", __FILE__, __LINE__, __func__ ); PRINT(__VA_ARGS__);} abort();} while (0)
do { if (DEBUG_TEST) { PRINT("%s:%d:%s(): INTERNAL ERROR", __FILE__, __LINE__, __func__); PRINT( \
__VA_ARGS__); } abort(); } while (0)
#define PROJECT_ID 'M'
@@ -50,7 +53,7 @@ extern int __psa_ff_client_security_state;
struct message_text {
int qid;
int32_t psa_type;
char buf [MAX_FRAGMENT_SIZE];
char buf[MAX_FRAGMENT_SIZE];
};
+163 -127
View File
@@ -35,23 +35,25 @@ static int nsacl[32];
static int strict_policy[32] = { 0 };
static uint32_t rot_svc_versions[32];
static int rot_svc_incoming_queue[32] = { -1 };
static struct connection connections[MAX_CLIENTS] = {{ 0 }};
static struct connection connections[MAX_CLIENTS] = { { 0 } };
static uint32_t exposed_signals = 0;
void print_vectors(vector_sizes_t *sizes) {
void print_vectors(vector_sizes_t *sizes)
{
INFO("Printing iovec sizes");
for (int j=0; j < PSA_MAX_IOVEC; j++) {
for (int j = 0; j < PSA_MAX_IOVEC; j++) {
INFO("Invec %d: %lu", j, sizes->invec_sizes[j]);
}
for (int j=0; j < PSA_MAX_IOVEC; j++) {
for (int j = 0; j < PSA_MAX_IOVEC; j++) {
INFO("Outvec %d: %lu", j, sizes->outvec_sizes[j]);
}
}
int find_connection(uint32_t client) {
for(int i=1; i < MAX_CLIENTS; i++) {
int find_connection(uint32_t client)
{
for (int i = 1; i < MAX_CLIENTS; i++) {
if (client == connections[i].client) {
return i;
}
@@ -59,7 +61,8 @@ int find_connection(uint32_t client) {
return -1;
}
void destroy_connection(uint32_t client) {
void destroy_connection(uint32_t client)
{
int idx = find_connection(client);
if (idx >= 0) {
connections[idx].client = 0;
@@ -70,18 +73,21 @@ void destroy_connection(uint32_t client) {
}
}
int find_free_connection() {
int find_free_connection()
{
INFO("Allocating connection");
return find_connection(0);
}
static void reply(psa_handle_t msg_handle, psa_status_t status) {
static void reply(psa_handle_t msg_handle, psa_status_t status)
{
pending_message[msg_handle] = 1;
psa_reply(msg_handle, status);
pending_message[msg_handle] = 0;
}
psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
{
psa_signal_t mask;
struct message msg;
@@ -99,21 +105,23 @@ psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
mask = signal_mask;
/* Check the status of each queue */
for (int i=0; i < 32; i++) {
for (int i = 0; i < 32; i++) {
if (mask & 0x1) {
if (i < 3) {
// do nothing (reserved)
}
else if (i == 3) {
} else if (i == 3) {
// this must be psa doorbell
}
else {
} else {
/* Check if this signal corresponds to a queue */
if (rot_svc_incoming_queue[i] >= 0 && (pending_message[i] == 0)) {
/* AFAIK there is no "peek" method in SysV, so try to get a message */
len = msgrcv (rot_svc_incoming_queue[i], &msg, sizeof (struct message_text), 0, IPC_NOWAIT);
len = msgrcv(rot_svc_incoming_queue[i],
&msg,
sizeof(struct message_text),
0,
IPC_NOWAIT);
if (len > 0) {
INFO("Storing that QID in message_client[%d]\n", i);
@@ -130,7 +138,8 @@ psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
/* Check if NS client is allowed for this RoT service */
if (nsacl[i] <= 0) {
#if 0
INFO("Rejecting non-secure client due to manifest security policy");
INFO(
"Rejecting non-secure client due to manifest security policy");
reply(i, PSA_ERROR_CONNECTION_REFUSED);
continue; /* Skip to next signal */
#endif
@@ -142,73 +151,80 @@ psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
INFO("Got a message from client ID %d\n", messages[i].client_id);
INFO("Message type is %lu\n", msg.message_type);
INFO("PSA message type is %d\n", msg.message_text.psa_type);
INFO("PSA message type is %d\n", msg.message_text.psa_type);
messages[i].handle = i;
switch(msg.message_text.psa_type) {
case PSA_IPC_CONNECT:
switch (msg.message_text.psa_type) {
case PSA_IPC_CONNECT:
if (len >= 16) {
memcpy(&requested_version, msg.message_text.buf, sizeof(requested_version));
INFO ("Requesting version %u\n", requested_version);
INFO ("Implemented version %u\n", rot_svc_versions[i]);
/* TODO: need to check whether the policy is strict,
* and if so, then reject the client if the number doesn't match */
if (len >= 16) {
memcpy(&requested_version, msg.message_text.buf,
sizeof(requested_version));
INFO("Requesting version %u\n", requested_version);
INFO("Implemented version %u\n", rot_svc_versions[i]);
/* TODO: need to check whether the policy is strict,
* and if so, then reject the client if the number doesn't match */
if (requested_version > rot_svc_versions[i]) {
INFO("Rejecting client because requested version that was too high");
reply(i, PSA_ERROR_CONNECTION_REFUSED);
continue; /* Skip to next signal */
if (requested_version > rot_svc_versions[i]) {
INFO(
"Rejecting client because requested version that was too high");
reply(i, PSA_ERROR_CONNECTION_REFUSED);
continue; /* Skip to next signal */
}
if (strict_policy[i] == 1 &&
(requested_version != rot_svc_versions[i])) {
INFO(
"Rejecting client because enforcing a STRICT version policy");
reply(i, PSA_ERROR_CONNECTION_REFUSED);
continue; /* Skip to next signal */
} else {
INFO("Not rejecting client");
}
}
if (strict_policy[i] == 1 && (requested_version != rot_svc_versions[i])) {
INFO("Rejecting client because enforcing a STRICT version policy");
reply(i, PSA_ERROR_CONNECTION_REFUSED);
continue; /* Skip to next signal */
messages[i].type = PSA_IPC_CONNECT;
if (idx < 0) {
idx = find_free_connection();
}
if (idx >= 0) {
connections[idx].client = msg.message_text.qid;
} else {
INFO ("Not rejecting client");
/* We've run out of system wide connections */
reply(i, PSA_ERROR_CONNECTION_BUSY);
INFO("Ran out of free connections");
continue;
}
}
messages[i].type = PSA_IPC_CONNECT;
break;
case PSA_IPC_DISCONNECT:
messages[i].type = PSA_IPC_DISCONNECT;
break;
case VERSION_REQUEST:
INFO("Got a version request");
reply(i, rot_svc_versions[i]);
continue; /* Skip to next signal */
break;
if (idx < 0) {
idx = find_free_connection();
}
default:
if (idx >= 0) {
connections[idx].client = msg.message_text.qid;
} else {
/* We've run out of system wide connections */
reply(i, PSA_ERROR_CONNECTION_BUSY);
INFO ("Ran out of free connections");
continue;
}
break;
case PSA_IPC_DISCONNECT:
messages[i].type = PSA_IPC_DISCONNECT;
break;
case VERSION_REQUEST:
INFO("Got a version request");
reply(i, rot_svc_versions[i]);
continue; /* Skip to next signal */
break;
default:
/* PSA CALL */
if (msg.message_text.psa_type >= 0) {
messages[i].type = msg.message_text.psa_type;
memcpy(&sizes, msg.message_text.buf, sizeof(sizes));
print_vectors(&sizes);
memcpy(&messages[i].in_size, &sizes.invec_sizes, (sizeof(size_t) * PSA_MAX_IOVEC));
memcpy(&messages[i].out_size, &sizes.outvec_sizes, (sizeof(size_t) * PSA_MAX_IOVEC));
} else {
FATAL("UNKNOWN MESSAGE TYPE RECEIVED %li\n", msg.message_type);
}
break;
/* PSA CALL */
if (msg.message_text.psa_type >= 0) {
messages[i].type = msg.message_text.psa_type;
memcpy(&sizes, msg.message_text.buf, sizeof(sizes));
print_vectors(&sizes);
memcpy(&messages[i].in_size, &sizes.invec_sizes,
(sizeof(size_t) * PSA_MAX_IOVEC));
memcpy(&messages[i].out_size, &sizes.outvec_sizes,
(sizeof(size_t) * PSA_MAX_IOVEC));
} else {
FATAL("UNKNOWN MESSAGE TYPE RECEIVED %li\n",
msg.message_type);
}
break;
}
messages[i].handle = i;
@@ -237,8 +253,7 @@ psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
/* There is no 'select' function in SysV to block on multiple queues, so busy-wait :( */
usleep(50000);
}
}
while (timeout == PSA_BLOCK);
} while (timeout == PSA_BLOCK);
INFO("\n");
@@ -246,13 +261,14 @@ psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout) {
return signal_mask & exposed_signals;
}
static int signal_to_index(psa_signal_t signal) {
static int signal_to_index(psa_signal_t signal)
{
int i;
int count = 0;
int ret = -1;
for (i=0; i<32; i++) {
for (i = 0; i < 32; i++) {
if (signal & 0x1) {
ret = i;
count++;
@@ -267,15 +283,18 @@ static int signal_to_index(psa_signal_t signal) {
return ret;
}
static void clear_signal(psa_signal_t signal) {
static void clear_signal(psa_signal_t signal)
{
exposed_signals = exposed_signals & ~signal;
}
void raise_signal(psa_signal_t signal) {
void raise_signal(psa_signal_t signal)
{
exposed_signals |= signal;
}
psa_status_t psa_get(psa_signal_t signal, psa_msg_t *msg) {
psa_status_t psa_get(psa_signal_t signal, psa_msg_t *msg)
{
int index = signal_to_index(signal);
if (index < 0) {
PROGRAMMER_ERROR("Bad signal\n");
@@ -297,22 +316,25 @@ psa_status_t psa_get(psa_signal_t signal, psa_msg_t *msg) {
return PSA_ERROR_DOES_NOT_EXIST;
}
static int is_valid_msg_handle(psa_handle_t h) {
static int is_valid_msg_handle(psa_handle_t h)
{
if (h > 0 && h < MAX_MESSAGES) {
return 1;
}
PROGRAMMER_ERROR("Not a valid message handle");
}
static inline int is_call_msg(psa_handle_t h) {
static inline int is_call_msg(psa_handle_t h)
{
assert(messages[h].type >= PSA_IPC_CALL);
return 1;
}
void psa_set_rhandle(psa_handle_t msg_handle, void *rhandle) {
void psa_set_rhandle(psa_handle_t msg_handle, void *rhandle)
{
is_valid_msg_handle(msg_handle);
int idx = find_connection(message_client[msg_handle]);
INFO ("Setting rhandle to %p", rhandle);
INFO("Setting rhandle to %p", rhandle);
assert(idx >= 0);
connections[idx].rhandle = rhandle;
}
@@ -323,7 +345,8 @@ static void send_msg(psa_handle_t msg_handle,
psa_status_t status,
size_t amount,
const void *data,
size_t data_amount) {
size_t data_amount)
{
struct message response;
int flags = 0;
@@ -343,14 +366,16 @@ static void send_msg(psa_handle_t msg_handle,
}
/* TODO: sizeof doesn't need to be so big here for small responses */
if (msgsnd (message_client[msg_handle], &response, sizeof (response.message_text), flags) == -1) {
if (msgsnd(message_client[msg_handle], &response, sizeof(response.message_text), flags) == -1) {
INFO("Failed to reply");
}
}
static size_t skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes) {
static size_t skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes)
{
if (num_bytes < (messages[msg_handle].in_size[invec_idx] - num_bytes)) {
messages[msg_handle].in_size[invec_idx] = messages[msg_handle].in_size[invec_idx] - num_bytes;
messages[msg_handle].in_size[invec_idx] = messages[msg_handle].in_size[invec_idx] -
num_bytes;
return num_bytes;
} else {
if (num_bytes >= messages[msg_handle].in_size[invec_idx]) {
@@ -364,7 +389,8 @@ static size_t skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes
}
size_t psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
void *buffer, size_t num_bytes) {
void *buffer, size_t num_bytes)
{
size_t sofar = 0;
struct message msg = { 0 };
int idx;
@@ -389,7 +415,7 @@ size_t psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
idx = find_connection(message_client[msg_handle]);
assert(idx >= 0);
len = msgrcv (connections[idx].client_to_server_q, &msg, sizeof (struct message_text), 0, 0);
len = msgrcv(connections[idx].client_to_server_q, &msg, sizeof(struct message_text), 0, 0);
len = (len - sizeof(msg.message_text.qid));
if (len < 0) {
@@ -416,7 +442,8 @@ size_t psa_read(psa_handle_t msg_handle, uint32_t invec_idx,
}
void psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
const void *buffer, size_t num_bytes) {
const void *buffer, size_t num_bytes)
{
size_t sofar = 0;
struct message msg = { 0 };
@@ -431,7 +458,8 @@ void psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
}
if (num_bytes > messages[msg_handle].out_size[outvec_idx]) {
PROGRAMMER_ERROR("Program tried to write too much data %lu/%lu", num_bytes, messages[msg_handle].out_size[outvec_idx]);
PROGRAMMER_ERROR("Program tried to write too much data %lu/%lu", num_bytes,
messages[msg_handle].out_size[outvec_idx]);
}
while (sofar < num_bytes) {
@@ -447,7 +475,7 @@ void psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
idx = find_connection(message_client[msg_handle]);
assert(idx >= 0);
len = msgrcv (connections[idx].client_to_server_q, &msg, sizeof (struct message_text), 0, 0);
len = msgrcv(connections[idx].client_to_server_q, &msg, sizeof(struct message_text), 0, 0);
if (len < 1) {
FATAL("Client didn't give me a full response");
}
@@ -458,7 +486,8 @@ void psa_write(psa_handle_t msg_handle, uint32_t outvec_idx,
messages[msg_handle].out_size[outvec_idx] -= num_bytes;
}
size_t psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes) {
size_t psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes)
{
is_valid_msg_handle(msg_handle);
is_call_msg(msg_handle);
@@ -470,17 +499,19 @@ size_t psa_skip(psa_handle_t msg_handle, uint32_t invec_idx, size_t num_bytes) {
return ret;
}
static void destroy_temporary_queue(int myqid) {
static void destroy_temporary_queue(int myqid)
{
if (msgctl(myqid, IPC_RMID, NULL) != 0) {
INFO("ERROR: Failed to delete msg queue %d", myqid);
}
}
static int make_temporary_queue() {
static int make_temporary_queue()
{
int myqid;
if ((myqid = msgget (IPC_PRIVATE, 0660)) == -1) {
INFO ("msgget: myqid");
if ((myqid = msgget(IPC_PRIVATE, 0660)) == -1) {
INFO("msgget: myqid");
return -1;
}
return myqid;
@@ -489,7 +520,8 @@ static int make_temporary_queue() {
/**
* Assumes msg_handle is the index into the message array
*/
void psa_reply(psa_handle_t msg_handle, psa_status_t status) {
void psa_reply(psa_handle_t msg_handle, psa_status_t status)
{
int idx, q;
is_valid_msg_handle(msg_handle);
@@ -498,28 +530,28 @@ void psa_reply(psa_handle_t msg_handle, psa_status_t status) {
}
if (messages[msg_handle].type == PSA_IPC_CONNECT) {
switch(status) {
case PSA_SUCCESS:
idx = find_connection(message_client[msg_handle]);
q = make_temporary_queue();
if (q > 0 && idx >= 0) {
connections[idx].client_to_server_q = q;
status = q;
} else {
FATAL("What happened?");
}
break;
case PSA_ERROR_CONNECTION_REFUSED:
destroy_connection(message_client[msg_handle]);
break;
case PSA_ERROR_CONNECTION_BUSY:
destroy_connection(message_client[msg_handle]);
break;
case PSA_ERROR_PROGRAMMER_ERROR:
destroy_connection(message_client[msg_handle]);
break;
default:
PROGRAMMER_ERROR("Not a valid reply %d\n", status);
switch (status) {
case PSA_SUCCESS:
idx = find_connection(message_client[msg_handle]);
q = make_temporary_queue();
if (q > 0 && idx >= 0) {
connections[idx].client_to_server_q = q;
status = q;
} else {
FATAL("What happened?");
}
break;
case PSA_ERROR_CONNECTION_REFUSED:
destroy_connection(message_client[msg_handle]);
break;
case PSA_ERROR_CONNECTION_BUSY:
destroy_connection(message_client[msg_handle]);
break;
case PSA_ERROR_PROGRAMMER_ERROR:
destroy_connection(message_client[msg_handle]);
break;
default:
PROGRAMMER_ERROR("Not a valid reply %d\n", status);
}
} else if (messages[msg_handle].type == PSA_IPC_DISCONNECT) {
idx = find_connection(message_client[msg_handle]);
@@ -536,7 +568,8 @@ void psa_reply(psa_handle_t msg_handle, psa_status_t status) {
}
/* TODO: make sure you only clear interrupt signals, and not others */
void psa_eoi(psa_signal_t signal) {
void psa_eoi(psa_signal_t signal)
{
int index = signal_to_index(signal);
if (index >= 0 && (rot_svc_incoming_queue[index] >= 0)) {
clear_signal(signal);
@@ -545,7 +578,8 @@ void psa_eoi(psa_signal_t signal) {
}
}
void psa_notify(int32_t partition_id) {
void psa_notify(int32_t partition_id)
{
char pathname[PATHNAMESIZE] = { 0 };
if (partition_id < 0) {
@@ -558,7 +592,8 @@ void psa_notify(int32_t partition_id) {
INFO("psa_notify is unimplemented");
}
void psa_clear(void) {
void psa_clear(void)
{
clear_signal(PSA_DOORBELL);
}
@@ -566,7 +601,8 @@ void __init_psasim(const char **array,
int size,
const int allow_ns_clients_array[32],
const uint32_t versions[32],
const int strict_policy_array[32] ) {
const int strict_policy_array[32])
{
static uint8_t library_initialised = 0;
key_t key;
@@ -587,7 +623,7 @@ void __init_psasim(const char **array,
array[3] = doorbell_path;
for (int i=0; i < 32; i++) {
for (int i = 0; i < 32; i++) {
if (strncmp(array[i], "", 1) != 0) {
INFO("Setting up %s", array[i]);
@@ -597,12 +633,12 @@ void __init_psasim(const char **array,
fclose(fp);
}
if ((key = ftok (array[i], PROJECT_ID)) == -1) {
if ((key = ftok(array[i], PROJECT_ID)) == -1) {
FATAL("Error finding message queue during initialisation");
}
/* TODO: Investigate. Permissions are likely to be too relaxed */
if ((qid = msgget (key, IPC_CREAT | 0660)) == -1) {
if ((qid = msgget(key, IPC_CREAT | 0660)) == -1) {
FATAL("Error opening message queue during initialisation");
} else {
rot_svc_incoming_queue[i] = qid;
+5 -4
View File
@@ -10,9 +10,10 @@
#include <stdio.h>
#include <unistd.h>
int main() {
int main()
{
const char* text = "FOOBARCOOL!!";
const char *text = "FOOBARCOOL!!";
char output[100] = { 0 };
printf("My PID is %d\n", getpid());
@@ -21,10 +22,10 @@ int main() {
psa_handle_t h = psa_connect(PSA_SID_SHA256_SID, 1);
if (h < 0) {
printf ("Couldn't connect %d\n", h);
printf("Couldn't connect %d\n", h);
return 1;
} else {
int type = 2;
int type = 2;
puts("Calling!");
puts("Trying without invec");
printf("Answer to my call was %d (no invec)\n", psa_call(h, type, NULL, 0, NULL, 0));
+37 -36
View File
@@ -10,8 +10,9 @@
#include <unistd.h>
#include <stdio.h>
void printbits(uint32_t num) {
for (int i=0; i<32; i++) {
void printbits(uint32_t num)
{
for (int i = 0; i < 32; i++) {
if ((num >> (31-i) & 0x1)) {
printf("1");
} else {
@@ -32,7 +33,7 @@ int psa_sha256_main()
puts("Starting");
while(1) {
while (1) {
puts("Calling psa_wait");
psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
@@ -46,43 +47,43 @@ int psa_sha256_main()
if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) {
printf("My handle is %d\n", msg.handle);
printf("My rhandle is %p\n", (int *) msg.rhandle);
switch(msg.type) {
case PSA_IPC_CONNECT:
puts("Got a connection message");
psa_set_rhandle(msg.handle, (void *) &magic_num);
ret = PSA_SUCCESS;
break;
case PSA_IPC_DISCONNECT:
puts("Got a disconnection message");
ret = PSA_SUCCESS;
break;
switch (msg.type) {
case PSA_IPC_CONNECT:
puts("Got a connection message");
psa_set_rhandle(msg.handle, (void *) &magic_num);
ret = PSA_SUCCESS;
break;
case PSA_IPC_DISCONNECT:
puts("Got a disconnection message");
ret = PSA_SUCCESS;
break;
default:
printf("Got an IPC call of type %d\n", msg.type);
ret = 42;
size_t size = msg.in_size[0];
default:
printf("Got an IPC call of type %d\n", msg.type);
ret = 42;
size_t size = msg.in_size[0];
if ((size > 0) && (size <= sizeof(foo))) {
psa_read(msg.handle, 0, foo, 6);
foo[(BUF_SIZE-1)] = '\0';
printf("Reading payload: %s\n", foo);
psa_read(msg.handle, 0, foo+6, 6);
foo[(BUF_SIZE-1)] = '\0';
printf("Reading payload: %s\n", foo);
}
if ((size > 0) && (size <= sizeof(foo))) {
psa_read(msg.handle, 0, foo, 6);
foo[(BUF_SIZE-1)] = '\0';
printf("Reading payload: %s\n", foo);
psa_read(msg.handle, 0, foo+6, 6);
foo[(BUF_SIZE-1)] = '\0';
printf("Reading payload: %s\n", foo);
}
size = msg.out_size[0];
if ((size > 0)) {
puts("Writing response");
psa_write(msg.handle, 0, "RESP", 4);
psa_write(msg.handle, 0, "ONSE", 4);
}
size = msg.out_size[0];
if ((size > 0)) {
puts("Writing response");
psa_write(msg.handle, 0, "RESP", 4);
psa_write(msg.handle, 0, "ONSE", 4);
}
if (msg.client_id > 0) {
psa_notify(msg.client_id);
} else {
puts("Client is non-secure, so won't notify");
}
if (msg.client_id > 0) {
psa_notify(msg.client_id);
} else {
puts("Client is non-secure, so won't notify");
}
}