1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <interface/storage/storage.h>
26 #include <trusty/rpmb.h>
27 #include <trusty/trusty_ipc.h>
28 #include <trusty/util.h>
29
30 #define LOCAL_LOG 0
31
32 static bool initialized;
33 /* Address of rpmb device */
34 static void* proxy_rpmb;
35 struct trusty_ipc_chan proxy_chan;
36
37 struct storage_msg req_msg;
38 static uint8_t req_buf[4096];
39 static uint8_t read_buf[4096];
40
41 /*
42 * Read RPMB request from storage service. Writes message to @msg
43 * and @req.
44 *
45 * @chan: proxy ipc channel
46 * @msg: address of storage message header
47 * @req: address of storage message request
48 * @req_len: length of req in bytes
49 */
proxy_read_request(struct trusty_ipc_chan * chan,struct storage_msg * msg,void * req,size_t req_len)50 static int proxy_read_request(struct trusty_ipc_chan* chan,
51 struct storage_msg* msg,
52 void* req,
53 size_t req_len) {
54 int rc;
55
56 struct trusty_ipc_iovec req_iovs[2] = {
57 {.base = msg, .len = sizeof(*msg)},
58 {.base = req, .len = req_len},
59 };
60 rc = trusty_ipc_recv(chan, req_iovs, 2, false);
61 if (rc < 0) {
62 /* recv message failed */
63 trusty_error("%s: failed (%d) to recv request\n", __func__, rc);
64 return rc;
65 }
66
67 if ((size_t)rc < sizeof(*msg)) {
68 /* malformed message */
69 trusty_error("%s: malformed request (%zu)\n", __func__, (size_t)rc);
70 return TRUSTY_ERR_GENERIC;
71 }
72
73 return rc - sizeof(*msg); /* return payload size */
74 }
75
76 /*
77 * Send RPMB response to storage service
78 *
79 * @chan: proxy ipc channel
80 * @msg: address of storage message header
81 * @resp: address of storage message response
82 * @resp_len: length of resp in bytes
83 */
proxy_send_response(struct trusty_ipc_chan * chan,struct storage_msg * msg,void * resp,size_t resp_len)84 static int proxy_send_response(struct trusty_ipc_chan* chan,
85 struct storage_msg* msg,
86 void* resp,
87 size_t resp_len) {
88 struct trusty_ipc_iovec resp_iovs[2] = {{.base = msg, .len = sizeof(*msg)},
89 {.base = resp, .len = resp_len}};
90
91 msg->cmd |= STORAGE_RESP_BIT;
92 return trusty_ipc_send(chan, resp_iovs, resp ? 2 : 1, false);
93 }
94
95 /*
96 * Executes the RPMB request at @r, sends response to storage service.
97 *
98 * @chan: proxy ipc channel
99 * @msg: address of storage message header
100 * @r: address of storage message request
101 * @req_len: length of resp in bytes
102 */
proxy_handle_rpmb(struct trusty_ipc_chan * chan,struct storage_msg * msg,const void * r,size_t req_len)103 static int proxy_handle_rpmb(struct trusty_ipc_chan* chan,
104 struct storage_msg* msg,
105 const void* r,
106 size_t req_len) {
107 int rc;
108 size_t exp_len;
109 const void* write_data = NULL;
110 const void* rel_write_data = NULL;
111 const struct storage_rpmb_send_req* req = r;
112
113 if (req_len < sizeof(req)) {
114 msg->result = STORAGE_ERR_NOT_VALID;
115 goto err_response;
116 }
117
118 exp_len = sizeof(*req) + req->reliable_write_size + req->write_size;
119 if (req_len != exp_len) {
120 trusty_error(
121 "%s: malformed rpmb request: invalid length (%zu != %zu)\n",
122 __func__, req_len, exp_len);
123 msg->result = STORAGE_ERR_NOT_VALID;
124 goto err_response;
125 }
126
127 if (req->reliable_write_size) {
128 if ((req->reliable_write_size % MMC_BLOCK_SIZE) != 0) {
129 trusty_error("%s: invalid reliable write size %u\n", __func__,
130 req->reliable_write_size);
131 msg->result = STORAGE_ERR_NOT_VALID;
132 goto err_response;
133 }
134 rel_write_data = req->payload;
135 }
136
137 if (req->write_size) {
138 if ((req->write_size % MMC_BLOCK_SIZE) != 0) {
139 trusty_error("%s: invalid write size %u\n", __func__,
140 req->write_size);
141 msg->result = STORAGE_ERR_NOT_VALID;
142 goto err_response;
143 }
144 write_data = req->payload + req->reliable_write_size;
145 }
146
147 if (req->read_size) {
148 if (req->read_size % MMC_BLOCK_SIZE != 0 ||
149 req->read_size > sizeof(read_buf)) {
150 trusty_error("%s: invalid read size %u\n", __func__,
151 req->read_size);
152 msg->result = STORAGE_ERR_NOT_VALID;
153 goto err_response;
154 }
155 }
156
157 /* execute rpmb command */
158 rc = rpmb_storage_send(proxy_rpmb, rel_write_data, req->reliable_write_size,
159 write_data, req->write_size, read_buf,
160 req->read_size);
161 if (rc) {
162 trusty_error("%s: rpmb_storage_send failed: %d\n", __func__, rc);
163 msg->result = STORAGE_ERR_GENERIC;
164 goto err_response;
165 }
166
167 if (msg->flags & STORAGE_MSG_FLAG_POST_COMMIT) {
168 /*
169 * Nothing todo for post msg commit request as MMC_IOC_MULTI_CMD
170 * is fully synchronous in this implementation.
171 */
172 }
173
174 msg->result = STORAGE_NO_ERROR;
175 return proxy_send_response(chan, msg, read_buf, req->read_size);
176
177 err_response:
178 return proxy_send_response(chan, msg, NULL, 0);
179 }
180
181 /*
182 * Handles storage request.
183 *
184 * @chan: proxy ipc channel
185 * @msg: address of storage message header
186 * @req: address of storage message request
187 * @req_len: length of resp in bytes
188 */
proxy_handle_req(struct trusty_ipc_chan * chan,struct storage_msg * msg,const void * req,size_t req_len)189 static int proxy_handle_req(struct trusty_ipc_chan* chan,
190 struct storage_msg* msg,
191 const void* req,
192 size_t req_len) {
193 int rc;
194
195 if (msg->flags & STORAGE_MSG_FLAG_PRE_COMMIT) {
196 /* nothing to do */
197 }
198
199 switch (msg->cmd) {
200 case STORAGE_RPMB_SEND:
201 rc = proxy_handle_rpmb(chan, msg, req, req_len);
202 break;
203
204 case STORAGE_FILE_DELETE:
205 case STORAGE_FILE_OPEN:
206 case STORAGE_FILE_CLOSE:
207 case STORAGE_FILE_WRITE:
208 case STORAGE_FILE_READ:
209 case STORAGE_FILE_GET_SIZE:
210 case STORAGE_FILE_SET_SIZE:
211 /* Bulk filesystem is not supported */
212 msg->result = STORAGE_ERR_UNIMPLEMENTED;
213 rc = proxy_send_response(chan, msg, NULL, 0);
214 break;
215
216 default:
217 msg->result = STORAGE_ERR_UNIMPLEMENTED;
218 rc = proxy_send_response(chan, msg, NULL, 0);
219 }
220
221 return rc;
222 }
223
224 /*
225 * Invalidates @chan on hangup event
226 *
227 * @chan: proxy ipc channel
228 */
proxy_on_disconnect(struct trusty_ipc_chan * chan)229 static int proxy_on_disconnect(struct trusty_ipc_chan* chan) {
230 trusty_assert(chan);
231
232 trusty_debug("%s: closed by peer\n", __func__);
233 chan->handle = INVALID_IPC_HANDLE;
234 return TRUSTY_EVENT_HANDLED;
235 }
236
237 /*
238 * Handles received storage message on message event
239 *
240 * @chan: proxy ipc channel
241 */
proxy_on_message(struct trusty_ipc_chan * chan)242 static int proxy_on_message(struct trusty_ipc_chan* chan) {
243 int rc;
244
245 trusty_assert(chan);
246
247 /* read request */
248 rc = proxy_read_request(chan, &req_msg, req_buf, sizeof(req_buf));
249 if (rc < 0) {
250 trusty_error("%s: failed (%d) to read request\n", __func__, rc);
251 trusty_ipc_close(chan);
252 return rc;
253 }
254
255 /* handle it and send reply */
256 rc = proxy_handle_req(chan, &req_msg, req_buf, rc);
257 if (rc < 0) {
258 trusty_error("%s: failed (%d) to handle request\n", __func__, rc);
259 trusty_ipc_close(chan);
260 return rc;
261 }
262
263 return TRUSTY_EVENT_HANDLED;
264 }
265
266 static struct trusty_ipc_ops proxy_ops = {
267 .on_message = proxy_on_message,
268 .on_disconnect = proxy_on_disconnect,
269 };
270
271 /*
272 * Initialize RPMB storage proxy
273 */
rpmb_storage_proxy_init(struct trusty_ipc_dev * dev,void * rpmb_dev)274 int rpmb_storage_proxy_init(struct trusty_ipc_dev* dev, void* rpmb_dev) {
275 int rc;
276
277 trusty_assert(dev);
278 trusty_assert(!initialized);
279
280 /* attach rpmb device */
281 proxy_rpmb = rpmb_dev;
282
283 /* init ipc channel */
284 trusty_ipc_chan_init(&proxy_chan, dev);
285
286 /* connect to proxy service and wait for connect to complete */
287 rc = trusty_ipc_connect(&proxy_chan, STORAGE_DISK_PROXY_PORT, true);
288 if (rc < 0) {
289 trusty_error("%s: failed (%d) to connect to '%s'\n", __func__, rc,
290 STORAGE_DISK_PROXY_PORT);
291 return rc;
292 }
293
294 /* override default ops */
295 proxy_chan.ops = &proxy_ops;
296
297 do {
298 /* Check for RPMB events */
299 rc = trusty_ipc_poll_for_event(proxy_chan.dev);
300 if (rc < 0) {
301 trusty_error("%s: failed (%d) to get rpmb event\n", __func__, rc);
302 return rc;
303 }
304
305 if (proxy_chan.handle == INVALID_IPC_HANDLE) {
306 trusty_error("%s: unexpected proxy channel close\n", __func__);
307 return TRUSTY_ERR_CHANNEL_CLOSED;
308 }
309 } while (rc != TRUSTY_EVENT_NONE);
310
311 /* mark as initialized */
312 initialized = true;
313
314 return TRUSTY_ERR_NONE;
315 }
316
rpmb_storage_proxy_shutdown(struct trusty_ipc_dev * dev)317 void rpmb_storage_proxy_shutdown(struct trusty_ipc_dev* dev) {
318 trusty_assert(initialized);
319
320 /* close channel */
321 trusty_ipc_close(&proxy_chan);
322
323 initialized = false;
324 }
325