blob: 850b3cb6bee2129f5efd3ba08f10a5973dfdb470 [file] [log] [blame]
Etienne Carriere9b7b70d2020-05-16 10:27:23 +02001// SPDX-License-Identifier: GPL-2.0
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00003 * Copyright (c) 2020, ARM Limited. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02004 * Copyright (c) 2014, STMicroelectronics International N.V.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03007#include <errno.h>
Etienne Carrierea4653552017-01-11 10:04:24 +01008#include <limits.h>
Jerome Forissier0915b222021-10-27 18:20:59 +02009#ifdef OPENSSL_FOUND
10#include <openssl/bn.h>
11#include <openssl/err.h>
12#include <openssl/evp.h>
13#include <openssl/rsa.h>
14#include <openssl/sha.h>
15#endif
16#include <pta_attestation.h>
17#include <pta_invoke_tests.h>
18#include <pta_secstor_ta_mgmt.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010019#include <pthread.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020020#include <sdp_basic.h>
21#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020022#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010023#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020024#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060025#include <sys/stat.h>
26#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020027#include <ta_arm_bti.h>
28#include <ta_concurrent.h>
29#include <ta_create_fail_test.h>
30#include <ta_crypt.h>
31#include <ta_miss_test.h>
32#include <ta_os_test.h>
33#include <ta_rpc_test.h>
34#include <ta_sims_keepalive_test.h>
35#include <ta_sims_test.h>
36#include <ta_supp_plugin.h>
37#include <ta_tpm_log_test.h>
38#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010039#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020040#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010041#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020042
Jerome Forissier0915b222021-10-27 18:20:59 +020043#include "xtest_helpers.h"
44#include "xtest_test.h"
45#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030046
Jens Wiklanderec545fb2017-11-24 16:58:07 +010047#ifndef MIN
48#define MIN(a, b) ((a) < (b) ? (a) : (b))
49#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020050
Pascal Brandc639ac82015-07-02 08:53:34 +020051struct xtest_crypto_session {
52 ADBG_Case_t *c;
53 TEEC_Session *session;
54 uint32_t cmd_id_sha256;
55 uint32_t cmd_id_aes256ecb_encrypt;
56 uint32_t cmd_id_aes256ecb_decrypt;
57};
58
59static void xtest_crypto_test(struct xtest_crypto_session *cs)
60{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010061 uint32_t ret_orig = 0;
62 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020063 uint8_t crypt_in[16] = { 22, 17 };
64
65 crypt_in[15] = 60;
66
67 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
68 {
69 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
70
71 op.params[0].tmpref.buffer = crypt_in;
72 op.params[0].tmpref.size = sizeof(crypt_in);
73 op.params[1].tmpref.buffer = crypt_out;
74 op.params[1].tmpref.size = sizeof(crypt_out);
75 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
76 TEEC_MEMREF_TEMP_OUTPUT,
77 TEEC_NONE, TEEC_NONE);
78
79 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
80 TEEC_InvokeCommand(cs->session,
81 cs->
82 cmd_id_aes256ecb_encrypt,
83 &op,
84 &ret_orig));
85 }
86 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
87
88 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
89 {
90 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010091 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020092
93 op.params[0].tmpref.buffer = crypt_out;
94 op.params[0].tmpref.size = sizeof(crypt_out);
95 op.params[1].tmpref.buffer = out;
96 op.params[1].tmpref.size = sizeof(out);
97 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
98 TEEC_MEMREF_TEMP_OUTPUT,
99 TEEC_NONE, TEEC_NONE);
100
101 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
102 TEEC_InvokeCommand(cs->session,
103 cs->
104 cmd_id_aes256ecb_decrypt,
105 &op,
106 &ret_orig));
107
108 if (!ADBG_EXPECT(cs->c, 0,
109 memcmp(crypt_in, out, sizeof(crypt_in)))) {
110 Do_ADBG_Log("crypt_in:");
111 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
112 Do_ADBG_Log("out:");
113 Do_ADBG_HexLog(out, sizeof(out), 16);
114 }
115 }
116 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
117
118 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
119 {
120 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
121 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
122 static const uint8_t sha256_out[] = {
123 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
124 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
125 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
126 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
127 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100128 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200129
130 op.params[0].tmpref.buffer = (void *)sha256_in;
131 op.params[0].tmpref.size = sizeof(sha256_in);
132 op.params[1].tmpref.buffer = out;
133 op.params[1].tmpref.size = sizeof(out);
134 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
135 TEEC_MEMREF_TEMP_OUTPUT,
136 TEEC_NONE, TEEC_NONE);
137
138 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
139 TEEC_InvokeCommand(cs->session,
140 cs->
141 cmd_id_sha256,
142 &op,
143 &ret_orig));
144
145 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
146 sizeof(sha256_out)))) {
147 Do_ADBG_Log("sha256_out:");
148 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
149 Do_ADBG_Log("out:");
150 Do_ADBG_HexLog(out, sizeof(out), 16);
151 }
152 }
153 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
154
Etienne Carrierea3198522017-10-26 09:48:55 +0200155 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200156 {
157 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
158 static const uint8_t in[] = {
159 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
160 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
161 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
162 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
163 };
164 static const uint8_t exp_out[] = {
165 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
166 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
167 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
168 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
169 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100170 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200171
172 op.params[0].tmpref.buffer = (void *)in;
173 op.params[0].tmpref.size = sizeof(in);
174 op.params[1].tmpref.buffer = out;
175 op.params[1].tmpref.size = sizeof(out);
176 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
177 TEEC_MEMREF_TEMP_OUTPUT,
178 TEEC_NONE, TEEC_NONE);
179
180 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
181 TEEC_InvokeCommand(cs->session,
182 cs->
183 cmd_id_aes256ecb_encrypt,
184 &op,
185 &ret_orig));
186
187 if (!ADBG_EXPECT(cs->c, 0,
188 memcmp(exp_out, out, sizeof(exp_out)))) {
189 Do_ADBG_Log("exp_out:");
190 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
191 Do_ADBG_Log("out:");
192 Do_ADBG_HexLog(out, sizeof(out), 16);
193 }
194 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200195 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200196
Etienne Carrierea3198522017-10-26 09:48:55 +0200197 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200198 {
199 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
200 static const uint8_t in[] = {
201 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
202 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
203 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
204 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
205 };
206 static const uint8_t exp_out[] = {
207 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
208 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
209 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
210 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
211 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100212 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200213
214 op.params[0].tmpref.buffer = (void *)in;
215 op.params[0].tmpref.size = sizeof(in);
216 op.params[1].tmpref.buffer = out;
217 op.params[1].tmpref.size = sizeof(out);
218 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
219 TEEC_MEMREF_TEMP_OUTPUT,
220 TEEC_NONE, TEEC_NONE);
221
222 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
223 TEEC_InvokeCommand(cs->session,
224 cs->
225 cmd_id_aes256ecb_decrypt,
226 &op,
227 &ret_orig));
228
229 if (!ADBG_EXPECT(cs->c, 0,
230 memcmp(exp_out, out, sizeof(exp_out)))) {
231 Do_ADBG_Log("exp_out:");
232 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
233 Do_ADBG_Log("out:");
234 Do_ADBG_HexLog(out, sizeof(out), 16);
235 }
236 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200237 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200238}
239
240static void xtest_tee_test_1001(ADBG_Case_t *c)
241{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100242 TEEC_Result res = TEEC_ERROR_GENERIC;
243 TEEC_Session session = { };
244 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200245
Etienne Carriere11093162017-10-26 09:49:04 +0200246 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100247 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100248 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200249 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
250 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100251 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200252 }
253 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200254
Jens Wiklandercf16e842016-02-10 09:07:09 +0100255 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100256 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100257 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200258}
Jens Wiklander14f48872018-06-29 15:30:13 +0200259ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200260
Jens Wiklander1d70a112017-10-16 15:16:39 +0200261static void xtest_tee_test_1002(ADBG_Case_t *c)
262{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100263 TEEC_Result res = TEEC_ERROR_GENERIC;
264 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200265 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100266 uint32_t ret_orig = 0;
267 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200268 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100269 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200270
Etienne Carriere11093162017-10-26 09:49:04 +0200271 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200272 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
273 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200274 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
275 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200276 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200277 }
278 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200279
280 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
281 TEEC_NONE, TEEC_NONE);
282 op.params[0].tmpref.size = sizeof(buf);
283 op.params[0].tmpref.buffer = buf;
284
285 for (n = 0; n < sizeof(buf); n++)
286 buf[n] = n + 1;
287 for (n = 0; n < sizeof(buf); n++)
288 exp_sum += buf[n];
289
290 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
291 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
292 goto out;
293
294 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
295out:
296 TEEC_CloseSession(&session);
297}
Jens Wiklander14f48872018-06-29 15:30:13 +0200298ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200299
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100300struct test_1003_arg {
301 uint32_t test_type;
302 size_t repeat;
303 size_t max_before_lockers;
304 size_t max_during_lockers;
305 size_t before_lockers;
306 size_t during_lockers;
307 TEEC_Result res;
308 uint32_t error_orig;
309};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200310
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100311static void *test_1003_thread(void *arg)
312{
313 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100314 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100315 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100316 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100317
318 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
319 NULL, &a->error_orig);
320 if (a->res != TEEC_SUCCESS)
321 return NULL;
322
323 for (n = 0; n < a->repeat; n++) {
324 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
325
326 op.params[0].value.a = a->test_type;
327 op.params[0].value.b = rounds;
328
329 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
330 TEEC_VALUE_OUTPUT,
331 TEEC_NONE, TEEC_NONE);
332 a->res = TEEC_InvokeCommand(&session,
333 PTA_INVOKE_TESTS_CMD_MUTEX,
334 &op, &a->error_orig);
335 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
336 op.params[1].value.b != 1) {
337 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
338 a->res = TEEC_ERROR_BAD_STATE;
339 a->error_orig = 42;
340 break;
341 }
342
343 if (a->test_type == PTA_MUTEX_TEST_READER) {
344 if (op.params[1].value.a > a->max_before_lockers)
345 a->max_before_lockers = op.params[1].value.a;
346
347 if (op.params[1].value.b > a->max_during_lockers)
348 a->max_during_lockers = op.params[1].value.b;
349
350 a->before_lockers += op.params[1].value.a;
351 a->during_lockers += op.params[1].value.b;
352 }
353 }
354 TEEC_CloseSession(&session);
355
356 return NULL;
357}
358
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100359#define TEST_1003_THREAD_COUNT (3 * 2)
360
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100361static void xtest_tee_test_1003(ADBG_Case_t *c)
362{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100363 TEEC_Result res = TEEC_ERROR_GENERIC;
364 TEEC_Session session = { };
365 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100366 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100367 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100368 size_t max_read_concurrency = 0;
369 size_t max_read_waiters = 0;
370 size_t num_concurrent_read_lockers = 0;
371 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100372 size_t n = 0;
373 size_t nt = TEST_1003_THREAD_COUNT;
374 double mean_read_concurrency = 0;
375 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100376 size_t num_writers = 0;
377 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100378 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100379
380 /* Pseudo TA is optional: warn and nicely exit if not found */
381 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
382 &ret_orig);
383 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
384 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
385 return;
386 }
387 ADBG_EXPECT_TEEC_SUCCESS(c, res);
388 TEEC_CloseSession(&session);
389
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100390 for (n = 0; n < nt; n++) {
391 if (n % 3) {
392 arg[n].test_type = PTA_MUTEX_TEST_READER;
393 num_readers++;
394 } else {
395 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
396 num_writers++;
397 }
398 arg[n].repeat = repeat;
399 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
400 test_1003_thread, arg + n)))
401 nt = n; /* break loop and start cleanup */
402 }
403
404 for (n = 0; n < nt; n++) {
405 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
406 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
407 Do_ADBG_Log("error origin %" PRIu32,
408 arg[n].error_orig);
409 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
410 if (arg[n].max_during_lockers > max_read_concurrency)
411 max_read_concurrency =
412 arg[n].max_during_lockers;
413
414 if (arg[n].max_before_lockers > max_read_waiters)
415 max_read_waiters = arg[n].max_before_lockers;
416
417 num_concurrent_read_lockers += arg[n].during_lockers;
418 num_concurrent_read_waiters += arg[n].before_lockers;
419 }
420 }
421
422 mean_read_concurrency = (double)num_concurrent_read_lockers /
423 (double)(repeat * num_readers);
424 mean_read_waiters = (double)num_concurrent_read_waiters /
425 (double)(repeat * num_readers);
426
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100427 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
428 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100429 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
430 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
431 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
432 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
433}
Jens Wiklander14f48872018-06-29 15:30:13 +0200434ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
435 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200436
Pascal Brandc639ac82015-07-02 08:53:34 +0200437static void xtest_tee_test_1004(ADBG_Case_t *c)
438{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100439 TEEC_Session session = { };
440 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200441 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
442 TA_CRYPT_CMD_AES256ECB_ENC,
443 TA_CRYPT_CMD_AES256ECB_DEC };
444
445 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
446 &session, &crypt_user_ta_uuid,
447 NULL, &ret_orig)))
448 return;
449
450 /* Run the "complete crypto test suite" */
451 xtest_crypto_test(&cs);
452
453 TEEC_CloseSession(&session);
454}
Jens Wiklander14f48872018-06-29 15:30:13 +0200455ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200456
Etienne Carriere92c34422018-02-09 13:11:40 +0100457static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200458{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100459 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200460 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100461 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200462
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300463 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200464 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300465 &ret_orig)))
466 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200467
468 op.params[0].value.a = n;
469 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
470 TEEC_NONE);
471
472 (void)ADBG_EXPECT_TEEC_RESULT(c,
473 TEEC_ERROR_TARGET_DEAD,
474 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
475 &ret_orig));
476
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300477 (void)ADBG_EXPECT_TEEC_RESULT(c,
478 TEEC_ERROR_TARGET_DEAD,
479 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200480 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300481
Pascal Brandc639ac82015-07-02 08:53:34 +0200482 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
483
484 TEEC_CloseSession(&session);
485}
486
Etienne Carriere92c34422018-02-09 13:11:40 +0100487static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
488 size_t size)
489{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100490 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100491 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100492 uint32_t ret_orig = 0;
493 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100494
Etienne Carriere92c34422018-02-09 13:11:40 +0100495 shm.size = size;
496 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
497 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
498 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
499 return;
500
501 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
502 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
503 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200504 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100505
506 op.params[0].value.a = (uint32_t)n;
507 op.params[1].memref.parent = &shm;
508 op.params[1].memref.size = size;
509 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
510 TEEC_NONE, TEEC_NONE);
511
512 (void)ADBG_EXPECT_TEEC_RESULT(c,
513 TEEC_ERROR_TARGET_DEAD,
514 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
515 &ret_orig));
516
517 (void)ADBG_EXPECT_TEEC_RESULT(c,
518 TEEC_ERROR_TARGET_DEAD,
519 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
520 &ret_orig));
521
522 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
523
524 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200525rel_shm:
526 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100527}
528
Pascal Brandc639ac82015-07-02 08:53:34 +0200529static void xtest_tee_test_1005(ADBG_Case_t *c)
530{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100531 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200532#define MAX_SESSIONS 3
533 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100534 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200535
536 for (i = 0; i < MAX_SESSIONS; i++) {
537 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200538 xtest_teec_open_session(&sessions[i],
539 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200540 NULL, &ret_orig)))
541 break;
542 }
543
544 for (; --i >= 0; )
545 TEEC_CloseSession(&sessions[i]);
546}
Jens Wiklander14f48872018-06-29 15:30:13 +0200547ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200548
549static void xtest_tee_test_1006(ADBG_Case_t *c)
550{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100551 TEEC_Session session = { };
552 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200553 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100554 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200555
556 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
557 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
558 &ret_orig)))
559 return;
560
561 op.params[0].tmpref.buffer = buf;
562 op.params[0].tmpref.size = sizeof(buf);
563 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
564 TEEC_NONE, TEEC_NONE);
565
566 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
567 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
568 &ret_orig));
569
570 TEEC_CloseSession(&session);
571}
Jens Wiklander14f48872018-06-29 15:30:13 +0200572ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
573 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200574
575static void xtest_tee_test_1007(ADBG_Case_t *c)
576{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100577 TEEC_Session session = { };
578 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200579
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300580 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200581 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300582 &ret_orig)))
583 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200584
585 (void)ADBG_EXPECT_TEEC_RESULT(c,
586 TEEC_ERROR_TARGET_DEAD,
587 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
588 &ret_orig));
589
590 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
591
592 (void)ADBG_EXPECT_TEEC_RESULT(c,
593 TEEC_ERROR_TARGET_DEAD,
594 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
595 &ret_orig));
596
597 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
598
599 TEEC_CloseSession(&session);
600}
Jens Wiklander14f48872018-06-29 15:30:13 +0200601ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200602
Jerome Forissierf02a2212015-10-29 14:33:35 +0100603#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000604# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800605#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000606# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100607#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000608# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100609#endif
610
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100611static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600612{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100613 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600614
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100615 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100616 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100617 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200618 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
619 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
620 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600621 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200622
Jens Wiklanderb7940892015-10-23 16:02:40 +0200623 return fopen(buf, mode);
624}
625
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100626static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200627{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100628 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100629 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
630 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100631 TEEC_Result res = TEEC_ERROR_GENERIC;
632 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100633 FILE *f = NULL;
634 bool r = false;
635 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100636 size_t sz = 0;
637 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200638
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100639 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
640 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
641 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200642
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100643 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
644 if (!ADBG_EXPECT_NOT_NULL(c, f))
645 goto out;
646 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
647 goto out;
648 sz = ftell(f);
649 rewind(f);
650
651 buf = malloc(sz);
652 if (!ADBG_EXPECT_NOT_NULL(c, buf))
653 goto out;
654
655 fread_res = fread(buf, 1, sz, f);
656 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
657 goto out;
658
Jens Wiklander4441fe22015-10-23 16:53:02 +0200659 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100660 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200661
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100662 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200663
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100664 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
665 TEEC_NONE, TEEC_NONE);
666 op.params[0].tmpref.buffer = buf;
667 op.params[0].tmpref.size = sz;
668
669 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
670 &ret_orig);
671 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
672out:
673 free(buf);
674 if (f)
675 fclose(f);
676 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200677 return r;
678}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100679
680static void test_1008_corrupt_ta(ADBG_Case_t *c)
681{
682 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
683 TEEC_Result res = TEEC_ERROR_GENERIC;
684 TEEC_Session session = { };
685 uint32_t ret_orig = 0;
686
687 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
688 if (res) {
689 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
690 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200691 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100692 return;
693 }
694 TEEC_CloseSession(&session);
695
696 ADBG_EXPECT_TRUE(c,
697 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
698 ADBG_EXPECT_TRUE(c,
699 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
700 ADBG_EXPECT_TRUE(c,
701 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
702 ADBG_EXPECT_TRUE(c,
703 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
704 ADBG_EXPECT_TRUE(c,
705 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
706 ADBG_EXPECT_TRUE(c,
707 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
708 ADBG_EXPECT_TRUE(c,
709 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
710 ADBG_EXPECT_TRUE(c,
711 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
712 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
713 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
714}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200715
Pascal Brandc639ac82015-07-02 08:53:34 +0200716static void xtest_tee_test_1008(ADBG_Case_t *c)
717{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100718 TEEC_Session session = { };
719 TEEC_Session session_crypt = { };
720 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200721
722 Do_ADBG_BeginSubCase(c, "Invoke command");
723 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300724 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200725 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300726 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200727
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300728 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
729 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
730 NULL, &ret_orig));
731 TEEC_CloseSession(&session);
732 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200733
Pascal Brandc639ac82015-07-02 08:53:34 +0200734 }
735 Do_ADBG_EndSubCase(c, "Invoke command");
736
737 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
738 {
739 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
740
741 op.params[0].value.a = 2000;
742 op.paramTypes = TEEC_PARAM_TYPES(
743 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
744
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300745 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200746 xtest_teec_open_session(&session,
747 &os_test_ta_uuid,
748 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300749 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200750
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300751 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
752 TEEC_InvokeCommand(&session,
753 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
754 &op, &ret_orig));
755 TEEC_CloseSession(&session);
756 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200757 }
758 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
759
760 Do_ADBG_BeginSubCase(c, "Create session fail");
761 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100762 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200763
Pascal Brandc639ac82015-07-02 08:53:34 +0200764 for (n = 0; n < 100; n++) {
765 Do_ADBG_Log("n = %zu", n);
766 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
767 xtest_teec_open_session(&session_crypt,
768 &create_fail_test_ta_uuid,
769 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100770 /* level > 0 may be used to detect/debug memory leaks */
771 if (!level)
772 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200773 }
774 }
775 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200776
Jens Wiklander4441fe22015-10-23 16:53:02 +0200777 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100778 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200779 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200780}
Jens Wiklander14f48872018-06-29 15:30:13 +0200781ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
782 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200783
Pascal Brandc639ac82015-07-02 08:53:34 +0200784static void *cancellation_thread(void *arg)
785{
786 /*
787 * Sleep 0.5 seconds before cancellation to make sure that the other
788 * thread is in RPC_WAIT.
789 */
790 (void)usleep(500000);
791 TEEC_RequestCancellation(arg);
792 return NULL;
793}
Pascal Brandc639ac82015-07-02 08:53:34 +0200794
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300795static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
796 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200797{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100798 TEEC_Session session = { };
799 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200801
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100802 memset(&thr, 0, sizeof(thr));
803
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300804 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200805 {
806 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
807
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300808 if (ADBG_EXPECT_TEEC_SUCCESS(c,
809 xtest_teec_open_session(&session, &os_test_ta_uuid,
810 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200811
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300812 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
813 TEEC_ORIGIN_TRUSTED_APP,
814 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200815
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300816 op.params[0].value.a = timeout;
817 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
818 TEEC_NONE,
819 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820 if (cancel) {
821 (void)ADBG_EXPECT(c, 0,
822 pthread_create(&thr, NULL,
823 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200824
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300825 (void)ADBG_EXPECT_TEEC_RESULT(c,
826 TEEC_ERROR_CANCEL,
827 TEEC_InvokeCommand(&session,
828 TA_OS_TEST_CMD_WAIT,
829 &op,
830 &ret_orig));
831 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300832
833 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
834 TEEC_InvokeCommand(&session,
835 TA_OS_TEST_CMD_WAIT,
836 &op,
837 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300838 if (cancel)
839 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300840
841 TEEC_CloseSession(&session);
842 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200843 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300844 Do_ADBG_EndSubCase(c, "%s", subcase);
845}
846
847static void xtest_tee_test_1009(ADBG_Case_t *c)
848{
849 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
850 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300851 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300852 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200853}
Jens Wiklander14f48872018-06-29 15:30:13 +0200854ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200855
856static void xtest_tee_test_1010(ADBG_Case_t *c)
857{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100858 unsigned int n = 0;
859 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100860 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200861
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200862 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200863 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
864 xtest_tee_test_invalid_mem_access(c, n);
865 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
866 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100867
868 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
869 for (n = 1; n <= 5; n++) {
870 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200871 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100872 n, memref_sz[idx]);
873 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
874 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200875 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100876 n, memref_sz[idx]);
877 }
878 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200879}
Jens Wiklander14f48872018-06-29 15:30:13 +0200880ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
881 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200882
883static void xtest_tee_test_1011(ADBG_Case_t *c)
884{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100885 TEEC_Session session = { };
886 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200887 struct xtest_crypto_session cs = {
888 c, &session, TA_RPC_CMD_CRYPT_SHA256,
889 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
890 TA_RPC_CMD_CRYPT_AES256ECB_DEC
891 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100892 struct xtest_crypto_session cs_privmem = {
893 c, &session,
894 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
895 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
896 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
897 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200898 TEEC_UUID uuid = rpc_test_ta_uuid;
899
900 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
901 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
902 return;
903
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100904 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200905 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100906 * Run the "complete crypto test suite" using TA-to-TA
907 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200908 */
909 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100910 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
911
912 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
913 /*
914 * Run the "complete crypto test suite" using TA-to-TA
915 * communication via TA private memory.
916 */
917 xtest_crypto_test(&cs_privmem);
918 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
919
Pascal Brandc639ac82015-07-02 08:53:34 +0200920 TEEC_CloseSession(&session);
921}
Jens Wiklander14f48872018-06-29 15:30:13 +0200922ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
923 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200924
925/*
926 * Note that this test is failing when
927 * - running twice in a raw
928 * - and the user TA is statically linked
929 * This is because the counter is not reseted when opening the first session
930 * in case the TA is statically linked
931 */
932static void xtest_tee_test_1012(ADBG_Case_t *c)
933{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100934 TEEC_Session session1 = { };
935 TEEC_Session session2 = { };
936 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200937 TEEC_UUID uuid = sims_test_ta_uuid;
938
939 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
940 {
941 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
942 static const uint8_t in[] = {
943 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
944 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
945 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
946 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
947 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100948 uint8_t out[32] = { };
949 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200950
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300951 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200952 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300953 &ret_orig)))
954 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200955
956 op.params[0].value.a = 0;
957 op.params[1].tmpref.buffer = (void *)in;
958 op.params[1].tmpref.size = sizeof(in);
959 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
960 TEEC_MEMREF_TEMP_INPUT,
961 TEEC_NONE, TEEC_NONE);
962
963 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
964 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
965 &ret_orig));
966
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100967 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300968 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200969 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300970 &ret_orig)))
971 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200972
973 op.params[0].value.a = 0;
974 op.params[1].tmpref.buffer = out;
975 op.params[1].tmpref.size = sizeof(out);
976 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
977 TEEC_MEMREF_TEMP_OUTPUT,
978 TEEC_NONE, TEEC_NONE);
979
980 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
981 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
982 &op, &ret_orig));
983
984 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
985 sizeof(out))) {
986 Do_ADBG_Log("in:");
987 Do_ADBG_HexLog(in, sizeof(in), 16);
988 Do_ADBG_Log("out:");
989 Do_ADBG_HexLog(out, sizeof(out), 16);
990 }
991
992 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
993 TEEC_NONE, TEEC_NONE,
994 TEEC_NONE);
995
996 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
997 TEEC_InvokeCommand(&session1,
998 TA_SIMS_CMD_GET_COUNTER,
999 &op, &ret_orig));
1000
1001 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1002
1003 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1004 TEEC_InvokeCommand(&session2,
1005 TA_SIMS_CMD_GET_COUNTER, &op,
1006 &ret_orig));
1007
1008 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1009 TEEC_CloseSession(&session2);
1010 }
1011
1012 memset(out, 0, sizeof(out));
1013 op.params[0].value.a = 0;
1014 op.params[1].tmpref.buffer = out;
1015 op.params[1].tmpref.size = sizeof(out);
1016 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1017 TEEC_MEMREF_TEMP_OUTPUT,
1018 TEEC_NONE, TEEC_NONE);
1019
1020 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1021 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1022 &ret_orig));
1023
1024 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1025 Do_ADBG_Log("in:");
1026 Do_ADBG_HexLog(in, sizeof(in), 16);
1027 Do_ADBG_Log("out:");
1028 Do_ADBG_HexLog(out, sizeof(out), 16);
1029 }
1030
1031 TEEC_CloseSession(&session1);
1032 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001033 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001034}
Jens Wiklander14f48872018-06-29 15:30:13 +02001035ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1036 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001037
1038struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001039 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001040 uint32_t cmd;
1041 uint32_t repeat;
1042 TEEC_SharedMemory *shm;
1043 uint32_t error_orig;
1044 TEEC_Result res;
1045 uint32_t max_concurrency;
1046 const uint8_t *in;
1047 size_t in_len;
1048 uint8_t *out;
1049 size_t out_len;
1050};
1051
1052static void *test_1013_thread(void *arg)
1053{
1054 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001055 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001056 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1057 uint8_t p2 = TEEC_NONE;
1058 uint8_t p3 = TEEC_NONE;
1059
Jens Wiklander70672972016-04-06 00:01:45 +02001060 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001061 &a->error_orig);
1062 if (a->res != TEEC_SUCCESS)
1063 return NULL;
1064
1065 op.params[0].memref.parent = a->shm;
1066 op.params[0].memref.size = a->shm->size;
1067 op.params[0].memref.offset = 0;
1068 op.params[1].value.a = a->repeat;
1069 op.params[1].value.b = 0;
1070 op.params[2].tmpref.buffer = (void *)a->in;
1071 op.params[2].tmpref.size = a->in_len;
1072 op.params[3].tmpref.buffer = a->out;
1073 op.params[3].tmpref.size = a->out_len;
1074
1075 if (a->in_len)
1076 p2 = TEEC_MEMREF_TEMP_INPUT;
1077 if (a->out_len)
1078 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1079
1080 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1081 TEEC_VALUE_INOUT, p2, p3);
1082
1083 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1084 a->max_concurrency = op.params[1].value.b;
1085 a->out_len = op.params[3].tmpref.size;
1086 TEEC_CloseSession(&session);
1087 return NULL;
1088}
1089
Pascal Brand4fa35582015-12-17 10:59:12 +01001090#define NUM_THREADS 3
1091
Jens Wiklander70672972016-04-06 00:01:45 +02001092static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1093 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001094{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001095 size_t nt = 0;
1096 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001097 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001098 TEEC_SharedMemory shm = { };
1099 size_t max_concurrency = 0;
1100 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1102 static const uint8_t sha256_out[] = {
1103 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1104 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1105 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1106 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1107 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001108 uint8_t out[32] = { };
1109 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001110 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001111
Jens Wiklander70672972016-04-06 00:01:45 +02001112 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001113 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001114
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001115 shm.size = sizeof(struct ta_concurrent_shm);
1116 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1117 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1118 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1119 return;
1120
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001121 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001122 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001123 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001124
1125 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001126 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001127 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001128 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001129 arg[n].shm = &shm;
1130 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1131 test_1013_thread, arg + n)))
1132 nt = n; /* break loop and start cleanup */
1133 }
1134
1135 for (n = 0; n < nt; n++) {
1136 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001137 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1138 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1139 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1140 skip = true;
1141 continue;
1142 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001143 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1144 if (arg[n].max_concurrency > max_concurrency)
1145 max_concurrency = arg[n].max_concurrency;
1146 }
1147
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001148 /*
1149 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001150 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001151 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1152 * best result there).
1153 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001154 if (!skip) {
1155 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1156 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1157 NUM_THREADS);
1158 *mean_concurrency += max_concurrency;
1159 }
Jens Wiklander70672972016-04-06 00:01:45 +02001160 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001161
Jens Wiklander70672972016-04-06 00:01:45 +02001162 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001163 memset(shm.buffer, 0, shm.size);
1164 memset(arg, 0, sizeof(arg));
1165 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001166 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001167
1168 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001169 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001170 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001171 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001172 arg[n].shm = &shm;
1173 arg[n].in = sha256_in;
1174 arg[n].in_len = sizeof(sha256_in);
1175 arg[n].out = out;
1176 arg[n].out_len = sizeof(out);
1177 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1178 test_1013_thread, arg + n)))
1179 nt = n; /* break loop and start cleanup */
1180 }
1181
1182 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001183 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1184 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1185 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1186 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1187 continue;
1188 }
1189 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001190 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1191 arg[n].out, arg[n].out_len);
1192 if (arg[n].max_concurrency > max_concurrency)
1193 max_concurrency = arg[n].max_concurrency;
1194 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001195 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001196 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001197
Pascal Brand4fa35582015-12-17 10:59:12 +01001198 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001199 TEEC_ReleaseSharedMemory(&shm);
1200}
Pascal Brand4fa35582015-12-17 10:59:12 +01001201
1202static void xtest_tee_test_1013(ADBG_Case_t *c)
1203{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001204 int i = 0;
1205 double mean_concurrency = 0;
1206 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001207 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001208
1209 if (level == 0)
1210 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001211
Jens Wiklander70672972016-04-06 00:01:45 +02001212 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001213 mean_concurrency = 0;
1214 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001215 xtest_tee_test_1013_single(c, &concurrency,
1216 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001217 mean_concurrency += concurrency;
1218 }
1219 mean_concurrency /= nb_loops;
1220
1221 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1222 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001223 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001224
Jens Wiklander70672972016-04-06 00:01:45 +02001225 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1226 mean_concurrency = 0;
1227 for (i = 0; i < nb_loops; i++) {
1228 xtest_tee_test_1013_single(c, &concurrency,
1229 &concurrent_large_ta_uuid);
1230 mean_concurrency += concurrency;
1231 }
1232 mean_concurrency /= nb_loops;
1233
1234 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1235 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1236 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1237}
Jens Wiklander14f48872018-06-29 15:30:13 +02001238ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001239 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001240
1241#ifdef CFG_SECURE_DATA_PATH
1242static void xtest_tee_test_1014(ADBG_Case_t *c)
1243{
1244 UNUSED(c);
1245
1246 int size = 17000;
1247 int loop = 10;
1248 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1249 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001250 int test = 0;
1251 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001252
1253 test = TEST_NS_TO_TA;
1254 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001255 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001256 ADBG_EXPECT(c, 0, ret);
1257 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1258
1259 test = TEST_TA_TO_TA;
1260 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001261 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001262 ADBG_EXPECT(c, 0, ret);
1263 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1264
1265 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001266 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001267 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001268 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001269 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001270
1271 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001272 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001273 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001274 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001275 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001276
1277 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
1278 ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
1279 ADBG_EXPECT(c, 0, ret);
1280 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001281}
Jens Wiklander14f48872018-06-29 15:30:13 +02001282ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1283 "Test secure data path against SDP TAs and pTAs");
1284#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001285
Jerome Forissierf4259942022-01-11 14:27:05 +01001286#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001287static void xtest_tee_test_1015(ADBG_Case_t *c)
1288{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001289 TEEC_Result res = TEEC_ERROR_GENERIC;
1290 TEEC_Session session = { };
1291 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001292
Etienne Carriere11093162017-10-26 09:49:04 +02001293 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001294 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1295 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001296 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1297 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001298 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001299 }
1300 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001301
1302 ADBG_EXPECT_TEEC_SUCCESS(c,
1303 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1304 NULL, &ret_orig));
1305 TEEC_CloseSession(&session);
1306}
Jens Wiklander14f48872018-06-29 15:30:13 +02001307ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1308 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001309#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001310
1311static void xtest_tee_test_1016(ADBG_Case_t *c)
1312{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001313 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001314 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001315 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001316
1317 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1318 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1319 &ret_orig)))
1320 return;
1321
1322 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1323 TEEC_NONE);
1324
1325 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1326 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1327 &ret_orig));
1328
1329 TEEC_CloseSession(&session);
1330}
Jens Wiklander14f48872018-06-29 15:30:13 +02001331ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1332 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001333
1334static void xtest_tee_test_1017(ADBG_Case_t *c)
1335{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001336 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001337 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001338 uint32_t ret_orig = 0;
1339 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001340 size_t page_size = 4096;
1341
Jens Wiklander87e81702018-03-20 12:00:00 +08001342 shm.size = 8 * page_size;
1343 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1344 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1345 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1346 return;
1347
1348 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1349 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1350 &ret_orig)))
1351 goto out;
1352
1353 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1354 TEEC_MEMREF_PARTIAL_INPUT,
1355 TEEC_MEMREF_PARTIAL_OUTPUT,
1356 TEEC_MEMREF_PARTIAL_OUTPUT);
1357
1358 /*
1359 * The first two memrefs are supposed to be combined into in
1360 * region and the last two memrefs should have one region each
1361 * when the parameters are mapped for the TA.
1362 */
1363 op.params[0].memref.parent = &shm;
1364 op.params[0].memref.size = page_size;
1365 op.params[0].memref.offset = 0;
1366
1367 op.params[1].memref.parent = &shm;
1368 op.params[1].memref.size = page_size;
1369 op.params[1].memref.offset = page_size;
1370
1371 op.params[2].memref.parent = &shm;
1372 op.params[2].memref.size = page_size;
1373 op.params[2].memref.offset = 4 * page_size;
1374
1375 op.params[3].memref.parent = &shm;
1376 op.params[3].memref.size = 2 * page_size;
1377 op.params[3].memref.offset = 6 * page_size;
1378
1379 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1380 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1381 &ret_orig));
1382
1383 TEEC_CloseSession(&session);
1384out:
1385 TEEC_ReleaseSharedMemory(&shm);
1386}
Jens Wiklander14f48872018-06-29 15:30:13 +02001387ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1388 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001389
Etienne Carriere84382b32018-04-25 18:30:30 +02001390static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1391 TEEC_SharedMemory *shm)
1392{
1393 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1394 TEEC_Result ret = TEEC_ERROR_GENERIC;
1395 uint32_t ret_orig = 0;
1396
1397 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1398 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1399
1400 op.params[0].memref.parent = shm;
1401 op.params[0].memref.size = shm->size / 2;
1402 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1403
1404 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1405 &op, &ret_orig);
1406
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001407 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001408 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1409 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1410 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1411 }
1412}
1413
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001414static void xtest_tee_test_1018(ADBG_Case_t *c)
1415{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001416 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001417 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001418 uint32_t ret_orig = 0;
1419 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001420 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001421 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001422 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001423 uint8_t buffer[6001] = { };
1424
1425 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1426 xtest_teec_open_session(&session,
1427 &os_test_ta_uuid,
1428 NULL,
1429 &ret_orig)))
1430 return;
1431
Joakim Becha1212b62020-04-07 12:06:00 +02001432 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001433
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001434 shm.size = 8 * page_size;
1435 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1436 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001437 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1438 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001439 goto out;
1440
1441 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1442 TEEC_MEMREF_PARTIAL_INPUT,
1443 TEEC_MEMREF_PARTIAL_OUTPUT,
1444 TEEC_MEMREF_PARTIAL_OUTPUT);
1445
1446 /*
1447 * The first two memrefs are supposed to be combined into in
1448 * region and the last two memrefs should have one region each
1449 * when the parameters are mapped for the TA.
1450 */
1451 op.params[0].memref.parent = &shm;
1452 op.params[0].memref.size = page_size;
1453 op.params[0].memref.offset = 0;
1454
1455 op.params[1].memref.parent = &shm;
1456 op.params[1].memref.size = page_size;
1457 op.params[1].memref.offset = page_size;
1458
1459 op.params[2].memref.parent = &shm;
1460 op.params[2].memref.size = page_size;
1461 op.params[2].memref.offset = 4 * page_size;
1462
1463 op.params[3].memref.parent = &shm;
1464 op.params[3].memref.size = 3 * page_size;
1465 op.params[3].memref.offset = 6 * page_size;
1466
Etienne Carriere84382b32018-04-25 18:30:30 +02001467 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1468 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001469
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001470 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001471 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1472 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1473 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1474 }
1475
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001476 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001477 Do_ADBG_EndSubCase(c, NULL);
1478
1479 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1480
1481 memset(&shm, 0, sizeof(shm));
1482 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1483 shm.buffer = buffer;
1484 shm.size = sizeof(buffer);
1485
1486 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1487 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1488 &shm)))
1489 goto out;
1490
1491 invoke_1byte_out_of_bounds(c, &session, &shm);
1492
1493 TEEC_ReleaseSharedMemory(&shm);
1494 Do_ADBG_EndSubCase(c, NULL);
1495
1496 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1497
1498 memset(&shm, 0, sizeof(shm));
1499 shm.size = sizeof(buffer);
1500 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1501 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1502 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1503 &shm)))
1504 goto out;
1505
1506 invoke_1byte_out_of_bounds(c, &session, &shm);
1507
1508 TEEC_ReleaseSharedMemory(&shm);
1509 Do_ADBG_EndSubCase(c, NULL);
1510
1511out:
1512 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001513}
Jens Wiklander14f48872018-06-29 15:30:13 +02001514ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1515 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001516
Jerome Forissier53bde722018-05-31 09:14:54 +02001517static void xtest_tee_test_1019(ADBG_Case_t *c)
1518{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001519 TEEC_Session session = { };
1520 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001521
1522 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1523 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1524 &ret_orig)))
1525 return;
1526
1527 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1528 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1529 &ret_orig));
1530
1531 (void)ADBG_EXPECT_TEEC_RESULT(c,
1532 TEEC_ERROR_TARGET_DEAD,
1533 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1534 NULL, &ret_orig));
1535
1536 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1537
1538 TEEC_CloseSession(&session);
1539}
Jens Wiklander14f48872018-06-29 15:30:13 +02001540ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1541 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001542
1543static void xtest_tee_test_1020(ADBG_Case_t *c)
1544{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001545 TEEC_Result res = TEEC_ERROR_GENERIC;
1546 TEEC_Session session = { };
1547 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001548
1549 /* Pseudo TA is optional: warn and nicely exit if not found */
1550 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1551 &ret_orig);
1552 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1553 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1554 return;
1555 }
1556 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1557
1558 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1559 NULL, &ret_orig);
1560 if (res != TEEC_SUCCESS) {
1561 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1562 ret_orig);
1563 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1564 Do_ADBG_Log(" - 1020 - skip test, feature not "
1565 "implemented");
1566 goto out;
1567 }
1568 /* Error */
1569 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1570 }
1571out:
1572 TEEC_CloseSession(&session);
1573}
1574ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1575 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001576
1577static TEEC_Result open_sec_session(TEEC_Session *session,
1578 const TEEC_UUID *uuid)
1579{
1580 TEEC_Result res = TEEC_ERROR_GENERIC;
1581 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1582 uint32_t ret_orig = 0;
1583
1584 op.params[0].tmpref.buffer = (void *)uuid;
1585 op.params[0].tmpref.size = sizeof(*uuid);
1586 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1587 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1588
1589 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1590 &op, &ret_orig);
1591 if (res != TEEC_SUCCESS)
1592 return TEEC_ERROR_GENERIC;
1593
1594 return res;
1595}
1596
1597static TEEC_Result sims_get_counter(TEEC_Session *session,
1598 uint32_t *counter)
1599{
1600 TEEC_Result res = TEEC_ERROR_GENERIC;
1601 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1602 uint32_t ret_orig = 0;
1603
1604 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1605 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1606
1607 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1608 &op, &ret_orig);
1609 if (res == TEEC_SUCCESS)
1610 *counter = op.params[0].value.a;
1611
1612 return res;
1613}
1614
1615static TEEC_Result trigger_panic(TEEC_Session *session,
1616 const TEEC_UUID *uuid)
1617{
1618 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1619 uint32_t ret_orig = 0;
1620
1621 if (!uuid) {
1622 op.params[0].tmpref.buffer = NULL;
1623 op.params[0].tmpref.size = 0;
1624 } else {
1625 op.params[0].tmpref.buffer = (void *)uuid;
1626 op.params[0].tmpref.size = sizeof(*uuid);
1627 }
1628 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1629 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1630
1631 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1632 &op, &ret_orig);
1633}
1634
1635static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1636 bool multi_instance)
1637{
1638 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1639 uint32_t counter = 0;
1640 uint32_t ret_orig = 0;
1641 uint32_t exp_counter = 0;
1642 TEEC_Session cs[3] = { };
1643
1644 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1645 xtest_teec_open_session(&cs[0], uuid, NULL,
1646 &ret_orig)))
1647 return;
1648
1649 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1650 xtest_teec_open_session(&cs[1], uuid, NULL,
1651 &ret_orig)))
1652 goto bail0;
1653
1654 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1655 goto bail1;
1656
1657 if (!ADBG_EXPECT(c, 0, counter))
1658 goto bail1;
1659
1660 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1661 goto bail1;
1662
1663 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001664 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001665 goto bail1;
1666
1667 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1668 trigger_panic(&cs[1], NULL)))
1669 goto bail1;
1670
1671 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1672 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1673 sims_get_counter(&cs[0], &counter)))
1674 goto bail1;
1675
1676 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1677 sims_get_counter(&cs[1], &counter)))
1678 goto bail1;
1679
1680 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001681 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001682 xtest_teec_open_session(&cs[1], uuid, NULL,
1683 &ret_orig)))
1684 goto bail1;
1685
1686 /* Sanity check of still valid TA context */
1687 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1688 xtest_teec_open_session(&cs[2], uuid, NULL,
1689 &ret_orig)))
1690 goto bail1;
1691
1692 /* Sanity check of still valid TA context */
1693 if (multi_instance) {
1694 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1695 sims_get_counter(&cs[0], &counter)))
1696 goto bail2;
1697
1698 if (!ADBG_EXPECT(c, 0, counter))
1699 goto bail2;
1700 }
1701
1702 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1703 goto bail2;
1704
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001705 exp_counter = multi_instance ? 0 : 1;
1706 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001707 goto bail2;
1708
1709bail2:
1710 TEEC_CloseSession(&cs[2]);
1711bail1:
1712 TEEC_CloseSession(&cs[1]);
1713bail0:
1714 TEEC_CloseSession(&cs[0]);
1715}
1716
1717static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1718 const TEEC_UUID *uuid2)
1719{
1720 uint32_t ret_orig = 0;
1721 uint32_t counter = 0;
1722 TEEC_Session cs[3] = { };
1723
1724 /* Test pre-conditions */
1725 /* 2.1 - CA opens a session toward TA1 */
1726 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1727 xtest_teec_open_session(&cs[0], uuid1, NULL,
1728 &ret_orig)))
1729 return;
1730
1731 /* 2.2 - CA opens a session toward TA2 */
1732 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1733 xtest_teec_open_session(&cs[1], uuid2, NULL,
1734 &ret_orig)))
1735 goto bail0;
1736
1737 /* 2.3 - TA1 opens a session toward TA2 */
1738 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1739 goto bail1;
1740
1741 /* 2.4 - CA invokes TA2 which panics */
1742 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1743 trigger_panic(&cs[1], NULL)))
1744 goto bail1;
1745
1746 /* Expected results */
1747 /* 2.5 - Expect CA->TA1 session is still alive */
1748 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1749 goto bail1;
1750
1751 /* 2.6 - Expect CA->TA2 session is properly released */
1752 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1753 sims_get_counter(&cs[1], &counter)))
1754 goto bail1;
1755
1756bail1:
1757 TEEC_CloseSession(&cs[1]);
1758bail0:
1759 TEEC_CloseSession(&cs[0]);
1760
1761 memset(cs, 0, sizeof(cs));
1762
1763 /* Test pre-conditions */
1764 /* 2.1 - CA opens a session toward TA1 */
1765 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1766 xtest_teec_open_session(&cs[0], uuid1, NULL,
1767 &ret_orig)))
1768 return;
1769
1770 /* 2.2 - CA opens a session toward TA2 */
1771 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1772 xtest_teec_open_session(&cs[1], uuid2, NULL,
1773 &ret_orig)))
1774 goto bail2;
1775
1776 /* 2.3 - TA1 opens a session toward TA2 */
1777 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1778 goto bail3;
1779
1780 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1781 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1782 goto bail3;
1783
1784 /* Expected results */
1785 /* 2.5 - Expect CA->TA1 session is still alive */
1786 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1787 goto bail3;
1788
1789 /* 2.6 - Expect CA->TA2 session is properly released */
1790 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1791 sims_get_counter(&cs[1], &counter)))
1792 goto bail3;
1793
1794bail3:
1795 TEEC_CloseSession(&cs[1]);
1796bail2:
1797 TEEC_CloseSession(&cs[0]);
1798}
1799
1800static void xtest_tee_test_1021(ADBG_Case_t *c)
1801{
1802 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1803 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1804 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1805
1806 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1807 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1808 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1809
1810 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1811 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1812 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1813
1814 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1815 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1816 &sims_keepalive_test_ta_uuid);
1817 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1818}
1819ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1820 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001821
1822static void xtest_tee_test_1022(ADBG_Case_t *c)
1823{
1824 TEEC_Session session = { 0 };
1825 uint32_t ret_orig = 0;
1826
1827 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1828 xtest_teec_open_session(&session, &os_test_ta_uuid,
1829 NULL, &ret_orig)))
1830 return;
1831
1832 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1833 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1834 &ret_orig));
1835
1836 (void)ADBG_EXPECT_TEEC_RESULT(c,
1837 TEEC_ERROR_TARGET_DEAD,
1838 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1839 NULL, &ret_orig));
1840
1841 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1842
1843 TEEC_CloseSession(&session);
1844}
1845ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1846 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001847
1848/*
1849 * Testing the ELF initialization (.init_array)
1850 *
1851 * - The TA has a global variable which can also be accessed by the two shared
1852 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1853 * dlopen())
1854 * - The TA and both libraries have initialization functions (declared with the
1855 * "constructor" attribute) which perform the following:
1856 * * The TA multiplies by 10 then adds 1
1857 * * os_test_lib multiplies by 10 then adds 2
1858 * * os_test_lib_dl multiplies by 10 then adds 3
1859 * By testing the variable value we make sure the initializations occurred in
1860 * the correct order.
1861 */
1862static void xtest_tee_test_1023(ADBG_Case_t *c)
1863{
1864 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1865 TEEC_Session session = { 0 };
1866 uint32_t ret_orig = 0;
1867
1868 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1869 TEEC_NONE, TEEC_NONE);
1870
1871 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1872 xtest_teec_open_session(&session, &os_test_ta_uuid,
1873 NULL, &ret_orig)))
1874 return;
1875
1876 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1877 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1878 &ret_orig));
1879
1880 /* Expected: initialization of os_test_lib, then TA */
1881 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1882
1883 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1884 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1885 &ret_orig));
1886
1887 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1888 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1889 &ret_orig));
1890
1891 /* Expected: initialization of os_test_lib_dl */
1892 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1893
1894 TEEC_CloseSession(&session);
1895}
1896ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1897 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001898
1899#ifdef CFG_CORE_TPM_EVENT_LOG
1900static void xtest_tee_test_1024(ADBG_Case_t *c)
1901{
1902 TEEC_Session session = {};
1903 uint32_t ret_orig = 0;
1904
1905 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1906 NULL, &ret_orig);
1907
1908 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1909 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1910 TA_TPM_TEST_GET_LOG,
1911 NULL, &ret_orig));
1912 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1913
1914 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1915 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1916 TA_TPM_TEST_SHORT_BUF,
1917 NULL, &ret_orig));
1918 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1919
1920 TEEC_CloseSession(&session);
1921}
1922
1923ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1924 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1925#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001926
1927static void xtest_tee_test_1025(ADBG_Case_t *c)
1928{
1929 TEEC_Session session = {};
1930 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1931 uint32_t ret_orig = 0;
1932 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001933 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001934 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001935
1936 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
1937
1938 memset(&shm, 0, sizeof(shm));
1939 shm.flags = TEEC_MEM_INPUT;
1940 shm.buffer = NULL;
1941 shm.size = 0;
1942
1943 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1944 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1945
1946 memset(&shm, 0, sizeof(shm));
1947 shm.flags = TEEC_MEM_OUTPUT;
1948 shm.buffer = NULL;
1949 shm.size = 0;
1950
1951 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
1952 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
1953
1954 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001955
Etienne Carrierec602a522020-04-13 18:53:17 +02001956 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02001957 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02001958 return;
1959 }
1960
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001961 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1962 xtest_teec_open_session(&session,
1963 &os_test_ta_uuid,
1964 NULL, &ret_orig)))
1965 return;
1966
1967 empty_buf = malloc(1);
1968 if (!empty_buf) {
1969 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001970 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001971 }
1972
1973 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1974 TEEC_MEMREF_TEMP_INPUT,
1975 TEEC_MEMREF_TEMP_OUTPUT,
1976 TEEC_MEMREF_TEMP_OUTPUT);
1977
1978 Do_ADBG_BeginSubCase(c,
1979 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1980
1981 op.params[0].tmpref.buffer = empty_buf;
1982 op.params[0].tmpref.size = 0;
1983
1984 op.params[1].tmpref.buffer = NULL;
1985 op.params[1].tmpref.size = 0;
1986
1987 op.params[2].tmpref.buffer = empty_buf;
1988 op.params[2].tmpref.size = 0;
1989
1990 op.params[3].tmpref.buffer = NULL;
1991 op.params[3].tmpref.size = 0;
1992
1993 ADBG_EXPECT(c, TEE_SUCCESS,
1994 TEEC_InvokeCommand(&session,
1995 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
1996 &ret_orig));
1997
1998 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
1999
2000 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2001
2002 op.params[0].tmpref.buffer = empty_buf;
2003 op.params[0].tmpref.size = 1;
2004
2005 op.params[1].tmpref.buffer = NULL;
2006 op.params[1].tmpref.size = 0;
2007
2008 op.params[2].tmpref.buffer = empty_buf;
2009 op.params[2].tmpref.size = 0;
2010
2011 op.params[3].tmpref.buffer = NULL;
2012 op.params[3].tmpref.size = 0;
2013
2014 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2015 TEEC_InvokeCommand(&session,
2016 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2017 &ret_orig));
2018
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002019 TEEC_CloseSession(&session);
2020
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002021 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2022
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002023 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2024
2025 /* Pseudo TA is optional: warn and nicely exit if not found */
2026 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2027 &ret_orig);
2028 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2029 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2030 goto out;
2031 }
2032 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2033 goto out;
2034
2035 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2036 TEEC_NONE, TEEC_NONE);
2037 op.params[0].tmpref.buffer = NULL;
2038 op.params[0].tmpref.size = 0;
2039
2040 ADBG_EXPECT(c, TEE_SUCCESS,
2041 TEEC_InvokeCommand(&session,
2042 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2043 &op, &ret_orig));
2044
2045out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002046 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002047out:
2048 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002049 free(empty_buf);
2050}
2051ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2052 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002053
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002054#define TEE_UUID_NS_NAME_SIZE 128
2055
2056/*
2057 * TEE Client UUID name space identifier (UUIDv4)
2058 *
2059 * Value here is random UUID that is allocated as name space identifier for
2060 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2061 */
2062static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2063
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002064/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2065static TEEC_UUID client_uuid_public = { };
2066
2067static void xtest_tee_test_1026(ADBG_Case_t *c)
2068{
2069 TEEC_Result result = TEEC_ERROR_GENERIC;
2070 uint32_t ret_orig = 0;
2071 TEEC_Session session = { };
2072 uint32_t login = UINT32_MAX;
2073 TEEC_UUID client_uuid = { };
2074
2075 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2076 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2077
2078 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2079 return;
2080
2081 result = ta_os_test_cmd_client_identity(&session, &login,
2082 &client_uuid);
2083
2084 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2085 goto out;
2086
2087 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2088
2089 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2090 sizeof(TEEC_UUID));
2091
2092out:
2093 TEEC_CloseSession(&session);
2094}
2095
2096ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2097 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002098
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002099/*
2100 * regression_1027
2101 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002102 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2103 * login client UUID generation")
2104 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002105 *
2106 * xtest skips the test when not built with OpenSSL.
2107 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002108static void xtest_tee_test_1027(ADBG_Case_t *c)
2109{
Victor Chong8e070bc2020-05-13 09:59:33 +01002110#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002111 TEEC_Result result = TEEC_ERROR_GENERIC;
2112 uint32_t ret_orig = 0;
2113 TEEC_Session session = { };
2114 uint32_t login = UINT32_MAX;
2115 TEEC_UUID client_uuid = { };
2116 TEEC_UUID expected_client_uuid = { };
2117 TEEC_UUID uuid_ns = { };
2118 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2119
2120 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2121
2122 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2123 return;
2124
2125 sprintf(uuid_name, "uid=%x", geteuid());
2126
2127 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2128 strlen(uuid_name));
2129 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2130 return;
2131
2132 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2133 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2134
2135 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2136 return;
2137
2138 result = ta_os_test_cmd_client_identity(&session, &login,
2139 &client_uuid);
2140
2141 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2142 goto out;
2143
2144 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2145
2146 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2147 sizeof(TEEC_UUID));
2148
2149out:
2150 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002151#else /*!OPENSSL_FOUND*/
2152 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002153 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002154 /* xtest_uuid_v5() depends on OpenSSL */
2155 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2156#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002157}
2158
2159ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2160 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002161
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002162/*
2163 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002164 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2165 * login client UUID generation")
2166 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002167 *
2168 * xtest skips the test when not built with OpenSSL.
2169 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002170static void xtest_tee_test_1028(ADBG_Case_t *c)
2171{
Victor Chong8e070bc2020-05-13 09:59:33 +01002172#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002173 TEEC_Result result = TEEC_ERROR_GENERIC;
2174 uint32_t ret_orig = 0;
2175 TEEC_Session session = { };
2176 uint32_t login = UINT32_MAX;
2177 TEEC_UUID client_uuid = { };
2178 TEEC_UUID expected_client_uuid = { };
2179 TEEC_UUID uuid_ns = { };
2180 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2181 uint32_t group = 0;
2182
2183 group = getegid();
2184
2185 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2186
2187 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2188 return;
2189
2190 sprintf(uuid_name, "gid=%x", group);
2191
2192 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2193 strlen(uuid_name));
2194 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2195 return;
2196
2197 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2198 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2199
2200 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2201 return;
2202
2203 result = ta_os_test_cmd_client_identity(&session, &login,
2204 &client_uuid);
2205
2206 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2207 goto out;
2208
2209 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2210
2211 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2212 sizeof(TEEC_UUID));
2213
2214out:
2215 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002216#else /*!OPENSSL_FOUND*/
2217 UNUSED(c);
2218 /* xtest_uuid_v5() depends on OpenSSL */
2219 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2220#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002221}
2222
2223ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2224 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002225
2226static void xtest_tee_test_1029(ADBG_Case_t *c)
2227{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002228 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002229 TEEC_Session session = { 0 };
2230 uint32_t ret_orig = 0;
2231
2232 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2233 xtest_teec_open_session(&session, &os_test_ta_uuid,
2234 NULL, &ret_orig)))
2235 return;
2236
2237 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002238 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2239 &ret_orig);
2240 if (res == TEEC_ERROR_NOT_SUPPORTED)
2241 Do_ADBG_Log(" - 1029 - skip test, "
2242 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2243 else
2244 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002245 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2246
2247 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002248 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2249 &ret_orig);
2250 if (res == TEEC_ERROR_NOT_SUPPORTED)
2251 Do_ADBG_Log(" - 1029 - skip test, "
2252 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2253 else
2254 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002255 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2256
2257 TEEC_CloseSession(&session);
2258}
2259ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2260 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002261
2262static void xtest_tee_test_1030(ADBG_Case_t *c)
2263{
2264 TEEC_Session session = { 0 };
2265 uint32_t ret_orig = 0;
2266
2267 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2268 xtest_teec_open_session(&session, &os_test_ta_uuid,
2269 NULL, &ret_orig)))
2270 return;
2271
2272 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2273 ADBG_EXPECT_TEEC_SUCCESS(c,
2274 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2275 &ret_orig));
2276 Do_ADBG_EndSubCase(c, "Before dlopen()");
2277
2278 Do_ADBG_BeginSubCase(c, "After dlopen()");
2279 ADBG_EXPECT_TEEC_SUCCESS(c,
2280 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2281 &ret_orig));
2282 Do_ADBG_EndSubCase(c, "After dlopen()");
2283
2284 TEEC_CloseSession(&session);
2285}
2286ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2287 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002288
2289#ifndef __clang__
2290static void xtest_tee_test_1031(ADBG_Case_t *c)
2291{
2292 TEEC_Result ret = TEE_SUCCESS;
2293 TEEC_Session session = { 0 };
2294 uint32_t ret_orig = 0;
2295
2296 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2297 xtest_teec_open_session(&session, &os_test_ta_uuid,
2298 NULL, &ret_orig)))
2299 return;
2300
2301 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2302 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2303 &ret_orig);
2304 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2305 printf("TA not built with C++ support, skipping C++ tests\n");
2306 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2307 goto out;
2308
2309 }
2310 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2311 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2312
2313 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2314 ADBG_EXPECT_TEEC_SUCCESS(c,
2315 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2316 NULL, &ret_orig));
2317 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2318
2319 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2320 ADBG_EXPECT_TEEC_SUCCESS(c,
2321 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2322 NULL, &ret_orig));
2323 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2324
2325 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2326 ADBG_EXPECT_TEEC_SUCCESS(c,
2327 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2328 &ret_orig));
2329 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2330
2331 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2332 ADBG_EXPECT_TEEC_SUCCESS(c,
2333 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2334 &ret_orig));
2335 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2336out:
2337 TEEC_CloseSession(&session);
2338}
2339ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2340 "Test C++ features");
2341#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002342
2343static void xtest_tee_test_1032(ADBG_Case_t *c)
2344{
2345 TEEC_Result res = TEEC_SUCCESS;
2346 TEEC_Context ctx = { };
2347 TEEC_SharedMemory shm1 = {
2348 .buffer = xtest_tee_test_1032,
2349 .size = 32,
2350 .flags = TEEC_MEM_INPUT,
2351 };
2352 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2353 TEEC_SharedMemory shm2 = {
2354 .buffer = (void *)dummy_data,
2355 .size = sizeof(dummy_data),
2356 .flags = TEEC_MEM_INPUT,
2357 };
2358
2359 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2360 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2361 return;
2362
2363 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2364 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2365 TEEC_ReleaseSharedMemory(&shm1);
2366
2367 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2368 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2369 TEEC_ReleaseSharedMemory(&shm2);
2370
2371 TEEC_FinalizeContext(&ctx);
2372}
2373ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2374 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002375
2376static void xtest_tee_test_1033(ADBG_Case_t *c)
2377{
2378 TEEC_Session session = { };
2379 uint32_t ret_orig = 0;
2380
2381 /* TA will ping the test plugin during open session operation */
2382 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2383 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2384 NULL, &ret_orig)))
2385 return;
2386
2387 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2388 {
2389 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2390
2391 op.params[0].value.a = 20;
2392 op.params[0].value.b = 10;
2393 op.params[1].value.a = '+';
2394 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2395 TEEC_VALUE_INPUT, TEEC_NONE,
2396 TEEC_NONE);
2397
2398 ADBG_EXPECT_TEEC_SUCCESS(c,
2399 TEEC_InvokeCommand(&session,
2400 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2401 &ret_orig));
2402 ADBG_EXPECT(c, 30, op.params[0].value.a);
2403
2404 /* reassign, because the values was changed during previous op */
2405 op.params[0].value.a = 20;
2406 op.params[0].value.b = 10;
2407 op.params[1].value.a = '-';
2408 ADBG_EXPECT_TEEC_SUCCESS(c,
2409 TEEC_InvokeCommand(&session,
2410 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2411 &ret_orig));
2412 ADBG_EXPECT(c, 10, op.params[0].value.a);
2413 }
2414 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2415
2416 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2417 {
2418 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002419 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002420
2421 op.params[0].tmpref.buffer = to_plugin;
2422 op.params[0].tmpref.size = sizeof(to_plugin);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002423 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002424 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002425 TEEC_NONE, TEEC_NONE);
2426
2427 ADBG_EXPECT_TEEC_SUCCESS(c,
2428 TEEC_InvokeCommand(&session,
2429 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2430 &op, &ret_orig));
2431
2432 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002433 * The test plugin must calculate a sum of the input elements
2434 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002435 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002436 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002437 }
2438 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2439
2440 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2441 {
2442 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2443 char from_plugin[64] = { };
2444 char expected_arr[] = "Array from plugin";
2445 size_t expectes_size = sizeof(expected_arr);
2446
2447 op.params[0].tmpref.buffer = from_plugin;
2448 op.params[0].tmpref.size = sizeof(from_plugin);
2449 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2450 TEEC_VALUE_OUTPUT, TEEC_NONE,
2451 TEEC_NONE);
2452 ADBG_EXPECT_TEEC_SUCCESS(c,
2453 TEEC_InvokeCommand(&session,
2454 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2455 &ret_orig));
2456 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2457 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2458 }
2459 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2460
2461 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2462 {
2463 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2464
2465 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2466 TEEC_NONE, TEEC_NONE);
2467 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2468 TEEC_InvokeCommand(&session,
2469 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2470 &ret_orig));
2471 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2472 TEEC_InvokeCommand(&session,
2473 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2474 &ret_orig));
2475 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2476 TEEC_InvokeCommand(&session,
2477 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2478 &ret_orig));
2479 }
2480 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2481
2482 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2483 {
2484 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2485
2486 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2487 TEEC_NONE, TEEC_NONE);
2488 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2489 TEEC_InvokeCommand(&session,
2490 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2491 &op, &ret_orig));
2492 }
2493 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2494
2495 TEEC_CloseSession(&session);
2496}
2497ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2498 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002499
2500static void xtest_tee_test_1034(ADBG_Case_t *c)
2501{
2502 TEEC_Result res = TEEC_SUCCESS;
2503 TEEC_Session session = { };
2504 uint32_t ret_orig = 0;
2505
2506 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2507 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002508 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2509 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
2510 } else {
2511 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002512 TEEC_CloseSession(&session);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002513 }
Jens Wiklander94fb1582021-05-19 10:14:57 +02002514}
2515ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2516 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302517
2518#if defined(CFG_TA_BTI)
2519struct bti_test {
2520 uint32_t cmd;
2521 uint32_t func;
2522};
2523
2524#define BTI_TEST(caller_func, bti_func) { \
2525 .cmd = caller_func, \
2526 .func = bti_func, \
2527 }
2528
2529static const struct bti_test bti_cases_success[] = {
2530 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2531 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2532 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2533 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2534 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2535 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2536 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2537};
2538
2539static const struct bti_test bti_cases_panic[] = {
2540 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2541 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2542 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2543 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2544 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2545};
2546
2547static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2548{
2549 TEEC_Session session = {};
2550 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2551 uint32_t ret_orig = 0;
2552 TEEC_Result res;
2553
2554 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2555 TEEC_NONE);
2556 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2557 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2558 NULL, &ret_orig)))
2559 return;
2560
2561 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2562 &ret_orig);
2563 if (!res) {
2564 if(op.params[0].value.a)
2565 *bti = true;
2566 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2567 }
2568
2569 TEEC_CloseSession(&session);
2570}
2571
2572static void xtest_tee_test_1035(ADBG_Case_t *c)
2573{
2574 TEEC_Session session = {};
2575 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2576 struct bti_test const *test = NULL;
2577 uint32_t ret_orig = 0;
2578 TEEC_Result res;
2579 unsigned int n = 0;
2580 bool cpu_feature_bti = false;
2581
2582 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2583 get_cpu_feature(c, &cpu_feature_bti);
2584 Do_ADBG_EndSubCase(c, "BTI Implemented");
2585
2586 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2587 TEEC_NONE);
2588
2589 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2590 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2591 NULL, &ret_orig)))
2592 return;
2593
2594 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2595 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2596 test = &bti_cases_success[n];
2597
2598 op.params[0].value.a = test->func;
2599
2600 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2601 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2602 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2603 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2604 TEEC_CloseSession(&session);
2605 return;
2606 }
2607
2608 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2609
2610 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2611
2612 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2613 }
2614 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2615
2616 TEEC_CloseSession(&session);
2617
2618 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2619 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2620 test = &bti_cases_panic[n];
2621 res = TEEC_SUCCESS;
2622
2623 if (cpu_feature_bti)
2624 res = TEEC_ERROR_TARGET_DEAD;
2625
2626 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2627 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2628 NULL, &ret_orig)))
2629 goto out;
2630
2631 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2632 op.params[0].value.a = test->func;
2633
2634 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2635 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2636
2637 if (cpu_feature_bti)
2638 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2639 ret_orig);
2640
2641 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2642
2643 TEEC_CloseSession(&session);
2644 }
2645
2646out:
2647 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2648}
2649ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2650#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302651
2652static void xtest_tee_test_1036(ADBG_Case_t *c)
2653{
2654 TEEC_Session session = { };
2655 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2656 uint32_t ret_orig = 0;
2657 TEEC_Result res = TEEC_SUCCESS;
2658
2659 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2660 TEEC_NONE);
2661
2662 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2663 xtest_teec_open_session(&session, &os_test_ta_uuid,
2664 NULL, &ret_orig)))
2665 return;
2666
2667 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2668
2669 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2670 &ret_orig);
2671 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2672 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2673 goto out;
2674 }
2675
2676 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2677 goto out;
2678
2679 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2680
2681 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2682
2683 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2684 TEEC_InvokeCommand(&session,
2685 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2686 &op, &ret_orig));
2687
2688 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2689
2690 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2691
2692out:
2693 TEEC_CloseSession(&session);
2694}
2695ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2696 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002697
2698#define ATT_MAX_KEYSZ 4096
2699
2700#ifdef OPENSSL_FOUND
2701static RSA *att_key;
2702static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2703
2704/*
2705 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2706 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2707 */
2708static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2709 uint8_t *buf)
2710{
2711 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2712 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2713 unsigned char *ta_hash = buf;
2714 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2715 SHA256_CTX ctx = { };
2716 int salt_len = 32; /* Hard-coded in the PTA */
2717 int st = 0;
2718
2719 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2720 return;
2721
2722 SHA256_Init(&ctx);
2723 SHA256_Update(&ctx, nonce, nonce_size);
2724 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2725 SHA256_Final(digest, &ctx);
2726
2727 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2728 RSA_NO_PADDING);
2729 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2730 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2731 EVP_sha256(), decr, salt_len);
2732 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2733}
2734
2735static void free_att_key(void)
2736{
2737 RSA_free(att_key);
2738 att_key = NULL;
2739}
2740
2741static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2742 size_t n_sz)
2743{
2744 BIGNUM *bn_e = NULL;
2745 BIGNUM *bn_n = NULL;
2746 int st = 0;
2747
2748 att_key_size = n_sz;
2749 att_key = RSA_new();
2750 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2751 return;
2752
2753 bn_e = BN_bin2bn(e, e_sz, NULL);
2754 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2755 goto err;
2756 bn_n = BN_bin2bn(n, n_sz, NULL);
2757 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2758 goto err;
2759
2760 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2761 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2762 goto err;
2763 return;
2764err:
2765 free_att_key();
2766}
2767#else
2768#define check_signature(...)
2769#define set_att_key(...)
2770#define free_att_key()
2771#endif
2772
2773/*
2774 * Verification of the output of the attestation PTA
2775 * - (If hash != NULL) check buf contains the expected hash
2776 * - (If OpenSSL is available) Check that the signature is valid
2777 */
2778static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2779 uint8_t *hash, uint8_t *buf)
2780{
2781 if (hash)
2782 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2783
2784 check_signature(c, nonce, nonce_size, buf);
2785}
2786
2787/* Invoke attestation PTA to return the public key */
2788static void get_att_public_key(ADBG_Case_t *c)
2789{
2790 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2791 uint8_t e[3] = { }; /* We know e == 65537... */
2792 size_t n_sz = sizeof(n);
2793 size_t e_sz = sizeof(e);
2794 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2795 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2796 TEEC_Result res = TEEC_ERROR_GENERIC;
2797 TEEC_Session session = { };
2798 uint32_t ret_orig = 0;
2799
2800 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2801 TEEC_MEMREF_TEMP_OUTPUT,
2802 TEEC_VALUE_OUTPUT, TEEC_NONE);
2803 op.params[0].tmpref.buffer = e;
2804 op.params[0].tmpref.size = e_sz;
2805 op.params[1].tmpref.buffer = n;
2806 op.params[1].tmpref.size = n_sz;
2807
2808 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2809 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2810 return;
2811
2812 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2813 &ret_orig);
2814
2815 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2816 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2817 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2818 goto out;
2819
2820 e_sz = op.params[0].tmpref.size;
2821 n_sz = op.params[1].tmpref.size;
2822 set_att_key(c, e, e_sz, n, n_sz);
2823out:
2824 TEEC_CloseSession(&session);
2825}
2826
2827/* Invoke attestation PTA to hash the TEE binary */
2828static void attestation_tee(ADBG_Case_t *c)
2829{
2830 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2831 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2832 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2833 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2834 TEEC_Result res = TEEC_ERROR_GENERIC;
2835 TEEC_Session session = { };
2836 uint32_t ret_orig = 0;
2837
2838 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2839 TEEC_MEMREF_TEMP_OUTPUT,
2840 TEEC_NONE, TEEC_NONE);
2841 op.params[0].tmpref.buffer = nonce;
2842 op.params[0].tmpref.size = sizeof(nonce);
2843 op.params[1].tmpref.buffer = measurement;
2844 op.params[1].tmpref.size = sizeof(measurement);
2845
2846 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2847 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2848 return;
2849
2850 /* Hash TEE and check signature */
2851 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2852 &ret_orig);
2853 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2854 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2855
2856 TEEC_CloseSession(&session);
2857}
2858
2859/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2860static void attestation_ta_shdr(ADBG_Case_t *c)
2861{
2862 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2863 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2864 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2865 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2866 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2867 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2868 TEEC_Result res = TEEC_ERROR_GENERIC;
2869 TEEC_Session session = { };
2870 uint32_t ret_orig = 0;
2871 int cmp = 0;
2872
2873 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2874 TEEC_MEMREF_TEMP_INPUT,
2875 TEEC_MEMREF_TEMP_OUTPUT,
2876 TEEC_NONE);
2877 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2878 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2879 op.params[1].tmpref.buffer = nonce;
2880 op.params[1].tmpref.size = sizeof(nonce);
2881 op.params[2].tmpref.buffer = measurement;
2882 op.params[2].tmpref.size = sizeof(measurement);
2883
2884 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2885 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2886 return;
2887
2888 /* Hash TA and check signature */
2889 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2890 &op, &ret_orig);
2891 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2892 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2893 /* Save hash */
2894 memcpy(hash1, measurement, 32);
2895
2896 /* Hash TA again */
2897 memset(measurement, 0, sizeof(measurement));
2898 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2899 &op, &ret_orig);
2900 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2901 /* New hash should be identical */
2902 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2903
2904 /* Hash another TA */
2905 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
2906 memset(measurement, 0, sizeof(measurement));
2907 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2908 &op, &ret_orig);
2909 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2910 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2911 memcpy(hash2, measurement, 32);
2912 /* Different binaries should have different hashes */
2913 cmp = memcmp(hash1, hash2, sizeof(hash1));
2914 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
2915
2916 TEEC_CloseSession(&session);
2917}
2918
2919/*
2920 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
2921 * itself.
2922 */
2923static void attestation_ta_memory(ADBG_Case_t *c)
2924{
2925 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2926 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2927 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2928 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2929 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2930 TEEC_Result res = TEEC_ERROR_GENERIC;
2931 TEEC_Session session = { };
2932 uint32_t ret_orig = 0;
2933 int cmp = 0;
2934
2935 Do_ADBG_BeginSubCase(c, "Consecutive calls");
2936
2937 /* Open session to os_test TA */
2938 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
2939 &ret_orig);
2940 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2941 return;
2942
2943 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2944 TEEC_MEMREF_TEMP_OUTPUT,
2945 TEEC_NONE, TEEC_NONE);
2946 op.params[0].tmpref.buffer = nonce;
2947 op.params[0].tmpref.size = sizeof(nonce);
2948 op.params[1].tmpref.buffer = measurement;
2949 op.params[1].tmpref.size = sizeof(measurement);
2950
2951 /* Hash TA */
2952 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2953 &ret_orig);
2954 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2955
2956 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2957 memcpy(hash1, measurement, 32);
2958
2959 /* Hash TA again */
2960 memset(measurement, 0, sizeof(measurement));
2961 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2962 &ret_orig);
2963 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2964
2965 /* New hash should be identical to hash1 */
2966 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2967
2968 Do_ADBG_EndSubCase(c, "Consecutive calls");
2969
2970 /* Close TA session, will cause unload of TA */
2971 TEEC_CloseSession(&session);
2972
2973 Do_ADBG_BeginSubCase(c, "TA reload");
2974
2975 /* Load TA again and open a new session */
2976 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
2977 &ret_orig);
2978 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2979 if (res)
2980 return;
2981
2982 /* Hash TA one more time */
2983 memset(measurement, 0, sizeof(measurement));
2984 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
2985 &ret_orig);
2986 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2987
2988 /* Hash after reload should still be the same */
2989 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2990
2991 Do_ADBG_EndSubCase(c, "TA reload");
2992
2993 Do_ADBG_BeginSubCase(c, "Add shared library");
2994
2995 /*
2996 * Invoke a TA command that causes some additional code to be mapped
2997 * (shared library)
2998 */
2999 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3000 &ret_orig);
3001 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3002
3003 /* Hash TA one last time */
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 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3010 memcpy(hash2, measurement, 32);
3011
3012 /* Different binaries mapped mean different hashes */
3013 cmp = memcmp(hash1, hash2, sizeof(hash1));
3014 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3015
3016 Do_ADBG_EndSubCase(c, "Add shared library");
3017
3018 TEEC_CloseSession(&session);
3019}
3020
3021static void xtest_tee_test_1037(ADBG_Case_t *c)
3022{
3023 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3024 TEEC_Result res = TEEC_ERROR_GENERIC;
3025 TEEC_Session session = { };
3026 uint32_t ret_orig = 0;
3027
3028 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3029 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3030 Do_ADBG_Log(" skip test, pseudo TA not found");
3031 return;
3032 }
3033
3034 Do_ADBG_BeginSubCase(c, "Get public key");
3035 get_att_public_key(c);
3036 Do_ADBG_EndSubCase(c, "Get public key");
3037
3038 Do_ADBG_BeginSubCase(c, "TEE attestation");
3039 attestation_tee(c);
3040 Do_ADBG_EndSubCase(c, "TEE attestation");
3041
3042 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3043 attestation_ta_shdr(c);
3044 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3045
3046 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3047 attestation_ta_memory(c);
3048 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3049
3050 free_att_key();
3051}
3052ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3053 "Remote attestation");