blob: 4bb072584cf0539cf14768baefe6fe07f6a0145f [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00002 * Copyright (c) 2020, ARM Limited. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02003 * Copyright (c) 2014, STMicroelectronics International N.V.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Etienne Carrierea4653552017-01-11 10:04:24 +010015#include <limits.h>
16#include <pthread.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020017#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010018#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020019#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060020#include <sys/stat.h>
21#include <sys/types.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010022#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020023
24#include "xtest_test.h"
25#include "xtest_helpers.h"
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +030026#include "xtest_uuid_helpers.h"
Jens Wiklander4441fe22015-10-23 16:53:02 +020027#include <signed_hdr.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010028#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020029
Etienne Carriere726d8bc2017-03-21 15:45:59 +010030#include <pta_invoke_tests.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020031#include <ta_crypt.h>
32#include <ta_os_test.h>
33#include <ta_create_fail_test.h>
34#include <ta_rpc_test.h>
35#include <ta_sims_test.h>
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +030036#include <ta_miss_test.h>
37#include <ta_sims_keepalive_test.h>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020038#include <ta_concurrent.h>
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +000039#include <ta_tpm_log_test.h>
Etienne Carriere50abf9a2017-03-24 11:33:50 +010040#include <sdp_basic.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010041#include <pta_secstor_ta_mgmt.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010042
43#ifndef MIN
44#define MIN(a, b) ((a) < (b) ? (a) : (b))
45#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020046
Pascal Brandc639ac82015-07-02 08:53:34 +020047struct xtest_crypto_session {
48 ADBG_Case_t *c;
49 TEEC_Session *session;
50 uint32_t cmd_id_sha256;
51 uint32_t cmd_id_aes256ecb_encrypt;
52 uint32_t cmd_id_aes256ecb_decrypt;
53};
54
55static void xtest_crypto_test(struct xtest_crypto_session *cs)
56{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010057 uint32_t ret_orig = 0;
58 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020059 uint8_t crypt_in[16] = { 22, 17 };
60
61 crypt_in[15] = 60;
62
63 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
64 {
65 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
66
67 op.params[0].tmpref.buffer = crypt_in;
68 op.params[0].tmpref.size = sizeof(crypt_in);
69 op.params[1].tmpref.buffer = crypt_out;
70 op.params[1].tmpref.size = sizeof(crypt_out);
71 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
72 TEEC_MEMREF_TEMP_OUTPUT,
73 TEEC_NONE, TEEC_NONE);
74
75 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
76 TEEC_InvokeCommand(cs->session,
77 cs->
78 cmd_id_aes256ecb_encrypt,
79 &op,
80 &ret_orig));
81 }
82 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
83
84 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
85 {
86 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010087 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020088
89 op.params[0].tmpref.buffer = crypt_out;
90 op.params[0].tmpref.size = sizeof(crypt_out);
91 op.params[1].tmpref.buffer = out;
92 op.params[1].tmpref.size = sizeof(out);
93 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
94 TEEC_MEMREF_TEMP_OUTPUT,
95 TEEC_NONE, TEEC_NONE);
96
97 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
98 TEEC_InvokeCommand(cs->session,
99 cs->
100 cmd_id_aes256ecb_decrypt,
101 &op,
102 &ret_orig));
103
104 if (!ADBG_EXPECT(cs->c, 0,
105 memcmp(crypt_in, out, sizeof(crypt_in)))) {
106 Do_ADBG_Log("crypt_in:");
107 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
108 Do_ADBG_Log("out:");
109 Do_ADBG_HexLog(out, sizeof(out), 16);
110 }
111 }
112 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
113
114 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
115 {
116 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
117 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
118 static const uint8_t sha256_out[] = {
119 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
120 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
121 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
122 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
123 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100124 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200125
126 op.params[0].tmpref.buffer = (void *)sha256_in;
127 op.params[0].tmpref.size = sizeof(sha256_in);
128 op.params[1].tmpref.buffer = out;
129 op.params[1].tmpref.size = sizeof(out);
130 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
131 TEEC_MEMREF_TEMP_OUTPUT,
132 TEEC_NONE, TEEC_NONE);
133
134 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
135 TEEC_InvokeCommand(cs->session,
136 cs->
137 cmd_id_sha256,
138 &op,
139 &ret_orig));
140
141 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
142 sizeof(sha256_out)))) {
143 Do_ADBG_Log("sha256_out:");
144 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
145 Do_ADBG_Log("out:");
146 Do_ADBG_HexLog(out, sizeof(out), 16);
147 }
148 }
149 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
150
Etienne Carrierea3198522017-10-26 09:48:55 +0200151 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200152 {
153 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
154 static const uint8_t in[] = {
155 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
156 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
157 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
158 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
159 };
160 static const uint8_t exp_out[] = {
161 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
162 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
163 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
164 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
165 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100166 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200167
168 op.params[0].tmpref.buffer = (void *)in;
169 op.params[0].tmpref.size = sizeof(in);
170 op.params[1].tmpref.buffer = out;
171 op.params[1].tmpref.size = sizeof(out);
172 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
173 TEEC_MEMREF_TEMP_OUTPUT,
174 TEEC_NONE, TEEC_NONE);
175
176 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
177 TEEC_InvokeCommand(cs->session,
178 cs->
179 cmd_id_aes256ecb_encrypt,
180 &op,
181 &ret_orig));
182
183 if (!ADBG_EXPECT(cs->c, 0,
184 memcmp(exp_out, out, sizeof(exp_out)))) {
185 Do_ADBG_Log("exp_out:");
186 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
187 Do_ADBG_Log("out:");
188 Do_ADBG_HexLog(out, sizeof(out), 16);
189 }
190 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200191 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200192
Etienne Carrierea3198522017-10-26 09:48:55 +0200193 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200194 {
195 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
196 static const uint8_t in[] = {
197 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
198 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
199 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
200 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
201 };
202 static const uint8_t exp_out[] = {
203 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
204 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
205 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
206 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
207 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100208 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200209
210 op.params[0].tmpref.buffer = (void *)in;
211 op.params[0].tmpref.size = sizeof(in);
212 op.params[1].tmpref.buffer = out;
213 op.params[1].tmpref.size = sizeof(out);
214 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
215 TEEC_MEMREF_TEMP_OUTPUT,
216 TEEC_NONE, TEEC_NONE);
217
218 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
219 TEEC_InvokeCommand(cs->session,
220 cs->
221 cmd_id_aes256ecb_decrypt,
222 &op,
223 &ret_orig));
224
225 if (!ADBG_EXPECT(cs->c, 0,
226 memcmp(exp_out, out, sizeof(exp_out)))) {
227 Do_ADBG_Log("exp_out:");
228 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
229 Do_ADBG_Log("out:");
230 Do_ADBG_HexLog(out, sizeof(out), 16);
231 }
232 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200233 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200234}
235
236static void xtest_tee_test_1001(ADBG_Case_t *c)
237{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100238 TEEC_Result res = TEEC_ERROR_GENERIC;
239 TEEC_Session session = { };
240 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200241
Etienne Carriere11093162017-10-26 09:49:04 +0200242 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100243 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100244 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200245 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
246 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100247 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200248 }
249 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200250
Jens Wiklandercf16e842016-02-10 09:07:09 +0100251 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100252 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100253 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200254}
Jens Wiklander14f48872018-06-29 15:30:13 +0200255ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200256
Jens Wiklander1d70a112017-10-16 15:16:39 +0200257static void xtest_tee_test_1002(ADBG_Case_t *c)
258{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100259 TEEC_Result res = TEEC_ERROR_GENERIC;
260 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200261 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100262 uint32_t ret_orig = 0;
263 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200264 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100265 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200266
Etienne Carriere11093162017-10-26 09:49:04 +0200267 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200268 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
269 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200270 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
271 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200272 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200273 }
274 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200275
276 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
277 TEEC_NONE, TEEC_NONE);
278 op.params[0].tmpref.size = sizeof(buf);
279 op.params[0].tmpref.buffer = buf;
280
281 for (n = 0; n < sizeof(buf); n++)
282 buf[n] = n + 1;
283 for (n = 0; n < sizeof(buf); n++)
284 exp_sum += buf[n];
285
286 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
287 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
288 goto out;
289
290 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
291out:
292 TEEC_CloseSession(&session);
293}
Jens Wiklander14f48872018-06-29 15:30:13 +0200294ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200295
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100296struct test_1003_arg {
297 uint32_t test_type;
298 size_t repeat;
299 size_t max_before_lockers;
300 size_t max_during_lockers;
301 size_t before_lockers;
302 size_t during_lockers;
303 TEEC_Result res;
304 uint32_t error_orig;
305};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200306
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100307static void *test_1003_thread(void *arg)
308{
309 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100310 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100311 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100312 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100313
314 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
315 NULL, &a->error_orig);
316 if (a->res != TEEC_SUCCESS)
317 return NULL;
318
319 for (n = 0; n < a->repeat; n++) {
320 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
321
322 op.params[0].value.a = a->test_type;
323 op.params[0].value.b = rounds;
324
325 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
326 TEEC_VALUE_OUTPUT,
327 TEEC_NONE, TEEC_NONE);
328 a->res = TEEC_InvokeCommand(&session,
329 PTA_INVOKE_TESTS_CMD_MUTEX,
330 &op, &a->error_orig);
331 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
332 op.params[1].value.b != 1) {
333 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
334 a->res = TEEC_ERROR_BAD_STATE;
335 a->error_orig = 42;
336 break;
337 }
338
339 if (a->test_type == PTA_MUTEX_TEST_READER) {
340 if (op.params[1].value.a > a->max_before_lockers)
341 a->max_before_lockers = op.params[1].value.a;
342
343 if (op.params[1].value.b > a->max_during_lockers)
344 a->max_during_lockers = op.params[1].value.b;
345
346 a->before_lockers += op.params[1].value.a;
347 a->during_lockers += op.params[1].value.b;
348 }
349 }
350 TEEC_CloseSession(&session);
351
352 return NULL;
353}
354
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100355#define TEST_1003_THREAD_COUNT (3 * 2)
356
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100357static void xtest_tee_test_1003(ADBG_Case_t *c)
358{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100359 TEEC_Result res = TEEC_ERROR_GENERIC;
360 TEEC_Session session = { };
361 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100362 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100363 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100364 size_t max_read_concurrency = 0;
365 size_t max_read_waiters = 0;
366 size_t num_concurrent_read_lockers = 0;
367 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100368 size_t n = 0;
369 size_t nt = TEST_1003_THREAD_COUNT;
370 double mean_read_concurrency = 0;
371 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100372 size_t num_writers = 0;
373 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100374 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100375
376 /* Pseudo TA is optional: warn and nicely exit if not found */
377 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
378 &ret_orig);
379 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
380 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
381 return;
382 }
383 ADBG_EXPECT_TEEC_SUCCESS(c, res);
384 TEEC_CloseSession(&session);
385
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100386 for (n = 0; n < nt; n++) {
387 if (n % 3) {
388 arg[n].test_type = PTA_MUTEX_TEST_READER;
389 num_readers++;
390 } else {
391 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
392 num_writers++;
393 }
394 arg[n].repeat = repeat;
395 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
396 test_1003_thread, arg + n)))
397 nt = n; /* break loop and start cleanup */
398 }
399
400 for (n = 0; n < nt; n++) {
401 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
402 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
403 Do_ADBG_Log("error origin %" PRIu32,
404 arg[n].error_orig);
405 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
406 if (arg[n].max_during_lockers > max_read_concurrency)
407 max_read_concurrency =
408 arg[n].max_during_lockers;
409
410 if (arg[n].max_before_lockers > max_read_waiters)
411 max_read_waiters = arg[n].max_before_lockers;
412
413 num_concurrent_read_lockers += arg[n].during_lockers;
414 num_concurrent_read_waiters += arg[n].before_lockers;
415 }
416 }
417
418 mean_read_concurrency = (double)num_concurrent_read_lockers /
419 (double)(repeat * num_readers);
420 mean_read_waiters = (double)num_concurrent_read_waiters /
421 (double)(repeat * num_readers);
422
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100423 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
424 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100425 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
426 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
427 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
428 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
429}
Jens Wiklander14f48872018-06-29 15:30:13 +0200430ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
431 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200432
Pascal Brandc639ac82015-07-02 08:53:34 +0200433static void xtest_tee_test_1004(ADBG_Case_t *c)
434{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100435 TEEC_Session session = { };
436 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200437 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
438 TA_CRYPT_CMD_AES256ECB_ENC,
439 TA_CRYPT_CMD_AES256ECB_DEC };
440
441 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
442 &session, &crypt_user_ta_uuid,
443 NULL, &ret_orig)))
444 return;
445
446 /* Run the "complete crypto test suite" */
447 xtest_crypto_test(&cs);
448
449 TEEC_CloseSession(&session);
450}
Jens Wiklander14f48872018-06-29 15:30:13 +0200451ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200452
Etienne Carriere92c34422018-02-09 13:11:40 +0100453static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200454{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100455 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200456 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100457 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200458
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300459 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200460 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300461 &ret_orig)))
462 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200463
464 op.params[0].value.a = n;
465 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
466 TEEC_NONE);
467
468 (void)ADBG_EXPECT_TEEC_RESULT(c,
469 TEEC_ERROR_TARGET_DEAD,
470 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
471 &ret_orig));
472
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300473 (void)ADBG_EXPECT_TEEC_RESULT(c,
474 TEEC_ERROR_TARGET_DEAD,
475 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200476 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300477
Pascal Brandc639ac82015-07-02 08:53:34 +0200478 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
479
480 TEEC_CloseSession(&session);
481}
482
Etienne Carriere92c34422018-02-09 13:11:40 +0100483static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
484 size_t size)
485{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100486 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100487 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100488 uint32_t ret_orig = 0;
489 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100490
Etienne Carriere92c34422018-02-09 13:11:40 +0100491 shm.size = size;
492 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
493 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
494 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
495 return;
496
497 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
498 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
499 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200500 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100501
502 op.params[0].value.a = (uint32_t)n;
503 op.params[1].memref.parent = &shm;
504 op.params[1].memref.size = size;
505 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
506 TEEC_NONE, TEEC_NONE);
507
508 (void)ADBG_EXPECT_TEEC_RESULT(c,
509 TEEC_ERROR_TARGET_DEAD,
510 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
511 &ret_orig));
512
513 (void)ADBG_EXPECT_TEEC_RESULT(c,
514 TEEC_ERROR_TARGET_DEAD,
515 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
516 &ret_orig));
517
518 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
519
520 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200521rel_shm:
522 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100523}
524
Pascal Brandc639ac82015-07-02 08:53:34 +0200525static void xtest_tee_test_1005(ADBG_Case_t *c)
526{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100527 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200528#define MAX_SESSIONS 3
529 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100530 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200531
532 for (i = 0; i < MAX_SESSIONS; i++) {
533 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200534 xtest_teec_open_session(&sessions[i],
535 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200536 NULL, &ret_orig)))
537 break;
538 }
539
540 for (; --i >= 0; )
541 TEEC_CloseSession(&sessions[i]);
542}
Jens Wiklander14f48872018-06-29 15:30:13 +0200543ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200544
545static void xtest_tee_test_1006(ADBG_Case_t *c)
546{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100547 TEEC_Session session = { };
548 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200549 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100550 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200551
552 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
553 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
554 &ret_orig)))
555 return;
556
557 op.params[0].tmpref.buffer = buf;
558 op.params[0].tmpref.size = sizeof(buf);
559 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
560 TEEC_NONE, TEEC_NONE);
561
562 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
563 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
564 &ret_orig));
565
566 TEEC_CloseSession(&session);
567}
Jens Wiklander14f48872018-06-29 15:30:13 +0200568ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
569 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200570
571static void xtest_tee_test_1007(ADBG_Case_t *c)
572{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100573 TEEC_Session session = { };
574 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200575
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300576 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200577 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300578 &ret_orig)))
579 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200580
581 (void)ADBG_EXPECT_TEEC_RESULT(c,
582 TEEC_ERROR_TARGET_DEAD,
583 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
584 &ret_orig));
585
586 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
587
588 (void)ADBG_EXPECT_TEEC_RESULT(c,
589 TEEC_ERROR_TARGET_DEAD,
590 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
591 &ret_orig));
592
593 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
594
595 TEEC_CloseSession(&session);
596}
Jens Wiklander14f48872018-06-29 15:30:13 +0200597ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200598
Jerome Forissierf02a2212015-10-29 14:33:35 +0100599#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000600# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800601#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000602# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100603#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000604# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100605#endif
606
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100607static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600608{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100609 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600610
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100611 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100612 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100613 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200614 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
615 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
616 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600617 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200618
Jens Wiklanderb7940892015-10-23 16:02:40 +0200619 return fopen(buf, mode);
620}
621
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100622static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200623{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100624 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100625 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
626 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100627 TEEC_Result res = TEEC_ERROR_GENERIC;
628 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100629 FILE *f = NULL;
630 bool r = false;
631 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100632 size_t sz = 0;
633 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200634
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100635 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
636 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
637 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200638
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100639 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
640 if (!ADBG_EXPECT_NOT_NULL(c, f))
641 goto out;
642 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
643 goto out;
644 sz = ftell(f);
645 rewind(f);
646
647 buf = malloc(sz);
648 if (!ADBG_EXPECT_NOT_NULL(c, buf))
649 goto out;
650
651 fread_res = fread(buf, 1, sz, f);
652 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
653 goto out;
654
Jens Wiklander4441fe22015-10-23 16:53:02 +0200655 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200657
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100658 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200659
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100660 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
661 TEEC_NONE, TEEC_NONE);
662 op.params[0].tmpref.buffer = buf;
663 op.params[0].tmpref.size = sz;
664
665 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
666 &ret_orig);
667 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
668out:
669 free(buf);
670 if (f)
671 fclose(f);
672 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200673 return r;
674}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100675
676static void test_1008_corrupt_ta(ADBG_Case_t *c)
677{
678 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
679 TEEC_Result res = TEEC_ERROR_GENERIC;
680 TEEC_Session session = { };
681 uint32_t ret_orig = 0;
682
683 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
684 if (res) {
685 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
686 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200687 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100688 return;
689 }
690 TEEC_CloseSession(&session);
691
692 ADBG_EXPECT_TRUE(c,
693 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
694 ADBG_EXPECT_TRUE(c,
695 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
696 ADBG_EXPECT_TRUE(c,
697 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
698 ADBG_EXPECT_TRUE(c,
699 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
700 ADBG_EXPECT_TRUE(c,
701 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
702 ADBG_EXPECT_TRUE(c,
703 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
704 ADBG_EXPECT_TRUE(c,
705 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
706 ADBG_EXPECT_TRUE(c,
707 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
708 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
709 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
710}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200711
Pascal Brandc639ac82015-07-02 08:53:34 +0200712static void xtest_tee_test_1008(ADBG_Case_t *c)
713{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100714 TEEC_Session session = { };
715 TEEC_Session session_crypt = { };
716 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200717
718 Do_ADBG_BeginSubCase(c, "Invoke command");
719 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300720 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200721 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300722 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200723
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300724 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
725 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
726 NULL, &ret_orig));
727 TEEC_CloseSession(&session);
728 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200729
Pascal Brandc639ac82015-07-02 08:53:34 +0200730 }
731 Do_ADBG_EndSubCase(c, "Invoke command");
732
733 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
734 {
735 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
736
737 op.params[0].value.a = 2000;
738 op.paramTypes = TEEC_PARAM_TYPES(
739 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
740
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300741 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200742 xtest_teec_open_session(&session,
743 &os_test_ta_uuid,
744 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300745 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200746
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300747 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
748 TEEC_InvokeCommand(&session,
749 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
750 &op, &ret_orig));
751 TEEC_CloseSession(&session);
752 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200753 }
754 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
755
756 Do_ADBG_BeginSubCase(c, "Create session fail");
757 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100758 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200759
Pascal Brandc639ac82015-07-02 08:53:34 +0200760 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
761 xtest_teec_open_session(&session_crypt,
762 &create_fail_test_ta_uuid, NULL,
763 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200764 /*
765 * Run this several times to see that there's no memory leakage.
766 */
767 for (n = 0; n < 100; n++) {
768 Do_ADBG_Log("n = %zu", n);
769 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
770 xtest_teec_open_session(&session_crypt,
771 &create_fail_test_ta_uuid,
772 NULL, &ret_orig));
773 }
774 }
775 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200776
Jens Wiklander4441fe22015-10-23 16:53:02 +0200777 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100778 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200779 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200780}
Jens Wiklander14f48872018-06-29 15:30:13 +0200781ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
782 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200783
Pascal Brandc639ac82015-07-02 08:53:34 +0200784static void *cancellation_thread(void *arg)
785{
786 /*
787 * Sleep 0.5 seconds before cancellation to make sure that the other
788 * thread is in RPC_WAIT.
789 */
790 (void)usleep(500000);
791 TEEC_RequestCancellation(arg);
792 return NULL;
793}
Pascal Brandc639ac82015-07-02 08:53:34 +0200794
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300795static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
796 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200797{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100798 TEEC_Session session = { };
799 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200801
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100802 memset(&thr, 0, sizeof(thr));
803
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300804 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200805 {
806 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
807
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300808 if (ADBG_EXPECT_TEEC_SUCCESS(c,
809 xtest_teec_open_session(&session, &os_test_ta_uuid,
810 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200811
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300812 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
813 TEEC_ORIGIN_TRUSTED_APP,
814 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200815
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300816 op.params[0].value.a = timeout;
817 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
818 TEEC_NONE,
819 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820 if (cancel) {
821 (void)ADBG_EXPECT(c, 0,
822 pthread_create(&thr, NULL,
823 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200824
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300825 (void)ADBG_EXPECT_TEEC_RESULT(c,
826 TEEC_ERROR_CANCEL,
827 TEEC_InvokeCommand(&session,
828 TA_OS_TEST_CMD_WAIT,
829 &op,
830 &ret_orig));
831 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832
833 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
834 TEEC_InvokeCommand(&session,
835 TA_OS_TEST_CMD_WAIT,
836 &op,
837 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300838 if (cancel)
839 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300840
841 TEEC_CloseSession(&session);
842 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200843 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300844 Do_ADBG_EndSubCase(c, "%s", subcase);
845}
846
847static void xtest_tee_test_1009(ADBG_Case_t *c)
848{
849 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
850 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300851 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300852 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200853}
Jens Wiklander14f48872018-06-29 15:30:13 +0200854ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200855
856static void xtest_tee_test_1010(ADBG_Case_t *c)
857{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100858 unsigned int n = 0;
859 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100860 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200861
862 for (n = 1; n <= 5; n++) {
863 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
864 xtest_tee_test_invalid_mem_access(c, n);
865 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
866 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100867
868 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
869 for (n = 1; n <= 5; n++) {
870 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200871 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100872 n, memref_sz[idx]);
873 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
874 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200875 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100876 n, memref_sz[idx]);
877 }
878 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200879}
Jens Wiklander14f48872018-06-29 15:30:13 +0200880ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
881 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200882
883static void xtest_tee_test_1011(ADBG_Case_t *c)
884{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100885 TEEC_Session session = { };
886 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200887 struct xtest_crypto_session cs = {
888 c, &session, TA_RPC_CMD_CRYPT_SHA256,
889 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
890 TA_RPC_CMD_CRYPT_AES256ECB_DEC
891 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100892 struct xtest_crypto_session cs_privmem = {
893 c, &session,
894 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
895 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
896 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
897 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200898 TEEC_UUID uuid = rpc_test_ta_uuid;
899
900 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
901 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
902 return;
903
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100904 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200905 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100906 * Run the "complete crypto test suite" using TA-to-TA
907 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200908 */
909 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100910 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
911
912 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
913 /*
914 * Run the "complete crypto test suite" using TA-to-TA
915 * communication via TA private memory.
916 */
917 xtest_crypto_test(&cs_privmem);
918 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
919
Pascal Brandc639ac82015-07-02 08:53:34 +0200920 TEEC_CloseSession(&session);
921}
Jens Wiklander14f48872018-06-29 15:30:13 +0200922ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
923 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200924
925/*
926 * Note that this test is failing when
927 * - running twice in a raw
928 * - and the user TA is statically linked
929 * This is because the counter is not reseted when opening the first session
930 * in case the TA is statically linked
931 */
932static void xtest_tee_test_1012(ADBG_Case_t *c)
933{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100934 TEEC_Session session1 = { };
935 TEEC_Session session2 = { };
936 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200937 TEEC_UUID uuid = sims_test_ta_uuid;
938
939 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
940 {
941 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
942 static const uint8_t in[] = {
943 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
944 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
945 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
946 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
947 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100948 uint8_t out[32] = { };
949 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200950
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300951 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200952 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300953 &ret_orig)))
954 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200955
956 op.params[0].value.a = 0;
957 op.params[1].tmpref.buffer = (void *)in;
958 op.params[1].tmpref.size = sizeof(in);
959 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
960 TEEC_MEMREF_TEMP_INPUT,
961 TEEC_NONE, TEEC_NONE);
962
963 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
964 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
965 &ret_orig));
966
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100967 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300968 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200969 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300970 &ret_orig)))
971 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200972
973 op.params[0].value.a = 0;
974 op.params[1].tmpref.buffer = out;
975 op.params[1].tmpref.size = sizeof(out);
976 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
977 TEEC_MEMREF_TEMP_OUTPUT,
978 TEEC_NONE, TEEC_NONE);
979
980 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
981 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
982 &op, &ret_orig));
983
984 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
985 sizeof(out))) {
986 Do_ADBG_Log("in:");
987 Do_ADBG_HexLog(in, sizeof(in), 16);
988 Do_ADBG_Log("out:");
989 Do_ADBG_HexLog(out, sizeof(out), 16);
990 }
991
992 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
993 TEEC_NONE, TEEC_NONE,
994 TEEC_NONE);
995
996 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
997 TEEC_InvokeCommand(&session1,
998 TA_SIMS_CMD_GET_COUNTER,
999 &op, &ret_orig));
1000
1001 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1002
1003 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1004 TEEC_InvokeCommand(&session2,
1005 TA_SIMS_CMD_GET_COUNTER, &op,
1006 &ret_orig));
1007
1008 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1009 TEEC_CloseSession(&session2);
1010 }
1011
1012 memset(out, 0, sizeof(out));
1013 op.params[0].value.a = 0;
1014 op.params[1].tmpref.buffer = out;
1015 op.params[1].tmpref.size = sizeof(out);
1016 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1017 TEEC_MEMREF_TEMP_OUTPUT,
1018 TEEC_NONE, TEEC_NONE);
1019
1020 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1021 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1022 &ret_orig));
1023
1024 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1025 Do_ADBG_Log("in:");
1026 Do_ADBG_HexLog(in, sizeof(in), 16);
1027 Do_ADBG_Log("out:");
1028 Do_ADBG_HexLog(out, sizeof(out), 16);
1029 }
1030
1031 TEEC_CloseSession(&session1);
1032 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001033 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001034}
Jens Wiklander14f48872018-06-29 15:30:13 +02001035ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1036 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001037
1038struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001039 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001040 uint32_t cmd;
1041 uint32_t repeat;
1042 TEEC_SharedMemory *shm;
1043 uint32_t error_orig;
1044 TEEC_Result res;
1045 uint32_t max_concurrency;
1046 const uint8_t *in;
1047 size_t in_len;
1048 uint8_t *out;
1049 size_t out_len;
1050};
1051
1052static void *test_1013_thread(void *arg)
1053{
1054 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001055 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001056 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1057 uint8_t p2 = TEEC_NONE;
1058 uint8_t p3 = TEEC_NONE;
1059
Jens Wiklander70672972016-04-06 00:01:45 +02001060 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001061 &a->error_orig);
1062 if (a->res != TEEC_SUCCESS)
1063 return NULL;
1064
1065 op.params[0].memref.parent = a->shm;
1066 op.params[0].memref.size = a->shm->size;
1067 op.params[0].memref.offset = 0;
1068 op.params[1].value.a = a->repeat;
1069 op.params[1].value.b = 0;
1070 op.params[2].tmpref.buffer = (void *)a->in;
1071 op.params[2].tmpref.size = a->in_len;
1072 op.params[3].tmpref.buffer = a->out;
1073 op.params[3].tmpref.size = a->out_len;
1074
1075 if (a->in_len)
1076 p2 = TEEC_MEMREF_TEMP_INPUT;
1077 if (a->out_len)
1078 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1079
1080 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1081 TEEC_VALUE_INOUT, p2, p3);
1082
1083 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1084 a->max_concurrency = op.params[1].value.b;
1085 a->out_len = op.params[3].tmpref.size;
1086 TEEC_CloseSession(&session);
1087 return NULL;
1088}
1089
Pascal Brand4fa35582015-12-17 10:59:12 +01001090#define NUM_THREADS 3
1091
Jens Wiklander70672972016-04-06 00:01:45 +02001092static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1093 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001094{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001095 size_t nt = 0;
1096 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001097 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001098 TEEC_SharedMemory shm = { };
1099 size_t max_concurrency = 0;
1100 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1102 static const uint8_t sha256_out[] = {
1103 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1104 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1105 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1106 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1107 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001108 uint8_t out[32] = { };
1109 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001110
Jens Wiklander70672972016-04-06 00:01:45 +02001111 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001112 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001113
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001114 shm.size = sizeof(struct ta_concurrent_shm);
1115 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1116 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1117 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1118 return;
1119
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001120 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001121 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001122 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001123
1124 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001125 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001126 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001127 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001128 arg[n].shm = &shm;
1129 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1130 test_1013_thread, arg + n)))
1131 nt = n; /* break loop and start cleanup */
1132 }
1133
1134 for (n = 0; n < nt; n++) {
1135 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1136 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1137 if (arg[n].max_concurrency > max_concurrency)
1138 max_concurrency = arg[n].max_concurrency;
1139 }
1140
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001141 /*
1142 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001143 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001144 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1145 * best result there).
1146 */
1147 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001148 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001149 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001150 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001151
Jens Wiklander70672972016-04-06 00:01:45 +02001152 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001153 memset(shm.buffer, 0, shm.size);
1154 memset(arg, 0, sizeof(arg));
1155 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001156 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001157
1158 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001159 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001160 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001161 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001162 arg[n].shm = &shm;
1163 arg[n].in = sha256_in;
1164 arg[n].in_len = sizeof(sha256_in);
1165 arg[n].out = out;
1166 arg[n].out_len = sizeof(out);
1167 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1168 test_1013_thread, arg + n)))
1169 nt = n; /* break loop and start cleanup */
1170 }
1171
1172 for (n = 0; n < nt; n++) {
1173 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1174 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1175 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1176 arg[n].out, arg[n].out_len);
1177 if (arg[n].max_concurrency > max_concurrency)
1178 max_concurrency = arg[n].max_concurrency;
1179 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001180 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001181 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001182
Pascal Brand4fa35582015-12-17 10:59:12 +01001183 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001184 TEEC_ReleaseSharedMemory(&shm);
1185}
Pascal Brand4fa35582015-12-17 10:59:12 +01001186
1187static void xtest_tee_test_1013(ADBG_Case_t *c)
1188{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001189 int i = 0;
1190 double mean_concurrency = 0;
1191 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001192 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001193
1194 if (level == 0)
1195 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001196
Jens Wiklander70672972016-04-06 00:01:45 +02001197 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001198 mean_concurrency = 0;
1199 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001200 xtest_tee_test_1013_single(c, &concurrency,
1201 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001202 mean_concurrency += concurrency;
1203 }
1204 mean_concurrency /= nb_loops;
1205
1206 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1207 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001208 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001209
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001210#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001211 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1212 mean_concurrency = 0;
1213 for (i = 0; i < nb_loops; i++) {
1214 xtest_tee_test_1013_single(c, &concurrency,
1215 &concurrent_large_ta_uuid);
1216 mean_concurrency += concurrency;
1217 }
1218 mean_concurrency /= nb_loops;
1219
1220 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1221 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1222 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001223#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001224}
Jens Wiklander14f48872018-06-29 15:30:13 +02001225ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001226 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001227
1228#ifdef CFG_SECURE_DATA_PATH
1229static void xtest_tee_test_1014(ADBG_Case_t *c)
1230{
1231 UNUSED(c);
1232
1233 int size = 17000;
1234 int loop = 10;
1235 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1236 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001237 int test = 0;
1238 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001239
1240 test = TEST_NS_TO_TA;
1241 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
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);
1244 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1245
1246 test = TEST_TA_TO_TA;
1247 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
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, 0, ret);
1250 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1251
1252 test = TEST_TA_TO_PTA;
1253 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001254 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001255 ADBG_EXPECT(c, 0, ret);
1256 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1257
1258 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001259 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001260 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001261 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001262 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001263
1264 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
1265 ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
1266 ADBG_EXPECT(c, 0, ret);
1267 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001268}
Jens Wiklander14f48872018-06-29 15:30:13 +02001269ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1270 "Test secure data path against SDP TAs and pTAs");
1271#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001272
1273static void xtest_tee_test_1015(ADBG_Case_t *c)
1274{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001275 TEEC_Result res = TEEC_ERROR_GENERIC;
1276 TEEC_Session session = { };
1277 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001278
Etienne Carriere11093162017-10-26 09:49:04 +02001279 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001280 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1281 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001282 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1283 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001284 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001285 }
1286 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001287
1288 ADBG_EXPECT_TEEC_SUCCESS(c,
1289 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1290 NULL, &ret_orig));
1291 TEEC_CloseSession(&session);
1292}
Jens Wiklander14f48872018-06-29 15:30:13 +02001293ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1294 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001295
1296static void xtest_tee_test_1016(ADBG_Case_t *c)
1297{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001298 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001299 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001300 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001301
1302 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1303 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1304 &ret_orig)))
1305 return;
1306
1307 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1308 TEEC_NONE);
1309
1310 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1311 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1312 &ret_orig));
1313
1314 TEEC_CloseSession(&session);
1315}
Jens Wiklander14f48872018-06-29 15:30:13 +02001316ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1317 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001318
1319static void xtest_tee_test_1017(ADBG_Case_t *c)
1320{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001321 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001322 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001323 uint32_t ret_orig = 0;
1324 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001325 size_t page_size = 4096;
1326
Jens Wiklander87e81702018-03-20 12:00:00 +08001327 shm.size = 8 * page_size;
1328 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1329 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1330 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1331 return;
1332
1333 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1334 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1335 &ret_orig)))
1336 goto out;
1337
1338 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1339 TEEC_MEMREF_PARTIAL_INPUT,
1340 TEEC_MEMREF_PARTIAL_OUTPUT,
1341 TEEC_MEMREF_PARTIAL_OUTPUT);
1342
1343 /*
1344 * The first two memrefs are supposed to be combined into in
1345 * region and the last two memrefs should have one region each
1346 * when the parameters are mapped for the TA.
1347 */
1348 op.params[0].memref.parent = &shm;
1349 op.params[0].memref.size = page_size;
1350 op.params[0].memref.offset = 0;
1351
1352 op.params[1].memref.parent = &shm;
1353 op.params[1].memref.size = page_size;
1354 op.params[1].memref.offset = page_size;
1355
1356 op.params[2].memref.parent = &shm;
1357 op.params[2].memref.size = page_size;
1358 op.params[2].memref.offset = 4 * page_size;
1359
1360 op.params[3].memref.parent = &shm;
1361 op.params[3].memref.size = 2 * page_size;
1362 op.params[3].memref.offset = 6 * page_size;
1363
1364 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1365 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1366 &ret_orig));
1367
1368 TEEC_CloseSession(&session);
1369out:
1370 TEEC_ReleaseSharedMemory(&shm);
1371}
Jens Wiklander14f48872018-06-29 15:30:13 +02001372ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1373 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001374
Etienne Carriere84382b32018-04-25 18:30:30 +02001375static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1376 TEEC_SharedMemory *shm)
1377{
1378 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1379 TEEC_Result ret = TEEC_ERROR_GENERIC;
1380 uint32_t ret_orig = 0;
1381
1382 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1383 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1384
1385 op.params[0].memref.parent = shm;
1386 op.params[0].memref.size = shm->size / 2;
1387 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1388
1389 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1390 &op, &ret_orig);
1391
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001392 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001393 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1394 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1395 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1396 }
1397}
1398
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001399static void xtest_tee_test_1018(ADBG_Case_t *c)
1400{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001401 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001402 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001403 uint32_t ret_orig = 0;
1404 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001405 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001406 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001407 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001408 uint8_t buffer[6001] = { };
1409
1410 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1411 xtest_teec_open_session(&session,
1412 &os_test_ta_uuid,
1413 NULL,
1414 &ret_orig)))
1415 return;
1416
Joakim Becha1212b62020-04-07 12:06:00 +02001417 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001418
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001419 shm.size = 8 * page_size;
1420 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1421 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001422 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1423 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001424 goto out;
1425
1426 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1427 TEEC_MEMREF_PARTIAL_INPUT,
1428 TEEC_MEMREF_PARTIAL_OUTPUT,
1429 TEEC_MEMREF_PARTIAL_OUTPUT);
1430
1431 /*
1432 * The first two memrefs are supposed to be combined into in
1433 * region and the last two memrefs should have one region each
1434 * when the parameters are mapped for the TA.
1435 */
1436 op.params[0].memref.parent = &shm;
1437 op.params[0].memref.size = page_size;
1438 op.params[0].memref.offset = 0;
1439
1440 op.params[1].memref.parent = &shm;
1441 op.params[1].memref.size = page_size;
1442 op.params[1].memref.offset = page_size;
1443
1444 op.params[2].memref.parent = &shm;
1445 op.params[2].memref.size = page_size;
1446 op.params[2].memref.offset = 4 * page_size;
1447
1448 op.params[3].memref.parent = &shm;
1449 op.params[3].memref.size = 3 * page_size;
1450 op.params[3].memref.offset = 6 * page_size;
1451
Etienne Carriere84382b32018-04-25 18:30:30 +02001452 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1453 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001454
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001455 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001456 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1457 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1458 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1459 }
1460
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001461 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001462 Do_ADBG_EndSubCase(c, NULL);
1463
1464 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1465
1466 memset(&shm, 0, sizeof(shm));
1467 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1468 shm.buffer = buffer;
1469 shm.size = sizeof(buffer);
1470
1471 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1472 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1473 &shm)))
1474 goto out;
1475
1476 invoke_1byte_out_of_bounds(c, &session, &shm);
1477
1478 TEEC_ReleaseSharedMemory(&shm);
1479 Do_ADBG_EndSubCase(c, NULL);
1480
1481 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1482
1483 memset(&shm, 0, sizeof(shm));
1484 shm.size = sizeof(buffer);
1485 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1486 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1487 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1488 &shm)))
1489 goto out;
1490
1491 invoke_1byte_out_of_bounds(c, &session, &shm);
1492
1493 TEEC_ReleaseSharedMemory(&shm);
1494 Do_ADBG_EndSubCase(c, NULL);
1495
1496out:
1497 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001498}
Jens Wiklander14f48872018-06-29 15:30:13 +02001499ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1500 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001501
Victor Chong3ff36f52018-06-07 04:37:00 +01001502#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001503static void xtest_tee_test_1019(ADBG_Case_t *c)
1504{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001505 TEEC_Session session = { };
1506 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001507
1508 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1509 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1510 &ret_orig)))
1511 return;
1512
1513 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1514 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1515 &ret_orig));
1516
1517 (void)ADBG_EXPECT_TEEC_RESULT(c,
1518 TEEC_ERROR_TARGET_DEAD,
1519 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1520 NULL, &ret_orig));
1521
1522 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1523
1524 TEEC_CloseSession(&session);
1525}
Jens Wiklander14f48872018-06-29 15:30:13 +02001526ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1527 "Test dynamically linked TA");
1528#endif /*CFG_TA_DYNLINK*/
Jerome Forissier3357f872018-10-05 15:13:44 +02001529
1530static void xtest_tee_test_1020(ADBG_Case_t *c)
1531{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001532 TEEC_Result res = TEEC_ERROR_GENERIC;
1533 TEEC_Session session = { };
1534 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001535
1536 /* Pseudo TA is optional: warn and nicely exit if not found */
1537 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1538 &ret_orig);
1539 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1540 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1541 return;
1542 }
1543 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1544
1545 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1546 NULL, &ret_orig);
1547 if (res != TEEC_SUCCESS) {
1548 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1549 ret_orig);
1550 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1551 Do_ADBG_Log(" - 1020 - skip test, feature not "
1552 "implemented");
1553 goto out;
1554 }
1555 /* Error */
1556 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1557 }
1558out:
1559 TEEC_CloseSession(&session);
1560}
1561ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1562 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001563
1564static TEEC_Result open_sec_session(TEEC_Session *session,
1565 const TEEC_UUID *uuid)
1566{
1567 TEEC_Result res = TEEC_ERROR_GENERIC;
1568 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1569 uint32_t ret_orig = 0;
1570
1571 op.params[0].tmpref.buffer = (void *)uuid;
1572 op.params[0].tmpref.size = sizeof(*uuid);
1573 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1574 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1575
1576 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1577 &op, &ret_orig);
1578 if (res != TEEC_SUCCESS)
1579 return TEEC_ERROR_GENERIC;
1580
1581 return res;
1582}
1583
1584static TEEC_Result sims_get_counter(TEEC_Session *session,
1585 uint32_t *counter)
1586{
1587 TEEC_Result res = TEEC_ERROR_GENERIC;
1588 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1589 uint32_t ret_orig = 0;
1590
1591 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1592 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1593
1594 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1595 &op, &ret_orig);
1596 if (res == TEEC_SUCCESS)
1597 *counter = op.params[0].value.a;
1598
1599 return res;
1600}
1601
1602static TEEC_Result trigger_panic(TEEC_Session *session,
1603 const TEEC_UUID *uuid)
1604{
1605 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1606 uint32_t ret_orig = 0;
1607
1608 if (!uuid) {
1609 op.params[0].tmpref.buffer = NULL;
1610 op.params[0].tmpref.size = 0;
1611 } else {
1612 op.params[0].tmpref.buffer = (void *)uuid;
1613 op.params[0].tmpref.size = sizeof(*uuid);
1614 }
1615 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1616 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1617
1618 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1619 &op, &ret_orig);
1620}
1621
1622static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1623 bool multi_instance)
1624{
1625 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1626 uint32_t counter = 0;
1627 uint32_t ret_orig = 0;
1628 uint32_t exp_counter = 0;
1629 TEEC_Session cs[3] = { };
1630
1631 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1632 xtest_teec_open_session(&cs[0], uuid, NULL,
1633 &ret_orig)))
1634 return;
1635
1636 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1637 xtest_teec_open_session(&cs[1], uuid, NULL,
1638 &ret_orig)))
1639 goto bail0;
1640
1641 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1642 goto bail1;
1643
1644 if (!ADBG_EXPECT(c, 0, counter))
1645 goto bail1;
1646
1647 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1648 goto bail1;
1649
1650 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001651 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001652 goto bail1;
1653
1654 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1655 trigger_panic(&cs[1], NULL)))
1656 goto bail1;
1657
1658 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1659 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1660 sims_get_counter(&cs[0], &counter)))
1661 goto bail1;
1662
1663 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1664 sims_get_counter(&cs[1], &counter)))
1665 goto bail1;
1666
1667 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001668 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001669 xtest_teec_open_session(&cs[1], uuid, NULL,
1670 &ret_orig)))
1671 goto bail1;
1672
1673 /* Sanity check of still valid TA context */
1674 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1675 xtest_teec_open_session(&cs[2], uuid, NULL,
1676 &ret_orig)))
1677 goto bail1;
1678
1679 /* Sanity check of still valid TA context */
1680 if (multi_instance) {
1681 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1682 sims_get_counter(&cs[0], &counter)))
1683 goto bail2;
1684
1685 if (!ADBG_EXPECT(c, 0, counter))
1686 goto bail2;
1687 }
1688
1689 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1690 goto bail2;
1691
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001692 exp_counter = multi_instance ? 0 : 1;
1693 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001694 goto bail2;
1695
1696bail2:
1697 TEEC_CloseSession(&cs[2]);
1698bail1:
1699 TEEC_CloseSession(&cs[1]);
1700bail0:
1701 TEEC_CloseSession(&cs[0]);
1702}
1703
1704static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1705 const TEEC_UUID *uuid2)
1706{
1707 uint32_t ret_orig = 0;
1708 uint32_t counter = 0;
1709 TEEC_Session cs[3] = { };
1710
1711 /* Test pre-conditions */
1712 /* 2.1 - CA opens a session toward TA1 */
1713 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1714 xtest_teec_open_session(&cs[0], uuid1, NULL,
1715 &ret_orig)))
1716 return;
1717
1718 /* 2.2 - CA opens a session toward TA2 */
1719 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1720 xtest_teec_open_session(&cs[1], uuid2, NULL,
1721 &ret_orig)))
1722 goto bail0;
1723
1724 /* 2.3 - TA1 opens a session toward TA2 */
1725 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1726 goto bail1;
1727
1728 /* 2.4 - CA invokes TA2 which panics */
1729 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1730 trigger_panic(&cs[1], NULL)))
1731 goto bail1;
1732
1733 /* Expected results */
1734 /* 2.5 - Expect CA->TA1 session is still alive */
1735 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1736 goto bail1;
1737
1738 /* 2.6 - Expect CA->TA2 session is properly released */
1739 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1740 sims_get_counter(&cs[1], &counter)))
1741 goto bail1;
1742
1743bail1:
1744 TEEC_CloseSession(&cs[1]);
1745bail0:
1746 TEEC_CloseSession(&cs[0]);
1747
1748 memset(cs, 0, sizeof(cs));
1749
1750 /* Test pre-conditions */
1751 /* 2.1 - CA opens a session toward TA1 */
1752 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1753 xtest_teec_open_session(&cs[0], uuid1, NULL,
1754 &ret_orig)))
1755 return;
1756
1757 /* 2.2 - CA opens a session toward TA2 */
1758 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1759 xtest_teec_open_session(&cs[1], uuid2, NULL,
1760 &ret_orig)))
1761 goto bail2;
1762
1763 /* 2.3 - TA1 opens a session toward TA2 */
1764 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1765 goto bail3;
1766
1767 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1768 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1769 goto bail3;
1770
1771 /* Expected results */
1772 /* 2.5 - Expect CA->TA1 session is still alive */
1773 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1774 goto bail3;
1775
1776 /* 2.6 - Expect CA->TA2 session is properly released */
1777 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1778 sims_get_counter(&cs[1], &counter)))
1779 goto bail3;
1780
1781bail3:
1782 TEEC_CloseSession(&cs[1]);
1783bail2:
1784 TEEC_CloseSession(&cs[0]);
1785}
1786
1787static void xtest_tee_test_1021(ADBG_Case_t *c)
1788{
1789 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1790 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1791 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1792
1793 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1794 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1795 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1796
1797 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1798 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1799 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1800
1801 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1802 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1803 &sims_keepalive_test_ta_uuid);
1804 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1805}
1806ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1807 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001808
1809static void xtest_tee_test_1022(ADBG_Case_t *c)
1810{
1811 TEEC_Session session = { 0 };
1812 uint32_t ret_orig = 0;
1813
1814 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1815 xtest_teec_open_session(&session, &os_test_ta_uuid,
1816 NULL, &ret_orig)))
1817 return;
1818
1819 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1820 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1821 &ret_orig));
1822
1823 (void)ADBG_EXPECT_TEEC_RESULT(c,
1824 TEEC_ERROR_TARGET_DEAD,
1825 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1826 NULL, &ret_orig));
1827
1828 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1829
1830 TEEC_CloseSession(&session);
1831}
1832ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1833 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001834
1835/*
1836 * Testing the ELF initialization (.init_array)
1837 *
1838 * - The TA has a global variable which can also be accessed by the two shared
1839 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1840 * dlopen())
1841 * - The TA and both libraries have initialization functions (declared with the
1842 * "constructor" attribute) which perform the following:
1843 * * The TA multiplies by 10 then adds 1
1844 * * os_test_lib multiplies by 10 then adds 2
1845 * * os_test_lib_dl multiplies by 10 then adds 3
1846 * By testing the variable value we make sure the initializations occurred in
1847 * the correct order.
1848 */
1849static void xtest_tee_test_1023(ADBG_Case_t *c)
1850{
1851 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1852 TEEC_Session session = { 0 };
1853 uint32_t ret_orig = 0;
1854
1855 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1856 TEEC_NONE, TEEC_NONE);
1857
1858 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1859 xtest_teec_open_session(&session, &os_test_ta_uuid,
1860 NULL, &ret_orig)))
1861 return;
1862
1863 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1864 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1865 &ret_orig));
1866
1867 /* Expected: initialization of os_test_lib, then TA */
1868 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1869
1870 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1871 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1872 &ret_orig));
1873
1874 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1875 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1876 &ret_orig));
1877
1878 /* Expected: initialization of os_test_lib_dl */
1879 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1880
1881 TEEC_CloseSession(&session);
1882}
1883ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1884 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001885
1886#ifdef CFG_CORE_TPM_EVENT_LOG
1887static void xtest_tee_test_1024(ADBG_Case_t *c)
1888{
1889 TEEC_Session session = {};
1890 uint32_t ret_orig = 0;
1891
1892 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1893 NULL, &ret_orig);
1894
1895 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1896 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1897 TA_TPM_TEST_GET_LOG,
1898 NULL, &ret_orig));
1899 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1900
1901 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1902 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1903 TA_TPM_TEST_SHORT_BUF,
1904 NULL, &ret_orig));
1905 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1906
1907 TEEC_CloseSession(&session);
1908}
1909
1910ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1911 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1912#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001913
1914static void xtest_tee_test_1025(ADBG_Case_t *c)
1915{
1916 TEEC_Session session = {};
1917 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1918 uint32_t ret_orig = 0;
1919 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001920 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001921 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001922
1923 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1924
1925 memset(&shm, 0, sizeof(shm));
1926 shm.flags = TEEC_MEM_INPUT;
1927 shm.buffer = NULL;
1928 shm.size = 0;
1929
1930 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1931 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1932
1933 memset(&shm, 0, sizeof(shm));
1934 shm.flags = TEEC_MEM_OUTPUT;
1935 shm.buffer = NULL;
1936 shm.size = 0;
1937
1938 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1939 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1940
1941 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001942
Etienne Carrierec602a522020-04-13 18:53:17 +02001943 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001944 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001945 return;
1946 }
1947
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001948 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1949 xtest_teec_open_session(&session,
1950 &os_test_ta_uuid,
1951 NULL, &ret_orig)))
1952 return;
1953
1954 empty_buf = malloc(1);
1955 if (!empty_buf) {
1956 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001957 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001958 }
1959
1960 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1961 TEEC_MEMREF_TEMP_INPUT,
1962 TEEC_MEMREF_TEMP_OUTPUT,
1963 TEEC_MEMREF_TEMP_OUTPUT);
1964
1965 Do_ADBG_BeginSubCase(c,
1966 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1967
1968 op.params[0].tmpref.buffer = empty_buf;
1969 op.params[0].tmpref.size = 0;
1970
1971 op.params[1].tmpref.buffer = NULL;
1972 op.params[1].tmpref.size = 0;
1973
1974 op.params[2].tmpref.buffer = empty_buf;
1975 op.params[2].tmpref.size = 0;
1976
1977 op.params[3].tmpref.buffer = NULL;
1978 op.params[3].tmpref.size = 0;
1979
1980 ADBG_EXPECT(c, TEE_SUCCESS,
1981 TEEC_InvokeCommand(&session,
1982 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1983 &ret_orig));
1984
1985 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1986
1987 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
1988
1989 op.params[0].tmpref.buffer = empty_buf;
1990 op.params[0].tmpref.size = 1;
1991
1992 op.params[1].tmpref.buffer = NULL;
1993 op.params[1].tmpref.size = 0;
1994
1995 op.params[2].tmpref.buffer = empty_buf;
1996 op.params[2].tmpref.size = 0;
1997
1998 op.params[3].tmpref.buffer = NULL;
1999 op.params[3].tmpref.size = 0;
2000
2001 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2002 TEEC_InvokeCommand(&session,
2003 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2004 &ret_orig));
2005
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002006 TEEC_CloseSession(&session);
2007
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002008 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2009
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002010 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2011
2012 /* Pseudo TA is optional: warn and nicely exit if not found */
2013 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2014 &ret_orig);
2015 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2016 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2017 goto out;
2018 }
2019 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2020 goto out;
2021
2022 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2023 TEEC_NONE, TEEC_NONE);
2024 op.params[0].tmpref.buffer = NULL;
2025 op.params[0].tmpref.size = 0;
2026
2027 ADBG_EXPECT(c, TEE_SUCCESS,
2028 TEEC_InvokeCommand(&session,
2029 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2030 &op, &ret_orig));
2031
2032out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002033 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002034out:
2035 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002036 free(empty_buf);
2037}
2038ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2039 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002040
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002041#define TEE_UUID_NS_NAME_SIZE 128
2042
2043/*
2044 * TEE Client UUID name space identifier (UUIDv4)
2045 *
2046 * Value here is random UUID that is allocated as name space identifier for
2047 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2048 */
2049static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2050
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002051/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2052static TEEC_UUID client_uuid_public = { };
2053
2054static void xtest_tee_test_1026(ADBG_Case_t *c)
2055{
2056 TEEC_Result result = TEEC_ERROR_GENERIC;
2057 uint32_t ret_orig = 0;
2058 TEEC_Session session = { };
2059 uint32_t login = UINT32_MAX;
2060 TEEC_UUID client_uuid = { };
2061
2062 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2063 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2064
2065 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2066 return;
2067
2068 result = ta_os_test_cmd_client_identity(&session, &login,
2069 &client_uuid);
2070
2071 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2072 goto out;
2073
2074 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2075
2076 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2077 sizeof(TEEC_UUID));
2078
2079out:
2080 TEEC_CloseSession(&session);
2081}
2082
2083ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2084 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002085
2086static void xtest_tee_test_1027(ADBG_Case_t *c)
2087{
Victor Chong8e070bc2020-05-13 09:59:33 +01002088#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002089 TEEC_Result result = TEEC_ERROR_GENERIC;
2090 uint32_t ret_orig = 0;
2091 TEEC_Session session = { };
2092 uint32_t login = UINT32_MAX;
2093 TEEC_UUID client_uuid = { };
2094 TEEC_UUID expected_client_uuid = { };
2095 TEEC_UUID uuid_ns = { };
2096 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2097
2098 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2099
2100 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2101 return;
2102
2103 sprintf(uuid_name, "uid=%x", geteuid());
2104
2105 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2106 strlen(uuid_name));
2107 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2108 return;
2109
2110 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2111 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2112
2113 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2114 return;
2115
2116 result = ta_os_test_cmd_client_identity(&session, &login,
2117 &client_uuid);
2118
2119 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2120 goto out;
2121
2122 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2123
2124 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2125 sizeof(TEEC_UUID));
2126
2127out:
2128 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002129#else /*!OPENSSL_FOUND*/
2130 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002131 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002132 /* xtest_uuid_v5() depends on OpenSSL */
2133 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2134#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002135}
2136
2137ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2138 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002139
2140static void xtest_tee_test_1028(ADBG_Case_t *c)
2141{
Victor Chong8e070bc2020-05-13 09:59:33 +01002142#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002143 TEEC_Result result = TEEC_ERROR_GENERIC;
2144 uint32_t ret_orig = 0;
2145 TEEC_Session session = { };
2146 uint32_t login = UINT32_MAX;
2147 TEEC_UUID client_uuid = { };
2148 TEEC_UUID expected_client_uuid = { };
2149 TEEC_UUID uuid_ns = { };
2150 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2151 uint32_t group = 0;
2152
2153 group = getegid();
2154
2155 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2156
2157 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2158 return;
2159
2160 sprintf(uuid_name, "gid=%x", group);
2161
2162 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2163 strlen(uuid_name));
2164 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2165 return;
2166
2167 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2168 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2169
2170 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2171 return;
2172
2173 result = ta_os_test_cmd_client_identity(&session, &login,
2174 &client_uuid);
2175
2176 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2177 goto out;
2178
2179 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2180
2181 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2182 sizeof(TEEC_UUID));
2183
2184out:
2185 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002186#else /*!OPENSSL_FOUND*/
2187 UNUSED(c);
2188 /* xtest_uuid_v5() depends on OpenSSL */
2189 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2190#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002191}
2192
2193ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2194 "Session: group login for current user's effective group");