forked from trusty-ia/trusty_app_storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_tipc.c
1416 lines (1189 loc) · 38.3 KB
/
client_tipc.c
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013-2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "client_tipc.h"
#include <assert.h>
#include <errno.h>
#include <uapi/err.h>
#include <lk/list.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <trusty_std.h>
#include <openssl/mem.h>
#include <interface/storage/storage.h>
#include "client_session_tipc.h"
#include "file.h"
#include "ipc.h"
#include "session.h"
#include "tipc_limits.h"
#include "att_keybox.h"
#include "rpmb.h"
#include "trusty_key_crypt.h"
/* macros to help manage debug output */
#define SS_ERR(args...) fprintf(stderr, "ss: " args)
#define SS_DBG_IO(args...) do {} while(0)
#if 0
/* this can generate alot of spew on debug builds */
#define SS_INFO(args...) fprintf(stderr, "ss: " args)
#else
#define SS_INFO(args...) do {} while(0)
#endif
#define RPMB_BLOCK_SIZE 256
#define ATTKB_PRESENCE (1 << 0)
#define ATTKB_MAX_SIZE (16 * 1024)
#ifndef RETRIEVE_ATTKB_FROM_RAW_RPMB
#define RPMB_BLOCK_INFO_INDEX 1
#endif
static int client_handle_msg(struct ipc_channel_context *ctx, void *msg, size_t msg_size);
static void client_disconnect(struct ipc_channel_context *context);
static int send_response(struct storage_client_session *session,
enum storage_err result, struct storage_msg *msg,
void *out, size_t out_size);
/*
* Legal secure storage directory and file names contain only
* characters from the following set: [a-z][A-Z][0-9][.-_]
*
* It is not null terminated.
*/
static int is_valid_name(const char *name, size_t name_len)
{
size_t i;
if (!name_len)
return 0;
for (i = 0; i < name_len; i++) {
if ((name[i] >= 'a') && (name[i] <= 'z'))
continue;
if ((name[i] >= 'A') && (name[i] <= 'Z'))
continue;
if ((name[i] >= '0') && (name[i] <= '9'))
continue;
if ((name[i] == '.') || (name[i] == '-') || (name[i] == '_'))
continue;
/* not a legal character so reject this name */
return 0;
}
return 1;
}
static int get_path(char *path_out, size_t path_out_size,
const uuid_t *uuid,
const char *file_name, size_t file_name_len)
{
unsigned int rc;
rc = snprintf(path_out, path_out_size,
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x/",
uuid->time_low,
uuid->time_mid,
uuid->time_hi_and_version,
uuid->clock_seq_and_node[0],
uuid->clock_seq_and_node[1],
uuid->clock_seq_and_node[2],
uuid->clock_seq_and_node[3],
uuid->clock_seq_and_node[4],
uuid->clock_seq_and_node[5],
uuid->clock_seq_and_node[6],
uuid->clock_seq_and_node[7]);
if (rc + file_name_len >= path_out_size) {
return STORAGE_ERR_NOT_VALID;
}
memcpy(path_out + rc, file_name, file_name_len);
path_out[rc + file_name_len] = '\0';
return STORAGE_NO_ERROR;
}
static enum storage_err session_set_files_count(struct storage_client_session *session,
size_t files_count)
{
struct file_handle **files;
if (files_count > STORAGE_MAX_OPEN_FILES) {
SS_ERR("%s: too many open files\n", __func__);
return STORAGE_ERR_NOT_VALID;
}
files = realloc(session->files, sizeof(files[0]) * files_count);
if (!files) {
SS_ERR("%s: out of memory\n", __func__);
return STORAGE_ERR_GENERIC;
}
if (files_count > session->files_count)
memset(files + session->files_count, 0,
sizeof(files[0]) * (files_count - session->files_count));
session->files = files;
session->files_count = files_count;
SS_INFO("%s: new file table size, 0x%x\n", __func__, files_count);
return STORAGE_NO_ERROR;
}
static void session_shrink_files(struct storage_client_session *session)
{
uint32_t handle;
handle = session->files_count;
while (handle > 0 && !session->files[handle - 1])
handle--;
if (handle < session->files_count)
session_set_files_count(session, handle);
}
static void session_close_all_files(struct storage_client_session *session)
{
uint32_t f_handle;
struct file_handle *file;
for (f_handle = 0; f_handle < session->files_count; f_handle++) {
file = session->files[f_handle];
if (file) {
file_close(file);
free(file);
}
}
if (session->files) {
free(session->files);
}
session->files_count = 0;
}
static enum storage_err create_file_handle(struct storage_client_session *session,
uint32_t *handlep,
struct file_handle **file_p)
{
enum storage_err result;
uint32_t handle;
struct file_handle *file;
for (handle = 0; handle < session->files_count; handle++)
if (!session->files[handle])
break;
if (handle >= session->files_count) {
result = session_set_files_count(session, handle + 1);
if (result != STORAGE_NO_ERROR)
return result;
}
file = calloc(1, sizeof(*file));
if (!file) {
SS_ERR("%s: out of memory\n", __func__);
return STORAGE_ERR_GENERIC;
}
session->files[handle] = file;
SS_INFO("%s: created file handle 0x%x\n", __func__, handle);
*handlep = handle;
*file_p = file;
return STORAGE_NO_ERROR;
}
static void free_file_handle(struct storage_client_session *session, uint32_t handle)
{
if (handle >= session->files_count) {
SS_ERR("%s: invalid handle, 0x%x\n", __func__, handle);
return;
}
if (session->files[handle] == NULL) {
SS_ERR("%s: closed handle, 0x%x\n", __func__, handle);
return;
}
free(session->files[handle]);
session->files[handle] = NULL;
SS_INFO("%s: deleted file handle 0x%x\n", __func__, handle);
session_shrink_files(session);
}
static struct file_handle *get_file_handle(struct storage_client_session *session,
uint32_t handle)
{
struct file_handle *file;
if (handle >= session->files_count) {
SS_ERR("%s: invalid handle, 0x%x\n", __func__, handle);
return NULL;
}
file = session->files[handle];
if (!file) {
SS_ERR("%s: closed handle, 0x%x\n", __func__, handle);
return NULL;
}
return file;
}
static enum storage_err storage_file_delete(struct storage_msg *msg,
struct storage_file_delete_req *req, size_t req_size,
struct storage_client_session *session)
{
bool deleted;
enum storage_err result;
const char *fname;
size_t fname_len;
uint32_t flags;
char path_buf[FS_PATH_MAX];
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
return STORAGE_ERR_NOT_VALID;
}
flags = req->flags;
if ((flags & ~STORAGE_FILE_DELETE_MASK) != 0) {
SS_ERR("invalid delete flags 0x%x\n", flags);
return STORAGE_ERR_NOT_VALID;
}
/* make sure filename is legal */
fname = req->name;
fname_len = req_size - sizeof(*req);
if (!is_valid_name(fname, fname_len)) {
SS_ERR("%s: invalid filename\n", __func__);
return STORAGE_ERR_NOT_VALID;
}
result = get_path(path_buf, sizeof(path_buf), &session->uuid, fname, fname_len);
if (result != STORAGE_NO_ERROR) {
return result;
}
SS_INFO("%s: path %s\n", __func__, path_buf);
deleted = file_delete(&session->tr, path_buf);
if (session->tr.failed) {
SS_ERR("%s: transaction failed\n", __func__);
return STORAGE_ERR_GENERIC;
} else if (!deleted) {
return STORAGE_ERR_NOT_FOUND;
}
if (msg->flags & STORAGE_MSG_FLAG_TRANSACT_COMPLETE) {
transaction_complete(&session->tr);
if (session->tr.failed) {
SS_ERR("%s: transaction commit failed\n", __func__);
return STORAGE_ERR_GENERIC;
}
return STORAGE_NO_ERROR;
}
return STORAGE_NO_ERROR;
}
/**
* storage_file_check_name - Check if file handle matches path
* @tr: Transaction object.
* @file: File handle object.
* @path: Path to check.
*
* Return: %true if @file matches @path, %false otherwise.
*/
static bool storage_file_check_name(struct transaction *tr,
const struct file_handle *file,
const char *path)
{
bool ret;
const struct file_info *file_info;
obj_ref_t ref = OBJ_REF_INITIAL_VALUE(ref);
file_info = file_get_info(tr, &file->block_mac, &ref);
if (!file_info) {
printf("can't read file entry at %lld\n",
block_mac_to_block(tr, &file->block_mac));
return false;
}
assert(file_info);
ret = strcmp(file_info->path, path) == 0;
file_info_put(file_info, &ref);
return ret;
}
static enum storage_err storage_file_move(struct storage_msg *msg,
struct storage_file_move_req *req, size_t req_size,
struct storage_client_session *session)
{
bool moved;
bool found;
enum storage_err result;
const char *old_name;
const char *new_name;
size_t fname_len;
size_t old_len;
size_t new_len;
uint32_t flags;
struct file_handle *file = NULL;
char path_buf[FS_PATH_MAX];
enum file_create_mode file_create_mode;
struct file_handle tmp_file;
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
return STORAGE_ERR_NOT_VALID;
}
flags = req->flags;
if ((flags & ~STORAGE_FILE_MOVE_MASK) != 0) {
SS_ERR("invalid move flags 0x%x\n", flags);
return STORAGE_ERR_NOT_VALID;
}
if (flags & STORAGE_FILE_MOVE_CREATE) {
if (flags & STORAGE_FILE_MOVE_CREATE_EXCLUSIVE) {
file_create_mode = FILE_OPEN_CREATE_EXCLUSIVE;
} else {
file_create_mode = FILE_OPEN_CREATE;
}
} else {
file_create_mode = FILE_OPEN_NO_CREATE;
}
/* make sure filename is legal */
old_name = req->old_new_name;
fname_len = req_size - sizeof(*req);
if (!is_valid_name(old_name, fname_len)) {
SS_ERR("%s: invalid filename\n", __func__);
return STORAGE_ERR_NOT_VALID;
}
old_len = req->old_name_len;
if (old_len >= fname_len) {
SS_ERR("%s: invalid old filename length (%zd) >= (%zd)\n",
__func__, old_len, fname_len);
return STORAGE_ERR_NOT_VALID;
}
new_len = fname_len - old_len;
new_name = old_name + old_len;
if (flags & STORAGE_FILE_MOVE_OPEN_FILE) {
file = get_file_handle(session, req->handle);
if (!file)
return STORAGE_ERR_NOT_VALID;
}
result = get_path(path_buf, sizeof(path_buf), &session->uuid,
old_name, old_len);
if (result != STORAGE_NO_ERROR) {
return result;
}
SS_INFO("%s: old path %s\n", __func__, path_buf);
if (file) {
if (!storage_file_check_name(&session->tr, file, path_buf)) {
return STORAGE_ERR_NOT_VALID;
}
} else {
found = file_open(&session->tr, path_buf,
&tmp_file, FILE_OPEN_NO_CREATE);
if (!found) {
return STORAGE_ERR_NOT_FOUND;
}
file = &tmp_file;
}
result = get_path(path_buf, sizeof(path_buf), &session->uuid,
new_name, new_len);
if (result != STORAGE_NO_ERROR) {
if (file == &tmp_file) {
file_close(&tmp_file);
}
return result;
}
SS_INFO("%s: new path %s\n", __func__, path_buf);
moved = file_move(&session->tr, file, path_buf, file_create_mode);
if (file == &tmp_file) {
file_close(&tmp_file);
}
if (session->tr.failed) {
SS_ERR("%s: transaction failed\n", __func__);
return STORAGE_ERR_GENERIC;
} else if (!moved) {
return (flags & STORAGE_FILE_MOVE_CREATE) ?
STORAGE_ERR_EXIST : STORAGE_ERR_NOT_FOUND;
}
if (msg->flags & STORAGE_MSG_FLAG_TRANSACT_COMPLETE) {
transaction_complete(&session->tr);
if (session->tr.failed) {
SS_ERR("%s: transaction commit failed\n", __func__);
return STORAGE_ERR_GENERIC;
}
return STORAGE_NO_ERROR;
}
return STORAGE_NO_ERROR;
}
static int storage_file_open(struct storage_msg *msg,
struct storage_file_open_req *req, size_t req_size,
struct storage_client_session *session)
{
bool found;
enum storage_err result;
struct file_handle *file = NULL;
const char *fname;
size_t fname_len;
uint32_t flags, f_handle;
char path_buf[FS_PATH_MAX];
void *out = NULL;
size_t out_size = 0;
enum file_create_mode file_create_mode;
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_size;
}
flags = req->flags;
if ((flags & ~STORAGE_FILE_OPEN_MASK) != 0) {
SS_ERR("%s: invalid flags 0x%x\n", __func__, flags);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_mask;
}
/* make sure filename is legal */
fname = req->name;
fname_len = req_size - sizeof(*req);
if (!is_valid_name(fname, fname_len)) {
SS_ERR("%s: invalid filename\n", __func__);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_name;
}
result = get_path(path_buf, sizeof(path_buf), &session->uuid, fname, fname_len);
if (result != STORAGE_NO_ERROR) {
goto err_get_path;
}
SS_INFO("%s: path %s flags 0x%x\n", __func__, path_buf, flags);
SS_INFO("%s: call create_file_handle\n", __func__);
/* alloc file info struct */
result = create_file_handle(session, &f_handle, &file);
if (result != STORAGE_NO_ERROR)
goto err_create_file_handle;
if (flags & STORAGE_FILE_OPEN_CREATE) {
if (flags & STORAGE_FILE_OPEN_CREATE_EXCLUSIVE) {
file_create_mode = FILE_OPEN_CREATE_EXCLUSIVE;
} else {
file_create_mode = FILE_OPEN_CREATE;
}
} else {
file_create_mode = FILE_OPEN_NO_CREATE;
}
found = file_open(&session->tr, path_buf, file, file_create_mode);
if (!found) {
/* TODO: get more accurate error code from file_open */
if (session->tr.failed) {
result = STORAGE_ERR_GENERIC;
} else if (flags & STORAGE_FILE_OPEN_CREATE) {
result = STORAGE_ERR_EXIST;
} else {
result = STORAGE_ERR_NOT_FOUND;
}
goto err_open_file;
}
if ((flags & STORAGE_FILE_OPEN_TRUNCATE) && file->size) {
file_set_size(&session->tr, file, 0);
}
if (session->tr.failed) {
SS_ERR("%s: transaction failed\n", __func__);
result = STORAGE_ERR_GENERIC;
goto err_transaction_failed;
}
if (msg->flags & STORAGE_MSG_FLAG_TRANSACT_COMPLETE) {
transaction_complete(&session->tr);
if (session->tr.failed) {
SS_ERR("%s: transaction commit failed\n", __func__);
result = STORAGE_ERR_GENERIC;
goto err_transaction_failed;
}
}
struct storage_file_open_resp resp = { .handle = f_handle };
out = &resp;
out_size = sizeof(resp);
result = STORAGE_NO_ERROR;
goto done;
err_transaction_failed:
file_close(file);
err_open_file:
free_file_handle(session, f_handle);
err_create_file_handle:
err_get_path:
err_invalid_name:
err_invalid_mask:
err_invalid_size:
done:
return send_response(session, result, msg, out, out_size);
}
static enum storage_err storage_file_close(struct storage_msg *msg,
struct storage_file_close_req *req, size_t req_size,
struct storage_client_session *session)
{
struct file_handle *file;
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
return STORAGE_ERR_NOT_VALID;
}
file = get_file_handle(session, req->handle);
if (!file)
return STORAGE_ERR_NOT_VALID;
file_close(file);
free_file_handle(session, req->handle);
if (msg->flags & STORAGE_MSG_FLAG_TRANSACT_COMPLETE) {
transaction_complete(&session->tr);
if (session->tr.failed) {
SS_ERR("%s: transaction commit failed\n", __func__);
return STORAGE_ERR_GENERIC;
}
return STORAGE_NO_ERROR;
}
return STORAGE_NO_ERROR;
}
static int storage_file_read(struct storage_msg *msg,
struct storage_file_read_req *req, size_t req_size,
struct storage_client_session *session)
{
enum storage_err result = STORAGE_NO_ERROR;
void *bufp = NULL;
size_t buflen;
size_t bytes_left, len;
uint64_t offset;
struct file_handle *file;
void *out = NULL;
size_t out_size = 0;
size_t block_size = get_file_block_size(session->tr.fs);
data_block_t block_num;
const uint8_t *block_data;
obj_ref_t block_data_ref = OBJ_REF_INITIAL_VALUE(block_data_ref);
size_t block_offset;
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
file = get_file_handle(session, req->handle);
if (!file) {
SS_ERR("%s: invalid file handle (%d)\n", __func__, req->handle);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
buflen = req->size;
if (buflen > STORAGE_MAX_BUFFER_SIZE - sizeof(*msg)) {
SS_ERR("can't read more than %d bytes, requested %zd\n",
STORAGE_MAX_BUFFER_SIZE, buflen);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
offset = req->offset;
if (offset > file->size) {
SS_ERR("can't read past end of file (%lld > %lld)\n",
offset, file->size);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
// reuse the input buffer
bufp = (uint8_t *)(msg + 1);
/* calc number of bytes to read */
if ((offset + buflen) > file->size) {
bytes_left = (size_t)(file->size - offset);
} else {
bytes_left = buflen;
}
buflen = bytes_left; /* save to return it to caller */
SS_INFO("%s: start 0x%x cnt %d\n", __func__, offset, bytes_left);
result = STORAGE_NO_ERROR;
while (bytes_left) {
block_num = offset / block_size;
block_data = file_get_block(&session->tr, file, block_num,
&block_data_ref);
block_offset = offset % block_size;
len = (block_offset + bytes_left > block_size) ?
block_size - block_offset : bytes_left;
if (!block_data) {
if (session->tr.failed) {
SS_ERR("error reading block %lld\n", block_num);
result = STORAGE_ERR_GENERIC;
goto err_get_block;
}
memset(bufp, 0, len);
} else {
memcpy(bufp, block_data + block_offset, len);
file_block_put(block_data, &block_data_ref);
}
bytes_left -= len;
offset += len;
bufp += len;
}
out = (uint8_t *)(msg + 1);
out_size = buflen;
err_get_block:
err_invalid_input:
return send_response(session, result, msg, out, out_size);
}
static enum storage_err storage_create_gap(struct storage_client_session *session,
struct file_handle *file)
{
size_t block_size = get_file_block_size(session->tr.fs);
data_block_t block_num;
size_t block_offset;
uint8_t *block_data;
obj_ref_t block_data_ref = OBJ_REF_INITIAL_VALUE(block_data_ref);
block_num = file->size / block_size;
block_offset = file->size % block_size;
if (block_offset) {
/*
* The file does not currently end on a block boundary.
* We don't clear data in partial blocks when truncating
* a file, so the last block could contain data that
* should not be readable. We unconditionally clear the
* exposed data when creating gaps in the file, as we
* don't know if that data is already clear.
*/
block_data = file_get_block_write(&session->tr, file, block_num,
true, &block_data_ref);
if (!block_data) {
SS_ERR("error getting block %lld\n", block_num);
return STORAGE_ERR_GENERIC;
}
memset(block_data + block_offset, 0, block_size - block_offset);
file_block_put_dirty(&session->tr, file, block_num,
block_data, &block_data_ref);
SS_INFO("%s: clear block at old size 0x%llx, block_offset 0x%zx\n",
__func__, file->size, block_offset);
}
return STORAGE_NO_ERROR;
}
static enum storage_err storage_file_write(struct storage_msg *msg,
struct storage_file_write_req *req, size_t req_size,
struct storage_client_session *session)
{
enum storage_err result = STORAGE_NO_ERROR;
const void *bufp = NULL;
size_t buflen;
uint64_t offset, end_offset, bytes_left;
size_t len;
struct file_handle *file;
size_t block_size = get_file_block_size(session->tr.fs);
data_block_t block_num;
uint8_t *block_data;
obj_ref_t block_data_ref = OBJ_REF_INITIAL_VALUE(block_data_ref);
size_t block_offset;
if (req_size <= sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
return STORAGE_ERR_NOT_VALID;
}
file = get_file_handle(session, req->handle);
if (!file) {
SS_ERR("%s: invalid file handle (%d)\n", __func__, req->handle);
return STORAGE_ERR_NOT_VALID;
}
offset = req->offset;
if (offset > file->size) {
result = storage_create_gap(session, file);
if (result != STORAGE_NO_ERROR) {
goto err_write;
}
}
bufp = req->data;
buflen = req_size - sizeof(*req);
end_offset = offset + buflen - 1;
bytes_left = end_offset - offset + 1;
/* transfer data one ss block at a time */
while (bytes_left) {
block_num = offset / block_size;
block_offset = offset % block_size;
len = (block_offset + bytes_left > block_size) ?
block_size - block_offset : bytes_left;
block_data = file_get_block_write(&session->tr, file, block_num,
len != block_size, &block_data_ref);
if (!block_data) {
SS_ERR("error getting block %lld\n", block_num);
result = STORAGE_ERR_GENERIC;
goto err_write;
}
memcpy(block_data + block_offset, bufp, len);
file_block_put_dirty(&session->tr, file, block_num,
block_data, &block_data_ref);
SS_INFO("%s: bufp %p offset 0x%llx len 0x%x\n",
__func__, bufp, offset, len);
bytes_left -= len;
offset += len;
bufp += len;
}
if (offset > file->size) {
file_set_size(&session->tr, file, offset);
}
if (session->tr.failed) {
SS_ERR("%s: transaction failed\n", __func__);
return STORAGE_ERR_GENERIC;
}
if (msg->flags & STORAGE_MSG_FLAG_TRANSACT_COMPLETE) {
transaction_complete(&session->tr);
if (session->tr.failed) {
SS_ERR("%s: transaction commit failed\n", __func__);
return STORAGE_ERR_GENERIC;
}
}
return STORAGE_NO_ERROR;
err_write:
if (!session->tr.failed) {
transaction_fail(&session->tr);
}
err_transaction_complete:
return result;
}
struct storage_file_list_state {
struct file_iterate_state iter;
char prefix[34];
size_t prefix_len;
uint8_t buf[1024];
size_t buf_used;
uint8_t max_count;
uint8_t count;
};
static bool storage_file_list_buf_full(struct storage_file_list_state *miter)
{
size_t max_item_size = FS_PATH_MAX - miter->prefix_len + 2;
if (miter->max_count && miter->count >= miter->max_count) {
return true;
}
return miter->buf_used + max_item_size > sizeof(miter->buf);
}
static void storage_file_list_add(struct storage_file_list_state *miter,
uint8_t flags, const char *path)
{
struct storage_file_list_resp *resp;
assert(!storage_file_list_buf_full(miter));
resp = (void *)(miter->buf + miter->buf_used);
resp->flags = flags;
miter->buf_used++;
miter->count++;
if (path) {
strcpy(resp->name, path);
miter->buf_used += strlen(path) + 1;
}
}
static bool storage_file_list_iter(struct file_iterate_state *iter,
struct transaction *tr,
const struct block_mac *block_mac,
bool added, bool removed)
{
struct storage_file_list_state *miter =
containerof(iter, struct storage_file_list_state, iter);
const struct file_info *file_info;
obj_ref_t ref = OBJ_REF_INITIAL_VALUE(ref);
file_info = file_get_info(tr, block_mac, &ref);
if (strncmp(file_info->path, miter->prefix, miter->prefix_len) == 0) {
storage_file_list_add(miter,
added ? STORAGE_FILE_LIST_ADDED :
removed ? STORAGE_FILE_LIST_REMOVED :
STORAGE_FILE_LIST_COMMITTED,
file_info->path + miter->prefix_len);
}
file_info_put(file_info, &ref);
return storage_file_list_buf_full(miter);
}
static int storage_file_list(struct storage_msg *msg,
struct storage_file_list_req *req, size_t req_size,
struct storage_client_session *session)
{
enum storage_err result = STORAGE_NO_ERROR;
void *out = NULL;
size_t out_size = 0;
bool ret;
const char *last_name;
char path_buf[FS_PATH_MAX];
uint8_t last_state;
const char *fname;
size_t fname_len;
struct storage_file_list_state state = {
.iter.file = storage_file_list_iter,
.buf_used = 0,
};
if (req_size < sizeof(*req)) {
SS_ERR("%s: invalid request size (%zd)\n", __func__, req_size);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
result = get_path(state.prefix, sizeof(state.prefix), &session->uuid, "", 0);
if (result != STORAGE_NO_ERROR) {
SS_ERR("%s: internal error, get_path failed\n", __func__);
result = STORAGE_ERR_GENERIC;
goto err_get_path_prefix;
}
state.prefix_len = strlen(state.prefix);
last_state = req->flags & STORAGE_FILE_LIST_STATE_MASK;
if (last_state == STORAGE_FILE_LIST_END) {
SS_ERR("%s: invalid request state (%d)\n", __func__, last_state);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_state;
}
if (last_state == STORAGE_FILE_LIST_START) {
last_name = NULL;
} else {
/* make sure filename is legal */
fname = req->name;
fname_len = req_size - sizeof(*req);
if (!is_valid_name(fname, fname_len)) {
SS_ERR("%s: invalid filename\n", __func__);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_filename;
}
result = get_path(path_buf, sizeof(path_buf), &session->uuid, fname, fname_len);
if (result != STORAGE_NO_ERROR) {
goto err_invalid_filename;
}
last_name = path_buf;
}
if (last_state != STORAGE_FILE_LIST_ADDED) {
ret = file_iterate(&session->tr, last_name, false, &state.iter);
last_name = NULL;
} else {
ret = true;
}
if (ret && !storage_file_list_buf_full(&state)) {
ret = file_iterate(&session->tr, last_name, true, &state.iter);
}
if (!ret) {
SS_ERR("%s: file_iterate failed\n", __func__);
result = STORAGE_ERR_GENERIC;
goto err_file_iterate;
}
if (!storage_file_list_buf_full(&state)) {
storage_file_list_add(&state, STORAGE_FILE_LIST_END, NULL);
}
out = state.buf;
out_size = state.buf_used;
err_file_iterate:
err_invalid_filename:
err_invalid_state:
err_get_path_prefix:
err_invalid_input:
return send_response(session, result, msg, out, out_size);
}
static int storage_file_get_size(struct storage_msg *msg,
struct storage_file_get_size_req *req, size_t req_size,
struct storage_client_session *session)
{
bool valid;
struct file_handle *file;
enum storage_err result = STORAGE_NO_ERROR;
void *out = NULL;
size_t out_size = 0;
if (req_size != sizeof(*req)) {
SS_ERR("%s: inavlid request size (%zd)\n", __func__, req_size);
result = STORAGE_ERR_NOT_VALID;
goto err_invalid_input;
}
file = get_file_handle(session, req->handle);
if (!file) {
SS_ERR("%s: invalid file handle (%d)\n", __func__, req->handle);
result = STORAGE_ERR_NOT_VALID;