blob: 9bcff163ca9147bfb8d86e537874e8c722a4677d [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>
Jens Wiklander6b5d9a32024-12-10 17:43:47 +010027#include <sys/stat.h>
David Brownb2865ab2016-08-02 11:44:41 -060028#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020029#include <ta_arm_bti.h>
30#include <ta_concurrent.h>
31#include <ta_create_fail_test.h>
32#include <ta_crypt.h>
33#include <ta_miss_test.h>
34#include <ta_os_test.h>
35#include <ta_rpc_test.h>
36#include <ta_sims_keepalive_test.h>
37#include <ta_sims_test.h>
38#include <ta_supp_plugin.h>
39#include <ta_tpm_log_test.h>
40#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010041#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020042#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010043#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020044
Jens Wiklander6b5d9a32024-12-10 17:43:47 +010045#include "ftpm_ta.h"
Jerome Forissier0915b222021-10-27 18:20:59 +020046#include "xtest_helpers.h"
47#include "xtest_test.h"
48#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030049
Jens Wiklanderec545fb2017-11-24 16:58:07 +010050#ifndef MIN
51#define MIN(a, b) ((a) < (b) ? (a) : (b))
52#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020053
Etienne Carrierec0a61722023-03-07 17:47:42 +010054#define STATS_UUID \
55 { 0xd96a5b40, 0xe2c7, 0xb1af, \
56 { 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
57
58#define STATS_CMD_PAGER_STATS 0
59
60#define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096)
61
Pascal Brandc639ac82015-07-02 08:53:34 +020062struct xtest_crypto_session {
63 ADBG_Case_t *c;
64 TEEC_Session *session;
65 uint32_t cmd_id_sha256;
66 uint32_t cmd_id_aes256ecb_encrypt;
67 uint32_t cmd_id_aes256ecb_decrypt;
68};
69
Etienne Carrierec0a61722023-03-07 17:47:42 +010070static bool optee_pager_with_small_pool(void)
71{
72 TEEC_Result res = TEEC_ERROR_GENERIC;
73 TEEC_UUID uuid = STATS_UUID;
74 TEEC_Context ctx = { };
75 TEEC_Session sess = { };
76 TEEC_Operation op = { };
77 uint32_t eo = 0;
78 bool rc = false;
79
80 res = TEEC_InitializeContext(NULL, &ctx);
81 if (res)
82 return false;
83
84 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
85 NULL, &eo);
86 if (res)
87 goto out_ctx;
88
89 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_VALUE_OUTPUT,
90 TEEC_VALUE_OUTPUT, TEEC_NONE);
91 res = TEEC_InvokeCommand(&sess, STATS_CMD_PAGER_STATS, &op, &eo);
92 if (res)
93 goto out_sess;
94
95 if (op.params[0].value.b &&
96 op.params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD)
97 rc = true;
98
99out_sess:
100 TEEC_CloseSession(&sess);
101out_ctx:
102 TEEC_FinalizeContext(&ctx);
103
104 return rc;
105}
106
Pascal Brandc639ac82015-07-02 08:53:34 +0200107static void xtest_crypto_test(struct xtest_crypto_session *cs)
108{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100109 uint32_t ret_orig = 0;
110 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200111 uint8_t crypt_in[16] = { 22, 17 };
112
113 crypt_in[15] = 60;
114
115 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
116 {
117 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
118
119 op.params[0].tmpref.buffer = crypt_in;
120 op.params[0].tmpref.size = sizeof(crypt_in);
121 op.params[1].tmpref.buffer = crypt_out;
122 op.params[1].tmpref.size = sizeof(crypt_out);
123 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
124 TEEC_MEMREF_TEMP_OUTPUT,
125 TEEC_NONE, TEEC_NONE);
126
127 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
128 TEEC_InvokeCommand(cs->session,
129 cs->
130 cmd_id_aes256ecb_encrypt,
131 &op,
132 &ret_orig));
133 }
134 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
135
136 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
137 {
138 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100139 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200140
141 op.params[0].tmpref.buffer = crypt_out;
142 op.params[0].tmpref.size = sizeof(crypt_out);
143 op.params[1].tmpref.buffer = out;
144 op.params[1].tmpref.size = sizeof(out);
145 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
146 TEEC_MEMREF_TEMP_OUTPUT,
147 TEEC_NONE, TEEC_NONE);
148
149 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
150 TEEC_InvokeCommand(cs->session,
151 cs->
152 cmd_id_aes256ecb_decrypt,
153 &op,
154 &ret_orig));
155
156 if (!ADBG_EXPECT(cs->c, 0,
157 memcmp(crypt_in, out, sizeof(crypt_in)))) {
158 Do_ADBG_Log("crypt_in:");
159 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
160 Do_ADBG_Log("out:");
161 Do_ADBG_HexLog(out, sizeof(out), 16);
162 }
163 }
164 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
165
166 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
167 {
168 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
169 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
170 static const uint8_t sha256_out[] = {
171 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
172 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
173 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
174 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
175 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100176 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200177
178 op.params[0].tmpref.buffer = (void *)sha256_in;
179 op.params[0].tmpref.size = sizeof(sha256_in);
180 op.params[1].tmpref.buffer = out;
181 op.params[1].tmpref.size = sizeof(out);
182 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
183 TEEC_MEMREF_TEMP_OUTPUT,
184 TEEC_NONE, TEEC_NONE);
185
186 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
187 TEEC_InvokeCommand(cs->session,
188 cs->
189 cmd_id_sha256,
190 &op,
191 &ret_orig));
192
193 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
194 sizeof(sha256_out)))) {
195 Do_ADBG_Log("sha256_out:");
196 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
197 Do_ADBG_Log("out:");
198 Do_ADBG_HexLog(out, sizeof(out), 16);
199 }
200 }
201 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
202
Etienne Carrierea3198522017-10-26 09:48:55 +0200203 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200204 {
205 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
206 static const uint8_t in[] = {
207 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
208 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
209 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
210 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
211 };
212 static const uint8_t exp_out[] = {
213 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
214 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
215 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
216 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
217 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100218 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200219
220 op.params[0].tmpref.buffer = (void *)in;
221 op.params[0].tmpref.size = sizeof(in);
222 op.params[1].tmpref.buffer = out;
223 op.params[1].tmpref.size = sizeof(out);
224 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
225 TEEC_MEMREF_TEMP_OUTPUT,
226 TEEC_NONE, TEEC_NONE);
227
228 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
229 TEEC_InvokeCommand(cs->session,
230 cs->
231 cmd_id_aes256ecb_encrypt,
232 &op,
233 &ret_orig));
234
235 if (!ADBG_EXPECT(cs->c, 0,
236 memcmp(exp_out, out, sizeof(exp_out)))) {
237 Do_ADBG_Log("exp_out:");
238 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
239 Do_ADBG_Log("out:");
240 Do_ADBG_HexLog(out, sizeof(out), 16);
241 }
242 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200243 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200244
Etienne Carrierea3198522017-10-26 09:48:55 +0200245 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200246 {
247 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
248 static const uint8_t in[] = {
249 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
250 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
251 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
252 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
253 };
254 static const uint8_t exp_out[] = {
255 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
256 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
257 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
258 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
259 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100260 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200261
262 op.params[0].tmpref.buffer = (void *)in;
263 op.params[0].tmpref.size = sizeof(in);
264 op.params[1].tmpref.buffer = out;
265 op.params[1].tmpref.size = sizeof(out);
266 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
267 TEEC_MEMREF_TEMP_OUTPUT,
268 TEEC_NONE, TEEC_NONE);
269
270 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
271 TEEC_InvokeCommand(cs->session,
272 cs->
273 cmd_id_aes256ecb_decrypt,
274 &op,
275 &ret_orig));
276
277 if (!ADBG_EXPECT(cs->c, 0,
278 memcmp(exp_out, out, sizeof(exp_out)))) {
279 Do_ADBG_Log("exp_out:");
280 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
281 Do_ADBG_Log("out:");
282 Do_ADBG_HexLog(out, sizeof(out), 16);
283 }
284 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200285 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200286}
287
288static void xtest_tee_test_1001(ADBG_Case_t *c)
289{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100290 TEEC_Result res = TEEC_ERROR_GENERIC;
291 TEEC_Session session = { };
292 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200293
Etienne Carriere11093162017-10-26 09:49:04 +0200294 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100295 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100296 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200297 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
298 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100299 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200300 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100301 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
302 return;
303
304 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200305
Jens Wiklandercf16e842016-02-10 09:07:09 +0100306 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100307 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Etienne Carriere3ea96722022-03-11 09:16:40 +0100308
309 Do_ADBG_EndSubCase(c, "Core self tests");
310
311 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
312
313 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
314 &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL,
315 &ret_orig));
316
317 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
318
Jens Wiklandercf16e842016-02-10 09:07:09 +0100319 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200320}
Jens Wiklander14f48872018-06-29 15:30:13 +0200321ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200322
Jens Wiklander1d70a112017-10-16 15:16:39 +0200323static void xtest_tee_test_1002(ADBG_Case_t *c)
324{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100325 TEEC_Result res = TEEC_ERROR_GENERIC;
326 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200327 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100328 uint32_t ret_orig = 0;
329 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200330 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100331 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200332
Etienne Carriere11093162017-10-26 09:49:04 +0200333 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200334 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
335 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200336 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
337 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200338 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200339 }
340 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200341
342 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
343 TEEC_NONE, TEEC_NONE);
344 op.params[0].tmpref.size = sizeof(buf);
345 op.params[0].tmpref.buffer = buf;
346
347 for (n = 0; n < sizeof(buf); n++)
348 buf[n] = n + 1;
349 for (n = 0; n < sizeof(buf); n++)
350 exp_sum += buf[n];
351
352 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
353 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
354 goto out;
355
356 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
357out:
358 TEEC_CloseSession(&session);
359}
Jens Wiklander14f48872018-06-29 15:30:13 +0200360ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200361
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100362struct test_1003_arg {
363 uint32_t test_type;
364 size_t repeat;
365 size_t max_before_lockers;
366 size_t max_during_lockers;
367 size_t before_lockers;
368 size_t during_lockers;
369 TEEC_Result res;
370 uint32_t error_orig;
371};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200372
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100373static void *test_1003_thread(void *arg)
374{
375 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100376 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100377 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100378 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100379
380 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
381 NULL, &a->error_orig);
382 if (a->res != TEEC_SUCCESS)
383 return NULL;
384
385 for (n = 0; n < a->repeat; n++) {
386 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
387
388 op.params[0].value.a = a->test_type;
389 op.params[0].value.b = rounds;
390
391 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
392 TEEC_VALUE_OUTPUT,
393 TEEC_NONE, TEEC_NONE);
394 a->res = TEEC_InvokeCommand(&session,
395 PTA_INVOKE_TESTS_CMD_MUTEX,
396 &op, &a->error_orig);
397 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
398 op.params[1].value.b != 1) {
399 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
400 a->res = TEEC_ERROR_BAD_STATE;
401 a->error_orig = 42;
402 break;
403 }
404
405 if (a->test_type == PTA_MUTEX_TEST_READER) {
406 if (op.params[1].value.a > a->max_before_lockers)
407 a->max_before_lockers = op.params[1].value.a;
408
409 if (op.params[1].value.b > a->max_during_lockers)
410 a->max_during_lockers = op.params[1].value.b;
411
412 a->before_lockers += op.params[1].value.a;
413 a->during_lockers += op.params[1].value.b;
414 }
415 }
416 TEEC_CloseSession(&session);
417
418 return NULL;
419}
420
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100421#define TEST_1003_THREAD_COUNT (3 * 2)
422
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100423static void xtest_tee_test_1003(ADBG_Case_t *c)
424{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100425 TEEC_Result res = TEEC_ERROR_GENERIC;
426 TEEC_Session session = { };
427 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100428 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100429 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100430 size_t max_read_concurrency = 0;
431 size_t max_read_waiters = 0;
432 size_t num_concurrent_read_lockers = 0;
433 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100434 size_t n = 0;
435 size_t nt = TEST_1003_THREAD_COUNT;
436 double mean_read_concurrency = 0;
437 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100438 size_t num_writers = 0;
439 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100440 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100441
442 /* Pseudo TA is optional: warn and nicely exit if not found */
443 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
444 &ret_orig);
445 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
446 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
447 return;
448 }
449 ADBG_EXPECT_TEEC_SUCCESS(c, res);
450 TEEC_CloseSession(&session);
451
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100452 for (n = 0; n < nt; n++) {
453 if (n % 3) {
454 arg[n].test_type = PTA_MUTEX_TEST_READER;
455 num_readers++;
456 } else {
457 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
458 num_writers++;
459 }
460 arg[n].repeat = repeat;
461 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
462 test_1003_thread, arg + n)))
463 nt = n; /* break loop and start cleanup */
464 }
465
466 for (n = 0; n < nt; n++) {
467 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
468 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
469 Do_ADBG_Log("error origin %" PRIu32,
470 arg[n].error_orig);
471 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
472 if (arg[n].max_during_lockers > max_read_concurrency)
473 max_read_concurrency =
474 arg[n].max_during_lockers;
475
476 if (arg[n].max_before_lockers > max_read_waiters)
477 max_read_waiters = arg[n].max_before_lockers;
478
479 num_concurrent_read_lockers += arg[n].during_lockers;
480 num_concurrent_read_waiters += arg[n].before_lockers;
481 }
482 }
483
484 mean_read_concurrency = (double)num_concurrent_read_lockers /
485 (double)(repeat * num_readers);
486 mean_read_waiters = (double)num_concurrent_read_waiters /
487 (double)(repeat * num_readers);
488
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100489 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
490 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100491 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
492 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
493 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
494 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
495}
Jens Wiklander14f48872018-06-29 15:30:13 +0200496ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
497 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200498
Pascal Brandc639ac82015-07-02 08:53:34 +0200499static void xtest_tee_test_1004(ADBG_Case_t *c)
500{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100501 TEEC_Session session = { };
502 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200503 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
504 TA_CRYPT_CMD_AES256ECB_ENC,
505 TA_CRYPT_CMD_AES256ECB_DEC };
506
507 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
508 &session, &crypt_user_ta_uuid,
509 NULL, &ret_orig)))
510 return;
511
512 /* Run the "complete crypto test suite" */
513 xtest_crypto_test(&cs);
514
515 TEEC_CloseSession(&session);
516}
Jens Wiklander14f48872018-06-29 15:30:13 +0200517ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200518
Etienne Carriere92c34422018-02-09 13:11:40 +0100519static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200520{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100521 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200522 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100523 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200524
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300525 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200526 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300527 &ret_orig)))
528 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200529
530 op.params[0].value.a = n;
531 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
532 TEEC_NONE);
533
534 (void)ADBG_EXPECT_TEEC_RESULT(c,
535 TEEC_ERROR_TARGET_DEAD,
536 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
537 &ret_orig));
538
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300539 (void)ADBG_EXPECT_TEEC_RESULT(c,
540 TEEC_ERROR_TARGET_DEAD,
541 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200542 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300543
Pascal Brandc639ac82015-07-02 08:53:34 +0200544 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
545
546 TEEC_CloseSession(&session);
547}
548
Etienne Carriere92c34422018-02-09 13:11:40 +0100549static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
550 size_t size)
551{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100552 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100553 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100554 uint32_t ret_orig = 0;
555 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100556
Etienne Carriere92c34422018-02-09 13:11:40 +0100557 shm.size = size;
558 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
559 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
560 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
561 return;
562
563 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
564 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
565 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200566 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100567
568 op.params[0].value.a = (uint32_t)n;
569 op.params[1].memref.parent = &shm;
570 op.params[1].memref.size = size;
571 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
572 TEEC_NONE, TEEC_NONE);
573
574 (void)ADBG_EXPECT_TEEC_RESULT(c,
575 TEEC_ERROR_TARGET_DEAD,
576 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
577 &ret_orig));
578
579 (void)ADBG_EXPECT_TEEC_RESULT(c,
580 TEEC_ERROR_TARGET_DEAD,
581 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
582 &ret_orig));
583
584 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
585
586 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200587rel_shm:
588 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100589}
590
Pascal Brandc639ac82015-07-02 08:53:34 +0200591static void xtest_tee_test_1005(ADBG_Case_t *c)
592{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100593 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200594#define MAX_SESSIONS 3
595 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100596 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200597
598 for (i = 0; i < MAX_SESSIONS; i++) {
599 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200600 xtest_teec_open_session(&sessions[i],
601 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200602 NULL, &ret_orig)))
603 break;
604 }
605
606 for (; --i >= 0; )
607 TEEC_CloseSession(&sessions[i]);
608}
Jens Wiklander14f48872018-06-29 15:30:13 +0200609ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200610
611static void xtest_tee_test_1006(ADBG_Case_t *c)
612{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100613 TEEC_Session session = { };
614 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200615 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100616 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200617
618 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
619 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
620 &ret_orig)))
621 return;
622
623 op.params[0].tmpref.buffer = buf;
624 op.params[0].tmpref.size = sizeof(buf);
625 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
626 TEEC_NONE, TEEC_NONE);
627
628 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
629 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
630 &ret_orig));
631
632 TEEC_CloseSession(&session);
633}
Jens Wiklander14f48872018-06-29 15:30:13 +0200634ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
635 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200636
637static void xtest_tee_test_1007(ADBG_Case_t *c)
638{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100639 TEEC_Session session = { };
640 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200641
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300642 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200643 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300644 &ret_orig)))
645 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200646
647 (void)ADBG_EXPECT_TEEC_RESULT(c,
648 TEEC_ERROR_TARGET_DEAD,
649 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
650 &ret_orig));
651
652 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
653
654 (void)ADBG_EXPECT_TEEC_RESULT(c,
655 TEEC_ERROR_TARGET_DEAD,
656 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
657 &ret_orig));
658
659 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
660
661 TEEC_CloseSession(&session);
662}
Jens Wiklander14f48872018-06-29 15:30:13 +0200663ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200664
Jerome Forissierf02a2212015-10-29 14:33:35 +0100665#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000666# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800667#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000668# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100669#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000670# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100671#endif
672
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100673static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600674{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100675 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600676
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100677 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100678 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100679 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200680 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
681 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
682 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600683 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200684
Jens Wiklanderb7940892015-10-23 16:02:40 +0200685 return fopen(buf, mode);
686}
687
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100688static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200689{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100690 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100691 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
692 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100693 TEEC_Result res = TEEC_ERROR_GENERIC;
694 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100695 FILE *f = NULL;
696 bool r = false;
697 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100698 size_t sz = 0;
699 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200700
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100701 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
702 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
703 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200704
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100705 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
706 if (!ADBG_EXPECT_NOT_NULL(c, f))
707 goto out;
708 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
709 goto out;
710 sz = ftell(f);
711 rewind(f);
712
713 buf = malloc(sz);
714 if (!ADBG_EXPECT_NOT_NULL(c, buf))
715 goto out;
716
717 fread_res = fread(buf, 1, sz, f);
718 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
719 goto out;
720
Jens Wiklander4441fe22015-10-23 16:53:02 +0200721 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100722 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200723
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100724 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200725
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100726 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
727 TEEC_NONE, TEEC_NONE);
728 op.params[0].tmpref.buffer = buf;
729 op.params[0].tmpref.size = sz;
730
731 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
732 &ret_orig);
733 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
734out:
735 free(buf);
736 if (f)
737 fclose(f);
738 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200739 return r;
740}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100741
742static void test_1008_corrupt_ta(ADBG_Case_t *c)
743{
744 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
745 TEEC_Result res = TEEC_ERROR_GENERIC;
746 TEEC_Session session = { };
747 uint32_t ret_orig = 0;
748
749 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
750 if (res) {
751 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
752 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200753 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100754 return;
755 }
756 TEEC_CloseSession(&session);
757
758 ADBG_EXPECT_TRUE(c,
759 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
760 ADBG_EXPECT_TRUE(c,
761 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
762 ADBG_EXPECT_TRUE(c,
763 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
764 ADBG_EXPECT_TRUE(c,
765 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
766 ADBG_EXPECT_TRUE(c,
767 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
768 ADBG_EXPECT_TRUE(c,
769 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
770 ADBG_EXPECT_TRUE(c,
771 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
772 ADBG_EXPECT_TRUE(c,
773 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
774 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
775 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
776}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200777
Pascal Brandc639ac82015-07-02 08:53:34 +0200778static void xtest_tee_test_1008(ADBG_Case_t *c)
779{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100780 TEEC_Session session = { };
781 TEEC_Session session_crypt = { };
782 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200783
784 Do_ADBG_BeginSubCase(c, "Invoke command");
785 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300786 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200787 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300788 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200789
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300790 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
791 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
792 NULL, &ret_orig));
793 TEEC_CloseSession(&session);
794 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200795
Pascal Brandc639ac82015-07-02 08:53:34 +0200796 }
797 Do_ADBG_EndSubCase(c, "Invoke command");
798
799 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
800 {
801 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
802
803 op.params[0].value.a = 2000;
804 op.paramTypes = TEEC_PARAM_TYPES(
805 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
806
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300807 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200808 xtest_teec_open_session(&session,
809 &os_test_ta_uuid,
810 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300811 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200812
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300813 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
814 TEEC_InvokeCommand(&session,
815 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
816 &op, &ret_orig));
817 TEEC_CloseSession(&session);
818 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200819 }
820 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
821
822 Do_ADBG_BeginSubCase(c, "Create session fail");
823 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100824 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200825
Pascal Brandc639ac82015-07-02 08:53:34 +0200826 for (n = 0; n < 100; n++) {
827 Do_ADBG_Log("n = %zu", n);
828 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
829 xtest_teec_open_session(&session_crypt,
830 &create_fail_test_ta_uuid,
831 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100832 /* level > 0 may be used to detect/debug memory leaks */
833 if (!level)
834 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200835 }
836 }
837 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200838
Jens Wiklander4441fe22015-10-23 16:53:02 +0200839 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100840 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200841 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200842}
Jens Wiklander14f48872018-06-29 15:30:13 +0200843ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
844 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200845
Pascal Brandc639ac82015-07-02 08:53:34 +0200846static void *cancellation_thread(void *arg)
847{
848 /*
849 * Sleep 0.5 seconds before cancellation to make sure that the other
850 * thread is in RPC_WAIT.
851 */
852 (void)usleep(500000);
853 TEEC_RequestCancellation(arg);
854 return NULL;
855}
Pascal Brandc639ac82015-07-02 08:53:34 +0200856
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300857static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
858 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200859{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100860 TEEC_Session session = { };
861 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300862 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200863
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100864 memset(&thr, 0, sizeof(thr));
865
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300866 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200867 {
868 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
869
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300870 if (ADBG_EXPECT_TEEC_SUCCESS(c,
871 xtest_teec_open_session(&session, &os_test_ta_uuid,
872 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200873
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300874 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
875 TEEC_ORIGIN_TRUSTED_APP,
876 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200877
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300878 op.params[0].value.a = timeout;
879 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
880 TEEC_NONE,
881 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300882 if (cancel) {
883 (void)ADBG_EXPECT(c, 0,
884 pthread_create(&thr, NULL,
885 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200886
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300887 (void)ADBG_EXPECT_TEEC_RESULT(c,
888 TEEC_ERROR_CANCEL,
889 TEEC_InvokeCommand(&session,
890 TA_OS_TEST_CMD_WAIT,
891 &op,
892 &ret_orig));
893 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300894
895 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
896 TEEC_InvokeCommand(&session,
897 TA_OS_TEST_CMD_WAIT,
898 &op,
899 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300900 if (cancel)
901 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300902
903 TEEC_CloseSession(&session);
904 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200905 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300906 Do_ADBG_EndSubCase(c, "%s", subcase);
907}
908
909static void xtest_tee_test_1009(ADBG_Case_t *c)
910{
911 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
912 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300913 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300914 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200915}
Jens Wiklander14f48872018-06-29 15:30:13 +0200916ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200917
918static void xtest_tee_test_1010(ADBG_Case_t *c)
919{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100920 unsigned int n = 0;
921 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100922 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200923
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200924 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200925 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
926 xtest_tee_test_invalid_mem_access(c, n);
927 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
928 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100929
930 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
931 for (n = 1; n <= 5; n++) {
932 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200933 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100934 n, memref_sz[idx]);
935 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
936 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200937 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100938 n, memref_sz[idx]);
939 }
940 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200941}
Jens Wiklander14f48872018-06-29 15:30:13 +0200942ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
943 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200944
945static void xtest_tee_test_1011(ADBG_Case_t *c)
946{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100947 TEEC_Session session = { };
948 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200949 struct xtest_crypto_session cs = {
950 c, &session, TA_RPC_CMD_CRYPT_SHA256,
951 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
952 TA_RPC_CMD_CRYPT_AES256ECB_DEC
953 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100954 struct xtest_crypto_session cs_privmem = {
955 c, &session,
956 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
957 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
958 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
959 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200960 TEEC_UUID uuid = rpc_test_ta_uuid;
961
962 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
963 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
964 return;
965
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100966 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200967 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100968 * Run the "complete crypto test suite" using TA-to-TA
969 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200970 */
971 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100972 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
973
974 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
975 /*
976 * Run the "complete crypto test suite" using TA-to-TA
977 * communication via TA private memory.
978 */
979 xtest_crypto_test(&cs_privmem);
980 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
981
Pascal Brandc639ac82015-07-02 08:53:34 +0200982 TEEC_CloseSession(&session);
983}
Jens Wiklander14f48872018-06-29 15:30:13 +0200984ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
985 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200986
987/*
988 * Note that this test is failing when
989 * - running twice in a raw
990 * - and the user TA is statically linked
991 * This is because the counter is not reseted when opening the first session
992 * in case the TA is statically linked
993 */
994static void xtest_tee_test_1012(ADBG_Case_t *c)
995{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100996 TEEC_Session session1 = { };
997 TEEC_Session session2 = { };
998 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200999 TEEC_UUID uuid = sims_test_ta_uuid;
1000
1001 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
1002 {
1003 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1004 static const uint8_t in[] = {
1005 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
1006 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
1007 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
1008 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
1009 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001010 uint8_t out[32] = { };
1011 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +02001012
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001013 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001014 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001015 &ret_orig)))
1016 return;
Pascal Brandc639ac82015-07-02 08:53:34 +02001017
1018 op.params[0].value.a = 0;
1019 op.params[1].tmpref.buffer = (void *)in;
1020 op.params[1].tmpref.size = sizeof(in);
1021 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1022 TEEC_MEMREF_TEMP_INPUT,
1023 TEEC_NONE, TEEC_NONE);
1024
1025 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1026 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
1027 &ret_orig));
1028
Jerome Forissier3cc0a422017-12-14 09:53:24 +01001029 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001030 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001031 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001032 &ret_orig)))
1033 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +02001034
1035 op.params[0].value.a = 0;
1036 op.params[1].tmpref.buffer = out;
1037 op.params[1].tmpref.size = sizeof(out);
1038 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1039 TEEC_MEMREF_TEMP_OUTPUT,
1040 TEEC_NONE, TEEC_NONE);
1041
1042 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1043 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1044 &op, &ret_orig));
1045
1046 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1047 sizeof(out))) {
1048 Do_ADBG_Log("in:");
1049 Do_ADBG_HexLog(in, sizeof(in), 16);
1050 Do_ADBG_Log("out:");
1051 Do_ADBG_HexLog(out, sizeof(out), 16);
1052 }
1053
1054 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1055 TEEC_NONE, TEEC_NONE,
1056 TEEC_NONE);
1057
1058 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1059 TEEC_InvokeCommand(&session1,
1060 TA_SIMS_CMD_GET_COUNTER,
1061 &op, &ret_orig));
1062
1063 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1064
1065 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1066 TEEC_InvokeCommand(&session2,
1067 TA_SIMS_CMD_GET_COUNTER, &op,
1068 &ret_orig));
1069
1070 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1071 TEEC_CloseSession(&session2);
1072 }
1073
1074 memset(out, 0, sizeof(out));
1075 op.params[0].value.a = 0;
1076 op.params[1].tmpref.buffer = out;
1077 op.params[1].tmpref.size = sizeof(out);
1078 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1079 TEEC_MEMREF_TEMP_OUTPUT,
1080 TEEC_NONE, TEEC_NONE);
1081
1082 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1083 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1084 &ret_orig));
1085
1086 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1087 Do_ADBG_Log("in:");
1088 Do_ADBG_HexLog(in, sizeof(in), 16);
1089 Do_ADBG_Log("out:");
1090 Do_ADBG_HexLog(out, sizeof(out), 16);
1091 }
1092
1093 TEEC_CloseSession(&session1);
1094 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001095 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001096}
Jens Wiklander14f48872018-06-29 15:30:13 +02001097ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1098 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001099
1100struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001101 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001102 uint32_t cmd;
1103 uint32_t repeat;
1104 TEEC_SharedMemory *shm;
1105 uint32_t error_orig;
1106 TEEC_Result res;
1107 uint32_t max_concurrency;
1108 const uint8_t *in;
1109 size_t in_len;
1110 uint8_t *out;
1111 size_t out_len;
1112};
1113
1114static void *test_1013_thread(void *arg)
1115{
1116 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001117 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001118 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1119 uint8_t p2 = TEEC_NONE;
1120 uint8_t p3 = TEEC_NONE;
1121
Jens Wiklander70672972016-04-06 00:01:45 +02001122 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001123 &a->error_orig);
1124 if (a->res != TEEC_SUCCESS)
1125 return NULL;
1126
1127 op.params[0].memref.parent = a->shm;
1128 op.params[0].memref.size = a->shm->size;
1129 op.params[0].memref.offset = 0;
1130 op.params[1].value.a = a->repeat;
1131 op.params[1].value.b = 0;
1132 op.params[2].tmpref.buffer = (void *)a->in;
1133 op.params[2].tmpref.size = a->in_len;
1134 op.params[3].tmpref.buffer = a->out;
1135 op.params[3].tmpref.size = a->out_len;
1136
1137 if (a->in_len)
1138 p2 = TEEC_MEMREF_TEMP_INPUT;
1139 if (a->out_len)
1140 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1141
1142 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1143 TEEC_VALUE_INOUT, p2, p3);
1144
1145 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1146 a->max_concurrency = op.params[1].value.b;
1147 a->out_len = op.params[3].tmpref.size;
1148 TEEC_CloseSession(&session);
1149 return NULL;
1150}
1151
Pascal Brand4fa35582015-12-17 10:59:12 +01001152#define NUM_THREADS 3
1153
Jens Wiklander70672972016-04-06 00:01:45 +02001154static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1155 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001156{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001157 size_t nt = 0;
1158 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001159 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001160 TEEC_SharedMemory shm = { };
1161 size_t max_concurrency = 0;
1162 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001163 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1164 static const uint8_t sha256_out[] = {
1165 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1166 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1167 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1168 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1169 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001170 uint8_t out[32] = { };
1171 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001172 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001173
Etienne Carrierec0a61722023-03-07 17:47:42 +01001174 /* Decrease number of loops when pager has a small page pool */
1175 if (level == 0 && optee_pager_with_small_pool())
1176 repeat = 250;
1177
Jens Wiklander70672972016-04-06 00:01:45 +02001178 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001179 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001180
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001181 shm.size = sizeof(struct ta_concurrent_shm);
1182 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1183 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1184 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1185 return;
1186
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001187 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001188 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001189 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001190
1191 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001192 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001193 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001194 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001195 arg[n].shm = &shm;
1196 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1197 test_1013_thread, arg + n)))
1198 nt = n; /* break loop and start cleanup */
1199 }
1200
1201 for (n = 0; n < nt; n++) {
1202 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001203 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1204 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1205 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1206 skip = true;
1207 continue;
1208 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001209 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1210 if (arg[n].max_concurrency > max_concurrency)
1211 max_concurrency = arg[n].max_concurrency;
1212 }
1213
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001214 /*
1215 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001216 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001217 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1218 * best result there).
1219 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001220 if (!skip) {
1221 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1222 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1223 NUM_THREADS);
1224 *mean_concurrency += max_concurrency;
1225 }
Jens Wiklander70672972016-04-06 00:01:45 +02001226 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001227
Jens Wiklander70672972016-04-06 00:01:45 +02001228 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001229 memset(shm.buffer, 0, shm.size);
1230 memset(arg, 0, sizeof(arg));
1231 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001232 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001233
1234 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001235 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001236 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001237 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001238 arg[n].shm = &shm;
1239 arg[n].in = sha256_in;
1240 arg[n].in_len = sizeof(sha256_in);
1241 arg[n].out = out;
1242 arg[n].out_len = sizeof(out);
1243 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1244 test_1013_thread, arg + n)))
1245 nt = n; /* break loop and start cleanup */
1246 }
1247
1248 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001249 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1250 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1251 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1252 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1253 continue;
1254 }
1255 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001256 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1257 arg[n].out, arg[n].out_len);
1258 if (arg[n].max_concurrency > max_concurrency)
1259 max_concurrency = arg[n].max_concurrency;
1260 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001261 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001262 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001263
Pascal Brand4fa35582015-12-17 10:59:12 +01001264 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001265 TEEC_ReleaseSharedMemory(&shm);
1266}
Pascal Brand4fa35582015-12-17 10:59:12 +01001267
1268static void xtest_tee_test_1013(ADBG_Case_t *c)
1269{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001270 int i = 0;
1271 double mean_concurrency = 0;
1272 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001273 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001274
1275 if (level == 0)
1276 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001277
Jens Wiklander70672972016-04-06 00:01:45 +02001278 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001279 mean_concurrency = 0;
1280 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001281 xtest_tee_test_1013_single(c, &concurrency,
1282 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001283 mean_concurrency += concurrency;
1284 }
1285 mean_concurrency /= nb_loops;
1286
1287 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1288 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001289 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001290
Jens Wiklander70672972016-04-06 00:01:45 +02001291 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1292 mean_concurrency = 0;
1293 for (i = 0; i < nb_loops; i++) {
1294 xtest_tee_test_1013_single(c, &concurrency,
1295 &concurrent_large_ta_uuid);
1296 mean_concurrency += concurrency;
1297 }
1298 mean_concurrency /= nb_loops;
1299
1300 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1301 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1302 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1303}
Jens Wiklander14f48872018-06-29 15:30:13 +02001304ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001305 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001306
1307#ifdef CFG_SECURE_DATA_PATH
1308static void xtest_tee_test_1014(ADBG_Case_t *c)
1309{
1310 UNUSED(c);
1311
1312 int size = 17000;
1313 int loop = 10;
Olivier Masseda5282a2022-04-07 11:24:08 +02001314 const char *heap_name = DEFAULT_HEAP_NAME;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001315 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001316 int test = 0;
1317 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001318
1319 test = TEST_NS_TO_TA;
1320 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001321 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001322 ADBG_EXPECT(c, 0, ret);
1323 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1324
1325 test = TEST_TA_TO_TA;
1326 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001327 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001328 ADBG_EXPECT(c, 0, ret);
1329 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1330
1331 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001332 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001333 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001334 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001335 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001336
1337 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001338 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001339 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001340 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001341 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001342
1343 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001344 ret = sdp_out_of_bounds_memref_test(size, heap_name, 0);
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001345 ADBG_EXPECT(c, 0, ret);
1346 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001347}
Jens Wiklander14f48872018-06-29 15:30:13 +02001348ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1349 "Test secure data path against SDP TAs and pTAs");
1350#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001351
Jerome Forissierf4259942022-01-11 14:27:05 +01001352#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001353static void xtest_tee_test_1015(ADBG_Case_t *c)
1354{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001355 TEEC_Result res = TEEC_ERROR_GENERIC;
1356 TEEC_Session session = { };
1357 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001358
Etienne Carriere11093162017-10-26 09:49:04 +02001359 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001360 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1361 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001362 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1363 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001364 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001365 }
1366 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001367
1368 ADBG_EXPECT_TEEC_SUCCESS(c,
1369 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1370 NULL, &ret_orig));
1371 TEEC_CloseSession(&session);
1372}
Jens Wiklander14f48872018-06-29 15:30:13 +02001373ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1374 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001375#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001376
1377static void xtest_tee_test_1016(ADBG_Case_t *c)
1378{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001379 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001380 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001381 uint32_t ret_orig = 0;
Vincent Mailholda741cd2023-11-16 20:20:54 +09001382 int dummy = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001383
1384 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1385 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1386 &ret_orig)))
1387 return;
1388
1389 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1390 TEEC_NONE);
1391
1392 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1393 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1394 &ret_orig));
1395
Vincent Mailholda741cd2023-11-16 20:20:54 +09001396 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1397 TEEC_MEMREF_TEMP_INOUT,
1398 TEEC_MEMREF_TEMP_OUTPUT,
1399 TEEC_NONE);
1400
1401 op.params[0].tmpref.buffer = &dummy;
1402 op.params[0].tmpref.size = 0;
1403
1404 op.params[1].tmpref.buffer = &dummy;
1405 op.params[1].tmpref.size = 0;
1406
1407 op.params[2].tmpref.buffer = &dummy;
1408 op.params[2].tmpref.size = 0;
1409
1410 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1411 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF_SIZE0,
1412 &op, &ret_orig));
1413
Jerome Forissiere916b102017-06-07 17:55:52 +02001414 TEEC_CloseSession(&session);
1415}
Jens Wiklander14f48872018-06-29 15:30:13 +02001416ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1417 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001418
1419static void xtest_tee_test_1017(ADBG_Case_t *c)
1420{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001421 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001422 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001423 uint32_t ret_orig = 0;
1424 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001425 size_t page_size = 4096;
1426
Jens Wiklander87e81702018-03-20 12:00:00 +08001427 shm.size = 8 * page_size;
1428 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1429 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1430 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1431 return;
1432
1433 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1434 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1435 &ret_orig)))
1436 goto out;
1437
1438 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1439 TEEC_MEMREF_PARTIAL_INPUT,
1440 TEEC_MEMREF_PARTIAL_OUTPUT,
1441 TEEC_MEMREF_PARTIAL_OUTPUT);
1442
1443 /*
1444 * The first two memrefs are supposed to be combined into in
1445 * region and the last two memrefs should have one region each
1446 * when the parameters are mapped for the TA.
1447 */
1448 op.params[0].memref.parent = &shm;
1449 op.params[0].memref.size = page_size;
1450 op.params[0].memref.offset = 0;
1451
1452 op.params[1].memref.parent = &shm;
1453 op.params[1].memref.size = page_size;
1454 op.params[1].memref.offset = page_size;
1455
1456 op.params[2].memref.parent = &shm;
1457 op.params[2].memref.size = page_size;
1458 op.params[2].memref.offset = 4 * page_size;
1459
1460 op.params[3].memref.parent = &shm;
1461 op.params[3].memref.size = 2 * page_size;
1462 op.params[3].memref.offset = 6 * page_size;
1463
1464 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1465 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1466 &ret_orig));
1467
1468 TEEC_CloseSession(&session);
1469out:
1470 TEEC_ReleaseSharedMemory(&shm);
1471}
Jens Wiklander14f48872018-06-29 15:30:13 +02001472ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1473 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001474
Etienne Carriere84382b32018-04-25 18:30:30 +02001475static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1476 TEEC_SharedMemory *shm)
1477{
1478 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1479 TEEC_Result ret = TEEC_ERROR_GENERIC;
1480 uint32_t ret_orig = 0;
1481
1482 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1483 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1484
1485 op.params[0].memref.parent = shm;
1486 op.params[0].memref.size = shm->size / 2;
1487 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1488
1489 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1490 &op, &ret_orig);
1491
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001492 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001493 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1494 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1495 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1496 }
1497}
1498
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001499static void xtest_tee_test_1018(ADBG_Case_t *c)
1500{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001501 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001502 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001503 uint32_t ret_orig = 0;
1504 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001505 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001506 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001507 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001508 uint8_t buffer[6001] = { };
1509
1510 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1511 xtest_teec_open_session(&session,
1512 &os_test_ta_uuid,
1513 NULL,
1514 &ret_orig)))
1515 return;
1516
Joakim Becha1212b62020-04-07 12:06:00 +02001517 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001518
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001519 shm.size = 8 * page_size;
1520 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1521 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001522 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1523 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001524 goto out;
1525
1526 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1527 TEEC_MEMREF_PARTIAL_INPUT,
1528 TEEC_MEMREF_PARTIAL_OUTPUT,
1529 TEEC_MEMREF_PARTIAL_OUTPUT);
1530
1531 /*
1532 * The first two memrefs are supposed to be combined into in
1533 * region and the last two memrefs should have one region each
1534 * when the parameters are mapped for the TA.
1535 */
1536 op.params[0].memref.parent = &shm;
1537 op.params[0].memref.size = page_size;
1538 op.params[0].memref.offset = 0;
1539
1540 op.params[1].memref.parent = &shm;
1541 op.params[1].memref.size = page_size;
1542 op.params[1].memref.offset = page_size;
1543
1544 op.params[2].memref.parent = &shm;
1545 op.params[2].memref.size = page_size;
1546 op.params[2].memref.offset = 4 * page_size;
1547
1548 op.params[3].memref.parent = &shm;
1549 op.params[3].memref.size = 3 * page_size;
1550 op.params[3].memref.offset = 6 * page_size;
1551
Etienne Carriere84382b32018-04-25 18:30:30 +02001552 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1553 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001554
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001555 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001556 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1557 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1558 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1559 }
1560
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001561 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001562 Do_ADBG_EndSubCase(c, NULL);
1563
1564 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1565
1566 memset(&shm, 0, sizeof(shm));
1567 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1568 shm.buffer = buffer;
1569 shm.size = sizeof(buffer);
1570
1571 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1572 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1573 &shm)))
1574 goto out;
1575
1576 invoke_1byte_out_of_bounds(c, &session, &shm);
1577
1578 TEEC_ReleaseSharedMemory(&shm);
1579 Do_ADBG_EndSubCase(c, NULL);
1580
1581 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1582
1583 memset(&shm, 0, sizeof(shm));
1584 shm.size = sizeof(buffer);
1585 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1586 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1587 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1588 &shm)))
1589 goto out;
1590
1591 invoke_1byte_out_of_bounds(c, &session, &shm);
1592
1593 TEEC_ReleaseSharedMemory(&shm);
1594 Do_ADBG_EndSubCase(c, NULL);
1595
1596out:
1597 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001598}
Jens Wiklander14f48872018-06-29 15:30:13 +02001599ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1600 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001601
Jerome Forissier53bde722018-05-31 09:14:54 +02001602static void xtest_tee_test_1019(ADBG_Case_t *c)
1603{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001604 TEEC_Session session = { };
1605 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001606
1607 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1608 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1609 &ret_orig)))
1610 return;
1611
1612 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1613 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1614 &ret_orig));
1615
1616 (void)ADBG_EXPECT_TEEC_RESULT(c,
1617 TEEC_ERROR_TARGET_DEAD,
1618 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1619 NULL, &ret_orig));
1620
1621 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1622
1623 TEEC_CloseSession(&session);
1624}
Jens Wiklander14f48872018-06-29 15:30:13 +02001625ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1626 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001627
1628static void xtest_tee_test_1020(ADBG_Case_t *c)
1629{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001630 TEEC_Result res = TEEC_ERROR_GENERIC;
1631 TEEC_Session session = { };
1632 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001633
1634 /* Pseudo TA is optional: warn and nicely exit if not found */
1635 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1636 &ret_orig);
1637 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1638 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1639 return;
1640 }
1641 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1642
1643 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1644 NULL, &ret_orig);
1645 if (res != TEEC_SUCCESS) {
1646 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1647 ret_orig);
1648 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1649 Do_ADBG_Log(" - 1020 - skip test, feature not "
1650 "implemented");
1651 goto out;
1652 }
1653 /* Error */
1654 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1655 }
1656out:
1657 TEEC_CloseSession(&session);
1658}
1659ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1660 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001661
1662static TEEC_Result open_sec_session(TEEC_Session *session,
1663 const TEEC_UUID *uuid)
1664{
1665 TEEC_Result res = TEEC_ERROR_GENERIC;
1666 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1667 uint32_t ret_orig = 0;
1668
1669 op.params[0].tmpref.buffer = (void *)uuid;
1670 op.params[0].tmpref.size = sizeof(*uuid);
1671 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1672 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1673
1674 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1675 &op, &ret_orig);
1676 if (res != TEEC_SUCCESS)
1677 return TEEC_ERROR_GENERIC;
1678
1679 return res;
1680}
1681
1682static TEEC_Result sims_get_counter(TEEC_Session *session,
1683 uint32_t *counter)
1684{
1685 TEEC_Result res = TEEC_ERROR_GENERIC;
1686 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1687 uint32_t ret_orig = 0;
1688
1689 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1690 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1691
1692 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1693 &op, &ret_orig);
1694 if (res == TEEC_SUCCESS)
1695 *counter = op.params[0].value.a;
1696
1697 return res;
1698}
1699
1700static TEEC_Result trigger_panic(TEEC_Session *session,
1701 const TEEC_UUID *uuid)
1702{
1703 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1704 uint32_t ret_orig = 0;
1705
1706 if (!uuid) {
1707 op.params[0].tmpref.buffer = NULL;
1708 op.params[0].tmpref.size = 0;
1709 } else {
1710 op.params[0].tmpref.buffer = (void *)uuid;
1711 op.params[0].tmpref.size = sizeof(*uuid);
1712 }
1713 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1714 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1715
1716 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1717 &op, &ret_orig);
1718}
1719
1720static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1721 bool multi_instance)
1722{
1723 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1724 uint32_t counter = 0;
1725 uint32_t ret_orig = 0;
1726 uint32_t exp_counter = 0;
1727 TEEC_Session cs[3] = { };
1728
1729 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1730 xtest_teec_open_session(&cs[0], uuid, NULL,
1731 &ret_orig)))
1732 return;
1733
1734 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1735 xtest_teec_open_session(&cs[1], uuid, NULL,
1736 &ret_orig)))
1737 goto bail0;
1738
1739 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1740 goto bail1;
1741
1742 if (!ADBG_EXPECT(c, 0, counter))
1743 goto bail1;
1744
1745 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1746 goto bail1;
1747
1748 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001749 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001750 goto bail1;
1751
1752 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1753 trigger_panic(&cs[1], NULL)))
1754 goto bail1;
1755
1756 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1757 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1758 sims_get_counter(&cs[0], &counter)))
1759 goto bail1;
1760
1761 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1762 sims_get_counter(&cs[1], &counter)))
1763 goto bail1;
1764
1765 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001766 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001767 xtest_teec_open_session(&cs[1], uuid, NULL,
1768 &ret_orig)))
1769 goto bail1;
1770
1771 /* Sanity check of still valid TA context */
1772 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1773 xtest_teec_open_session(&cs[2], uuid, NULL,
1774 &ret_orig)))
1775 goto bail1;
1776
1777 /* Sanity check of still valid TA context */
1778 if (multi_instance) {
1779 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1780 sims_get_counter(&cs[0], &counter)))
1781 goto bail2;
1782
1783 if (!ADBG_EXPECT(c, 0, counter))
1784 goto bail2;
1785 }
1786
1787 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1788 goto bail2;
1789
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001790 exp_counter = multi_instance ? 0 : 1;
1791 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001792 goto bail2;
1793
1794bail2:
1795 TEEC_CloseSession(&cs[2]);
1796bail1:
1797 TEEC_CloseSession(&cs[1]);
1798bail0:
1799 TEEC_CloseSession(&cs[0]);
1800}
1801
1802static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1803 const TEEC_UUID *uuid2)
1804{
1805 uint32_t ret_orig = 0;
1806 uint32_t counter = 0;
1807 TEEC_Session cs[3] = { };
1808
1809 /* Test pre-conditions */
1810 /* 2.1 - CA opens a session toward TA1 */
1811 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1812 xtest_teec_open_session(&cs[0], uuid1, NULL,
1813 &ret_orig)))
1814 return;
1815
1816 /* 2.2 - CA opens a session toward TA2 */
1817 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1818 xtest_teec_open_session(&cs[1], uuid2, NULL,
1819 &ret_orig)))
1820 goto bail0;
1821
1822 /* 2.3 - TA1 opens a session toward TA2 */
1823 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1824 goto bail1;
1825
1826 /* 2.4 - CA invokes TA2 which panics */
1827 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1828 trigger_panic(&cs[1], NULL)))
1829 goto bail1;
1830
1831 /* Expected results */
1832 /* 2.5 - Expect CA->TA1 session is still alive */
1833 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1834 goto bail1;
1835
1836 /* 2.6 - Expect CA->TA2 session is properly released */
1837 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1838 sims_get_counter(&cs[1], &counter)))
1839 goto bail1;
1840
1841bail1:
1842 TEEC_CloseSession(&cs[1]);
1843bail0:
1844 TEEC_CloseSession(&cs[0]);
1845
1846 memset(cs, 0, sizeof(cs));
1847
1848 /* Test pre-conditions */
1849 /* 2.1 - CA opens a session toward TA1 */
1850 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1851 xtest_teec_open_session(&cs[0], uuid1, NULL,
1852 &ret_orig)))
1853 return;
1854
1855 /* 2.2 - CA opens a session toward TA2 */
1856 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1857 xtest_teec_open_session(&cs[1], uuid2, NULL,
1858 &ret_orig)))
1859 goto bail2;
1860
1861 /* 2.3 - TA1 opens a session toward TA2 */
1862 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1863 goto bail3;
1864
1865 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1866 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1867 goto bail3;
1868
1869 /* Expected results */
1870 /* 2.5 - Expect CA->TA1 session is still alive */
1871 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1872 goto bail3;
1873
1874 /* 2.6 - Expect CA->TA2 session is properly released */
1875 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1876 sims_get_counter(&cs[1], &counter)))
1877 goto bail3;
1878
1879bail3:
1880 TEEC_CloseSession(&cs[1]);
1881bail2:
1882 TEEC_CloseSession(&cs[0]);
1883}
1884
1885static void xtest_tee_test_1021(ADBG_Case_t *c)
1886{
1887 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1888 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1889 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1890
1891 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1892 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1893 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1894
1895 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1896 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1897 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1898
1899 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1900 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1901 &sims_keepalive_test_ta_uuid);
1902 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1903}
1904ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1905 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001906
1907static void xtest_tee_test_1022(ADBG_Case_t *c)
1908{
1909 TEEC_Session session = { 0 };
1910 uint32_t ret_orig = 0;
1911
1912 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1913 xtest_teec_open_session(&session, &os_test_ta_uuid,
1914 NULL, &ret_orig)))
1915 return;
1916
1917 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1918 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1919 &ret_orig));
1920
1921 (void)ADBG_EXPECT_TEEC_RESULT(c,
1922 TEEC_ERROR_TARGET_DEAD,
1923 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1924 NULL, &ret_orig));
1925
1926 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1927
1928 TEEC_CloseSession(&session);
1929}
1930ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1931 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001932
1933/*
1934 * Testing the ELF initialization (.init_array)
1935 *
1936 * - The TA has a global variable which can also be accessed by the two shared
1937 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1938 * dlopen())
1939 * - The TA and both libraries have initialization functions (declared with the
1940 * "constructor" attribute) which perform the following:
1941 * * The TA multiplies by 10 then adds 1
1942 * * os_test_lib multiplies by 10 then adds 2
1943 * * os_test_lib_dl multiplies by 10 then adds 3
1944 * By testing the variable value we make sure the initializations occurred in
1945 * the correct order.
1946 */
1947static void xtest_tee_test_1023(ADBG_Case_t *c)
1948{
1949 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1950 TEEC_Session session = { 0 };
1951 uint32_t ret_orig = 0;
1952
1953 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1954 TEEC_NONE, TEEC_NONE);
1955
1956 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1957 xtest_teec_open_session(&session, &os_test_ta_uuid,
1958 NULL, &ret_orig)))
1959 return;
1960
1961 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1962 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1963 &ret_orig));
1964
1965 /* Expected: initialization of os_test_lib, then TA */
1966 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1967
1968 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1969 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1970 &ret_orig));
1971
1972 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1973 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1974 &ret_orig));
1975
1976 /* Expected: initialization of os_test_lib_dl */
1977 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1978
1979 TEEC_CloseSession(&session);
1980}
1981ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1982 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001983
1984#ifdef CFG_CORE_TPM_EVENT_LOG
1985static void xtest_tee_test_1024(ADBG_Case_t *c)
1986{
1987 TEEC_Session session = {};
1988 uint32_t ret_orig = 0;
1989
1990 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1991 NULL, &ret_orig);
1992
1993 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1994 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1995 TA_TPM_TEST_GET_LOG,
1996 NULL, &ret_orig));
1997 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1998
1999 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
2000 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
2001 TA_TPM_TEST_SHORT_BUF,
2002 NULL, &ret_orig));
2003 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
2004
2005 TEEC_CloseSession(&session);
2006}
2007
2008ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
2009 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
2010#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002011
2012static void xtest_tee_test_1025(ADBG_Case_t *c)
2013{
2014 TEEC_Session session = {};
2015 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2016 uint32_t ret_orig = 0;
2017 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02002018 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002019 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02002020
2021 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
2022
2023 memset(&shm, 0, sizeof(shm));
2024 shm.flags = TEEC_MEM_INPUT;
2025 shm.buffer = NULL;
2026 shm.size = 0;
2027
2028 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2029 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2030
2031 memset(&shm, 0, sizeof(shm));
2032 shm.flags = TEEC_MEM_OUTPUT;
2033 shm.buffer = NULL;
2034 shm.size = 0;
2035
2036 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2037 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2038
2039 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002040
Julianus Larsona79e6ee2023-05-17 11:35:03 +02002041 if (!xtest_teec_ctx.imp.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02002042 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02002043 return;
2044 }
2045
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002046 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2047 xtest_teec_open_session(&session,
2048 &os_test_ta_uuid,
2049 NULL, &ret_orig)))
2050 return;
2051
2052 empty_buf = malloc(1);
2053 if (!empty_buf) {
2054 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002055 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002056 }
2057
2058 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2059 TEEC_MEMREF_TEMP_INPUT,
2060 TEEC_MEMREF_TEMP_OUTPUT,
2061 TEEC_MEMREF_TEMP_OUTPUT);
2062
2063 Do_ADBG_BeginSubCase(c,
2064 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2065
2066 op.params[0].tmpref.buffer = empty_buf;
2067 op.params[0].tmpref.size = 0;
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_SUCCESS,
2079 TEEC_InvokeCommand(&session,
2080 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2081 &ret_orig));
2082
2083 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2084
2085 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2086
2087 op.params[0].tmpref.buffer = empty_buf;
2088 op.params[0].tmpref.size = 1;
2089
2090 op.params[1].tmpref.buffer = NULL;
2091 op.params[1].tmpref.size = 0;
2092
2093 op.params[2].tmpref.buffer = empty_buf;
2094 op.params[2].tmpref.size = 0;
2095
2096 op.params[3].tmpref.buffer = NULL;
2097 op.params[3].tmpref.size = 0;
2098
2099 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2100 TEEC_InvokeCommand(&session,
2101 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2102 &ret_orig));
2103
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002104 TEEC_CloseSession(&session);
2105
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002106 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2107
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002108 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2109
2110 /* Pseudo TA is optional: warn and nicely exit if not found */
2111 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2112 &ret_orig);
2113 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2114 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2115 goto out;
2116 }
2117 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2118 goto out;
2119
2120 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2121 TEEC_NONE, TEEC_NONE);
2122 op.params[0].tmpref.buffer = NULL;
2123 op.params[0].tmpref.size = 0;
2124
2125 ADBG_EXPECT(c, TEE_SUCCESS,
2126 TEEC_InvokeCommand(&session,
2127 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2128 &op, &ret_orig));
2129
2130out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002131 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002132out:
2133 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002134 free(empty_buf);
2135}
2136ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2137 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002138
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002139#define TEE_UUID_NS_NAME_SIZE 128
2140
2141/*
2142 * TEE Client UUID name space identifier (UUIDv4)
2143 *
2144 * Value here is random UUID that is allocated as name space identifier for
2145 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2146 */
2147static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2148
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002149/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2150static TEEC_UUID client_uuid_public = { };
2151
2152static void xtest_tee_test_1026(ADBG_Case_t *c)
2153{
2154 TEEC_Result result = TEEC_ERROR_GENERIC;
2155 uint32_t ret_orig = 0;
2156 TEEC_Session session = { };
2157 uint32_t login = UINT32_MAX;
2158 TEEC_UUID client_uuid = { };
2159
2160 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2161 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2162
2163 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2164 return;
2165
2166 result = ta_os_test_cmd_client_identity(&session, &login,
2167 &client_uuid);
2168
2169 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2170 goto out;
2171
2172 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2173
2174 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2175 sizeof(TEEC_UUID));
2176
2177out:
2178 TEEC_CloseSession(&session);
2179}
2180
2181ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2182 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002183
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002184/*
2185 * regression_1027
2186 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002187 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2188 * login client UUID generation")
2189 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002190 *
2191 * xtest skips the test when not built with OpenSSL.
2192 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002193static void xtest_tee_test_1027(ADBG_Case_t *c)
2194{
Victor Chong8e070bc2020-05-13 09:59:33 +01002195#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002196 TEEC_Result result = TEEC_ERROR_GENERIC;
2197 uint32_t ret_orig = 0;
2198 TEEC_Session session = { };
2199 uint32_t login = UINT32_MAX;
2200 TEEC_UUID client_uuid = { };
2201 TEEC_UUID expected_client_uuid = { };
2202 TEEC_UUID uuid_ns = { };
2203 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2204
2205 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2206
2207 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2208 return;
2209
2210 sprintf(uuid_name, "uid=%x", geteuid());
2211
2212 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2213 strlen(uuid_name));
2214 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2215 return;
2216
2217 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2218 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2219
2220 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2221 return;
2222
2223 result = ta_os_test_cmd_client_identity(&session, &login,
2224 &client_uuid);
2225
2226 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2227 goto out;
2228
2229 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2230
2231 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2232 sizeof(TEEC_UUID));
2233
2234out:
2235 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002236#else /*!OPENSSL_FOUND*/
2237 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002238 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002239 /* xtest_uuid_v5() depends on OpenSSL */
2240 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2241#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002242}
2243
2244ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2245 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002246
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002247/*
2248 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002249 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2250 * login client UUID generation")
2251 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002252 *
2253 * xtest skips the test when not built with OpenSSL.
2254 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002255static void xtest_tee_test_1028(ADBG_Case_t *c)
2256{
Victor Chong8e070bc2020-05-13 09:59:33 +01002257#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002258 TEEC_Result result = TEEC_ERROR_GENERIC;
2259 uint32_t ret_orig = 0;
2260 TEEC_Session session = { };
2261 uint32_t login = UINT32_MAX;
2262 TEEC_UUID client_uuid = { };
2263 TEEC_UUID expected_client_uuid = { };
2264 TEEC_UUID uuid_ns = { };
2265 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2266 uint32_t group = 0;
2267
2268 group = getegid();
2269
2270 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2271
2272 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2273 return;
2274
2275 sprintf(uuid_name, "gid=%x", group);
2276
2277 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2278 strlen(uuid_name));
2279 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2280 return;
2281
2282 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2283 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2284
2285 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2286 return;
2287
2288 result = ta_os_test_cmd_client_identity(&session, &login,
2289 &client_uuid);
2290
2291 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2292 goto out;
2293
2294 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2295
2296 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2297 sizeof(TEEC_UUID));
2298
2299out:
2300 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002301#else /*!OPENSSL_FOUND*/
2302 UNUSED(c);
2303 /* xtest_uuid_v5() depends on OpenSSL */
2304 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2305#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002306}
2307
2308ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2309 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002310
2311static void xtest_tee_test_1029(ADBG_Case_t *c)
2312{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002313 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002314 TEEC_Session session = { 0 };
2315 uint32_t ret_orig = 0;
2316
2317 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2318 xtest_teec_open_session(&session, &os_test_ta_uuid,
2319 NULL, &ret_orig)))
2320 return;
2321
2322 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002323 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2324 &ret_orig);
2325 if (res == TEEC_ERROR_NOT_SUPPORTED)
2326 Do_ADBG_Log(" - 1029 - skip test, "
2327 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2328 else
2329 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002330 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2331
2332 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002333 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2334 &ret_orig);
2335 if (res == TEEC_ERROR_NOT_SUPPORTED)
2336 Do_ADBG_Log(" - 1029 - skip test, "
2337 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2338 else
2339 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002340 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2341
2342 TEEC_CloseSession(&session);
2343}
2344ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2345 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002346
2347static void xtest_tee_test_1030(ADBG_Case_t *c)
2348{
2349 TEEC_Session session = { 0 };
2350 uint32_t ret_orig = 0;
2351
2352 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2353 xtest_teec_open_session(&session, &os_test_ta_uuid,
2354 NULL, &ret_orig)))
2355 return;
2356
2357 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2358 ADBG_EXPECT_TEEC_SUCCESS(c,
2359 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2360 &ret_orig));
2361 Do_ADBG_EndSubCase(c, "Before dlopen()");
2362
2363 Do_ADBG_BeginSubCase(c, "After dlopen()");
2364 ADBG_EXPECT_TEEC_SUCCESS(c,
2365 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2366 &ret_orig));
2367 Do_ADBG_EndSubCase(c, "After dlopen()");
2368
2369 TEEC_CloseSession(&session);
2370}
2371ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2372 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002373
2374#ifndef __clang__
2375static void xtest_tee_test_1031(ADBG_Case_t *c)
2376{
2377 TEEC_Result ret = TEE_SUCCESS;
2378 TEEC_Session session = { 0 };
2379 uint32_t ret_orig = 0;
2380
2381 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2382 xtest_teec_open_session(&session, &os_test_ta_uuid,
2383 NULL, &ret_orig)))
2384 return;
2385
2386 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2387 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2388 &ret_orig);
2389 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2390 printf("TA not built with C++ support, skipping C++ tests\n");
2391 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2392 goto out;
2393
2394 }
2395 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2396 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2397
2398 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2399 ADBG_EXPECT_TEEC_SUCCESS(c,
2400 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2401 NULL, &ret_orig));
2402 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2403
2404 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2405 ADBG_EXPECT_TEEC_SUCCESS(c,
2406 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2407 NULL, &ret_orig));
2408 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2409
2410 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2411 ADBG_EXPECT_TEEC_SUCCESS(c,
2412 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2413 &ret_orig));
2414 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2415
2416 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2417 ADBG_EXPECT_TEEC_SUCCESS(c,
2418 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2419 &ret_orig));
2420 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2421out:
2422 TEEC_CloseSession(&session);
2423}
2424ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2425 "Test C++ features");
2426#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002427
2428static void xtest_tee_test_1032(ADBG_Case_t *c)
2429{
2430 TEEC_Result res = TEEC_SUCCESS;
2431 TEEC_Context ctx = { };
2432 TEEC_SharedMemory shm1 = {
2433 .buffer = xtest_tee_test_1032,
2434 .size = 32,
2435 .flags = TEEC_MEM_INPUT,
2436 };
2437 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2438 TEEC_SharedMemory shm2 = {
2439 .buffer = (void *)dummy_data,
2440 .size = sizeof(dummy_data),
2441 .flags = TEEC_MEM_INPUT,
2442 };
2443
2444 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2445 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2446 return;
2447
2448 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2449 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2450 TEEC_ReleaseSharedMemory(&shm1);
2451
2452 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2453 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2454 TEEC_ReleaseSharedMemory(&shm2);
2455
2456 TEEC_FinalizeContext(&ctx);
2457}
2458ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2459 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002460
2461static void xtest_tee_test_1033(ADBG_Case_t *c)
2462{
2463 TEEC_Session session = { };
2464 uint32_t ret_orig = 0;
2465
2466 /* TA will ping the test plugin during open session operation */
2467 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2468 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2469 NULL, &ret_orig)))
2470 return;
2471
2472 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2473 {
2474 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2475
2476 op.params[0].value.a = 20;
2477 op.params[0].value.b = 10;
2478 op.params[1].value.a = '+';
2479 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2480 TEEC_VALUE_INPUT, TEEC_NONE,
2481 TEEC_NONE);
2482
2483 ADBG_EXPECT_TEEC_SUCCESS(c,
2484 TEEC_InvokeCommand(&session,
2485 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2486 &ret_orig));
2487 ADBG_EXPECT(c, 30, op.params[0].value.a);
2488
2489 /* reassign, because the values was changed during previous op */
2490 op.params[0].value.a = 20;
2491 op.params[0].value.b = 10;
2492 op.params[1].value.a = '-';
2493 ADBG_EXPECT_TEEC_SUCCESS(c,
2494 TEEC_InvokeCommand(&session,
2495 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2496 &ret_orig));
2497 ADBG_EXPECT(c, 10, op.params[0].value.a);
2498 }
2499 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2500
2501 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2502 {
2503 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002504 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002505
2506 op.params[0].tmpref.buffer = to_plugin;
2507 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002508 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002509 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002510 TEEC_NONE, TEEC_NONE);
2511
2512 ADBG_EXPECT_TEEC_SUCCESS(c,
2513 TEEC_InvokeCommand(&session,
2514 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2515 &op, &ret_orig));
2516
2517 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002518 * The test plugin must calculate a sum of the input elements
2519 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002520 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002521 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002522 }
2523 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2524
2525 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2526 {
2527 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2528 char from_plugin[64] = { };
2529 char expected_arr[] = "Array from plugin";
2530 size_t expectes_size = sizeof(expected_arr);
2531
2532 op.params[0].tmpref.buffer = from_plugin;
2533 op.params[0].tmpref.size = sizeof(from_plugin);
2534 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2535 TEEC_VALUE_OUTPUT, TEEC_NONE,
2536 TEEC_NONE);
2537 ADBG_EXPECT_TEEC_SUCCESS(c,
2538 TEEC_InvokeCommand(&session,
2539 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2540 &ret_orig));
2541 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2542 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2543 }
2544 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2545
2546 Do_ADBG_BeginSubCase(c, "Not allow bad input to a 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, TEE_ERROR_BAD_PARAMETERS,
2553 TEEC_InvokeCommand(&session,
2554 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2555 &ret_orig));
2556 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2557 TEEC_InvokeCommand(&session,
2558 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2559 &ret_orig));
2560 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2561 TEEC_InvokeCommand(&session,
2562 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2563 &ret_orig));
2564 }
2565 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2566
2567 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2568 {
2569 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2570
2571 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
Vincent Mailhol2ea0aa72023-11-16 20:20:54 +09002572 TEEC_NONE, TEEC_NONE);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002573 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2574 TEEC_InvokeCommand(&session,
2575 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2576 &op, &ret_orig));
2577 }
2578 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2579
2580 TEEC_CloseSession(&session);
2581}
2582ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2583 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002584
2585static void xtest_tee_test_1034(ADBG_Case_t *c)
2586{
2587 TEEC_Result res = TEEC_SUCCESS;
2588 TEEC_Session session = { };
2589 uint32_t ret_orig = 0;
2590
2591 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2592 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002593 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2594 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
Tim Lee5502a992023-07-12 16:48:47 +08002595 return;
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002596 }
Tim Lee5502a992023-07-12 16:48:47 +08002597 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2598 if (!res)
2599 TEEC_CloseSession(&session);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002600}
2601ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2602 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302603
2604#if defined(CFG_TA_BTI)
2605struct bti_test {
2606 uint32_t cmd;
2607 uint32_t func;
2608};
2609
2610#define BTI_TEST(caller_func, bti_func) { \
2611 .cmd = caller_func, \
2612 .func = bti_func, \
2613 }
2614
2615static const struct bti_test bti_cases_success[] = {
2616 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2617 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2618 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2619 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2620 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2621 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2622 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2623};
2624
2625static const struct bti_test bti_cases_panic[] = {
2626 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2627 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2628 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2629 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2630 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2631};
2632
2633static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2634{
2635 TEEC_Session session = {};
2636 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2637 uint32_t ret_orig = 0;
2638 TEEC_Result res;
2639
2640 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2641 TEEC_NONE);
2642 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2643 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2644 NULL, &ret_orig)))
2645 return;
2646
2647 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2648 &ret_orig);
2649 if (!res) {
2650 if(op.params[0].value.a)
2651 *bti = true;
2652 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2653 }
2654
2655 TEEC_CloseSession(&session);
2656}
2657
2658static void xtest_tee_test_1035(ADBG_Case_t *c)
2659{
2660 TEEC_Session session = {};
2661 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2662 struct bti_test const *test = NULL;
2663 uint32_t ret_orig = 0;
2664 TEEC_Result res;
2665 unsigned int n = 0;
2666 bool cpu_feature_bti = false;
2667
2668 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2669 get_cpu_feature(c, &cpu_feature_bti);
2670 Do_ADBG_EndSubCase(c, "BTI Implemented");
2671
2672 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2673 TEEC_NONE);
2674
2675 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2676 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2677 NULL, &ret_orig)))
2678 return;
2679
2680 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2681 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2682 test = &bti_cases_success[n];
2683
2684 op.params[0].value.a = test->func;
2685
2686 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2687 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2688 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2689 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2690 TEEC_CloseSession(&session);
2691 return;
2692 }
2693
2694 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2695
2696 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2697
2698 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2699 }
2700 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2701
2702 TEEC_CloseSession(&session);
2703
2704 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2705 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2706 test = &bti_cases_panic[n];
2707 res = TEEC_SUCCESS;
2708
2709 if (cpu_feature_bti)
2710 res = TEEC_ERROR_TARGET_DEAD;
2711
2712 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2713 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2714 NULL, &ret_orig)))
2715 goto out;
2716
2717 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2718 op.params[0].value.a = test->func;
2719
2720 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2721 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2722
2723 if (cpu_feature_bti)
2724 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2725 ret_orig);
2726
2727 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2728
2729 TEEC_CloseSession(&session);
2730 }
2731
2732out:
2733 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2734}
2735ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2736#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302737
2738static void xtest_tee_test_1036(ADBG_Case_t *c)
2739{
2740 TEEC_Session session = { };
2741 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2742 uint32_t ret_orig = 0;
2743 TEEC_Result res = TEEC_SUCCESS;
2744
2745 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2746 TEEC_NONE);
2747
2748 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2749 xtest_teec_open_session(&session, &os_test_ta_uuid,
2750 NULL, &ret_orig)))
2751 return;
2752
2753 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2754
2755 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2756 &ret_orig);
2757 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2758 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2759 goto out;
2760 }
2761
2762 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2763 goto out;
2764
2765 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2766
2767 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2768
2769 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2770 TEEC_InvokeCommand(&session,
2771 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2772 &op, &ret_orig));
2773
2774 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2775
2776 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2777
2778out:
2779 TEEC_CloseSession(&session);
2780}
2781ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2782 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002783
2784#define ATT_MAX_KEYSZ 4096
2785
2786#ifdef OPENSSL_FOUND
2787static RSA *att_key;
2788static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2789
2790/*
2791 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2792 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2793 */
2794static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2795 uint8_t *buf)
2796{
2797 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2798 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2799 unsigned char *ta_hash = buf;
2800 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2801 SHA256_CTX ctx = { };
2802 int salt_len = 32; /* Hard-coded in the PTA */
2803 int st = 0;
2804
2805 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2806 return;
2807
2808 SHA256_Init(&ctx);
2809 SHA256_Update(&ctx, nonce, nonce_size);
2810 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2811 SHA256_Final(digest, &ctx);
2812
2813 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2814 RSA_NO_PADDING);
2815 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2816 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2817 EVP_sha256(), decr, salt_len);
2818 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2819}
2820
2821static void free_att_key(void)
2822{
2823 RSA_free(att_key);
2824 att_key = NULL;
2825}
2826
Clement Faure44a31d02022-03-30 10:50:52 +02002827#if OPENSSL_VERSION_NUMBER < 0x10100000L
2828static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2829{
2830 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2831 return 0;
2832
2833 if (n != NULL) {
2834 BN_free(r->n);
2835 r->n = n;
2836 }
2837
2838 if (e != NULL) {
2839 BN_free(r->e);
2840 r->e = e;
2841 }
2842
2843 if (d != NULL) {
2844 BN_free(r->d);
2845 r->d = d;
2846 }
2847
2848 return 1;
2849}
2850#endif
2851
Jerome Forissier0915b222021-10-27 18:20:59 +02002852static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2853 size_t n_sz)
2854{
2855 BIGNUM *bn_e = NULL;
2856 BIGNUM *bn_n = NULL;
2857 int st = 0;
2858
2859 att_key_size = n_sz;
2860 att_key = RSA_new();
2861 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2862 return;
2863
2864 bn_e = BN_bin2bn(e, e_sz, NULL);
2865 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2866 goto err;
2867 bn_n = BN_bin2bn(n, n_sz, NULL);
2868 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2869 goto err;
2870
2871 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2872 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2873 goto err;
2874 return;
2875err:
2876 free_att_key();
2877}
2878#else
2879#define check_signature(...)
2880#define set_att_key(...)
2881#define free_att_key()
2882#endif
2883
2884/*
2885 * Verification of the output of the attestation PTA
2886 * - (If hash != NULL) check buf contains the expected hash
2887 * - (If OpenSSL is available) Check that the signature is valid
2888 */
2889static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2890 uint8_t *hash, uint8_t *buf)
2891{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002892 (void)nonce;
2893 (void)nonce_size;
2894
Jerome Forissier0915b222021-10-27 18:20:59 +02002895 if (hash)
2896 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2897
2898 check_signature(c, nonce, nonce_size, buf);
2899}
2900
2901/* Invoke attestation PTA to return the public key */
2902static void get_att_public_key(ADBG_Case_t *c)
2903{
2904 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2905 uint8_t e[3] = { }; /* We know e == 65537... */
2906 size_t n_sz = sizeof(n);
2907 size_t e_sz = sizeof(e);
2908 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2909 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2910 TEEC_Result res = TEEC_ERROR_GENERIC;
2911 TEEC_Session session = { };
2912 uint32_t ret_orig = 0;
2913
2914 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2915 TEEC_MEMREF_TEMP_OUTPUT,
2916 TEEC_VALUE_OUTPUT, TEEC_NONE);
2917 op.params[0].tmpref.buffer = e;
2918 op.params[0].tmpref.size = e_sz;
2919 op.params[1].tmpref.buffer = n;
2920 op.params[1].tmpref.size = n_sz;
2921
2922 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2923 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2924 return;
2925
2926 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2927 &ret_orig);
2928
2929 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2930 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2931 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2932 goto out;
2933
2934 e_sz = op.params[0].tmpref.size;
2935 n_sz = op.params[1].tmpref.size;
2936 set_att_key(c, e, e_sz, n, n_sz);
2937out:
2938 TEEC_CloseSession(&session);
2939}
2940
2941/* Invoke attestation PTA to hash the TEE binary */
2942static void attestation_tee(ADBG_Case_t *c)
2943{
2944 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2945 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2946 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2947 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2948 TEEC_Result res = TEEC_ERROR_GENERIC;
2949 TEEC_Session session = { };
2950 uint32_t ret_orig = 0;
2951
2952 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2953 TEEC_MEMREF_TEMP_OUTPUT,
2954 TEEC_NONE, TEEC_NONE);
2955 op.params[0].tmpref.buffer = nonce;
2956 op.params[0].tmpref.size = sizeof(nonce);
2957 op.params[1].tmpref.buffer = measurement;
2958 op.params[1].tmpref.size = sizeof(measurement);
2959
2960 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2961 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2962 return;
2963
2964 /* Hash TEE and check signature */
2965 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2966 &ret_orig);
2967 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2968 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2969
2970 TEEC_CloseSession(&session);
2971}
2972
2973/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2974static void attestation_ta_shdr(ADBG_Case_t *c)
2975{
2976 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2977 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2978 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2979 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2980 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2981 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2982 TEEC_Result res = TEEC_ERROR_GENERIC;
2983 TEEC_Session session = { };
2984 uint32_t ret_orig = 0;
2985 int cmp = 0;
2986
2987 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2988 TEEC_MEMREF_TEMP_INPUT,
2989 TEEC_MEMREF_TEMP_OUTPUT,
2990 TEEC_NONE);
2991 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2992 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2993 op.params[1].tmpref.buffer = nonce;
2994 op.params[1].tmpref.size = sizeof(nonce);
2995 op.params[2].tmpref.buffer = measurement;
2996 op.params[2].tmpref.size = sizeof(measurement);
2997
2998 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2999 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3000 return;
3001
3002 /* Hash TA and check signature */
3003 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3004 &op, &ret_orig);
3005 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3006 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3007 /* Save hash */
3008 memcpy(hash1, measurement, 32);
3009
3010 /* Hash TA again */
3011 memset(measurement, 0, sizeof(measurement));
3012 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3013 &op, &ret_orig);
3014 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3015 /* New hash should be identical */
3016 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3017
3018 /* Hash another TA */
3019 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
3020 memset(measurement, 0, sizeof(measurement));
3021 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3022 &op, &ret_orig);
3023 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3024 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3025 memcpy(hash2, measurement, 32);
3026 /* Different binaries should have different hashes */
3027 cmp = memcmp(hash1, hash2, sizeof(hash1));
3028 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3029
3030 TEEC_CloseSession(&session);
3031}
3032
3033/*
3034 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
3035 * itself.
3036 */
3037static void attestation_ta_memory(ADBG_Case_t *c)
3038{
3039 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3040 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
3041 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
3042 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
3043 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
3044 TEEC_Result res = TEEC_ERROR_GENERIC;
3045 TEEC_Session session = { };
3046 uint32_t ret_orig = 0;
3047 int cmp = 0;
3048
3049 Do_ADBG_BeginSubCase(c, "Consecutive calls");
3050
3051 /* Open session to os_test TA */
3052 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3053 &ret_orig);
3054 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3055 return;
3056
3057 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3058 TEEC_MEMREF_TEMP_OUTPUT,
3059 TEEC_NONE, TEEC_NONE);
3060 op.params[0].tmpref.buffer = nonce;
3061 op.params[0].tmpref.size = sizeof(nonce);
3062 op.params[1].tmpref.buffer = measurement;
3063 op.params[1].tmpref.size = sizeof(measurement);
3064
3065 /* Hash TA */
3066 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3067 &ret_orig);
3068 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3069
3070 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3071 memcpy(hash1, measurement, 32);
3072
3073 /* Hash TA again */
3074 memset(measurement, 0, sizeof(measurement));
3075 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3076 &ret_orig);
3077 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3078
3079 /* New hash should be identical to hash1 */
3080 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3081
3082 Do_ADBG_EndSubCase(c, "Consecutive calls");
3083
3084 /* Close TA session, will cause unload of TA */
3085 TEEC_CloseSession(&session);
3086
3087 Do_ADBG_BeginSubCase(c, "TA reload");
3088
3089 /* Load TA again and open a new session */
3090 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3091 &ret_orig);
3092 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3093 if (res)
3094 return;
3095
3096 /* Hash TA one more 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 /* Hash after reload should still be the same */
3103 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3104
3105 Do_ADBG_EndSubCase(c, "TA reload");
3106
3107 Do_ADBG_BeginSubCase(c, "Add shared library");
3108
3109 /*
3110 * Invoke a TA command that causes some additional code to be mapped
3111 * (shared library)
3112 */
3113 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3114 &ret_orig);
3115 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3116
3117 /* Hash TA one last time */
3118 memset(measurement, 0, sizeof(measurement));
3119 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3120 &ret_orig);
3121 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3122
3123 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3124 memcpy(hash2, measurement, 32);
3125
3126 /* Different binaries mapped mean different hashes */
3127 cmp = memcmp(hash1, hash2, sizeof(hash1));
3128 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3129
3130 Do_ADBG_EndSubCase(c, "Add shared library");
3131
3132 TEEC_CloseSession(&session);
3133}
3134
3135static void xtest_tee_test_1037(ADBG_Case_t *c)
3136{
3137 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3138 TEEC_Result res = TEEC_ERROR_GENERIC;
3139 TEEC_Session session = { };
3140 uint32_t ret_orig = 0;
3141
3142 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3143 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3144 Do_ADBG_Log(" skip test, pseudo TA not found");
3145 return;
3146 }
3147
chenchaokaiaf751932023-05-29 14:25:50 +08003148 TEEC_CloseSession(&session);
3149
Jerome Forissier0915b222021-10-27 18:20:59 +02003150 Do_ADBG_BeginSubCase(c, "Get public key");
3151 get_att_public_key(c);
3152 Do_ADBG_EndSubCase(c, "Get public key");
3153
3154 Do_ADBG_BeginSubCase(c, "TEE attestation");
3155 attestation_tee(c);
3156 Do_ADBG_EndSubCase(c, "TEE attestation");
3157
3158 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3159 attestation_ta_shdr(c);
3160 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3161
3162 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3163 attestation_ta_memory(c);
3164 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3165
3166 free_att_key();
3167}
3168ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3169 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003170
3171static void xtest_tee_test_1038(ADBG_Case_t *c)
3172{
3173 TEEC_Session session = { };
3174 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3175 uint32_t ret_orig = 0;
3176 TEEC_Result res = TEEC_SUCCESS;
3177
3178 Do_ADBG_BeginSubCase(c, "MTE use after free");
3179
3180 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3181 TEEC_NONE);
3182
3183 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3184 xtest_teec_open_session(&session, &os_test_ta_uuid,
3185 NULL, &ret_orig)))
3186 return;
3187
3188 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3189 &op, &ret_orig);
3190 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3191 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3192 goto out;
3193 }
3194
3195 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3196 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3197 TEEC_CloseSession(&session);
3198
3199 Do_ADBG_EndSubCase(c, "MTE use after free");
3200
3201 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3202
3203 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3204 TEEC_NONE);
3205
3206 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3207 xtest_teec_open_session(&session, &os_test_ta_uuid,
3208 NULL, &ret_orig)))
3209 return;
3210
3211 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3212 &op, &ret_orig);
3213 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3214 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3215 goto out;
3216 }
3217
3218 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3219 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3220
3221 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3222
3223 Do_ADBG_BeginSubCase(c, "MTE double free");
3224
3225 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3226 TEEC_NONE);
3227
3228 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3229 xtest_teec_open_session(&session, &os_test_ta_uuid,
3230 NULL, &ret_orig)))
3231 return;
3232
3233 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3234 &op, &ret_orig);
3235 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3236 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3237 goto out;
3238 }
3239
3240 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3241 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3242
3243 Do_ADBG_EndSubCase(c, "MTE double free");
3244
3245 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3246
3247 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3248 TEEC_NONE);
3249
3250 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3251 xtest_teec_open_session(&session, &os_test_ta_uuid,
3252 NULL, &ret_orig)))
3253 return;
3254
3255 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3256 &op, &ret_orig);
3257 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3258 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3259 goto out;
3260 }
3261
3262 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3263 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3264
3265 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3266
3267
3268out:
3269 TEEC_CloseSession(&session);
3270}
3271ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3272 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003273
3274static void xtest_tee_test_1039(ADBG_Case_t *c)
3275{
3276 TEEC_Session session = { };
3277 uint32_t ret_orig = 0;
3278
3279 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3280 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3281 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3282 NULL, &ret_orig)))
3283 TEEC_CloseSession(&session);
3284 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3285
3286 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3287 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3288 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3289 NULL, &ret_orig)))
3290 TEEC_CloseSession(&session);
3291 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3292
3293}
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003294ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3295 "Test subkey verification");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003296
3297struct test_1040_thread_arg {
3298 TEEC_Result res;
3299 pthread_t thr;
3300};
3301
3302static void *test_1040_thread(void *arg)
3303{
3304 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3305 struct test_1040_thread_arg *a = arg;
3306 TEEC_Result res = TEEC_SUCCESS;
3307 uint32_t err_orig = 0;
3308 TEEC_Session session = { };
3309 size_t loop_count = 100;
3310 size_t n = 0;
3311
3312 if (level == 0)
3313 loop_count /= 2;
3314
3315 while (n < loop_count) {
3316 res = xtest_teec_open_session(&session, &sims_test_ta_uuid,
3317 NULL, &err_orig);
3318 if (res) {
3319 if (res == TEEC_ERROR_TARGET_DEAD)
3320 continue;
3321 a->res = res;
3322 return NULL;
3323 }
3324
3325 memset(&op, 0, sizeof(op));
3326 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3327 TEEC_NONE, TEEC_NONE,
3328 TEEC_NONE);
3329 res = TEEC_InvokeCommand(&session, TA_SIMS_CMD_PANIC, &op,
3330 &err_orig);
3331 TEEC_CloseSession(&session);
3332 if (res != TEEC_ERROR_TARGET_DEAD) {
3333 if (res)
3334 a->res = res;
3335 else
3336 a->res = TEEC_ERROR_GENERIC;
3337 return NULL;
3338 }
3339 n++;
3340 }
3341 a->res = TEEC_SUCCESS;
3342 return NULL;
3343}
3344
3345static void xtest_tee_test_1040(ADBG_Case_t *c)
3346{
3347 struct test_1040_thread_arg arg[NUM_THREADS] = { };
3348 size_t nt = NUM_THREADS;
3349 size_t n = 0;
3350
3351 Do_ADBG_BeginSubCase(c, "Concurent invoke with panic in TA");
3352 for (n = 0; n < nt; n++) {
3353 if (!ADBG_EXPECT(c, 0, pthread_create(&arg[n].thr, NULL,
3354 test_1040_thread,
3355 arg + n)))
3356 nt = n; /* break loop and start cleanup */
3357 }
3358 for (n = 0; n < nt; n++) {
3359 ADBG_EXPECT(c, 0, pthread_join(arg[n].thr, NULL));
3360 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
3361 }
3362 Do_ADBG_EndSubCase(c, "Concurent invoke with panic in TA");
3363}
3364ADBG_CASE_DEFINE(regression, 1040, xtest_tee_test_1040,
3365 "Test panic in concurrent open/invoke/close session");
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003366
3367static void xtest_tee_test_1041(ADBG_Case_t *c)
3368{
3369 const char fname[] = "/dev/tpm0";
3370 TEE_Result res = TEEC_SUCCESS;
3371 TEEC_Session sess = { };
3372 uint32_t ret_orig = 0;
3373 struct stat sb = { };
Jerome Forissiera1739a12025-01-15 10:10:36 +01003374 bool found = false;
Jens Wiklander237d2782025-02-04 14:43:20 +01003375 int max_wait = 20;
Jerome Forissiera1739a12025-01-15 10:10:36 +01003376 int i = 0;
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003377
3378 res = xtest_teec_open_session(&sess, &(const TEEC_UUID)TA_FTPM_UUID,
3379 NULL, &ret_orig);
3380 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3381 Do_ADBG_Log("skip test, fTPM TA not present");
3382 return;
3383 }
3384 if (res != TEEC_ERROR_BUSY && ADBG_EXPECT_TEEC_SUCCESS(c, res))
3385 TEEC_CloseSession(&sess);
3386
Jerome Forissiera1739a12025-01-15 10:10:36 +01003387 /*
3388 * Check for the presence of the TPM device. Give the kernel some time
3389 * (5 seconds) otherwise the test may fail if run immediately after
3390 * boot.
3391 */
Jens Wiklander237d2782025-02-04 14:43:20 +01003392 for (i = 0; i < max_wait; i++) {
Jerome Forissiera1739a12025-01-15 10:10:36 +01003393 if (stat(fname, &sb) == 0) {
3394 found = true;
3395 break;
3396 }
Jens Wiklander237d2782025-02-04 14:43:20 +01003397 Do_ADBG_Log("Waiting for TPM device %d / %d", i, max_wait);
Jerome Forissiera1739a12025-01-15 10:10:36 +01003398 sleep(1);
3399 }
3400 if (!ADBG_EXPECT_TRUE(c, found)) {
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003401 Do_ADBG_Log("stat(\"%s\"): %s", fname, strerror(errno));
3402 if (res != TEEC_ERROR_BUSY)
3403 Do_ADBG_Log("Perhaps fTPM hasn't finished probing");
3404 return;
3405 }
3406
3407 if (!ADBG_EXPECT_TRUE(c, S_ISCHR(sb.st_mode)))
3408 Do_ADBG_Log("Expected \"%s\" to be a character device", fname);
3409}
3410ADBG_CASE_DEFINE(regression, 1041, xtest_tee_test_1041, "Test fTPM sanity");