blob: a70a722ee4712effd7c2d726778e394d79fa42c5 [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 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
753 xtest_teec_open_session(&session_crypt,
754 &create_fail_test_ta_uuid, NULL,
755 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200756 /*
757 * Run this several times to see that there's no memory leakage.
758 */
759 for (n = 0; n < 100; n++) {
760 Do_ADBG_Log("n = %zu", n);
761 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
762 xtest_teec_open_session(&session_crypt,
763 &create_fail_test_ta_uuid,
764 NULL, &ret_orig));
765 }
766 }
767 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200768
Jens Wiklander4441fe22015-10-23 16:53:02 +0200769 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100770 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200771 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200772}
Jens Wiklander14f48872018-06-29 15:30:13 +0200773ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
774 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200775
Pascal Brandc639ac82015-07-02 08:53:34 +0200776static void *cancellation_thread(void *arg)
777{
778 /*
779 * Sleep 0.5 seconds before cancellation to make sure that the other
780 * thread is in RPC_WAIT.
781 */
782 (void)usleep(500000);
783 TEEC_RequestCancellation(arg);
784 return NULL;
785}
Pascal Brandc639ac82015-07-02 08:53:34 +0200786
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300787static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
788 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200789{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100790 TEEC_Session session = { };
791 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300792 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200793
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100794 memset(&thr, 0, sizeof(thr));
795
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300796 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200797 {
798 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
799
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 if (ADBG_EXPECT_TEEC_SUCCESS(c,
801 xtest_teec_open_session(&session, &os_test_ta_uuid,
802 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200803
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300804 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
805 TEEC_ORIGIN_TRUSTED_APP,
806 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200807
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300808 op.params[0].value.a = timeout;
809 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
810 TEEC_NONE,
811 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300812 if (cancel) {
813 (void)ADBG_EXPECT(c, 0,
814 pthread_create(&thr, NULL,
815 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200816
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300817 (void)ADBG_EXPECT_TEEC_RESULT(c,
818 TEEC_ERROR_CANCEL,
819 TEEC_InvokeCommand(&session,
820 TA_OS_TEST_CMD_WAIT,
821 &op,
822 &ret_orig));
823 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300824
825 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
826 TEEC_InvokeCommand(&session,
827 TA_OS_TEST_CMD_WAIT,
828 &op,
829 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300830 if (cancel)
831 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832
833 TEEC_CloseSession(&session);
834 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200835 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300836 Do_ADBG_EndSubCase(c, "%s", subcase);
837}
838
839static void xtest_tee_test_1009(ADBG_Case_t *c)
840{
841 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
842 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300843 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300844 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200845}
Jens Wiklander14f48872018-06-29 15:30:13 +0200846ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200847
848static void xtest_tee_test_1010(ADBG_Case_t *c)
849{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100850 unsigned int n = 0;
851 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100852 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200853
854 for (n = 1; n <= 5; n++) {
855 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
856 xtest_tee_test_invalid_mem_access(c, n);
857 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
858 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100859
860 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
861 for (n = 1; n <= 5; n++) {
862 Do_ADBG_BeginSubCase(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 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
866 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200867 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100868 n, memref_sz[idx]);
869 }
870 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200871}
Jens Wiklander14f48872018-06-29 15:30:13 +0200872ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
873 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200874
875static void xtest_tee_test_1011(ADBG_Case_t *c)
876{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100877 TEEC_Session session = { };
878 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200879 struct xtest_crypto_session cs = {
880 c, &session, TA_RPC_CMD_CRYPT_SHA256,
881 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
882 TA_RPC_CMD_CRYPT_AES256ECB_DEC
883 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100884 struct xtest_crypto_session cs_privmem = {
885 c, &session,
886 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
887 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
888 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
889 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200890 TEEC_UUID uuid = rpc_test_ta_uuid;
891
892 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
893 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
894 return;
895
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100896 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200897 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100898 * Run the "complete crypto test suite" using TA-to-TA
899 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200900 */
901 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100902 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
903
904 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
905 /*
906 * Run the "complete crypto test suite" using TA-to-TA
907 * communication via TA private memory.
908 */
909 xtest_crypto_test(&cs_privmem);
910 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
911
Pascal Brandc639ac82015-07-02 08:53:34 +0200912 TEEC_CloseSession(&session);
913}
Jens Wiklander14f48872018-06-29 15:30:13 +0200914ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
915 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200916
917/*
918 * Note that this test is failing when
919 * - running twice in a raw
920 * - and the user TA is statically linked
921 * This is because the counter is not reseted when opening the first session
922 * in case the TA is statically linked
923 */
924static void xtest_tee_test_1012(ADBG_Case_t *c)
925{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100926 TEEC_Session session1 = { };
927 TEEC_Session session2 = { };
928 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200929 TEEC_UUID uuid = sims_test_ta_uuid;
930
931 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
932 {
933 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
934 static const uint8_t in[] = {
935 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
936 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
937 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
938 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
939 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100940 uint8_t out[32] = { };
941 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200942
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300943 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200944 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300945 &ret_orig)))
946 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200947
948 op.params[0].value.a = 0;
949 op.params[1].tmpref.buffer = (void *)in;
950 op.params[1].tmpref.size = sizeof(in);
951 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
952 TEEC_MEMREF_TEMP_INPUT,
953 TEEC_NONE, TEEC_NONE);
954
955 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
956 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
957 &ret_orig));
958
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100959 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300960 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200961 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300962 &ret_orig)))
963 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200964
965 op.params[0].value.a = 0;
966 op.params[1].tmpref.buffer = out;
967 op.params[1].tmpref.size = sizeof(out);
968 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
969 TEEC_MEMREF_TEMP_OUTPUT,
970 TEEC_NONE, TEEC_NONE);
971
972 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
973 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
974 &op, &ret_orig));
975
976 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
977 sizeof(out))) {
978 Do_ADBG_Log("in:");
979 Do_ADBG_HexLog(in, sizeof(in), 16);
980 Do_ADBG_Log("out:");
981 Do_ADBG_HexLog(out, sizeof(out), 16);
982 }
983
984 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
985 TEEC_NONE, TEEC_NONE,
986 TEEC_NONE);
987
988 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
989 TEEC_InvokeCommand(&session1,
990 TA_SIMS_CMD_GET_COUNTER,
991 &op, &ret_orig));
992
993 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
994
995 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
996 TEEC_InvokeCommand(&session2,
997 TA_SIMS_CMD_GET_COUNTER, &op,
998 &ret_orig));
999
1000 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1001 TEEC_CloseSession(&session2);
1002 }
1003
1004 memset(out, 0, sizeof(out));
1005 op.params[0].value.a = 0;
1006 op.params[1].tmpref.buffer = out;
1007 op.params[1].tmpref.size = sizeof(out);
1008 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1009 TEEC_MEMREF_TEMP_OUTPUT,
1010 TEEC_NONE, TEEC_NONE);
1011
1012 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1013 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1014 &ret_orig));
1015
1016 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1017 Do_ADBG_Log("in:");
1018 Do_ADBG_HexLog(in, sizeof(in), 16);
1019 Do_ADBG_Log("out:");
1020 Do_ADBG_HexLog(out, sizeof(out), 16);
1021 }
1022
1023 TEEC_CloseSession(&session1);
1024 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001025 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001026}
Jens Wiklander14f48872018-06-29 15:30:13 +02001027ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1028 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001029
1030struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001031 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001032 uint32_t cmd;
1033 uint32_t repeat;
1034 TEEC_SharedMemory *shm;
1035 uint32_t error_orig;
1036 TEEC_Result res;
1037 uint32_t max_concurrency;
1038 const uint8_t *in;
1039 size_t in_len;
1040 uint8_t *out;
1041 size_t out_len;
1042};
1043
1044static void *test_1013_thread(void *arg)
1045{
1046 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001047 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001048 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1049 uint8_t p2 = TEEC_NONE;
1050 uint8_t p3 = TEEC_NONE;
1051
Jens Wiklander70672972016-04-06 00:01:45 +02001052 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001053 &a->error_orig);
1054 if (a->res != TEEC_SUCCESS)
1055 return NULL;
1056
1057 op.params[0].memref.parent = a->shm;
1058 op.params[0].memref.size = a->shm->size;
1059 op.params[0].memref.offset = 0;
1060 op.params[1].value.a = a->repeat;
1061 op.params[1].value.b = 0;
1062 op.params[2].tmpref.buffer = (void *)a->in;
1063 op.params[2].tmpref.size = a->in_len;
1064 op.params[3].tmpref.buffer = a->out;
1065 op.params[3].tmpref.size = a->out_len;
1066
1067 if (a->in_len)
1068 p2 = TEEC_MEMREF_TEMP_INPUT;
1069 if (a->out_len)
1070 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1071
1072 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1073 TEEC_VALUE_INOUT, p2, p3);
1074
1075 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1076 a->max_concurrency = op.params[1].value.b;
1077 a->out_len = op.params[3].tmpref.size;
1078 TEEC_CloseSession(&session);
1079 return NULL;
1080}
1081
Pascal Brand4fa35582015-12-17 10:59:12 +01001082#define NUM_THREADS 3
1083
Jens Wiklander70672972016-04-06 00:01:45 +02001084static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1085 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001086{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001087 size_t nt = 0;
1088 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001089 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001090 TEEC_SharedMemory shm = { };
1091 size_t max_concurrency = 0;
1092 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001093 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1094 static const uint8_t sha256_out[] = {
1095 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1096 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1097 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1098 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1099 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001100 uint8_t out[32] = { };
1101 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001102
Jens Wiklander70672972016-04-06 00:01:45 +02001103 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001104 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001105
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001106 shm.size = sizeof(struct ta_concurrent_shm);
1107 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1108 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1109 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1110 return;
1111
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001112 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001113 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001114 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001115
1116 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001117 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001118 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001119 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001120 arg[n].shm = &shm;
1121 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1122 test_1013_thread, arg + n)))
1123 nt = n; /* break loop and start cleanup */
1124 }
1125
1126 for (n = 0; n < nt; n++) {
1127 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1128 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1129 if (arg[n].max_concurrency > max_concurrency)
1130 max_concurrency = arg[n].max_concurrency;
1131 }
1132
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001133 /*
1134 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001135 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001136 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1137 * best result there).
1138 */
1139 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001140 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001141 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001142 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001143
Jens Wiklander70672972016-04-06 00:01:45 +02001144 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001145 memset(shm.buffer, 0, shm.size);
1146 memset(arg, 0, sizeof(arg));
1147 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001148 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001149
1150 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001151 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001152 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001153 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001154 arg[n].shm = &shm;
1155 arg[n].in = sha256_in;
1156 arg[n].in_len = sizeof(sha256_in);
1157 arg[n].out = out;
1158 arg[n].out_len = sizeof(out);
1159 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1160 test_1013_thread, arg + n)))
1161 nt = n; /* break loop and start cleanup */
1162 }
1163
1164 for (n = 0; n < nt; n++) {
1165 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1166 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1167 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1168 arg[n].out, arg[n].out_len);
1169 if (arg[n].max_concurrency > max_concurrency)
1170 max_concurrency = arg[n].max_concurrency;
1171 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001172 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001173 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001174
Pascal Brand4fa35582015-12-17 10:59:12 +01001175 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001176 TEEC_ReleaseSharedMemory(&shm);
1177}
Pascal Brand4fa35582015-12-17 10:59:12 +01001178
1179static void xtest_tee_test_1013(ADBG_Case_t *c)
1180{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001181 int i = 0;
1182 double mean_concurrency = 0;
1183 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001184 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001185
1186 if (level == 0)
1187 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001188
Jens Wiklander70672972016-04-06 00:01:45 +02001189 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001190 mean_concurrency = 0;
1191 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001192 xtest_tee_test_1013_single(c, &concurrency,
1193 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001194 mean_concurrency += concurrency;
1195 }
1196 mean_concurrency /= nb_loops;
1197
1198 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1199 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001200 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001201
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001202#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001203 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1204 mean_concurrency = 0;
1205 for (i = 0; i < nb_loops; i++) {
1206 xtest_tee_test_1013_single(c, &concurrency,
1207 &concurrent_large_ta_uuid);
1208 mean_concurrency += concurrency;
1209 }
1210 mean_concurrency /= nb_loops;
1211
1212 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1213 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1214 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001215#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001216}
Jens Wiklander14f48872018-06-29 15:30:13 +02001217ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001218 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001219
1220#ifdef CFG_SECURE_DATA_PATH
1221static void xtest_tee_test_1014(ADBG_Case_t *c)
1222{
1223 UNUSED(c);
1224
1225 int size = 17000;
1226 int loop = 10;
1227 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1228 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001229 int test = 0;
1230 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001231
1232 test = TEST_NS_TO_TA;
1233 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001234 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001235 ADBG_EXPECT(c, 0, ret);
1236 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1237
1238 test = TEST_TA_TO_TA;
1239 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001240 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001241 ADBG_EXPECT(c, 0, ret);
1242 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1243
1244 test = TEST_TA_TO_PTA;
1245 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001246 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001247 ADBG_EXPECT(c, 0, ret);
1248 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1249
1250 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001251 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001252 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001253 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001254 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001255
1256 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
1257 ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
1258 ADBG_EXPECT(c, 0, ret);
1259 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001260}
Jens Wiklander14f48872018-06-29 15:30:13 +02001261ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1262 "Test secure data path against SDP TAs and pTAs");
1263#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001264
1265static void xtest_tee_test_1015(ADBG_Case_t *c)
1266{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001267 TEEC_Result res = TEEC_ERROR_GENERIC;
1268 TEEC_Session session = { };
1269 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001270
Etienne Carriere11093162017-10-26 09:49:04 +02001271 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001272 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1273 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001274 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1275 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001276 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001277 }
1278 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001279
1280 ADBG_EXPECT_TEEC_SUCCESS(c,
1281 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1282 NULL, &ret_orig));
1283 TEEC_CloseSession(&session);
1284}
Jens Wiklander14f48872018-06-29 15:30:13 +02001285ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1286 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001287
1288static void xtest_tee_test_1016(ADBG_Case_t *c)
1289{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001290 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001291 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001292 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001293
1294 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1295 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1296 &ret_orig)))
1297 return;
1298
1299 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1300 TEEC_NONE);
1301
1302 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1303 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1304 &ret_orig));
1305
1306 TEEC_CloseSession(&session);
1307}
Jens Wiklander14f48872018-06-29 15:30:13 +02001308ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1309 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001310
1311static void xtest_tee_test_1017(ADBG_Case_t *c)
1312{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001313 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001314 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001315 uint32_t ret_orig = 0;
1316 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001317 size_t page_size = 4096;
1318
Jens Wiklander87e81702018-03-20 12:00:00 +08001319 shm.size = 8 * page_size;
1320 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1321 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1322 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1323 return;
1324
1325 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1326 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1327 &ret_orig)))
1328 goto out;
1329
1330 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1331 TEEC_MEMREF_PARTIAL_INPUT,
1332 TEEC_MEMREF_PARTIAL_OUTPUT,
1333 TEEC_MEMREF_PARTIAL_OUTPUT);
1334
1335 /*
1336 * The first two memrefs are supposed to be combined into in
1337 * region and the last two memrefs should have one region each
1338 * when the parameters are mapped for the TA.
1339 */
1340 op.params[0].memref.parent = &shm;
1341 op.params[0].memref.size = page_size;
1342 op.params[0].memref.offset = 0;
1343
1344 op.params[1].memref.parent = &shm;
1345 op.params[1].memref.size = page_size;
1346 op.params[1].memref.offset = page_size;
1347
1348 op.params[2].memref.parent = &shm;
1349 op.params[2].memref.size = page_size;
1350 op.params[2].memref.offset = 4 * page_size;
1351
1352 op.params[3].memref.parent = &shm;
1353 op.params[3].memref.size = 2 * page_size;
1354 op.params[3].memref.offset = 6 * page_size;
1355
1356 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1357 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1358 &ret_orig));
1359
1360 TEEC_CloseSession(&session);
1361out:
1362 TEEC_ReleaseSharedMemory(&shm);
1363}
Jens Wiklander14f48872018-06-29 15:30:13 +02001364ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1365 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001366
Etienne Carriere84382b32018-04-25 18:30:30 +02001367static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1368 TEEC_SharedMemory *shm)
1369{
1370 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1371 TEEC_Result ret = TEEC_ERROR_GENERIC;
1372 uint32_t ret_orig = 0;
1373
1374 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1375 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1376
1377 op.params[0].memref.parent = shm;
1378 op.params[0].memref.size = shm->size / 2;
1379 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1380
1381 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1382 &op, &ret_orig);
1383
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001384 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001385 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1386 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1387 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1388 }
1389}
1390
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001391static void xtest_tee_test_1018(ADBG_Case_t *c)
1392{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001393 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001394 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001395 uint32_t ret_orig = 0;
1396 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001397 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001398 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001399 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001400 uint8_t buffer[6001] = { };
1401
1402 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1403 xtest_teec_open_session(&session,
1404 &os_test_ta_uuid,
1405 NULL,
1406 &ret_orig)))
1407 return;
1408
Joakim Becha1212b62020-04-07 12:06:00 +02001409 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001410
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001411 shm.size = 8 * page_size;
1412 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1413 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001414 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1415 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001416 goto out;
1417
1418 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1419 TEEC_MEMREF_PARTIAL_INPUT,
1420 TEEC_MEMREF_PARTIAL_OUTPUT,
1421 TEEC_MEMREF_PARTIAL_OUTPUT);
1422
1423 /*
1424 * The first two memrefs are supposed to be combined into in
1425 * region and the last two memrefs should have one region each
1426 * when the parameters are mapped for the TA.
1427 */
1428 op.params[0].memref.parent = &shm;
1429 op.params[0].memref.size = page_size;
1430 op.params[0].memref.offset = 0;
1431
1432 op.params[1].memref.parent = &shm;
1433 op.params[1].memref.size = page_size;
1434 op.params[1].memref.offset = page_size;
1435
1436 op.params[2].memref.parent = &shm;
1437 op.params[2].memref.size = page_size;
1438 op.params[2].memref.offset = 4 * page_size;
1439
1440 op.params[3].memref.parent = &shm;
1441 op.params[3].memref.size = 3 * page_size;
1442 op.params[3].memref.offset = 6 * page_size;
1443
Etienne Carriere84382b32018-04-25 18:30:30 +02001444 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1445 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001446
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001447 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001448 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1449 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1450 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1451 }
1452
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001453 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001454 Do_ADBG_EndSubCase(c, NULL);
1455
1456 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1457
1458 memset(&shm, 0, sizeof(shm));
1459 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1460 shm.buffer = buffer;
1461 shm.size = sizeof(buffer);
1462
1463 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1464 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1465 &shm)))
1466 goto out;
1467
1468 invoke_1byte_out_of_bounds(c, &session, &shm);
1469
1470 TEEC_ReleaseSharedMemory(&shm);
1471 Do_ADBG_EndSubCase(c, NULL);
1472
1473 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1474
1475 memset(&shm, 0, sizeof(shm));
1476 shm.size = sizeof(buffer);
1477 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1478 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1479 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1480 &shm)))
1481 goto out;
1482
1483 invoke_1byte_out_of_bounds(c, &session, &shm);
1484
1485 TEEC_ReleaseSharedMemory(&shm);
1486 Do_ADBG_EndSubCase(c, NULL);
1487
1488out:
1489 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001490}
Jens Wiklander14f48872018-06-29 15:30:13 +02001491ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1492 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001493
Victor Chong3ff36f52018-06-07 04:37:00 +01001494#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001495static void xtest_tee_test_1019(ADBG_Case_t *c)
1496{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001497 TEEC_Session session = { };
1498 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001499
1500 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1501 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1502 &ret_orig)))
1503 return;
1504
1505 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1506 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1507 &ret_orig));
1508
1509 (void)ADBG_EXPECT_TEEC_RESULT(c,
1510 TEEC_ERROR_TARGET_DEAD,
1511 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1512 NULL, &ret_orig));
1513
1514 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1515
1516 TEEC_CloseSession(&session);
1517}
Jens Wiklander14f48872018-06-29 15:30:13 +02001518ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1519 "Test dynamically linked TA");
1520#endif /*CFG_TA_DYNLINK*/
Jerome Forissier3357f872018-10-05 15:13:44 +02001521
1522static void xtest_tee_test_1020(ADBG_Case_t *c)
1523{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001524 TEEC_Result res = TEEC_ERROR_GENERIC;
1525 TEEC_Session session = { };
1526 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001527
1528 /* Pseudo TA is optional: warn and nicely exit if not found */
1529 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1530 &ret_orig);
1531 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1532 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1533 return;
1534 }
1535 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1536
1537 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1538 NULL, &ret_orig);
1539 if (res != TEEC_SUCCESS) {
1540 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1541 ret_orig);
1542 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1543 Do_ADBG_Log(" - 1020 - skip test, feature not "
1544 "implemented");
1545 goto out;
1546 }
1547 /* Error */
1548 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1549 }
1550out:
1551 TEEC_CloseSession(&session);
1552}
1553ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1554 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001555
1556static TEEC_Result open_sec_session(TEEC_Session *session,
1557 const TEEC_UUID *uuid)
1558{
1559 TEEC_Result res = TEEC_ERROR_GENERIC;
1560 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1561 uint32_t ret_orig = 0;
1562
1563 op.params[0].tmpref.buffer = (void *)uuid;
1564 op.params[0].tmpref.size = sizeof(*uuid);
1565 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1566 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1567
1568 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1569 &op, &ret_orig);
1570 if (res != TEEC_SUCCESS)
1571 return TEEC_ERROR_GENERIC;
1572
1573 return res;
1574}
1575
1576static TEEC_Result sims_get_counter(TEEC_Session *session,
1577 uint32_t *counter)
1578{
1579 TEEC_Result res = TEEC_ERROR_GENERIC;
1580 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1581 uint32_t ret_orig = 0;
1582
1583 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1584 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1585
1586 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1587 &op, &ret_orig);
1588 if (res == TEEC_SUCCESS)
1589 *counter = op.params[0].value.a;
1590
1591 return res;
1592}
1593
1594static TEEC_Result trigger_panic(TEEC_Session *session,
1595 const TEEC_UUID *uuid)
1596{
1597 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1598 uint32_t ret_orig = 0;
1599
1600 if (!uuid) {
1601 op.params[0].tmpref.buffer = NULL;
1602 op.params[0].tmpref.size = 0;
1603 } else {
1604 op.params[0].tmpref.buffer = (void *)uuid;
1605 op.params[0].tmpref.size = sizeof(*uuid);
1606 }
1607 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1608 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1609
1610 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1611 &op, &ret_orig);
1612}
1613
1614static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1615 bool multi_instance)
1616{
1617 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1618 uint32_t counter = 0;
1619 uint32_t ret_orig = 0;
1620 uint32_t exp_counter = 0;
1621 TEEC_Session cs[3] = { };
1622
1623 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1624 xtest_teec_open_session(&cs[0], uuid, NULL,
1625 &ret_orig)))
1626 return;
1627
1628 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1629 xtest_teec_open_session(&cs[1], uuid, NULL,
1630 &ret_orig)))
1631 goto bail0;
1632
1633 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1634 goto bail1;
1635
1636 if (!ADBG_EXPECT(c, 0, counter))
1637 goto bail1;
1638
1639 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1640 goto bail1;
1641
1642 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001643 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001644 goto bail1;
1645
1646 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1647 trigger_panic(&cs[1], NULL)))
1648 goto bail1;
1649
1650 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1651 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1652 sims_get_counter(&cs[0], &counter)))
1653 goto bail1;
1654
1655 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1656 sims_get_counter(&cs[1], &counter)))
1657 goto bail1;
1658
1659 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001660 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001661 xtest_teec_open_session(&cs[1], uuid, NULL,
1662 &ret_orig)))
1663 goto bail1;
1664
1665 /* Sanity check of still valid TA context */
1666 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1667 xtest_teec_open_session(&cs[2], uuid, NULL,
1668 &ret_orig)))
1669 goto bail1;
1670
1671 /* Sanity check of still valid TA context */
1672 if (multi_instance) {
1673 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1674 sims_get_counter(&cs[0], &counter)))
1675 goto bail2;
1676
1677 if (!ADBG_EXPECT(c, 0, counter))
1678 goto bail2;
1679 }
1680
1681 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1682 goto bail2;
1683
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001684 exp_counter = multi_instance ? 0 : 1;
1685 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001686 goto bail2;
1687
1688bail2:
1689 TEEC_CloseSession(&cs[2]);
1690bail1:
1691 TEEC_CloseSession(&cs[1]);
1692bail0:
1693 TEEC_CloseSession(&cs[0]);
1694}
1695
1696static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1697 const TEEC_UUID *uuid2)
1698{
1699 uint32_t ret_orig = 0;
1700 uint32_t counter = 0;
1701 TEEC_Session cs[3] = { };
1702
1703 /* Test pre-conditions */
1704 /* 2.1 - CA opens a session toward TA1 */
1705 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1706 xtest_teec_open_session(&cs[0], uuid1, NULL,
1707 &ret_orig)))
1708 return;
1709
1710 /* 2.2 - CA opens a session toward TA2 */
1711 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1712 xtest_teec_open_session(&cs[1], uuid2, NULL,
1713 &ret_orig)))
1714 goto bail0;
1715
1716 /* 2.3 - TA1 opens a session toward TA2 */
1717 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1718 goto bail1;
1719
1720 /* 2.4 - CA invokes TA2 which panics */
1721 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1722 trigger_panic(&cs[1], NULL)))
1723 goto bail1;
1724
1725 /* Expected results */
1726 /* 2.5 - Expect CA->TA1 session is still alive */
1727 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1728 goto bail1;
1729
1730 /* 2.6 - Expect CA->TA2 session is properly released */
1731 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1732 sims_get_counter(&cs[1], &counter)))
1733 goto bail1;
1734
1735bail1:
1736 TEEC_CloseSession(&cs[1]);
1737bail0:
1738 TEEC_CloseSession(&cs[0]);
1739
1740 memset(cs, 0, sizeof(cs));
1741
1742 /* Test pre-conditions */
1743 /* 2.1 - CA opens a session toward TA1 */
1744 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1745 xtest_teec_open_session(&cs[0], uuid1, NULL,
1746 &ret_orig)))
1747 return;
1748
1749 /* 2.2 - CA opens a session toward TA2 */
1750 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1751 xtest_teec_open_session(&cs[1], uuid2, NULL,
1752 &ret_orig)))
1753 goto bail2;
1754
1755 /* 2.3 - TA1 opens a session toward TA2 */
1756 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1757 goto bail3;
1758
1759 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1760 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1761 goto bail3;
1762
1763 /* Expected results */
1764 /* 2.5 - Expect CA->TA1 session is still alive */
1765 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1766 goto bail3;
1767
1768 /* 2.6 - Expect CA->TA2 session is properly released */
1769 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1770 sims_get_counter(&cs[1], &counter)))
1771 goto bail3;
1772
1773bail3:
1774 TEEC_CloseSession(&cs[1]);
1775bail2:
1776 TEEC_CloseSession(&cs[0]);
1777}
1778
1779static void xtest_tee_test_1021(ADBG_Case_t *c)
1780{
1781 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1782 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1783 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1784
1785 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1786 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1787 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1788
1789 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1790 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1791 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1792
1793 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1794 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1795 &sims_keepalive_test_ta_uuid);
1796 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1797}
1798ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1799 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001800
1801static void xtest_tee_test_1022(ADBG_Case_t *c)
1802{
1803 TEEC_Session session = { 0 };
1804 uint32_t ret_orig = 0;
1805
1806 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1807 xtest_teec_open_session(&session, &os_test_ta_uuid,
1808 NULL, &ret_orig)))
1809 return;
1810
1811 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1812 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1813 &ret_orig));
1814
1815 (void)ADBG_EXPECT_TEEC_RESULT(c,
1816 TEEC_ERROR_TARGET_DEAD,
1817 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1818 NULL, &ret_orig));
1819
1820 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1821
1822 TEEC_CloseSession(&session);
1823}
1824ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1825 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001826
1827/*
1828 * Testing the ELF initialization (.init_array)
1829 *
1830 * - The TA has a global variable which can also be accessed by the two shared
1831 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1832 * dlopen())
1833 * - The TA and both libraries have initialization functions (declared with the
1834 * "constructor" attribute) which perform the following:
1835 * * The TA multiplies by 10 then adds 1
1836 * * os_test_lib multiplies by 10 then adds 2
1837 * * os_test_lib_dl multiplies by 10 then adds 3
1838 * By testing the variable value we make sure the initializations occurred in
1839 * the correct order.
1840 */
1841static void xtest_tee_test_1023(ADBG_Case_t *c)
1842{
1843 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1844 TEEC_Session session = { 0 };
1845 uint32_t ret_orig = 0;
1846
1847 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1848 TEEC_NONE, TEEC_NONE);
1849
1850 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1851 xtest_teec_open_session(&session, &os_test_ta_uuid,
1852 NULL, &ret_orig)))
1853 return;
1854
1855 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1856 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1857 &ret_orig));
1858
1859 /* Expected: initialization of os_test_lib, then TA */
1860 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1861
1862 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1863 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1864 &ret_orig));
1865
1866 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1867 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1868 &ret_orig));
1869
1870 /* Expected: initialization of os_test_lib_dl */
1871 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1872
1873 TEEC_CloseSession(&session);
1874}
1875ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1876 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001877
1878#ifdef CFG_CORE_TPM_EVENT_LOG
1879static void xtest_tee_test_1024(ADBG_Case_t *c)
1880{
1881 TEEC_Session session = {};
1882 uint32_t ret_orig = 0;
1883
1884 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1885 NULL, &ret_orig);
1886
1887 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1888 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1889 TA_TPM_TEST_GET_LOG,
1890 NULL, &ret_orig));
1891 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1892
1893 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1894 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1895 TA_TPM_TEST_SHORT_BUF,
1896 NULL, &ret_orig));
1897 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1898
1899 TEEC_CloseSession(&session);
1900}
1901
1902ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1903 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1904#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001905
1906static void xtest_tee_test_1025(ADBG_Case_t *c)
1907{
1908 TEEC_Session session = {};
1909 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1910 uint32_t ret_orig = 0;
1911 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001912 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001913 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001914
1915 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1916
1917 memset(&shm, 0, sizeof(shm));
1918 shm.flags = TEEC_MEM_INPUT;
1919 shm.buffer = NULL;
1920 shm.size = 0;
1921
1922 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1923 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1924
1925 memset(&shm, 0, sizeof(shm));
1926 shm.flags = TEEC_MEM_OUTPUT;
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 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001934
Etienne Carrierec602a522020-04-13 18:53:17 +02001935 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001936 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001937 return;
1938 }
1939
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001940 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1941 xtest_teec_open_session(&session,
1942 &os_test_ta_uuid,
1943 NULL, &ret_orig)))
1944 return;
1945
1946 empty_buf = malloc(1);
1947 if (!empty_buf) {
1948 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001949 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001950 }
1951
1952 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1953 TEEC_MEMREF_TEMP_INPUT,
1954 TEEC_MEMREF_TEMP_OUTPUT,
1955 TEEC_MEMREF_TEMP_OUTPUT);
1956
1957 Do_ADBG_BeginSubCase(c,
1958 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1959
1960 op.params[0].tmpref.buffer = empty_buf;
1961 op.params[0].tmpref.size = 0;
1962
1963 op.params[1].tmpref.buffer = NULL;
1964 op.params[1].tmpref.size = 0;
1965
1966 op.params[2].tmpref.buffer = empty_buf;
1967 op.params[2].tmpref.size = 0;
1968
1969 op.params[3].tmpref.buffer = NULL;
1970 op.params[3].tmpref.size = 0;
1971
1972 ADBG_EXPECT(c, TEE_SUCCESS,
1973 TEEC_InvokeCommand(&session,
1974 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1975 &ret_orig));
1976
1977 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1978
1979 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
1980
1981 op.params[0].tmpref.buffer = empty_buf;
1982 op.params[0].tmpref.size = 1;
1983
1984 op.params[1].tmpref.buffer = NULL;
1985 op.params[1].tmpref.size = 0;
1986
1987 op.params[2].tmpref.buffer = empty_buf;
1988 op.params[2].tmpref.size = 0;
1989
1990 op.params[3].tmpref.buffer = NULL;
1991 op.params[3].tmpref.size = 0;
1992
1993 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
1994 TEEC_InvokeCommand(&session,
1995 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1996 &ret_orig));
1997
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001998 TEEC_CloseSession(&session);
1999
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002000 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2001
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002002 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2003
2004 /* Pseudo TA is optional: warn and nicely exit if not found */
2005 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2006 &ret_orig);
2007 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2008 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2009 goto out;
2010 }
2011 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2012 goto out;
2013
2014 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2015 TEEC_NONE, TEEC_NONE);
2016 op.params[0].tmpref.buffer = NULL;
2017 op.params[0].tmpref.size = 0;
2018
2019 ADBG_EXPECT(c, TEE_SUCCESS,
2020 TEEC_InvokeCommand(&session,
2021 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2022 &op, &ret_orig));
2023
2024out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002025 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002026out:
2027 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002028 free(empty_buf);
2029}
2030ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2031 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002032
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002033#define TEE_UUID_NS_NAME_SIZE 128
2034
2035/*
2036 * TEE Client UUID name space identifier (UUIDv4)
2037 *
2038 * Value here is random UUID that is allocated as name space identifier for
2039 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2040 */
2041static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2042
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002043/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2044static TEEC_UUID client_uuid_public = { };
2045
2046static void xtest_tee_test_1026(ADBG_Case_t *c)
2047{
2048 TEEC_Result result = TEEC_ERROR_GENERIC;
2049 uint32_t ret_orig = 0;
2050 TEEC_Session session = { };
2051 uint32_t login = UINT32_MAX;
2052 TEEC_UUID client_uuid = { };
2053
2054 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2055 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2056
2057 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2058 return;
2059
2060 result = ta_os_test_cmd_client_identity(&session, &login,
2061 &client_uuid);
2062
2063 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2064 goto out;
2065
2066 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2067
2068 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2069 sizeof(TEEC_UUID));
2070
2071out:
2072 TEEC_CloseSession(&session);
2073}
2074
2075ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2076 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002077
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002078/*
2079 * regression_1027
2080 * Depends on OpenSSL
2081 * Depends on kernel commit "tee: optee: Add support for session login client UUID generation"
2082 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
2083 * Upstream: <put sha-1 here when known>
2084 *
2085 * xtest skips the test when not built with OpenSSL.
2086 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002087static void xtest_tee_test_1027(ADBG_Case_t *c)
2088{
Victor Chong8e070bc2020-05-13 09:59:33 +01002089#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002090 TEEC_Result result = TEEC_ERROR_GENERIC;
2091 uint32_t ret_orig = 0;
2092 TEEC_Session session = { };
2093 uint32_t login = UINT32_MAX;
2094 TEEC_UUID client_uuid = { };
2095 TEEC_UUID expected_client_uuid = { };
2096 TEEC_UUID uuid_ns = { };
2097 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2098
2099 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2100
2101 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2102 return;
2103
2104 sprintf(uuid_name, "uid=%x", geteuid());
2105
2106 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2107 strlen(uuid_name));
2108 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2109 return;
2110
2111 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2112 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2113
2114 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2115 return;
2116
2117 result = ta_os_test_cmd_client_identity(&session, &login,
2118 &client_uuid);
2119
2120 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2121 goto out;
2122
2123 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2124
2125 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2126 sizeof(TEEC_UUID));
2127
2128out:
2129 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002130#else /*!OPENSSL_FOUND*/
2131 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002132 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002133 /* xtest_uuid_v5() depends on OpenSSL */
2134 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2135#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002136}
2137
2138ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2139 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002140
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002141/*
2142 * regression_1028
2143 * Depends on OpenSSL and kernel commit "tee: optee: Add support for session login client UUID generation"
2144 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
2145 * Upstream: <put sha-1 here when known>
2146 *
2147 * xtest skips the test when not built with OpenSSL.
2148 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002149static void xtest_tee_test_1028(ADBG_Case_t *c)
2150{
Victor Chong8e070bc2020-05-13 09:59:33 +01002151#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002152 TEEC_Result result = TEEC_ERROR_GENERIC;
2153 uint32_t ret_orig = 0;
2154 TEEC_Session session = { };
2155 uint32_t login = UINT32_MAX;
2156 TEEC_UUID client_uuid = { };
2157 TEEC_UUID expected_client_uuid = { };
2158 TEEC_UUID uuid_ns = { };
2159 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2160 uint32_t group = 0;
2161
2162 group = getegid();
2163
2164 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2165
2166 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2167 return;
2168
2169 sprintf(uuid_name, "gid=%x", group);
2170
2171 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2172 strlen(uuid_name));
2173 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2174 return;
2175
2176 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2177 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2178
2179 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2180 return;
2181
2182 result = ta_os_test_cmd_client_identity(&session, &login,
2183 &client_uuid);
2184
2185 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2186 goto out;
2187
2188 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2189
2190 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2191 sizeof(TEEC_UUID));
2192
2193out:
2194 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002195#else /*!OPENSSL_FOUND*/
2196 UNUSED(c);
2197 /* xtest_uuid_v5() depends on OpenSSL */
2198 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2199#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002200}
2201
2202ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2203 "Session: group login for current user's effective group");