summaryrefslogtreecommitdiff
path: root/lib/trusty/ql-tipc/rpmb_proxy.c
blob: 74ce4b32e876c084c2d9dd14c33674ccc47dff5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <trusty/rpmb.h>
#include <trusty/trusty_ipc.h>
#include <trusty/util.h>
#include <interface/storage/storage.h>

#define LOCAL_LOG 0

static bool initialized;
/* Address of rpmb device */
static void *proxy_rpmb;
struct trusty_ipc_chan proxy_chan;

struct storage_msg req_msg;
static uint8_t req_buf[4096];
static uint8_t read_buf[4096];
bool proxy_resp_flag = false;
static int req_len = 0;

/*
 * Read RPMB request from storage service. Writes message to @msg
 * and @req.
 *
 * @chan:    proxy ipc channel
 * @msg:     address of storage message header
 * @req:     address of storage message request
 * @req_len: length of req in bytes
 */
static int proxy_read_request(struct trusty_ipc_chan *chan,
                              struct storage_msg *msg, void *req,
                              size_t req_len)
{
    int rc;

    struct trusty_ipc_iovec req_iovs[2] = {
        { .base = msg, .len = sizeof(*msg) },
        { .base = req, .len = req_len },
    };
    rc = trusty_ipc_recv(chan, req_iovs, 2, false);
    if (rc < 0) {
        /* recv message failed */
        trusty_error("%s: failed (%d) to recv request\n", __func__, rc);
        return rc;
    }

    if ((size_t)rc < sizeof(*msg)) {
        /* malformed message */
        trusty_error("%s: malformed request (%zu)\n", __func__, (size_t)rc);
        return TRUSTY_ERR_GENERIC;
    }

    return rc - sizeof(*msg); /* return payload size */
}

/*
 * Read RPMB response from storage service. Writes message to @msg
 * and @req.
 *
 * @chan:     proxy ipc channel
 * @msg:      address of storage message header
 * @cmd:      cmd corresponding to the request
 * @resp:     address of storage message response
 * @resp_len: length of resp in bytes
 */
static int proxy_read_response(struct trusty_ipc_chan *chan, struct storage_msg *msg,
                               uint32_t cmd, void *resp, size_t resp_len)
{
    int rc;

    struct trusty_ipc_iovec resp_iovs[2] = {
        { .base = msg, .len = sizeof(*msg) },
        { .base = resp, .len = resp_len },
    };
    rc = trusty_ipc_recv(chan, resp_iovs, resp ? 2 : 1, true);
    if (rc < 0) {
        /* recv message failed */
        trusty_error("%s: failed (%d) to recv response\n", __func__, rc);
        return rc;
    }
    if (proxy_resp_flag) {
        memcpy(msg, &req_msg, sizeof(struct storage_msg));
        if (resp)
            memcpy(resp, req_buf, req_len);
        rc = req_len + sizeof(struct storage_msg);
        proxy_resp_flag = false;
    }

    if ((size_t)rc < sizeof(*msg)) {
        /* malformed message */
        trusty_error("%s: malformed request (%zu)\n", __func__, (size_t)rc);
        return TRUSTY_ERR_GENERIC;
    }

    if (msg->cmd != (cmd | STORAGE_RESP_BIT)) {
        trusty_error("malformed response, cmd: 0x%x\n", msg->cmd);
        return TRUSTY_ERR_GENERIC;
    }

    return rc - sizeof(*msg); /* return payload size */
}

/*
 * Send RPMB response to storage service
 *
 * @chan:     proxy ipc channel
 * @msg:      address of storage message header
 * @resp:     address of storage message response
 * @resp_len: length of resp in bytes
 */
static int proxy_send_response(struct trusty_ipc_chan *chan,
                               struct storage_msg *msg, void *resp,
                               size_t resp_len)
{
    struct trusty_ipc_iovec resp_iovs[2] = {
        { .base = msg, .len = sizeof(*msg) },
        { .base = resp, .len = resp_len }
    };

    msg->cmd |= STORAGE_RESP_BIT;
    return trusty_ipc_send(chan, resp_iovs, resp ? 2 : 1, false);
}

/*
 * Send RPMB request to storage service
 *
 * @chan:     proxy ipc channel
 * @msg:      address of storage message header
 * @req:      address of storage message request
 * @req_len:  length of request in bytes
 */
static int proxy_send_request(struct trusty_ipc_chan *chan,
                              struct storage_msg *msg, void *req,
                              size_t req_len)
{
    struct trusty_ipc_iovec req_iovs[2] = {
        { .base = msg, .len = sizeof(*msg) },
        { .base = req, .len = req_len }
    };

    return trusty_ipc_send(chan, req_iovs, req ? 2 : 1, false);
}

