blob: 857094974ac2670d56855126f799365a5f3f4159 [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 Forissier03aa2792023-02-07 14:22:24 +010020#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier0915b222021-10-27 18:20:59 +020021#include <sdp_basic.h>
Jerome Forissier03aa2792023-02-07 14:22:24 +010022#endif
Jerome Forissier0915b222021-10-27 18:20:59 +020023#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020024#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010025#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020026#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060027#include <sys/stat.h>
28#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020029#include <ta_arm_bti.h>
30#include <ta_concurrent.h>
31#include <ta_create_fail_test.h>
32#include <ta_crypt.h>
33#include <ta_miss_test.h>
34#include <ta_os_test.h>
35#include <ta_rpc_test.h>
36#include <ta_sims_keepalive_test.h>
37#include <ta_sims_test.h>
38#include <ta_supp_plugin.h>
39#include <ta_tpm_log_test.h>
40#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010041#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020042#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010043#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020044
Jerome Forissier0915b222021-10-27 18:20:59 +020045#include "xtest_helpers.h"
46#include "xtest_test.h"
47#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030048
Jens Wiklanderec545fb2017-11-24 16:58:07 +010049#ifndef MIN
50#define MIN(a, b) ((a) < (b) ? (a) : (b))
51#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020052
Pascal Brandc639ac82015-07-02 08:53:34 +020053struct xtest_crypto_session {
54 ADBG_Case_t *c;
55 TEEC_Session *session;
56 uint32_t cmd_id_sha256;
57 uint32_t cmd_id_aes256ecb_encrypt;
58 uint32_t cmd_id_aes256ecb_decrypt;
59};
60
61static void xtest_crypto_test(struct xtest_crypto_session *cs)
62{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010063 uint32_t ret_orig = 0;
64 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020065 uint8_t crypt_in[16] = { 22, 17 };
66
67 crypt_in[15] = 60;
68
69 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
70 {
71 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
72
73 op.params[0].tmpref.buffer = crypt_in;
74 op.params[0].tmpref.size = sizeof(crypt_in);
75 op.params[1].tmpref.buffer = crypt_out;
76 op.params[1].tmpref.size = sizeof(crypt_out);
77 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
78 TEEC_MEMREF_TEMP_OUTPUT,
79 TEEC_NONE, TEEC_NONE);
80
81 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
82 TEEC_InvokeCommand(cs->session,
83 cs->
84 cmd_id_aes256ecb_encrypt,
85 &op,
86 &ret_orig));
87 }
88 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
89
90 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
91 {
92 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010093 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020094
95 op.params[0].tmpref.buffer = crypt_out;
96 op.params[0].tmpref.size = sizeof(crypt_out);
97 op.params[1].tmpref.buffer = out;
98 op.params[1].tmpref.size = sizeof(out);
99 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
100 TEEC_MEMREF_TEMP_OUTPUT,
101 TEEC_NONE, TEEC_NONE);
102
103 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
104 TEEC_InvokeCommand(cs->session,
105 cs->
106 cmd_id_aes256ecb_decrypt,
107 &op,
108 &ret_orig));
109
110 if (!ADBG_EXPECT(cs->c, 0,
111 memcmp(crypt_in, out, sizeof(crypt_in)))) {
112 Do_ADBG_Log("crypt_in:");
113 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
114 Do_ADBG_Log("out:");
115 Do_ADBG_HexLog(out, sizeof(out), 16);
116 }
117 }
118 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
119
120 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
121 {
122 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
123 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
124 static const uint8_t sha256_out[] = {
125 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
126 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
127 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
128 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
129 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100130 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200131
132 op.params[0].tmpref.buffer = (void *)sha256_in;
133 op.params[0].tmpref.size = sizeof(sha256_in);
134 op.params[1].tmpref.buffer = out;
135 op.params[1].tmpref.size = sizeof(out);
136 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
137 TEEC_MEMREF_TEMP_OUTPUT,
138 TEEC_NONE, TEEC_NONE);
139
140 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
141 TEEC_InvokeCommand(cs->session,
142 cs->
143 cmd_id_sha256,
144 &op,
145 &ret_orig));
146
147 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
148 sizeof(sha256_out)))) {
149 Do_ADBG_Log("sha256_out:");
150 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
151 Do_ADBG_Log("out:");
152 Do_ADBG_HexLog(out, sizeof(out), 16);
153 }
154 }
155 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
156
Etienne Carrierea3198522017-10-26 09:48:55 +0200157 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200158 {
159 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
160 static const uint8_t in[] = {
161 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
162 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
163 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
164 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
165 };
166 static const uint8_t exp_out[] = {
167 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
168 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
169 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
170 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
171 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100172 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200173
174 op.params[0].tmpref.buffer = (void *)in;
175 op.params[0].tmpref.size = sizeof(in);
176 op.params[1].tmpref.buffer = out;
177 op.params[1].tmpref.size = sizeof(out);
178 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
179 TEEC_MEMREF_TEMP_OUTPUT,
180 TEEC_NONE, TEEC_NONE);
181
182 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
183 TEEC_InvokeCommand(cs->session,
184 cs->
185 cmd_id_aes256ecb_encrypt,
186 &op,
187 &ret_orig));
188
189 if (!ADBG_EXPECT(cs->c, 0,
190 memcmp(exp_out, out, sizeof(exp_out)))) {
191 Do_ADBG_Log("exp_out:");
192 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
193 Do_ADBG_Log("out:");
194 Do_ADBG_HexLog(out, sizeof(out), 16);
195 }
196 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200197 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200198
Etienne Carrierea3198522017-10-26 09:48:55 +0200199 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200200 {
201 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
202 static const uint8_t in[] = {
203 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
204 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
205 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
206 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
207 };
208 static const uint8_t exp_out[] = {
209 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
210 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
211 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
212 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
213 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100214 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200215
216 op.params[0].tmpref.buffer = (void *)in;
217 op.params[0].tmpref.size = sizeof(in);
218 op.params[1].tmpref.buffer = out;
219 op.params[1].tmpref.size = sizeof(out);
220 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
221 TEEC_MEMREF_TEMP_OUTPUT,
222 TEEC_NONE, TEEC_NONE);
223
224 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
225 TEEC_InvokeCommand(cs->session,
226 cs->
227 cmd_id_aes256ecb_decrypt,
228 &op,
229 &ret_orig));
230
231 if (!ADBG_EXPECT(cs->c, 0,
232 memcmp(exp_out, out, sizeof(exp_out)))) {
233 Do_ADBG_Log("exp_out:");
234 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
235 Do_ADBG_Log("out:");
236 Do_ADBG_HexLog(out, sizeof(out), 16);
237 }
238 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200239 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200240}
241
242static void xtest_tee_test_1001(ADBG_Case_t *c)
243{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100244 TEEC_Result res = TEEC_ERROR_GENERIC;
245 TEEC_Session session = { };
246 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200247
Etienne Carriere11093162017-10-26 09:49:04 +0200248 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100249 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100250 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200251 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
252 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100253 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200254 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100255 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
256 return;
257
258 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200259
Jens Wiklandercf16e842016-02-10 09:07:09 +0100260 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100261 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Etienne Carriere3ea96722022-03-11 09:16:40 +0100262
263 Do_ADBG_EndSubCase(c, "Core self tests");
264
265 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
266
267 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
268 &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL,
269 &ret_orig));
270
271 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
272
Jens Wiklandercf16e842016-02-10 09:07:09 +0100273 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200274}
Jens Wiklander14f48872018-06-29 15:30:13 +0200275ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200276
Jens Wiklander1d70a112017-10-16 15:16:39 +0200277static void xtest_tee_test_1002(ADBG_Case_t *c)
278{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100279 TEEC_Result res = TEEC_ERROR_GENERIC;
280 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200281 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100282 uint32_t ret_orig = 0;
283 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200284 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100285 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200286
Etienne Carriere11093162017-10-26 09:49:04 +0200287 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200288 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
289 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200290 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
291 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200292 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200293 }
294 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200295
296 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
297 TEEC_NONE, TEEC_NONE);
298 op.params[0].tmpref.size = sizeof(buf);
299 op.params[0].tmpref.buffer = buf;
300
301 for (n = 0; n < sizeof(buf); n++)
302 buf[n] = n + 1;
303 for (n = 0; n < sizeof(buf); n++)
304 exp_sum += buf[n];
305
306 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
307 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
308 goto out;
309
310 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
311out:
312 TEEC_CloseSession(&session);
313}
Jens Wiklander14f48872018-06-29 15:30:13 +0200314ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200315
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100316struct test_1003_arg {
317 uint32_t test_type;
318 size_t repeat;
319 size_t max_before_lockers;
320 size_t max_during_lockers;
321 size_t before_lockers;
322 size_t during_lockers;
323 TEEC_Result res;
324 uint32_t error_orig;
325};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200326
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100327static void *test_1003_thread(void *arg)
328{
329 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100330 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100331 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100332 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100333
334 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
335 NULL, &a->error_orig);
336 if (a->res != TEEC_SUCCESS)
337 return NULL;
338
339 for (n = 0; n < a->repeat; n++) {
340 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
341
342 op.params[0].value.a = a->test_type;
343 op.params[0].value.b = rounds;
344
345 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
346 TEEC_VALUE_OUTPUT,
347 TEEC_NONE, TEEC_NONE);
348 a->res = TEEC_InvokeCommand(&session,
349 PTA_INVOKE_TESTS_CMD_MUTEX,
350 &op, &a->error_orig);
351 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
352 op.params[1].value.b != 1) {
353 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
354 a->res = TEEC_ERROR_BAD_STATE;
355 a->error_orig = 42;
356 break;
357 }
358
359 if (a->test_type == PTA_MUTEX_TEST_READER) {
360 if (op.params[1].value.a > a->max_before_lockers)
361 a->max_before_lockers = op.params[1].value.a;
362
363 if (op.params[1].value.b > a->max_during_lockers)
364 a->max_during_lockers = op.params[1].value.b;
365
366 a->before_lockers += op.params[1].value.a;
367 a->during_lockers += op.params[1].value.b;
368 }
369 }
370 TEEC_CloseSession(&session);
371
372 return NULL;
373}
374
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100375#define TEST_1003_THREAD_COUNT (3 * 2)
376
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100377static void xtest_tee_test_1003(ADBG_Case_t *c)
378{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100379 TEEC_Result res = TEEC_ERROR_GENERIC;
380 TEEC_Session session = { };
381 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100382 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100383 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100384 size_t max_read_concurrency = 0;
385 size_t max_read_waiters = 0;
386 size_t num_concurrent_read_lockers = 0;
387 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100388 size_t n = 0;
389 size_t nt = TEST_1003_THREAD_COUNT;
390 double mean_read_concurrency = 0;
391 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100392 size_t num_writers = 0;
393 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100394 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100395
396 /* Pseudo TA is optional: warn and nicely exit if not found */
397 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
398 &ret_orig);
399 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
400 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
401 return;
402 }
403 ADBG_EXPECT_TEEC_SUCCESS(c, res);
404 TEEC_CloseSession(&session);
405
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100406 for (n = 0; n < nt; n++) {
407 if (n % 3) {
408 arg[n].test_type = PTA_MUTEX_TEST_READER;
409 num_readers++;
410 } else {
411 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
412 num_writers++;
413 }
414 arg[n].repeat = repeat;
415 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
416 test_1003_thread, arg + n)))
417 nt = n; /* break loop and start cleanup */
418 }
419
420 for (n = 0; n < nt; n++) {
421 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
422 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
423 Do_ADBG_Log("error origin %" PRIu32,
424 arg[n].error_orig);
425 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
426 if (arg[n].max_during_lockers > max_read_concurrency)
427 max_read_concurrency =
428 arg[n].max_during_lockers;
429
430 if (arg[n].max_before_lockers > max_read_waiters)
431 max_read_waiters = arg[n].max_before_lockers;
432
433 num_concurrent_read_lockers += arg[n].during_lockers;
434 num_concurrent_read_waiters += arg[n].before_lockers;
435 }
436 }
437
438 mean_read_concurrency = (double)num_concurrent_read_lockers /
439 (double)(repeat * num_readers);
440 mean_read_waiters = (double)num_concurrent_read_waiters /
441 (double)(repeat * num_readers);
442
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100443 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
444 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100445 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
446 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
447 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
448 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
449}
Jens Wiklander14f48872018-06-29 15:30:13 +0200450ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
451 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200452
Pascal Brandc639ac82015-07-02 08:53:34 +0200453static void xtest_tee_test_1004(ADBG_Case_t *c)
454{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100455 TEEC_Session session = { };
456 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200457 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
458 TA_CRYPT_CMD_AES256ECB_ENC,
459 TA_CRYPT_CMD_AES256ECB_DEC };
460
461 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
462 &session, &crypt_user_ta_uuid,
463 NULL, &ret_orig)))
464 return;
465
466 /* Run the "complete crypto test suite" */
467 xtest_crypto_test(&cs);
468
469 TEEC_CloseSession(&session);
470}
Jens Wiklander14f48872018-06-29 15:30:13 +0200471ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200472
Etienne Carriere92c34422018-02-09 13:11:40 +0100473static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200474{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100475 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200476 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100477 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200478
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300479 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200480 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300481 &ret_orig)))
482 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200483
484 op.params[0].value.a = n;
485 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
486 TEEC_NONE);
487
488 (void)ADBG_EXPECT_TEEC_RESULT(c,
489 TEEC_ERROR_TARGET_DEAD,
490 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
491 &ret_orig));
492
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300493 (void)ADBG_EXPECT_TEEC_RESULT(c,
494 TEEC_ERROR_TARGET_DEAD,
495 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200496 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300497
Pascal Brandc639ac82015-07-02 08:53:34 +0200498 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
499
500 TEEC_CloseSession(&session);
501}
502
Etienne Carriere92c34422018-02-09 13:11:40 +0100503static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
504 size_t size)
505{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100506 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100507 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100508 uint32_t ret_orig = 0;
509 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100510
Etienne Carriere92c34422018-02-09 13:11:40 +0100511 shm.size = size;
512 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
513 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
514 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
515 return;
516
517 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
518 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
519 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200520 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100521
522 op.params[0].value.a = (uint32_t)n;
523 op.params[1].memref.parent = &shm;
524 op.params[1].memref.size = size;
525 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
526 TEEC_NONE, TEEC_NONE);
527
528 (void)ADBG_EXPECT_TEEC_RESULT(c,
529 TEEC_ERROR_TARGET_DEAD,
530 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
531 &ret_orig));
532
533 (void)ADBG_EXPECT_TEEC_RESULT(c,
534 TEEC_ERROR_TARGET_DEAD,
535 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
536 &ret_orig));
537
538 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
539
540 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200541rel_shm:
542 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100543}
544
Pascal Brandc639ac82015-07-02 08:53:34 +0200545static void xtest_tee_test_1005(ADBG_Case_t *c)
546{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100547 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200548#define MAX_SESSIONS 3
549 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100550 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200551
552 for (i = 0; i < MAX_SESSIONS; i++) {
553 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200554 xtest_teec_open_session(&sessions[i],
555 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200556 NULL, &ret_orig)))
557 break;
558 }
559
560 for (; --i >= 0; )
561 TEEC_CloseSession(&sessions[i]);
562}
Jens Wiklander14f48872018-06-29 15:30:13 +0200563ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200564
565static void xtest_tee_test_1006(ADBG_Case_t *c)
566{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100567 TEEC_Session session = { };
568 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200569 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100570 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200571
572 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
573 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
574 &ret_orig)))
575 return;
576
577 op.params[0].tmpref.buffer = buf;
578 op.params[0].tmpref.size = sizeof(buf);
579 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
580 TEEC_NONE, TEEC_NONE);
581
582 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
583 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
584 &ret_orig));
585
586 TEEC_CloseSession(&session);
587}
Jens Wiklander14f48872018-06-29 15:30:13 +0200588ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
589 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200590
591static void xtest_tee_test_1007(ADBG_Case_t *c)
592{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100593 TEEC_Session session = { };
594 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200595
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300596 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200597 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300598 &ret_orig)))
599 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200600
601 (void)ADBG_EXPECT_TEEC_RESULT(c,
602 TEEC_ERROR_TARGET_DEAD,
603 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
604 &ret_orig));
605
606 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
607
608 (void)ADBG_EXPECT_TEEC_RESULT(c,
609 TEEC_ERROR_TARGET_DEAD,
610 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
611 &ret_orig));
612
613 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
614
615 TEEC_CloseSession(&session);
616}
Jens Wiklander14f48872018-06-29 15:30:13 +0200617ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200618
Jerome Forissierf02a2212015-10-29 14:33:35 +0100619#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000620# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800621#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000622# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100623#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000624# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100625#endif
626
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100627static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600628{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100629 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600630
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100631 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100632 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100633 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200634 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
635 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
636 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600637 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200638
Jens Wiklanderb7940892015-10-23 16:02:40 +0200639 return fopen(buf, mode);
640}
641
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100642static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200643{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100644 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100645 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
646 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100647 TEEC_Result res = TEEC_ERROR_GENERIC;
648 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100649 FILE *f = NULL;
650 bool r = false;
651 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100652 size_t sz = 0;
653 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200654
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100655 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
656 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
657 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200658
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100659 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
660 if (!ADBG_EXPECT_NOT_NULL(c, f))
661 goto out;
662 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
663 goto out;
664 sz = ftell(f);
665 rewind(f);
666
667 buf = malloc(sz);
668 if (!ADBG_EXPECT_NOT_NULL(c, buf))
669 goto out;
670
671 fread_res = fread(buf, 1, sz, f);
672 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
673 goto out;
674
Jens Wiklander4441fe22015-10-23 16:53:02 +0200675 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100676 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200677
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100678 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200679
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100680 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
681 TEEC_NONE, TEEC_NONE);
682 op.params[0].tmpref.buffer = buf;
683 op.params[0].tmpref.size = sz;
684
685 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
686 &ret_orig);
687 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
688out:
689 free(buf);
690 if (f)
691 fclose(f);
692 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200693 return r;
694}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100695
696static void test_1008_corrupt_ta(ADBG_Case_t *c)
697{
698 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
699 TEEC_Result res = TEEC_ERROR_GENERIC;
700 TEEC_Session session = { };
701 uint32_t ret_orig = 0;
702
703 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
704 if (res) {
705 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
706 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200707 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100708 return;
709 }
710 TEEC_CloseSession(&session);
711
712 ADBG_EXPECT_TRUE(c,
713 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
714 ADBG_EXPECT_TRUE(c,
715 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
716 ADBG_EXPECT_TRUE(c,
717 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
718 ADBG_EXPECT_TRUE(c,
719 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
720 ADBG_EXPECT_TRUE(c,
721 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
722 ADBG_EXPECT_TRUE(c,
723 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
724 ADBG_EXPECT_TRUE(c,
725 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
726 ADBG_EXPECT_TRUE(c,
727 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
728 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
729 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
730}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200731
Pascal Brandc639ac82015-07-02 08:53:34 +0200732static void xtest_tee_test_1008(ADBG_Case_t *c)
733{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100734 TEEC_Session session = { };
735 TEEC_Session session_crypt = { };
736 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200737
738 Do_ADBG_BeginSubCase(c, "Invoke command");
739 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300740 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200741 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300742 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200743
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300744 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
745 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
746 NULL, &ret_orig));
747 TEEC_CloseSession(&session);
748 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200749
Pascal Brandc639ac82015-07-02 08:53:34 +0200750 }
751 Do_ADBG_EndSubCase(c, "Invoke command");
752
753 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
754 {
755 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
756
757 op.params[0].value.a = 2000;
758 op.paramTypes = TEEC_PARAM_TYPES(
759 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
760
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300761 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200762 xtest_teec_open_session(&session,
763 &os_test_ta_uuid,
764 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300765 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200766
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300767 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
768 TEEC_InvokeCommand(&session,
769 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
770 &op, &ret_orig));
771 TEEC_CloseSession(&session);
772 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200773 }
774 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
775
776 Do_ADBG_BeginSubCase(c, "Create session fail");
777 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100778 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200779
Pascal Brandc639ac82015-07-02 08:53:34 +0200780 for (n = 0; n < 100; n++) {
781 Do_ADBG_Log("n = %zu", n);
782 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
783 xtest_teec_open_session(&session_crypt,
784 &create_fail_test_ta_uuid,
785 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100786 /* level > 0 may be used to detect/debug memory leaks */
787 if (!level)
788 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200789 }
790 }
791 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200792
Jens Wiklander4441fe22015-10-23 16:53:02 +0200793 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100794 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200795 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200796}
Jens Wiklander14f48872018-06-29 15:30:13 +0200797ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
798 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200799
Pascal Brandc639ac82015-07-02 08:53:34 +0200800static void *cancellation_thread(void *arg)
801{
802 /*
803 * Sleep 0.5 seconds before cancellation to make sure that the other
804 * thread is in RPC_WAIT.
805 */
806 (void)usleep(500000);
807 TEEC_RequestCancellation(arg);
808 return NULL;
809}
Pascal Brandc639ac82015-07-02 08:53:34 +0200810
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300811static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
812 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200813{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100814 TEEC_Session session = { };
815 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300816 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200817
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100818 memset(&thr, 0, sizeof(thr));
819
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200821 {
822 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
823
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300824 if (ADBG_EXPECT_TEEC_SUCCESS(c,
825 xtest_teec_open_session(&session, &os_test_ta_uuid,
826 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200827
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300828 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
829 TEEC_ORIGIN_TRUSTED_APP,
830 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200831
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832 op.params[0].value.a = timeout;
833 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
834 TEEC_NONE,
835 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300836 if (cancel) {
837 (void)ADBG_EXPECT(c, 0,
838 pthread_create(&thr, NULL,
839 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200840
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300841 (void)ADBG_EXPECT_TEEC_RESULT(c,
842 TEEC_ERROR_CANCEL,
843 TEEC_InvokeCommand(&session,
844 TA_OS_TEST_CMD_WAIT,
845 &op,
846 &ret_orig));
847 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300848
849 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
850 TEEC_InvokeCommand(&session,
851 TA_OS_TEST_CMD_WAIT,
852 &op,
853 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300854 if (cancel)
855 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300856
857 TEEC_CloseSession(&session);
858 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200859 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300860 Do_ADBG_EndSubCase(c, "%s", subcase);
861}
862
863static void xtest_tee_test_1009(ADBG_Case_t *c)
864{
865 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
866 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300867 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300868 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200869}
Jens Wiklander14f48872018-06-29 15:30:13 +0200870ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200871
872static void xtest_tee_test_1010(ADBG_Case_t *c)
873{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100874 unsigned int n = 0;
875 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100876 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200877
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200878 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200879 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
880 xtest_tee_test_invalid_mem_access(c, n);
881 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
882 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100883
884 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
885 for (n = 1; n <= 5; n++) {
886 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200887 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100888 n, memref_sz[idx]);
889 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
890 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200891 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100892 n, memref_sz[idx]);
893 }
894 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200895}
Jens Wiklander14f48872018-06-29 15:30:13 +0200896ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
897 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200898
899static void xtest_tee_test_1011(ADBG_Case_t *c)
900{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100901 TEEC_Session session = { };
902 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200903 struct xtest_crypto_session cs = {
904 c, &session, TA_RPC_CMD_CRYPT_SHA256,
905 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
906 TA_RPC_CMD_CRYPT_AES256ECB_DEC
907 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100908 struct xtest_crypto_session cs_privmem = {
909 c, &session,
910 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
911 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
912 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
913 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200914 TEEC_UUID uuid = rpc_test_ta_uuid;
915
916 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
917 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
918 return;
919
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100920 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200921 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100922 * Run the "complete crypto test suite" using TA-to-TA
923 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200924 */
925 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100926 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
927
928 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
929 /*
930 * Run the "complete crypto test suite" using TA-to-TA
931 * communication via TA private memory.
932 */
933 xtest_crypto_test(&cs_privmem);
934 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
935
Pascal Brandc639ac82015-07-02 08:53:34 +0200936 TEEC_CloseSession(&session);
937}
Jens Wiklander14f48872018-06-29 15:30:13 +0200938ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
939 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200940
941/*
942 * Note that this test is failing when
943 * - running twice in a raw
944 * - and the user TA is statically linked
945 * This is because the counter is not reseted when opening the first session
946 * in case the TA is statically linked
947 */
948static void xtest_tee_test_1012(ADBG_Case_t *c)
949{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100950 TEEC_Session session1 = { };
951 TEEC_Session session2 = { };
952 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200953 TEEC_UUID uuid = sims_test_ta_uuid;
954
955 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
956 {
957 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
958 static const uint8_t in[] = {
959 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
960 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
961 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
962 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
963 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100964 uint8_t out[32] = { };
965 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200966
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300967 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200968 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300969 &ret_orig)))
970 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200971
972 op.params[0].value.a = 0;
973 op.params[1].tmpref.buffer = (void *)in;
974 op.params[1].tmpref.size = sizeof(in);
975 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
976 TEEC_MEMREF_TEMP_INPUT,
977 TEEC_NONE, TEEC_NONE);
978
979 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
980 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
981 &ret_orig));
982
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100983 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300984 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200985 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300986 &ret_orig)))
987 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200988
989 op.params[0].value.a = 0;
990 op.params[1].tmpref.buffer = out;
991 op.params[1].tmpref.size = sizeof(out);
992 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
993 TEEC_MEMREF_TEMP_OUTPUT,
994 TEEC_NONE, TEEC_NONE);
995
996 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
997 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
998 &op, &ret_orig));
999
1000 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1001 sizeof(out))) {
1002 Do_ADBG_Log("in:");
1003 Do_ADBG_HexLog(in, sizeof(in), 16);
1004 Do_ADBG_Log("out:");
1005 Do_ADBG_HexLog(out, sizeof(out), 16);
1006 }
1007
1008 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1009 TEEC_NONE, TEEC_NONE,
1010 TEEC_NONE);
1011
1012 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1013 TEEC_InvokeCommand(&session1,
1014 TA_SIMS_CMD_GET_COUNTER,
1015 &op, &ret_orig));
1016
1017 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1018
1019 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1020 TEEC_InvokeCommand(&session2,
1021 TA_SIMS_CMD_GET_COUNTER, &op,
1022 &ret_orig));
1023
1024 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1025 TEEC_CloseSession(&session2);
1026 }
1027
1028 memset(out, 0, sizeof(out));
1029 op.params[0].value.a = 0;
1030 op.params[1].tmpref.buffer = out;
1031 op.params[1].tmpref.size = sizeof(out);
1032 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1033 TEEC_MEMREF_TEMP_OUTPUT,
1034 TEEC_NONE, TEEC_NONE);
1035
1036 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1037 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1038 &ret_orig));
1039
1040 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1041 Do_ADBG_Log("in:");
1042 Do_ADBG_HexLog(in, sizeof(in), 16);
1043 Do_ADBG_Log("out:");
1044 Do_ADBG_HexLog(out, sizeof(out), 16);
1045 }
1046
1047 TEEC_CloseSession(&session1);
1048 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001049 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001050}
Jens Wiklander14f48872018-06-29 15:30:13 +02001051ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1052 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001053
1054struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001055 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001056 uint32_t cmd;
1057 uint32_t repeat;
1058 TEEC_SharedMemory *shm;
1059 uint32_t error_orig;
1060 TEEC_Result res;
1061 uint32_t max_concurrency;
1062 const uint8_t *in;
1063 size_t in_len;
1064 uint8_t *out;
1065 size_t out_len;
1066};
1067
1068static void *test_1013_thread(void *arg)
1069{
1070 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001071 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001072 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1073 uint8_t p2 = TEEC_NONE;
1074 uint8_t p3 = TEEC_NONE;
1075
Jens Wiklander70672972016-04-06 00:01:45 +02001076 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001077 &a->error_orig);
1078 if (a->res != TEEC_SUCCESS)
1079 return NULL;
1080
1081 op.params[0].memref.parent = a->shm;
1082 op.params[0].memref.size = a->shm->size;
1083 op.params[0].memref.offset = 0;
1084 op.params[1].value.a = a->repeat;
1085 op.params[1].value.b = 0;
1086 op.params[2].tmpref.buffer = (void *)a->in;
1087 op.params[2].tmpref.size = a->in_len;
1088 op.params[3].tmpref.buffer = a->out;
1089 op.params[3].tmpref.size = a->out_len;
1090
1091 if (a->in_len)
1092 p2 = TEEC_MEMREF_TEMP_INPUT;
1093 if (a->out_len)
1094 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1095
1096 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1097 TEEC_VALUE_INOUT, p2, p3);
1098
1099 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1100 a->max_concurrency = op.params[1].value.b;
1101 a->out_len = op.params[3].tmpref.size;
1102 TEEC_CloseSession(&session);
1103 return NULL;
1104}
1105
Pascal Brand4fa35582015-12-17 10:59:12 +01001106#define NUM_THREADS 3
1107
Jens Wiklander70672972016-04-06 00:01:45 +02001108static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1109 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001110{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001111 size_t nt = 0;
1112 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001113 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001114 TEEC_SharedMemory shm = { };
1115 size_t max_concurrency = 0;
1116 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001117 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1118 static const uint8_t sha256_out[] = {
1119 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1120 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1121 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1122 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1123 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001124 uint8_t out[32] = { };
1125 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001126 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001127
Jens Wiklander70672972016-04-06 00:01:45 +02001128 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001129 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001130
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001131 shm.size = sizeof(struct ta_concurrent_shm);
1132 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1133 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1134 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1135 return;
1136
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001137 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001138 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001139 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001140
1141 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001142 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001143 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001144 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001145 arg[n].shm = &shm;
1146 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1147 test_1013_thread, arg + n)))
1148 nt = n; /* break loop and start cleanup */
1149 }
1150
1151 for (n = 0; n < nt; n++) {
1152 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001153 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1154 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1155 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1156 skip = true;
1157 continue;
1158 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001159 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1160 if (arg[n].max_concurrency > max_concurrency)
1161 max_concurrency = arg[n].max_concurrency;
1162 }
1163
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001164 /*
1165 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001166 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001167 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1168 * best result there).
1169 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001170 if (!skip) {
1171 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1172 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1173 NUM_THREADS);
1174 *mean_concurrency += max_concurrency;
1175 }
Jens Wiklander70672972016-04-06 00:01:45 +02001176 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001177
Jens Wiklander70672972016-04-06 00:01:45 +02001178 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001179 memset(shm.buffer, 0, shm.size);
1180 memset(arg, 0, sizeof(arg));
1181 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001182 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001183
1184 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001185 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001186 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001187 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001188 arg[n].shm = &shm;
1189 arg[n].in = sha256_in;
1190 arg[n].in_len = sizeof(sha256_in);
1191 arg[n].out = out;
1192 arg[n].out_len = sizeof(out);
1193 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1194 test_1013_thread, arg + n)))
1195 nt = n; /* break loop and start cleanup */
1196 }
1197
1198 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001199 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1200 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1201 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1202 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1203 continue;
1204 }
1205 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001206 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1207 arg[n].out, arg[n].out_len);
1208 if (arg[n].max_concurrency > max_concurrency)
1209 max_concurrency = arg[n].max_concurrency;
1210 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001211 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001212 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001213
Pascal Brand4fa35582015-12-17 10:59:12 +01001214 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001215 TEEC_ReleaseSharedMemory(&shm);
1216}
Pascal Brand4fa35582015-12-17 10:59:12 +01001217
1218static void xtest_tee_test_1013(ADBG_Case_t *c)
1219{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001220 int i = 0;
1221 double mean_concurrency = 0;
1222 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001223 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001224
1225 if (level == 0)
1226 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001227
Jens Wiklander70672972016-04-06 00:01:45 +02001228 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001229 mean_concurrency = 0;
1230 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001231 xtest_tee_test_1013_single(c, &concurrency,
1232 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001233 mean_concurrency += concurrency;
1234 }
1235 mean_concurrency /= nb_loops;
1236
1237 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1238 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001239 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001240
Jens Wiklander70672972016-04-06 00:01:45 +02001241 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1242 mean_concurrency = 0;
1243 for (i = 0; i < nb_loops; i++) {
1244 xtest_tee_test_1013_single(c, &concurrency,
1245 &concurrent_large_ta_uuid);
1246 mean_concurrency += concurrency;
1247 }
1248 mean_concurrency /= nb_loops;
1249
1250 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1251 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1252 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1253}
Jens Wiklander14f48872018-06-29 15:30:13 +02001254ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001255 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001256
1257#ifdef CFG_SECURE_DATA_PATH
1258static void xtest_tee_test_1014(ADBG_Case_t *c)
1259{
1260 UNUSED(c);
1261
1262 int size = 17000;
1263 int loop = 10;
Olivier Masseda5282a2022-04-07 11:24:08 +02001264 const char *heap_name = DEFAULT_HEAP_NAME;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001265 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001266 int test = 0;
1267 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001268
1269 test = TEST_NS_TO_TA;
1270 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001271 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001272 ADBG_EXPECT(c, 0, ret);
1273 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1274
1275 test = TEST_TA_TO_TA;
1276 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001277 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001278 ADBG_EXPECT(c, 0, ret);
1279 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1280
1281 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001282 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001283 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001284 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001285 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001286
1287 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001288 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001289 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001290 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001291 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001292
1293 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001294 ret = sdp_out_of_bounds_memref_test(size, heap_name, 0);
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001295 ADBG_EXPECT(c, 0, ret);
1296 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001297}
Jens Wiklander14f48872018-06-29 15:30:13 +02001298ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1299 "Test secure data path against SDP TAs and pTAs");
1300#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001301
Jerome Forissierf4259942022-01-11 14:27:05 +01001302#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001303static void xtest_tee_test_1015(ADBG_Case_t *c)
1304{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001305 TEEC_Result res = TEEC_ERROR_GENERIC;
1306 TEEC_Session session = { };
1307 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001308
Etienne Carriere11093162017-10-26 09:49:04 +02001309 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001310 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1311 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001312 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1313 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001314 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001315 }
1316 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001317
1318 ADBG_EXPECT_TEEC_SUCCESS(c,
1319 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1320 NULL, &ret_orig));
1321 TEEC_CloseSession(&session);
1322}
Jens Wiklander14f48872018-06-29 15:30:13 +02001323ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1324 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001325#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001326
1327static void xtest_tee_test_1016(ADBG_Case_t *c)
1328{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001329 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001330 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001331 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001332
1333 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1334 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1335 &ret_orig)))
1336 return;
1337
1338 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1339 TEEC_NONE);
1340
1341 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1342 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1343 &ret_orig));
1344
1345 TEEC_CloseSession(&session);
1346}
Jens Wiklander14f48872018-06-29 15:30:13 +02001347ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1348 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001349
1350static void xtest_tee_test_1017(ADBG_Case_t *c)
1351{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001352 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001353 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001354 uint32_t ret_orig = 0;
1355 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001356 size_t page_size = 4096;
1357
Jens Wiklander87e81702018-03-20 12:00:00 +08001358 shm.size = 8 * page_size;
1359 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1360 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1361 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1362 return;
1363
1364 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1365 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1366 &ret_orig)))
1367 goto out;
1368
1369 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1370 TEEC_MEMREF_PARTIAL_INPUT,
1371 TEEC_MEMREF_PARTIAL_OUTPUT,
1372 TEEC_MEMREF_PARTIAL_OUTPUT);
1373
1374 /*
1375 * The first two memrefs are supposed to be combined into in
1376 * region and the last two memrefs should have one region each
1377 * when the parameters are mapped for the TA.
1378 */
1379 op.params[0].memref.parent = &shm;
1380 op.params[0].memref.size = page_size;
1381 op.params[0].memref.offset = 0;
1382
1383 op.params[1].memref.parent = &shm;
1384 op.params[1].memref.size = page_size;
1385 op.params[1].memref.offset = page_size;
1386
1387 op.params[2].memref.parent = &shm;
1388 op.params[2].memref.size = page_size;
1389 op.params[2].memref.offset = 4 * page_size;
1390
1391 op.params[3].memref.parent = &shm;
1392 op.params[3].memref.size = 2 * page_size;
1393 op.params[3].memref.offset = 6 * page_size;
1394
1395 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1396 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1397 &ret_orig));
1398
1399 TEEC_CloseSession(&session);
1400out:
1401 TEEC_ReleaseSharedMemory(&shm);
1402}
Jens Wiklander14f48872018-06-29 15:30:13 +02001403ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1404 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001405
Etienne Carriere84382b32018-04-25 18:30:30 +02001406static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1407 TEEC_SharedMemory *shm)
1408{
1409 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1410 TEEC_Result ret = TEEC_ERROR_GENERIC;
1411 uint32_t ret_orig = 0;
1412
1413 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1414 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1415
1416 op.params[0].memref.parent = shm;
1417 op.params[0].memref.size = shm->size / 2;
1418 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1419
1420 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1421 &op, &ret_orig);
1422
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001423 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001424 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1425 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1426 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1427 }
1428}
1429
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001430static void xtest_tee_test_1018(ADBG_Case_t *c)
1431{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001432 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001433 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001434 uint32_t ret_orig = 0;
1435 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001436 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001437 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001438 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001439 uint8_t buffer[6001] = { };
1440
1441 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1442 xtest_teec_open_session(&session,
1443 &os_test_ta_uuid,
1444 NULL,
1445 &ret_orig)))
1446 return;
1447
Joakim Becha1212b62020-04-07 12:06:00 +02001448 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001449
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001450 shm.size = 8 * page_size;
1451 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1452 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001453 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1454 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001455 goto out;
1456
1457 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1458 TEEC_MEMREF_PARTIAL_INPUT,
1459 TEEC_MEMREF_PARTIAL_OUTPUT,
1460 TEEC_MEMREF_PARTIAL_OUTPUT);
1461
1462 /*
1463 * The first two memrefs are supposed to be combined into in
1464 * region and the last two memrefs should have one region each
1465 * when the parameters are mapped for the TA.
1466 */
1467 op.params[0].memref.parent = &shm;
1468 op.params[0].memref.size = page_size;
1469 op.params[0].memref.offset = 0;
1470
1471 op.params[1].memref.parent = &shm;
1472 op.params[1].memref.size = page_size;
1473 op.params[1].memref.offset = page_size;
1474
1475 op.params[2].memref.parent = &shm;
1476 op.params[2].memref.size = page_size;
1477 op.params[2].memref.offset = 4 * page_size;
1478
1479 op.params[3].memref.parent = &shm;
1480 op.params[3].memref.size = 3 * page_size;
1481 op.params[3].memref.offset = 6 * page_size;
1482
Etienne Carriere84382b32018-04-25 18:30:30 +02001483 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1484 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001485
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001486 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001487 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1488 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1489 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1490 }
1491
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001492 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001493 Do_ADBG_EndSubCase(c, NULL);
1494
1495 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1496
1497 memset(&shm, 0, sizeof(shm));
1498 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1499 shm.buffer = buffer;
1500 shm.size = sizeof(buffer);
1501
1502 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1503 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1504 &shm)))
1505 goto out;
1506
1507 invoke_1byte_out_of_bounds(c, &session, &shm);
1508
1509 TEEC_ReleaseSharedMemory(&shm);
1510 Do_ADBG_EndSubCase(c, NULL);
1511
1512 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1513
1514 memset(&shm, 0, sizeof(shm));
1515 shm.size = sizeof(buffer);
1516 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1517 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1518 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1519 &shm)))
1520 goto out;
1521
1522 invoke_1byte_out_of_bounds(c, &session, &shm);
1523
1524 TEEC_ReleaseSharedMemory(&shm);
1525 Do_ADBG_EndSubCase(c, NULL);
1526
1527out:
1528 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001529}
Jens Wiklander14f48872018-06-29 15:30:13 +02001530ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1531 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001532
Jerome Forissier53bde722018-05-31 09:14:54 +02001533static void xtest_tee_test_1019(ADBG_Case_t *c)
1534{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001535 TEEC_Session session = { };
1536 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001537
1538 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1539 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1540 &ret_orig)))
1541 return;
1542
1543 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1544 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1545 &ret_orig));
1546
1547 (void)ADBG_EXPECT_TEEC_RESULT(c,
1548 TEEC_ERROR_TARGET_DEAD,
1549 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1550 NULL, &ret_orig));
1551
1552 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1553
1554 TEEC_CloseSession(&session);
1555}
Jens Wiklander14f48872018-06-29 15:30:13 +02001556ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1557 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001558
1559static void xtest_tee_test_1020(ADBG_Case_t *c)
1560{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001561 TEEC_Result res = TEEC_ERROR_GENERIC;
1562 TEEC_Session session = { };
1563 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001564
1565 /* Pseudo TA is optional: warn and nicely exit if not found */
1566 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1567 &ret_orig);
1568 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1569 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1570 return;
1571 }
1572 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1573
1574 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1575 NULL, &ret_orig);
1576 if (res != TEEC_SUCCESS) {
1577 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1578 ret_orig);
1579 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1580 Do_ADBG_Log(" - 1020 - skip test, feature not "
1581 "implemented");
1582 goto out;
1583 }
1584 /* Error */
1585 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1586 }
1587out:
1588 TEEC_CloseSession(&session);
1589}
1590ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1591 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001592
1593static TEEC_Result open_sec_session(TEEC_Session *session,
1594 const TEEC_UUID *uuid)
1595{
1596 TEEC_Result res = TEEC_ERROR_GENERIC;
1597 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1598 uint32_t ret_orig = 0;
1599
1600 op.params[0].tmpref.buffer = (void *)uuid;
1601 op.params[0].tmpref.size = sizeof(*uuid);
1602 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1603 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1604
1605 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1606 &op, &ret_orig);
1607 if (res != TEEC_SUCCESS)
1608 return TEEC_ERROR_GENERIC;
1609
1610 return res;
1611}
1612
1613static TEEC_Result sims_get_counter(TEEC_Session *session,
1614 uint32_t *counter)
1615{
1616 TEEC_Result res = TEEC_ERROR_GENERIC;
1617 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1618 uint32_t ret_orig = 0;
1619
1620 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1621 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1622
1623 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1624 &op, &ret_orig);
1625 if (res == TEEC_SUCCESS)
1626 *counter = op.params[0].value.a;
1627
1628 return res;
1629}
1630
1631static TEEC_Result trigger_panic(TEEC_Session *session,
1632 const TEEC_UUID *uuid)
1633{
1634 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1635 uint32_t ret_orig = 0;
1636
1637 if (!uuid) {
1638 op.params[0].tmpref.buffer = NULL;
1639 op.params[0].tmpref.size = 0;
1640 } else {
1641 op.params[0].tmpref.buffer = (void *)uuid;
1642 op.params[0].tmpref.size = sizeof(*uuid);
1643 }
1644 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1645 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1646
1647 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1648 &op, &ret_orig);
1649}
1650
1651static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1652 bool multi_instance)
1653{
1654 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1655 uint32_t counter = 0;
1656 uint32_t ret_orig = 0;
1657 uint32_t exp_counter = 0;
1658 TEEC_Session cs[3] = { };
1659
1660 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1661 xtest_teec_open_session(&cs[0], uuid, NULL,
1662 &ret_orig)))
1663 return;
1664
1665 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1666 xtest_teec_open_session(&cs[1], uuid, NULL,
1667 &ret_orig)))
1668 goto bail0;
1669
1670 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1671 goto bail1;
1672
1673 if (!ADBG_EXPECT(c, 0, counter))
1674 goto bail1;
1675
1676 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1677 goto bail1;
1678
1679 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001680 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001681 goto bail1;
1682
1683 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1684 trigger_panic(&cs[1], NULL)))
1685 goto bail1;
1686
1687 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1688 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1689 sims_get_counter(&cs[0], &counter)))
1690 goto bail1;
1691
1692 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1693 sims_get_counter(&cs[1], &counter)))
1694 goto bail1;
1695
1696 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001697 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001698 xtest_teec_open_session(&cs[1], uuid, NULL,
1699 &ret_orig)))
1700 goto bail1;
1701
1702 /* Sanity check of still valid TA context */
1703 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1704 xtest_teec_open_session(&cs[2], uuid, NULL,
1705 &ret_orig)))
1706 goto bail1;
1707
1708 /* Sanity check of still valid TA context */
1709 if (multi_instance) {
1710 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1711 sims_get_counter(&cs[0], &counter)))
1712 goto bail2;
1713
1714 if (!ADBG_EXPECT(c, 0, counter))
1715 goto bail2;
1716 }
1717
1718 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1719 goto bail2;
1720
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001721 exp_counter = multi_instance ? 0 : 1;
1722 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001723 goto bail2;
1724
1725bail2:
1726 TEEC_CloseSession(&cs[2]);
1727bail1:
1728 TEEC_CloseSession(&cs[1]);
1729bail0:
1730 TEEC_CloseSession(&cs[0]);
1731}
1732
1733static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1734 const TEEC_UUID *uuid2)
1735{
1736 uint32_t ret_orig = 0;
1737 uint32_t counter = 0;
1738 TEEC_Session cs[3] = { };
1739
1740 /* Test pre-conditions */
1741 /* 2.1 - CA opens a session toward TA1 */
1742 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1743 xtest_teec_open_session(&cs[0], uuid1, NULL,
1744 &ret_orig)))
1745 return;
1746
1747 /* 2.2 - CA opens a session toward TA2 */
1748 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1749 xtest_teec_open_session(&cs[1], uuid2, NULL,
1750 &ret_orig)))
1751 goto bail0;
1752
1753 /* 2.3 - TA1 opens a session toward TA2 */
1754 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1755 goto bail1;
1756
1757 /* 2.4 - CA invokes TA2 which panics */
1758 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1759 trigger_panic(&cs[1], NULL)))
1760 goto bail1;
1761
1762 /* Expected results */
1763 /* 2.5 - Expect CA->TA1 session is still alive */
1764 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1765 goto bail1;
1766
1767 /* 2.6 - Expect CA->TA2 session is properly released */
1768 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1769 sims_get_counter(&cs[1], &counter)))
1770 goto bail1;
1771
1772bail1:
1773 TEEC_CloseSession(&cs[1]);
1774bail0:
1775 TEEC_CloseSession(&cs[0]);
1776
1777 memset(cs, 0, sizeof(cs));
1778
1779 /* Test pre-conditions */
1780 /* 2.1 - CA opens a session toward TA1 */
1781 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1782 xtest_teec_open_session(&cs[0], uuid1, NULL,
1783 &ret_orig)))
1784 return;
1785
1786 /* 2.2 - CA opens a session toward TA2 */
1787 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1788 xtest_teec_open_session(&cs[1], uuid2, NULL,
1789 &ret_orig)))
1790 goto bail2;
1791
1792 /* 2.3 - TA1 opens a session toward TA2 */
1793 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1794 goto bail3;
1795
1796 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1797 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1798 goto bail3;
1799
1800 /* Expected results */
1801 /* 2.5 - Expect CA->TA1 session is still alive */
1802 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1803 goto bail3;
1804
1805 /* 2.6 - Expect CA->TA2 session is properly released */
1806 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1807 sims_get_counter(&cs[1], &counter)))
1808 goto bail3;
1809
1810bail3:
1811 TEEC_CloseSession(&cs[1]);
1812bail2:
1813 TEEC_CloseSession(&cs[0]);
1814}
1815
1816static void xtest_tee_test_1021(ADBG_Case_t *c)
1817{
1818 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1819 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1820 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1821
1822 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1823 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1824 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1825
1826 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1827 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1828 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1829
1830 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1831 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1832 &sims_keepalive_test_ta_uuid);
1833 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1834}
1835ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1836 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001837
1838static void xtest_tee_test_1022(ADBG_Case_t *c)
1839{
1840 TEEC_Session session = { 0 };
1841 uint32_t ret_orig = 0;
1842
1843 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1844 xtest_teec_open_session(&session, &os_test_ta_uuid,
1845 NULL, &ret_orig)))
1846 return;
1847
1848 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1849 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1850 &ret_orig));
1851
1852 (void)ADBG_EXPECT_TEEC_RESULT(c,
1853 TEEC_ERROR_TARGET_DEAD,
1854 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1855 NULL, &ret_orig));
1856
1857 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1858
1859 TEEC_CloseSession(&session);
1860}
1861ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1862 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001863
1864/*
1865 * Testing the ELF initialization (.init_array)
1866 *
1867 * - The TA has a global variable which can also be accessed by the two shared
1868 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1869 * dlopen())
1870 * - The TA and both libraries have initialization functions (declared with the
1871 * "constructor" attribute) which perform the following:
1872 * * The TA multiplies by 10 then adds 1
1873 * * os_test_lib multiplies by 10 then adds 2
1874 * * os_test_lib_dl multiplies by 10 then adds 3
1875 * By testing the variable value we make sure the initializations occurred in
1876 * the correct order.
1877 */
1878static void xtest_tee_test_1023(ADBG_Case_t *c)
1879{
1880 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1881 TEEC_Session session = { 0 };
1882 uint32_t ret_orig = 0;
1883
1884 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1885 TEEC_NONE, TEEC_NONE);
1886
1887 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1888 xtest_teec_open_session(&session, &os_test_ta_uuid,
1889 NULL, &ret_orig)))
1890 return;
1891
1892 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1893 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1894 &ret_orig));
1895
1896 /* Expected: initialization of os_test_lib, then TA */
1897 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1898
1899 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1900 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1901 &ret_orig));
1902
1903 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1904 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1905 &ret_orig));
1906
1907 /* Expected: initialization of os_test_lib_dl */
1908 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1909
1910 TEEC_CloseSession(&session);
1911}
1912ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1913 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001914
1915#ifdef CFG_CORE_TPM_EVENT_LOG
1916static void xtest_tee_test_1024(ADBG_Case_t *c)
1917{
1918 TEEC_Session session = {};
1919 uint32_t ret_orig = 0;
1920
1921 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1922 NULL, &ret_orig);
1923
1924 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1925 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1926 TA_TPM_TEST_GET_LOG,
1927 NULL, &ret_orig));
1928 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1929
1930 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1931 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1932 TA_TPM_TEST_SHORT_BUF,
1933 NULL, &ret_orig));
1934 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1935
1936 TEEC_CloseSession(&session);
1937}
1938
1939ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1940 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1941#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001942
1943static void xtest_tee_test_1025(ADBG_Case_t *c)
1944{
1945 TEEC_Session session = {};
1946 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1947 uint32_t ret_orig = 0;
1948 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001949 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001950 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001951
1952 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1953
1954 memset(&shm, 0, sizeof(shm));
1955 shm.flags = TEEC_MEM_INPUT;
1956 shm.buffer = NULL;
1957 shm.size = 0;
1958
1959 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1960 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1961
1962 memset(&shm, 0, sizeof(shm));
1963 shm.flags = TEEC_MEM_OUTPUT;
1964 shm.buffer = NULL;
1965 shm.size = 0;
1966
1967 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1968 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1969
1970 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001971
Etienne Carrierec602a522020-04-13 18:53:17 +02001972 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001973 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001974 return;
1975 }
1976
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001977 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1978 xtest_teec_open_session(&session,
1979 &os_test_ta_uuid,
1980 NULL, &ret_orig)))
1981 return;
1982
1983 empty_buf = malloc(1);
1984 if (!empty_buf) {
1985 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001986 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001987 }
1988
1989 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1990 TEEC_MEMREF_TEMP_INPUT,
1991 TEEC_MEMREF_TEMP_OUTPUT,
1992 TEEC_MEMREF_TEMP_OUTPUT);
1993
1994 Do_ADBG_BeginSubCase(c,
1995 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1996
1997 op.params[0].tmpref.buffer = empty_buf;
1998 op.params[0].tmpref.size = 0;
1999
2000 op.params[1].tmpref.buffer = NULL;
2001 op.params[1].tmpref.size = 0;
2002
2003 op.params[2].tmpref.buffer = empty_buf;
2004 op.params[2].tmpref.size = 0;
2005
2006 op.params[3].tmpref.buffer = NULL;
2007 op.params[3].tmpref.size = 0;
2008
2009 ADBG_EXPECT(c, TEE_SUCCESS,
2010 TEEC_InvokeCommand(&session,
2011 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2012 &ret_orig));
2013
2014 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2015
2016 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2017
2018 op.params[0].tmpref.buffer = empty_buf;
2019 op.params[0].tmpref.size = 1;
2020
2021 op.params[1].tmpref.buffer = NULL;
2022 op.params[1].tmpref.size = 0;
2023
2024 op.params[2].tmpref.buffer = empty_buf;
2025 op.params[2].tmpref.size = 0;
2026
2027 op.params[3].tmpref.buffer = NULL;
2028 op.params[3].tmpref.size = 0;
2029
2030 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2031 TEEC_InvokeCommand(&session,
2032 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2033 &ret_orig));
2034
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002035 TEEC_CloseSession(&session);
2036
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002037 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2038
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002039 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2040
2041 /* Pseudo TA is optional: warn and nicely exit if not found */
2042 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2043 &ret_orig);
2044 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2045 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2046 goto out;
2047 }
2048 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2049 goto out;
2050
2051 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2052 TEEC_NONE, TEEC_NONE);
2053 op.params[0].tmpref.buffer = NULL;
2054 op.params[0].tmpref.size = 0;
2055
2056 ADBG_EXPECT(c, TEE_SUCCESS,
2057 TEEC_InvokeCommand(&session,
2058 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2059 &op, &ret_orig));
2060
2061out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002062 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002063out:
2064 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002065 free(empty_buf);
2066}
2067ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2068 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002069
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002070#define TEE_UUID_NS_NAME_SIZE 128
2071
2072/*
2073 * TEE Client UUID name space identifier (UUIDv4)
2074 *
2075 * Value here is random UUID that is allocated as name space identifier for
2076 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2077 */
2078static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2079
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002080/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2081static TEEC_UUID client_uuid_public = { };
2082
2083static void xtest_tee_test_1026(ADBG_Case_t *c)
2084{
2085 TEEC_Result result = TEEC_ERROR_GENERIC;
2086 uint32_t ret_orig = 0;
2087 TEEC_Session session = { };
2088 uint32_t login = UINT32_MAX;
2089 TEEC_UUID client_uuid = { };
2090
2091 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2092 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2093
2094 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2095 return;
2096
2097 result = ta_os_test_cmd_client_identity(&session, &login,
2098 &client_uuid);
2099
2100 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2101 goto out;
2102
2103 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2104
2105 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2106 sizeof(TEEC_UUID));
2107
2108out:
2109 TEEC_CloseSession(&session);
2110}
2111
2112ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2113 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002114
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002115/*
2116 * regression_1027
2117 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002118 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2119 * login client UUID generation")
2120 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002121 *
2122 * xtest skips the test when not built with OpenSSL.
2123 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002124static void xtest_tee_test_1027(ADBG_Case_t *c)
2125{
Victor Chong8e070bc2020-05-13 09:59:33 +01002126#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002127 TEEC_Result result = TEEC_ERROR_GENERIC;
2128 uint32_t ret_orig = 0;
2129 TEEC_Session session = { };
2130 uint32_t login = UINT32_MAX;
2131 TEEC_UUID client_uuid = { };
2132 TEEC_UUID expected_client_uuid = { };
2133 TEEC_UUID uuid_ns = { };
2134 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2135
2136 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2137
2138 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2139 return;
2140
2141 sprintf(uuid_name, "uid=%x", geteuid());
2142
2143 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2144 strlen(uuid_name));
2145 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2146 return;
2147
2148 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2149 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2150
2151 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2152 return;
2153
2154 result = ta_os_test_cmd_client_identity(&session, &login,
2155 &client_uuid);
2156
2157 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2158 goto out;
2159
2160 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2161
2162 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2163 sizeof(TEEC_UUID));
2164
2165out:
2166 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002167#else /*!OPENSSL_FOUND*/
2168 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002169 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002170 /* xtest_uuid_v5() depends on OpenSSL */
2171 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2172#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002173}
2174
2175ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2176 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002177
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002178/*
2179 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002180 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2181 * login client UUID generation")
2182 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002183 *
2184 * xtest skips the test when not built with OpenSSL.
2185 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002186static void xtest_tee_test_1028(ADBG_Case_t *c)
2187{
Victor Chong8e070bc2020-05-13 09:59:33 +01002188#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002189 TEEC_Result result = TEEC_ERROR_GENERIC;
2190 uint32_t ret_orig = 0;
2191 TEEC_Session session = { };
2192 uint32_t login = UINT32_MAX;
2193 TEEC_UUID client_uuid = { };
2194 TEEC_UUID expected_client_uuid = { };
2195 TEEC_UUID uuid_ns = { };
2196 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2197 uint32_t group = 0;
2198
2199 group = getegid();
2200
2201 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2202
2203 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2204 return;
2205
2206 sprintf(uuid_name, "gid=%x", group);
2207
2208 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2209 strlen(uuid_name));
2210 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2211 return;
2212
2213 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2214 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2215
2216 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2217 return;
2218
2219 result = ta_os_test_cmd_client_identity(&session, &login,
2220 &client_uuid);
2221
2222 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2223 goto out;
2224
2225 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2226
2227 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2228 sizeof(TEEC_UUID));
2229
2230out:
2231 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002232#else /*!OPENSSL_FOUND*/
2233 UNUSED(c);
2234 /* xtest_uuid_v5() depends on OpenSSL */
2235 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2236#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002237}
2238
2239ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2240 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002241
2242static void xtest_tee_test_1029(ADBG_Case_t *c)
2243{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002244 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002245 TEEC_Session session = { 0 };
2246 uint32_t ret_orig = 0;
2247
2248 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2249 xtest_teec_open_session(&session, &os_test_ta_uuid,
2250 NULL, &ret_orig)))
2251 return;
2252
2253 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002254 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2255 &ret_orig);
2256 if (res == TEEC_ERROR_NOT_SUPPORTED)
2257 Do_ADBG_Log(" - 1029 - skip test, "
2258 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2259 else
2260 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002261 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2262
2263 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002264 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2265 &ret_orig);
2266 if (res == TEEC_ERROR_NOT_SUPPORTED)
2267 Do_ADBG_Log(" - 1029 - skip test, "
2268 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2269 else
2270 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002271 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2272
2273 TEEC_CloseSession(&session);
2274}
2275ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2276 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002277
2278static void xtest_tee_test_1030(ADBG_Case_t *c)
2279{
2280 TEEC_Session session = { 0 };
2281 uint32_t ret_orig = 0;
2282
2283 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2284 xtest_teec_open_session(&session, &os_test_ta_uuid,
2285 NULL, &ret_orig)))
2286 return;
2287
2288 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2289 ADBG_EXPECT_TEEC_SUCCESS(c,
2290 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2291 &ret_orig));
2292 Do_ADBG_EndSubCase(c, "Before dlopen()");
2293
2294 Do_ADBG_BeginSubCase(c, "After dlopen()");
2295 ADBG_EXPECT_TEEC_SUCCESS(c,
2296 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2297 &ret_orig));
2298 Do_ADBG_EndSubCase(c, "After dlopen()");
2299
2300 TEEC_CloseSession(&session);
2301}
2302ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2303 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002304
2305#ifndef __clang__
2306static void xtest_tee_test_1031(ADBG_Case_t *c)
2307{
2308 TEEC_Result ret = TEE_SUCCESS;
2309 TEEC_Session session = { 0 };
2310 uint32_t ret_orig = 0;
2311
2312 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2313 xtest_teec_open_session(&session, &os_test_ta_uuid,
2314 NULL, &ret_orig)))
2315 return;
2316
2317 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2318 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2319 &ret_orig);
2320 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2321 printf("TA not built with C++ support, skipping C++ tests\n");
2322 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2323 goto out;
2324
2325 }
2326 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2327 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2328
2329 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2330 ADBG_EXPECT_TEEC_SUCCESS(c,
2331 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2332 NULL, &ret_orig));
2333 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2334
2335 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2336 ADBG_EXPECT_TEEC_SUCCESS(c,
2337 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2338 NULL, &ret_orig));
2339 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2340
2341 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2342 ADBG_EXPECT_TEEC_SUCCESS(c,
2343 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2344 &ret_orig));
2345 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2346
2347 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2348 ADBG_EXPECT_TEEC_SUCCESS(c,
2349 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2350 &ret_orig));
2351 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2352out:
2353 TEEC_CloseSession(&session);
2354}
2355ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2356 "Test C++ features");
2357#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002358
2359static void xtest_tee_test_1032(ADBG_Case_t *c)
2360{
2361 TEEC_Result res = TEEC_SUCCESS;
2362 TEEC_Context ctx = { };
2363 TEEC_SharedMemory shm1 = {
2364 .buffer = xtest_tee_test_1032,
2365 .size = 32,
2366 .flags = TEEC_MEM_INPUT,
2367 };
2368 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2369 TEEC_SharedMemory shm2 = {
2370 .buffer = (void *)dummy_data,
2371 .size = sizeof(dummy_data),
2372 .flags = TEEC_MEM_INPUT,
2373 };
2374
2375 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2376 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2377 return;
2378
2379 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2380 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2381 TEEC_ReleaseSharedMemory(&shm1);
2382
2383 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2384 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2385 TEEC_ReleaseSharedMemory(&shm2);
2386
2387 TEEC_FinalizeContext(&ctx);
2388}
2389ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2390 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002391
2392static void xtest_tee_test_1033(ADBG_Case_t *c)
2393{
2394 TEEC_Session session = { };
2395 uint32_t ret_orig = 0;
2396
2397 /* TA will ping the test plugin during open session operation */
2398 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2399 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2400 NULL, &ret_orig)))
2401 return;
2402
2403 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2404 {
2405 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2406
2407 op.params[0].value.a = 20;
2408 op.params[0].value.b = 10;
2409 op.params[1].value.a = '+';
2410 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2411 TEEC_VALUE_INPUT, TEEC_NONE,
2412 TEEC_NONE);
2413
2414 ADBG_EXPECT_TEEC_SUCCESS(c,
2415 TEEC_InvokeCommand(&session,
2416 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2417 &ret_orig));
2418 ADBG_EXPECT(c, 30, op.params[0].value.a);
2419
2420 /* reassign, because the values was changed during previous op */
2421 op.params[0].value.a = 20;
2422 op.params[0].value.b = 10;
2423 op.params[1].value.a = '-';
2424 ADBG_EXPECT_TEEC_SUCCESS(c,
2425 TEEC_InvokeCommand(&session,
2426 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2427 &ret_orig));
2428 ADBG_EXPECT(c, 10, op.params[0].value.a);
2429 }
2430 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2431
2432 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2433 {
2434 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002435 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002436
2437 op.params[0].tmpref.buffer = to_plugin;
2438 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002439 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002440 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002441 TEEC_NONE, TEEC_NONE);
2442
2443 ADBG_EXPECT_TEEC_SUCCESS(c,
2444 TEEC_InvokeCommand(&session,
2445 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2446 &op, &ret_orig));
2447
2448 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002449 * The test plugin must calculate a sum of the input elements
2450 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002451 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002452 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002453 }
2454 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2455
2456 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2457 {
2458 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2459 char from_plugin[64] = { };
2460 char expected_arr[] = "Array from plugin";
2461 size_t expectes_size = sizeof(expected_arr);
2462
2463 op.params[0].tmpref.buffer = from_plugin;
2464 op.params[0].tmpref.size = sizeof(from_plugin);
2465 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2466 TEEC_VALUE_OUTPUT, TEEC_NONE,
2467 TEEC_NONE);
2468 ADBG_EXPECT_TEEC_SUCCESS(c,
2469 TEEC_InvokeCommand(&session,
2470 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2471 &ret_orig));
2472 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2473 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2474 }
2475 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2476
2477 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2478 {
2479 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2480
2481 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2482 TEEC_NONE, TEEC_NONE);
2483 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2484 TEEC_InvokeCommand(&session,
2485 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2486 &ret_orig));
2487 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2488 TEEC_InvokeCommand(&session,
2489 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2490 &ret_orig));
2491 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2492 TEEC_InvokeCommand(&session,
2493 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2494 &ret_orig));
2495 }
2496 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2497
2498 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2499 {
2500 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2501
2502 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2503 TEEC_NONE, TEEC_NONE);
2504 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2505 TEEC_InvokeCommand(&session,
2506 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2507 &op, &ret_orig));
2508 }
2509 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2510
2511 TEEC_CloseSession(&session);
2512}
2513ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2514 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002515
2516static void xtest_tee_test_1034(ADBG_Case_t *c)
2517{
2518 TEEC_Result res = TEEC_SUCCESS;
2519 TEEC_Session session = { };
2520 uint32_t ret_orig = 0;
2521
2522 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2523 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002524 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2525 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
2526 } else {
2527 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002528 TEEC_CloseSession(&session);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002529 }
Jens Wiklander94fb1582021-05-19 10:14:57 +02002530}
2531ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2532 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302533
2534#if defined(CFG_TA_BTI)
2535struct bti_test {
2536 uint32_t cmd;
2537 uint32_t func;
2538};
2539
2540#define BTI_TEST(caller_func, bti_func) { \
2541 .cmd = caller_func, \
2542 .func = bti_func, \
2543 }
2544
2545static const struct bti_test bti_cases_success[] = {
2546 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2547 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2548 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2549 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2550 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2551 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2552 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2553};
2554
2555static const struct bti_test bti_cases_panic[] = {
2556 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2557 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2558 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2559 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2560 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2561};
2562
2563static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2564{
2565 TEEC_Session session = {};
2566 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2567 uint32_t ret_orig = 0;
2568 TEEC_Result res;
2569
2570 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2571 TEEC_NONE);
2572 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2573 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2574 NULL, &ret_orig)))
2575 return;
2576
2577 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2578 &ret_orig);
2579 if (!res) {
2580 if(op.params[0].value.a)
2581 *bti = true;
2582 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2583 }
2584
2585 TEEC_CloseSession(&session);
2586}
2587
2588static void xtest_tee_test_1035(ADBG_Case_t *c)
2589{
2590 TEEC_Session session = {};
2591 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2592 struct bti_test const *test = NULL;
2593 uint32_t ret_orig = 0;
2594 TEEC_Result res;
2595 unsigned int n = 0;
2596 bool cpu_feature_bti = false;
2597
2598 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2599 get_cpu_feature(c, &cpu_feature_bti);
2600 Do_ADBG_EndSubCase(c, "BTI Implemented");
2601
2602 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2603 TEEC_NONE);
2604
2605 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2606 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2607 NULL, &ret_orig)))
2608 return;
2609
2610 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2611 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2612 test = &bti_cases_success[n];
2613
2614 op.params[0].value.a = test->func;
2615
2616 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2617 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2618 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2619 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2620 TEEC_CloseSession(&session);
2621 return;
2622 }
2623
2624 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2625
2626 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2627
2628 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2629 }
2630 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2631
2632 TEEC_CloseSession(&session);
2633
2634 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2635 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2636 test = &bti_cases_panic[n];
2637 res = TEEC_SUCCESS;
2638
2639 if (cpu_feature_bti)
2640 res = TEEC_ERROR_TARGET_DEAD;
2641
2642 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2643 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2644 NULL, &ret_orig)))
2645 goto out;
2646
2647 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2648 op.params[0].value.a = test->func;
2649
2650 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2651 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2652
2653 if (cpu_feature_bti)
2654 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2655 ret_orig);
2656
2657 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2658
2659 TEEC_CloseSession(&session);
2660 }
2661
2662out:
2663 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2664}
2665ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2666#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302667
2668static void xtest_tee_test_1036(ADBG_Case_t *c)
2669{
2670 TEEC_Session session = { };
2671 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2672 uint32_t ret_orig = 0;
2673 TEEC_Result res = TEEC_SUCCESS;
2674
2675 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2676 TEEC_NONE);
2677
2678 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2679 xtest_teec_open_session(&session, &os_test_ta_uuid,
2680 NULL, &ret_orig)))
2681 return;
2682
2683 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2684
2685 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2686 &ret_orig);
2687 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2688 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2689 goto out;
2690 }
2691
2692 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2693 goto out;
2694
2695 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2696
2697 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2698
2699 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2700 TEEC_InvokeCommand(&session,
2701 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2702 &op, &ret_orig));
2703
2704 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2705
2706 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2707
2708out:
2709 TEEC_CloseSession(&session);
2710}
2711ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2712 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002713
2714#define ATT_MAX_KEYSZ 4096
2715
2716#ifdef OPENSSL_FOUND
2717static RSA *att_key;
2718static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2719
2720/*
2721 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2722 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2723 */
2724static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2725 uint8_t *buf)
2726{
2727 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2728 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2729 unsigned char *ta_hash = buf;
2730 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2731 SHA256_CTX ctx = { };
2732 int salt_len = 32; /* Hard-coded in the PTA */
2733 int st = 0;
2734
2735 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2736 return;
2737
2738 SHA256_Init(&ctx);
2739 SHA256_Update(&ctx, nonce, nonce_size);
2740 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2741 SHA256_Final(digest, &ctx);
2742
2743 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2744 RSA_NO_PADDING);
2745 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2746 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2747 EVP_sha256(), decr, salt_len);
2748 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2749}
2750
2751static void free_att_key(void)
2752{
2753 RSA_free(att_key);
2754 att_key = NULL;
2755}
2756
Clement Faure44a31d02022-03-30 10:50:52 +02002757#if OPENSSL_VERSION_NUMBER < 0x10100000L
2758static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2759{
2760 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2761 return 0;
2762
2763 if (n != NULL) {
2764 BN_free(r->n);
2765 r->n = n;
2766 }
2767
2768 if (e != NULL) {
2769 BN_free(r->e);
2770 r->e = e;
2771 }
2772
2773 if (d != NULL) {
2774 BN_free(r->d);
2775 r->d = d;
2776 }
2777
2778 return 1;
2779}
2780#endif
2781
Jerome Forissier0915b222021-10-27 18:20:59 +02002782static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2783 size_t n_sz)
2784{
2785 BIGNUM *bn_e = NULL;
2786 BIGNUM *bn_n = NULL;
2787 int st = 0;
2788
2789 att_key_size = n_sz;
2790 att_key = RSA_new();
2791 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2792 return;
2793
2794 bn_e = BN_bin2bn(e, e_sz, NULL);
2795 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2796 goto err;
2797 bn_n = BN_bin2bn(n, n_sz, NULL);
2798 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2799 goto err;
2800
2801 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2802 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2803 goto err;
2804 return;
2805err:
2806 free_att_key();
2807}
2808#else
2809#define check_signature(...)
2810#define set_att_key(...)
2811#define free_att_key()
2812#endif
2813
2814/*
2815 * Verification of the output of the attestation PTA
2816 * - (If hash != NULL) check buf contains the expected hash
2817 * - (If OpenSSL is available) Check that the signature is valid
2818 */
2819static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2820 uint8_t *hash, uint8_t *buf)
2821{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002822 (void)nonce;
2823 (void)nonce_size;
2824
Jerome Forissier0915b222021-10-27 18:20:59 +02002825 if (hash)
2826 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2827
2828 check_signature(c, nonce, nonce_size, buf);
2829}
2830
2831/* Invoke attestation PTA to return the public key */
2832static void get_att_public_key(ADBG_Case_t *c)
2833{
2834 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2835 uint8_t e[3] = { }; /* We know e == 65537... */
2836 size_t n_sz = sizeof(n);
2837 size_t e_sz = sizeof(e);
2838 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2839 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2840 TEEC_Result res = TEEC_ERROR_GENERIC;
2841 TEEC_Session session = { };
2842 uint32_t ret_orig = 0;
2843
2844 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2845 TEEC_MEMREF_TEMP_OUTPUT,
2846 TEEC_VALUE_OUTPUT, TEEC_NONE);
2847 op.params[0].tmpref.buffer = e;
2848 op.params[0].tmpref.size = e_sz;
2849 op.params[1].tmpref.buffer = n;
2850 op.params[1].tmpref.size = n_sz;
2851
2852 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2853 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2854 return;
2855
2856 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2857 &ret_orig);
2858
2859 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2860 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2861 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2862 goto out;
2863
2864 e_sz = op.params[0].tmpref.size;
2865 n_sz = op.params[1].tmpref.size;
2866 set_att_key(c, e, e_sz, n, n_sz);
2867out:
2868 TEEC_CloseSession(&session);
2869}
2870
2871/* Invoke attestation PTA to hash the TEE binary */
2872static void attestation_tee(ADBG_Case_t *c)
2873{
2874 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2875 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2876 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2877 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2878 TEEC_Result res = TEEC_ERROR_GENERIC;
2879 TEEC_Session session = { };
2880 uint32_t ret_orig = 0;
2881
2882 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2883 TEEC_MEMREF_TEMP_OUTPUT,
2884 TEEC_NONE, TEEC_NONE);
2885 op.params[0].tmpref.buffer = nonce;
2886 op.params[0].tmpref.size = sizeof(nonce);
2887 op.params[1].tmpref.buffer = measurement;
2888 op.params[1].tmpref.size = sizeof(measurement);
2889
2890 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2891 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2892 return;
2893
2894 /* Hash TEE and check signature */
2895 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2896 &ret_orig);
2897 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2898 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2899
2900 TEEC_CloseSession(&session);
2901}
2902
2903/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2904static void attestation_ta_shdr(ADBG_Case_t *c)
2905{
2906 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2907 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2908 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2909 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2910 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2911 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2912 TEEC_Result res = TEEC_ERROR_GENERIC;
2913 TEEC_Session session = { };
2914 uint32_t ret_orig = 0;
2915 int cmp = 0;
2916
2917 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2918 TEEC_MEMREF_TEMP_INPUT,
2919 TEEC_MEMREF_TEMP_OUTPUT,
2920 TEEC_NONE);
2921 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2922 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2923 op.params[1].tmpref.buffer = nonce;
2924 op.params[1].tmpref.size = sizeof(nonce);
2925 op.params[2].tmpref.buffer = measurement;
2926 op.params[2].tmpref.size = sizeof(measurement);
2927
2928 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2929 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2930 return;
2931
2932 /* Hash TA and check signature */
2933 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2934 &op, &ret_orig);
2935 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2936 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2937 /* Save hash */
2938 memcpy(hash1, measurement, 32);
2939
2940 /* Hash TA again */
2941 memset(measurement, 0, sizeof(measurement));
2942 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2943 &op, &ret_orig);
2944 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2945 /* New hash should be identical */
2946 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2947
2948 /* Hash another TA */
2949 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
2950 memset(measurement, 0, sizeof(measurement));
2951 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2952 &op, &ret_orig);
2953 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2954 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2955 memcpy(hash2, measurement, 32);
2956 /* Different binaries should have different hashes */
2957 cmp = memcmp(hash1, hash2, sizeof(hash1));
2958 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
2959
2960 TEEC_CloseSession(&session);
2961}
2962
2963/*
2964 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
2965 * itself.
2966 */
2967static void attestation_ta_memory(ADBG_Case_t *c)
2968{
2969 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2970 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2971 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2972 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2973 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2974 TEEC_Result res = TEEC_ERROR_GENERIC;
2975 TEEC_Session session = { };
2976 uint32_t ret_orig = 0;
2977 int cmp = 0;
2978
2979 Do_ADBG_BeginSubCase(c, "Consecutive calls");
2980
2981 /* Open session to os_test TA */
2982 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
2983 &ret_orig);
2984 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2985 return;
2986
2987 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2988 TEEC_MEMREF_TEMP_OUTPUT,
2989 TEEC_NONE, TEEC_NONE);
2990 op.params[0].tmpref.buffer = nonce;
2991 op.params[0].tmpref.size = sizeof(nonce);
2992 op.params[1].tmpref.buffer = measurement;
2993 op.params[1].tmpref.size = sizeof(measurement);
2994
2995 /* Hash TA */
2996 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2997 &ret_orig);
2998 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2999
3000 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3001 memcpy(hash1, measurement, 32);
3002
3003 /* Hash TA again */
3004 memset(measurement, 0, sizeof(measurement));
3005 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3006 &ret_orig);
3007 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3008
3009 /* New hash should be identical to hash1 */
3010 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3011
3012 Do_ADBG_EndSubCase(c, "Consecutive calls");
3013
3014 /* Close TA session, will cause unload of TA */
3015 TEEC_CloseSession(&session);
3016
3017 Do_ADBG_BeginSubCase(c, "TA reload");
3018
3019 /* Load TA again and open a new session */
3020 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3021 &ret_orig);
3022 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3023 if (res)
3024 return;
3025
3026 /* Hash TA one more time */
3027 memset(measurement, 0, sizeof(measurement));
3028 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3029 &ret_orig);
3030 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3031
3032 /* Hash after reload should still be the same */
3033 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3034
3035 Do_ADBG_EndSubCase(c, "TA reload");
3036
3037 Do_ADBG_BeginSubCase(c, "Add shared library");
3038
3039 /*
3040 * Invoke a TA command that causes some additional code to be mapped
3041 * (shared library)
3042 */
3043 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3044 &ret_orig);
3045 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3046
3047 /* Hash TA one last time */
3048 memset(measurement, 0, sizeof(measurement));
3049 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3050 &ret_orig);
3051 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3052
3053 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3054 memcpy(hash2, measurement, 32);
3055
3056 /* Different binaries mapped mean different hashes */
3057 cmp = memcmp(hash1, hash2, sizeof(hash1));
3058 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3059
3060 Do_ADBG_EndSubCase(c, "Add shared library");
3061
3062 TEEC_CloseSession(&session);
3063}
3064
3065static void xtest_tee_test_1037(ADBG_Case_t *c)
3066{
3067 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3068 TEEC_Result res = TEEC_ERROR_GENERIC;
3069 TEEC_Session session = { };
3070 uint32_t ret_orig = 0;
3071
3072 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3073 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3074 Do_ADBG_Log(" skip test, pseudo TA not found");
3075 return;
3076 }
3077
3078 Do_ADBG_BeginSubCase(c, "Get public key");
3079 get_att_public_key(c);
3080 Do_ADBG_EndSubCase(c, "Get public key");
3081
3082 Do_ADBG_BeginSubCase(c, "TEE attestation");
3083 attestation_tee(c);
3084 Do_ADBG_EndSubCase(c, "TEE attestation");
3085
3086 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3087 attestation_ta_shdr(c);
3088 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3089
3090 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3091 attestation_ta_memory(c);
3092 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3093
3094 free_att_key();
3095}
3096ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3097 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003098
3099static void xtest_tee_test_1038(ADBG_Case_t *c)
3100{
3101 TEEC_Session session = { };
3102 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3103 uint32_t ret_orig = 0;
3104 TEEC_Result res = TEEC_SUCCESS;
3105
3106 Do_ADBG_BeginSubCase(c, "MTE use after free");
3107
3108 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3109 TEEC_NONE);
3110
3111 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3112 xtest_teec_open_session(&session, &os_test_ta_uuid,
3113 NULL, &ret_orig)))
3114 return;
3115
3116 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3117 &op, &ret_orig);
3118 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3119 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3120 goto out;
3121 }
3122
3123 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3124 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3125 TEEC_CloseSession(&session);
3126
3127 Do_ADBG_EndSubCase(c, "MTE use after free");
3128
3129 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3130
3131 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3132 TEEC_NONE);
3133
3134 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3135 xtest_teec_open_session(&session, &os_test_ta_uuid,
3136 NULL, &ret_orig)))
3137 return;
3138
3139 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3140 &op, &ret_orig);
3141 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3142 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3143 goto out;
3144 }
3145
3146 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3147 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3148
3149 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3150
3151 Do_ADBG_BeginSubCase(c, "MTE double free");
3152
3153 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3154 TEEC_NONE);
3155
3156 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3157 xtest_teec_open_session(&session, &os_test_ta_uuid,
3158 NULL, &ret_orig)))
3159 return;
3160
3161 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3162 &op, &ret_orig);
3163 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3164 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3165 goto out;
3166 }
3167
3168 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3169 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3170
3171 Do_ADBG_EndSubCase(c, "MTE double free");
3172
3173 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3174
3175 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3176 TEEC_NONE);
3177
3178 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3179 xtest_teec_open_session(&session, &os_test_ta_uuid,
3180 NULL, &ret_orig)))
3181 return;
3182
3183 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3184 &op, &ret_orig);
3185 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3186 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3187 goto out;
3188 }
3189
3190 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3191 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3192
3193 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3194
3195
3196out:
3197 TEEC_CloseSession(&session);
3198}
3199ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3200 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003201
3202static void xtest_tee_test_1039(ADBG_Case_t *c)
3203{
3204 TEEC_Session session = { };
3205 uint32_t ret_orig = 0;
3206
3207 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3208 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3209 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3210 NULL, &ret_orig)))
3211 TEEC_CloseSession(&session);
3212 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3213
3214 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3215 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3216 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3217 NULL, &ret_orig)))
3218 TEEC_CloseSession(&session);
3219 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3220
3221}
3222
3223
3224ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3225 "Test subkey verification");