blob: 7b487b67e134eb76177a7a7fcbffbf4210e58a18 [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;
Jerome Forissiere916b102017-06-07 17:55:52 +02001380
1381 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1382 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1383 &ret_orig)))
1384 return;
1385
1386 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1387 TEEC_NONE);
1388
1389 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1390 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1391 &ret_orig));
1392
1393 TEEC_CloseSession(&session);
1394}
Jens Wiklander14f48872018-06-29 15:30:13 +02001395ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1396 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001397
1398static void xtest_tee_test_1017(ADBG_Case_t *c)
1399{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001400 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001401 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001402 uint32_t ret_orig = 0;
1403 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001404 size_t page_size = 4096;
1405
Jens Wiklander87e81702018-03-20 12:00:00 +08001406 shm.size = 8 * page_size;
1407 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1408 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1409 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1410 return;
1411
1412 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1413 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1414 &ret_orig)))
1415 goto out;
1416
1417 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1418 TEEC_MEMREF_PARTIAL_INPUT,
1419 TEEC_MEMREF_PARTIAL_OUTPUT,
1420 TEEC_MEMREF_PARTIAL_OUTPUT);
1421
1422 /*
1423 * The first two memrefs are supposed to be combined into in
1424 * region and the last two memrefs should have one region each
1425 * when the parameters are mapped for the TA.
1426 */
1427 op.params[0].memref.parent = &shm;
1428 op.params[0].memref.size = page_size;
1429 op.params[0].memref.offset = 0;
1430
1431 op.params[1].memref.parent = &shm;
1432 op.params[1].memref.size = page_size;
1433 op.params[1].memref.offset = page_size;
1434
1435 op.params[2].memref.parent = &shm;
1436 op.params[2].memref.size = page_size;
1437 op.params[2].memref.offset = 4 * page_size;
1438
1439 op.params[3].memref.parent = &shm;
1440 op.params[3].memref.size = 2 * page_size;
1441 op.params[3].memref.offset = 6 * page_size;
1442
1443 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1444 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1445 &ret_orig));
1446
1447 TEEC_CloseSession(&session);
1448out:
1449 TEEC_ReleaseSharedMemory(&shm);
1450}
Jens Wiklander14f48872018-06-29 15:30:13 +02001451ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1452 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001453
Etienne Carriere84382b32018-04-25 18:30:30 +02001454static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1455 TEEC_SharedMemory *shm)
1456{
1457 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1458 TEEC_Result ret = TEEC_ERROR_GENERIC;
1459 uint32_t ret_orig = 0;
1460
1461 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1462 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1463
1464 op.params[0].memref.parent = shm;
1465 op.params[0].memref.size = shm->size / 2;
1466 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1467
1468 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1469 &op, &ret_orig);
1470
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001471 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001472 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1473 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1474 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1475 }
1476}
1477
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001478static void xtest_tee_test_1018(ADBG_Case_t *c)
1479{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001480 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001481 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001482 uint32_t ret_orig = 0;
1483 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001484 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001485 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001486 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001487 uint8_t buffer[6001] = { };
1488
1489 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1490 xtest_teec_open_session(&session,
1491 &os_test_ta_uuid,
1492 NULL,
1493 &ret_orig)))
1494 return;
1495
Joakim Becha1212b62020-04-07 12:06:00 +02001496 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001497
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001498 shm.size = 8 * page_size;
1499 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1500 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001501 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1502 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001503 goto out;
1504
1505 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1506 TEEC_MEMREF_PARTIAL_INPUT,
1507 TEEC_MEMREF_PARTIAL_OUTPUT,
1508 TEEC_MEMREF_PARTIAL_OUTPUT);
1509
1510 /*
1511 * The first two memrefs are supposed to be combined into in
1512 * region and the last two memrefs should have one region each
1513 * when the parameters are mapped for the TA.
1514 */
1515 op.params[0].memref.parent = &shm;
1516 op.params[0].memref.size = page_size;
1517 op.params[0].memref.offset = 0;
1518
1519 op.params[1].memref.parent = &shm;
1520 op.params[1].memref.size = page_size;
1521 op.params[1].memref.offset = page_size;
1522
1523 op.params[2].memref.parent = &shm;
1524 op.params[2].memref.size = page_size;
1525 op.params[2].memref.offset = 4 * page_size;
1526
1527 op.params[3].memref.parent = &shm;
1528 op.params[3].memref.size = 3 * page_size;
1529 op.params[3].memref.offset = 6 * page_size;
1530
Etienne Carriere84382b32018-04-25 18:30:30 +02001531 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1532 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001533
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001534 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001535 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1536 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1537 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1538 }
1539
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001540 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001541 Do_ADBG_EndSubCase(c, NULL);
1542
1543 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1544
1545 memset(&shm, 0, sizeof(shm));
1546 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1547 shm.buffer = buffer;
1548 shm.size = sizeof(buffer);
1549
1550 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1551 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1552 &shm)))
1553 goto out;
1554
1555 invoke_1byte_out_of_bounds(c, &session, &shm);
1556
1557 TEEC_ReleaseSharedMemory(&shm);
1558 Do_ADBG_EndSubCase(c, NULL);
1559
1560 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1561
1562 memset(&shm, 0, sizeof(shm));
1563 shm.size = sizeof(buffer);
1564 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1565 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1566 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1567 &shm)))
1568 goto out;
1569
1570 invoke_1byte_out_of_bounds(c, &session, &shm);
1571
1572 TEEC_ReleaseSharedMemory(&shm);
1573 Do_ADBG_EndSubCase(c, NULL);
1574
1575out:
1576 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001577}
Jens Wiklander14f48872018-06-29 15:30:13 +02001578ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1579 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001580
Jerome Forissier53bde722018-05-31 09:14:54 +02001581static void xtest_tee_test_1019(ADBG_Case_t *c)
1582{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001583 TEEC_Session session = { };
1584 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001585
1586 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1587 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1588 &ret_orig)))
1589 return;
1590
1591 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1592 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1593 &ret_orig));
1594
1595 (void)ADBG_EXPECT_TEEC_RESULT(c,
1596 TEEC_ERROR_TARGET_DEAD,
1597 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1598 NULL, &ret_orig));
1599
1600 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1601
1602 TEEC_CloseSession(&session);
1603}
Jens Wiklander14f48872018-06-29 15:30:13 +02001604ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1605 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001606
1607static void xtest_tee_test_1020(ADBG_Case_t *c)
1608{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001609 TEEC_Result res = TEEC_ERROR_GENERIC;
1610 TEEC_Session session = { };
1611 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001612
1613 /* Pseudo TA is optional: warn and nicely exit if not found */
1614 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1615 &ret_orig);
1616 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1617 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1618 return;
1619 }
1620 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1621
1622 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1623 NULL, &ret_orig);
1624 if (res != TEEC_SUCCESS) {
1625 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1626 ret_orig);
1627 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1628 Do_ADBG_Log(" - 1020 - skip test, feature not "
1629 "implemented");
1630 goto out;
1631 }
1632 /* Error */
1633 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1634 }
1635out:
1636 TEEC_CloseSession(&session);
1637}
1638ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1639 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001640
1641static TEEC_Result open_sec_session(TEEC_Session *session,
1642 const TEEC_UUID *uuid)
1643{
1644 TEEC_Result res = TEEC_ERROR_GENERIC;
1645 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1646 uint32_t ret_orig = 0;
1647
1648 op.params[0].tmpref.buffer = (void *)uuid;
1649 op.params[0].tmpref.size = sizeof(*uuid);
1650 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1651 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1652
1653 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1654 &op, &ret_orig);
1655 if (res != TEEC_SUCCESS)
1656 return TEEC_ERROR_GENERIC;
1657
1658 return res;
1659}
1660
1661static TEEC_Result sims_get_counter(TEEC_Session *session,
1662 uint32_t *counter)
1663{
1664 TEEC_Result res = TEEC_ERROR_GENERIC;
1665 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1666 uint32_t ret_orig = 0;
1667
1668 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1669 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1670
1671 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1672 &op, &ret_orig);
1673 if (res == TEEC_SUCCESS)
1674 *counter = op.params[0].value.a;
1675
1676 return res;
1677}
1678
1679static TEEC_Result trigger_panic(TEEC_Session *session,
1680 const TEEC_UUID *uuid)
1681{
1682 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1683 uint32_t ret_orig = 0;
1684
1685 if (!uuid) {
1686 op.params[0].tmpref.buffer = NULL;
1687 op.params[0].tmpref.size = 0;
1688 } else {
1689 op.params[0].tmpref.buffer = (void *)uuid;
1690 op.params[0].tmpref.size = sizeof(*uuid);
1691 }
1692 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1693 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1694
1695 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1696 &op, &ret_orig);
1697}
1698
1699static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1700 bool multi_instance)
1701{
1702 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1703 uint32_t counter = 0;
1704 uint32_t ret_orig = 0;
1705 uint32_t exp_counter = 0;
1706 TEEC_Session cs[3] = { };
1707
1708 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1709 xtest_teec_open_session(&cs[0], uuid, NULL,
1710 &ret_orig)))
1711 return;
1712
1713 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1714 xtest_teec_open_session(&cs[1], uuid, NULL,
1715 &ret_orig)))
1716 goto bail0;
1717
1718 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1719 goto bail1;
1720
1721 if (!ADBG_EXPECT(c, 0, counter))
1722 goto bail1;
1723
1724 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1725 goto bail1;
1726
1727 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001728 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001729 goto bail1;
1730
1731 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1732 trigger_panic(&cs[1], NULL)))
1733 goto bail1;
1734
1735 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1736 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1737 sims_get_counter(&cs[0], &counter)))
1738 goto bail1;
1739
1740 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1741 sims_get_counter(&cs[1], &counter)))
1742 goto bail1;
1743
1744 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001745 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001746 xtest_teec_open_session(&cs[1], uuid, NULL,
1747 &ret_orig)))
1748 goto bail1;
1749
1750 /* Sanity check of still valid TA context */
1751 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1752 xtest_teec_open_session(&cs[2], uuid, NULL,
1753 &ret_orig)))
1754 goto bail1;
1755
1756 /* Sanity check of still valid TA context */
1757 if (multi_instance) {
1758 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1759 sims_get_counter(&cs[0], &counter)))
1760 goto bail2;
1761
1762 if (!ADBG_EXPECT(c, 0, counter))
1763 goto bail2;
1764 }
1765
1766 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1767 goto bail2;
1768
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001769 exp_counter = multi_instance ? 0 : 1;
1770 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001771 goto bail2;
1772
1773bail2:
1774 TEEC_CloseSession(&cs[2]);
1775bail1:
1776 TEEC_CloseSession(&cs[1]);
1777bail0:
1778 TEEC_CloseSession(&cs[0]);
1779}
1780
1781static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1782 const TEEC_UUID *uuid2)
1783{
1784 uint32_t ret_orig = 0;
1785 uint32_t counter = 0;
1786 TEEC_Session cs[3] = { };
1787
1788 /* Test pre-conditions */
1789 /* 2.1 - CA opens a session toward TA1 */
1790 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1791 xtest_teec_open_session(&cs[0], uuid1, NULL,
1792 &ret_orig)))
1793 return;
1794
1795 /* 2.2 - CA opens a session toward TA2 */
1796 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1797 xtest_teec_open_session(&cs[1], uuid2, NULL,
1798 &ret_orig)))
1799 goto bail0;
1800
1801 /* 2.3 - TA1 opens a session toward TA2 */
1802 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1803 goto bail1;
1804
1805 /* 2.4 - CA invokes TA2 which panics */
1806 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1807 trigger_panic(&cs[1], NULL)))
1808 goto bail1;
1809
1810 /* Expected results */
1811 /* 2.5 - Expect CA->TA1 session is still alive */
1812 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1813 goto bail1;
1814
1815 /* 2.6 - Expect CA->TA2 session is properly released */
1816 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1817 sims_get_counter(&cs[1], &counter)))
1818 goto bail1;
1819
1820bail1:
1821 TEEC_CloseSession(&cs[1]);
1822bail0:
1823 TEEC_CloseSession(&cs[0]);
1824
1825 memset(cs, 0, sizeof(cs));
1826
1827 /* Test pre-conditions */
1828 /* 2.1 - CA opens a session toward TA1 */
1829 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1830 xtest_teec_open_session(&cs[0], uuid1, NULL,
1831 &ret_orig)))
1832 return;
1833
1834 /* 2.2 - CA opens a session toward TA2 */
1835 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1836 xtest_teec_open_session(&cs[1], uuid2, NULL,
1837 &ret_orig)))
1838 goto bail2;
1839
1840 /* 2.3 - TA1 opens a session toward TA2 */
1841 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1842 goto bail3;
1843
1844 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1845 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1846 goto bail3;
1847
1848 /* Expected results */
1849 /* 2.5 - Expect CA->TA1 session is still alive */
1850 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1851 goto bail3;
1852
1853 /* 2.6 - Expect CA->TA2 session is properly released */
1854 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1855 sims_get_counter(&cs[1], &counter)))
1856 goto bail3;
1857
1858bail3:
1859 TEEC_CloseSession(&cs[1]);
1860bail2:
1861 TEEC_CloseSession(&cs[0]);
1862}
1863
1864static void xtest_tee_test_1021(ADBG_Case_t *c)
1865{
1866 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1867 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1868 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1869
1870 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1871 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1872 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1873
1874 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1875 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1876 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1877
1878 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1879 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1880 &sims_keepalive_test_ta_uuid);
1881 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1882}
1883ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1884 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001885
1886static void xtest_tee_test_1022(ADBG_Case_t *c)
1887{
1888 TEEC_Session session = { 0 };
1889 uint32_t ret_orig = 0;
1890
1891 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1892 xtest_teec_open_session(&session, &os_test_ta_uuid,
1893 NULL, &ret_orig)))
1894 return;
1895
1896 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1897 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1898 &ret_orig));
1899
1900 (void)ADBG_EXPECT_TEEC_RESULT(c,
1901 TEEC_ERROR_TARGET_DEAD,
1902 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1903 NULL, &ret_orig));
1904
1905 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1906
1907 TEEC_CloseSession(&session);
1908}
1909ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1910 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001911
1912/*
1913 * Testing the ELF initialization (.init_array)
1914 *
1915 * - The TA has a global variable which can also be accessed by the two shared
1916 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1917 * dlopen())
1918 * - The TA and both libraries have initialization functions (declared with the
1919 * "constructor" attribute) which perform the following:
1920 * * The TA multiplies by 10 then adds 1
1921 * * os_test_lib multiplies by 10 then adds 2
1922 * * os_test_lib_dl multiplies by 10 then adds 3
1923 * By testing the variable value we make sure the initializations occurred in
1924 * the correct order.
1925 */
1926static void xtest_tee_test_1023(ADBG_Case_t *c)
1927{
1928 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1929 TEEC_Session session = { 0 };
1930 uint32_t ret_orig = 0;
1931
1932 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1933 TEEC_NONE, TEEC_NONE);
1934
1935 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1936 xtest_teec_open_session(&session, &os_test_ta_uuid,
1937 NULL, &ret_orig)))
1938 return;
1939
1940 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1941 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1942 &ret_orig));
1943
1944 /* Expected: initialization of os_test_lib, then TA */
1945 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1946
1947 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1948 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1949 &ret_orig));
1950
1951 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1952 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1953 &ret_orig));
1954
1955 /* Expected: initialization of os_test_lib_dl */
1956 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1957
1958 TEEC_CloseSession(&session);
1959}
1960ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1961 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001962
1963#ifdef CFG_CORE_TPM_EVENT_LOG
1964static void xtest_tee_test_1024(ADBG_Case_t *c)
1965{
1966 TEEC_Session session = {};
1967 uint32_t ret_orig = 0;
1968
1969 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1970 NULL, &ret_orig);
1971
1972 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1973 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1974 TA_TPM_TEST_GET_LOG,
1975 NULL, &ret_orig));
1976 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1977
1978 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1979 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1980 TA_TPM_TEST_SHORT_BUF,
1981 NULL, &ret_orig));
1982 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1983
1984 TEEC_CloseSession(&session);
1985}
1986
1987ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1988 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1989#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001990
1991static void xtest_tee_test_1025(ADBG_Case_t *c)
1992{
1993 TEEC_Session session = {};
1994 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1995 uint32_t ret_orig = 0;
1996 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001997 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001998 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02001999
2000 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
2001
2002 memset(&shm, 0, sizeof(shm));
2003 shm.flags = TEEC_MEM_INPUT;
2004 shm.buffer = NULL;
2005 shm.size = 0;
2006
2007 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2008 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2009
2010 memset(&shm, 0, sizeof(shm));
2011 shm.flags = TEEC_MEM_OUTPUT;
2012 shm.buffer = NULL;
2013 shm.size = 0;
2014
2015 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2016 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2017
2018 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002019
Etienne Carrierec602a522020-04-13 18:53:17 +02002020 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02002021 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02002022 return;
2023 }
2024
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002025 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2026 xtest_teec_open_session(&session,
2027 &os_test_ta_uuid,
2028 NULL, &ret_orig)))
2029 return;
2030
2031 empty_buf = malloc(1);
2032 if (!empty_buf) {
2033 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002034 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002035 }
2036
2037 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2038 TEEC_MEMREF_TEMP_INPUT,
2039 TEEC_MEMREF_TEMP_OUTPUT,
2040 TEEC_MEMREF_TEMP_OUTPUT);
2041
2042 Do_ADBG_BeginSubCase(c,
2043 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2044
2045 op.params[0].tmpref.buffer = empty_buf;
2046 op.params[0].tmpref.size = 0;
2047
2048 op.params[1].tmpref.buffer = NULL;
2049 op.params[1].tmpref.size = 0;
2050
2051 op.params[2].tmpref.buffer = empty_buf;
2052 op.params[2].tmpref.size = 0;
2053
2054 op.params[3].tmpref.buffer = NULL;
2055 op.params[3].tmpref.size = 0;
2056
2057 ADBG_EXPECT(c, TEE_SUCCESS,
2058 TEEC_InvokeCommand(&session,
2059 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2060 &ret_orig));
2061
2062 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2063
2064 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2065
2066 op.params[0].tmpref.buffer = empty_buf;
2067 op.params[0].tmpref.size = 1;
2068
2069 op.params[1].tmpref.buffer = NULL;
2070 op.params[1].tmpref.size = 0;
2071
2072 op.params[2].tmpref.buffer = empty_buf;
2073 op.params[2].tmpref.size = 0;
2074
2075 op.params[3].tmpref.buffer = NULL;
2076 op.params[3].tmpref.size = 0;
2077
2078 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2079 TEEC_InvokeCommand(&session,
2080 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2081 &ret_orig));
2082
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002083 TEEC_CloseSession(&session);
2084
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002085 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2086
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002087 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2088
2089 /* Pseudo TA is optional: warn and nicely exit if not found */
2090 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2091 &ret_orig);
2092 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2093 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2094 goto out;
2095 }
2096 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2097 goto out;
2098
2099 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2100 TEEC_NONE, TEEC_NONE);
2101 op.params[0].tmpref.buffer = NULL;
2102 op.params[0].tmpref.size = 0;
2103
2104 ADBG_EXPECT(c, TEE_SUCCESS,
2105 TEEC_InvokeCommand(&session,
2106 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2107 &op, &ret_orig));
2108
2109out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002110 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002111out:
2112 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002113 free(empty_buf);
2114}
2115ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2116 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002117
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002118#define TEE_UUID_NS_NAME_SIZE 128
2119
2120/*
2121 * TEE Client UUID name space identifier (UUIDv4)
2122 *
2123 * Value here is random UUID that is allocated as name space identifier for
2124 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2125 */
2126static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2127
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002128/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2129static TEEC_UUID client_uuid_public = { };
2130
2131static void xtest_tee_test_1026(ADBG_Case_t *c)
2132{
2133 TEEC_Result result = TEEC_ERROR_GENERIC;
2134 uint32_t ret_orig = 0;
2135 TEEC_Session session = { };
2136 uint32_t login = UINT32_MAX;
2137 TEEC_UUID client_uuid = { };
2138
2139 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2140 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2141
2142 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2143 return;
2144
2145 result = ta_os_test_cmd_client_identity(&session, &login,
2146 &client_uuid);
2147
2148 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2149 goto out;
2150
2151 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2152
2153 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2154 sizeof(TEEC_UUID));
2155
2156out:
2157 TEEC_CloseSession(&session);
2158}
2159
2160ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2161 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002162
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002163/*
2164 * regression_1027
2165 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002166 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2167 * login client UUID generation")
2168 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002169 *
2170 * xtest skips the test when not built with OpenSSL.
2171 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002172static void xtest_tee_test_1027(ADBG_Case_t *c)
2173{
Victor Chong8e070bc2020-05-13 09:59:33 +01002174#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002175 TEEC_Result result = TEEC_ERROR_GENERIC;
2176 uint32_t ret_orig = 0;
2177 TEEC_Session session = { };
2178 uint32_t login = UINT32_MAX;
2179 TEEC_UUID client_uuid = { };
2180 TEEC_UUID expected_client_uuid = { };
2181 TEEC_UUID uuid_ns = { };
2182 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2183
2184 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2185
2186 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2187 return;
2188
2189 sprintf(uuid_name, "uid=%x", geteuid());
2190
2191 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2192 strlen(uuid_name));
2193 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2194 return;
2195
2196 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2197 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2198
2199 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2200 return;
2201
2202 result = ta_os_test_cmd_client_identity(&session, &login,
2203 &client_uuid);
2204
2205 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2206 goto out;
2207
2208 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2209
2210 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2211 sizeof(TEEC_UUID));
2212
2213out:
2214 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002215#else /*!OPENSSL_FOUND*/
2216 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002217 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002218 /* xtest_uuid_v5() depends on OpenSSL */
2219 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2220#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002221}
2222
2223ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2224 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002225
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002226/*
2227 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002228 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2229 * login client UUID generation")
2230 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002231 *
2232 * xtest skips the test when not built with OpenSSL.
2233 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002234static void xtest_tee_test_1028(ADBG_Case_t *c)
2235{
Victor Chong8e070bc2020-05-13 09:59:33 +01002236#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002237 TEEC_Result result = TEEC_ERROR_GENERIC;
2238 uint32_t ret_orig = 0;
2239 TEEC_Session session = { };
2240 uint32_t login = UINT32_MAX;
2241 TEEC_UUID client_uuid = { };
2242 TEEC_UUID expected_client_uuid = { };
2243 TEEC_UUID uuid_ns = { };
2244 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2245 uint32_t group = 0;
2246
2247 group = getegid();
2248
2249 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2250
2251 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2252 return;
2253
2254 sprintf(uuid_name, "gid=%x", group);
2255
2256 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2257 strlen(uuid_name));
2258 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2259 return;
2260
2261 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2262 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2263
2264 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2265 return;
2266
2267 result = ta_os_test_cmd_client_identity(&session, &login,
2268 &client_uuid);
2269
2270 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2271 goto out;
2272
2273 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2274
2275 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2276 sizeof(TEEC_UUID));
2277
2278out:
2279 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002280#else /*!OPENSSL_FOUND*/
2281 UNUSED(c);
2282 /* xtest_uuid_v5() depends on OpenSSL */
2283 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2284#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002285}
2286
2287ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2288 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002289
2290static void xtest_tee_test_1029(ADBG_Case_t *c)
2291{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002292 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002293 TEEC_Session session = { 0 };
2294 uint32_t ret_orig = 0;
2295
2296 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2297 xtest_teec_open_session(&session, &os_test_ta_uuid,
2298 NULL, &ret_orig)))
2299 return;
2300
2301 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002302 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2303 &ret_orig);
2304 if (res == TEEC_ERROR_NOT_SUPPORTED)
2305 Do_ADBG_Log(" - 1029 - skip test, "
2306 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2307 else
2308 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002309 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2310
2311 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002312 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2313 &ret_orig);
2314 if (res == TEEC_ERROR_NOT_SUPPORTED)
2315 Do_ADBG_Log(" - 1029 - skip test, "
2316 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2317 else
2318 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002319 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2320
2321 TEEC_CloseSession(&session);
2322}
2323ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2324 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002325
2326static void xtest_tee_test_1030(ADBG_Case_t *c)
2327{
2328 TEEC_Session session = { 0 };
2329 uint32_t ret_orig = 0;
2330
2331 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2332 xtest_teec_open_session(&session, &os_test_ta_uuid,
2333 NULL, &ret_orig)))
2334 return;
2335
2336 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2337 ADBG_EXPECT_TEEC_SUCCESS(c,
2338 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2339 &ret_orig));
2340 Do_ADBG_EndSubCase(c, "Before dlopen()");
2341
2342 Do_ADBG_BeginSubCase(c, "After dlopen()");
2343 ADBG_EXPECT_TEEC_SUCCESS(c,
2344 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2345 &ret_orig));
2346 Do_ADBG_EndSubCase(c, "After dlopen()");
2347
2348 TEEC_CloseSession(&session);
2349}
2350ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2351 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002352
2353#ifndef __clang__
2354static void xtest_tee_test_1031(ADBG_Case_t *c)
2355{
2356 TEEC_Result ret = TEE_SUCCESS;
2357 TEEC_Session session = { 0 };
2358 uint32_t ret_orig = 0;
2359
2360 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2361 xtest_teec_open_session(&session, &os_test_ta_uuid,
2362 NULL, &ret_orig)))
2363 return;
2364
2365 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2366 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2367 &ret_orig);
2368 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2369 printf("TA not built with C++ support, skipping C++ tests\n");
2370 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2371 goto out;
2372
2373 }
2374 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2375 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2376
2377 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2378 ADBG_EXPECT_TEEC_SUCCESS(c,
2379 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2380 NULL, &ret_orig));
2381 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2382
2383 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2384 ADBG_EXPECT_TEEC_SUCCESS(c,
2385 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2386 NULL, &ret_orig));
2387 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2388
2389 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2390 ADBG_EXPECT_TEEC_SUCCESS(c,
2391 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2392 &ret_orig));
2393 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2394
2395 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2396 ADBG_EXPECT_TEEC_SUCCESS(c,
2397 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2398 &ret_orig));
2399 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2400out:
2401 TEEC_CloseSession(&session);
2402}
2403ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2404 "Test C++ features");
2405#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002406
2407static void xtest_tee_test_1032(ADBG_Case_t *c)
2408{
2409 TEEC_Result res = TEEC_SUCCESS;
2410 TEEC_Context ctx = { };
2411 TEEC_SharedMemory shm1 = {
2412 .buffer = xtest_tee_test_1032,
2413 .size = 32,
2414 .flags = TEEC_MEM_INPUT,
2415 };
2416 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2417 TEEC_SharedMemory shm2 = {
2418 .buffer = (void *)dummy_data,
2419 .size = sizeof(dummy_data),
2420 .flags = TEEC_MEM_INPUT,
2421 };
2422
2423 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2424 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2425 return;
2426
2427 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2428 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2429 TEEC_ReleaseSharedMemory(&shm1);
2430
2431 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2432 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2433 TEEC_ReleaseSharedMemory(&shm2);
2434
2435 TEEC_FinalizeContext(&ctx);
2436}
2437ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2438 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002439
2440static void xtest_tee_test_1033(ADBG_Case_t *c)
2441{
2442 TEEC_Session session = { };
2443 uint32_t ret_orig = 0;
2444
2445 /* TA will ping the test plugin during open session operation */
2446 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2447 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2448 NULL, &ret_orig)))
2449 return;
2450
2451 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2452 {
2453 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2454
2455 op.params[0].value.a = 20;
2456 op.params[0].value.b = 10;
2457 op.params[1].value.a = '+';
2458 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2459 TEEC_VALUE_INPUT, TEEC_NONE,
2460 TEEC_NONE);
2461
2462 ADBG_EXPECT_TEEC_SUCCESS(c,
2463 TEEC_InvokeCommand(&session,
2464 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2465 &ret_orig));
2466 ADBG_EXPECT(c, 30, op.params[0].value.a);
2467
2468 /* reassign, because the values was changed during previous op */
2469 op.params[0].value.a = 20;
2470 op.params[0].value.b = 10;
2471 op.params[1].value.a = '-';
2472 ADBG_EXPECT_TEEC_SUCCESS(c,
2473 TEEC_InvokeCommand(&session,
2474 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2475 &ret_orig));
2476 ADBG_EXPECT(c, 10, op.params[0].value.a);
2477 }
2478 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2479
2480 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2481 {
2482 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002483 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002484
2485 op.params[0].tmpref.buffer = to_plugin;
2486 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002487 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002488 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002489 TEEC_NONE, TEEC_NONE);
2490
2491 ADBG_EXPECT_TEEC_SUCCESS(c,
2492 TEEC_InvokeCommand(&session,
2493 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2494 &op, &ret_orig));
2495
2496 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002497 * The test plugin must calculate a sum of the input elements
2498 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002499 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002500 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002501 }
2502 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2503
2504 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2505 {
2506 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2507 char from_plugin[64] = { };
2508 char expected_arr[] = "Array from plugin";
2509 size_t expectes_size = sizeof(expected_arr);
2510
2511 op.params[0].tmpref.buffer = from_plugin;
2512 op.params[0].tmpref.size = sizeof(from_plugin);
2513 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2514 TEEC_VALUE_OUTPUT, TEEC_NONE,
2515 TEEC_NONE);
2516 ADBG_EXPECT_TEEC_SUCCESS(c,
2517 TEEC_InvokeCommand(&session,
2518 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2519 &ret_orig));
2520 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2521 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2522 }
2523 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2524
2525 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2526 {
2527 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2528
2529 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2530 TEEC_NONE, TEEC_NONE);
2531 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2532 TEEC_InvokeCommand(&session,
2533 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2534 &ret_orig));
2535 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2536 TEEC_InvokeCommand(&session,
2537 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2538 &ret_orig));
2539 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2540 TEEC_InvokeCommand(&session,
2541 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2542 &ret_orig));
2543 }
2544 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2545
2546 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2547 {
2548 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2549
2550 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2551 TEEC_NONE, TEEC_NONE);
2552 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2553 TEEC_InvokeCommand(&session,
2554 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2555 &op, &ret_orig));
2556 }
2557 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2558
2559 TEEC_CloseSession(&session);
2560}
2561ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2562 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002563
2564static void xtest_tee_test_1034(ADBG_Case_t *c)
2565{
2566 TEEC_Result res = TEEC_SUCCESS;
2567 TEEC_Session session = { };
2568 uint32_t ret_orig = 0;
2569
2570 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2571 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002572 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2573 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
Tim Lee5502a992023-07-12 16:48:47 +08002574 return;
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002575 }
Tim Lee5502a992023-07-12 16:48:47 +08002576 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2577 if (!res)
2578 TEEC_CloseSession(&session);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002579}
2580ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2581 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302582
2583#if defined(CFG_TA_BTI)
2584struct bti_test {
2585 uint32_t cmd;
2586 uint32_t func;
2587};
2588
2589#define BTI_TEST(caller_func, bti_func) { \
2590 .cmd = caller_func, \
2591 .func = bti_func, \
2592 }
2593
2594static const struct bti_test bti_cases_success[] = {
2595 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2596 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2597 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2598 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2599 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2600 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2601 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2602};
2603
2604static const struct bti_test bti_cases_panic[] = {
2605 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2606 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2607 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2608 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2609 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2610};
2611
2612static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2613{
2614 TEEC_Session session = {};
2615 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2616 uint32_t ret_orig = 0;
2617 TEEC_Result res;
2618
2619 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2620 TEEC_NONE);
2621 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2622 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2623 NULL, &ret_orig)))
2624 return;
2625
2626 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2627 &ret_orig);
2628 if (!res) {
2629 if(op.params[0].value.a)
2630 *bti = true;
2631 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2632 }
2633
2634 TEEC_CloseSession(&session);
2635}
2636
2637static void xtest_tee_test_1035(ADBG_Case_t *c)
2638{
2639 TEEC_Session session = {};
2640 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2641 struct bti_test const *test = NULL;
2642 uint32_t ret_orig = 0;
2643 TEEC_Result res;
2644 unsigned int n = 0;
2645 bool cpu_feature_bti = false;
2646
2647 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2648 get_cpu_feature(c, &cpu_feature_bti);
2649 Do_ADBG_EndSubCase(c, "BTI Implemented");
2650
2651 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2652 TEEC_NONE);
2653
2654 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2655 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2656 NULL, &ret_orig)))
2657 return;
2658
2659 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2660 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2661 test = &bti_cases_success[n];
2662
2663 op.params[0].value.a = test->func;
2664
2665 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2666 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2667 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2668 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2669 TEEC_CloseSession(&session);
2670 return;
2671 }
2672
2673 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2674
2675 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2676
2677 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2678 }
2679 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2680
2681 TEEC_CloseSession(&session);
2682
2683 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2684 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2685 test = &bti_cases_panic[n];
2686 res = TEEC_SUCCESS;
2687
2688 if (cpu_feature_bti)
2689 res = TEEC_ERROR_TARGET_DEAD;
2690
2691 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2692 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2693 NULL, &ret_orig)))
2694 goto out;
2695
2696 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2697 op.params[0].value.a = test->func;
2698
2699 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2700 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2701
2702 if (cpu_feature_bti)
2703 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2704 ret_orig);
2705
2706 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2707
2708 TEEC_CloseSession(&session);
2709 }
2710
2711out:
2712 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2713}
2714ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2715#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302716
2717static void xtest_tee_test_1036(ADBG_Case_t *c)
2718{
2719 TEEC_Session session = { };
2720 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2721 uint32_t ret_orig = 0;
2722 TEEC_Result res = TEEC_SUCCESS;
2723
2724 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2725 TEEC_NONE);
2726
2727 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2728 xtest_teec_open_session(&session, &os_test_ta_uuid,
2729 NULL, &ret_orig)))
2730 return;
2731
2732 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2733
2734 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2735 &ret_orig);
2736 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2737 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2738 goto out;
2739 }
2740
2741 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2742 goto out;
2743
2744 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2745
2746 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2747
2748 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2749 TEEC_InvokeCommand(&session,
2750 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2751 &op, &ret_orig));
2752
2753 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2754
2755 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2756
2757out:
2758 TEEC_CloseSession(&session);
2759}
2760ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2761 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002762
2763#define ATT_MAX_KEYSZ 4096
2764
2765#ifdef OPENSSL_FOUND
2766static RSA *att_key;
2767static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2768
2769/*
2770 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2771 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2772 */
2773static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2774 uint8_t *buf)
2775{
2776 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2777 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2778 unsigned char *ta_hash = buf;
2779 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2780 SHA256_CTX ctx = { };
2781 int salt_len = 32; /* Hard-coded in the PTA */
2782 int st = 0;
2783
2784 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2785 return;
2786
2787 SHA256_Init(&ctx);
2788 SHA256_Update(&ctx, nonce, nonce_size);
2789 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2790 SHA256_Final(digest, &ctx);
2791
2792 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2793 RSA_NO_PADDING);
2794 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2795 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2796 EVP_sha256(), decr, salt_len);
2797 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2798}
2799
2800static void free_att_key(void)
2801{
2802 RSA_free(att_key);
2803 att_key = NULL;
2804}
2805
Clement Faure44a31d02022-03-30 10:50:52 +02002806#if OPENSSL_VERSION_NUMBER < 0x10100000L
2807static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2808{
2809 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2810 return 0;
2811
2812 if (n != NULL) {
2813 BN_free(r->n);
2814 r->n = n;
2815 }
2816
2817 if (e != NULL) {
2818 BN_free(r->e);
2819 r->e = e;
2820 }
2821
2822 if (d != NULL) {
2823 BN_free(r->d);
2824 r->d = d;
2825 }
2826
2827 return 1;
2828}
2829#endif
2830
Jerome Forissier0915b222021-10-27 18:20:59 +02002831static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2832 size_t n_sz)
2833{
2834 BIGNUM *bn_e = NULL;
2835 BIGNUM *bn_n = NULL;
2836 int st = 0;
2837
2838 att_key_size = n_sz;
2839 att_key = RSA_new();
2840 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2841 return;
2842
2843 bn_e = BN_bin2bn(e, e_sz, NULL);
2844 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2845 goto err;
2846 bn_n = BN_bin2bn(n, n_sz, NULL);
2847 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2848 goto err;
2849
2850 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2851 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2852 goto err;
2853 return;
2854err:
2855 free_att_key();
2856}
2857#else
2858#define check_signature(...)
2859#define set_att_key(...)
2860#define free_att_key()
2861#endif
2862
2863/*
2864 * Verification of the output of the attestation PTA
2865 * - (If hash != NULL) check buf contains the expected hash
2866 * - (If OpenSSL is available) Check that the signature is valid
2867 */
2868static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2869 uint8_t *hash, uint8_t *buf)
2870{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002871 (void)nonce;
2872 (void)nonce_size;
2873
Jerome Forissier0915b222021-10-27 18:20:59 +02002874 if (hash)
2875 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2876
2877 check_signature(c, nonce, nonce_size, buf);
2878}
2879
2880/* Invoke attestation PTA to return the public key */
2881static void get_att_public_key(ADBG_Case_t *c)
2882{
2883 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2884 uint8_t e[3] = { }; /* We know e == 65537... */
2885 size_t n_sz = sizeof(n);
2886 size_t e_sz = sizeof(e);
2887 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2888 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2889 TEEC_Result res = TEEC_ERROR_GENERIC;
2890 TEEC_Session session = { };
2891 uint32_t ret_orig = 0;
2892
2893 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2894 TEEC_MEMREF_TEMP_OUTPUT,
2895 TEEC_VALUE_OUTPUT, TEEC_NONE);
2896 op.params[0].tmpref.buffer = e;
2897 op.params[0].tmpref.size = e_sz;
2898 op.params[1].tmpref.buffer = n;
2899 op.params[1].tmpref.size = n_sz;
2900
2901 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2902 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2903 return;
2904
2905 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2906 &ret_orig);
2907
2908 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2909 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2910 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2911 goto out;
2912
2913 e_sz = op.params[0].tmpref.size;
2914 n_sz = op.params[1].tmpref.size;
2915 set_att_key(c, e, e_sz, n, n_sz);
2916out:
2917 TEEC_CloseSession(&session);
2918}
2919
2920/* Invoke attestation PTA to hash the TEE binary */
2921static void attestation_tee(ADBG_Case_t *c)
2922{
2923 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2924 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2925 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2926 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2927 TEEC_Result res = TEEC_ERROR_GENERIC;
2928 TEEC_Session session = { };
2929 uint32_t ret_orig = 0;
2930
2931 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2932 TEEC_MEMREF_TEMP_OUTPUT,
2933 TEEC_NONE, TEEC_NONE);
2934 op.params[0].tmpref.buffer = nonce;
2935 op.params[0].tmpref.size = sizeof(nonce);
2936 op.params[1].tmpref.buffer = measurement;
2937 op.params[1].tmpref.size = sizeof(measurement);
2938
2939 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2940 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2941 return;
2942
2943 /* Hash TEE and check signature */
2944 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2945 &ret_orig);
2946 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2947 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2948
2949 TEEC_CloseSession(&session);
2950}
2951
2952/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2953static void attestation_ta_shdr(ADBG_Case_t *c)
2954{
2955 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2956 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2957 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2958 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2959 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2960 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2961 TEEC_Result res = TEEC_ERROR_GENERIC;
2962 TEEC_Session session = { };
2963 uint32_t ret_orig = 0;
2964 int cmp = 0;
2965
2966 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2967 TEEC_MEMREF_TEMP_INPUT,
2968 TEEC_MEMREF_TEMP_OUTPUT,
2969 TEEC_NONE);
2970 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2971 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2972 op.params[1].tmpref.buffer = nonce;
2973 op.params[1].tmpref.size = sizeof(nonce);
2974 op.params[2].tmpref.buffer = measurement;
2975 op.params[2].tmpref.size = sizeof(measurement);
2976
2977 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2978 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2979 return;
2980
2981 /* Hash TA and check signature */
2982 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2983 &op, &ret_orig);
2984 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2985 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2986 /* Save hash */
2987 memcpy(hash1, measurement, 32);
2988
2989 /* Hash TA again */
2990 memset(measurement, 0, sizeof(measurement));
2991 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2992 &op, &ret_orig);
2993 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2994 /* New hash should be identical */
2995 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2996
2997 /* Hash another TA */
2998 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
2999 memset(measurement, 0, sizeof(measurement));
3000 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3001 &op, &ret_orig);
3002 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3003 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3004 memcpy(hash2, measurement, 32);
3005 /* Different binaries should have different hashes */
3006 cmp = memcmp(hash1, hash2, sizeof(hash1));
3007 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3008
3009 TEEC_CloseSession(&session);
3010}
3011
3012/*
3013 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
3014 * itself.
3015 */
3016static void attestation_ta_memory(ADBG_Case_t *c)
3017{
3018 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3019 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
3020 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
3021 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
3022 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
3023 TEEC_Result res = TEEC_ERROR_GENERIC;
3024 TEEC_Session session = { };
3025 uint32_t ret_orig = 0;
3026 int cmp = 0;
3027
3028 Do_ADBG_BeginSubCase(c, "Consecutive calls");
3029
3030 /* Open session to os_test TA */
3031 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3032 &ret_orig);
3033 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3034 return;
3035
3036 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3037 TEEC_MEMREF_TEMP_OUTPUT,
3038 TEEC_NONE, TEEC_NONE);
3039 op.params[0].tmpref.buffer = nonce;
3040 op.params[0].tmpref.size = sizeof(nonce);
3041 op.params[1].tmpref.buffer = measurement;
3042 op.params[1].tmpref.size = sizeof(measurement);
3043
3044 /* Hash TA */
3045 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3046 &ret_orig);
3047 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3048
3049 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3050 memcpy(hash1, measurement, 32);
3051
3052 /* Hash TA again */
3053 memset(measurement, 0, sizeof(measurement));
3054 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3055 &ret_orig);
3056 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3057
3058 /* New hash should be identical to hash1 */
3059 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3060
3061 Do_ADBG_EndSubCase(c, "Consecutive calls");
3062
3063 /* Close TA session, will cause unload of TA */
3064 TEEC_CloseSession(&session);
3065
3066 Do_ADBG_BeginSubCase(c, "TA reload");
3067
3068 /* Load TA again and open a new session */
3069 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3070 &ret_orig);
3071 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3072 if (res)
3073 return;
3074
3075 /* Hash TA one more time */
3076 memset(measurement, 0, sizeof(measurement));
3077 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3078 &ret_orig);
3079 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3080
3081 /* Hash after reload should still be the same */
3082 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3083
3084 Do_ADBG_EndSubCase(c, "TA reload");
3085
3086 Do_ADBG_BeginSubCase(c, "Add shared library");
3087
3088 /*
3089 * Invoke a TA command that causes some additional code to be mapped
3090 * (shared library)
3091 */
3092 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3093 &ret_orig);
3094 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3095
3096 /* Hash TA one last time */
3097 memset(measurement, 0, sizeof(measurement));
3098 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3099 &ret_orig);
3100 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3101
3102 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3103 memcpy(hash2, measurement, 32);
3104
3105 /* Different binaries mapped mean different hashes */
3106 cmp = memcmp(hash1, hash2, sizeof(hash1));
3107 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3108
3109 Do_ADBG_EndSubCase(c, "Add shared library");
3110
3111 TEEC_CloseSession(&session);
3112}
3113
3114static void xtest_tee_test_1037(ADBG_Case_t *c)
3115{
3116 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3117 TEEC_Result res = TEEC_ERROR_GENERIC;
3118 TEEC_Session session = { };
3119 uint32_t ret_orig = 0;
3120
3121 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3122 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3123 Do_ADBG_Log(" skip test, pseudo TA not found");
3124 return;
3125 }
3126
chenchaokaiaf751932023-05-29 14:25:50 +08003127 TEEC_CloseSession(&session);
3128
Jerome Forissier0915b222021-10-27 18:20:59 +02003129 Do_ADBG_BeginSubCase(c, "Get public key");
3130 get_att_public_key(c);
3131 Do_ADBG_EndSubCase(c, "Get public key");
3132
3133 Do_ADBG_BeginSubCase(c, "TEE attestation");
3134 attestation_tee(c);
3135 Do_ADBG_EndSubCase(c, "TEE attestation");
3136
3137 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3138 attestation_ta_shdr(c);
3139 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3140
3141 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3142 attestation_ta_memory(c);
3143 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3144
3145 free_att_key();
3146}
3147ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3148 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003149
3150static void xtest_tee_test_1038(ADBG_Case_t *c)
3151{
3152 TEEC_Session session = { };
3153 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3154 uint32_t ret_orig = 0;
3155 TEEC_Result res = TEEC_SUCCESS;
3156
3157 Do_ADBG_BeginSubCase(c, "MTE use after free");
3158
3159 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3160 TEEC_NONE);
3161
3162 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3163 xtest_teec_open_session(&session, &os_test_ta_uuid,
3164 NULL, &ret_orig)))
3165 return;
3166
3167 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3168 &op, &ret_orig);
3169 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3170 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3171 goto out;
3172 }
3173
3174 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3175 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3176 TEEC_CloseSession(&session);
3177
3178 Do_ADBG_EndSubCase(c, "MTE use after free");
3179
3180 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3181
3182 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3183 TEEC_NONE);
3184
3185 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3186 xtest_teec_open_session(&session, &os_test_ta_uuid,
3187 NULL, &ret_orig)))
3188 return;
3189
3190 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3191 &op, &ret_orig);
3192 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3193 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3194 goto out;
3195 }
3196
3197 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3198 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3199
3200 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3201
3202 Do_ADBG_BeginSubCase(c, "MTE double free");
3203
3204 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3205 TEEC_NONE);
3206
3207 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3208 xtest_teec_open_session(&session, &os_test_ta_uuid,
3209 NULL, &ret_orig)))
3210 return;
3211
3212 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3213 &op, &ret_orig);
3214 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3215 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3216 goto out;
3217 }
3218
3219 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3220 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3221
3222 Do_ADBG_EndSubCase(c, "MTE double free");
3223
3224 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3225
3226 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3227 TEEC_NONE);
3228
3229 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3230 xtest_teec_open_session(&session, &os_test_ta_uuid,
3231 NULL, &ret_orig)))
3232 return;
3233
3234 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3235 &op, &ret_orig);
3236 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3237 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3238 goto out;
3239 }
3240
3241 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3242 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3243
3244 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3245
3246
3247out:
3248 TEEC_CloseSession(&session);
3249}
3250ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3251 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003252
3253static void xtest_tee_test_1039(ADBG_Case_t *c)
3254{
3255 TEEC_Session session = { };
3256 uint32_t ret_orig = 0;
3257
3258 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3259 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3260 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3261 NULL, &ret_orig)))
3262 TEEC_CloseSession(&session);
3263 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3264
3265 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3266 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3267 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3268 NULL, &ret_orig)))
3269 TEEC_CloseSession(&session);
3270 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3271
3272}
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003273ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3274 "Test subkey verification");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003275
3276struct test_1040_thread_arg {
3277 TEEC_Result res;
3278 pthread_t thr;
3279};
3280
3281static void *test_1040_thread(void *arg)
3282{
3283 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3284 struct test_1040_thread_arg *a = arg;
3285 TEEC_Result res = TEEC_SUCCESS;
3286 uint32_t err_orig = 0;
3287 TEEC_Session session = { };
3288 size_t loop_count = 100;
3289 size_t n = 0;
3290
3291 if (level == 0)
3292 loop_count /= 2;
3293
3294 while (n < loop_count) {
3295 res = xtest_teec_open_session(&session, &sims_test_ta_uuid,
3296 NULL, &err_orig);
3297 if (res) {
3298 if (res == TEEC_ERROR_TARGET_DEAD)
3299 continue;
3300 a->res = res;
3301 return NULL;
3302 }
3303
3304 memset(&op, 0, sizeof(op));
3305 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3306 TEEC_NONE, TEEC_NONE,
3307 TEEC_NONE);
3308 res = TEEC_InvokeCommand(&session, TA_SIMS_CMD_PANIC, &op,
3309 &err_orig);
3310 TEEC_CloseSession(&session);
3311 if (res != TEEC_ERROR_TARGET_DEAD) {
3312 if (res)
3313 a->res = res;
3314 else
3315 a->res = TEEC_ERROR_GENERIC;
3316 return NULL;
3317 }
3318 n++;
3319 }
3320 a->res = TEEC_SUCCESS;
3321 return NULL;
3322}
3323
3324static void xtest_tee_test_1040(ADBG_Case_t *c)
3325{
3326 struct test_1040_thread_arg arg[NUM_THREADS] = { };
3327 size_t nt = NUM_THREADS;
3328 size_t n = 0;
3329
3330 Do_ADBG_BeginSubCase(c, "Concurent invoke with panic in TA");
3331 for (n = 0; n < nt; n++) {
3332 if (!ADBG_EXPECT(c, 0, pthread_create(&arg[n].thr, NULL,
3333 test_1040_thread,
3334 arg + n)))
3335 nt = n; /* break loop and start cleanup */
3336 }
3337 for (n = 0; n < nt; n++) {
3338 ADBG_EXPECT(c, 0, pthread_join(arg[n].thr, NULL));
3339 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
3340 }
3341 Do_ADBG_EndSubCase(c, "Concurent invoke with panic in TA");
3342}
3343ADBG_CASE_DEFINE(regression, 1040, xtest_tee_test_1040,
3344 "Test panic in concurrent open/invoke/close session");