blob: 9981d01a768fddc06ed159de41420b84135dde15 [file] [log] [blame]
Etienne Carriere9b7b70d2020-05-16 10:27:23 +02001// SPDX-License-Identifier: GPL-2.0
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00003 * Copyright (c) 2020, ARM Limited. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02004 * Copyright (c) 2014, STMicroelectronics International N.V.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03007#include <errno.h>
Etienne Carrierea4653552017-01-11 10:04:24 +01008#include <limits.h>
Jerome Forissier0915b222021-10-27 18:20:59 +02009#ifdef OPENSSL_FOUND
10#include <openssl/bn.h>
11#include <openssl/err.h>
12#include <openssl/evp.h>
13#include <openssl/rsa.h>
14#include <openssl/sha.h>
15#endif
16#include <pta_attestation.h>
17#include <pta_invoke_tests.h>
18#include <pta_secstor_ta_mgmt.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010019#include <pthread.h>
Jerome Forissier03aa2792023-02-07 14:22:24 +010020#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier0915b222021-10-27 18:20:59 +020021#include <sdp_basic.h>
Jerome Forissier03aa2792023-02-07 14:22:24 +010022#endif
Jerome Forissier0915b222021-10-27 18:20:59 +020023#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020024#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010025#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020026#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060027#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020028#include <ta_arm_bti.h>
29#include <ta_concurrent.h>
30#include <ta_create_fail_test.h>
31#include <ta_crypt.h>
32#include <ta_miss_test.h>
33#include <ta_os_test.h>
34#include <ta_rpc_test.h>
35#include <ta_sims_keepalive_test.h>
36#include <ta_sims_test.h>
37#include <ta_supp_plugin.h>
38#include <ta_tpm_log_test.h>
39#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010040#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020041#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010042#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020043
Jerome Forissier0915b222021-10-27 18:20:59 +020044#include "xtest_helpers.h"
45#include "xtest_test.h"
46#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030047
Jens Wiklanderec545fb2017-11-24 16:58:07 +010048#ifndef MIN
49#define MIN(a, b) ((a) < (b) ? (a) : (b))
50#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020051
Etienne Carrierec0a61722023-03-07 17:47:42 +010052#define STATS_UUID \
53 { 0xd96a5b40, 0xe2c7, 0xb1af, \
54 { 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
55
56#define STATS_CMD_PAGER_STATS 0
57
58#define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096)
59
Pascal Brandc639ac82015-07-02 08:53:34 +020060struct xtest_crypto_session {
61 ADBG_Case_t *c;
62 TEEC_Session *session;
63 uint32_t cmd_id_sha256;
64 uint32_t cmd_id_aes256ecb_encrypt;
65 uint32_t cmd_id_aes256ecb_decrypt;
66};
67
Etienne Carrierec0a61722023-03-07 17:47:42 +010068static bool optee_pager_with_small_pool(void)
69{
70 TEEC_Result res = TEEC_ERROR_GENERIC;
71 TEEC_UUID uuid = STATS_UUID;
72 TEEC_Context ctx = { };
73 TEEC_Session sess = { };
74 TEEC_Operation op = { };
75 uint32_t eo = 0;
76 bool rc = false;
77
78 res = TEEC_InitializeContext(NULL, &ctx);
79 if (res)
80 return false;
81
82 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
83 NULL, &eo);
84 if (res)
85 goto out_ctx;
86
87 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_VALUE_OUTPUT,
88 TEEC_VALUE_OUTPUT, TEEC_NONE);
89 res = TEEC_InvokeCommand(&sess, STATS_CMD_PAGER_STATS, &op, &eo);
90 if (res)
91 goto out_sess;
92
93 if (op.params[0].value.b &&
94 op.params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD)
95 rc = true;
96
97out_sess:
98 TEEC_CloseSession(&sess);
99out_ctx:
100 TEEC_FinalizeContext(&ctx);
101
102 return rc;
103}
104
Pascal Brandc639ac82015-07-02 08:53:34 +0200105static void xtest_crypto_test(struct xtest_crypto_session *cs)
106{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100107 uint32_t ret_orig = 0;
108 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200109 uint8_t crypt_in[16] = { 22, 17 };
110
111 crypt_in[15] = 60;
112
113 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
114 {
115 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
116
117 op.params[0].tmpref.buffer = crypt_in;
118 op.params[0].tmpref.size = sizeof(crypt_in);
119 op.params[1].tmpref.buffer = crypt_out;
120 op.params[1].tmpref.size = sizeof(crypt_out);
121 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
122 TEEC_MEMREF_TEMP_OUTPUT,
123 TEEC_NONE, TEEC_NONE);
124
125 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
126 TEEC_InvokeCommand(cs->session,
127 cs->
128 cmd_id_aes256ecb_encrypt,
129 &op,
130 &ret_orig));
131 }
132 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
133
134 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
135 {
136 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100137 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200138
139 op.params[0].tmpref.buffer = crypt_out;
140 op.params[0].tmpref.size = sizeof(crypt_out);
141 op.params[1].tmpref.buffer = out;
142 op.params[1].tmpref.size = sizeof(out);
143 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
144 TEEC_MEMREF_TEMP_OUTPUT,
145 TEEC_NONE, TEEC_NONE);
146
147 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
148 TEEC_InvokeCommand(cs->session,
149 cs->
150 cmd_id_aes256ecb_decrypt,
151 &op,
152 &ret_orig));
153
154 if (!ADBG_EXPECT(cs->c, 0,
155 memcmp(crypt_in, out, sizeof(crypt_in)))) {
156 Do_ADBG_Log("crypt_in:");
157 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
158 Do_ADBG_Log("out:");
159 Do_ADBG_HexLog(out, sizeof(out), 16);
160 }
161 }
162 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
163
164 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
165 {
166 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
167 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
168 static const uint8_t sha256_out[] = {
169 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
170 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
171 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
172 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
173 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100174 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200175
176 op.params[0].tmpref.buffer = (void *)sha256_in;
177 op.params[0].tmpref.size = sizeof(sha256_in);
178 op.params[1].tmpref.buffer = out;
179 op.params[1].tmpref.size = sizeof(out);
180 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
181 TEEC_MEMREF_TEMP_OUTPUT,
182 TEEC_NONE, TEEC_NONE);
183
184 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
185 TEEC_InvokeCommand(cs->session,
186 cs->
187 cmd_id_sha256,
188 &op,
189 &ret_orig));
190
191 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
192 sizeof(sha256_out)))) {
193 Do_ADBG_Log("sha256_out:");
194 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
195 Do_ADBG_Log("out:");
196 Do_ADBG_HexLog(out, sizeof(out), 16);
197 }
198 }
199 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
200
Etienne Carrierea3198522017-10-26 09:48:55 +0200201 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200202 {
203 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
204 static const uint8_t in[] = {
205 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
206 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
207 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
208 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
209 };
210 static const uint8_t exp_out[] = {
211 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
212 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
213 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
214 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
215 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100216 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200217
218 op.params[0].tmpref.buffer = (void *)in;
219 op.params[0].tmpref.size = sizeof(in);
220 op.params[1].tmpref.buffer = out;
221 op.params[1].tmpref.size = sizeof(out);
222 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
223 TEEC_MEMREF_TEMP_OUTPUT,
224 TEEC_NONE, TEEC_NONE);
225
226 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
227 TEEC_InvokeCommand(cs->session,
228 cs->
229 cmd_id_aes256ecb_encrypt,
230 &op,
231 &ret_orig));
232
233 if (!ADBG_EXPECT(cs->c, 0,
234 memcmp(exp_out, out, sizeof(exp_out)))) {
235 Do_ADBG_Log("exp_out:");
236 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
237 Do_ADBG_Log("out:");
238 Do_ADBG_HexLog(out, sizeof(out), 16);
239 }
240 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200241 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200242
Etienne Carrierea3198522017-10-26 09:48:55 +0200243 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200244 {
245 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
246 static const uint8_t in[] = {
247 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
248 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
249 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
250 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
251 };
252 static const uint8_t exp_out[] = {
253 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
254 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
255 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
256 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
257 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100258 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200259
260 op.params[0].tmpref.buffer = (void *)in;
261 op.params[0].tmpref.size = sizeof(in);
262 op.params[1].tmpref.buffer = out;
263 op.params[1].tmpref.size = sizeof(out);
264 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
265 TEEC_MEMREF_TEMP_OUTPUT,
266 TEEC_NONE, TEEC_NONE);
267
268 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
269 TEEC_InvokeCommand(cs->session,
270 cs->
271 cmd_id_aes256ecb_decrypt,
272 &op,
273 &ret_orig));
274
275 if (!ADBG_EXPECT(cs->c, 0,
276 memcmp(exp_out, out, sizeof(exp_out)))) {
277 Do_ADBG_Log("exp_out:");
278 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
279 Do_ADBG_Log("out:");
280 Do_ADBG_HexLog(out, sizeof(out), 16);
281 }
282 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200283 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200284}
285
286static void xtest_tee_test_1001(ADBG_Case_t *c)
287{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100288 TEEC_Result res = TEEC_ERROR_GENERIC;
289 TEEC_Session session = { };
290 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200291
Etienne Carriere11093162017-10-26 09:49:04 +0200292 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100293 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100294 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200295 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
296 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100297 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200298 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100299 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
300 return;
301
302 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200303
Jens Wiklandercf16e842016-02-10 09:07:09 +0100304 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100305 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Etienne Carriere3ea96722022-03-11 09:16:40 +0100306
307 Do_ADBG_EndSubCase(c, "Core self tests");
308
309 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
310
311 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
312 &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL,
313 &ret_orig));
314
315 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
316
Jens Wiklandercf16e842016-02-10 09:07:09 +0100317 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200318}
Jens Wiklander14f48872018-06-29 15:30:13 +0200319ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200320
Jens Wiklander1d70a112017-10-16 15:16:39 +0200321static void xtest_tee_test_1002(ADBG_Case_t *c)
322{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100323 TEEC_Result res = TEEC_ERROR_GENERIC;
324 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200325 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100326 uint32_t ret_orig = 0;
327 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200328 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100329 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200330
Etienne Carriere11093162017-10-26 09:49:04 +0200331 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200332 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
333 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200334 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
335 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200336 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200337 }
338 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200339
340 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
341 TEEC_NONE, TEEC_NONE);
342 op.params[0].tmpref.size = sizeof(buf);
343 op.params[0].tmpref.buffer = buf;
344
345 for (n = 0; n < sizeof(buf); n++)
346 buf[n] = n + 1;
347 for (n = 0; n < sizeof(buf); n++)
348 exp_sum += buf[n];
349
350 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
351 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
352 goto out;
353
354 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
355out:
356 TEEC_CloseSession(&session);
357}
Jens Wiklander14f48872018-06-29 15:30:13 +0200358ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200359
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100360struct test_1003_arg {
361 uint32_t test_type;
362 size_t repeat;
363 size_t max_before_lockers;
364 size_t max_during_lockers;
365 size_t before_lockers;
366 size_t during_lockers;
367 TEEC_Result res;
368 uint32_t error_orig;
369};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200370
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100371static void *test_1003_thread(void *arg)
372{
373 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100374 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100375 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100376 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100377
378 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
379 NULL, &a->error_orig);
380 if (a->res != TEEC_SUCCESS)
381 return NULL;
382
383 for (n = 0; n < a->repeat; n++) {
384 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
385
386 op.params[0].value.a = a->test_type;
387 op.params[0].value.b = rounds;
388
389 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
390 TEEC_VALUE_OUTPUT,
391 TEEC_NONE, TEEC_NONE);
392 a->res = TEEC_InvokeCommand(&session,
393 PTA_INVOKE_TESTS_CMD_MUTEX,
394 &op, &a->error_orig);
395 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
396 op.params[1].value.b != 1) {
397 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
398 a->res = TEEC_ERROR_BAD_STATE;
399 a->error_orig = 42;
400 break;
401 }
402
403 if (a->test_type == PTA_MUTEX_TEST_READER) {
404 if (op.params[1].value.a > a->max_before_lockers)
405 a->max_before_lockers = op.params[1].value.a;
406
407 if (op.params[1].value.b > a->max_during_lockers)
408 a->max_during_lockers = op.params[1].value.b;
409
410 a->before_lockers += op.params[1].value.a;
411 a->during_lockers += op.params[1].value.b;
412 }
413 }
414 TEEC_CloseSession(&session);
415
416 return NULL;
417}
418
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100419#define TEST_1003_THREAD_COUNT (3 * 2)
420
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100421static void xtest_tee_test_1003(ADBG_Case_t *c)
422{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100423 TEEC_Result res = TEEC_ERROR_GENERIC;
424 TEEC_Session session = { };
425 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100426 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100427 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100428 size_t max_read_concurrency = 0;
429 size_t max_read_waiters = 0;
430 size_t num_concurrent_read_lockers = 0;
431 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100432 size_t n = 0;
433 size_t nt = TEST_1003_THREAD_COUNT;
434 double mean_read_concurrency = 0;
435 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100436 size_t num_writers = 0;
437 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100438 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100439
440 /* Pseudo TA is optional: warn and nicely exit if not found */
441 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
442 &ret_orig);
443 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
444 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
445 return;
446 }
447 ADBG_EXPECT_TEEC_SUCCESS(c, res);
448 TEEC_CloseSession(&session);
449
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100450 for (n = 0; n < nt; n++) {
451 if (n % 3) {
452 arg[n].test_type = PTA_MUTEX_TEST_READER;
453 num_readers++;
454 } else {
455 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
456 num_writers++;
457 }
458 arg[n].repeat = repeat;
459 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
460 test_1003_thread, arg + n)))
461 nt = n; /* break loop and start cleanup */
462 }
463
464 for (n = 0; n < nt; n++) {
465 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
466 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
467 Do_ADBG_Log("error origin %" PRIu32,
468 arg[n].error_orig);
469 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
470 if (arg[n].max_during_lockers > max_read_concurrency)
471 max_read_concurrency =
472 arg[n].max_during_lockers;
473
474 if (arg[n].max_before_lockers > max_read_waiters)
475 max_read_waiters = arg[n].max_before_lockers;
476
477 num_concurrent_read_lockers += arg[n].during_lockers;
478 num_concurrent_read_waiters += arg[n].before_lockers;
479 }
480 }
481
482 mean_read_concurrency = (double)num_concurrent_read_lockers /
483 (double)(repeat * num_readers);
484 mean_read_waiters = (double)num_concurrent_read_waiters /
485 (double)(repeat * num_readers);
486
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100487 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
488 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100489 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
490 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
491 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
492 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
493}
Jens Wiklander14f48872018-06-29 15:30:13 +0200494ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
495 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200496
Pascal Brandc639ac82015-07-02 08:53:34 +0200497static void xtest_tee_test_1004(ADBG_Case_t *c)
498{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100499 TEEC_Session session = { };
500 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200501 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
502 TA_CRYPT_CMD_AES256ECB_ENC,
503 TA_CRYPT_CMD_AES256ECB_DEC };
504
505 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
506 &session, &crypt_user_ta_uuid,
507 NULL, &ret_orig)))
508 return;
509
510 /* Run the "complete crypto test suite" */
511 xtest_crypto_test(&cs);
512
513 TEEC_CloseSession(&session);
514}
Jens Wiklander14f48872018-06-29 15:30:13 +0200515ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200516
Etienne Carriere92c34422018-02-09 13:11:40 +0100517static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200518{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100519 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200520 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100521 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200522
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300523 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200524 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300525 &ret_orig)))
526 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200527
528 op.params[0].value.a = n;
529 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
530 TEEC_NONE);
531
532 (void)ADBG_EXPECT_TEEC_RESULT(c,
533 TEEC_ERROR_TARGET_DEAD,
534 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
535 &ret_orig));
536
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300537 (void)ADBG_EXPECT_TEEC_RESULT(c,
538 TEEC_ERROR_TARGET_DEAD,
539 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200540 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300541
Pascal Brandc639ac82015-07-02 08:53:34 +0200542 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
543
544 TEEC_CloseSession(&session);
545}
546
Etienne Carriere92c34422018-02-09 13:11:40 +0100547static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
548 size_t size)
549{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100550 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100551 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100552 uint32_t ret_orig = 0;
553 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100554
Etienne Carriere92c34422018-02-09 13:11:40 +0100555 shm.size = size;
556 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
557 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
558 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
559 return;
560
561 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
562 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
563 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200564 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100565
566 op.params[0].value.a = (uint32_t)n;
567 op.params[1].memref.parent = &shm;
568 op.params[1].memref.size = size;
569 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
570 TEEC_NONE, TEEC_NONE);
571
572 (void)ADBG_EXPECT_TEEC_RESULT(c,
573 TEEC_ERROR_TARGET_DEAD,
574 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
575 &ret_orig));
576
577 (void)ADBG_EXPECT_TEEC_RESULT(c,
578 TEEC_ERROR_TARGET_DEAD,
579 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
580 &ret_orig));
581
582 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
583
584 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200585rel_shm:
586 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100587}
588
Pascal Brandc639ac82015-07-02 08:53:34 +0200589static void xtest_tee_test_1005(ADBG_Case_t *c)
590{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100591 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200592#define MAX_SESSIONS 3
593 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100594 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200595
596 for (i = 0; i < MAX_SESSIONS; i++) {
597 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200598 xtest_teec_open_session(&sessions[i],
599 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200600 NULL, &ret_orig)))
601 break;
602 }
603
604 for (; --i >= 0; )
605 TEEC_CloseSession(&sessions[i]);
606}
Jens Wiklander14f48872018-06-29 15:30:13 +0200607ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200608
609static void xtest_tee_test_1006(ADBG_Case_t *c)
610{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100611 TEEC_Session session = { };
612 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200613 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100614 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200615
616 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
617 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
618 &ret_orig)))
619 return;
620
621 op.params[0].tmpref.buffer = buf;
622 op.params[0].tmpref.size = sizeof(buf);
623 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
624 TEEC_NONE, TEEC_NONE);
625
626 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
627 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
628 &ret_orig));
629
630 TEEC_CloseSession(&session);
631}
Jens Wiklander14f48872018-06-29 15:30:13 +0200632ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
633 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200634
635static void xtest_tee_test_1007(ADBG_Case_t *c)
636{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100637 TEEC_Session session = { };
638 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200639
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300640 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200641 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300642 &ret_orig)))
643 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200644
645 (void)ADBG_EXPECT_TEEC_RESULT(c,
646 TEEC_ERROR_TARGET_DEAD,
647 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
648 &ret_orig));
649
650 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
651
652 (void)ADBG_EXPECT_TEEC_RESULT(c,
653 TEEC_ERROR_TARGET_DEAD,
654 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
655 &ret_orig));
656
657 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
658
659 TEEC_CloseSession(&session);
660}
Jens Wiklander14f48872018-06-29 15:30:13 +0200661ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200662
Jerome Forissierf02a2212015-10-29 14:33:35 +0100663#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000664# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800665#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000666# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100667#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000668# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100669#endif
670
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100671static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600672{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100673 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600674
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100675 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100676 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100677 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200678 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
679 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
680 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600681 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200682
Jens Wiklanderb7940892015-10-23 16:02:40 +0200683 return fopen(buf, mode);
684}
685
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100686static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200687{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100688 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100689 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
690 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100691 TEEC_Result res = TEEC_ERROR_GENERIC;
692 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100693 FILE *f = NULL;
694 bool r = false;
695 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100696 size_t sz = 0;
697 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200698
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100699 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
700 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
701 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200702
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100703 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
704 if (!ADBG_EXPECT_NOT_NULL(c, f))
705 goto out;
706 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
707 goto out;
708 sz = ftell(f);
709 rewind(f);
710
711 buf = malloc(sz);
712 if (!ADBG_EXPECT_NOT_NULL(c, buf))
713 goto out;
714
715 fread_res = fread(buf, 1, sz, f);
716 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
717 goto out;
718
Jens Wiklander4441fe22015-10-23 16:53:02 +0200719 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100720 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200721
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100722 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200723
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100724 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
725 TEEC_NONE, TEEC_NONE);
726 op.params[0].tmpref.buffer = buf;
727 op.params[0].tmpref.size = sz;
728
729 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
730 &ret_orig);
731 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
732out:
733 free(buf);
734 if (f)
735 fclose(f);
736 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200737 return r;
738}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100739
740static void test_1008_corrupt_ta(ADBG_Case_t *c)
741{
742 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
743 TEEC_Result res = TEEC_ERROR_GENERIC;
744 TEEC_Session session = { };
745 uint32_t ret_orig = 0;
746
747 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
748 if (res) {
749 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
750 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200751 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100752 return;
753 }
754 TEEC_CloseSession(&session);
755
756 ADBG_EXPECT_TRUE(c,
757 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
758 ADBG_EXPECT_TRUE(c,
759 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
760 ADBG_EXPECT_TRUE(c,
761 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
762 ADBG_EXPECT_TRUE(c,
763 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
764 ADBG_EXPECT_TRUE(c,
765 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
766 ADBG_EXPECT_TRUE(c,
767 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
768 ADBG_EXPECT_TRUE(c,
769 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
770 ADBG_EXPECT_TRUE(c,
771 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
772 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
773 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
774}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200775
Pascal Brandc639ac82015-07-02 08:53:34 +0200776static void xtest_tee_test_1008(ADBG_Case_t *c)
777{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100778 TEEC_Session session = { };
779 TEEC_Session session_crypt = { };
780 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200781
782 Do_ADBG_BeginSubCase(c, "Invoke command");
783 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300784 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200785 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300786 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200787
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300788 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
789 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
790 NULL, &ret_orig));
791 TEEC_CloseSession(&session);
792 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200793
Pascal Brandc639ac82015-07-02 08:53:34 +0200794 }
795 Do_ADBG_EndSubCase(c, "Invoke command");
796
797 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
798 {
799 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
800
801 op.params[0].value.a = 2000;
802 op.paramTypes = TEEC_PARAM_TYPES(
803 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
804
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300805 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200806 xtest_teec_open_session(&session,
807 &os_test_ta_uuid,
808 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300809 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200810
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300811 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
812 TEEC_InvokeCommand(&session,
813 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
814 &op, &ret_orig));
815 TEEC_CloseSession(&session);
816 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200817 }
818 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
819
820 Do_ADBG_BeginSubCase(c, "Create session fail");
821 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100822 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200823
Pascal Brandc639ac82015-07-02 08:53:34 +0200824 for (n = 0; n < 100; n++) {
825 Do_ADBG_Log("n = %zu", n);
826 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
827 xtest_teec_open_session(&session_crypt,
828 &create_fail_test_ta_uuid,
829 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100830 /* level > 0 may be used to detect/debug memory leaks */
831 if (!level)
832 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200833 }
834 }
835 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200836
Jens Wiklander4441fe22015-10-23 16:53:02 +0200837 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100838 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200839 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200840}
Jens Wiklander14f48872018-06-29 15:30:13 +0200841ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
842 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200843
Pascal Brandc639ac82015-07-02 08:53:34 +0200844static void *cancellation_thread(void *arg)
845{
846 /*
847 * Sleep 0.5 seconds before cancellation to make sure that the other
848 * thread is in RPC_WAIT.
849 */
850 (void)usleep(500000);
851 TEEC_RequestCancellation(arg);
852 return NULL;
853}
Pascal Brandc639ac82015-07-02 08:53:34 +0200854
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300855static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
856 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200857{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100858 TEEC_Session session = { };
859 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300860 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200861
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100862 memset(&thr, 0, sizeof(thr));
863
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300864 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200865 {
866 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
867
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300868 if (ADBG_EXPECT_TEEC_SUCCESS(c,
869 xtest_teec_open_session(&session, &os_test_ta_uuid,
870 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200871
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300872 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
873 TEEC_ORIGIN_TRUSTED_APP,
874 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200875
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300876 op.params[0].value.a = timeout;
877 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
878 TEEC_NONE,
879 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300880 if (cancel) {
881 (void)ADBG_EXPECT(c, 0,
882 pthread_create(&thr, NULL,
883 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200884
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300885 (void)ADBG_EXPECT_TEEC_RESULT(c,
886 TEEC_ERROR_CANCEL,
887 TEEC_InvokeCommand(&session,
888 TA_OS_TEST_CMD_WAIT,
889 &op,
890 &ret_orig));
891 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300892
893 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
894 TEEC_InvokeCommand(&session,
895 TA_OS_TEST_CMD_WAIT,
896 &op,
897 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300898 if (cancel)
899 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300900
901 TEEC_CloseSession(&session);
902 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200903 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300904 Do_ADBG_EndSubCase(c, "%s", subcase);
905}
906
907static void xtest_tee_test_1009(ADBG_Case_t *c)
908{
909 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
910 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300911 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300912 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200913}
Jens Wiklander14f48872018-06-29 15:30:13 +0200914ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200915
916static void xtest_tee_test_1010(ADBG_Case_t *c)
917{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100918 unsigned int n = 0;
919 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100920 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200921
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200922 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200923 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
924 xtest_tee_test_invalid_mem_access(c, n);
925 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
926 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100927
928 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
929 for (n = 1; n <= 5; n++) {
930 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200931 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100932 n, memref_sz[idx]);
933 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
934 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200935 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100936 n, memref_sz[idx]);
937 }
938 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200939}
Jens Wiklander14f48872018-06-29 15:30:13 +0200940ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
941 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200942
943static void xtest_tee_test_1011(ADBG_Case_t *c)
944{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100945 TEEC_Session session = { };
946 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200947 struct xtest_crypto_session cs = {
948 c, &session, TA_RPC_CMD_CRYPT_SHA256,
949 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
950 TA_RPC_CMD_CRYPT_AES256ECB_DEC
951 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100952 struct xtest_crypto_session cs_privmem = {
953 c, &session,
954 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
955 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
956 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
957 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200958 TEEC_UUID uuid = rpc_test_ta_uuid;
959
960 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
961 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
962 return;
963
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100964 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200965 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100966 * Run the "complete crypto test suite" using TA-to-TA
967 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200968 */
969 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100970 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
971
972 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
973 /*
974 * Run the "complete crypto test suite" using TA-to-TA
975 * communication via TA private memory.
976 */
977 xtest_crypto_test(&cs_privmem);
978 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
979
Pascal Brandc639ac82015-07-02 08:53:34 +0200980 TEEC_CloseSession(&session);
981}
Jens Wiklander14f48872018-06-29 15:30:13 +0200982ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
983 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200984
985/*
986 * Note that this test is failing when
987 * - running twice in a raw
988 * - and the user TA is statically linked
989 * This is because the counter is not reseted when opening the first session
990 * in case the TA is statically linked
991 */
992static void xtest_tee_test_1012(ADBG_Case_t *c)
993{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100994 TEEC_Session session1 = { };
995 TEEC_Session session2 = { };
996 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200997 TEEC_UUID uuid = sims_test_ta_uuid;
998
999 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
1000 {
1001 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1002 static const uint8_t in[] = {
1003 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
1004 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
1005 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
1006 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
1007 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001008 uint8_t out[32] = { };
1009 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +02001010
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001011 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001012 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001013 &ret_orig)))
1014 return;
Pascal Brandc639ac82015-07-02 08:53:34 +02001015
1016 op.params[0].value.a = 0;
1017 op.params[1].tmpref.buffer = (void *)in;
1018 op.params[1].tmpref.size = sizeof(in);
1019 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1020 TEEC_MEMREF_TEMP_INPUT,
1021 TEEC_NONE, TEEC_NONE);
1022
1023 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1024 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
1025 &ret_orig));
1026
Jerome Forissier3cc0a422017-12-14 09:53:24 +01001027 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001028 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001029 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001030 &ret_orig)))
1031 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +02001032
1033 op.params[0].value.a = 0;
1034 op.params[1].tmpref.buffer = out;
1035 op.params[1].tmpref.size = sizeof(out);
1036 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1037 TEEC_MEMREF_TEMP_OUTPUT,
1038 TEEC_NONE, TEEC_NONE);
1039
1040 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1041 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1042 &op, &ret_orig));
1043
1044 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1045 sizeof(out))) {
1046 Do_ADBG_Log("in:");
1047 Do_ADBG_HexLog(in, sizeof(in), 16);
1048 Do_ADBG_Log("out:");
1049 Do_ADBG_HexLog(out, sizeof(out), 16);
1050 }
1051
1052 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1053 TEEC_NONE, TEEC_NONE,
1054 TEEC_NONE);
1055
1056 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1057 TEEC_InvokeCommand(&session1,
1058 TA_SIMS_CMD_GET_COUNTER,
1059 &op, &ret_orig));
1060
1061 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1062
1063 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1064 TEEC_InvokeCommand(&session2,
1065 TA_SIMS_CMD_GET_COUNTER, &op,
1066 &ret_orig));
1067
1068 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1069 TEEC_CloseSession(&session2);
1070 }
1071
1072 memset(out, 0, sizeof(out));
1073 op.params[0].value.a = 0;
1074 op.params[1].tmpref.buffer = out;
1075 op.params[1].tmpref.size = sizeof(out);
1076 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1077 TEEC_MEMREF_TEMP_OUTPUT,
1078 TEEC_NONE, TEEC_NONE);
1079
1080 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1081 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1082 &ret_orig));
1083
1084 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1085 Do_ADBG_Log("in:");
1086 Do_ADBG_HexLog(in, sizeof(in), 16);
1087 Do_ADBG_Log("out:");
1088 Do_ADBG_HexLog(out, sizeof(out), 16);
1089 }
1090
1091 TEEC_CloseSession(&session1);
1092 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001093 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001094}
Jens Wiklander14f48872018-06-29 15:30:13 +02001095ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1096 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001097
1098struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001099 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001100 uint32_t cmd;
1101 uint32_t repeat;
1102 TEEC_SharedMemory *shm;
1103 uint32_t error_orig;
1104 TEEC_Result res;
1105 uint32_t max_concurrency;
1106 const uint8_t *in;
1107 size_t in_len;
1108 uint8_t *out;
1109 size_t out_len;
1110};
1111
1112static void *test_1013_thread(void *arg)
1113{
1114 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001115 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001116 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1117 uint8_t p2 = TEEC_NONE;
1118 uint8_t p3 = TEEC_NONE;
1119
Jens Wiklander70672972016-04-06 00:01:45 +02001120 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001121 &a->error_orig);
1122 if (a->res != TEEC_SUCCESS)
1123 return NULL;
1124
1125 op.params[0].memref.parent = a->shm;
1126 op.params[0].memref.size = a->shm->size;
1127 op.params[0].memref.offset = 0;
1128 op.params[1].value.a = a->repeat;
1129 op.params[1].value.b = 0;
1130 op.params[2].tmpref.buffer = (void *)a->in;
1131 op.params[2].tmpref.size = a->in_len;
1132 op.params[3].tmpref.buffer = a->out;
1133 op.params[3].tmpref.size = a->out_len;
1134
1135 if (a->in_len)
1136 p2 = TEEC_MEMREF_TEMP_INPUT;
1137 if (a->out_len)
1138 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1139
1140 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1141 TEEC_VALUE_INOUT, p2, p3);
1142
1143 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1144 a->max_concurrency = op.params[1].value.b;
1145 a->out_len = op.params[3].tmpref.size;
1146 TEEC_CloseSession(&session);
1147 return NULL;
1148}
1149
Pascal Brand4fa35582015-12-17 10:59:12 +01001150#define NUM_THREADS 3
1151
Jens Wiklander70672972016-04-06 00:01:45 +02001152static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1153 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001154{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001155 size_t nt = 0;
1156 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001157 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001158 TEEC_SharedMemory shm = { };
1159 size_t max_concurrency = 0;
1160 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001161 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1162 static const uint8_t sha256_out[] = {
1163 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1164 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1165 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1166 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1167 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001168 uint8_t out[32] = { };
1169 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001170 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001171
Etienne Carrierec0a61722023-03-07 17:47:42 +01001172 /* Decrease number of loops when pager has a small page pool */
1173 if (level == 0 && optee_pager_with_small_pool())
1174 repeat = 250;
1175
Jens Wiklander70672972016-04-06 00:01:45 +02001176 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001177 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001178
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001179 shm.size = sizeof(struct ta_concurrent_shm);
1180 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1181 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1182 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1183 return;
1184
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001185 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001186 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001187 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001188
1189 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001190 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001191 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001192 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001193 arg[n].shm = &shm;
1194 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1195 test_1013_thread, arg + n)))
1196 nt = n; /* break loop and start cleanup */
1197 }
1198
1199 for (n = 0; n < nt; n++) {
1200 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001201 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1202 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1203 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1204 skip = true;
1205 continue;
1206 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001207 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1208 if (arg[n].max_concurrency > max_concurrency)
1209 max_concurrency = arg[n].max_concurrency;
1210 }
1211
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001212 /*
1213 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001214 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001215 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1216 * best result there).
1217 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001218 if (!skip) {
1219 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1220 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1221 NUM_THREADS);
1222 *mean_concurrency += max_concurrency;
1223 }
Jens Wiklander70672972016-04-06 00:01:45 +02001224 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001225
Jens Wiklander70672972016-04-06 00:01:45 +02001226 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001227 memset(shm.buffer, 0, shm.size);
1228 memset(arg, 0, sizeof(arg));
1229 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001230 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001231
1232 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001233 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001234 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001235 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001236 arg[n].shm = &shm;
1237 arg[n].in = sha256_in;
1238 arg[n].in_len = sizeof(sha256_in);
1239 arg[n].out = out;
1240 arg[n].out_len = sizeof(out);
1241 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1242 test_1013_thread, arg + n)))
1243 nt = n; /* break loop and start cleanup */
1244 }
1245
1246 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001247 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1248 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1249 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1250 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1251 continue;
1252 }
1253 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001254 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1255 arg[n].out, arg[n].out_len);
1256 if (arg[n].max_concurrency > max_concurrency)
1257 max_concurrency = arg[n].max_concurrency;
1258 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001259 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001260 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001261
Pascal Brand4fa35582015-12-17 10:59:12 +01001262 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001263 TEEC_ReleaseSharedMemory(&shm);
1264}
Pascal Brand4fa35582015-12-17 10:59:12 +01001265
1266static void xtest_tee_test_1013(ADBG_Case_t *c)
1267{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001268 int i = 0;
1269 double mean_concurrency = 0;
1270 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001271 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001272
1273 if (level == 0)
1274 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001275
Jens Wiklander70672972016-04-06 00:01:45 +02001276 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001277 mean_concurrency = 0;
1278 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001279 xtest_tee_test_1013_single(c, &concurrency,
1280 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001281 mean_concurrency += concurrency;
1282 }
1283 mean_concurrency /= nb_loops;
1284
1285 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1286 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001287 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001288
Jens Wiklander70672972016-04-06 00:01:45 +02001289 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1290 mean_concurrency = 0;
1291 for (i = 0; i < nb_loops; i++) {
1292 xtest_tee_test_1013_single(c, &concurrency,
1293 &concurrent_large_ta_uuid);
1294 mean_concurrency += concurrency;
1295 }
1296 mean_concurrency /= nb_loops;
1297
1298 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1299 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1300 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1301}
Jens Wiklander14f48872018-06-29 15:30:13 +02001302ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001303 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001304
1305#ifdef CFG_SECURE_DATA_PATH
1306static void xtest_tee_test_1014(ADBG_Case_t *c)
1307{
1308 UNUSED(c);
1309
1310 int size = 17000;
1311 int loop = 10;
Olivier Masseda5282a2022-04-07 11:24:08 +02001312 const char *heap_name = DEFAULT_HEAP_NAME;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001313 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001314 int test = 0;
1315 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001316
1317 test = TEST_NS_TO_TA;
1318 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001319 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001320 ADBG_EXPECT(c, 0, ret);
1321 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1322
1323 test = TEST_TA_TO_TA;
1324 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001325 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001326 ADBG_EXPECT(c, 0, ret);
1327 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1328
1329 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001330 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001331 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001332 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001333 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001334
1335 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001336 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001337 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001338 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001339 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001340
1341 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001342 ret = sdp_out_of_bounds_memref_test(size, heap_name, 0);
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001343 ADBG_EXPECT(c, 0, ret);
1344 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001345}
Jens Wiklander14f48872018-06-29 15:30:13 +02001346ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1347 "Test secure data path against SDP TAs and pTAs");
1348#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001349
Jerome Forissierf4259942022-01-11 14:27:05 +01001350#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001351static void xtest_tee_test_1015(ADBG_Case_t *c)
1352{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001353 TEEC_Result res = TEEC_ERROR_GENERIC;
1354 TEEC_Session session = { };
1355 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001356
Etienne Carriere11093162017-10-26 09:49:04 +02001357 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001358 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1359 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001360 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1361 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001362 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001363 }
1364 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001365
1366 ADBG_EXPECT_TEEC_SUCCESS(c,
1367 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1368 NULL, &ret_orig));
1369 TEEC_CloseSession(&session);
1370}
Jens Wiklander14f48872018-06-29 15:30:13 +02001371ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1372 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001373#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001374
1375static void xtest_tee_test_1016(ADBG_Case_t *c)
1376{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001377 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001378 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001379 uint32_t ret_orig = 0;
Vincent Mailholda741cd2023-11-16 20:20:54 +09001380 int dummy = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001381
1382 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1383 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1384 &ret_orig)))
1385 return;
1386
1387 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1388 TEEC_NONE);
1389
1390 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1391 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1392 &ret_orig));
1393
Vincent Mailholda741cd2023-11-16 20:20:54 +09001394 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1395 TEEC_MEMREF_TEMP_INOUT,
1396 TEEC_MEMREF_TEMP_OUTPUT,
1397 TEEC_NONE);
1398
1399 op.params[0].tmpref.buffer = &dummy;
1400 op.params[0].tmpref.size = 0;
1401
1402 op.params[1].tmpref.buffer = &dummy;
1403 op.params[1].tmpref.size = 0;
1404
1405 op.params[2].tmpref.buffer = &dummy;
1406 op.params[2].tmpref.size = 0;
1407
1408 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1409 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF_SIZE0,
1410 &op, &ret_orig));
1411
Jerome Forissiere916b102017-06-07 17:55:52 +02001412 TEEC_CloseSession(&session);
1413}
Jens Wiklander14f48872018-06-29 15:30:13 +02001414ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1415 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001416
1417static void xtest_tee_test_1017(ADBG_Case_t *c)
1418{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001419 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001420 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001421 uint32_t ret_orig = 0;
1422 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001423 size_t page_size = 4096;
1424
Jens Wiklander87e81702018-03-20 12:00:00 +08001425 shm.size = 8 * page_size;
1426 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1427 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1428 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1429 return;
1430
1431 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1432 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1433 &ret_orig)))
1434 goto out;
1435
1436 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1437 TEEC_MEMREF_PARTIAL_INPUT,
1438 TEEC_MEMREF_PARTIAL_OUTPUT,
1439 TEEC_MEMREF_PARTIAL_OUTPUT);
1440
1441 /*
1442 * The first two memrefs are supposed to be combined into in
1443 * region and the last two memrefs should have one region each
1444 * when the parameters are mapped for the TA.
1445 */
1446 op.params[0].memref.parent = &shm;
1447 op.params[0].memref.size = page_size;
1448 op.params[0].memref.offset = 0;
1449
1450 op.params[1].memref.parent = &shm;
1451 op.params[1].memref.size = page_size;
1452 op.params[1].memref.offset = page_size;
1453
1454 op.params[2].memref.parent = &shm;
1455 op.params[2].memref.size = page_size;
1456 op.params[2].memref.offset = 4 * page_size;
1457
1458 op.params[3].memref.parent = &shm;
1459 op.params[3].memref.size = 2 * page_size;
1460 op.params[3].memref.offset = 6 * page_size;
1461
1462 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1463 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1464 &ret_orig));
1465
1466 TEEC_CloseSession(&session);
1467out:
1468 TEEC_ReleaseSharedMemory(&shm);
1469}
Jens Wiklander14f48872018-06-29 15:30:13 +02001470ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1471 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001472
Etienne Carriere84382b32018-04-25 18:30:30 +02001473static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1474 TEEC_SharedMemory *shm)
1475{
1476 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1477 TEEC_Result ret = TEEC_ERROR_GENERIC;
1478 uint32_t ret_orig = 0;
1479
1480 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1481 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1482
1483 op.params[0].memref.parent = shm;
1484 op.params[0].memref.size = shm->size / 2;
1485 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1486
1487 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1488 &op, &ret_orig);
1489
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001490 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001491 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1492 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1493 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1494 }
1495}
1496
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001497static void xtest_tee_test_1018(ADBG_Case_t *c)
1498{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001499 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001500 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001501 uint32_t ret_orig = 0;
1502 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001503 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001504 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001505 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001506 uint8_t buffer[6001] = { };
1507
1508 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1509 xtest_teec_open_session(&session,
1510 &os_test_ta_uuid,
1511 NULL,
1512 &ret_orig)))
1513 return;
1514
Joakim Becha1212b62020-04-07 12:06:00 +02001515 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001516
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001517 shm.size = 8 * page_size;
1518 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1519 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001520 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1521 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001522 goto out;
1523
1524 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1525 TEEC_MEMREF_PARTIAL_INPUT,
1526 TEEC_MEMREF_PARTIAL_OUTPUT,
1527 TEEC_MEMREF_PARTIAL_OUTPUT);
1528
1529 /*
1530 * The first two memrefs are supposed to be combined into in
1531 * region and the last two memrefs should have one region each
1532 * when the parameters are mapped for the TA.
1533 */
1534 op.params[0].memref.parent = &shm;
1535 op.params[0].memref.size = page_size;
1536 op.params[0].memref.offset = 0;
1537
1538 op.params[1].memref.parent = &shm;
1539 op.params[1].memref.size = page_size;
1540 op.params[1].memref.offset = page_size;
1541
1542 op.params[2].memref.parent = &shm;
1543 op.params[2].memref.size = page_size;
1544 op.params[2].memref.offset = 4 * page_size;
1545
1546 op.params[3].memref.parent = &shm;
1547 op.params[3].memref.size = 3 * page_size;
1548 op.params[3].memref.offset = 6 * page_size;
1549
Etienne Carriere84382b32018-04-25 18:30:30 +02001550 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1551 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001552
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001553 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001554 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1555 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1556 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1557 }
1558
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001559 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001560 Do_ADBG_EndSubCase(c, NULL);
1561
1562 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1563
1564 memset(&shm, 0, sizeof(shm));
1565 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1566 shm.buffer = buffer;
1567 shm.size = sizeof(buffer);
1568
1569 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1570 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1571 &shm)))
1572 goto out;
1573
1574 invoke_1byte_out_of_bounds(c, &session, &shm);
1575
1576 TEEC_ReleaseSharedMemory(&shm);
1577 Do_ADBG_EndSubCase(c, NULL);
1578
1579 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1580
1581 memset(&shm, 0, sizeof(shm));
1582 shm.size = sizeof(buffer);
1583 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1584 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1585 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1586 &shm)))
1587 goto out;
1588
1589 invoke_1byte_out_of_bounds(c, &session, &shm);
1590
1591 TEEC_ReleaseSharedMemory(&shm);
1592 Do_ADBG_EndSubCase(c, NULL);
1593
1594out:
1595 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001596}
Jens Wiklander14f48872018-06-29 15:30:13 +02001597ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1598 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001599
Jerome Forissier53bde722018-05-31 09:14:54 +02001600static void xtest_tee_test_1019(ADBG_Case_t *c)
1601{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001602 TEEC_Session session = { };
1603 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001604
1605 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1606 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1607 &ret_orig)))
1608 return;
1609
1610 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1611 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1612 &ret_orig));
1613
1614 (void)ADBG_EXPECT_TEEC_RESULT(c,
1615 TEEC_ERROR_TARGET_DEAD,
1616 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1617 NULL, &ret_orig));
1618
1619 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1620
1621 TEEC_CloseSession(&session);
1622}
Jens Wiklander14f48872018-06-29 15:30:13 +02001623ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1624 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001625
1626static void xtest_tee_test_1020(ADBG_Case_t *c)
1627{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001628 TEEC_Result res = TEEC_ERROR_GENERIC;
1629 TEEC_Session session = { };
1630 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001631
1632 /* Pseudo TA is optional: warn and nicely exit if not found */
1633 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1634 &ret_orig);
1635 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1636 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1637 return;
1638 }
1639 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1640
1641 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1642 NULL, &ret_orig);
1643 if (res != TEEC_SUCCESS) {
1644 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1645 ret_orig);
1646 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1647 Do_ADBG_Log(" - 1020 - skip test, feature not "
1648 "implemented");
1649 goto out;
1650 }
1651 /* Error */
1652 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1653 }
1654out:
1655 TEEC_CloseSession(&session);
1656}
1657ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1658 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001659
1660static TEEC_Result open_sec_session(TEEC_Session *session,
1661 const TEEC_UUID *uuid)
1662{
1663 TEEC_Result res = TEEC_ERROR_GENERIC;
1664 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1665 uint32_t ret_orig = 0;
1666
1667 op.params[0].tmpref.buffer = (void *)uuid;
1668 op.params[0].tmpref.size = sizeof(*uuid);
1669 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1670 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1671
1672 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1673 &op, &ret_orig);
1674 if (res != TEEC_SUCCESS)
1675 return TEEC_ERROR_GENERIC;
1676
1677 return res;
1678}
1679
1680static TEEC_Result sims_get_counter(TEEC_Session *session,
1681 uint32_t *counter)
1682{
1683 TEEC_Result res = TEEC_ERROR_GENERIC;
1684 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1685 uint32_t ret_orig = 0;
1686
1687 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1688 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1689
1690 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1691 &op, &ret_orig);
1692 if (res == TEEC_SUCCESS)
1693 *counter = op.params[0].value.a;
1694
1695 return res;
1696}
1697
1698static TEEC_Result trigger_panic(TEEC_Session *session,
1699 const TEEC_UUID *uuid)
1700{
1701 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1702 uint32_t ret_orig = 0;
1703
1704 if (!uuid) {
1705 op.params[0].tmpref.buffer = NULL;
1706 op.params[0].tmpref.size = 0;
1707 } else {
1708 op.params[0].tmpref.buffer = (void *)uuid;
1709 op.params[0].tmpref.size = sizeof(*uuid);
1710 }
1711 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1712 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1713
1714 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1715 &op, &ret_orig);
1716}
1717
1718static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1719 bool multi_instance)
1720{
1721 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1722 uint32_t counter = 0;
1723 uint32_t ret_orig = 0;
1724 uint32_t exp_counter = 0;
1725 TEEC_Session cs[3] = { };
1726
1727 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1728 xtest_teec_open_session(&cs[0], uuid, NULL,
1729 &ret_orig)))
1730 return;
1731
1732 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1733 xtest_teec_open_session(&cs[1], uuid, NULL,
1734 &ret_orig)))
1735 goto bail0;
1736
1737 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1738 goto bail1;
1739
1740 if (!ADBG_EXPECT(c, 0, counter))
1741 goto bail1;
1742
1743 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1744 goto bail1;
1745
1746 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001747 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001748 goto bail1;
1749
1750 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1751 trigger_panic(&cs[1], NULL)))
1752 goto bail1;
1753
1754 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1755 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1756 sims_get_counter(&cs[0], &counter)))
1757 goto bail1;
1758
1759 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1760 sims_get_counter(&cs[1], &counter)))
1761 goto bail1;
1762
1763 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001764 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001765 xtest_teec_open_session(&cs[1], uuid, NULL,
1766 &ret_orig)))
1767 goto bail1;
1768
1769 /* Sanity check of still valid TA context */
1770 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1771 xtest_teec_open_session(&cs[2], uuid, NULL,
1772 &ret_orig)))
1773 goto bail1;
1774
1775 /* Sanity check of still valid TA context */
1776 if (multi_instance) {
1777 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1778 sims_get_counter(&cs[0], &counter)))
1779 goto bail2;
1780
1781 if (!ADBG_EXPECT(c, 0, counter))
1782 goto bail2;
1783 }
1784
1785 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1786 goto bail2;
1787
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001788 exp_counter = multi_instance ? 0 : 1;
1789 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001790 goto bail2;
1791
1792bail2:
1793 TEEC_CloseSession(&cs[2]);
1794bail1:
1795 TEEC_CloseSession(&cs[1]);
1796bail0:
1797 TEEC_CloseSession(&cs[0]);
1798}
1799
1800static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1801 const TEEC_UUID *uuid2)
1802{
1803 uint32_t ret_orig = 0;
1804 uint32_t counter = 0;
1805 TEEC_Session cs[3] = { };
1806
1807 /* Test pre-conditions */
1808 /* 2.1 - CA opens a session toward TA1 */
1809 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1810 xtest_teec_open_session(&cs[0], uuid1, NULL,
1811 &ret_orig)))
1812 return;
1813
1814 /* 2.2 - CA opens a session toward TA2 */
1815 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1816 xtest_teec_open_session(&cs[1], uuid2, NULL,
1817 &ret_orig)))
1818 goto bail0;
1819
1820 /* 2.3 - TA1 opens a session toward TA2 */
1821 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1822 goto bail1;
1823
1824 /* 2.4 - CA invokes TA2 which panics */
1825 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1826 trigger_panic(&cs[1], NULL)))
1827 goto bail1;
1828
1829 /* Expected results */
1830 /* 2.5 - Expect CA->TA1 session is still alive */
1831 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1832 goto bail1;
1833
1834 /* 2.6 - Expect CA->TA2 session is properly released */
1835 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1836 sims_get_counter(&cs[1], &counter)))
1837 goto bail1;
1838
1839bail1:
1840 TEEC_CloseSession(&cs[1]);
1841bail0:
1842 TEEC_CloseSession(&cs[0]);
1843
1844 memset(cs, 0, sizeof(cs));
1845
1846 /* Test pre-conditions */
1847 /* 2.1 - CA opens a session toward TA1 */
1848 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1849 xtest_teec_open_session(&cs[0], uuid1, NULL,
1850 &ret_orig)))
1851 return;
1852
1853 /* 2.2 - CA opens a session toward TA2 */
1854 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1855 xtest_teec_open_session(&cs[1], uuid2, NULL,
1856 &ret_orig)))
1857 goto bail2;
1858
1859 /* 2.3 - TA1 opens a session toward TA2 */
1860 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1861 goto bail3;
1862
1863 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1864 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1865 goto bail3;
1866
1867 /* Expected results */
1868 /* 2.5 - Expect CA->TA1 session is still alive */
1869 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1870 goto bail3;
1871
1872 /* 2.6 - Expect CA->TA2 session is properly released */
1873 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1874 sims_get_counter(&cs[1], &counter)))
1875 goto bail3;
1876
1877bail3:
1878 TEEC_CloseSession(&cs[1]);
1879bail2:
1880 TEEC_CloseSession(&cs[0]);
1881}
1882
1883static void xtest_tee_test_1021(ADBG_Case_t *c)
1884{
1885 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1886 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1887 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1888
1889 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1890 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1891 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1892
1893 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1894 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1895 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1896
1897 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1898 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1899 &sims_keepalive_test_ta_uuid);
1900 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1901}
1902ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1903 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001904
1905static void xtest_tee_test_1022(ADBG_Case_t *c)
1906{
1907 TEEC_Session session = { 0 };
1908 uint32_t ret_orig = 0;
1909
1910 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1911 xtest_teec_open_session(&session, &os_test_ta_uuid,
1912 NULL, &ret_orig)))
1913 return;
1914
1915 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1916 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1917 &ret_orig));
1918
1919 (void)ADBG_EXPECT_TEEC_RESULT(c,
1920 TEEC_ERROR_TARGET_DEAD,
1921 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1922 NULL, &ret_orig));
1923
1924 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1925
1926 TEEC_CloseSession(&session);
1927}
1928ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1929 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001930
1931/*
1932 * Testing the ELF initialization (.init_array)
1933 *
1934 * - The TA has a global variable which can also be accessed by the two shared
1935 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1936 * dlopen())
1937 * - The TA and both libraries have initialization functions (declared with the
1938 * "constructor" attribute) which perform the following:
1939 * * The TA multiplies by 10 then adds 1
1940 * * os_test_lib multiplies by 10 then adds 2
1941 * * os_test_lib_dl multiplies by 10 then adds 3
1942 * By testing the variable value we make sure the initializations occurred in
1943 * the correct order.
1944 */
1945static void xtest_tee_test_1023(ADBG_Case_t *c)
1946{
1947 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1948 TEEC_Session session = { 0 };
1949 uint32_t ret_orig = 0;
1950
1951 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1952 TEEC_NONE, TEEC_NONE);
1953
1954 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1955 xtest_teec_open_session(&session, &os_test_ta_uuid,
1956 NULL, &ret_orig)))
1957 return;
1958
1959 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1960 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1961 &ret_orig));
1962
1963 /* Expected: initialization of os_test_lib, then TA */
1964 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1965
1966 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1967 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1968 &ret_orig));
1969
1970 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1971 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1972 &ret_orig));
1973
1974 /* Expected: initialization of os_test_lib_dl */
1975 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1976
1977 TEEC_CloseSession(&session);
1978}
1979ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1980 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001981
1982#ifdef CFG_CORE_TPM_EVENT_LOG
1983static void xtest_tee_test_1024(ADBG_Case_t *c)
1984{
1985 TEEC_Session session = {};
1986 uint32_t ret_orig = 0;
1987
1988 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1989 NULL, &ret_orig);
1990
1991 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1992 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1993 TA_TPM_TEST_GET_LOG,
1994 NULL, &ret_orig));
1995 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1996
1997 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1998 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1999 TA_TPM_TEST_SHORT_BUF,
2000 NULL, &ret_orig));
2001 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
2002
2003 TEEC_CloseSession(&session);
2004}
2005
2006ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
2007 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
2008#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002009
2010static void xtest_tee_test_1025(ADBG_Case_t *c)
2011{
2012 TEEC_Session session = {};
2013 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2014 uint32_t ret_orig = 0;
2015 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02002016 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002017 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02002018
2019 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
2020
2021 memset(&shm, 0, sizeof(shm));
2022 shm.flags = TEEC_MEM_INPUT;
2023 shm.buffer = NULL;
2024 shm.size = 0;
2025
2026 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2027 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2028
2029 memset(&shm, 0, sizeof(shm));
2030 shm.flags = TEEC_MEM_OUTPUT;
2031 shm.buffer = NULL;
2032 shm.size = 0;
2033
2034 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2035 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2036
2037 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002038
Julianus Larsona79e6ee2023-05-17 11:35:03 +02002039 if (!xtest_teec_ctx.imp.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02002040 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02002041 return;
2042 }
2043
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002044 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2045 xtest_teec_open_session(&session,
2046 &os_test_ta_uuid,
2047 NULL, &ret_orig)))
2048 return;
2049
2050 empty_buf = malloc(1);
2051 if (!empty_buf) {
2052 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002053 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002054 }
2055
2056 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2057 TEEC_MEMREF_TEMP_INPUT,
2058 TEEC_MEMREF_TEMP_OUTPUT,
2059 TEEC_MEMREF_TEMP_OUTPUT);
2060
2061 Do_ADBG_BeginSubCase(c,
2062 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2063
2064 op.params[0].tmpref.buffer = empty_buf;
2065 op.params[0].tmpref.size = 0;
2066
2067 op.params[1].tmpref.buffer = NULL;
2068 op.params[1].tmpref.size = 0;
2069
2070 op.params[2].tmpref.buffer = empty_buf;
2071 op.params[2].tmpref.size = 0;
2072
2073 op.params[3].tmpref.buffer = NULL;
2074 op.params[3].tmpref.size = 0;
2075
2076 ADBG_EXPECT(c, TEE_SUCCESS,
2077 TEEC_InvokeCommand(&session,
2078 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2079 &ret_orig));
2080
2081 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2082
2083 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2084
2085 op.params[0].tmpref.buffer = empty_buf;
2086 op.params[0].tmpref.size = 1;
2087
2088 op.params[1].tmpref.buffer = NULL;
2089 op.params[1].tmpref.size = 0;
2090
2091 op.params[2].tmpref.buffer = empty_buf;
2092 op.params[2].tmpref.size = 0;
2093
2094 op.params[3].tmpref.buffer = NULL;
2095 op.params[3].tmpref.size = 0;
2096
2097 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2098 TEEC_InvokeCommand(&session,
2099 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2100 &ret_orig));
2101
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002102 TEEC_CloseSession(&session);
2103
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002104 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2105
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002106 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2107
2108 /* Pseudo TA is optional: warn and nicely exit if not found */
2109 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2110 &ret_orig);
2111 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2112 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2113 goto out;
2114 }
2115 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2116 goto out;
2117
2118 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2119 TEEC_NONE, TEEC_NONE);
2120 op.params[0].tmpref.buffer = NULL;
2121 op.params[0].tmpref.size = 0;
2122
2123 ADBG_EXPECT(c, TEE_SUCCESS,
2124 TEEC_InvokeCommand(&session,
2125 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2126 &op, &ret_orig));
2127
2128out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002129 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002130out:
2131 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002132 free(empty_buf);
2133}
2134ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2135 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002136
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002137#define TEE_UUID_NS_NAME_SIZE 128
2138
2139/*
2140 * TEE Client UUID name space identifier (UUIDv4)
2141 *
2142 * Value here is random UUID that is allocated as name space identifier for
2143 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2144 */
2145static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2146
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002147/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2148static TEEC_UUID client_uuid_public = { };
2149
2150static void xtest_tee_test_1026(ADBG_Case_t *c)
2151{
2152 TEEC_Result result = TEEC_ERROR_GENERIC;
2153 uint32_t ret_orig = 0;
2154 TEEC_Session session = { };
2155 uint32_t login = UINT32_MAX;
2156 TEEC_UUID client_uuid = { };
2157
2158 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2159 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2160
2161 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2162 return;
2163
2164 result = ta_os_test_cmd_client_identity(&session, &login,
2165 &client_uuid);
2166
2167 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2168 goto out;
2169
2170 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2171
2172 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2173 sizeof(TEEC_UUID));
2174
2175out:
2176 TEEC_CloseSession(&session);
2177}
2178
2179ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2180 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002181
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002182/*
2183 * regression_1027
2184 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002185 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2186 * login client UUID generation")
2187 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002188 *
2189 * xtest skips the test when not built with OpenSSL.
2190 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002191static void xtest_tee_test_1027(ADBG_Case_t *c)
2192{
Victor Chong8e070bc2020-05-13 09:59:33 +01002193#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002194 TEEC_Result result = TEEC_ERROR_GENERIC;
2195 uint32_t ret_orig = 0;
2196 TEEC_Session session = { };
2197 uint32_t login = UINT32_MAX;
2198 TEEC_UUID client_uuid = { };
2199 TEEC_UUID expected_client_uuid = { };
2200 TEEC_UUID uuid_ns = { };
2201 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2202
2203 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2204
2205 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2206 return;
2207
2208 sprintf(uuid_name, "uid=%x", geteuid());
2209
2210 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2211 strlen(uuid_name));
2212 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2213 return;
2214
2215 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2216 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2217
2218 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2219 return;
2220
2221 result = ta_os_test_cmd_client_identity(&session, &login,
2222 &client_uuid);
2223
2224 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2225 goto out;
2226
2227 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2228
2229 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2230 sizeof(TEEC_UUID));
2231
2232out:
2233 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002234#else /*!OPENSSL_FOUND*/
2235 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002236 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002237 /* xtest_uuid_v5() depends on OpenSSL */
2238 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2239#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002240}
2241
2242ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2243 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002244
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002245/*
2246 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002247 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2248 * login client UUID generation")
2249 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002250 *
2251 * xtest skips the test when not built with OpenSSL.
2252 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002253static void xtest_tee_test_1028(ADBG_Case_t *c)
2254{
Victor Chong8e070bc2020-05-13 09:59:33 +01002255#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002256 TEEC_Result result = TEEC_ERROR_GENERIC;
2257 uint32_t ret_orig = 0;
2258 TEEC_Session session = { };
2259 uint32_t login = UINT32_MAX;
2260 TEEC_UUID client_uuid = { };
2261 TEEC_UUID expected_client_uuid = { };
2262 TEEC_UUID uuid_ns = { };
2263 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2264 uint32_t group = 0;
2265
2266 group = getegid();
2267
2268 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2269
2270 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2271 return;
2272
2273 sprintf(uuid_name, "gid=%x", group);
2274
2275 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2276 strlen(uuid_name));
2277 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2278 return;
2279
2280 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2281 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2282
2283 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2284 return;
2285
2286 result = ta_os_test_cmd_client_identity(&session, &login,
2287 &client_uuid);
2288
2289 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2290 goto out;
2291
2292 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2293
2294 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2295 sizeof(TEEC_UUID));
2296
2297out:
2298 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002299#else /*!OPENSSL_FOUND*/
2300 UNUSED(c);
2301 /* xtest_uuid_v5() depends on OpenSSL */
2302 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2303#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002304}
2305
2306ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2307 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002308
2309static void xtest_tee_test_1029(ADBG_Case_t *c)
2310{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002311 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002312 TEEC_Session session = { 0 };
2313 uint32_t ret_orig = 0;
2314
2315 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2316 xtest_teec_open_session(&session, &os_test_ta_uuid,
2317 NULL, &ret_orig)))
2318 return;
2319
2320 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002321 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2322 &ret_orig);
2323 if (res == TEEC_ERROR_NOT_SUPPORTED)
2324 Do_ADBG_Log(" - 1029 - skip test, "
2325 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2326 else
2327 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002328 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2329
2330 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002331 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2332 &ret_orig);
2333 if (res == TEEC_ERROR_NOT_SUPPORTED)
2334 Do_ADBG_Log(" - 1029 - skip test, "
2335 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2336 else
2337 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002338 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2339
2340 TEEC_CloseSession(&session);
2341}
2342ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2343 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002344
2345static void xtest_tee_test_1030(ADBG_Case_t *c)
2346{
2347 TEEC_Session session = { 0 };
2348 uint32_t ret_orig = 0;
2349
2350 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2351 xtest_teec_open_session(&session, &os_test_ta_uuid,
2352 NULL, &ret_orig)))
2353 return;
2354
2355 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2356 ADBG_EXPECT_TEEC_SUCCESS(c,
2357 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2358 &ret_orig));
2359 Do_ADBG_EndSubCase(c, "Before dlopen()");
2360
2361 Do_ADBG_BeginSubCase(c, "After dlopen()");
2362 ADBG_EXPECT_TEEC_SUCCESS(c,
2363 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2364 &ret_orig));
2365 Do_ADBG_EndSubCase(c, "After dlopen()");
2366
2367 TEEC_CloseSession(&session);
2368}
2369ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2370 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002371
2372#ifndef __clang__
2373static void xtest_tee_test_1031(ADBG_Case_t *c)
2374{
2375 TEEC_Result ret = TEE_SUCCESS;
2376 TEEC_Session session = { 0 };
2377 uint32_t ret_orig = 0;
2378
2379 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2380 xtest_teec_open_session(&session, &os_test_ta_uuid,
2381 NULL, &ret_orig)))
2382 return;
2383
2384 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2385 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2386 &ret_orig);
2387 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2388 printf("TA not built with C++ support, skipping C++ tests\n");
2389 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2390 goto out;
2391
2392 }
2393 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2394 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2395
2396 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2397 ADBG_EXPECT_TEEC_SUCCESS(c,
2398 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2399 NULL, &ret_orig));
2400 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2401
2402 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2403 ADBG_EXPECT_TEEC_SUCCESS(c,
2404 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2405 NULL, &ret_orig));
2406 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2407
2408 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2409 ADBG_EXPECT_TEEC_SUCCESS(c,
2410 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2411 &ret_orig));
2412 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2413
2414 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2415 ADBG_EXPECT_TEEC_SUCCESS(c,
2416 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2417 &ret_orig));
2418 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2419out:
2420 TEEC_CloseSession(&session);
2421}
2422ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2423 "Test C++ features");
2424#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002425
2426static void xtest_tee_test_1032(ADBG_Case_t *c)
2427{
2428 TEEC_Result res = TEEC_SUCCESS;
2429 TEEC_Context ctx = { };
2430 TEEC_SharedMemory shm1 = {
2431 .buffer = xtest_tee_test_1032,
2432 .size = 32,
2433 .flags = TEEC_MEM_INPUT,
2434 };
2435 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2436 TEEC_SharedMemory shm2 = {
2437 .buffer = (void *)dummy_data,
2438 .size = sizeof(dummy_data),
2439 .flags = TEEC_MEM_INPUT,
2440 };
2441
2442 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2443 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2444 return;
2445
2446 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2447 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2448 TEEC_ReleaseSharedMemory(&shm1);
2449
2450 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2451 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2452 TEEC_ReleaseSharedMemory(&shm2);
2453
2454 TEEC_FinalizeContext(&ctx);
2455}
2456ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2457 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002458
2459static void xtest_tee_test_1033(ADBG_Case_t *c)
2460{
2461 TEEC_Session session = { };
2462 uint32_t ret_orig = 0;
2463
2464 /* TA will ping the test plugin during open session operation */
2465 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2466 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2467 NULL, &ret_orig)))
2468 return;
2469
2470 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2471 {
2472 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2473
2474 op.params[0].value.a = 20;
2475 op.params[0].value.b = 10;
2476 op.params[1].value.a = '+';
2477 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2478 TEEC_VALUE_INPUT, TEEC_NONE,
2479 TEEC_NONE);
2480
2481 ADBG_EXPECT_TEEC_SUCCESS(c,
2482 TEEC_InvokeCommand(&session,
2483 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2484 &ret_orig));
2485 ADBG_EXPECT(c, 30, op.params[0].value.a);
2486
2487 /* reassign, because the values was changed during previous op */
2488 op.params[0].value.a = 20;
2489 op.params[0].value.b = 10;
2490 op.params[1].value.a = '-';
2491 ADBG_EXPECT_TEEC_SUCCESS(c,
2492 TEEC_InvokeCommand(&session,
2493 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2494 &ret_orig));
2495 ADBG_EXPECT(c, 10, op.params[0].value.a);
2496 }
2497 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2498
2499 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2500 {
2501 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002502 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002503
2504 op.params[0].tmpref.buffer = to_plugin;
2505 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002506 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002507 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002508 TEEC_NONE, TEEC_NONE);
2509
2510 ADBG_EXPECT_TEEC_SUCCESS(c,
2511 TEEC_InvokeCommand(&session,
2512 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2513 &op, &ret_orig));
2514
2515 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002516 * The test plugin must calculate a sum of the input elements
2517 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002518 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002519 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002520 }
2521 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2522
2523 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2524 {
2525 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2526 char from_plugin[64] = { };
2527 char expected_arr[] = "Array from plugin";
2528 size_t expectes_size = sizeof(expected_arr);
2529
2530 op.params[0].tmpref.buffer = from_plugin;
2531 op.params[0].tmpref.size = sizeof(from_plugin);
2532 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2533 TEEC_VALUE_OUTPUT, TEEC_NONE,
2534 TEEC_NONE);
2535 ADBG_EXPECT_TEEC_SUCCESS(c,
2536 TEEC_InvokeCommand(&session,
2537 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2538 &ret_orig));
2539 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2540 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2541 }
2542 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2543
2544 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2545 {
2546 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2547
2548 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2549 TEEC_NONE, TEEC_NONE);
2550 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2551 TEEC_InvokeCommand(&session,
2552 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2553 &ret_orig));
2554 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2555 TEEC_InvokeCommand(&session,
2556 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2557 &ret_orig));
2558 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2559 TEEC_InvokeCommand(&session,
2560 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2561 &ret_orig));
2562 }
2563 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2564
2565 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2566 {
2567 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2568
2569 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
Vincent Mailhol2ea0aa72023-11-16 20:20:54 +09002570 TEEC_NONE, TEEC_NONE);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002571 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2572 TEEC_InvokeCommand(&session,
2573 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2574 &op, &ret_orig));
2575 }
2576 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2577
2578 TEEC_CloseSession(&session);
2579}
2580ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2581 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002582
2583static void xtest_tee_test_1034(ADBG_Case_t *c)
2584{
2585 TEEC_Result res = TEEC_SUCCESS;
2586 TEEC_Session session = { };
2587 uint32_t ret_orig = 0;
2588
2589 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2590 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002591 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2592 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
Tim Lee5502a992023-07-12 16:48:47 +08002593 return;
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002594 }
Tim Lee5502a992023-07-12 16:48:47 +08002595 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2596 if (!res)
2597 TEEC_CloseSession(&session);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002598}
2599ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2600 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302601
2602#if defined(CFG_TA_BTI)
2603struct bti_test {
2604 uint32_t cmd;
2605 uint32_t func;
2606};
2607
2608#define BTI_TEST(caller_func, bti_func) { \
2609 .cmd = caller_func, \
2610 .func = bti_func, \
2611 }
2612
2613static const struct bti_test bti_cases_success[] = {
2614 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2615 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2616 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2617 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2618 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2619 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2620 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2621};
2622
2623static const struct bti_test bti_cases_panic[] = {
2624 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2625 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2626 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2627 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2628 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2629};
2630
2631static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2632{
2633 TEEC_Session session = {};
2634 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2635 uint32_t ret_orig = 0;
2636 TEEC_Result res;
2637
2638 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2639 TEEC_NONE);
2640 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2641 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2642 NULL, &ret_orig)))
2643 return;
2644
2645 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2646 &ret_orig);
2647 if (!res) {
2648 if(op.params[0].value.a)
2649 *bti = true;
2650 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2651 }
2652
2653 TEEC_CloseSession(&session);
2654}
2655
2656static void xtest_tee_test_1035(ADBG_Case_t *c)
2657{
2658 TEEC_Session session = {};
2659 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2660 struct bti_test const *test = NULL;
2661 uint32_t ret_orig = 0;
2662 TEEC_Result res;
2663 unsigned int n = 0;
2664 bool cpu_feature_bti = false;
2665
2666 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2667 get_cpu_feature(c, &cpu_feature_bti);
2668 Do_ADBG_EndSubCase(c, "BTI Implemented");
2669
2670 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2671 TEEC_NONE);
2672
2673 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2674 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2675 NULL, &ret_orig)))
2676 return;
2677
2678 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2679 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2680 test = &bti_cases_success[n];
2681
2682 op.params[0].value.a = test->func;
2683
2684 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2685 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2686 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2687 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2688 TEEC_CloseSession(&session);
2689 return;
2690 }
2691
2692 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2693
2694 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2695
2696 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2697 }
2698 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2699
2700 TEEC_CloseSession(&session);
2701
2702 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2703 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2704 test = &bti_cases_panic[n];
2705 res = TEEC_SUCCESS;
2706
2707 if (cpu_feature_bti)
2708 res = TEEC_ERROR_TARGET_DEAD;
2709
2710 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2711 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2712 NULL, &ret_orig)))
2713 goto out;
2714
2715 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2716 op.params[0].value.a = test->func;
2717
2718 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2719 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2720
2721 if (cpu_feature_bti)
2722 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2723 ret_orig);
2724
2725 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2726
2727 TEEC_CloseSession(&session);
2728 }
2729
2730out:
2731 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2732}
2733ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2734#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302735
2736static void xtest_tee_test_1036(ADBG_Case_t *c)
2737{
2738 TEEC_Session session = { };
2739 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2740 uint32_t ret_orig = 0;
2741 TEEC_Result res = TEEC_SUCCESS;
2742
2743 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2744 TEEC_NONE);
2745
2746 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2747 xtest_teec_open_session(&session, &os_test_ta_uuid,
2748 NULL, &ret_orig)))
2749 return;
2750
2751 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2752
2753 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2754 &ret_orig);
2755 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2756 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2757 goto out;
2758 }
2759
2760 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2761 goto out;
2762
2763 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2764
2765 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2766
2767 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2768 TEEC_InvokeCommand(&session,
2769 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2770 &op, &ret_orig));
2771
2772 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2773
2774 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2775
2776out:
2777 TEEC_CloseSession(&session);
2778}
2779ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2780 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002781
2782#define ATT_MAX_KEYSZ 4096
2783
2784#ifdef OPENSSL_FOUND
2785static RSA *att_key;
2786static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2787
2788/*
2789 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2790 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2791 */
2792static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2793 uint8_t *buf)
2794{
2795 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2796 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2797 unsigned char *ta_hash = buf;
2798 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2799 SHA256_CTX ctx = { };
2800 int salt_len = 32; /* Hard-coded in the PTA */
2801 int st = 0;
2802
2803 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2804 return;
2805
2806 SHA256_Init(&ctx);
2807 SHA256_Update(&ctx, nonce, nonce_size);
2808 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2809 SHA256_Final(digest, &ctx);
2810
2811 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2812 RSA_NO_PADDING);
2813 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2814 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2815 EVP_sha256(), decr, salt_len);
2816 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2817}
2818
2819static void free_att_key(void)
2820{
2821 RSA_free(att_key);
2822 att_key = NULL;
2823}
2824
Clement Faure44a31d02022-03-30 10:50:52 +02002825#if OPENSSL_VERSION_NUMBER < 0x10100000L
2826static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2827{
2828 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2829 return 0;
2830
2831 if (n != NULL) {
2832 BN_free(r->n);
2833 r->n = n;
2834 }
2835
2836 if (e != NULL) {
2837 BN_free(r->e);
2838 r->e = e;
2839 }
2840
2841 if (d != NULL) {
2842 BN_free(r->d);
2843 r->d = d;
2844 }
2845
2846 return 1;
2847}
2848#endif
2849
Jerome Forissier0915b222021-10-27 18:20:59 +02002850static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2851 size_t n_sz)
2852{
2853 BIGNUM *bn_e = NULL;
2854 BIGNUM *bn_n = NULL;
2855 int st = 0;
2856
2857 att_key_size = n_sz;
2858 att_key = RSA_new();
2859 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2860 return;
2861
2862 bn_e = BN_bin2bn(e, e_sz, NULL);
2863 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2864 goto err;
2865 bn_n = BN_bin2bn(n, n_sz, NULL);
2866 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2867 goto err;
2868
2869 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2870 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2871 goto err;
2872 return;
2873err:
2874 free_att_key();
2875}
2876#else
2877#define check_signature(...)
2878#define set_att_key(...)
2879#define free_att_key()
2880#endif
2881
2882/*
2883 * Verification of the output of the attestation PTA
2884 * - (If hash != NULL) check buf contains the expected hash
2885 * - (If OpenSSL is available) Check that the signature is valid
2886 */
2887static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2888 uint8_t *hash, uint8_t *buf)
2889{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002890 (void)nonce;
2891 (void)nonce_size;
2892
Jerome Forissier0915b222021-10-27 18:20:59 +02002893 if (hash)
2894 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2895
2896 check_signature(c, nonce, nonce_size, buf);
2897}
2898
2899/* Invoke attestation PTA to return the public key */
2900static void get_att_public_key(ADBG_Case_t *c)
2901{
2902 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2903 uint8_t e[3] = { }; /* We know e == 65537... */
2904 size_t n_sz = sizeof(n);
2905 size_t e_sz = sizeof(e);
2906 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2907 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2908 TEEC_Result res = TEEC_ERROR_GENERIC;
2909 TEEC_Session session = { };
2910 uint32_t ret_orig = 0;
2911
2912 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2913 TEEC_MEMREF_TEMP_OUTPUT,
2914 TEEC_VALUE_OUTPUT, TEEC_NONE);
2915 op.params[0].tmpref.buffer = e;
2916 op.params[0].tmpref.size = e_sz;
2917 op.params[1].tmpref.buffer = n;
2918 op.params[1].tmpref.size = n_sz;
2919
2920 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2921 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2922 return;
2923
2924 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2925 &ret_orig);
2926
2927 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2928 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2929 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2930 goto out;
2931
2932 e_sz = op.params[0].tmpref.size;
2933 n_sz = op.params[1].tmpref.size;
2934 set_att_key(c, e, e_sz, n, n_sz);
2935out:
2936 TEEC_CloseSession(&session);
2937}
2938
2939/* Invoke attestation PTA to hash the TEE binary */
2940static void attestation_tee(ADBG_Case_t *c)
2941{
2942 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2943 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2944 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2945 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2946 TEEC_Result res = TEEC_ERROR_GENERIC;
2947 TEEC_Session session = { };
2948 uint32_t ret_orig = 0;
2949
2950 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2951 TEEC_MEMREF_TEMP_OUTPUT,
2952 TEEC_NONE, TEEC_NONE);
2953 op.params[0].tmpref.buffer = nonce;
2954 op.params[0].tmpref.size = sizeof(nonce);
2955 op.params[1].tmpref.buffer = measurement;
2956 op.params[1].tmpref.size = sizeof(measurement);
2957
2958 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2959 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2960 return;
2961
2962 /* Hash TEE and check signature */
2963 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2964 &ret_orig);
2965 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2966 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2967
2968 TEEC_CloseSession(&session);
2969}
2970
2971/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2972static void attestation_ta_shdr(ADBG_Case_t *c)
2973{
2974 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2975 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2976 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2977 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2978 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2979 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2980 TEEC_Result res = TEEC_ERROR_GENERIC;
2981 TEEC_Session session = { };
2982 uint32_t ret_orig = 0;
2983 int cmp = 0;
2984
2985 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2986 TEEC_MEMREF_TEMP_INPUT,
2987 TEEC_MEMREF_TEMP_OUTPUT,
2988 TEEC_NONE);
2989 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2990 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2991 op.params[1].tmpref.buffer = nonce;
2992 op.params[1].tmpref.size = sizeof(nonce);
2993 op.params[2].tmpref.buffer = measurement;
2994 op.params[2].tmpref.size = sizeof(measurement);
2995
2996 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2997 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2998 return;
2999
3000 /* Hash TA and check signature */
3001 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3002 &op, &ret_orig);
3003 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3004 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3005 /* Save hash */
3006 memcpy(hash1, measurement, 32);
3007
3008 /* Hash TA again */
3009 memset(measurement, 0, sizeof(measurement));
3010 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3011 &op, &ret_orig);
3012 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3013 /* New hash should be identical */
3014 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3015
3016 /* Hash another TA */
3017 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
3018 memset(measurement, 0, sizeof(measurement));
3019 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3020 &op, &ret_orig);
3021 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3022 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3023 memcpy(hash2, measurement, 32);
3024 /* Different binaries should have different hashes */
3025 cmp = memcmp(hash1, hash2, sizeof(hash1));
3026 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3027
3028 TEEC_CloseSession(&session);
3029}
3030
3031/*
3032 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
3033 * itself.
3034 */
3035static void attestation_ta_memory(ADBG_Case_t *c)
3036{
3037 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3038 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
3039 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
3040 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
3041 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
3042 TEEC_Result res = TEEC_ERROR_GENERIC;
3043 TEEC_Session session = { };
3044 uint32_t ret_orig = 0;
3045 int cmp = 0;
3046
3047 Do_ADBG_BeginSubCase(c, "Consecutive calls");
3048
3049 /* Open session to os_test TA */
3050 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3051 &ret_orig);
3052 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3053 return;
3054
3055 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3056 TEEC_MEMREF_TEMP_OUTPUT,
3057 TEEC_NONE, TEEC_NONE);
3058 op.params[0].tmpref.buffer = nonce;
3059 op.params[0].tmpref.size = sizeof(nonce);
3060 op.params[1].tmpref.buffer = measurement;
3061 op.params[1].tmpref.size = sizeof(measurement);
3062
3063 /* Hash TA */
3064 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3065 &ret_orig);
3066 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3067
3068 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3069 memcpy(hash1, measurement, 32);
3070
3071 /* Hash TA again */
3072 memset(measurement, 0, sizeof(measurement));
3073 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3074 &ret_orig);
3075 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3076
3077 /* New hash should be identical to hash1 */
3078 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3079
3080 Do_ADBG_EndSubCase(c, "Consecutive calls");
3081
3082 /* Close TA session, will cause unload of TA */
3083 TEEC_CloseSession(&session);
3084
3085 Do_ADBG_BeginSubCase(c, "TA reload");
3086
3087 /* Load TA again and open a new session */
3088 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3089 &ret_orig);
3090 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3091 if (res)
3092 return;
3093
3094 /* Hash TA one more time */
3095 memset(measurement, 0, sizeof(measurement));
3096 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3097 &ret_orig);
3098 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3099
3100 /* Hash after reload should still be the same */
3101 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3102
3103 Do_ADBG_EndSubCase(c, "TA reload");
3104
3105 Do_ADBG_BeginSubCase(c, "Add shared library");
3106
3107 /*
3108 * Invoke a TA command that causes some additional code to be mapped
3109 * (shared library)
3110 */
3111 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3112 &ret_orig);
3113 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3114
3115 /* Hash TA one last time */
3116 memset(measurement, 0, sizeof(measurement));
3117 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3118 &ret_orig);
3119 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3120
3121 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3122 memcpy(hash2, measurement, 32);
3123
3124 /* Different binaries mapped mean different hashes */
3125 cmp = memcmp(hash1, hash2, sizeof(hash1));
3126 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3127
3128 Do_ADBG_EndSubCase(c, "Add shared library");
3129
3130 TEEC_CloseSession(&session);
3131}
3132
3133static void xtest_tee_test_1037(ADBG_Case_t *c)
3134{
3135 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3136 TEEC_Result res = TEEC_ERROR_GENERIC;
3137 TEEC_Session session = { };
3138 uint32_t ret_orig = 0;
3139
3140 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3141 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3142 Do_ADBG_Log(" skip test, pseudo TA not found");
3143 return;
3144 }
3145
chenchaokaiaf751932023-05-29 14:25:50 +08003146 TEEC_CloseSession(&session);
3147
Jerome Forissier0915b222021-10-27 18:20:59 +02003148 Do_ADBG_BeginSubCase(c, "Get public key");
3149 get_att_public_key(c);
3150 Do_ADBG_EndSubCase(c, "Get public key");
3151
3152 Do_ADBG_BeginSubCase(c, "TEE attestation");
3153 attestation_tee(c);
3154 Do_ADBG_EndSubCase(c, "TEE attestation");
3155
3156 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3157 attestation_ta_shdr(c);
3158 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3159
3160 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3161 attestation_ta_memory(c);
3162 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3163
3164 free_att_key();
3165}
3166ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3167 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003168
3169static void xtest_tee_test_1038(ADBG_Case_t *c)
3170{
3171 TEEC_Session session = { };
3172 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3173 uint32_t ret_orig = 0;
3174 TEEC_Result res = TEEC_SUCCESS;
3175
3176 Do_ADBG_BeginSubCase(c, "MTE use after free");
3177
3178 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3179 TEEC_NONE);
3180
3181 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3182 xtest_teec_open_session(&session, &os_test_ta_uuid,
3183 NULL, &ret_orig)))
3184 return;
3185
3186 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3187 &op, &ret_orig);
3188 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3189 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3190 goto out;
3191 }
3192
3193 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3194 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3195 TEEC_CloseSession(&session);
3196
3197 Do_ADBG_EndSubCase(c, "MTE use after free");
3198
3199 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3200
3201 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3202 TEEC_NONE);
3203
3204 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3205 xtest_teec_open_session(&session, &os_test_ta_uuid,
3206 NULL, &ret_orig)))
3207 return;
3208
3209 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3210 &op, &ret_orig);
3211 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3212 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3213 goto out;
3214 }
3215
3216 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3217 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3218
3219 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3220
3221 Do_ADBG_BeginSubCase(c, "MTE double free");
3222
3223 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3224 TEEC_NONE);
3225
3226 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3227 xtest_teec_open_session(&session, &os_test_ta_uuid,
3228 NULL, &ret_orig)))
3229 return;
3230
3231 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3232 &op, &ret_orig);
3233 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3234 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3235 goto out;
3236 }
3237
3238 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3239 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3240
3241 Do_ADBG_EndSubCase(c, "MTE double free");
3242
3243 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3244
3245 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3246 TEEC_NONE);
3247
3248 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3249 xtest_teec_open_session(&session, &os_test_ta_uuid,
3250 NULL, &ret_orig)))
3251 return;
3252
3253 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3254 &op, &ret_orig);
3255 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3256 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3257 goto out;
3258 }
3259
3260 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3261 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3262
3263 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3264
3265
3266out:
3267 TEEC_CloseSession(&session);
3268}
3269ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3270 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003271
3272static void xtest_tee_test_1039(ADBG_Case_t *c)
3273{
3274 TEEC_Session session = { };
3275 uint32_t ret_orig = 0;
3276
3277 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3278 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3279 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3280 NULL, &ret_orig)))
3281 TEEC_CloseSession(&session);
3282 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3283
3284 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3285 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3286 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3287 NULL, &ret_orig)))
3288 TEEC_CloseSession(&session);
3289 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3290
3291}
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003292ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3293 "Test subkey verification");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003294
3295struct test_1040_thread_arg {
3296 TEEC_Result res;
3297 pthread_t thr;
3298};
3299
3300static void *test_1040_thread(void *arg)
3301{
3302 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3303 struct test_1040_thread_arg *a = arg;
3304 TEEC_Result res = TEEC_SUCCESS;
3305 uint32_t err_orig = 0;
3306 TEEC_Session session = { };
3307 size_t loop_count = 100;
3308 size_t n = 0;
3309
3310 if (level == 0)
3311 loop_count /= 2;
3312
3313 while (n < loop_count) {
3314 res = xtest_teec_open_session(&session, &sims_test_ta_uuid,
3315 NULL, &err_orig);
3316 if (res) {
3317 if (res == TEEC_ERROR_TARGET_DEAD)
3318 continue;
3319 a->res = res;
3320 return NULL;
3321 }
3322
3323 memset(&op, 0, sizeof(op));
3324 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3325 TEEC_NONE, TEEC_NONE,
3326 TEEC_NONE);
3327 res = TEEC_InvokeCommand(&session, TA_SIMS_CMD_PANIC, &op,
3328 &err_orig);
3329 TEEC_CloseSession(&session);
3330 if (res != TEEC_ERROR_TARGET_DEAD) {
3331 if (res)
3332 a->res = res;
3333 else
3334 a->res = TEEC_ERROR_GENERIC;
3335 return NULL;
3336 }
3337 n++;
3338 }
3339 a->res = TEEC_SUCCESS;
3340 return NULL;
3341}
3342
3343static void xtest_tee_test_1040(ADBG_Case_t *c)
3344{
3345 struct test_1040_thread_arg arg[NUM_THREADS] = { };
3346 size_t nt = NUM_THREADS;
3347 size_t n = 0;
3348
3349 Do_ADBG_BeginSubCase(c, "Concurent invoke with panic in TA");
3350 for (n = 0; n < nt; n++) {
3351 if (!ADBG_EXPECT(c, 0, pthread_create(&arg[n].thr, NULL,
3352 test_1040_thread,
3353 arg + n)))
3354 nt = n; /* break loop and start cleanup */
3355 }
3356 for (n = 0; n < nt; n++) {
3357 ADBG_EXPECT(c, 0, pthread_join(arg[n].thr, NULL));
3358 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
3359 }
3360 Do_ADBG_EndSubCase(c, "Concurent invoke with panic in TA");
3361}
3362ADBG_CASE_DEFINE(regression, 1040, xtest_tee_test_1040,
3363 "Test panic in concurrent open/invoke/close session");