Merge "Enable non-secure side to receive messages > 4K"

This commit is contained in:
Treehugger Robot 2017-08-12 03:02:01 +00:00 committed by Gerrit Code Review
commit 28fa8b0924
3 changed files with 42 additions and 25 deletions

View file

@ -24,7 +24,8 @@
// Commands // Commands
enum keymaster_command : uint32_t { enum keymaster_command : uint32_t {
KEYMASTER_RESP_BIT = 1, KEYMASTER_RESP_BIT = 1,
KEYMASTER_REQ_SHIFT = 1, KEYMASTER_STOP_BIT = 2,
KEYMASTER_REQ_SHIFT = 2,
KM_GENERATE_KEY = (0 << KEYMASTER_REQ_SHIFT), KM_GENERATE_KEY = (0 << KEYMASTER_REQ_SHIFT),
KM_BEGIN_OPERATION = (1 << KEYMASTER_REQ_SHIFT), KM_BEGIN_OPERATION = (1 << KEYMASTER_REQ_SHIFT),

View file

@ -36,7 +36,8 @@
#include "trusty_keymaster_device.h" #include "trusty_keymaster_device.h"
#include "trusty_keymaster_ipc.h" #include "trusty_keymaster_ipc.h"
const uint32_t RECV_BUF_SIZE = PAGE_SIZE; // Maximum size of message from Trusty is 8K (for RSA attestation key and chain)
const uint32_t RECV_BUF_SIZE = 2*PAGE_SIZE;
const uint32_t SEND_BUF_SIZE = (PAGE_SIZE - sizeof(struct keymaster_message) - 16 /* tipc header */); const uint32_t SEND_BUF_SIZE = (PAGE_SIZE - sizeof(struct keymaster_message) - 16 /* tipc header */);
const size_t kMaximumAttestationChallengeLength = 128; const size_t kMaximumAttestationChallengeLength = 128;
@ -770,6 +771,9 @@ keymaster_error_t TrustyKeymasterDevice::Send(uint32_t command, const Serializab
ALOGV("Sending %d byte request\n", (int)req.SerializedSize()); ALOGV("Sending %d byte request\n", (int)req.SerializedSize());
int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size); int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size);
if (rc < 0) { if (rc < 0) {
// Reset the connection on tipc error
trusty_keymaster_disconnect();
trusty_keymaster_connect();
ALOGE("tipc error: %d\n", rc); ALOGE("tipc error: %d\n", rc);
// TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately. // TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately.
return translate_error(rc); return translate_error(rc);
@ -777,8 +781,7 @@ keymaster_error_t TrustyKeymasterDevice::Send(uint32_t command, const Serializab
ALOGV("Received %d byte response\n", rsp_size); ALOGV("Received %d byte response\n", rsp_size);
} }
const keymaster_message* msg = (keymaster_message*)recv_buf; const uint8_t* p = recv_buf;
const uint8_t* p = msg->payload;
if (!rsp->Deserialize(&p, p + rsp_size)) { if (!rsp->Deserialize(&p, p + rsp_size)) {
ALOGE("Error deserializing response of size %d\n", (int)rsp_size); ALOGE("Error deserializing response of size %d\n", (int)rsp_size);
return KM_ERROR_UNKNOWN_ERROR; return KM_ERROR_UNKNOWN_ERROR;

View file

@ -23,6 +23,8 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <algorithm>
#include <log/log.h> #include <log/log.h>
#include <trusty/tipc.h> #include <trusty/tipc.h>
@ -31,7 +33,7 @@
#define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0" #define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0"
static int handle_ = 0; static int handle_ = -1;
int trusty_keymaster_connect() { int trusty_keymaster_connect() {
int rc = tipc_connect(TRUSTY_DEVICE_NAME, KEYMASTER_PORT); int rc = tipc_connect(TRUSTY_DEVICE_NAME, KEYMASTER_PORT);
@ -45,7 +47,7 @@ int trusty_keymaster_connect() {
int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out, int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out,
uint32_t* out_size) { uint32_t* out_size) {
if (handle_ == 0) { if (handle_ < 0) {
ALOGE("not connected\n"); ALOGE("not connected\n");
return -EINVAL; return -EINVAL;
} }
@ -62,32 +64,43 @@ int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out
ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno)); ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno));
return -errno; return -errno;
} }
size_t out_max_size = *out_size;
*out_size = 0;
struct iovec iov[2];
struct keymaster_message header;
iov[0] = {.iov_base = &header, .iov_len = sizeof(struct keymaster_message)};
while (true) {
iov[1] = {
.iov_base = out + *out_size,
.iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH, out_max_size - *out_size)};
rc = readv(handle_, iov, 2);
if (rc < 0) {
ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT,
strerror(errno));
return -errno;
}
rc = read(handle_, out, *out_size); if ((size_t)rc < sizeof(struct keymaster_message)) {
if (rc < 0) { ALOGE("invalid response size (%d)\n", (int)rc);
ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, return -EINVAL;
strerror(errno)); }
return -errno;
if ((cmd | KEYMASTER_RESP_BIT) != (header.cmd & ~(KEYMASTER_STOP_BIT))) {
ALOGE("invalid command (%d)", header.cmd);
return -EINVAL;
}
*out_size += ((size_t)rc - sizeof(struct keymaster_message));
if (header.cmd & KEYMASTER_STOP_BIT) {
break;
}
} }
if ((size_t)rc < sizeof(struct keymaster_message)) {
ALOGE("invalid response size (%d)\n", (int)rc);
return -EINVAL;
}
msg = (struct keymaster_message*)out;
if ((cmd | KEYMASTER_RESP_BIT) != msg->cmd) {
ALOGE("invalid command (%d)", msg->cmd);
return -EINVAL;
}
*out_size = ((size_t)rc) - sizeof(struct keymaster_message);
return rc; return rc;
} }
void trusty_keymaster_disconnect() { void trusty_keymaster_disconnect() {
if (handle_ != 0) { if (handle_ >= 0) {
tipc_close(handle_); tipc_close(handle_);
} }
handle_ = -1;
} }