1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "rpmb_mock"
26 
27 #include "rpmb_protocol.h"
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <log/log.h>
34 #include <openssl/hmac.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43 
44 /* verbose is an int for getopt */
45 static int verbose = false;
46 
47 #if OPENSSL_VERSION_NUMBER < 0x10100000L
48 
HMAC_CTX_new(void)49 HMAC_CTX* HMAC_CTX_new(void) {
50     HMAC_CTX* ctx = malloc(sizeof(*ctx));
51     if (ctx != NULL) {
52         HMAC_CTX_init(ctx);
53     }
54     return ctx;
55 }
56 
HMAC_CTX_free(HMAC_CTX * ctx)57 void HMAC_CTX_free(HMAC_CTX* ctx) {
58     if (ctx != NULL) {
59         HMAC_CTX_cleanup(ctx);
60         free(ctx);
61     }
62 }
63 
64 #endif
65 
66 #define MAX_WRITE_COUNTER (0xffffffff)
67 
68 struct rpmb_data_header {
69     uint32_t write_counter;
70     uint16_t max_block;
71     uint8_t pad1;
72     uint8_t key_programmed;
73     struct rpmb_key key;
74     uint8_t pad[512 - 4 - 2 - 1 - 1 - sizeof(struct rpmb_key)];
75 };
76 
77 #define MAX_PACKET_COUNT (8)
78 
79 struct rpmb_dev_state {
80     struct rpmb_data_header header;
81     struct rpmb_packet cmd[MAX_PACKET_COUNT];
82     struct rpmb_packet res[MAX_PACKET_COUNT];
83     uint16_t cmd_count;
84     uint16_t res_count;
85     int data_fd;
86 };
87 
88 /* TODO: move to common location */
rpmb_mac(struct rpmb_key key,struct rpmb_packet * packet,size_t packet_count,struct rpmb_key * mac)89 static int rpmb_mac(struct rpmb_key key, struct rpmb_packet* packet, size_t packet_count,
90                     struct rpmb_key* mac) {
91     size_t i;
92     int hmac_ret;
93     unsigned int md_len;
94     HMAC_CTX* hmac_ctx;
95 
96     hmac_ctx = HMAC_CTX_new();
97     hmac_ret = HMAC_Init_ex(hmac_ctx, &key, sizeof(key), EVP_sha256(), NULL);
98     if (!hmac_ret) {
99         ALOGE("HMAC_Init_ex failed\n");
100         goto err;
101     }
102     for (i = 0; i < packet_count; i++) {
103         hmac_ret = HMAC_Update(hmac_ctx, packet[i].data, 284);
104         if (!hmac_ret) {
105             ALOGE("HMAC_Update failed\n");
106             goto err;
107         }
108     }
109     hmac_ret = HMAC_Final(hmac_ctx, mac->byte, &md_len);
110     if (md_len != sizeof(mac->byte)) {
111         ALOGE("bad md_len %d != %zd\n", md_len, sizeof(mac->byte));
112         exit(1);
113     }
114     if (!hmac_ret) {
115         ALOGE("HMAC_Final failed\n");
116         goto err;
117     }
118 
119 err:
120     HMAC_CTX_free(hmac_ctx);
121     return hmac_ret ? 0 : -1;
122 }
123 
rpmb_file_seek(struct rpmb_dev_state * s,uint16_t addr)124 static int rpmb_file_seek(struct rpmb_dev_state* s, uint16_t addr) {
125     int ret;
126     int pos = addr * RPMB_PACKET_DATA_SIZE + sizeof(s->header);
127     ret = lseek(s->data_fd, pos, SEEK_SET);
128     if (ret != pos) {
129         ALOGE("rpmb_dev: seek to %d failed, got %d\n", pos, ret);
130         return -1;
131     }
132     return 0;
133 }
134 
rpmb_dev_program_key(struct rpmb_dev_state * s)135 static uint16_t rpmb_dev_program_key(struct rpmb_dev_state* s) {
136     int ret;
137 
138     if (s->header.key_programmed) {
139         return RPMB_RES_WRITE_FAILURE;
140     }
141 
142     s->header.key = s->cmd[0].key_mac;
143     s->header.key_programmed = 1;
144 
145     ret = lseek(s->data_fd, 0, SEEK_SET);
146     if (ret) {
147         ALOGE("rpmb_dev: Failed to seek rpmb data file\n");
148         return RPMB_RES_WRITE_FAILURE;
149     }
150 
151     ret = write(s->data_fd, &s->header, sizeof(s->header));
152     if (ret != sizeof(s->header)) {
153         ALOGE("rpmb_dev: Failed to write rpmb key: %d, %s\n", ret, strerror(errno));
154 
155         return RPMB_RES_WRITE_FAILURE;
156     }
157 
158     return RPMB_RES_OK;
159 }
160 
rpmb_dev_get_counter(struct rpmb_dev_state * s)161 static uint16_t rpmb_dev_get_counter(struct rpmb_dev_state* s) {
162     s->res[0].write_counter = rpmb_u32(s->header.write_counter);
163 
164     return RPMB_RES_OK;
165 }
166 
rpmb_dev_data_write(struct rpmb_dev_state * s)167 static uint16_t rpmb_dev_data_write(struct rpmb_dev_state* s) {
168     uint16_t addr = rpmb_get_u16(s->cmd[0].address);
169     uint16_t block_count = s->cmd_count;
170     uint32_t write_counter;
171     int ret;
172 
173     if (s->header.write_counter == MAX_WRITE_COUNTER) {
174         if (verbose) {
175             ALOGE("rpmb_dev: Write counter expired\n");
176         }
177         return RPMB_RES_WRITE_FAILURE;
178     }
179 
180     write_counter = rpmb_get_u32(s->cmd[0].write_counter);
181     if (s->header.write_counter != write_counter) {
182         if (verbose) {
183             ALOGE("rpmb_dev: Invalid write counter %u. Expected: %u\n", write_counter,
184                   s->header.write_counter);
185         }
186         return RPMB_RES_COUNT_FAILURE;
187     }
188 
189     ret = rpmb_file_seek(s, addr);
190     if (ret) {
191         ALOGE("rpmb_dev: Failed to seek rpmb data file\n");
192         return RPMB_RES_WRITE_FAILURE;
193     }
194 
195     for (int i = 0; i < block_count; i++) {
196         ret = write(s->data_fd, s->cmd[i].data, RPMB_PACKET_DATA_SIZE);
197         if (ret != RPMB_PACKET_DATA_SIZE) {
198             ALOGE("rpmb_dev: Failed to write rpmb data file: %d, %s\n", ret, strerror(errno));
199             return RPMB_RES_WRITE_FAILURE;
200         }
201     }
202 
203     s->header.write_counter++;
204 
205     ret = lseek(s->data_fd, 0, SEEK_SET);
206     if (ret) {
207         ALOGE("rpmb_dev: Failed to seek rpmb data file\n");
208         return RPMB_RES_WRITE_FAILURE;
209     }
210 
211     ret = write(s->data_fd, &s->header.write_counter, sizeof(s->header.write_counter));
212     if (ret != sizeof(s->header.write_counter)) {
213         ALOGE("rpmb_dev: Failed to write rpmb write counter: %d, %s\n", ret, strerror(errno));
214 
215         return RPMB_RES_WRITE_FAILURE;
216     }
217 
218     s->res[0].write_counter = rpmb_u32(s->header.write_counter);
219     return RPMB_RES_OK;
220 }
221 
rpmb_dev_data_read(struct rpmb_dev_state * s)222 static uint16_t rpmb_dev_data_read(struct rpmb_dev_state* s) {
223     uint16_t addr;
224     uint16_t block_count;
225     int ret;
226 
227     addr = rpmb_get_u16(s->cmd[0].address);
228     block_count = s->res_count;
229 
230     rpmb_file_seek(s, addr);
231 
232     for (int i = 0; i < block_count; i++) {
233         ret = read(s->data_fd, s->res[i].data, RPMB_PACKET_DATA_SIZE);
234         if (ret != 0 && ret != RPMB_PACKET_DATA_SIZE) {
235             ALOGE("rpmb_dev: Failed to read rpmb data file: %d, %s\n", ret, strerror(errno));
236             return RPMB_RES_READ_FAILURE;
237         }
238     }
239 
240     return RPMB_RES_OK;
241 }
242 
243 struct rpmb_dev_cmd {
244     uint16_t (*func)(struct rpmb_dev_state* s);
245     uint16_t resp;
246     bool key_mac_is_key;
247     bool check_mac;
248     bool check_result_read;
249     bool check_key_programmed;
250     bool check_addr;
251     bool multi_packet_cmd;
252     bool multi_packet_res;
253     bool res_mac;
254 };
255 
256 static struct rpmb_dev_cmd rpmb_dev_cmd_table[] = {
257         [RPMB_REQ_PROGRAM_KEY] =
258                 {
259                         .func = rpmb_dev_program_key,
260                         .resp = RPMB_RESP_PROGRAM_KEY,
261                         .key_mac_is_key = true,
262                         .check_result_read = true,
263                 },
264         [RPMB_REQ_GET_COUNTER] =
265                 {
266                         .func = rpmb_dev_get_counter,
267                         .resp = RPMB_RESP_GET_COUNTER,
268                         .check_key_programmed = true,
269                         .res_mac = true,
270                 },
271         [RPMB_REQ_DATA_WRITE] =
272                 {
273                         .func = rpmb_dev_data_write,
274                         .resp = RPMB_RESP_DATA_WRITE,
275                         .check_mac = true,
276                         .check_result_read = true,
277                         .check_key_programmed = true,
278                         .check_addr = true,
279                         .multi_packet_cmd = true,
280                         .res_mac = true,
281                 },
282         [RPMB_REQ_DATA_READ] =
283                 {
284                         .func = rpmb_dev_data_read,
285                         .resp = RPMB_RESP_DATA_READ,
286                         .check_addr = true,
287                         .multi_packet_res = true,
288                         .res_mac = true,
289                 },
290 };
291 
292 #define countof(arr) (sizeof(arr) / sizeof(arr[0]))
293 
rpmb_dev_process_cmd(struct rpmb_dev_state * s)294 static void rpmb_dev_process_cmd(struct rpmb_dev_state* s) {
295     assert(s->cmd_count > 0);
296     assert(s->res_count > 0);
297     uint16_t req_resp = rpmb_get_u16(s->cmd[0].req_resp);
298     uint16_t addr = rpmb_get_u16(s->cmd[0].address);
299     uint16_t sub_req;
300     uint16_t cmd_index = req_resp < countof(rpmb_dev_cmd_table) ? req_resp : 0;
301     struct rpmb_dev_cmd* cmd = &rpmb_dev_cmd_table[cmd_index];
302     uint16_t result = RPMB_RES_GENERAL_FAILURE;
303     struct rpmb_key mac;
304     uint16_t block_count = 0;
305 
306     if (cmd->check_result_read) {
307         sub_req = rpmb_get_u16(s->cmd[s->cmd_count - 1].req_resp);
308         if (sub_req != RPMB_REQ_RESULT_READ) {
309             if (verbose) {
310                 ALOGE("rpmb_dev: Request %d, missing result read request, got %d, cmd_count %d\n",
311                       req_resp, sub_req, s->cmd_count);
312             }
313             goto err;
314         }
315         assert(s->cmd_count > 1);
316         s->cmd_count--;
317     }
318 
319     if (cmd->check_mac) {
320         if (rpmb_mac(s->header.key, s->cmd, s->cmd_count, &mac) != 0) {
321             ALOGE("rpmb_dev: failed to caclulate mac\n");
322             goto err;
323         }
324     } else if (cmd->key_mac_is_key) {
325         mac = s->cmd[s->cmd_count - 1].key_mac;
326     } else {
327         memset(mac.byte, 0, sizeof(mac.byte));
328     }
329 
330     if (memcmp(&mac, s->cmd[s->cmd_count - 1].key_mac.byte, sizeof(mac))) {
331         if (verbose) {
332             ALOGE("rpmb_dev: Request %d, invalid MAC, cmd_count %d\n", req_resp, s->cmd_count);
333         }
334         if (cmd->check_mac) {
335             result = RPMB_RES_AUTH_FAILURE;
336         }
337         goto err;
338     }
339 
340     if (cmd->multi_packet_cmd) {
341         block_count = s->cmd_count;
342     }
343     if (cmd->multi_packet_res) {
344         block_count = s->res_count;
345     }
346 
347     if (cmd->check_addr && (addr + block_count > s->header.max_block + 1)) {
348         if (verbose) {
349             ALOGE("rpmb_dev: Request %d, invalid addr: 0x%x count 0x%x, Out of bounds. Max addr "
350                   "0x%x\n",
351                   req_resp, addr, block_count, s->header.max_block + 1);
352         }
353         result = RPMB_RES_ADDR_FAILURE;
354         goto err;
355     }
356     if (!cmd->check_addr && addr) {
357         if (verbose) {
358             ALOGE("rpmb_dev: Request %d, invalid addr: 0x%x != 0\n", req_resp, addr);
359         }
360         goto err;
361     }
362 
363     for (int i = 1; i < s->cmd_count; i++) {
364         sub_req = rpmb_get_u16(s->cmd[i].req_resp);
365         if (sub_req != req_resp) {
366             if (verbose) {
367                 ALOGE("rpmb_dev: Request %d, sub-request mismatch, %d, at %d\n", req_resp, i,
368                       sub_req);
369             }
370             goto err;
371         }
372     }
373     if (!cmd->multi_packet_cmd && s->cmd_count != 1) {
374         if (verbose) {
375             ALOGE("rpmb_dev: Request %d, bad cmd count %d, expected 1\n", req_resp, s->cmd_count);
376         }
377         goto err;
378     }
379     if (!cmd->multi_packet_res && s->res_count != 1) {
380         if (verbose) {
381             ALOGE("rpmb_dev: Request %d, bad res count %d, expected 1\n", req_resp, s->res_count);
382         }
383         goto err;
384     }
385 
386     if (cmd->check_key_programmed && !s->header.key_programmed) {
387         if (verbose) {
388             ALOGE("rpmb_dev: Request %d, key is not programmed\n", req_resp);
389         }
390         s->res[0].result = rpmb_u16(RPMB_RES_NO_AUTH_KEY);
391         return;
392     }
393 
394     if (!cmd->func) {
395         if (verbose) {
396             ALOGE("rpmb_dev: Unsupported request: %d\n", req_resp);
397         }
398         goto err;
399     }
400 
401     result = cmd->func(s);
402 
403 err:
404     if (s->header.write_counter == MAX_WRITE_COUNTER) {
405         result |= RPMB_RES_WRITE_COUNTER_EXPIRED;
406     }
407 
408     for (int i = 0; i < s->res_count; i++) {
409         s->res[i].nonce = s->cmd[0].nonce;
410         s->res[i].address = rpmb_u16(addr);
411         s->res[i].block_count = rpmb_u16(block_count);
412         s->res[i].result = rpmb_u16(result);
413         s->res[i].req_resp = rpmb_u16(cmd->resp);
414     }
415     if (cmd->res_mac) {
416         rpmb_mac(s->header.key, s->res, s->res_count, &s->res[s->res_count - 1].key_mac);
417     }
418 }
419 
420 /*
421  * Receives data until one of the following is true:
422  * - The buffer is full (return will be len)
423  * - The connection closed (return > 0, < len)
424  * - An error occurred (return will be the negative error code from recv)
425  */
recv_until(int sock,void * dest_in,size_t len)426 ssize_t recv_until(int sock, void* dest_in, size_t len) {
427     size_t bytes_recvd = 0;
428     char* dest = dest_in;
429     while (bytes_recvd < len) {
430         ssize_t ret = recv(sock, dest, len - bytes_recvd, 0);
431         if (ret < 0) {
432             return ret;
433         }
434         dest += ret;
435         bytes_recvd += ret;
436         if (ret == 0) {
437             break;
438         }
439     }
440     return bytes_recvd;
441 }
442 
443 /*
444  * Handles an incoming connection to the rpmb daemon.
445  * Returns 0 if the client disconnects without violating the protocol.
446  * Returns a negative value if we terminated the connection abnormally.
447  *
448  * Arguments:
449  *   conn_sock - an fd to send/recv on
450  *   s - an initialized rpmb device
451  */
handle_conn(struct rpmb_dev_state * s,int conn_sock)452 int handle_conn(struct rpmb_dev_state* s, int conn_sock) {
453     int ret;
454 
455     while (true) {
456         memset(s->res, 0, sizeof(s->res));
457         ret = recv_until(conn_sock, &s->res_count, sizeof(s->res_count));
458 
459         /*
460          * Disconnected while not in the middle of anything.
461          */
462         if (ret <= 0) {
463             return 0;
464         }
465 
466         if (s->res_count > MAX_PACKET_COUNT) {
467             ALOGE("rpmb_dev: Receive count too large: %d\n", s->res_count);
468             return -1;
469         }
470         if (s->res_count <= 0) {
471             ALOGE("rpmb_dev: Receive count too small: %d\n", s->res_count);
472             return -1;
473         }
474 
475         ret = recv_until(conn_sock, &s->cmd_count, sizeof(s->cmd_count));
476         if (ret != sizeof(s->cmd_count)) {
477             ALOGE("rpmb_dev: Failed to read cmd_count");
478             return -1;
479         }
480 
481         if (s->cmd_count == 0) {
482             ALOGE("rpmb_dev: Must contain at least one command\n");
483             return -1;
484         }
485 
486         if (s->cmd_count > MAX_PACKET_COUNT) {
487             ALOGE("rpmb_dev: Command count is too large\n");
488             return -1;
489         }
490 
491         size_t cmd_size = s->cmd_count * sizeof(s->cmd[0]);
492         ret = recv_until(conn_sock, s->cmd, cmd_size);
493         if (ret != (int)cmd_size) {
494             ALOGE("rpmb_dev: Failed to read command: "
495                   "cmd_size: %zu ret: %d, %s\n",
496                   cmd_size, ret, strerror(errno));
497             return -1;
498         }
499 
500         rpmb_dev_process_cmd(s);
501 
502         size_t resp_size = sizeof(s->res[0]) * s->res_count;
503         ret = send(conn_sock, s->res, resp_size, 0);
504         if (ret != (int)resp_size) {
505             ALOGE("rpmb_dev: Failed to send response: %d, %s\n", ret, strerror(errno));
506             return -1;
507         }
508     }
509 }
510 
usage(const char * argv0)511 void usage(const char* argv0) {
512     fprintf(stderr, "Usage: %s [-d|--dev] <datafile> [--sock] <socket_path>\n", argv0);
513     fprintf(stderr, "or:    %s [-d|--dev] <datafile> [--size <size>] [--key key]\n", argv0);
514 }
515 
main(int argc,char ** argv)516 int main(int argc, char** argv) {
517     struct rpmb_dev_state s;
518     int ret;
519     int cmdres_sock;
520     struct sockaddr_un cmdres_sockaddr;
521     const char* data_file_name = NULL;
522     const char* socket_path = NULL;
523     int open_flags;
524     int init = false;
525 
526     struct option long_options[] = {{"size", required_argument, 0, 0},
527                                     {"key", required_argument, 0, 0},
528                                     {"sock", required_argument, 0, 0},
529                                     {"dev", required_argument, 0, 'd'},
530                                     {"init", no_argument, &init, true},
531                                     {"verbose", no_argument, &verbose, true},
532                                     {0, 0, 0, 0}};
533 
534     memset(&s.header, 0, sizeof(s.header));
535 
536     while (1) {
537         int c;
538         int option_index = 0;
539         c = getopt_long(argc, argv, "d:", long_options, &option_index);
540         if (c == -1) {
541             break;
542         }
543 
544         switch (c) {
545             /* long args */
546             case 0:
547                 switch (option_index) {
548                     /* size */
549                     case 0:
550                         s.header.max_block = atoi(optarg) - 1;
551                         break;
552                     /* key */
553                     case 1:
554                         for (size_t i = 0; i < sizeof(s.header.key.byte); i++) {
555                             if (!optarg) {
556                                 break;
557                             }
558                             s.header.key.byte[i] = strtol(optarg, &optarg, 16);
559                             s.header.key_programmed = 1;
560                         }
561                         break;
562                     /* sock */
563                     case 2:
564                         socket_path = optarg;
565                         break;
566                 }
567                 break;
568             /* dev */
569             case 'd':
570                 data_file_name = optarg;
571                 break;
572             default:
573                 usage(argv[0]);
574                 return EXIT_FAILURE;
575         }
576     }
577 
578     /*
579      * We always need a data file, and at exactly one of --init or --sock
580      * must be specified.
581      */
582     if (!data_file_name || (!init == !socket_path)) {
583         usage(argv[0]);
584         return EXIT_FAILURE;
585     }
586 
587     /*
588      * If the file is already initialized, exit early.
589      */
590     if (init && !access(data_file_name, F_OK)) {
591         return EXIT_SUCCESS;
592     }
593 
594     open_flags = O_RDWR | O_SYNC;
595     if (init) {
596         open_flags |= O_CREAT | O_TRUNC;
597     }
598     s.data_fd = open(data_file_name, open_flags, S_IWUSR | S_IRUSR);
599     if (s.data_fd < 0) {
600         ALOGE("rpmb_dev: Failed to open rpmb data file, %s: %s\n", data_file_name, strerror(errno));
601         return EXIT_FAILURE;
602     }
603 
604     if (init) {
605         /* Create new rpmb data file */
606         if (s.header.max_block == 0) {
607             s.header.max_block = 512 - 1;
608         }
609         ret = write(s.data_fd, &s.header, sizeof(s.header));
610         if (ret != sizeof(s.header)) {
611             ALOGE("rpmb_dev: Failed to write rpmb data file: %d, %s\n", ret, strerror(errno));
612             return EXIT_FAILURE;
613         }
614         return EXIT_SUCCESS;
615     }
616 
617     ret = read(s.data_fd, &s.header, sizeof(s.header));
618     if (ret != sizeof(s.header)) {
619         ALOGE("rpmb_dev: Failed to read rpmb data file: %d, %s\n", ret, strerror(errno));
620         return EXIT_FAILURE;
621     }
622 
623     cmdres_sock = socket(AF_UNIX, SOCK_STREAM, 0);
624     if (cmdres_sock < 0) {
625         ALOGE("rpmb_dev: Failed to create command/response socket: %s\n", strerror(errno));
626         return EXIT_FAILURE;
627     }
628 
629     cmdres_sockaddr.sun_family = AF_UNIX;
630     strncpy(cmdres_sockaddr.sun_path, socket_path, sizeof(cmdres_sockaddr.sun_path));
631 
632     ret = bind(cmdres_sock, (struct sockaddr*)&cmdres_sockaddr, sizeof(struct sockaddr_un));
633     if (ret < 0) {
634         ALOGE("rpmb_dev: Failed to bind command/response socket: %s: %s\n", socket_path,
635               strerror(errno));
636         return EXIT_FAILURE;
637     }
638 
639     ret = listen(cmdres_sock, 1);
640     if (ret < 0) {
641         ALOGE("rpmb_dev: Failed to listen on command/response socket: %s\n", strerror(errno));
642         return EXIT_FAILURE;
643     }
644 
645     while (true) {
646         int conn_sock = accept(cmdres_sock, NULL, NULL);
647         if (conn_sock < 0) {
648             ALOGE("rpmb_dev: Could not accept connection: %s\n", strerror(errno));
649             return EXIT_FAILURE;
650         }
651         ret = handle_conn(&s, conn_sock);
652         close(conn_sock);
653         if (ret) {
654             ALOGE("rpmb_dev: Connection terminated: %d", ret);
655         }
656     }
657 }
658