blob: 5e3c7733fae0034dbca197e7daa57e440a7c64e8 [file] [log] [blame]
Etienne Carriere9b7b70d2020-05-16 10:27:23 +02001// SPDX-License-Identifier: GPL-2.0
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00003 * Copyright (c) 2020, ARM Limited. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02004 * Copyright (c) 2014, STMicroelectronics International N.V.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
Etienne Carrierea4653552017-01-11 10:04:24 +01007#include <limits.h>
8#include <pthread.h>
Pascal Brandc639ac82015-07-02 08:53:34 +02009#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010010#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020011#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060012#include <sys/stat.h>
13#include <sys/types.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010014#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020015
16#include "xtest_test.h"
17#include "xtest_helpers.h"
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +030018#include "xtest_uuid_helpers.h"
Jens Wiklander4441fe22015-10-23 16:53:02 +020019#include <signed_hdr.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010020#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020021
Etienne Carriere726d8bc2017-03-21 15:45:59 +010022#include <pta_invoke_tests.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020023#include <ta_crypt.h>
24#include <ta_os_test.h>
25#include <ta_create_fail_test.h>
26#include <ta_rpc_test.h>
27#include <ta_sims_test.h>
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +030028#include <ta_miss_test.h>
29#include <ta_sims_keepalive_test.h>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020030#include <ta_concurrent.h>
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +000031#include <ta_tpm_log_test.h>
Etienne Carriere50abf9a2017-03-24 11:33:50 +010032#include <sdp_basic.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010033#include <pta_secstor_ta_mgmt.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010034
35#ifndef MIN
36#define MIN(a, b) ((a) < (b) ? (a) : (b))
37#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020038
Pascal Brandc639ac82015-07-02 08:53:34 +020039struct xtest_crypto_session {
40 ADBG_Case_t *c;
41 TEEC_Session *session;
42 uint32_t cmd_id_sha256;
43 uint32_t cmd_id_aes256ecb_encrypt;
44 uint32_t cmd_id_aes256ecb_decrypt;
45};
46
47static void xtest_crypto_test(struct xtest_crypto_session *cs)
48{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010049 uint32_t ret_orig = 0;
50 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020051 uint8_t crypt_in[16] = { 22, 17 };
52
53 crypt_in[15] = 60;
54
55 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
56 {
57 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
58
59 op.params[0].tmpref.buffer = crypt_in;
60 op.params[0].tmpref.size = sizeof(crypt_in);
61 op.params[1].tmpref.buffer = crypt_out;
62 op.params[1].tmpref.size = sizeof(crypt_out);
63 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
64 TEEC_MEMREF_TEMP_OUTPUT,
65 TEEC_NONE, TEEC_NONE);
66
67 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
68 TEEC_InvokeCommand(cs->session,
69 cs->
70 cmd_id_aes256ecb_encrypt,
71 &op,
72 &ret_orig));
73 }
74 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
75
76 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
77 {
78 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010079 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020080
81 op.params[0].tmpref.buffer = crypt_out;
82 op.params[0].tmpref.size = sizeof(crypt_out);
83 op.params[1].tmpref.buffer = out;
84 op.params[1].tmpref.size = sizeof(out);
85 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
86 TEEC_MEMREF_TEMP_OUTPUT,
87 TEEC_NONE, TEEC_NONE);
88
89 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
90 TEEC_InvokeCommand(cs->session,
91 cs->
92 cmd_id_aes256ecb_decrypt,
93 &op,
94 &ret_orig));
95
96 if (!ADBG_EXPECT(cs->c, 0,
97 memcmp(crypt_in, out, sizeof(crypt_in)))) {
98 Do_ADBG_Log("crypt_in:");
99 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
100 Do_ADBG_Log("out:");
101 Do_ADBG_HexLog(out, sizeof(out), 16);
102 }
103 }
104 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
105
106 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
107 {
108 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
109 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
110 static const uint8_t sha256_out[] = {
111 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
112 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
113 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
114 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
115 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100116 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200117
118 op.params[0].tmpref.buffer = (void *)sha256_in;
119 op.params[0].tmpref.size = sizeof(sha256_in);
120 op.params[1].tmpref.buffer = out;
121 op.params[1].tmpref.size = sizeof(out);
122 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
123 TEEC_MEMREF_TEMP_OUTPUT,
124 TEEC_NONE, TEEC_NONE);
125
126 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
127 TEEC_InvokeCommand(cs->session,
128 cs->
129 cmd_id_sha256,
130 &op,
131 &ret_orig));
132
133 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
134 sizeof(sha256_out)))) {
135 Do_ADBG_Log("sha256_out:");
136 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
137 Do_ADBG_Log("out:");
138 Do_ADBG_HexLog(out, sizeof(out), 16);
139 }
140 }
141 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
142
Etienne Carrierea3198522017-10-26 09:48:55 +0200143 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200144 {
145 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
146 static const uint8_t in[] = {
147 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
148 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
149 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
150 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
151 };
152 static const uint8_t exp_out[] = {
153 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
154 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
155 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
156 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
157 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100158 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200159
160 op.params[0].tmpref.buffer = (void *)in;
161 op.params[0].tmpref.size = sizeof(in);
162 op.params[1].tmpref.buffer = out;
163 op.params[1].tmpref.size = sizeof(out);
164 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
165 TEEC_MEMREF_TEMP_OUTPUT,
166 TEEC_NONE, TEEC_NONE);
167
168 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
169 TEEC_InvokeCommand(cs->session,
170 cs->
171 cmd_id_aes256ecb_encrypt,
172 &op,
173 &ret_orig));
174
175 if (!ADBG_EXPECT(cs->c, 0,
176 memcmp(exp_out, out, sizeof(exp_out)))) {
177 Do_ADBG_Log("exp_out:");
178 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
179 Do_ADBG_Log("out:");
180 Do_ADBG_HexLog(out, sizeof(out), 16);
181 }
182 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200183 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200184
Etienne Carrierea3198522017-10-26 09:48:55 +0200185 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200186 {
187 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
188 static const uint8_t in[] = {
189 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
190 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
191 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
192 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
193 };
194 static const uint8_t exp_out[] = {
195 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
196 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
197 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
198 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
199 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100200 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200201
202 op.params[0].tmpref.buffer = (void *)in;
203 op.params[0].tmpref.size = sizeof(in);
204 op.params[1].tmpref.buffer = out;
205 op.params[1].tmpref.size = sizeof(out);
206 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
207 TEEC_MEMREF_TEMP_OUTPUT,
208 TEEC_NONE, TEEC_NONE);
209
210 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
211 TEEC_InvokeCommand(cs->session,
212 cs->
213 cmd_id_aes256ecb_decrypt,
214 &op,
215 &ret_orig));
216
217 if (!ADBG_EXPECT(cs->c, 0,
218 memcmp(exp_out, out, sizeof(exp_out)))) {
219 Do_ADBG_Log("exp_out:");
220 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
221 Do_ADBG_Log("out:");
222 Do_ADBG_HexLog(out, sizeof(out), 16);
223 }
224 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200225 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200226}
227
228static void xtest_tee_test_1001(ADBG_Case_t *c)
229{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100230 TEEC_Result res = TEEC_ERROR_GENERIC;
231 TEEC_Session session = { };
232 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200233
Etienne Carriere11093162017-10-26 09:49:04 +0200234 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100235 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100236 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200237 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
238 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100239 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200240 }
241 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200242
Jens Wiklandercf16e842016-02-10 09:07:09 +0100243 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100244 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100245 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200246}
Jens Wiklander14f48872018-06-29 15:30:13 +0200247ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200248
Jens Wiklander1d70a112017-10-16 15:16:39 +0200249static void xtest_tee_test_1002(ADBG_Case_t *c)
250{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100251 TEEC_Result res = TEEC_ERROR_GENERIC;
252 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200253 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100254 uint32_t ret_orig = 0;
255 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200256 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100257 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200258
Etienne Carriere11093162017-10-26 09:49:04 +0200259 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200260 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
261 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200262 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
263 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200264 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200265 }
266 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200267
268 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
269 TEEC_NONE, TEEC_NONE);
270 op.params[0].tmpref.size = sizeof(buf);
271 op.params[0].tmpref.buffer = buf;
272
273 for (n = 0; n < sizeof(buf); n++)
274 buf[n] = n + 1;
275 for (n = 0; n < sizeof(buf); n++)
276 exp_sum += buf[n];
277
278 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
279 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
280 goto out;
281
282 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
283out:
284 TEEC_CloseSession(&session);
285}
Jens Wiklander14f48872018-06-29 15:30:13 +0200286ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200287
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100288struct test_1003_arg {
289 uint32_t test_type;
290 size_t repeat;
291 size_t max_before_lockers;
292 size_t max_during_lockers;
293 size_t before_lockers;
294 size_t during_lockers;
295 TEEC_Result res;
296 uint32_t error_orig;
297};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200298
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100299static void *test_1003_thread(void *arg)
300{
301 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100302 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100303 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100304 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100305
306 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
307 NULL, &a->error_orig);
308 if (a->res != TEEC_SUCCESS)
309 return NULL;
310
311 for (n = 0; n < a->repeat; n++) {
312 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
313
314 op.params[0].value.a = a->test_type;
315 op.params[0].value.b = rounds;
316
317 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
318 TEEC_VALUE_OUTPUT,
319 TEEC_NONE, TEEC_NONE);
320 a->res = TEEC_InvokeCommand(&session,
321 PTA_INVOKE_TESTS_CMD_MUTEX,
322 &op, &a->error_orig);
323 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
324 op.params[1].value.b != 1) {
325 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
326 a->res = TEEC_ERROR_BAD_STATE;
327 a->error_orig = 42;
328 break;
329 }
330
331 if (a->test_type == PTA_MUTEX_TEST_READER) {
332 if (op.params[1].value.a > a->max_before_lockers)
333 a->max_before_lockers = op.params[1].value.a;
334
335 if (op.params[1].value.b > a->max_during_lockers)
336 a->max_during_lockers = op.params[1].value.b;
337
338 a->before_lockers += op.params[1].value.a;
339 a->during_lockers += op.params[1].value.b;
340 }
341 }
342 TEEC_CloseSession(&session);
343
344 return NULL;
345}
346
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100347#define TEST_1003_THREAD_COUNT (3 * 2)
348
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100349static void xtest_tee_test_1003(ADBG_Case_t *c)
350{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100351 TEEC_Result res = TEEC_ERROR_GENERIC;
352 TEEC_Session session = { };
353 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100354 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100355 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100356 size_t max_read_concurrency = 0;
357 size_t max_read_waiters = 0;
358 size_t num_concurrent_read_lockers = 0;
359 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100360 size_t n = 0;
361 size_t nt = TEST_1003_THREAD_COUNT;
362 double mean_read_concurrency = 0;
363 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100364 size_t num_writers = 0;
365 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100366 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100367
368 /* Pseudo TA is optional: warn and nicely exit if not found */
369 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
370 &ret_orig);
371 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
372 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
373 return;
374 }
375 ADBG_EXPECT_TEEC_SUCCESS(c, res);
376 TEEC_CloseSession(&session);
377
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100378 for (n = 0; n < nt; n++) {
379 if (n % 3) {
380 arg[n].test_type = PTA_MUTEX_TEST_READER;
381 num_readers++;
382 } else {
383 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
384 num_writers++;
385 }
386 arg[n].repeat = repeat;
387 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
388 test_1003_thread, arg + n)))
389 nt = n; /* break loop and start cleanup */
390 }
391
392 for (n = 0; n < nt; n++) {
393 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
394 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
395 Do_ADBG_Log("error origin %" PRIu32,
396 arg[n].error_orig);
397 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
398 if (arg[n].max_during_lockers > max_read_concurrency)
399 max_read_concurrency =
400 arg[n].max_during_lockers;
401
402 if (arg[n].max_before_lockers > max_read_waiters)
403 max_read_waiters = arg[n].max_before_lockers;
404
405 num_concurrent_read_lockers += arg[n].during_lockers;
406 num_concurrent_read_waiters += arg[n].before_lockers;
407 }
408 }
409
410 mean_read_concurrency = (double)num_concurrent_read_lockers /
411 (double)(repeat * num_readers);
412 mean_read_waiters = (double)num_concurrent_read_waiters /
413 (double)(repeat * num_readers);
414
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100415 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
416 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100417 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
418 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
419 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
420 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
421}
Jens Wiklander14f48872018-06-29 15:30:13 +0200422ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
423 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200424
Pascal Brandc639ac82015-07-02 08:53:34 +0200425static void xtest_tee_test_1004(ADBG_Case_t *c)
426{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100427 TEEC_Session session = { };
428 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200429 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
430 TA_CRYPT_CMD_AES256ECB_ENC,
431 TA_CRYPT_CMD_AES256ECB_DEC };
432
433 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
434 &session, &crypt_user_ta_uuid,
435 NULL, &ret_orig)))
436 return;
437
438 /* Run the "complete crypto test suite" */
439 xtest_crypto_test(&cs);
440
441 TEEC_CloseSession(&session);
442}
Jens Wiklander14f48872018-06-29 15:30:13 +0200443ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200444
Etienne Carriere92c34422018-02-09 13:11:40 +0100445static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200446{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100447 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200448 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100449 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200450
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300451 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200452 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300453 &ret_orig)))
454 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200455
456 op.params[0].value.a = n;
457 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
458 TEEC_NONE);
459
460 (void)ADBG_EXPECT_TEEC_RESULT(c,
461 TEEC_ERROR_TARGET_DEAD,
462 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
463 &ret_orig));
464
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300465 (void)ADBG_EXPECT_TEEC_RESULT(c,
466 TEEC_ERROR_TARGET_DEAD,
467 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200468 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300469
Pascal Brandc639ac82015-07-02 08:53:34 +0200470 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
471
472 TEEC_CloseSession(&session);
473}
474
Etienne Carriere92c34422018-02-09 13:11:40 +0100475static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
476 size_t size)
477{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100478 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100479 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100480 uint32_t ret_orig = 0;
481 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100482
Etienne Carriere92c34422018-02-09 13:11:40 +0100483 shm.size = size;
484 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
485 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
486 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
487 return;
488
489 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
490 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
491 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200492 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100493
494 op.params[0].value.a = (uint32_t)n;
495 op.params[1].memref.parent = &shm;
496 op.params[1].memref.size = size;
497 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
498 TEEC_NONE, TEEC_NONE);
499
500 (void)ADBG_EXPECT_TEEC_RESULT(c,
501 TEEC_ERROR_TARGET_DEAD,
502 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
503 &ret_orig));
504
505 (void)ADBG_EXPECT_TEEC_RESULT(c,
506 TEEC_ERROR_TARGET_DEAD,
507 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
508 &ret_orig));
509
510 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
511
512 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200513rel_shm:
514 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100515}
516
Pascal Brandc639ac82015-07-02 08:53:34 +0200517static void xtest_tee_test_1005(ADBG_Case_t *c)
518{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100519 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200520#define MAX_SESSIONS 3
521 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100522 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200523
524 for (i = 0; i < MAX_SESSIONS; i++) {
525 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200526 xtest_teec_open_session(&sessions[i],
527 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200528 NULL, &ret_orig)))
529 break;
530 }
531
532 for (; --i >= 0; )
533 TEEC_CloseSession(&sessions[i]);
534}
Jens Wiklander14f48872018-06-29 15:30:13 +0200535ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200536
537static void xtest_tee_test_1006(ADBG_Case_t *c)
538{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100539 TEEC_Session session = { };
540 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200541 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100542 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200543
544 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
545 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
546 &ret_orig)))
547 return;
548
549 op.params[0].tmpref.buffer = buf;
550 op.params[0].tmpref.size = sizeof(buf);
551 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
552 TEEC_NONE, TEEC_NONE);
553
554 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
555 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
556 &ret_orig));
557
558 TEEC_CloseSession(&session);
559}
Jens Wiklander14f48872018-06-29 15:30:13 +0200560ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
561 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200562
563static void xtest_tee_test_1007(ADBG_Case_t *c)
564{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100565 TEEC_Session session = { };
566 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200567
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300568 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200569 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300570 &ret_orig)))
571 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200572
573 (void)ADBG_EXPECT_TEEC_RESULT(c,
574 TEEC_ERROR_TARGET_DEAD,
575 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
576 &ret_orig));
577
578 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
579
580 (void)ADBG_EXPECT_TEEC_RESULT(c,
581 TEEC_ERROR_TARGET_DEAD,
582 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
583 &ret_orig));
584
585 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
586
587 TEEC_CloseSession(&session);
588}
Jens Wiklander14f48872018-06-29 15:30:13 +0200589ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200590
Jerome Forissierf02a2212015-10-29 14:33:35 +0100591#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000592# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800593#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000594# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100595#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000596# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100597#endif
598
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100599static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600600{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100601 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600602
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100603 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100604 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100605 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200606 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
607 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
608 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600609 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200610
Jens Wiklanderb7940892015-10-23 16:02:40 +0200611 return fopen(buf, mode);
612}
613
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100614static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200615{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100616 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100617 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
618 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100619 TEEC_Result res = TEEC_ERROR_GENERIC;
620 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100621 FILE *f = NULL;
622 bool r = false;
623 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100624 size_t sz = 0;
625 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200626
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100627 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
628 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
629 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200630
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100631 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
632 if (!ADBG_EXPECT_NOT_NULL(c, f))
633 goto out;
634 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
635 goto out;
636 sz = ftell(f);
637 rewind(f);
638
639 buf = malloc(sz);
640 if (!ADBG_EXPECT_NOT_NULL(c, buf))
641 goto out;
642
643 fread_res = fread(buf, 1, sz, f);
644 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
645 goto out;
646
Jens Wiklander4441fe22015-10-23 16:53:02 +0200647 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100648 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200649
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100650 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200651
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100652 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
653 TEEC_NONE, TEEC_NONE);
654 op.params[0].tmpref.buffer = buf;
655 op.params[0].tmpref.size = sz;
656
657 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
658 &ret_orig);
659 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
660out:
661 free(buf);
662 if (f)
663 fclose(f);
664 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200665 return r;
666}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100667
668static void test_1008_corrupt_ta(ADBG_Case_t *c)
669{
670 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
671 TEEC_Result res = TEEC_ERROR_GENERIC;
672 TEEC_Session session = { };
673 uint32_t ret_orig = 0;
674
675 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
676 if (res) {
677 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
678 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200679 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100680 return;
681 }
682 TEEC_CloseSession(&session);
683
684 ADBG_EXPECT_TRUE(c,
685 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
686 ADBG_EXPECT_TRUE(c,
687 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
688 ADBG_EXPECT_TRUE(c,
689 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
690 ADBG_EXPECT_TRUE(c,
691 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
692 ADBG_EXPECT_TRUE(c,
693 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
694 ADBG_EXPECT_TRUE(c,
695 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
696 ADBG_EXPECT_TRUE(c,
697 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
698 ADBG_EXPECT_TRUE(c,
699 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
700 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
701 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
702}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200703
Pascal Brandc639ac82015-07-02 08:53:34 +0200704static void xtest_tee_test_1008(ADBG_Case_t *c)
705{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100706 TEEC_Session session = { };
707 TEEC_Session session_crypt = { };
708 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200709
710 Do_ADBG_BeginSubCase(c, "Invoke command");
711 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300712 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200713 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300714 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200715
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300716 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
717 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
718 NULL, &ret_orig));
719 TEEC_CloseSession(&session);
720 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200721
Pascal Brandc639ac82015-07-02 08:53:34 +0200722 }
723 Do_ADBG_EndSubCase(c, "Invoke command");
724
725 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
726 {
727 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
728
729 op.params[0].value.a = 2000;
730 op.paramTypes = TEEC_PARAM_TYPES(
731 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
732
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300733 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200734 xtest_teec_open_session(&session,
735 &os_test_ta_uuid,
736 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300737 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200738
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300739 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
740 TEEC_InvokeCommand(&session,
741 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
742 &op, &ret_orig));
743 TEEC_CloseSession(&session);
744 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200745 }
746 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
747
748 Do_ADBG_BeginSubCase(c, "Create session fail");
749 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100750 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200751
Pascal Brandc639ac82015-07-02 08:53:34 +0200752 for (n = 0; n < 100; n++) {
753 Do_ADBG_Log("n = %zu", n);
754 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
755 xtest_teec_open_session(&session_crypt,
756 &create_fail_test_ta_uuid,
757 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100758 /* level > 0 may be used to detect/debug memory leaks */
759 if (!level)
760 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200761 }
762 }
763 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200764
Jens Wiklander4441fe22015-10-23 16:53:02 +0200765 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100766 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200767 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200768}
Jens Wiklander14f48872018-06-29 15:30:13 +0200769ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
770 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200771
Pascal Brandc639ac82015-07-02 08:53:34 +0200772static void *cancellation_thread(void *arg)
773{
774 /*
775 * Sleep 0.5 seconds before cancellation to make sure that the other
776 * thread is in RPC_WAIT.
777 */
778 (void)usleep(500000);
779 TEEC_RequestCancellation(arg);
780 return NULL;
781}
Pascal Brandc639ac82015-07-02 08:53:34 +0200782
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300783static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
784 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200785{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100786 TEEC_Session session = { };
787 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300788 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200789
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100790 memset(&thr, 0, sizeof(thr));
791
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300792 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200793 {
794 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
795
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300796 if (ADBG_EXPECT_TEEC_SUCCESS(c,
797 xtest_teec_open_session(&session, &os_test_ta_uuid,
798 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200799
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
801 TEEC_ORIGIN_TRUSTED_APP,
802 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200803
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300804 op.params[0].value.a = timeout;
805 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
806 TEEC_NONE,
807 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300808 if (cancel) {
809 (void)ADBG_EXPECT(c, 0,
810 pthread_create(&thr, NULL,
811 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200812
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300813 (void)ADBG_EXPECT_TEEC_RESULT(c,
814 TEEC_ERROR_CANCEL,
815 TEEC_InvokeCommand(&session,
816 TA_OS_TEST_CMD_WAIT,
817 &op,
818 &ret_orig));
819 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820
821 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
822 TEEC_InvokeCommand(&session,
823 TA_OS_TEST_CMD_WAIT,
824 &op,
825 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300826 if (cancel)
827 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300828
829 TEEC_CloseSession(&session);
830 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200831 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832 Do_ADBG_EndSubCase(c, "%s", subcase);
833}
834
835static void xtest_tee_test_1009(ADBG_Case_t *c)
836{
837 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
838 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300839 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300840 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200841}
Jens Wiklander14f48872018-06-29 15:30:13 +0200842ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200843
844static void xtest_tee_test_1010(ADBG_Case_t *c)
845{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100846 unsigned int n = 0;
847 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100848 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200849
850 for (n = 1; n <= 5; n++) {
851 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
852 xtest_tee_test_invalid_mem_access(c, n);
853 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
854 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100855
856 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
857 for (n = 1; n <= 5; n++) {
858 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200859 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100860 n, memref_sz[idx]);
861 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
862 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200863 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100864 n, memref_sz[idx]);
865 }
866 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200867}
Jens Wiklander14f48872018-06-29 15:30:13 +0200868ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
869 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200870
871static void xtest_tee_test_1011(ADBG_Case_t *c)
872{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100873 TEEC_Session session = { };
874 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200875 struct xtest_crypto_session cs = {
876 c, &session, TA_RPC_CMD_CRYPT_SHA256,
877 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
878 TA_RPC_CMD_CRYPT_AES256ECB_DEC
879 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100880 struct xtest_crypto_session cs_privmem = {
881 c, &session,
882 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
883 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
884 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
885 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200886 TEEC_UUID uuid = rpc_test_ta_uuid;
887
888 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
889 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
890 return;
891
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100892 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200893 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100894 * Run the "complete crypto test suite" using TA-to-TA
895 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200896 */
897 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100898 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
899
900 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
901 /*
902 * Run the "complete crypto test suite" using TA-to-TA
903 * communication via TA private memory.
904 */
905 xtest_crypto_test(&cs_privmem);
906 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
907
Pascal Brandc639ac82015-07-02 08:53:34 +0200908 TEEC_CloseSession(&session);
909}
Jens Wiklander14f48872018-06-29 15:30:13 +0200910ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
911 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200912
913/*
914 * Note that this test is failing when
915 * - running twice in a raw
916 * - and the user TA is statically linked
917 * This is because the counter is not reseted when opening the first session
918 * in case the TA is statically linked
919 */
920static void xtest_tee_test_1012(ADBG_Case_t *c)
921{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100922 TEEC_Session session1 = { };
923 TEEC_Session session2 = { };
924 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200925 TEEC_UUID uuid = sims_test_ta_uuid;
926
927 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
928 {
929 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
930 static const uint8_t in[] = {
931 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
932 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
933 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
934 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
935 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100936 uint8_t out[32] = { };
937 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200938
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300939 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200940 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300941 &ret_orig)))
942 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200943
944 op.params[0].value.a = 0;
945 op.params[1].tmpref.buffer = (void *)in;
946 op.params[1].tmpref.size = sizeof(in);
947 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
948 TEEC_MEMREF_TEMP_INPUT,
949 TEEC_NONE, TEEC_NONE);
950
951 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
952 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
953 &ret_orig));
954
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100955 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300956 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200957 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300958 &ret_orig)))
959 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200960
961 op.params[0].value.a = 0;
962 op.params[1].tmpref.buffer = out;
963 op.params[1].tmpref.size = sizeof(out);
964 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
965 TEEC_MEMREF_TEMP_OUTPUT,
966 TEEC_NONE, TEEC_NONE);
967
968 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
969 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
970 &op, &ret_orig));
971
972 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
973 sizeof(out))) {
974 Do_ADBG_Log("in:");
975 Do_ADBG_HexLog(in, sizeof(in), 16);
976 Do_ADBG_Log("out:");
977 Do_ADBG_HexLog(out, sizeof(out), 16);
978 }
979
980 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
981 TEEC_NONE, TEEC_NONE,
982 TEEC_NONE);
983
984 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
985 TEEC_InvokeCommand(&session1,
986 TA_SIMS_CMD_GET_COUNTER,
987 &op, &ret_orig));
988
989 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
990
991 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
992 TEEC_InvokeCommand(&session2,
993 TA_SIMS_CMD_GET_COUNTER, &op,
994 &ret_orig));
995
996 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
997 TEEC_CloseSession(&session2);
998 }
999
1000 memset(out, 0, sizeof(out));
1001 op.params[0].value.a = 0;
1002 op.params[1].tmpref.buffer = out;
1003 op.params[1].tmpref.size = sizeof(out);
1004 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1005 TEEC_MEMREF_TEMP_OUTPUT,
1006 TEEC_NONE, TEEC_NONE);
1007
1008 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1009 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1010 &ret_orig));
1011
1012 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1013 Do_ADBG_Log("in:");
1014 Do_ADBG_HexLog(in, sizeof(in), 16);
1015 Do_ADBG_Log("out:");
1016 Do_ADBG_HexLog(out, sizeof(out), 16);
1017 }
1018
1019 TEEC_CloseSession(&session1);
1020 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001021 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001022}
Jens Wiklander14f48872018-06-29 15:30:13 +02001023ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1024 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001025
1026struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001027 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001028 uint32_t cmd;
1029 uint32_t repeat;
1030 TEEC_SharedMemory *shm;
1031 uint32_t error_orig;
1032 TEEC_Result res;
1033 uint32_t max_concurrency;
1034 const uint8_t *in;
1035 size_t in_len;
1036 uint8_t *out;
1037 size_t out_len;
1038};
1039
1040static void *test_1013_thread(void *arg)
1041{
1042 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001043 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001044 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1045 uint8_t p2 = TEEC_NONE;
1046 uint8_t p3 = TEEC_NONE;
1047
Jens Wiklander70672972016-04-06 00:01:45 +02001048 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001049 &a->error_orig);
1050 if (a->res != TEEC_SUCCESS)
1051 return NULL;
1052
1053 op.params[0].memref.parent = a->shm;
1054 op.params[0].memref.size = a->shm->size;
1055 op.params[0].memref.offset = 0;
1056 op.params[1].value.a = a->repeat;
1057 op.params[1].value.b = 0;
1058 op.params[2].tmpref.buffer = (void *)a->in;
1059 op.params[2].tmpref.size = a->in_len;
1060 op.params[3].tmpref.buffer = a->out;
1061 op.params[3].tmpref.size = a->out_len;
1062
1063 if (a->in_len)
1064 p2 = TEEC_MEMREF_TEMP_INPUT;
1065 if (a->out_len)
1066 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1067
1068 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1069 TEEC_VALUE_INOUT, p2, p3);
1070
1071 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1072 a->max_concurrency = op.params[1].value.b;
1073 a->out_len = op.params[3].tmpref.size;
1074 TEEC_CloseSession(&session);
1075 return NULL;
1076}
1077
Pascal Brand4fa35582015-12-17 10:59:12 +01001078#define NUM_THREADS 3
1079
Jens Wiklander70672972016-04-06 00:01:45 +02001080static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1081 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001082{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001083 size_t nt = 0;
1084 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001085 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001086 TEEC_SharedMemory shm = { };
1087 size_t max_concurrency = 0;
1088 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001089 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1090 static const uint8_t sha256_out[] = {
1091 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1092 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1093 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1094 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1095 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001096 uint8_t out[32] = { };
1097 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001098
Jens Wiklander70672972016-04-06 00:01:45 +02001099 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001100 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001102 shm.size = sizeof(struct ta_concurrent_shm);
1103 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1104 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1105 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1106 return;
1107
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001108 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001109 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001110 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001111
1112 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001113 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001114 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001115 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001116 arg[n].shm = &shm;
1117 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1118 test_1013_thread, arg + n)))
1119 nt = n; /* break loop and start cleanup */
1120 }
1121
1122 for (n = 0; n < nt; n++) {
1123 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1124 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1125 if (arg[n].max_concurrency > max_concurrency)
1126 max_concurrency = arg[n].max_concurrency;
1127 }
1128
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001129 /*
1130 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001131 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001132 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1133 * best result there).
1134 */
1135 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001136 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001137 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001138 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001139
Jens Wiklander70672972016-04-06 00:01:45 +02001140 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001141 memset(shm.buffer, 0, shm.size);
1142 memset(arg, 0, sizeof(arg));
1143 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001144 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001145
1146 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001147 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001148 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001149 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001150 arg[n].shm = &shm;
1151 arg[n].in = sha256_in;
1152 arg[n].in_len = sizeof(sha256_in);
1153 arg[n].out = out;
1154 arg[n].out_len = sizeof(out);
1155 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1156 test_1013_thread, arg + n)))
1157 nt = n; /* break loop and start cleanup */
1158 }
1159
1160 for (n = 0; n < nt; n++) {
1161 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1162 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1163 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1164 arg[n].out, arg[n].out_len);
1165 if (arg[n].max_concurrency > max_concurrency)
1166 max_concurrency = arg[n].max_concurrency;
1167 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001168 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001169 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001170
Pascal Brand4fa35582015-12-17 10:59:12 +01001171 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001172 TEEC_ReleaseSharedMemory(&shm);
1173}
Pascal Brand4fa35582015-12-17 10:59:12 +01001174
1175static void xtest_tee_test_1013(ADBG_Case_t *c)
1176{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001177 int i = 0;
1178 double mean_concurrency = 0;
1179 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001180 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001181
1182 if (level == 0)
1183 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001184
Jens Wiklander70672972016-04-06 00:01:45 +02001185 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001186 mean_concurrency = 0;
1187 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001188 xtest_tee_test_1013_single(c, &concurrency,
1189 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001190 mean_concurrency += concurrency;
1191 }
1192 mean_concurrency /= nb_loops;
1193
1194 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1195 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001196 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001197
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001198#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001199 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1200 mean_concurrency = 0;
1201 for (i = 0; i < nb_loops; i++) {
1202 xtest_tee_test_1013_single(c, &concurrency,
1203 &concurrent_large_ta_uuid);
1204 mean_concurrency += concurrency;
1205 }
1206 mean_concurrency /= nb_loops;
1207
1208 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1209 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1210 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001211#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001212}
Jens Wiklander14f48872018-06-29 15:30:13 +02001213ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001214 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001215
1216#ifdef CFG_SECURE_DATA_PATH
1217static void xtest_tee_test_1014(ADBG_Case_t *c)
1218{
1219 UNUSED(c);
1220
1221 int size = 17000;
1222 int loop = 10;
1223 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1224 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001225 int test = 0;
1226 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001227
1228 test = TEST_NS_TO_TA;
1229 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001230 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001231 ADBG_EXPECT(c, 0, ret);
1232 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1233
1234 test = TEST_TA_TO_TA;
1235 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001236 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001237 ADBG_EXPECT(c, 0, ret);
1238 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1239
1240 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001241 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001242 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001243 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001244 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001245
1246 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001247 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001248 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001249 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001250 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001251
1252 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
1253 ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
1254 ADBG_EXPECT(c, 0, ret);
1255 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001256}
Jens Wiklander14f48872018-06-29 15:30:13 +02001257ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1258 "Test secure data path against SDP TAs and pTAs");
1259#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001260
1261static void xtest_tee_test_1015(ADBG_Case_t *c)
1262{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001263 TEEC_Result res = TEEC_ERROR_GENERIC;
1264 TEEC_Session session = { };
1265 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001266
Etienne Carriere11093162017-10-26 09:49:04 +02001267 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001268 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1269 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001270 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1271 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001272 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001273 }
1274 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001275
1276 ADBG_EXPECT_TEEC_SUCCESS(c,
1277 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1278 NULL, &ret_orig));
1279 TEEC_CloseSession(&session);
1280}
Jens Wiklander14f48872018-06-29 15:30:13 +02001281ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1282 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001283
1284static void xtest_tee_test_1016(ADBG_Case_t *c)
1285{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001286 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001287 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001288 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001289
1290 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1291 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1292 &ret_orig)))
1293 return;
1294
1295 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1296 TEEC_NONE);
1297
1298 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1299 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1300 &ret_orig));
1301
1302 TEEC_CloseSession(&session);
1303}
Jens Wiklander14f48872018-06-29 15:30:13 +02001304ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1305 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001306
1307static void xtest_tee_test_1017(ADBG_Case_t *c)
1308{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001309 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001310 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001311 uint32_t ret_orig = 0;
1312 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001313 size_t page_size = 4096;
1314
Jens Wiklander87e81702018-03-20 12:00:00 +08001315 shm.size = 8 * page_size;
1316 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1317 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1318 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1319 return;
1320
1321 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1322 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1323 &ret_orig)))
1324 goto out;
1325
1326 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1327 TEEC_MEMREF_PARTIAL_INPUT,
1328 TEEC_MEMREF_PARTIAL_OUTPUT,
1329 TEEC_MEMREF_PARTIAL_OUTPUT);
1330
1331 /*
1332 * The first two memrefs are supposed to be combined into in
1333 * region and the last two memrefs should have one region each
1334 * when the parameters are mapped for the TA.
1335 */
1336 op.params[0].memref.parent = &shm;
1337 op.params[0].memref.size = page_size;
1338 op.params[0].memref.offset = 0;
1339
1340 op.params[1].memref.parent = &shm;
1341 op.params[1].memref.size = page_size;
1342 op.params[1].memref.offset = page_size;
1343
1344 op.params[2].memref.parent = &shm;
1345 op.params[2].memref.size = page_size;
1346 op.params[2].memref.offset = 4 * page_size;
1347
1348 op.params[3].memref.parent = &shm;
1349 op.params[3].memref.size = 2 * page_size;
1350 op.params[3].memref.offset = 6 * page_size;
1351
1352 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1353 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1354 &ret_orig));
1355
1356 TEEC_CloseSession(&session);
1357out:
1358 TEEC_ReleaseSharedMemory(&shm);
1359}
Jens Wiklander14f48872018-06-29 15:30:13 +02001360ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1361 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001362
Etienne Carriere84382b32018-04-25 18:30:30 +02001363static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1364 TEEC_SharedMemory *shm)
1365{
1366 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1367 TEEC_Result ret = TEEC_ERROR_GENERIC;
1368 uint32_t ret_orig = 0;
1369
1370 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1371 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1372
1373 op.params[0].memref.parent = shm;
1374 op.params[0].memref.size = shm->size / 2;
1375 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1376
1377 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1378 &op, &ret_orig);
1379
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001380 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001381 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1382 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1383 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1384 }
1385}
1386
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001387static void xtest_tee_test_1018(ADBG_Case_t *c)
1388{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001389 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001390 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001391 uint32_t ret_orig = 0;
1392 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001393 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001394 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001395 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001396 uint8_t buffer[6001] = { };
1397
1398 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1399 xtest_teec_open_session(&session,
1400 &os_test_ta_uuid,
1401 NULL,
1402 &ret_orig)))
1403 return;
1404
Joakim Becha1212b62020-04-07 12:06:00 +02001405 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001406
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001407 shm.size = 8 * page_size;
1408 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1409 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001410 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1411 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001412 goto out;
1413
1414 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1415 TEEC_MEMREF_PARTIAL_INPUT,
1416 TEEC_MEMREF_PARTIAL_OUTPUT,
1417 TEEC_MEMREF_PARTIAL_OUTPUT);
1418
1419 /*
1420 * The first two memrefs are supposed to be combined into in
1421 * region and the last two memrefs should have one region each
1422 * when the parameters are mapped for the TA.
1423 */
1424 op.params[0].memref.parent = &shm;
1425 op.params[0].memref.size = page_size;
1426 op.params[0].memref.offset = 0;
1427
1428 op.params[1].memref.parent = &shm;
1429 op.params[1].memref.size = page_size;
1430 op.params[1].memref.offset = page_size;
1431
1432 op.params[2].memref.parent = &shm;
1433 op.params[2].memref.size = page_size;
1434 op.params[2].memref.offset = 4 * page_size;
1435
1436 op.params[3].memref.parent = &shm;
1437 op.params[3].memref.size = 3 * page_size;
1438 op.params[3].memref.offset = 6 * page_size;
1439
Etienne Carriere84382b32018-04-25 18:30:30 +02001440 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1441 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001442
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001443 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001444 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1445 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1446 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1447 }
1448
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001449 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001450 Do_ADBG_EndSubCase(c, NULL);
1451
1452 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1453
1454 memset(&shm, 0, sizeof(shm));
1455 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1456 shm.buffer = buffer;
1457 shm.size = sizeof(buffer);
1458
1459 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1460 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1461 &shm)))
1462 goto out;
1463
1464 invoke_1byte_out_of_bounds(c, &session, &shm);
1465
1466 TEEC_ReleaseSharedMemory(&shm);
1467 Do_ADBG_EndSubCase(c, NULL);
1468
1469 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1470
1471 memset(&shm, 0, sizeof(shm));
1472 shm.size = sizeof(buffer);
1473 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1474 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1475 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1476 &shm)))
1477 goto out;
1478
1479 invoke_1byte_out_of_bounds(c, &session, &shm);
1480
1481 TEEC_ReleaseSharedMemory(&shm);
1482 Do_ADBG_EndSubCase(c, NULL);
1483
1484out:
1485 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001486}
Jens Wiklander14f48872018-06-29 15:30:13 +02001487ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1488 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001489
Jerome Forissier53bde722018-05-31 09:14:54 +02001490static void xtest_tee_test_1019(ADBG_Case_t *c)
1491{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001492 TEEC_Session session = { };
1493 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001494
1495 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1496 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1497 &ret_orig)))
1498 return;
1499
1500 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1501 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1502 &ret_orig));
1503
1504 (void)ADBG_EXPECT_TEEC_RESULT(c,
1505 TEEC_ERROR_TARGET_DEAD,
1506 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1507 NULL, &ret_orig));
1508
1509 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1510
1511 TEEC_CloseSession(&session);
1512}
Jens Wiklander14f48872018-06-29 15:30:13 +02001513ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1514 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001515
1516static void xtest_tee_test_1020(ADBG_Case_t *c)
1517{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001518 TEEC_Result res = TEEC_ERROR_GENERIC;
1519 TEEC_Session session = { };
1520 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001521
1522 /* Pseudo TA is optional: warn and nicely exit if not found */
1523 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1524 &ret_orig);
1525 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1526 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1527 return;
1528 }
1529 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1530
1531 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1532 NULL, &ret_orig);
1533 if (res != TEEC_SUCCESS) {
1534 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1535 ret_orig);
1536 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1537 Do_ADBG_Log(" - 1020 - skip test, feature not "
1538 "implemented");
1539 goto out;
1540 }
1541 /* Error */
1542 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1543 }
1544out:
1545 TEEC_CloseSession(&session);
1546}
1547ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1548 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001549
1550static TEEC_Result open_sec_session(TEEC_Session *session,
1551 const TEEC_UUID *uuid)
1552{
1553 TEEC_Result res = TEEC_ERROR_GENERIC;
1554 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1555 uint32_t ret_orig = 0;
1556
1557 op.params[0].tmpref.buffer = (void *)uuid;
1558 op.params[0].tmpref.size = sizeof(*uuid);
1559 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1560 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1561
1562 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1563 &op, &ret_orig);
1564 if (res != TEEC_SUCCESS)
1565 return TEEC_ERROR_GENERIC;
1566
1567 return res;
1568}
1569
1570static TEEC_Result sims_get_counter(TEEC_Session *session,
1571 uint32_t *counter)
1572{
1573 TEEC_Result res = TEEC_ERROR_GENERIC;
1574 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1575 uint32_t ret_orig = 0;
1576
1577 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1578 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1579
1580 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1581 &op, &ret_orig);
1582 if (res == TEEC_SUCCESS)
1583 *counter = op.params[0].value.a;
1584
1585 return res;
1586}
1587
1588static TEEC_Result trigger_panic(TEEC_Session *session,
1589 const TEEC_UUID *uuid)
1590{
1591 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1592 uint32_t ret_orig = 0;
1593
1594 if (!uuid) {
1595 op.params[0].tmpref.buffer = NULL;
1596 op.params[0].tmpref.size = 0;
1597 } else {
1598 op.params[0].tmpref.buffer = (void *)uuid;
1599 op.params[0].tmpref.size = sizeof(*uuid);
1600 }
1601 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1602 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1603
1604 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1605 &op, &ret_orig);
1606}
1607
1608static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1609 bool multi_instance)
1610{
1611 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1612 uint32_t counter = 0;
1613 uint32_t ret_orig = 0;
1614 uint32_t exp_counter = 0;
1615 TEEC_Session cs[3] = { };
1616
1617 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1618 xtest_teec_open_session(&cs[0], uuid, NULL,
1619 &ret_orig)))
1620 return;
1621
1622 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1623 xtest_teec_open_session(&cs[1], uuid, NULL,
1624 &ret_orig)))
1625 goto bail0;
1626
1627 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1628 goto bail1;
1629
1630 if (!ADBG_EXPECT(c, 0, counter))
1631 goto bail1;
1632
1633 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1634 goto bail1;
1635
1636 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001637 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001638 goto bail1;
1639
1640 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1641 trigger_panic(&cs[1], NULL)))
1642 goto bail1;
1643
1644 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1645 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1646 sims_get_counter(&cs[0], &counter)))
1647 goto bail1;
1648
1649 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1650 sims_get_counter(&cs[1], &counter)))
1651 goto bail1;
1652
1653 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001654 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001655 xtest_teec_open_session(&cs[1], uuid, NULL,
1656 &ret_orig)))
1657 goto bail1;
1658
1659 /* Sanity check of still valid TA context */
1660 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1661 xtest_teec_open_session(&cs[2], uuid, NULL,
1662 &ret_orig)))
1663 goto bail1;
1664
1665 /* Sanity check of still valid TA context */
1666 if (multi_instance) {
1667 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1668 sims_get_counter(&cs[0], &counter)))
1669 goto bail2;
1670
1671 if (!ADBG_EXPECT(c, 0, counter))
1672 goto bail2;
1673 }
1674
1675 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1676 goto bail2;
1677
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001678 exp_counter = multi_instance ? 0 : 1;
1679 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001680 goto bail2;
1681
1682bail2:
1683 TEEC_CloseSession(&cs[2]);
1684bail1:
1685 TEEC_CloseSession(&cs[1]);
1686bail0:
1687 TEEC_CloseSession(&cs[0]);
1688}
1689
1690static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1691 const TEEC_UUID *uuid2)
1692{
1693 uint32_t ret_orig = 0;
1694 uint32_t counter = 0;
1695 TEEC_Session cs[3] = { };
1696
1697 /* Test pre-conditions */
1698 /* 2.1 - CA opens a session toward TA1 */
1699 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1700 xtest_teec_open_session(&cs[0], uuid1, NULL,
1701 &ret_orig)))
1702 return;
1703
1704 /* 2.2 - CA opens a session toward TA2 */
1705 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1706 xtest_teec_open_session(&cs[1], uuid2, NULL,
1707 &ret_orig)))
1708 goto bail0;
1709
1710 /* 2.3 - TA1 opens a session toward TA2 */
1711 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1712 goto bail1;
1713
1714 /* 2.4 - CA invokes TA2 which panics */
1715 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1716 trigger_panic(&cs[1], NULL)))
1717 goto bail1;
1718
1719 /* Expected results */
1720 /* 2.5 - Expect CA->TA1 session is still alive */
1721 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1722 goto bail1;
1723
1724 /* 2.6 - Expect CA->TA2 session is properly released */
1725 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1726 sims_get_counter(&cs[1], &counter)))
1727 goto bail1;
1728
1729bail1:
1730 TEEC_CloseSession(&cs[1]);
1731bail0:
1732 TEEC_CloseSession(&cs[0]);
1733
1734 memset(cs, 0, sizeof(cs));
1735
1736 /* Test pre-conditions */
1737 /* 2.1 - CA opens a session toward TA1 */
1738 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1739 xtest_teec_open_session(&cs[0], uuid1, NULL,
1740 &ret_orig)))
1741 return;
1742
1743 /* 2.2 - CA opens a session toward TA2 */
1744 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1745 xtest_teec_open_session(&cs[1], uuid2, NULL,
1746 &ret_orig)))
1747 goto bail2;
1748
1749 /* 2.3 - TA1 opens a session toward TA2 */
1750 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1751 goto bail3;
1752
1753 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1754 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1755 goto bail3;
1756
1757 /* Expected results */
1758 /* 2.5 - Expect CA->TA1 session is still alive */
1759 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1760 goto bail3;
1761
1762 /* 2.6 - Expect CA->TA2 session is properly released */
1763 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1764 sims_get_counter(&cs[1], &counter)))
1765 goto bail3;
1766
1767bail3:
1768 TEEC_CloseSession(&cs[1]);
1769bail2:
1770 TEEC_CloseSession(&cs[0]);
1771}
1772
1773static void xtest_tee_test_1021(ADBG_Case_t *c)
1774{
1775 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1776 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1777 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1778
1779 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1780 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1781 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1782
1783 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1784 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1785 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1786
1787 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1788 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1789 &sims_keepalive_test_ta_uuid);
1790 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1791}
1792ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1793 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001794
1795static void xtest_tee_test_1022(ADBG_Case_t *c)
1796{
1797 TEEC_Session session = { 0 };
1798 uint32_t ret_orig = 0;
1799
1800 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1801 xtest_teec_open_session(&session, &os_test_ta_uuid,
1802 NULL, &ret_orig)))
1803 return;
1804
1805 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1806 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1807 &ret_orig));
1808
1809 (void)ADBG_EXPECT_TEEC_RESULT(c,
1810 TEEC_ERROR_TARGET_DEAD,
1811 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1812 NULL, &ret_orig));
1813
1814 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1815
1816 TEEC_CloseSession(&session);
1817}
1818ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1819 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001820
1821/*
1822 * Testing the ELF initialization (.init_array)
1823 *
1824 * - The TA has a global variable which can also be accessed by the two shared
1825 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1826 * dlopen())
1827 * - The TA and both libraries have initialization functions (declared with the
1828 * "constructor" attribute) which perform the following:
1829 * * The TA multiplies by 10 then adds 1
1830 * * os_test_lib multiplies by 10 then adds 2
1831 * * os_test_lib_dl multiplies by 10 then adds 3
1832 * By testing the variable value we make sure the initializations occurred in
1833 * the correct order.
1834 */
1835static void xtest_tee_test_1023(ADBG_Case_t *c)
1836{
1837 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1838 TEEC_Session session = { 0 };
1839 uint32_t ret_orig = 0;
1840
1841 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1842 TEEC_NONE, TEEC_NONE);
1843
1844 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1845 xtest_teec_open_session(&session, &os_test_ta_uuid,
1846 NULL, &ret_orig)))
1847 return;
1848
1849 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1850 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1851 &ret_orig));
1852
1853 /* Expected: initialization of os_test_lib, then TA */
1854 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1855
1856 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1857 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1858 &ret_orig));
1859
1860 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1861 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1862 &ret_orig));
1863
1864 /* Expected: initialization of os_test_lib_dl */
1865 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1866
1867 TEEC_CloseSession(&session);
1868}
1869ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1870 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001871
1872#ifdef CFG_CORE_TPM_EVENT_LOG
1873static void xtest_tee_test_1024(ADBG_Case_t *c)
1874{
1875 TEEC_Session session = {};
1876 uint32_t ret_orig = 0;
1877
1878 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1879 NULL, &ret_orig);
1880
1881 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1882 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1883 TA_TPM_TEST_GET_LOG,
1884 NULL, &ret_orig));
1885 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1886
1887 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1888 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1889 TA_TPM_TEST_SHORT_BUF,
1890 NULL, &ret_orig));
1891 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1892
1893 TEEC_CloseSession(&session);
1894}
1895
1896ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1897 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1898#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001899
1900static void xtest_tee_test_1025(ADBG_Case_t *c)
1901{
1902 TEEC_Session session = {};
1903 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1904 uint32_t ret_orig = 0;
1905 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001906 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001907 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001908
1909 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1910
1911 memset(&shm, 0, sizeof(shm));
1912 shm.flags = TEEC_MEM_INPUT;
1913 shm.buffer = NULL;
1914 shm.size = 0;
1915
1916 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1917 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1918
1919 memset(&shm, 0, sizeof(shm));
1920 shm.flags = TEEC_MEM_OUTPUT;
1921 shm.buffer = NULL;
1922 shm.size = 0;
1923
1924 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1925 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1926
1927 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001928
Etienne Carrierec602a522020-04-13 18:53:17 +02001929 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001930 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001931 return;
1932 }
1933
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001934 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1935 xtest_teec_open_session(&session,
1936 &os_test_ta_uuid,
1937 NULL, &ret_orig)))
1938 return;
1939
1940 empty_buf = malloc(1);
1941 if (!empty_buf) {
1942 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001943 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001944 }
1945
1946 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1947 TEEC_MEMREF_TEMP_INPUT,
1948 TEEC_MEMREF_TEMP_OUTPUT,
1949 TEEC_MEMREF_TEMP_OUTPUT);
1950
1951 Do_ADBG_BeginSubCase(c,
1952 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1953
1954 op.params[0].tmpref.buffer = empty_buf;
1955 op.params[0].tmpref.size = 0;
1956
1957 op.params[1].tmpref.buffer = NULL;
1958 op.params[1].tmpref.size = 0;
1959
1960 op.params[2].tmpref.buffer = empty_buf;
1961 op.params[2].tmpref.size = 0;
1962
1963 op.params[3].tmpref.buffer = NULL;
1964 op.params[3].tmpref.size = 0;
1965
1966 ADBG_EXPECT(c, TEE_SUCCESS,
1967 TEEC_InvokeCommand(&session,
1968 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1969 &ret_orig));
1970
1971 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1972
1973 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
1974
1975 op.params[0].tmpref.buffer = empty_buf;
1976 op.params[0].tmpref.size = 1;
1977
1978 op.params[1].tmpref.buffer = NULL;
1979 op.params[1].tmpref.size = 0;
1980
1981 op.params[2].tmpref.buffer = empty_buf;
1982 op.params[2].tmpref.size = 0;
1983
1984 op.params[3].tmpref.buffer = NULL;
1985 op.params[3].tmpref.size = 0;
1986
1987 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
1988 TEEC_InvokeCommand(&session,
1989 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1990 &ret_orig));
1991
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001992 TEEC_CloseSession(&session);
1993
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001994 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
1995
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001996 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
1997
1998 /* Pseudo TA is optional: warn and nicely exit if not found */
1999 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2000 &ret_orig);
2001 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2002 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2003 goto out;
2004 }
2005 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2006 goto out;
2007
2008 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2009 TEEC_NONE, TEEC_NONE);
2010 op.params[0].tmpref.buffer = NULL;
2011 op.params[0].tmpref.size = 0;
2012
2013 ADBG_EXPECT(c, TEE_SUCCESS,
2014 TEEC_InvokeCommand(&session,
2015 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2016 &op, &ret_orig));
2017
2018out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002019 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002020out:
2021 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002022 free(empty_buf);
2023}
2024ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2025 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002026
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002027#define TEE_UUID_NS_NAME_SIZE 128
2028
2029/*
2030 * TEE Client UUID name space identifier (UUIDv4)
2031 *
2032 * Value here is random UUID that is allocated as name space identifier for
2033 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2034 */
2035static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2036
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002037/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2038static TEEC_UUID client_uuid_public = { };
2039
2040static void xtest_tee_test_1026(ADBG_Case_t *c)
2041{
2042 TEEC_Result result = TEEC_ERROR_GENERIC;
2043 uint32_t ret_orig = 0;
2044 TEEC_Session session = { };
2045 uint32_t login = UINT32_MAX;
2046 TEEC_UUID client_uuid = { };
2047
2048 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2049 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2050
2051 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2052 return;
2053
2054 result = ta_os_test_cmd_client_identity(&session, &login,
2055 &client_uuid);
2056
2057 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2058 goto out;
2059
2060 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2061
2062 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2063 sizeof(TEEC_UUID));
2064
2065out:
2066 TEEC_CloseSession(&session);
2067}
2068
2069ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2070 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002071
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002072/*
2073 * regression_1027
2074 * Depends on OpenSSL
2075 * Depends on kernel commit "tee: optee: Add support for session login client UUID generation"
2076 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
2077 * Upstream: <put sha-1 here when known>
2078 *
2079 * xtest skips the test when not built with OpenSSL.
2080 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002081static void xtest_tee_test_1027(ADBG_Case_t *c)
2082{
Victor Chong8e070bc2020-05-13 09:59:33 +01002083#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002084 TEEC_Result result = TEEC_ERROR_GENERIC;
2085 uint32_t ret_orig = 0;
2086 TEEC_Session session = { };
2087 uint32_t login = UINT32_MAX;
2088 TEEC_UUID client_uuid = { };
2089 TEEC_UUID expected_client_uuid = { };
2090 TEEC_UUID uuid_ns = { };
2091 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2092
2093 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2094
2095 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2096 return;
2097
2098 sprintf(uuid_name, "uid=%x", geteuid());
2099
2100 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2101 strlen(uuid_name));
2102 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2103 return;
2104
2105 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2106 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2107
2108 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2109 return;
2110
2111 result = ta_os_test_cmd_client_identity(&session, &login,
2112 &client_uuid);
2113
2114 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2115 goto out;
2116
2117 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2118
2119 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2120 sizeof(TEEC_UUID));
2121
2122out:
2123 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002124#else /*!OPENSSL_FOUND*/
2125 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002126 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002127 /* xtest_uuid_v5() depends on OpenSSL */
2128 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2129#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002130}
2131
2132ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2133 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002134
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002135/*
2136 * regression_1028
2137 * Depends on OpenSSL and kernel commit "tee: optee: Add support for session login client UUID generation"
2138 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
2139 * Upstream: <put sha-1 here when known>
2140 *
2141 * xtest skips the test when not built with OpenSSL.
2142 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002143static void xtest_tee_test_1028(ADBG_Case_t *c)
2144{
Victor Chong8e070bc2020-05-13 09:59:33 +01002145#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002146 TEEC_Result result = TEEC_ERROR_GENERIC;
2147 uint32_t ret_orig = 0;
2148 TEEC_Session session = { };
2149 uint32_t login = UINT32_MAX;
2150 TEEC_UUID client_uuid = { };
2151 TEEC_UUID expected_client_uuid = { };
2152 TEEC_UUID uuid_ns = { };
2153 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2154 uint32_t group = 0;
2155
2156 group = getegid();
2157
2158 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2159
2160 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2161 return;
2162
2163 sprintf(uuid_name, "gid=%x", group);
2164
2165 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2166 strlen(uuid_name));
2167 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2168 return;
2169
2170 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2171 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2172
2173 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2174 return;
2175
2176 result = ta_os_test_cmd_client_identity(&session, &login,
2177 &client_uuid);
2178
2179 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2180 goto out;
2181
2182 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2183
2184 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2185 sizeof(TEEC_UUID));
2186
2187out:
2188 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002189#else /*!OPENSSL_FOUND*/
2190 UNUSED(c);
2191 /* xtest_uuid_v5() depends on OpenSSL */
2192 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2193#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002194}
2195
2196ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2197 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002198
2199static void xtest_tee_test_1029(ADBG_Case_t *c)
2200{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002201 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002202 TEEC_Session session = { 0 };
2203 uint32_t ret_orig = 0;
2204
2205 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2206 xtest_teec_open_session(&session, &os_test_ta_uuid,
2207 NULL, &ret_orig)))
2208 return;
2209
2210 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002211 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2212 &ret_orig);
2213 if (res == TEEC_ERROR_NOT_SUPPORTED)
2214 Do_ADBG_Log(" - 1029 - skip test, "
2215 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2216 else
2217 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002218 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2219
2220 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002221 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2222 &ret_orig);
2223 if (res == TEEC_ERROR_NOT_SUPPORTED)
2224 Do_ADBG_Log(" - 1029 - skip test, "
2225 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2226 else
2227 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002228 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2229
2230 TEEC_CloseSession(&session);
2231}
2232ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2233 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002234
2235static void xtest_tee_test_1030(ADBG_Case_t *c)
2236{
2237 TEEC_Session session = { 0 };
2238 uint32_t ret_orig = 0;
2239
2240 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2241 xtest_teec_open_session(&session, &os_test_ta_uuid,
2242 NULL, &ret_orig)))
2243 return;
2244
2245 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2246 ADBG_EXPECT_TEEC_SUCCESS(c,
2247 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2248 &ret_orig));
2249 Do_ADBG_EndSubCase(c, "Before dlopen()");
2250
2251 Do_ADBG_BeginSubCase(c, "After dlopen()");
2252 ADBG_EXPECT_TEEC_SUCCESS(c,
2253 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2254 &ret_orig));
2255 Do_ADBG_EndSubCase(c, "After dlopen()");
2256
2257 TEEC_CloseSession(&session);
2258}
2259ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2260 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002261
2262#ifndef __clang__
2263static void xtest_tee_test_1031(ADBG_Case_t *c)
2264{
2265 TEEC_Result ret = TEE_SUCCESS;
2266 TEEC_Session session = { 0 };
2267 uint32_t ret_orig = 0;
2268
2269 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2270 xtest_teec_open_session(&session, &os_test_ta_uuid,
2271 NULL, &ret_orig)))
2272 return;
2273
2274 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2275 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2276 &ret_orig);
2277 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2278 printf("TA not built with C++ support, skipping C++ tests\n");
2279 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2280 goto out;
2281
2282 }
2283 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2284 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2285
2286 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2287 ADBG_EXPECT_TEEC_SUCCESS(c,
2288 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2289 NULL, &ret_orig));
2290 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2291
2292 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2293 ADBG_EXPECT_TEEC_SUCCESS(c,
2294 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2295 NULL, &ret_orig));
2296 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2297
2298 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2299 ADBG_EXPECT_TEEC_SUCCESS(c,
2300 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2301 &ret_orig));
2302 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2303
2304 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2305 ADBG_EXPECT_TEEC_SUCCESS(c,
2306 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2307 &ret_orig));
2308 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2309out:
2310 TEEC_CloseSession(&session);
2311}
2312ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2313 "Test C++ features");
2314#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002315
2316static void xtest_tee_test_1032(ADBG_Case_t *c)
2317{
2318 TEEC_Result res = TEEC_SUCCESS;
2319 TEEC_Context ctx = { };
2320 TEEC_SharedMemory shm1 = {
2321 .buffer = xtest_tee_test_1032,
2322 .size = 32,
2323 .flags = TEEC_MEM_INPUT,
2324 };
2325 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2326 TEEC_SharedMemory shm2 = {
2327 .buffer = (void *)dummy_data,
2328 .size = sizeof(dummy_data),
2329 .flags = TEEC_MEM_INPUT,
2330 };
2331
2332 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2333 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2334 return;
2335
2336 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2337 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2338 TEEC_ReleaseSharedMemory(&shm1);
2339
2340 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2341 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2342 TEEC_ReleaseSharedMemory(&shm2);
2343
2344 TEEC_FinalizeContext(&ctx);
2345}
2346ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2347 "Register read-only shared memory");