-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathos_test.c
1603 lines (1357 loc) · 40.7 KB
/
os_test.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
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2014, STMicroelectronics International N.V.
* All rights reserved.
* Copyright (c) 2022, Linaro Limited.
*/
#include <compiler.h>
#include <dlfcn.h>
#include <link.h>
#include <memtag.h>
#include <setjmp.h>
#include <stdint.h>
#include <string.h>
#include <ta_crypt.h>
#include <ta_os_test.h>
#include <tee_internal_api_extensions.h>
#include "os_test.h"
#include "test_float_subj.h"
#include "os_test_lib.h"
#define STATS_UUID \
{ 0xd96a5b40, 0xe2c7, 0xb1af, \
{ 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
#define STATS_CMD_PAGER_STATS 0
#define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096)
enum p_type {
P_TYPE_BOOL,
P_TYPE_INT,
P_TYPE_UUID,
P_TYPE_IDENTITY,
P_TYPE_STRING,
P_TYPE_BINARY_BLOCK,
};
struct p_attr {
const char *str;
enum p_type type;
bool retrieved;
};
static TEE_Result check_returned_prop(
int line __maybe_unused, char *prop_name __maybe_unused,
TEE_Result return_res, TEE_Result expected_res,
uint32_t return_len, uint32_t expected_len)
{
if (return_res != expected_res) {
EMSG("From line %d (property name=%s): return_res=0x%x vs expected_res=0x%x",
line, (prop_name ? prop_name : "unknown"),
(unsigned int)return_res, (unsigned int)expected_res);
return TEE_ERROR_GENERIC;
}
if (return_len != expected_len) {
EMSG("From line %d (property name=%s): return_len=%u vs expected_res=%u",
line, (prop_name ? prop_name : "unknown"),
return_len, expected_len);
return TEE_ERROR_GENERIC;
}
return TEE_SUCCESS;
}
static TEE_Result check_binprop_ones(size_t size, char *bbuf, size_t bblen)
{
char ones[4] = { 0xff, 0xff, 0xff, 0xff };
if (size > 4 || bblen != size) {
EMSG("Size error (size=%zu, bblen=%zu)", size, bblen);
return TEE_ERROR_GENERIC;
}
if (strncmp(bbuf, ones, bblen)) {
EMSG("Unexpected content");
DHEXDUMP(bbuf, bblen);
return TEE_ERROR_GENERIC;
}
return TEE_SUCCESS;
}
static TEE_Result get_binblock_property(TEE_PropSetHandle h,
char *nbuf __unused, char **bbuf, size_t *bblen)
{
TEE_Result res = TEE_ERROR_GENERIC;
size_t block_len = 0;
*bbuf = NULL;
*bblen = 0;
res = TEE_GetPropertyAsBinaryBlock(h, NULL, *bbuf, &block_len);
if (res == TEE_SUCCESS && !block_len)
return TEE_SUCCESS;
if (res != TEE_ERROR_SHORT_BUFFER) {
EMSG("TEE_GetPropertyAsBinaryBlock() size query returned 0x%x",
(unsigned int)res);
return res ? res : TEE_ERROR_GENERIC;
}
*bbuf = TEE_Malloc(block_len, TEE_MALLOC_FILL_ZERO);
if (!bbuf)
return TEE_ERROR_OUT_OF_MEMORY;
res = TEE_GetPropertyAsBinaryBlock(h, NULL, *bbuf, &block_len);
if (res != TEE_SUCCESS)
EMSG("TEE_GetPropertyAsBinaryBlock(\"%s\") returned 0x%x",
nbuf, (unsigned int)res);
else
*bblen = block_len;
return res;
}
static TEE_Result print_properties(TEE_PropSetHandle h,
TEE_PropSetHandle prop_set,
struct p_attr *p_attrs, size_t num_p_attrs)
{
TEE_Result res = TEE_ERROR_GENERIC;
size_t n = 0;
TEE_StartPropertyEnumerator(h, prop_set);
while (true) {
char nbuf[256] = { };
char nbuf_small[256] = { };
char vbuf[256] = { };
char vbuf2[256] = { };
size_t nblen = sizeof(nbuf);
size_t nblen_small = 0;
size_t vblen = sizeof(vbuf);
size_t vblen2 = sizeof(vbuf2);
char *bbuf = NULL;
size_t bblen = 0;
res = TEE_GetPropertyName(h, nbuf, &nblen);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetPropertyName() returned %#"PRIx32, res);
return res;
}
if (nblen != strlen(nbuf) + 1) {
EMSG("Name has wrong size: %zu vs %zu",
nblen, strlen(nbuf) + 1);
return TEE_ERROR_GENERIC;
}
/* Get the property name with a very small buffer */
nblen_small = 2;
res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
nblen_small, nblen);
if (res != TEE_SUCCESS)
return res;
/* Get the property name with almost the correct buffer */
nblen_small = nblen - 1;
res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
nblen_small, nblen);
if (res != TEE_SUCCESS)
return res;
/* Get the property name with the exact buffer length */
nblen_small = nblen;
res = TEE_GetPropertyName(h, nbuf_small, &nblen_small);
res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
nblen_small, nblen);
if (res != TEE_SUCCESS)
return res;
/* Get the property value */
res = TEE_GetPropertyAsString(h, NULL, vbuf, &vblen);
res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
vblen, strlen(vbuf) + 1);
if (res != TEE_SUCCESS)
return res;
res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS,
vblen2, strlen(vbuf2) + 1);
if (res != TEE_SUCCESS)
return res;
if (strcmp(vbuf, vbuf2) != 0) {
EMSG("String of \"%s\" differs", nbuf);
return TEE_ERROR_GENERIC;
}
/* Get the property with a very small buffer */
if (vblen > 1) {
vblen2 = 1;
res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
res = check_returned_prop(__LINE__, nbuf, res,
TEE_ERROR_SHORT_BUFFER,
vblen2, vblen);
if (res != TEE_SUCCESS)
return res;
}
/* Get the property with almost the correct buffer */
vblen2 = vblen - 1;
res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
res = check_returned_prop(__LINE__, nbuf, res, TEE_ERROR_SHORT_BUFFER,
vblen2, vblen);
if (res != TEE_SUCCESS)
return res;
/* Get the property name with the exact buffer length */
vblen2 = vblen;
res = TEE_GetPropertyAsString(prop_set, nbuf, vbuf2, &vblen2);
res = check_returned_prop(__LINE__, nbuf, res, TEE_SUCCESS, vblen2, vblen);
if (res != TEE_SUCCESS)
return res;
/* check specific myprop.hello property, which is larger than 80 */
if (!strcmp("myprop.hello", nbuf) &&
vblen2 != 1 + strlen("hello property, larger than 80 characters, so that it checks that it is not truncated by anything in the source code which may be wrong")) {
EMSG("TEE_GetPropertyAsString(\"%s\") is truncated - returned \"%s\"",
nbuf, vbuf);
return TEE_ERROR_GENERIC;
}
DMSG("Found \"%s\" value \"%s\"", nbuf, vbuf);
for (n = 0; n < num_p_attrs; n++) {
if (strcmp(nbuf, p_attrs[n].str) != 0)
continue;
if (p_attrs[n].retrieved) {
EMSG("Value \"%s\" already retrieved",
p_attrs[n].str);
return TEE_ERROR_GENERIC;
}
p_attrs[n].retrieved = true;
switch (p_attrs[n].type) {
case P_TYPE_BOOL:
{
bool v = false;
res =
TEE_GetPropertyAsBool(h, NULL, &v);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetPropertyAsBool(\"%s\") returned %#"PRIx32,
nbuf, res);
return res;
}
}
break;
case P_TYPE_INT:
{
uint32_t v = 0;
res = TEE_GetPropertyAsU32(h, NULL, &v);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetPropertyAsU32(\"%s\") returned %#"PRIx32,
nbuf, res);
return res;
}
}
break;
case P_TYPE_UUID:
{
TEE_UUID v = { };
res =
TEE_GetPropertyAsUUID(h, NULL, &v);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetPropertyAsUUID(\"%s\") returned %#"PRIx32,
nbuf, res);
return res;
}
}
break;
case P_TYPE_IDENTITY:
{
TEE_Identity v = { };
res =
TEE_GetPropertyAsIdentity(h, NULL,
&v);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetPropertyAsIdentity(\"%s\") returned %#"PRIx32,
nbuf, res);
return res;
}
}
break;
case P_TYPE_STRING:
/* Already read as string */
break;
case P_TYPE_BINARY_BLOCK:
res = get_binblock_property(h, nbuf, &bbuf, &bblen);
if (res)
return res;
if (!strcmp("myprop.binaryblock", nbuf)) {
const char exp_bin_value[] = "Hello world!";
if (bblen != strlen(exp_bin_value) ||
TEE_MemCompare(exp_bin_value, bbuf,
bblen)) {
EMSG("Binary buffer of \"%s\" differs from \"%s\"",
nbuf, exp_bin_value);
EMSG("Got \"%s\"", bbuf);
return TEE_ERROR_GENERIC;
}
} else if (!strcmp("myprop.binaryblock.1byte-ones",
nbuf)) {
res = check_binprop_ones(1, bbuf, bblen);
if (res)
return res;
} else if (!strcmp("myprop.binaryblock.2byte-ones",
nbuf)) {
res = check_binprop_ones(2, bbuf, bblen);
if (res)
return res;
} else if (!strcmp("myprop.binaryblock.3byte-ones",
nbuf)) {
res = check_binprop_ones(3, bbuf, bblen);
if (res)
return res;
} else if (!strcmp("myprop.binaryblock.4byte-ones",
nbuf)) {
res = check_binprop_ones(4, bbuf, bblen);
if (res)
return res;
} else if (!strcmp("myprop.binaryblock.empty1", nbuf) ||
!strcmp("myprop.binaryblock.empty2", nbuf) ||
!strcmp("myprop.binaryblock.empty3", nbuf)) {
if (bblen) {
EMSG("Property \"%s\": %zu byte(s)",
nbuf, bblen);
return TEE_ERROR_GENERIC;
}
} else {
EMSG("Unexpected property \"%s\"", nbuf);
TEE_Panic(0);
}
TEE_Free(bbuf);
break;
default:
EMSG("Unknown type (%d) for \"%s\"",
p_attrs[n].type, p_attrs[n].str);
return TEE_ERROR_GENERIC;
}
}
res = TEE_GetNextProperty(h);
if (res != TEE_SUCCESS) {
if (res == TEE_ERROR_ITEM_NOT_FOUND)
return TEE_SUCCESS;
return res;
}
}
}
static TEE_Result test_malloc(void)
{
void *p = TEE_Malloc(4, 0);
if (p == NULL) {
EMSG("TEE_Malloc() failed");
return TEE_ERROR_OUT_OF_MEMORY;
}
TEE_Free(p);
TEE_Free(NULL);
return TEE_SUCCESS;
}
static TEE_Result test_properties(void)
{
TEE_Result res = TEE_ERROR_GENERIC;
TEE_PropSetHandle h = TEE_HANDLE_NULL;
struct p_attr p_attrs[] = {
{"gpd.ta.appID", P_TYPE_UUID},
{"gpd.ta.singleInstance", P_TYPE_BOOL},
{"gpd.ta.multiSession", P_TYPE_BOOL},
{"gpd.ta.instanceKeepAlive", P_TYPE_BOOL},
{"gpd.ta.dataSize", P_TYPE_INT},
{"gpd.ta.stackSize", P_TYPE_INT},
{"gpd.ta.version", P_TYPE_STRING},
{"gpd.ta.description", P_TYPE_STRING},
{"gpd.client.identity", P_TYPE_IDENTITY},
{"gpd.tee.apiversion", P_TYPE_STRING},
{"gpd.tee.description", P_TYPE_STRING},
{"gpd.tee.deviceID", P_TYPE_UUID},
{"gpd.tee.systemTime.protectionLevel", P_TYPE_INT},
{"gpd.tee.TAPersistentTime.protectionLevel", P_TYPE_INT},
{"gpd.tee.arith.maxBigIntSize", P_TYPE_INT},
{"gpd.tee.cryptography.ecc", P_TYPE_BOOL},
{"gpd.tee.trustedStorage.antiRollback.protectionLevel", P_TYPE_INT},
{"gpd.tee.trustedos.implementation.version", P_TYPE_STRING},
{"gpd.tee.trustedos.implementation.binaryversion", P_TYPE_INT},
{"gpd.tee.trustedos.manufacturer", P_TYPE_STRING},
{"gpd.tee.firmware.implementation.version", P_TYPE_STRING},
{"gpd.tee.firmware.implementation.binaryversion", P_TYPE_INT},
{"gpd.tee.firmware.manufacturer", P_TYPE_STRING},
{"myprop.true", P_TYPE_BOOL},
{"myprop.42", P_TYPE_INT},
{"myprop.123", P_TYPE_UUID},
{"myprop.1234", P_TYPE_IDENTITY},
{"myprop.hello", P_TYPE_STRING},
{"myprop.binaryblock", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.1byte-ones", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.2byte-ones", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.3byte-ones", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.4byte-ones", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.empty1", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.empty2", P_TYPE_BINARY_BLOCK},
{"myprop.binaryblock.empty3", P_TYPE_BINARY_BLOCK},
};
const size_t num_p_attrs = sizeof(p_attrs) / sizeof(p_attrs[0]);
size_t n = 0;
res = TEE_AllocatePropertyEnumerator(&h);
if (res != TEE_SUCCESS) {
EMSG("TEE_AllocatePropertyEnumerator: returned %#"PRIx32, res);
return TEE_ERROR_GENERIC;
}
MSG("Getting properties for current TA");
res = print_properties(h, TEE_PROPSET_CURRENT_TA, p_attrs, num_p_attrs);
if (res != TEE_SUCCESS)
goto cleanup_return;
MSG("Getting properties for current client");
res = print_properties(h, TEE_PROPSET_CURRENT_CLIENT, p_attrs,
num_p_attrs);
if (res != TEE_SUCCESS)
goto cleanup_return;
MSG("Getting properties for implementation");
res = print_properties(h, TEE_PROPSET_TEE_IMPLEMENTATION, p_attrs,
num_p_attrs);
if (res != TEE_SUCCESS)
goto cleanup_return;
for (n = 0; n < num_p_attrs; n++) {
if (!p_attrs[n].retrieved) {
EMSG("\"%s\" not retrieved", p_attrs[n].str);
res = TEE_ERROR_GENERIC;
goto cleanup_return;
}
}
cleanup_return:
TEE_FreePropertyEnumerator(h);
return res;
}
static TEE_Result test_mem_access_right(uint32_t param_types,
TEE_Param params[4])
{
static const TEE_UUID test_uuid = TA_OS_TEST_UUID;
TEE_Result res = TEE_ERROR_GENERIC;
uint32_t ret_orig = 0;
uint32_t l_pts = 0;
TEE_Param l_params[4] = { };
uint8_t buf[32] = { };
TEE_TASessionHandle sess = TEE_HANDLE_NULL;
TEE_UUID *uuid = NULL;
if (param_types !=
TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0, 0))
return TEE_ERROR_BAD_PARAMETERS;
/* test access rights on memref parameter */
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
TEE_MEMORY_ACCESS_ANY_OWNER,
params[0].memref.buffer,
params[0].memref.size);
if (res != TEE_SUCCESS)
return res;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
params[0].memref.buffer,
params[0].memref.size);
if (res != TEE_ERROR_ACCESS_DENIED)
return TEE_ERROR_GENERIC;
/* test access rights on private read-only and read-write memory */
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
(void *)&test_uuid, sizeof(test_uuid));
if (res != TEE_SUCCESS)
return res;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_WRITE,
(void *)&test_uuid, sizeof(test_uuid));
if (res == TEE_SUCCESS)
return TEE_ERROR_GENERIC;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
TEE_MEMORY_ACCESS_WRITE,
&ret_orig, sizeof(ret_orig));
if (res != TEE_SUCCESS)
return res;
uuid = TEE_Malloc(sizeof(*uuid), 0);
if (!uuid)
return TEE_ERROR_OUT_OF_MEMORY;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
TEE_MEMORY_ACCESS_WRITE,
uuid, sizeof(*uuid));
TEE_Free(uuid);
if (res != TEE_SUCCESS)
return res;
/* test access rights on invalid memory (at least lower 256kB) */
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
NULL, 1);
if (res == TEE_SUCCESS)
return TEE_ERROR_GENERIC;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
(void*)(256 * 1024), 1);
if (res == TEE_SUCCESS)
return TEE_ERROR_GENERIC;
res = TEE_OpenTASession(&test_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
&sess, &ret_orig);
if (res != TEE_SUCCESS) {
EMSG("TEE_OpenTASession() failed: %#"PRIx32, res);
return res;
}
l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0);
l_params[0].memref.buffer = buf;
l_params[0].memref.size = sizeof(buf);
l_params[1].memref.buffer = NULL;
l_params[1].memref.size = 0;
res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
TA_OS_TEST_CMD_PARAMS_ACCESS,
l_pts, l_params, &ret_orig);
if (res != TEE_SUCCESS)
EMSG("TEE_InvokeTACommand() failed: %#"PRIx32, res);
TEE_CloseTASession(sess);
return res;
}
static TEE_Result test_time(void)
{
TEE_Result res = TEE_ERROR_GENERIC;
TEE_Time t = { };
TEE_Time sys_t = { };
static const TEE_Time null_time = { 0, 0 };
static const TEE_Time wrap_time = { UINT32_MAX, 999 };
TEE_GetSystemTime(&sys_t);
MSG("system time %"PRIu32".%03"PRIx32, sys_t.seconds, sys_t.millis);
TEE_GetREETime(&t);
MSG("REE time %"PRIu32".%03"PRIu32, t.seconds, t.millis);
res = TEE_GetTAPersistentTime(&t);
switch (res) {
case TEE_SUCCESS:
MSG("Stored TA time %"PRIu32".%03"PRIu32, t.seconds, t.millis);
break;
case TEE_ERROR_OVERFLOW:
EMSG("Stored TA time overflowed %"PRIu32".%03"PRIu32,
t.seconds, t.millis);
break;
case TEE_ERROR_TIME_NOT_SET:
EMSG("TA time not stored");
break;
case TEE_ERROR_TIME_NEEDS_RESET:
EMSG("TA time needs reset");
break;
default:
return res;
}
res = TEE_SetTAPersistentTime(&null_time);
if (res != TEE_SUCCESS) {
EMSG("TEE_SetTAPersistentTime() failed%#"PRIx32, res);
return res;
}
res = TEE_GetTAPersistentTime(&t);
if (res != TEE_SUCCESS) {
EMSG("TEE_GetTAPersistentTime() for null time: failed %#"PRIx32,
res);
return res;
}
MSG("TA time %"PRIu32".%03"PRIu32, t.seconds, t.millis);
/*
* The time between TEE_SetTAPersistentTime() and
* TEE_GetTAPersistentTime() should be much less than 1 second, in fact
* it's not even a millisecond.
*/
if (t.seconds > 1 || t.millis >= 1000) {
EMSG("Unexpected stored TA time %"PRIu32".%03"PRIu32, t.seconds,
t.millis);
return TEE_ERROR_BAD_STATE;
}
res = TEE_SetTAPersistentTime(&wrap_time);
if (res != TEE_SUCCESS) {
EMSG("TEE_SetTAPersistentTime() wrap: failed %#"PRIx32, res);
return res;
}
res = TEE_Wait(1000);
if (res != TEE_SUCCESS)
EMSG("TEE_Wait() wrap: failed %#"PRIx32, res);
res = TEE_GetTAPersistentTime(&t);
if (res != TEE_ERROR_OVERFLOW) {
EMSG("TEE_GetTAPersistentTime(): failed %#"PRIx32, res);
return TEE_ERROR_BAD_STATE;
}
MSG("TA time %"PRIu32".%03"PRIu32, t.seconds, t.millis);
if (t.seconds > 1) {
EMSG("Unexpected wrapped time %"PRIu32".%03"PRIu32, t.seconds,
t.millis);
return TEE_ERROR_BAD_STATE;
}
return TEE_SUCCESS;
}
#ifdef CFG_TA_FLOAT_SUPPORT
static bool my_dcmpeq(double v1, double v2, double prec)
{
return v1 > (v2 - prec) && v1 < (v2 + prec);
}
static bool my_fcmpeq(float v1, float v2, float prec)
{
return v1 > (v2 - prec) && v1 < (v2 + prec);
}
static TEE_Result test_float(void)
{
#define VAL1 2.6
#define VAL1_INT 2
#define VAL2 5.3
#define DPREC 0.000000000000001
#define FPREC 0.000001
#define EXPECT(expr) do { \
if (!(expr)) { \
EMSG("Expression %s failed", #expr); \
return TEE_ERROR_GENERIC; \
} \
} while (0)
IMSG("Testing floating point operations");
EXPECT(my_dcmpeq(test_float_dadd(VAL1, VAL2), VAL1 + VAL2, DPREC));
EXPECT(my_dcmpeq(test_float_ddiv(VAL1, VAL2), VAL1 / VAL2, DPREC));
EXPECT(my_dcmpeq(test_float_dmul(VAL1, VAL2), VAL1 * VAL2, DPREC));
EXPECT(my_dcmpeq(test_float_drsub(VAL1, VAL2), VAL2 - VAL1, DPREC));
EXPECT(my_dcmpeq(test_float_dsub(VAL1, VAL2), VAL1 - VAL2, DPREC));
EXPECT(test_float_dcmpeq(VAL1, VAL1) == 1);
EXPECT(test_float_dcmplt(VAL1, VAL2) == 1);
EXPECT(test_float_dcmple(VAL1, VAL1) == 1);
EXPECT(test_float_dcmpge(VAL1, VAL1) == 1);
EXPECT(test_float_dcmpgt(VAL2, VAL1) == 1);
EXPECT(my_fcmpeq(test_float_fadd(VAL1, VAL2), VAL1 + VAL2, FPREC));
EXPECT(my_fcmpeq(test_float_fdiv(VAL1, VAL2), VAL1 / VAL2, FPREC));
EXPECT(my_fcmpeq(test_float_fmul(VAL1, VAL2), VAL1 * VAL2, FPREC));
EXPECT(my_fcmpeq(test_float_frsub(VAL1, VAL2), VAL2 - VAL1, FPREC));
EXPECT(my_fcmpeq(test_float_fsub(VAL1, VAL2), VAL1 - VAL2, FPREC));
EXPECT(test_float_fcmpeq(VAL1, VAL1) == 1);
EXPECT(test_float_fcmplt(VAL1, VAL2) == 1);
EXPECT(test_float_fcmple(VAL1, VAL1) == 1);
EXPECT(test_float_fcmpge(VAL1, VAL1) == 1);
EXPECT(test_float_fcmpgt(VAL2, VAL1) == 1);
EXPECT(test_float_d2iz(VAL1) == VAL1_INT);
EXPECT(test_float_d2uiz(VAL1) == VAL1_INT);
EXPECT(test_float_d2lz(VAL1) == VAL1_INT);
EXPECT(test_float_d2ulz(VAL1) == VAL1_INT);
EXPECT(test_float_f2iz(VAL1) == VAL1_INT);
EXPECT(test_float_f2uiz(VAL1) == VAL1_INT);
EXPECT(test_float_f2lz(VAL1) == VAL1_INT);
EXPECT(test_float_f2ulz(VAL1) == VAL1_INT);
EXPECT(my_fcmpeq(test_float_d2f(VAL1), VAL1, FPREC));
EXPECT(my_dcmpeq(test_float_f2d(VAL1), VAL1, FPREC));
EXPECT(my_dcmpeq(test_float_i2d(VAL1_INT), VAL1_INT, DPREC));
EXPECT(my_dcmpeq(test_float_ui2d(VAL1_INT), VAL1_INT, DPREC));
EXPECT(my_dcmpeq(test_float_l2d(VAL1_INT), VAL1_INT, DPREC));
EXPECT(my_dcmpeq(test_float_ul2d(VAL1_INT), VAL1_INT, DPREC));
EXPECT(my_fcmpeq(test_float_i2f(VAL1_INT), VAL1_INT, FPREC));
EXPECT(my_fcmpeq(test_float_ui2f(VAL1_INT), VAL1_INT, FPREC));
EXPECT(my_fcmpeq(test_float_l2f(VAL1_INT), VAL1_INT, FPREC));
EXPECT(my_fcmpeq(test_float_ul2f(VAL1_INT), VAL1_INT, FPREC));
return TEE_SUCCESS;
}
#else /*CFG_TA_FLOAT_SUPPORT*/
static TEE_Result test_float(void)
{
IMSG("Floating point disabled");
return TEE_SUCCESS;
}
#endif /*CFG_TA_FLOAT_SUPPORT*/
#if defined(CFG_TA_BGET_TEST)
/* From libutils */
int bget_main_test(void *(*malloc_func)(size_t), void (*free_func)(void *));
static void *malloc_wrapper(size_t size)
{
return tee_map_zi(size, 0);
}
static void free_wrapper(void *ptr __unused)
{
}
static bool optee_pager_with_small_pool(void)
{
uint32_t ptypes = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
TEE_PARAM_TYPE_VALUE_OUTPUT,
TEE_PARAM_TYPE_VALUE_OUTPUT,
TEE_PARAM_TYPE_NONE);
static const TEE_UUID uuid = STATS_UUID;
TEE_TASessionHandle sess = TEE_HANDLE_NULL;
TEE_Result res = TEE_ERROR_GENERIC;
TEE_Param params[4] = { };
uint32_t eo = 0;
bool rc = false;
res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL, &sess,
&eo);
if (res)
return false;
res = TEE_InvokeTACommand(sess, 0, STATS_CMD_PAGER_STATS,
ptypes, params, &eo);
if (res == TEE_SUCCESS &&
params[0].value.b && params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD)
rc = true;
TEE_CloseTASession(sess);
return rc;
}
static TEE_Result test_bget(void)
{
if (optee_pager_with_small_pool()) {
IMSG("Skip testing bget due to pager pool constraints");
return TEE_SUCCESS;
}
DMSG("Testing bget");
if (bget_main_test(malloc_wrapper, free_wrapper)) {
EMSG("bget_main_test failed");
return TEE_ERROR_GENERIC;
}
DMSG("Bget OK");
return TEE_SUCCESS;
}
#else
static TEE_Result test_bget(void)
{
IMSG("Bget test disabled");
return TEE_SUCCESS;
}
#endif
static __noinline __noreturn void call_longjmp(jmp_buf env)
{
DMSG("Calling longjmp");
longjmp(env, 1);
EMSG("error: longjmp returned to calling function");
}
static TEE_Result test_setjmp(void)
{
jmp_buf env = { };
if (setjmp(env)) {
IMSG("Returned via longjmp");
return TEE_SUCCESS;
} else {
call_longjmp(env);
return TEE_ERROR_GENERIC;
}
}
TEE_Result ta_entry_basic(uint32_t param_types, TEE_Param params[4])
{
TEE_Result res = TEE_ERROR_GENERIC;
MSG("enter");
res = test_malloc();
if (res != TEE_SUCCESS)
return res;
res = test_properties();
if (res != TEE_SUCCESS)
return res;
res = test_mem_access_right(param_types, params);
if (res != TEE_SUCCESS)
return res;
res = test_time();
if (res != TEE_SUCCESS)
return res;
res = test_float();
if (res != TEE_SUCCESS)
return res;
res = test_setjmp();
if (res != TEE_SUCCESS)
return res;
res = test_bget();
if (res != TEE_SUCCESS)
return res;
return TEE_SUCCESS;
}
TEE_Result ta_entry_panic(uint32_t param_types, TEE_Param params[4])
{
volatile bool mytrue = true;
(void)param_types;
(void)params;
MSG("enter");
/*
* Somewhat clumsy way of avoiding compile errors if TEE_Panic() has
* the __noreturn attribute.
*/
if (mytrue)
TEE_Panic(0xbeef);
/*
* Should not be reached, but if it is the testsuite can detect that
* TEE_Panic() returned instead of panicking the TA.
*/
return TEE_SUCCESS;
}
TEE_Result ta_entry_client_with_timeout(uint32_t param_types,
TEE_Param params[4])
{
static const TEE_UUID os_test_uuid = TA_OS_TEST_UUID;
TEE_Result res = TEE_ERROR_GENERIC;
TEE_TASessionHandle sess = TEE_HANDLE_NULL;
uint32_t ret_orig = 0;
if (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE)) {
EMSG("bad parameters");
return TEE_ERROR_BAD_PARAMETERS;
}
res = TEE_OpenTASession(&os_test_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
&sess, &ret_orig);
if (res != TEE_SUCCESS) {
EMSG("TEE_OpenTASession() failed %#"PRIx32, res);
return res;
}
res =
TEE_InvokeTACommand(sess, params[0].value.a / 2,
TA_OS_TEST_CMD_WAIT, param_types, params,
&ret_orig);
if (ret_orig != TEE_ORIGIN_TRUSTED_APP || res != TEE_ERROR_CANCEL) {
EMSG("TEE_InvokeTACommand(): res %#"PRIx32" ret_orig %#"PRIx32,
res, ret_orig);
res = TEE_ERROR_GENERIC;
} else
res = TEE_SUCCESS;
TEE_CloseTASession(sess);
return res;
}
TEE_Result ta_entry_client(uint32_t param_types, TEE_Param params[4])
{
static const TEE_UUID crypt_uuid = TA_CRYPT_UUID;
TEE_Result res = TEE_ERROR_GENERIC;
uint32_t l_pts = 0;
TEE_Param l_params[4] = { };
TEE_TASessionHandle sess = TEE_HANDLE_NULL;
uint32_t ret_orig = 0;
static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
static const uint8_t sha256_out[] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
};
uint8_t out[32] = { 0 };
void *in = NULL;
(void)param_types;
(void)params;
DMSG("ta_entry_client: enter");
in = TEE_Malloc(sizeof(sha256_in), 0);
if (in == NULL)
return TEE_ERROR_OUT_OF_MEMORY;
TEE_MemMove(in, sha256_in, sizeof(sha256_in));
res = TEE_OpenTASession(&crypt_uuid, TEE_TIMEOUT_INFINITE, 0, NULL,
&sess, &ret_orig);
if (res != TEE_SUCCESS) {
EMSG("TEE_OpenTASession() failed %#"PRIx32, res);
goto cleanup_free;
}
l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
TEE_PARAM_TYPE_MEMREF_OUTPUT, 0, 0);
l_params[0].memref.buffer = in;
l_params[0].memref.size = sizeof(sha256_in);
l_params[1].memref.buffer = out;
l_params[1].memref.size = sizeof(out);
res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
TA_CRYPT_CMD_SHA256, l_pts, l_params,
&ret_orig);
if (res != TEE_SUCCESS) {
EMSG("TEE_InvokeTACommand() failed %#"PRIx32, res);
goto cleanup_close_session;
}
if (TEE_MemCompare(sha256_out, out, sizeof(sha256_out)) != 0) {
EMSG("out parameter failed");
res = TEE_ERROR_GENERIC;
goto cleanup_close_session;
}
cleanup_close_session:
TEE_CloseTASession(sess);
cleanup_free:
TEE_Free(in);
return res;
}
TEE_Result ta_entry_params_access_rights(uint32_t param_types, TEE_Param params[4])
{
TEE_Result res = TEE_ERROR_GENERIC;
if (param_types !=
TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
TEE_PARAM_TYPE_MEMREF_INPUT, 0, 0))
return TEE_ERROR_BAD_PARAMETERS;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ |
TEE_MEMORY_ACCESS_ANY_OWNER,
params[0].memref.buffer,
params[0].memref.size);
if (res != TEE_SUCCESS)
return res;
res = TEE_CheckMemoryAccessRights(TEE_MEMORY_ACCESS_READ,
params[0].memref.buffer,
params[0].memref.size);
if (res != TEE_ERROR_ACCESS_DENIED)
return TEE_ERROR_GENERIC;
if (params[1].memref.buffer || params[1].memref.size)
return TEE_ERROR_BAD_PARAMETERS;
return TEE_SUCCESS;
}
TEE_Result ta_entry_wait(uint32_t param_types, TEE_Param params[4])
{
TEE_Result res = TEE_SUCCESS;
(void)param_types;
MSG("waiting %"PRId32, params[0].value.a);
/* Wait */
res = TEE_Wait(params[0].value.a);