/*
 * Convenience function to send a request to the storage service and read the
 * response.
 *
 * @cmd: the command
 * @req: the request buffer
 * @req_size: size of the request buffer
 * @resp: the response buffer
 * @resp_size_p: pointer to the size of the response buffer. changed to the
                 actual size of the response read from the secure side
 */
static int storage_do_tipc(uint32_t cmd, void *req, uint32_t req_size, void *resp,
                       uint32_t *resp_size_p)
{
    int rc;
    struct storage_msg msg = { .cmd = cmd };

    if (!initialized) {
        trusty_error("%s: Secure storage TIPC client not initialized\n", __func__);
        return TRUSTY_ERR_GENERIC;
    }
    rc = proxy_send_request(&proxy_chan, &msg, req, req_size);
    if (rc < 0) {
        trusty_error("%s: failed (%d) to send storage request\n", __func__, rc);
        return rc;
    }

    uint32_t resp_size = resp_size_p ? *resp_size_p : 0;
    rc = proxy_read_response(&proxy_chan, &msg, cmd, resp, resp_size);
    if (rc < 0) {
        trusty_error("%s: failed (%d) to read secure storage response\n", __func__, rc);
        return rc;
    }
    /* change response size to actual response size */
    if (resp_size_p && rc != *resp_size_p) {
        *resp_size_p = rc;
    }
    if (msg.result != STORAGE_NO_ERROR) {
        trusty_error("%s: secure storage service returned error (%d)\n", __func__,
                     msg.result);
        return TRUSTY_ERR_GENERIC;
    }
    return TRUSTY_ERR_NONE;
}

/*
 * Executes the RPMB request at @r, sends response to storage service.
 *
 * @chan:    proxy ipc channel
 * @msg:     address of storage message header
 * @r:       address of storage message request
 * @req_len: length of resp in bytes
 */
static int proxy_handle_rpmb(struct trusty_ipc_chan *chan,
                             struct storage_msg *msg, const void *r,
                             size_t req_len)
{
    int rc;
    size_t exp_len;
    const void *write_data = NULL;
    const void *rel_write_data = NULL;
    const struct storage_rpmb_send_req *req = r;

    if (req_len < sizeof(req)) {
        msg->result = STORAGE_ERR_NOT_VALID;
        goto err_response;
    }

    exp_len = sizeof(*req) + req->reliable_write_size + req->write_size;
    if (req_len != exp_len) {
        trusty_error(
            "%s: malformed rpmb request: invalid length (%zu != %zu)\n",
            __func__, req_len, exp_len);
        msg->result = STORAGE_ERR_NOT_VALID;
        goto err_response;
    }

    if (req->reliable_write_size) {
        if ((req->reliable_write_size % MMC_BLOCK_SIZE) != 0) {
            trusty_error("%s: invalid reliable write size %u\n", __func__,
                         req->reliable_write_size);
            msg->result = STORAGE_ERR_NOT_VALID;
            goto err_response;
        }
        rel_write_data = req->payload;
    }

    if (req->write_size) {
        if ((req->write_size % MMC_BLOCK_SIZE) != 0) {
            trusty_error("%s: invalid write size %u\n", __func__,
                         req->write_size);
            msg->result = STORAGE_ERR_NOT_VALID;
            goto err_response;
        }
        write_data = req->payload + req->reliable_write_size;
    }

    if (req->read_size) {
        if (req->read_size % MMC_BLOCK_SIZE != 0 ||
            req->read_size > sizeof(read_buf)) {
            trusty_error("%s: invalid read size %u\n", __func__,
                         req->read_size);
            msg->result = STORAGE_ERR_NOT_VALID;
            goto err_response;
        }
    }

    /* execute rpmb command */
    rc = rpmb_storage_send(proxy_rpmb,
                           rel_write_data, req->reliable_write_size,
                           write_data, req->write_size,
                           read_buf, req->read_size);
    if (rc) {
        trusty_error("%s: rpmb_storage_send failed: %d\n", __func__, rc);
        msg->result = STORAGE_ERR_GENERIC;
        goto err_response;
    }

    if (msg->flags & STORAGE_MSG_FLAG_POST_COMMIT) {
        /*
         * Nothing todo for post msg commit request as MMC_IOC_MULTI_CMD
         * is fully synchronous in this implementation.
         */
    }

    msg->result = STORAGE_NO_ERROR;
    return proxy_send_response(chan, msg, read_buf, req->read_size);

err_response:
    return proxy_send_response(chan, msg, NULL, 0);
}

/*
 * Handles storage request.
 *
 * @chan:    proxy ipc channel
 * @msg:     address of storage message header
 * @req:     address of storage message request
 * @req_len: length of resp in bytes
 */
