blob: b4ce3670e84215278f8d5523fedd1f78f3e374c5 [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
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03007#include <errno.h>
Etienne Carrierea4653552017-01-11 10:04:24 +01008#include <limits.h>
Jerome Forissier0915b222021-10-27 18:20:59 +02009#ifdef OPENSSL_FOUND
10#include <openssl/bn.h>
11#include <openssl/err.h>
12#include <openssl/evp.h>
13#include <openssl/rsa.h>
14#include <openssl/sha.h>
15#endif
16#include <pta_attestation.h>
17#include <pta_invoke_tests.h>
18#include <pta_secstor_ta_mgmt.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010019#include <pthread.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020020#include <sdp_basic.h>
21#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020022#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010023#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020024#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060025#include <sys/stat.h>
26#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020027#include <ta_arm_bti.h>
28#include <ta_concurrent.h>
29#include <ta_create_fail_test.h>
30#include <ta_crypt.h>
31#include <ta_miss_test.h>
32#include <ta_os_test.h>
33#include <ta_rpc_test.h>
34#include <ta_sims_keepalive_test.h>
35#include <ta_sims_test.h>
36#include <ta_supp_plugin.h>
37#include <ta_tpm_log_test.h>
38#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010039#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020040#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010041#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020042
Jerome Forissier0915b222021-10-27 18:20:59 +020043#include "xtest_helpers.h"
44#include "xtest_test.h"
45#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030046
Jens Wiklanderec545fb2017-11-24 16:58:07 +010047#ifndef MIN
48#define MIN(a, b) ((a) < (b) ? (a) : (b))
49#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020050
Pascal Brandc639ac82015-07-02 08:53:34 +020051struct xtest_crypto_session {
52 ADBG_Case_t *c;
53 TEEC_Session *session;
54 uint32_t cmd_id_sha256;
55 uint32_t cmd_id_aes256ecb_encrypt;
56 uint32_t cmd_id_aes256ecb_decrypt;
57};
58
59static void xtest_crypto_test(struct xtest_crypto_session *cs)
60{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010061 uint32_t ret_orig = 0;
62 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020063 uint8_t crypt_in[16] = { 22, 17 };
64
65 crypt_in[15] = 60;
66
67 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
68 {
69 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
70
71 op.params[0].tmpref.buffer = crypt_in;
72 op.params[0].tmpref.size = sizeof(crypt_in);
73 op.params[1].tmpref.buffer = crypt_out;
74 op.params[1].tmpref.size = sizeof(crypt_out);
75 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
76 TEEC_MEMREF_TEMP_OUTPUT,
77 TEEC_NONE, TEEC_NONE);
78
79 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
80 TEEC_InvokeCommand(cs->session,
81 cs->
82 cmd_id_aes256ecb_encrypt,
83 &op,
84 &ret_orig));
85 }
86 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
87
88 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
89 {
90 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010091 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020092
93 op.params[0].tmpref.buffer = crypt_out;
94 op.params[0].tmpref.size = sizeof(crypt_out);
95 op.params[1].tmpref.buffer = out;
96 op.params[1].tmpref.size = sizeof(out);
97 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
98 TEEC_MEMREF_TEMP_OUTPUT,
99 TEEC_NONE, TEEC_NONE);
100
101 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
102 TEEC_InvokeCommand(cs->session,
103 cs->
104 cmd_id_aes256ecb_decrypt,
105 &op,
106 &ret_orig));
107
108 if (!ADBG_EXPECT(cs->c, 0,
109 memcmp(crypt_in, out, sizeof(crypt_in)))) {
110 Do_ADBG_Log("crypt_in:");
111 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
112 Do_ADBG_Log("out:");
113 Do_ADBG_HexLog(out, sizeof(out), 16);
114 }
115 }
116 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
117
118 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
119 {
120 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
121 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
122 static const uint8_t sha256_out[] = {
123 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
124 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
125 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
126 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
127 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100128 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200129
130 op.params[0].tmpref.buffer = (void *)sha256_in;
131 op.params[0].tmpref.size = sizeof(sha256_in);
132 op.params[1].tmpref.buffer = out;
133 op.params[1].tmpref.size = sizeof(out);
134 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
135 TEEC_MEMREF_TEMP_OUTPUT,
136 TEEC_NONE, TEEC_NONE);
137
138 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
139 TEEC_InvokeCommand(cs->session,
140 cs->
141 cmd_id_sha256,
142 &op,
143 &ret_orig));
144
145 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
146 sizeof(sha256_out)))) {
147 Do_ADBG_Log("sha256_out:");
148 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
149 Do_ADBG_Log("out:");
150 Do_ADBG_HexLog(out, sizeof(out), 16);
151 }
152 }
153 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
154
Etienne Carrierea3198522017-10-26 09:48:55 +0200155 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200156 {
157 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
158 static const uint8_t in[] = {
159 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
160 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
161 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
162 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
163 };
164 static const uint8_t exp_out[] = {
165 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
166 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
167 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
168 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
169 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100170 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200171
172 op.params[0].tmpref.buffer = (void *)in;
173 op.params[0].tmpref.size = sizeof(in);
174 op.params[1].tmpref.buffer = out;
175 op.params[1].tmpref.size = sizeof(out);
176 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
177 TEEC_MEMREF_TEMP_OUTPUT,
178 TEEC_NONE, TEEC_NONE);
179
180 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
181 TEEC_InvokeCommand(cs->session,
182 cs->
183 cmd_id_aes256ecb_encrypt,
184 &op,
185 &ret_orig));
186
187 if (!ADBG_EXPECT(cs->c, 0,
188 memcmp(exp_out, out, sizeof(exp_out)))) {
189 Do_ADBG_Log("exp_out:");
190 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
191 Do_ADBG_Log("out:");
192 Do_ADBG_HexLog(out, sizeof(out), 16);
193 }
194 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200195 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200196
Etienne Carrierea3198522017-10-26 09:48:55 +0200197 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200198 {
199 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
200 static const uint8_t in[] = {
201 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
202 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
203 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
204 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
205 };
206 static const uint8_t exp_out[] = {
207 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
208 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
209 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
210 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
211 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100212 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200213
214 op.params[0].tmpref.buffer = (void *)in;
215 op.params[0].tmpref.size = sizeof(in);
216 op.params[1].tmpref.buffer = out;
217 op.params[1].tmpref.size = sizeof(out);
218 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
219 TEEC_MEMREF_TEMP_OUTPUT,
220 TEEC_NONE, TEEC_NONE);
221
222 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
223 TEEC_InvokeCommand(cs->session,
224 cs->
225 cmd_id_aes256ecb_decrypt,
226 &op,
227 &ret_orig));
228
229 if (!ADBG_EXPECT(cs->c, 0,
230 memcmp(exp_out, out, sizeof(exp_out)))) {
231 Do_ADBG_Log("exp_out:");
232 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
233 Do_ADBG_Log("out:");
234 Do_ADBG_HexLog(out, sizeof(out), 16);
235 }
236 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200237 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200238}
239
240static void xtest_tee_test_1001(ADBG_Case_t *c)
241{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100242 TEEC_Result res = TEEC_ERROR_GENERIC;
243 TEEC_Session session = { };
244 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200245
Etienne Carriere11093162017-10-26 09:49:04 +0200246 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100247 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100248 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200249 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
250 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100251 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200252 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100253 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
254 return;
255
256 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200257
Jens Wiklandercf16e842016-02-10 09:07:09 +0100258 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100259 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Etienne Carriere3ea96722022-03-11 09:16:40 +0100260
261 Do_ADBG_EndSubCase(c, "Core self tests");
262
263 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
264
265 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
266 &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL,
267 &ret_orig));
268
269 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
270
Jens Wiklandercf16e842016-02-10 09:07:09 +0100271 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200272}
Jens Wiklander14f48872018-06-29 15:30:13 +0200273ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200274
Jens Wiklander1d70a112017-10-16 15:16:39 +0200275static void xtest_tee_test_1002(ADBG_Case_t *c)
276{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100277 TEEC_Result res = TEEC_ERROR_GENERIC;
278 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200279 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100280 uint32_t ret_orig = 0;
281 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200282 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100283 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200284
Etienne Carriere11093162017-10-26 09:49:04 +0200285 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200286 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
287 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200288 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
289 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200290 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200291 }
292 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200293
294 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
295 TEEC_NONE, TEEC_NONE);
296 op.params[0].tmpref.size = sizeof(buf);
297 op.params[0].tmpref.buffer = buf;
298
299 for (n = 0; n < sizeof(buf); n++)
300 buf[n] = n + 1;
301 for (n = 0; n < sizeof(buf); n++)
302 exp_sum += buf[n];
303
304 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
305 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
306 goto out;
307
308 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
309out:
310 TEEC_CloseSession(&session);
311}
Jens Wiklander14f48872018-06-29 15:30:13 +0200312ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200313
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100314struct test_1003_arg {
315 uint32_t test_type;
316 size_t repeat;
317 size_t max_before_lockers;
318 size_t max_during_lockers;
319 size_t before_lockers;
320 size_t during_lockers;
321 TEEC_Result res;
322 uint32_t error_orig;
323};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200324
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100325static void *test_1003_thread(void *arg)
326{
327 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100328 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100329 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100330 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100331
332 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
333 NULL, &a->error_orig);
334 if (a->res != TEEC_SUCCESS)
335 return NULL;
336
337 for (n = 0; n < a->repeat; n++) {
338 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
339
340 op.params[0].value.a = a->test_type;
341 op.params[0].value.b = rounds;
342
343 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
344 TEEC_VALUE_OUTPUT,
345 TEEC_NONE, TEEC_NONE);
346 a->res = TEEC_InvokeCommand(&session,
347 PTA_INVOKE_TESTS_CMD_MUTEX,
348 &op, &a->error_orig);
349 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
350 op.params[1].value.b != 1) {
351 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
352 a->res = TEEC_ERROR_BAD_STATE;
353 a->error_orig = 42;
354 break;
355 }
356
357 if (a->test_type == PTA_MUTEX_TEST_READER) {
358 if (op.params[1].value.a > a->max_before_lockers)
359 a->max_before_lockers = op.params[1].value.a;
360
361 if (op.params[1].value.b > a->max_during_lockers)
362 a->max_during_lockers = op.params[1].value.b;
363
364 a->before_lockers += op.params[1].value.a;
365 a->during_lockers += op.params[1].value.b;
366 }
367 }
368 TEEC_CloseSession(&session);
369
370 return NULL;
371}
372
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100373#define TEST_1003_THREAD_COUNT (3 * 2)
374
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100375static void xtest_tee_test_1003(ADBG_Case_t *c)
376{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100377 TEEC_Result res = TEEC_ERROR_GENERIC;
378 TEEC_Session session = { };
379 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100380 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100381 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100382 size_t max_read_concurrency = 0;
383 size_t max_read_waiters = 0;
384 size_t num_concurrent_read_lockers = 0;
385 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100386 size_t n = 0;
387 size_t nt = TEST_1003_THREAD_COUNT;
388 double mean_read_concurrency = 0;
389 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100390 size_t num_writers = 0;
391 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100392 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100393
394 /* Pseudo TA is optional: warn and nicely exit if not found */
395 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
396 &ret_orig);
397 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
398 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
399 return;
400 }
401 ADBG_EXPECT_TEEC_SUCCESS(c, res);
402 TEEC_CloseSession(&session);
403
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100404 for (n = 0; n < nt; n++) {
405 if (n % 3) {
406 arg[n].test_type = PTA_MUTEX_TEST_READER;
407 num_readers++;
408 } else {
409 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
410 num_writers++;
411 }
412 arg[n].repeat = repeat;
413 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
414 test_1003_thread, arg + n)))
415 nt = n; /* break loop and start cleanup */
416 }
417
418 for (n = 0; n < nt; n++) {
419 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
420 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
421 Do_ADBG_Log("error origin %" PRIu32,
422 arg[n].error_orig);
423 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
424 if (arg[n].max_during_lockers > max_read_concurrency)
425 max_read_concurrency =
426 arg[n].max_during_lockers;
427
428 if (arg[n].max_before_lockers > max_read_waiters)
429 max_read_waiters = arg[n].max_before_lockers;
430
431 num_concurrent_read_lockers += arg[n].during_lockers;
432 num_concurrent_read_waiters += arg[n].before_lockers;
433 }
434 }
435
436 mean_read_concurrency = (double)num_concurrent_read_lockers /
437 (double)(repeat * num_readers);
438 mean_read_waiters = (double)num_concurrent_read_waiters /
439 (double)(repeat * num_readers);
440
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100441 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
442 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100443 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
444 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
445 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
446 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
447}
Jens Wiklander14f48872018-06-29 15:30:13 +0200448ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
449 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200450
Pascal Brandc639ac82015-07-02 08:53:34 +0200451static void xtest_tee_test_1004(ADBG_Case_t *c)
452{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100453 TEEC_Session session = { };
454 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200455 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
456 TA_CRYPT_CMD_AES256ECB_ENC,
457 TA_CRYPT_CMD_AES256ECB_DEC };
458
459 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
460 &session, &crypt_user_ta_uuid,
461 NULL, &ret_orig)))
462 return;
463
464 /* Run the "complete crypto test suite" */
465 xtest_crypto_test(&cs);
466
467 TEEC_CloseSession(&session);
468}
Jens Wiklander14f48872018-06-29 15:30:13 +0200469ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200470
Etienne Carriere92c34422018-02-09 13:11:40 +0100471static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200472{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100473 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200474 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100475 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200476
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300477 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200478 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300479 &ret_orig)))
480 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200481
482 op.params[0].value.a = n;
483 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
484 TEEC_NONE);
485
486 (void)ADBG_EXPECT_TEEC_RESULT(c,
487 TEEC_ERROR_TARGET_DEAD,
488 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
489 &ret_orig));
490
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300491 (void)ADBG_EXPECT_TEEC_RESULT(c,
492 TEEC_ERROR_TARGET_DEAD,
493 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200494 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300495
Pascal Brandc639ac82015-07-02 08:53:34 +0200496 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
497
498 TEEC_CloseSession(&session);
499}
500
Etienne Carriere92c34422018-02-09 13:11:40 +0100501static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
502 size_t size)
503{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100504 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100505 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100506 uint32_t ret_orig = 0;
507 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100508
Etienne Carriere92c34422018-02-09 13:11:40 +0100509 shm.size = size;
510 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
511 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
512 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
513 return;
514
515 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
516 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
517 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200518 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100519
520 op.params[0].value.a = (uint32_t)n;
521 op.params[1].memref.parent = &shm;
522 op.params[1].memref.size = size;
523 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
524 TEEC_NONE, TEEC_NONE);
525
526 (void)ADBG_EXPECT_TEEC_RESULT(c,
527 TEEC_ERROR_TARGET_DEAD,
528 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
529 &ret_orig));
530
531 (void)ADBG_EXPECT_TEEC_RESULT(c,
532 TEEC_ERROR_TARGET_DEAD,
533 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
534 &ret_orig));
535
536 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
537
538 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200539rel_shm:
540 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100541}
542
Pascal Brandc639ac82015-07-02 08:53:34 +0200543static void xtest_tee_test_1005(ADBG_Case_t *c)
544{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100545 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200546#define MAX_SESSIONS 3
547 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100548 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200549
550 for (i = 0; i < MAX_SESSIONS; i++) {
551 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200552 xtest_teec_open_session(&sessions[i],
553 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200554 NULL, &ret_orig)))
555 break;
556 }
557
558 for (; --i >= 0; )
559 TEEC_CloseSession(&sessions[i]);
560}
Jens Wiklander14f48872018-06-29 15:30:13 +0200561ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200562
563static void xtest_tee_test_1006(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 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100568 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200569
570 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
571 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
572 &ret_orig)))
573 return;
574
575 op.params[0].tmpref.buffer = buf;
576 op.params[0].tmpref.size = sizeof(buf);
577 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
578 TEEC_NONE, TEEC_NONE);
579
580 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
581 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
582 &ret_orig));
583
584 TEEC_CloseSession(&session);
585}
Jens Wiklander14f48872018-06-29 15:30:13 +0200586ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
587 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200588
589static void xtest_tee_test_1007(ADBG_Case_t *c)
590{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100591 TEEC_Session session = { };
592 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200593
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300594 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200595 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300596 &ret_orig)))
597 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200598
599 (void)ADBG_EXPECT_TEEC_RESULT(c,
600 TEEC_ERROR_TARGET_DEAD,
601 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
602 &ret_orig));
603
604 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
605
606 (void)ADBG_EXPECT_TEEC_RESULT(c,
607 TEEC_ERROR_TARGET_DEAD,
608 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
609 &ret_orig));
610
611 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
612
613 TEEC_CloseSession(&session);
614}
Jens Wiklander14f48872018-06-29 15:30:13 +0200615ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200616
Jerome Forissierf02a2212015-10-29 14:33:35 +0100617#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000618# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800619#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000620# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100621#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000622# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100623#endif
624
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100625static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600626{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100627 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600628
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100629 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100630 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100631 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200632 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
633 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
634 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600635 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200636
Jens Wiklanderb7940892015-10-23 16:02:40 +0200637 return fopen(buf, mode);
638}
639
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100640static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200641{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100642 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100643 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
644 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100645 TEEC_Result res = TEEC_ERROR_GENERIC;
646 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100647 FILE *f = NULL;
648 bool r = false;
649 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100650 size_t sz = 0;
651 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200652
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100653 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
654 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
655 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200656
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100657 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
658 if (!ADBG_EXPECT_NOT_NULL(c, f))
659 goto out;
660 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
661 goto out;
662 sz = ftell(f);
663 rewind(f);
664
665 buf = malloc(sz);
666 if (!ADBG_EXPECT_NOT_NULL(c, buf))
667 goto out;
668
669 fread_res = fread(buf, 1, sz, f);
670 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
671 goto out;
672
Jens Wiklander4441fe22015-10-23 16:53:02 +0200673 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100674 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200675
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100676 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200677
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100678 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
679 TEEC_NONE, TEEC_NONE);
680 op.params[0].tmpref.buffer = buf;
681 op.params[0].tmpref.size = sz;
682
683 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
684 &ret_orig);
685 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
686out:
687 free(buf);
688 if (f)
689 fclose(f);
690 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200691 return r;
692}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100693
694static void test_1008_corrupt_ta(ADBG_Case_t *c)
695{
696 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
697 TEEC_Result res = TEEC_ERROR_GENERIC;
698 TEEC_Session session = { };
699 uint32_t ret_orig = 0;
700
701 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
702 if (res) {
703 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
704 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200705 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100706 return;
707 }
708 TEEC_CloseSession(&session);
709
710 ADBG_EXPECT_TRUE(c,
711 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
712 ADBG_EXPECT_TRUE(c,
713 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
714 ADBG_EXPECT_TRUE(c,
715 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
716 ADBG_EXPECT_TRUE(c,
717 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
718 ADBG_EXPECT_TRUE(c,
719 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
720 ADBG_EXPECT_TRUE(c,
721 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
722 ADBG_EXPECT_TRUE(c,
723 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
724 ADBG_EXPECT_TRUE(c,
725 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
726 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
727 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
728}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200729
Pascal Brandc639ac82015-07-02 08:53:34 +0200730static void xtest_tee_test_1008(ADBG_Case_t *c)
731{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100732 TEEC_Session session = { };
733 TEEC_Session session_crypt = { };
734 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200735
736 Do_ADBG_BeginSubCase(c, "Invoke command");
737 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300738 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200739 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300740 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200741
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300742 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
743 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
744 NULL, &ret_orig));
745 TEEC_CloseSession(&session);
746 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200747
Pascal Brandc639ac82015-07-02 08:53:34 +0200748 }
749 Do_ADBG_EndSubCase(c, "Invoke command");
750
751 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
752 {
753 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
754
755 op.params[0].value.a = 2000;
756 op.paramTypes = TEEC_PARAM_TYPES(
757 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
758
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300759 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200760 xtest_teec_open_session(&session,
761 &os_test_ta_uuid,
762 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300763 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200764
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300765 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
766 TEEC_InvokeCommand(&session,
767 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
768 &op, &ret_orig));
769 TEEC_CloseSession(&session);
770 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200771 }
772 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
773
774 Do_ADBG_BeginSubCase(c, "Create session fail");
775 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100776 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200777
Pascal Brandc639ac82015-07-02 08:53:34 +0200778 for (n = 0; n < 100; n++) {
779 Do_ADBG_Log("n = %zu", n);
780 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
781 xtest_teec_open_session(&session_crypt,
782 &create_fail_test_ta_uuid,
783 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100784 /* level > 0 may be used to detect/debug memory leaks */
785 if (!level)
786 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200787 }
788 }
789 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200790
Jens Wiklander4441fe22015-10-23 16:53:02 +0200791 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100792 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200793 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200794}
Jens Wiklander14f48872018-06-29 15:30:13 +0200795ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
796 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200797
Pascal Brandc639ac82015-07-02 08:53:34 +0200798static void *cancellation_thread(void *arg)
799{
800 /*
801 * Sleep 0.5 seconds before cancellation to make sure that the other
802 * thread is in RPC_WAIT.
803 */
804 (void)usleep(500000);
805 TEEC_RequestCancellation(arg);
806 return NULL;
807}
Pascal Brandc639ac82015-07-02 08:53:34 +0200808
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300809static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
810 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200811{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100812 TEEC_Session session = { };
813 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300814 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200815
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100816 memset(&thr, 0, sizeof(thr));
817
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300818 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200819 {
820 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
821
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300822 if (ADBG_EXPECT_TEEC_SUCCESS(c,
823 xtest_teec_open_session(&session, &os_test_ta_uuid,
824 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200825
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300826 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
827 TEEC_ORIGIN_TRUSTED_APP,
828 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200829
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300830 op.params[0].value.a = timeout;
831 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
832 TEEC_NONE,
833 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300834 if (cancel) {
835 (void)ADBG_EXPECT(c, 0,
836 pthread_create(&thr, NULL,
837 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200838
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300839 (void)ADBG_EXPECT_TEEC_RESULT(c,
840 TEEC_ERROR_CANCEL,
841 TEEC_InvokeCommand(&session,
842 TA_OS_TEST_CMD_WAIT,
843 &op,
844 &ret_orig));
845 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300846
847 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
848 TEEC_InvokeCommand(&session,
849 TA_OS_TEST_CMD_WAIT,
850 &op,
851 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300852 if (cancel)
853 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300854
855 TEEC_CloseSession(&session);
856 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200857 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300858 Do_ADBG_EndSubCase(c, "%s", subcase);
859}
860
861static void xtest_tee_test_1009(ADBG_Case_t *c)
862{
863 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
864 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300865 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300866 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200867}
Jens Wiklander14f48872018-06-29 15:30:13 +0200868ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200869
870static void xtest_tee_test_1010(ADBG_Case_t *c)
871{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100872 unsigned int n = 0;
873 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100874 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200875
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200876 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200877 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
878 xtest_tee_test_invalid_mem_access(c, n);
879 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
880 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100881
882 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
883 for (n = 1; n <= 5; n++) {
884 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200885 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100886 n, memref_sz[idx]);
887 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
888 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200889 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100890 n, memref_sz[idx]);
891 }
892 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200893}
Jens Wiklander14f48872018-06-29 15:30:13 +0200894ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
895 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200896
897static void xtest_tee_test_1011(ADBG_Case_t *c)
898{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100899 TEEC_Session session = { };
900 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200901 struct xtest_crypto_session cs = {
902 c, &session, TA_RPC_CMD_CRYPT_SHA256,
903 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
904 TA_RPC_CMD_CRYPT_AES256ECB_DEC
905 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100906 struct xtest_crypto_session cs_privmem = {
907 c, &session,
908 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
909 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
910 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
911 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200912 TEEC_UUID uuid = rpc_test_ta_uuid;
913
914 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
915 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
916 return;
917
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100918 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200919 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100920 * Run the "complete crypto test suite" using TA-to-TA
921 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200922 */
923 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100924 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
925
926 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
927 /*
928 * Run the "complete crypto test suite" using TA-to-TA
929 * communication via TA private memory.
930 */
931 xtest_crypto_test(&cs_privmem);
932 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
933
Pascal Brandc639ac82015-07-02 08:53:34 +0200934 TEEC_CloseSession(&session);
935}
Jens Wiklander14f48872018-06-29 15:30:13 +0200936ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
937 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200938
939/*
940 * Note that this test is failing when
941 * - running twice in a raw
942 * - and the user TA is statically linked
943 * This is because the counter is not reseted when opening the first session
944 * in case the TA is statically linked
945 */
946static void xtest_tee_test_1012(ADBG_Case_t *c)
947{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100948 TEEC_Session session1 = { };
949 TEEC_Session session2 = { };
950 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200951 TEEC_UUID uuid = sims_test_ta_uuid;
952
953 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
954 {
955 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
956 static const uint8_t in[] = {
957 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
958 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
959 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
960 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
961 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100962 uint8_t out[32] = { };
963 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200964
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300965 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200966 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300967 &ret_orig)))
968 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200969
970 op.params[0].value.a = 0;
971 op.params[1].tmpref.buffer = (void *)in;
972 op.params[1].tmpref.size = sizeof(in);
973 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
974 TEEC_MEMREF_TEMP_INPUT,
975 TEEC_NONE, TEEC_NONE);
976
977 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
978 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
979 &ret_orig));
980
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100981 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300982 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200983 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300984 &ret_orig)))
985 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200986
987 op.params[0].value.a = 0;
988 op.params[1].tmpref.buffer = out;
989 op.params[1].tmpref.size = sizeof(out);
990 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
991 TEEC_MEMREF_TEMP_OUTPUT,
992 TEEC_NONE, TEEC_NONE);
993
994 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
995 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
996 &op, &ret_orig));
997
998 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
999 sizeof(out))) {
1000 Do_ADBG_Log("in:");
1001 Do_ADBG_HexLog(in, sizeof(in), 16);
1002 Do_ADBG_Log("out:");
1003 Do_ADBG_HexLog(out, sizeof(out), 16);
1004 }
1005
1006 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1007 TEEC_NONE, TEEC_NONE,
1008 TEEC_NONE);
1009
1010 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1011 TEEC_InvokeCommand(&session1,
1012 TA_SIMS_CMD_GET_COUNTER,
1013 &op, &ret_orig));
1014
1015 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1016
1017 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1018 TEEC_InvokeCommand(&session2,
1019 TA_SIMS_CMD_GET_COUNTER, &op,
1020 &ret_orig));
1021
1022 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1023 TEEC_CloseSession(&session2);
1024 }
1025
1026 memset(out, 0, sizeof(out));
1027 op.params[0].value.a = 0;
1028 op.params[1].tmpref.buffer = out;
1029 op.params[1].tmpref.size = sizeof(out);
1030 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1031 TEEC_MEMREF_TEMP_OUTPUT,
1032 TEEC_NONE, TEEC_NONE);
1033
1034 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1035 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1036 &ret_orig));
1037
1038 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1039 Do_ADBG_Log("in:");
1040 Do_ADBG_HexLog(in, sizeof(in), 16);
1041 Do_ADBG_Log("out:");
1042 Do_ADBG_HexLog(out, sizeof(out), 16);
1043 }
1044
1045 TEEC_CloseSession(&session1);
1046 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001047 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001048}
Jens Wiklander14f48872018-06-29 15:30:13 +02001049ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1050 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001051
1052struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001053 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001054 uint32_t cmd;
1055 uint32_t repeat;
1056 TEEC_SharedMemory *shm;
1057 uint32_t error_orig;
1058 TEEC_Result res;
1059 uint32_t max_concurrency;
1060 const uint8_t *in;
1061 size_t in_len;
1062 uint8_t *out;
1063 size_t out_len;
1064};
1065
1066static void *test_1013_thread(void *arg)
1067{
1068 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001069 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001070 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1071 uint8_t p2 = TEEC_NONE;
1072 uint8_t p3 = TEEC_NONE;
1073
Jens Wiklander70672972016-04-06 00:01:45 +02001074 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001075 &a->error_orig);
1076 if (a->res != TEEC_SUCCESS)
1077 return NULL;
1078
1079 op.params[0].memref.parent = a->shm;
1080 op.params[0].memref.size = a->shm->size;
1081 op.params[0].memref.offset = 0;
1082 op.params[1].value.a = a->repeat;
1083 op.params[1].value.b = 0;
1084 op.params[2].tmpref.buffer = (void *)a->in;
1085 op.params[2].tmpref.size = a->in_len;
1086 op.params[3].tmpref.buffer = a->out;
1087 op.params[3].tmpref.size = a->out_len;
1088
1089 if (a->in_len)
1090 p2 = TEEC_MEMREF_TEMP_INPUT;
1091 if (a->out_len)
1092 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1093
1094 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1095 TEEC_VALUE_INOUT, p2, p3);
1096
1097 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1098 a->max_concurrency = op.params[1].value.b;
1099 a->out_len = op.params[3].tmpref.size;
1100 TEEC_CloseSession(&session);
1101 return NULL;
1102}
1103
Pascal Brand4fa35582015-12-17 10:59:12 +01001104#define NUM_THREADS 3
1105
Jens Wiklander70672972016-04-06 00:01:45 +02001106static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1107 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001108{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001109 size_t nt = 0;
1110 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001111 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001112 TEEC_SharedMemory shm = { };
1113 size_t max_concurrency = 0;
1114 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001115 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1116 static const uint8_t sha256_out[] = {
1117 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1118 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1119 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1120 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1121 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001122 uint8_t out[32] = { };
1123 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001124 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001125
Jens Wiklander70672972016-04-06 00:01:45 +02001126 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001127 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001128
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001129 shm.size = sizeof(struct ta_concurrent_shm);
1130 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1131 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1132 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1133 return;
1134
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001135 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001136 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001137 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001138
1139 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001140 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001141 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001142 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001143 arg[n].shm = &shm;
1144 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1145 test_1013_thread, arg + n)))
1146 nt = n; /* break loop and start cleanup */
1147 }
1148
1149 for (n = 0; n < nt; n++) {
1150 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001151 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1152 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1153 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1154 skip = true;
1155 continue;
1156 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001157 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1158 if (arg[n].max_concurrency > max_concurrency)
1159 max_concurrency = arg[n].max_concurrency;
1160 }
1161
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001162 /*
1163 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001164 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001165 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1166 * best result there).
1167 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001168 if (!skip) {
1169 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1170 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1171 NUM_THREADS);
1172 *mean_concurrency += max_concurrency;
1173 }
Jens Wiklander70672972016-04-06 00:01:45 +02001174 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001175
Jens Wiklander70672972016-04-06 00:01:45 +02001176 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001177 memset(shm.buffer, 0, shm.size);
1178 memset(arg, 0, sizeof(arg));
1179 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001180 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001181
1182 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001183 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001184 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001185 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001186 arg[n].shm = &shm;
1187 arg[n].in = sha256_in;
1188 arg[n].in_len = sizeof(sha256_in);
1189 arg[n].out = out;
1190 arg[n].out_len = sizeof(out);
1191 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1192 test_1013_thread, arg + n)))
1193 nt = n; /* break loop and start cleanup */
1194 }
1195
1196 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001197 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1198 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1199 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1200 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1201 continue;
1202 }
1203 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001204 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1205 arg[n].out, arg[n].out_len);
1206 if (arg[n].max_concurrency > max_concurrency)
1207 max_concurrency = arg[n].max_concurrency;
1208 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001209 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001210 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001211
Pascal Brand4fa35582015-12-17 10:59:12 +01001212 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001213 TEEC_ReleaseSharedMemory(&shm);
1214}
Pascal Brand4fa35582015-12-17 10:59:12 +01001215
1216static void xtest_tee_test_1013(ADBG_Case_t *c)
1217{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001218 int i = 0;
1219 double mean_concurrency = 0;
1220 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001221 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001222
1223 if (level == 0)
1224 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001225
Jens Wiklander70672972016-04-06 00:01:45 +02001226 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001227 mean_concurrency = 0;
1228 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001229 xtest_tee_test_1013_single(c, &concurrency,
1230 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001231 mean_concurrency += concurrency;
1232 }
1233 mean_concurrency /= nb_loops;
1234
1235 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1236 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001237 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001238
Jens Wiklander70672972016-04-06 00:01:45 +02001239 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1240 mean_concurrency = 0;
1241 for (i = 0; i < nb_loops; i++) {
1242 xtest_tee_test_1013_single(c, &concurrency,
1243 &concurrent_large_ta_uuid);
1244 mean_concurrency += concurrency;
1245 }
1246 mean_concurrency /= nb_loops;
1247
1248 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1249 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1250 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1251}
Jens Wiklander14f48872018-06-29 15:30:13 +02001252ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001253 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001254
1255#ifdef CFG_SECURE_DATA_PATH
1256static void xtest_tee_test_1014(ADBG_Case_t *c)
1257{
1258 UNUSED(c);
1259
1260 int size = 17000;
1261 int loop = 10;
1262 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1263 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001264 int test = 0;
1265 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001266
1267 test = TEST_NS_TO_TA;
1268 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001269 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001270 ADBG_EXPECT(c, 0, ret);
1271 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1272
1273 test = TEST_TA_TO_TA;
1274 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001275 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001276 ADBG_EXPECT(c, 0, ret);
1277 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1278
1279 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001280 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001281 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001282 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001283 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001284
1285 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001286 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001287 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001288 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001289 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001290
1291 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
1292 ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
1293 ADBG_EXPECT(c, 0, ret);
1294 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001295}
Jens Wiklander14f48872018-06-29 15:30:13 +02001296ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1297 "Test secure data path against SDP TAs and pTAs");
1298#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001299
Jerome Forissierf4259942022-01-11 14:27:05 +01001300#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001301static void xtest_tee_test_1015(ADBG_Case_t *c)
1302{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001303 TEEC_Result res = TEEC_ERROR_GENERIC;
1304 TEEC_Session session = { };
1305 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001306
Etienne Carriere11093162017-10-26 09:49:04 +02001307 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001308 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1309 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001310 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1311 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001312 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001313 }
1314 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001315
1316 ADBG_EXPECT_TEEC_SUCCESS(c,
1317 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1318 NULL, &ret_orig));
1319 TEEC_CloseSession(&session);
1320}
Jens Wiklander14f48872018-06-29 15:30:13 +02001321ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1322 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001323#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001324
1325static void xtest_tee_test_1016(ADBG_Case_t *c)
1326{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001327 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001328 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001329 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001330
1331 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1332 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1333 &ret_orig)))
1334 return;
1335
1336 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1337 TEEC_NONE);
1338
1339 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1340 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1341 &ret_orig));
1342
1343 TEEC_CloseSession(&session);
1344}
Jens Wiklander14f48872018-06-29 15:30:13 +02001345ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1346 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001347
1348static void xtest_tee_test_1017(ADBG_Case_t *c)
1349{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001350 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001351 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001352 uint32_t ret_orig = 0;
1353 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001354 size_t page_size = 4096;
1355
Jens Wiklander87e81702018-03-20 12:00:00 +08001356 shm.size = 8 * page_size;
1357 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1358 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1359 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1360 return;
1361
1362 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1363 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1364 &ret_orig)))
1365 goto out;
1366
1367 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1368 TEEC_MEMREF_PARTIAL_INPUT,
1369 TEEC_MEMREF_PARTIAL_OUTPUT,
1370 TEEC_MEMREF_PARTIAL_OUTPUT);
1371
1372 /*
1373 * The first two memrefs are supposed to be combined into in
1374 * region and the last two memrefs should have one region each
1375 * when the parameters are mapped for the TA.
1376 */
1377 op.params[0].memref.parent = &shm;
1378 op.params[0].memref.size = page_size;
1379 op.params[0].memref.offset = 0;
1380
1381 op.params[1].memref.parent = &shm;
1382 op.params[1].memref.size = page_size;
1383 op.params[1].memref.offset = page_size;
1384
1385 op.params[2].memref.parent = &shm;
1386 op.params[2].memref.size = page_size;
1387 op.params[2].memref.offset = 4 * page_size;
1388
1389 op.params[3].memref.parent = &shm;
1390 op.params[3].memref.size = 2 * page_size;
1391 op.params[3].memref.offset = 6 * page_size;
1392
1393 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1394 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1395 &ret_orig));
1396
1397 TEEC_CloseSession(&session);
1398out:
1399 TEEC_ReleaseSharedMemory(&shm);
1400}
Jens Wiklander14f48872018-06-29 15:30:13 +02001401ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1402 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001403
Etienne Carriere84382b32018-04-25 18:30:30 +02001404static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1405 TEEC_SharedMemory *shm)
1406{
1407 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1408 TEEC_Result ret = TEEC_ERROR_GENERIC;
1409 uint32_t ret_orig = 0;
1410
1411 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1412 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1413
1414 op.params[0].memref.parent = shm;
1415 op.params[0].memref.size = shm->size / 2;
1416 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1417
1418 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1419 &op, &ret_orig);
1420
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001421 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001422 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1423 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1424 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1425 }
1426}
1427
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001428static void xtest_tee_test_1018(ADBG_Case_t *c)
1429{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001430 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001431 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001432 uint32_t ret_orig = 0;
1433 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001434 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001435 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001436 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001437 uint8_t buffer[6001] = { };
1438
1439 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1440 xtest_teec_open_session(&session,
1441 &os_test_ta_uuid,
1442 NULL,
1443 &ret_orig)))
1444 return;
1445
Joakim Becha1212b62020-04-07 12:06:00 +02001446 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001447
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001448 shm.size = 8 * page_size;
1449 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1450 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001451 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1452 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001453 goto out;
1454
1455 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1456 TEEC_MEMREF_PARTIAL_INPUT,
1457 TEEC_MEMREF_PARTIAL_OUTPUT,
1458 TEEC_MEMREF_PARTIAL_OUTPUT);
1459
1460 /*
1461 * The first two memrefs are supposed to be combined into in
1462 * region and the last two memrefs should have one region each
1463 * when the parameters are mapped for the TA.
1464 */
1465 op.params[0].memref.parent = &shm;
1466 op.params[0].memref.size = page_size;
1467 op.params[0].memref.offset = 0;
1468
1469 op.params[1].memref.parent = &shm;
1470 op.params[1].memref.size = page_size;
1471 op.params[1].memref.offset = page_size;
1472
1473 op.params[2].memref.parent = &shm;
1474 op.params[2].memref.size = page_size;
1475 op.params[2].memref.offset = 4 * page_size;
1476
1477 op.params[3].memref.parent = &shm;
1478 op.params[3].memref.size = 3 * page_size;
1479 op.params[3].memref.offset = 6 * page_size;
1480
Etienne Carriere84382b32018-04-25 18:30:30 +02001481 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1482 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001483
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001484 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001485 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1486 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1487 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1488 }
1489
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001490 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001491 Do_ADBG_EndSubCase(c, NULL);
1492
1493 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1494
1495 memset(&shm, 0, sizeof(shm));
1496 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1497 shm.buffer = buffer;
1498 shm.size = sizeof(buffer);
1499
1500 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1501 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1502 &shm)))
1503 goto out;
1504
1505 invoke_1byte_out_of_bounds(c, &session, &shm);
1506
1507 TEEC_ReleaseSharedMemory(&shm);
1508 Do_ADBG_EndSubCase(c, NULL);
1509
1510 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1511
1512 memset(&shm, 0, sizeof(shm));
1513 shm.size = sizeof(buffer);
1514 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1515 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1516 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1517 &shm)))
1518 goto out;
1519
1520 invoke_1byte_out_of_bounds(c, &session, &shm);
1521
1522 TEEC_ReleaseSharedMemory(&shm);
1523 Do_ADBG_EndSubCase(c, NULL);
1524
1525out:
1526 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001527}
Jens Wiklander14f48872018-06-29 15:30:13 +02001528ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1529 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001530
Jerome Forissier53bde722018-05-31 09:14:54 +02001531static void xtest_tee_test_1019(ADBG_Case_t *c)
1532{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001533 TEEC_Session session = { };
1534 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001535
1536 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1537 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1538 &ret_orig)))
1539 return;
1540
1541 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1542 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1543 &ret_orig));
1544
1545 (void)ADBG_EXPECT_TEEC_RESULT(c,
1546 TEEC_ERROR_TARGET_DEAD,
1547 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1548 NULL, &ret_orig));
1549
1550 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1551
1552 TEEC_CloseSession(&session);
1553}
Jens Wiklander14f48872018-06-29 15:30:13 +02001554ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1555 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001556
1557static void xtest_tee_test_1020(ADBG_Case_t *c)
1558{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001559 TEEC_Result res = TEEC_ERROR_GENERIC;
1560 TEEC_Session session = { };
1561 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001562
1563 /* Pseudo TA is optional: warn and nicely exit if not found */
1564 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1565 &ret_orig);
1566 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1567 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1568 return;
1569 }
1570 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1571
1572 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1573 NULL, &ret_orig);
1574 if (res != TEEC_SUCCESS) {
1575 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1576 ret_orig);
1577 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1578 Do_ADBG_Log(" - 1020 - skip test, feature not "
1579 "implemented");
1580 goto out;
1581 }
1582 /* Error */
1583 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1584 }
1585out:
1586 TEEC_CloseSession(&session);
1587}
1588ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1589 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001590
1591static TEEC_Result open_sec_session(TEEC_Session *session,
1592 const TEEC_UUID *uuid)
1593{
1594 TEEC_Result res = TEEC_ERROR_GENERIC;
1595 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1596 uint32_t ret_orig = 0;
1597
1598 op.params[0].tmpref.buffer = (void *)uuid;
1599 op.params[0].tmpref.size = sizeof(*uuid);
1600 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1601 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1602
1603 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1604 &op, &ret_orig);
1605 if (res != TEEC_SUCCESS)
1606 return TEEC_ERROR_GENERIC;
1607
1608 return res;
1609}
1610
1611static TEEC_Result sims_get_counter(TEEC_Session *session,
1612 uint32_t *counter)
1613{
1614 TEEC_Result res = TEEC_ERROR_GENERIC;
1615 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1616 uint32_t ret_orig = 0;
1617
1618 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1619 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1620
1621 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1622 &op, &ret_orig);
1623 if (res == TEEC_SUCCESS)
1624 *counter = op.params[0].value.a;
1625
1626 return res;
1627}
1628
1629static TEEC_Result trigger_panic(TEEC_Session *session,
1630 const TEEC_UUID *uuid)
1631{
1632 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1633 uint32_t ret_orig = 0;
1634
1635 if (!uuid) {
1636 op.params[0].tmpref.buffer = NULL;
1637 op.params[0].tmpref.size = 0;
1638 } else {
1639 op.params[0].tmpref.buffer = (void *)uuid;
1640 op.params[0].tmpref.size = sizeof(*uuid);
1641 }
1642 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1643 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1644
1645 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1646 &op, &ret_orig);
1647}
1648
1649static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1650 bool multi_instance)
1651{
1652 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1653 uint32_t counter = 0;
1654 uint32_t ret_orig = 0;
1655 uint32_t exp_counter = 0;
1656 TEEC_Session cs[3] = { };
1657
1658 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1659 xtest_teec_open_session(&cs[0], uuid, NULL,
1660 &ret_orig)))
1661 return;
1662
1663 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1664 xtest_teec_open_session(&cs[1], uuid, NULL,
1665 &ret_orig)))
1666 goto bail0;
1667
1668 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1669 goto bail1;
1670
1671 if (!ADBG_EXPECT(c, 0, counter))
1672 goto bail1;
1673
1674 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1675 goto bail1;
1676
1677 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001678 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001679 goto bail1;
1680
1681 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1682 trigger_panic(&cs[1], NULL)))
1683 goto bail1;
1684
1685 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1686 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1687 sims_get_counter(&cs[0], &counter)))
1688 goto bail1;
1689
1690 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1691 sims_get_counter(&cs[1], &counter)))
1692 goto bail1;
1693
1694 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001695 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001696 xtest_teec_open_session(&cs[1], uuid, NULL,
1697 &ret_orig)))
1698 goto bail1;
1699
1700 /* Sanity check of still valid TA context */
1701 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1702 xtest_teec_open_session(&cs[2], uuid, NULL,
1703 &ret_orig)))
1704 goto bail1;
1705
1706 /* Sanity check of still valid TA context */
1707 if (multi_instance) {
1708 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1709 sims_get_counter(&cs[0], &counter)))
1710 goto bail2;
1711
1712 if (!ADBG_EXPECT(c, 0, counter))
1713 goto bail2;
1714 }
1715
1716 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1717 goto bail2;
1718
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001719 exp_counter = multi_instance ? 0 : 1;
1720 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001721 goto bail2;
1722
1723bail2:
1724 TEEC_CloseSession(&cs[2]);
1725bail1:
1726 TEEC_CloseSession(&cs[1]);
1727bail0:
1728 TEEC_CloseSession(&cs[0]);
1729}
1730
1731static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1732 const TEEC_UUID *uuid2)
1733{
1734 uint32_t ret_orig = 0;
1735 uint32_t counter = 0;
1736 TEEC_Session cs[3] = { };
1737
1738 /* Test pre-conditions */
1739 /* 2.1 - CA opens a session toward TA1 */
1740 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1741 xtest_teec_open_session(&cs[0], uuid1, NULL,
1742 &ret_orig)))
1743 return;
1744
1745 /* 2.2 - CA opens a session toward TA2 */
1746 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1747 xtest_teec_open_session(&cs[1], uuid2, NULL,
1748 &ret_orig)))
1749 goto bail0;
1750
1751 /* 2.3 - TA1 opens a session toward TA2 */
1752 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1753 goto bail1;
1754
1755 /* 2.4 - CA invokes TA2 which panics */
1756 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1757 trigger_panic(&cs[1], NULL)))
1758 goto bail1;
1759
1760 /* Expected results */
1761 /* 2.5 - Expect CA->TA1 session is still alive */
1762 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1763 goto bail1;
1764
1765 /* 2.6 - Expect CA->TA2 session is properly released */
1766 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1767 sims_get_counter(&cs[1], &counter)))
1768 goto bail1;
1769
1770bail1:
1771 TEEC_CloseSession(&cs[1]);
1772bail0:
1773 TEEC_CloseSession(&cs[0]);
1774
1775 memset(cs, 0, sizeof(cs));
1776
1777 /* Test pre-conditions */
1778 /* 2.1 - CA opens a session toward TA1 */
1779 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1780 xtest_teec_open_session(&cs[0], uuid1, NULL,
1781 &ret_orig)))
1782 return;
1783
1784 /* 2.2 - CA opens a session toward TA2 */
1785 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1786 xtest_teec_open_session(&cs[1], uuid2, NULL,
1787 &ret_orig)))
1788 goto bail2;
1789
1790 /* 2.3 - TA1 opens a session toward TA2 */
1791 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1792 goto bail3;
1793
1794 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1795 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1796 goto bail3;
1797
1798 /* Expected results */
1799 /* 2.5 - Expect CA->TA1 session is still alive */
1800 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1801 goto bail3;
1802
1803 /* 2.6 - Expect CA->TA2 session is properly released */
1804 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1805 sims_get_counter(&cs[1], &counter)))
1806 goto bail3;
1807
1808bail3:
1809 TEEC_CloseSession(&cs[1]);
1810bail2:
1811 TEEC_CloseSession(&cs[0]);
1812}
1813
1814static void xtest_tee_test_1021(ADBG_Case_t *c)
1815{
1816 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1817 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1818 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1819
1820 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1821 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1822 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1823
1824 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1825 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1826 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1827
1828 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1829 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1830 &sims_keepalive_test_ta_uuid);
1831 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1832}
1833ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1834 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001835
1836static void xtest_tee_test_1022(ADBG_Case_t *c)
1837{
1838 TEEC_Session session = { 0 };
1839 uint32_t ret_orig = 0;
1840
1841 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1842 xtest_teec_open_session(&session, &os_test_ta_uuid,
1843 NULL, &ret_orig)))
1844 return;
1845
1846 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1847 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1848 &ret_orig));
1849
1850 (void)ADBG_EXPECT_TEEC_RESULT(c,
1851 TEEC_ERROR_TARGET_DEAD,
1852 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1853 NULL, &ret_orig));
1854
1855 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1856
1857 TEEC_CloseSession(&session);
1858}
1859ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1860 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001861
1862/*
1863 * Testing the ELF initialization (.init_array)
1864 *
1865 * - The TA has a global variable which can also be accessed by the two shared
1866 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1867 * dlopen())
1868 * - The TA and both libraries have initialization functions (declared with the
1869 * "constructor" attribute) which perform the following:
1870 * * The TA multiplies by 10 then adds 1
1871 * * os_test_lib multiplies by 10 then adds 2
1872 * * os_test_lib_dl multiplies by 10 then adds 3
1873 * By testing the variable value we make sure the initializations occurred in
1874 * the correct order.
1875 */
1876static void xtest_tee_test_1023(ADBG_Case_t *c)
1877{
1878 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1879 TEEC_Session session = { 0 };
1880 uint32_t ret_orig = 0;
1881
1882 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1883 TEEC_NONE, TEEC_NONE);
1884
1885 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1886 xtest_teec_open_session(&session, &os_test_ta_uuid,
1887 NULL, &ret_orig)))
1888 return;
1889
1890 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1891 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1892 &ret_orig));
1893
1894 /* Expected: initialization of os_test_lib, then TA */
1895 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1896
1897 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1898 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1899 &ret_orig));
1900
1901 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1902 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1903 &ret_orig));
1904
1905 /* Expected: initialization of os_test_lib_dl */
1906 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1907
1908 TEEC_CloseSession(&session);
1909}
1910ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1911 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001912
1913#ifdef CFG_CORE_TPM_EVENT_LOG
1914static void xtest_tee_test_1024(ADBG_Case_t *c)
1915{
1916 TEEC_Session session = {};
1917 uint32_t ret_orig = 0;
1918
1919 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1920 NULL, &ret_orig);
1921
1922 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1923 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1924 TA_TPM_TEST_GET_LOG,
1925 NULL, &ret_orig));
1926 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1927
1928 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1929 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1930 TA_TPM_TEST_SHORT_BUF,
1931 NULL, &ret_orig));
1932 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1933
1934 TEEC_CloseSession(&session);
1935}
1936
1937ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1938 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1939#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001940
1941static void xtest_tee_test_1025(ADBG_Case_t *c)
1942{
1943 TEEC_Session session = {};
1944 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1945 uint32_t ret_orig = 0;
1946 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001947 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001948 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001949
1950 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1951
1952 memset(&shm, 0, sizeof(shm));
1953 shm.flags = TEEC_MEM_INPUT;
1954 shm.buffer = NULL;
1955 shm.size = 0;
1956
1957 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1958 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1959
1960 memset(&shm, 0, sizeof(shm));
1961 shm.flags = TEEC_MEM_OUTPUT;
1962 shm.buffer = NULL;
1963 shm.size = 0;
1964
1965 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1966 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1967
1968 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001969
Etienne Carrierec602a522020-04-13 18:53:17 +02001970 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001971 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001972 return;
1973 }
1974
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001975 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1976 xtest_teec_open_session(&session,
1977 &os_test_ta_uuid,
1978 NULL, &ret_orig)))
1979 return;
1980
1981 empty_buf = malloc(1);
1982 if (!empty_buf) {
1983 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001984 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001985 }
1986
1987 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1988 TEEC_MEMREF_TEMP_INPUT,
1989 TEEC_MEMREF_TEMP_OUTPUT,
1990 TEEC_MEMREF_TEMP_OUTPUT);
1991
1992 Do_ADBG_BeginSubCase(c,
1993 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1994
1995 op.params[0].tmpref.buffer = empty_buf;
1996 op.params[0].tmpref.size = 0;
1997
1998 op.params[1].tmpref.buffer = NULL;
1999 op.params[1].tmpref.size = 0;
2000
2001 op.params[2].tmpref.buffer = empty_buf;
2002 op.params[2].tmpref.size = 0;
2003
2004 op.params[3].tmpref.buffer = NULL;
2005 op.params[3].tmpref.size = 0;
2006
2007 ADBG_EXPECT(c, TEE_SUCCESS,
2008 TEEC_InvokeCommand(&session,
2009 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2010 &ret_orig));
2011
2012 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2013
2014 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2015
2016 op.params[0].tmpref.buffer = empty_buf;
2017 op.params[0].tmpref.size = 1;
2018
2019 op.params[1].tmpref.buffer = NULL;
2020 op.params[1].tmpref.size = 0;
2021
2022 op.params[2].tmpref.buffer = empty_buf;
2023 op.params[2].tmpref.size = 0;
2024
2025 op.params[3].tmpref.buffer = NULL;
2026 op.params[3].tmpref.size = 0;
2027
2028 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2029 TEEC_InvokeCommand(&session,
2030 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2031 &ret_orig));
2032
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002033 TEEC_CloseSession(&session);
2034
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002035 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2036
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002037 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2038
2039 /* Pseudo TA is optional: warn and nicely exit if not found */
2040 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2041 &ret_orig);
2042 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2043 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2044 goto out;
2045 }
2046 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2047 goto out;
2048
2049 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2050 TEEC_NONE, TEEC_NONE);
2051 op.params[0].tmpref.buffer = NULL;
2052 op.params[0].tmpref.size = 0;
2053
2054 ADBG_EXPECT(c, TEE_SUCCESS,
2055 TEEC_InvokeCommand(&session,
2056 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2057 &op, &ret_orig));
2058
2059out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002060 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002061out:
2062 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002063 free(empty_buf);
2064}
2065ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2066 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002067
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002068#define TEE_UUID_NS_NAME_SIZE 128
2069
2070/*
2071 * TEE Client UUID name space identifier (UUIDv4)
2072 *
2073 * Value here is random UUID that is allocated as name space identifier for
2074 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2075 */
2076static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2077
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002078/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2079static TEEC_UUID client_uuid_public = { };
2080
2081static void xtest_tee_test_1026(ADBG_Case_t *c)
2082{
2083 TEEC_Result result = TEEC_ERROR_GENERIC;
2084 uint32_t ret_orig = 0;
2085 TEEC_Session session = { };
2086 uint32_t login = UINT32_MAX;
2087 TEEC_UUID client_uuid = { };
2088
2089 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2090 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2091
2092 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2093 return;
2094
2095 result = ta_os_test_cmd_client_identity(&session, &login,
2096 &client_uuid);
2097
2098 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2099 goto out;
2100
2101 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2102
2103 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2104 sizeof(TEEC_UUID));
2105
2106out:
2107 TEEC_CloseSession(&session);
2108}
2109
2110ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2111 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002112
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002113/*
2114 * regression_1027
2115 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002116 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2117 * login client UUID generation")
2118 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002119 *
2120 * xtest skips the test when not built with OpenSSL.
2121 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002122static void xtest_tee_test_1027(ADBG_Case_t *c)
2123{
Victor Chong8e070bc2020-05-13 09:59:33 +01002124#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002125 TEEC_Result result = TEEC_ERROR_GENERIC;
2126 uint32_t ret_orig = 0;
2127 TEEC_Session session = { };
2128 uint32_t login = UINT32_MAX;
2129 TEEC_UUID client_uuid = { };
2130 TEEC_UUID expected_client_uuid = { };
2131 TEEC_UUID uuid_ns = { };
2132 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2133
2134 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2135
2136 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2137 return;
2138
2139 sprintf(uuid_name, "uid=%x", geteuid());
2140
2141 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2142 strlen(uuid_name));
2143 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2144 return;
2145
2146 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2147 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2148
2149 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2150 return;
2151
2152 result = ta_os_test_cmd_client_identity(&session, &login,
2153 &client_uuid);
2154
2155 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2156 goto out;
2157
2158 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2159
2160 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2161 sizeof(TEEC_UUID));
2162
2163out:
2164 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002165#else /*!OPENSSL_FOUND*/
2166 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002167 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002168 /* xtest_uuid_v5() depends on OpenSSL */
2169 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2170#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002171}
2172
2173ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2174 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002175
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002176/*
2177 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002178 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2179 * login client UUID generation")
2180 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002181 *
2182 * xtest skips the test when not built with OpenSSL.
2183 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002184static void xtest_tee_test_1028(ADBG_Case_t *c)
2185{
Victor Chong8e070bc2020-05-13 09:59:33 +01002186#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002187 TEEC_Result result = TEEC_ERROR_GENERIC;
2188 uint32_t ret_orig = 0;
2189 TEEC_Session session = { };
2190 uint32_t login = UINT32_MAX;
2191 TEEC_UUID client_uuid = { };
2192 TEEC_UUID expected_client_uuid = { };
2193 TEEC_UUID uuid_ns = { };
2194 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2195 uint32_t group = 0;
2196
2197 group = getegid();
2198
2199 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2200
2201 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2202 return;
2203
2204 sprintf(uuid_name, "gid=%x", group);
2205
2206 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2207 strlen(uuid_name));
2208 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2209 return;
2210
2211 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2212 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2213
2214 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2215 return;
2216
2217 result = ta_os_test_cmd_client_identity(&session, &login,
2218 &client_uuid);
2219
2220 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2221 goto out;
2222
2223 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2224
2225 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2226 sizeof(TEEC_UUID));
2227
2228out:
2229 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002230#else /*!OPENSSL_FOUND*/
2231 UNUSED(c);
2232 /* xtest_uuid_v5() depends on OpenSSL */
2233 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2234#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002235}
2236
2237ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2238 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002239
2240static void xtest_tee_test_1029(ADBG_Case_t *c)
2241{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002242 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002243 TEEC_Session session = { 0 };
2244 uint32_t ret_orig = 0;
2245
2246 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2247 xtest_teec_open_session(&session, &os_test_ta_uuid,
2248 NULL, &ret_orig)))
2249 return;
2250
2251 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002252 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2253 &ret_orig);
2254 if (res == TEEC_ERROR_NOT_SUPPORTED)
2255 Do_ADBG_Log(" - 1029 - skip test, "
2256 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2257 else
2258 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002259 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2260
2261 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002262 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2263 &ret_orig);
2264 if (res == TEEC_ERROR_NOT_SUPPORTED)
2265 Do_ADBG_Log(" - 1029 - skip test, "
2266 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2267 else
2268 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002269 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2270
2271 TEEC_CloseSession(&session);
2272}
2273ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2274 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002275
2276static void xtest_tee_test_1030(ADBG_Case_t *c)
2277{
2278 TEEC_Session session = { 0 };
2279 uint32_t ret_orig = 0;
2280
2281 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2282 xtest_teec_open_session(&session, &os_test_ta_uuid,
2283 NULL, &ret_orig)))
2284 return;
2285
2286 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2287 ADBG_EXPECT_TEEC_SUCCESS(c,
2288 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2289 &ret_orig));
2290 Do_ADBG_EndSubCase(c, "Before dlopen()");
2291
2292 Do_ADBG_BeginSubCase(c, "After dlopen()");
2293 ADBG_EXPECT_TEEC_SUCCESS(c,
2294 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2295 &ret_orig));
2296 Do_ADBG_EndSubCase(c, "After dlopen()");
2297
2298 TEEC_CloseSession(&session);
2299}
2300ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2301 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002302
2303#ifndef __clang__
2304static void xtest_tee_test_1031(ADBG_Case_t *c)
2305{
2306 TEEC_Result ret = TEE_SUCCESS;
2307 TEEC_Session session = { 0 };
2308 uint32_t ret_orig = 0;
2309
2310 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2311 xtest_teec_open_session(&session, &os_test_ta_uuid,
2312 NULL, &ret_orig)))
2313 return;
2314
2315 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2316 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2317 &ret_orig);
2318 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2319 printf("TA not built with C++ support, skipping C++ tests\n");
2320 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2321 goto out;
2322
2323 }
2324 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2325 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2326
2327 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2328 ADBG_EXPECT_TEEC_SUCCESS(c,
2329 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2330 NULL, &ret_orig));
2331 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2332
2333 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2334 ADBG_EXPECT_TEEC_SUCCESS(c,
2335 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2336 NULL, &ret_orig));
2337 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2338
2339 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2340 ADBG_EXPECT_TEEC_SUCCESS(c,
2341 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2342 &ret_orig));
2343 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2344
2345 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2346 ADBG_EXPECT_TEEC_SUCCESS(c,
2347 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2348 &ret_orig));
2349 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2350out:
2351 TEEC_CloseSession(&session);
2352}
2353ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2354 "Test C++ features");
2355#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002356
2357static void xtest_tee_test_1032(ADBG_Case_t *c)
2358{
2359 TEEC_Result res = TEEC_SUCCESS;
2360 TEEC_Context ctx = { };
2361 TEEC_SharedMemory shm1 = {
2362 .buffer = xtest_tee_test_1032,
2363 .size = 32,
2364 .flags = TEEC_MEM_INPUT,
2365 };
2366 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2367 TEEC_SharedMemory shm2 = {
2368 .buffer = (void *)dummy_data,
2369 .size = sizeof(dummy_data),
2370 .flags = TEEC_MEM_INPUT,
2371 };
2372
2373 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2374 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2375 return;
2376
2377 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2378 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2379 TEEC_ReleaseSharedMemory(&shm1);
2380
2381 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2382 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2383 TEEC_ReleaseSharedMemory(&shm2);
2384
2385 TEEC_FinalizeContext(&ctx);
2386}
2387ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2388 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002389
2390static void xtest_tee_test_1033(ADBG_Case_t *c)
2391{
2392 TEEC_Session session = { };
2393 uint32_t ret_orig = 0;
2394
2395 /* TA will ping the test plugin during open session operation */
2396 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2397 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2398 NULL, &ret_orig)))
2399 return;
2400
2401 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2402 {
2403 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2404
2405 op.params[0].value.a = 20;
2406 op.params[0].value.b = 10;
2407 op.params[1].value.a = '+';
2408 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2409 TEEC_VALUE_INPUT, TEEC_NONE,
2410 TEEC_NONE);
2411
2412 ADBG_EXPECT_TEEC_SUCCESS(c,
2413 TEEC_InvokeCommand(&session,
2414 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2415 &ret_orig));
2416 ADBG_EXPECT(c, 30, op.params[0].value.a);
2417
2418 /* reassign, because the values was changed during previous op */
2419 op.params[0].value.a = 20;
2420 op.params[0].value.b = 10;
2421 op.params[1].value.a = '-';
2422 ADBG_EXPECT_TEEC_SUCCESS(c,
2423 TEEC_InvokeCommand(&session,
2424 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2425 &ret_orig));
2426 ADBG_EXPECT(c, 10, op.params[0].value.a);
2427 }
2428 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2429
2430 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2431 {
2432 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002433 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002434
2435 op.params[0].tmpref.buffer = to_plugin;
2436 op.params[0].tmpref.size = sizeof(to_plugin);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002437 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002438 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002439 TEEC_NONE, TEEC_NONE);
2440
2441 ADBG_EXPECT_TEEC_SUCCESS(c,
2442 TEEC_InvokeCommand(&session,
2443 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2444 &op, &ret_orig));
2445
2446 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002447 * The test plugin must calculate a sum of the input elements
2448 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002449 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002450 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002451 }
2452 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2453
2454 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2455 {
2456 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2457 char from_plugin[64] = { };
2458 char expected_arr[] = "Array from plugin";
2459 size_t expectes_size = sizeof(expected_arr);
2460
2461 op.params[0].tmpref.buffer = from_plugin;
2462 op.params[0].tmpref.size = sizeof(from_plugin);
2463 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2464 TEEC_VALUE_OUTPUT, TEEC_NONE,
2465 TEEC_NONE);
2466 ADBG_EXPECT_TEEC_SUCCESS(c,
2467 TEEC_InvokeCommand(&session,
2468 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2469 &ret_orig));
2470 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2471 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2472 }
2473 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2474
2475 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2476 {
2477 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2478
2479 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2480 TEEC_NONE, TEEC_NONE);
2481 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2482 TEEC_InvokeCommand(&session,
2483 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2484 &ret_orig));
2485 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2486 TEEC_InvokeCommand(&session,
2487 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2488 &ret_orig));
2489 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2490 TEEC_InvokeCommand(&session,
2491 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2492 &ret_orig));
2493 }
2494 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2495
2496 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2497 {
2498 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2499
2500 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2501 TEEC_NONE, TEEC_NONE);
2502 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2503 TEEC_InvokeCommand(&session,
2504 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2505 &op, &ret_orig));
2506 }
2507 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2508
2509 TEEC_CloseSession(&session);
2510}
2511ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2512 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002513
2514static void xtest_tee_test_1034(ADBG_Case_t *c)
2515{
2516 TEEC_Result res = TEEC_SUCCESS;
2517 TEEC_Session session = { };
2518 uint32_t ret_orig = 0;
2519
2520 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2521 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002522 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2523 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
2524 } else {
2525 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002526 TEEC_CloseSession(&session);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002527 }
Jens Wiklander94fb1582021-05-19 10:14:57 +02002528}
2529ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2530 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302531
2532#if defined(CFG_TA_BTI)
2533struct bti_test {
2534 uint32_t cmd;
2535 uint32_t func;
2536};
2537
2538#define BTI_TEST(caller_func, bti_func) { \
2539 .cmd = caller_func, \
2540 .func = bti_func, \
2541 }
2542
2543static const struct bti_test bti_cases_success[] = {
2544 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2545 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2546 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2547 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2548 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2549 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2550 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2551};
2552
2553static const struct bti_test bti_cases_panic[] = {
2554 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2555 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2556 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2557 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2558 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2559};
2560
2561static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2562{
2563 TEEC_Session session = {};
2564 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2565 uint32_t ret_orig = 0;
2566 TEEC_Result res;
2567
2568 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2569 TEEC_NONE);
2570 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2571 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2572 NULL, &ret_orig)))
2573 return;
2574
2575 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2576 &ret_orig);
2577 if (!res) {
2578 if(op.params[0].value.a)
2579 *bti = true;
2580 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2581 }
2582
2583 TEEC_CloseSession(&session);
2584}
2585
2586static void xtest_tee_test_1035(ADBG_Case_t *c)
2587{
2588 TEEC_Session session = {};
2589 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2590 struct bti_test const *test = NULL;
2591 uint32_t ret_orig = 0;
2592 TEEC_Result res;
2593 unsigned int n = 0;
2594 bool cpu_feature_bti = false;
2595
2596 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2597 get_cpu_feature(c, &cpu_feature_bti);
2598 Do_ADBG_EndSubCase(c, "BTI Implemented");
2599
2600 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2601 TEEC_NONE);
2602
2603 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2604 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2605 NULL, &ret_orig)))
2606 return;
2607
2608 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2609 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2610 test = &bti_cases_success[n];
2611
2612 op.params[0].value.a = test->func;
2613
2614 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2615 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2616 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2617 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2618 TEEC_CloseSession(&session);
2619 return;
2620 }
2621
2622 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2623
2624 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2625
2626 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2627 }
2628 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2629
2630 TEEC_CloseSession(&session);
2631
2632 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2633 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2634 test = &bti_cases_panic[n];
2635 res = TEEC_SUCCESS;
2636
2637 if (cpu_feature_bti)
2638 res = TEEC_ERROR_TARGET_DEAD;
2639
2640 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2641 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2642 NULL, &ret_orig)))
2643 goto out;
2644
2645 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2646 op.params[0].value.a = test->func;
2647
2648 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2649 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2650
2651 if (cpu_feature_bti)
2652 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2653 ret_orig);
2654
2655 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2656
2657 TEEC_CloseSession(&session);
2658 }
2659
2660out:
2661 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2662}
2663ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2664#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302665
2666static void xtest_tee_test_1036(ADBG_Case_t *c)
2667{
2668 TEEC_Session session = { };
2669 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2670 uint32_t ret_orig = 0;
2671 TEEC_Result res = TEEC_SUCCESS;
2672
2673 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2674 TEEC_NONE);
2675
2676 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2677 xtest_teec_open_session(&session, &os_test_ta_uuid,
2678 NULL, &ret_orig)))
2679 return;
2680
2681 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2682
2683 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2684 &ret_orig);
2685 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2686 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2687 goto out;
2688 }
2689
2690 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2691 goto out;
2692
2693 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2694
2695 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2696
2697 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2698 TEEC_InvokeCommand(&session,
2699 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2700 &op, &ret_orig));
2701
2702 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2703
2704 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2705
2706out:
2707 TEEC_CloseSession(&session);
2708}
2709ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2710 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002711
2712#define ATT_MAX_KEYSZ 4096
2713
2714#ifdef OPENSSL_FOUND
2715static RSA *att_key;
2716static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2717
2718/*
2719 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2720 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2721 */
2722static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2723 uint8_t *buf)
2724{
2725 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2726 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2727 unsigned char *ta_hash = buf;
2728 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2729 SHA256_CTX ctx = { };
2730 int salt_len = 32; /* Hard-coded in the PTA */
2731 int st = 0;
2732
2733 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2734 return;
2735
2736 SHA256_Init(&ctx);
2737 SHA256_Update(&ctx, nonce, nonce_size);
2738 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2739 SHA256_Final(digest, &ctx);
2740
2741 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2742 RSA_NO_PADDING);
2743 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2744 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2745 EVP_sha256(), decr, salt_len);
2746 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2747}
2748
2749static void free_att_key(void)
2750{
2751 RSA_free(att_key);
2752 att_key = NULL;
2753}
2754
2755static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2756 size_t n_sz)
2757{
2758 BIGNUM *bn_e = NULL;
2759 BIGNUM *bn_n = NULL;
2760 int st = 0;
2761
2762 att_key_size = n_sz;
2763 att_key = RSA_new();
2764 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2765 return;
2766
2767 bn_e = BN_bin2bn(e, e_sz, NULL);
2768 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2769 goto err;
2770 bn_n = BN_bin2bn(n, n_sz, NULL);
2771 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2772 goto err;
2773
2774 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2775 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2776 goto err;
2777 return;
2778err:
2779 free_att_key();
2780}
2781#else
2782#define check_signature(...)
2783#define set_att_key(...)
2784#define free_att_key()
2785#endif
2786
2787/*
2788 * Verification of the output of the attestation PTA
2789 * - (If hash != NULL) check buf contains the expected hash
2790 * - (If OpenSSL is available) Check that the signature is valid
2791 */
2792static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2793 uint8_t *hash, uint8_t *buf)
2794{
2795 if (hash)
2796 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2797
2798 check_signature(c, nonce, nonce_size, buf);
2799}
2800
2801/* Invoke attestation PTA to return the public key */
2802static void get_att_public_key(ADBG_Case_t *c)
2803{
2804 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2805 uint8_t e[3] = { }; /* We know e == 65537... */
2806 size_t n_sz = sizeof(n);
2807 size_t e_sz = sizeof(e);
2808 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2809 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2810 TEEC_Result res = TEEC_ERROR_GENERIC;
2811 TEEC_Session session = { };
2812 uint32_t ret_orig = 0;
2813
2814 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2815 TEEC_MEMREF_TEMP_OUTPUT,
2816 TEEC_VALUE_OUTPUT, TEEC_NONE);
2817 op.params[0].tmpref.buffer = e;
2818 op.params[0].tmpref.size = e_sz;
2819 op.params[1].tmpref.buffer = n;
2820 op.params[1].tmpref.size = n_sz;
2821
2822 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2823 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2824 return;
2825
2826 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2827 &ret_orig);
2828
2829 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2830 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2831 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2832 goto out;
2833
2834 e_sz = op.params[0].tmpref.size;
2835 n_sz = op.params[1].tmpref.size;
2836 set_att_key(c, e, e_sz, n, n_sz);
2837out:
2838 TEEC_CloseSession(&session);
2839}
2840
2841/* Invoke attestation PTA to hash the TEE binary */
2842static void attestation_tee(ADBG_Case_t *c)
2843{
2844 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2845 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2846 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2847 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2848 TEEC_Result res = TEEC_ERROR_GENERIC;
2849 TEEC_Session session = { };
2850 uint32_t ret_orig = 0;
2851
2852 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2853 TEEC_MEMREF_TEMP_OUTPUT,
2854 TEEC_NONE, TEEC_NONE);
2855 op.params[0].tmpref.buffer = nonce;
2856 op.params[0].tmpref.size = sizeof(nonce);
2857 op.params[1].tmpref.buffer = measurement;
2858 op.params[1].tmpref.size = sizeof(measurement);
2859
2860 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2861 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2862 return;
2863
2864 /* Hash TEE and check signature */
2865 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2866 &ret_orig);
2867 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2868 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2869
2870 TEEC_CloseSession(&session);
2871}
2872
2873/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2874static void attestation_ta_shdr(ADBG_Case_t *c)
2875{
2876 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2877 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2878 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2879 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2880 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2881 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2882 TEEC_Result res = TEEC_ERROR_GENERIC;
2883 TEEC_Session session = { };
2884 uint32_t ret_orig = 0;
2885 int cmp = 0;
2886
2887 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2888 TEEC_MEMREF_TEMP_INPUT,
2889 TEEC_MEMREF_TEMP_OUTPUT,
2890 TEEC_NONE);
2891 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2892 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2893 op.params[1].tmpref.buffer = nonce;
2894 op.params[1].tmpref.size = sizeof(nonce);
2895 op.params[2].tmpref.buffer = measurement;
2896 op.params[2].tmpref.size = sizeof(measurement);
2897
2898 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2899 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2900 return;
2901
2902 /* Hash TA and check signature */
2903 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2904 &op, &ret_orig);
2905 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2906 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2907 /* Save hash */
2908 memcpy(hash1, measurement, 32);
2909
2910 /* Hash TA again */
2911 memset(measurement, 0, sizeof(measurement));
2912 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2913 &op, &ret_orig);
2914 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2915 /* New hash should be identical */
2916 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2917
2918 /* Hash another TA */
2919 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
2920 memset(measurement, 0, sizeof(measurement));
2921 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2922 &op, &ret_orig);
2923 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2924 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2925 memcpy(hash2, measurement, 32);
2926 /* Different binaries should have different hashes */
2927 cmp = memcmp(hash1, hash2, sizeof(hash1));
2928 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
2929
2930 TEEC_CloseSession(&session);
2931}
2932
2933/*
2934 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
2935 * itself.
2936 */
2937static void attestation_ta_memory(ADBG_Case_t *c)
2938{
2939 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2940 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2941 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2942 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2943 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2944 TEEC_Result res = TEEC_ERROR_GENERIC;
2945 TEEC_Session session = { };
2946 uint32_t ret_orig = 0;
2947 int cmp = 0;
2948
2949 Do_ADBG_BeginSubCase(c, "Consecutive calls");
2950
2951 /* Open session to os_test TA */
2952 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
2953 &ret_orig);
2954 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2955 return;
2956
2957 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2958 TEEC_MEMREF_TEMP_OUTPUT,
2959 TEEC_NONE, TEEC_NONE);
2960 op.params[0].tmpref.buffer = nonce;
2961 op.params[0].tmpref.size = sizeof(nonce);
2962 op.params[1].tmpref.buffer = measurement;
2963 op.params[1].tmpref.size = sizeof(measurement);
2964
2965 /* Hash TA */
2966 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2967 &ret_orig);
2968 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2969
2970 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2971 memcpy(hash1, measurement, 32);
2972
2973 /* Hash TA again */
2974 memset(measurement, 0, sizeof(measurement));
2975 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2976 &ret_orig);
2977 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2978
2979 /* New hash should be identical to hash1 */
2980 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2981
2982 Do_ADBG_EndSubCase(c, "Consecutive calls");
2983
2984 /* Close TA session, will cause unload of TA */
2985 TEEC_CloseSession(&session);
2986
2987 Do_ADBG_BeginSubCase(c, "TA reload");
2988
2989 /* Load TA again and open a new session */
2990 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
2991 &ret_orig);
2992 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2993 if (res)
2994 return;
2995
2996 /* Hash TA one more time */
2997 memset(measurement, 0, sizeof(measurement));
2998 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2999 &ret_orig);
3000 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3001
3002 /* Hash after reload should still be the same */
3003 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3004
3005 Do_ADBG_EndSubCase(c, "TA reload");
3006
3007 Do_ADBG_BeginSubCase(c, "Add shared library");
3008
3009 /*
3010 * Invoke a TA command that causes some additional code to be mapped
3011 * (shared library)
3012 */
3013 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3014 &ret_orig);
3015 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3016
3017 /* Hash TA one last time */
3018 memset(measurement, 0, sizeof(measurement));
3019 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3020 &ret_orig);
3021 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3022
3023 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3024 memcpy(hash2, measurement, 32);
3025
3026 /* Different binaries mapped mean different hashes */
3027 cmp = memcmp(hash1, hash2, sizeof(hash1));
3028 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3029
3030 Do_ADBG_EndSubCase(c, "Add shared library");
3031
3032 TEEC_CloseSession(&session);
3033}
3034
3035static void xtest_tee_test_1037(ADBG_Case_t *c)
3036{
3037 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3038 TEEC_Result res = TEEC_ERROR_GENERIC;
3039 TEEC_Session session = { };
3040 uint32_t ret_orig = 0;
3041
3042 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3043 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3044 Do_ADBG_Log(" skip test, pseudo TA not found");
3045 return;
3046 }
3047
3048 Do_ADBG_BeginSubCase(c, "Get public key");
3049 get_att_public_key(c);
3050 Do_ADBG_EndSubCase(c, "Get public key");
3051
3052 Do_ADBG_BeginSubCase(c, "TEE attestation");
3053 attestation_tee(c);
3054 Do_ADBG_EndSubCase(c, "TEE attestation");
3055
3056 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3057 attestation_ta_shdr(c);
3058 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3059
3060 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3061 attestation_ta_memory(c);
3062 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3063
3064 free_att_key();
3065}
3066ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3067 "Remote attestation");