static int proxy_handle_req(struct trusty_ipc_chan *chan,
                            struct storage_msg *msg, const void *req,
                            size_t req_len)
{
    int rc;

    if (msg->flags & STORAGE_MSG_FLAG_PRE_COMMIT) {
        /* nothing to do */
    }

    switch (msg->cmd) {
    case STORAGE_RPMB_SEND:
        rc = proxy_handle_rpmb(chan, msg, req, req_len);
        break;

    case STORAGE_FILE_DELETE:
    case STORAGE_FILE_OPEN:
    case STORAGE_FILE_CLOSE:
    case STORAGE_FILE_WRITE:
    case STORAGE_FILE_READ:
    case STORAGE_FILE_GET_SIZE:
    case STORAGE_FILE_SET_SIZE:
        /* Bulk filesystem is not supported */
        msg->result = STORAGE_ERR_UNIMPLEMENTED;
        rc = proxy_send_response(chan, msg, NULL, 0);
        break;

    default:
        msg->result = STORAGE_ERR_UNIMPLEMENTED;
        rc = proxy_send_response(chan, msg, NULL, 0);
    }

    return rc;
}

/*
 * Invalidates @chan on hangup event
 *
 * @chan:    proxy ipc channel
 */
static int proxy_on_disconnect(struct trusty_ipc_chan *chan)
{
    trusty_assert(chan);

    trusty_debug("%s: closed by peer\n", __func__);
    chan->handle = INVALID_IPC_HANDLE;
    return TRUSTY_EVENT_HANDLED;
}

/*
 * Handles received storage message on message event
 *
 * @chan:    proxy ipc channel
 */
static int proxy_on_message(struct trusty_ipc_chan *chan)
{
    int rc;

    trusty_assert(chan);

    /* read request */
    rc = proxy_read_request(chan, &req_msg, req_buf, sizeof(req_buf));
    if (rc < 0) {
        trusty_error("%s: failed (%d) to read request\n", __func__, rc);
        trusty_ipc_close(chan);
        return rc;
    }

    /**
     * The response of proxy will also be routed to here but we should
     * not handle them, just return and set "proxy_resp_flag" to indicate
     * func trusty_ipc_dev_recv() not try to receive the msg again.
     */
    if (req_msg.cmd & STORAGE_RESP_BIT) {
        chan->complete = 1;
        proxy_resp_flag = 1;
        req_len = rc;
        return TRUSTY_EVENT_HANDLED;
    }

    /* handle it and send reply */
    rc = proxy_handle_req(chan, &req_msg, req_buf, rc);
    if (rc < 0) {
        trusty_error("%s: failed (%d) to handle request\n", __func__, rc);
        trusty_ipc_close(chan);
        return rc;
    }

    return TRUSTY_EVENT_HANDLED;
}

static struct trusty_ipc_ops proxy_ops = {
    .on_message = proxy_on_message,
    .on_disconnect = proxy_on_disconnect,
};

/*
 * Initialize RPMB storage proxy
 */
int rpmb_storage_proxy_init(struct trusty_ipc_dev *dev, void *rpmb_dev)
{
    int rc;

    trusty_assert(dev);
    trusty_assert(!initialized);

    /* attach rpmb device  */
    proxy_rpmb = rpmb_dev;

    /* init ipc channel */
    trusty_ipc_chan_init(&proxy_chan, dev);

    /* connect to proxy service and wait for connect to complete */
    rc = trusty_ipc_connect(&proxy_chan, STORAGE_DISK_PROXY_PORT, true);
    if (rc < 0) {
        trusty_error("%s: failed (%d) to connect to '%s'\n", __func__, rc,
                     STORAGE_DISK_PROXY_PORT);
        return rc;
    }

    /* override default ops */
    proxy_chan.ops = &proxy_ops;

    do {
        /* Check for RPMB events */
        rc = trusty_ipc_poll_for_event(proxy_chan.dev);
        if (rc < 0) {
            trusty_error("%s: failed (%d) to get rpmb event\n", __func__, rc);
            return rc;
        }

        if (proxy_chan.handle == INVALID_IPC_HANDLE) {
            trusty_error("%s: unexpected proxy channel close\n", __func__);
            return TRUSTY_ERR_CHANNEL_CLOSED;
        }
    }
    while (rc != TRUSTY_EVENT_NONE);

    /* mark as initialized */
    initialized = true;

    return TRUSTY_ERR_NONE;
}

void rpmb_storage_proxy_shutdown(struct trusty_ipc_dev *dev)
{
    trusty_assert(initialized);

    /* close channel */
    trusty_ipc_close(&proxy_chan);

    initialized = false;
}

int storage_set_rpmb_key(void)
{
    uint32_t size = 0;
    return storage_do_tipc(STORAGE_RPMB_KEY_SET, NULL, 0, NULL, &size);
}

int storage_erase_rpmb(void)
{
    uint32_t size = 0;
    return storage_do_tipc(STORAGE_RPMB_ERASE_ALL, NULL, 0, NULL, &size);
}