blob: 272ab9332ae9abfd874f5a81cfd1f1217d1feb9a [file] [log] [blame]
Etienne Carriere9b7b70d2020-05-16 10:27:23 +02001// SPDX-License-Identifier: GPL-2.0
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00003 * Copyright (c) 2020, ARM Limited. All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02004 * Copyright (c) 2014, STMicroelectronics International N.V.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03007#include <errno.h>
Etienne Carrierea4653552017-01-11 10:04:24 +01008#include <limits.h>
Jerome Forissier0915b222021-10-27 18:20:59 +02009#ifdef OPENSSL_FOUND
10#include <openssl/bn.h>
11#include <openssl/err.h>
12#include <openssl/evp.h>
13#include <openssl/rsa.h>
14#include <openssl/sha.h>
15#endif
16#include <pta_attestation.h>
17#include <pta_invoke_tests.h>
18#include <pta_secstor_ta_mgmt.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010019#include <pthread.h>
Jerome Forissier03aa2792023-02-07 14:22:24 +010020#ifdef CFG_SECURE_DATA_PATH
Jerome Forissier0915b222021-10-27 18:20:59 +020021#include <sdp_basic.h>
Jerome Forissier03aa2792023-02-07 14:22:24 +010022#endif
Jerome Forissier0915b222021-10-27 18:20:59 +020023#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020024#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010025#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020026#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060027#include <sys/stat.h>
28#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020029#include <ta_arm_bti.h>
30#include <ta_concurrent.h>
31#include <ta_create_fail_test.h>
32#include <ta_crypt.h>
33#include <ta_miss_test.h>
34#include <ta_os_test.h>
35#include <ta_rpc_test.h>
36#include <ta_sims_keepalive_test.h>
37#include <ta_sims_test.h>
38#include <ta_supp_plugin.h>
39#include <ta_tpm_log_test.h>
40#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010041#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020042#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010043#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020044
Jerome Forissier0915b222021-10-27 18:20:59 +020045#include "xtest_helpers.h"
46#include "xtest_test.h"
47#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030048
Jens Wiklanderec545fb2017-11-24 16:58:07 +010049#ifndef MIN
50#define MIN(a, b) ((a) < (b) ? (a) : (b))
51#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020052
Etienne Carrierec0a61722023-03-07 17:47:42 +010053#define STATS_UUID \
54 { 0xd96a5b40, 0xe2c7, 0xb1af, \
55 { 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
56
57#define STATS_CMD_PAGER_STATS 0
58
59#define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096)
60
Pascal Brandc639ac82015-07-02 08:53:34 +020061struct xtest_crypto_session {
62 ADBG_Case_t *c;
63 TEEC_Session *session;
64 uint32_t cmd_id_sha256;
65 uint32_t cmd_id_aes256ecb_encrypt;
66 uint32_t cmd_id_aes256ecb_decrypt;
67};
68
Etienne Carrierec0a61722023-03-07 17:47:42 +010069static bool optee_pager_with_small_pool(void)
70{
71 TEEC_Result res = TEEC_ERROR_GENERIC;
72 TEEC_UUID uuid = STATS_UUID;
73 TEEC_Context ctx = { };
74 TEEC_Session sess = { };
75 TEEC_Operation op = { };
76 uint32_t eo = 0;
77 bool rc = false;
78
79 res = TEEC_InitializeContext(NULL, &ctx);
80 if (res)
81 return false;
82
83 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
84 NULL, &eo);
85 if (res)
86 goto out_ctx;
87
88 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_VALUE_OUTPUT,
89 TEEC_VALUE_OUTPUT, TEEC_NONE);
90 res = TEEC_InvokeCommand(&sess, STATS_CMD_PAGER_STATS, &op, &eo);
91 if (res)
92 goto out_sess;
93
94 if (op.params[0].value.b &&
95 op.params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD)
96 rc = true;
97
98out_sess:
99 TEEC_CloseSession(&sess);
100out_ctx:
101 TEEC_FinalizeContext(&ctx);
102
103 return rc;
104}
105
Pascal Brandc639ac82015-07-02 08:53:34 +0200106static void xtest_crypto_test(struct xtest_crypto_session *cs)
107{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100108 uint32_t ret_orig = 0;
109 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200110 uint8_t crypt_in[16] = { 22, 17 };
111
112 crypt_in[15] = 60;
113
114 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
115 {
116 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
117
118 op.params[0].tmpref.buffer = crypt_in;
119 op.params[0].tmpref.size = sizeof(crypt_in);
120 op.params[1].tmpref.buffer = crypt_out;
121 op.params[1].tmpref.size = sizeof(crypt_out);
122 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
123 TEEC_MEMREF_TEMP_OUTPUT,
124 TEEC_NONE, TEEC_NONE);
125
126 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
127 TEEC_InvokeCommand(cs->session,
128 cs->
129 cmd_id_aes256ecb_encrypt,
130 &op,
131 &ret_orig));
132 }
133 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
134
135 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
136 {
137 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100138 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200139
140 op.params[0].tmpref.buffer = crypt_out;
141 op.params[0].tmpref.size = sizeof(crypt_out);
142 op.params[1].tmpref.buffer = out;
143 op.params[1].tmpref.size = sizeof(out);
144 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
145 TEEC_MEMREF_TEMP_OUTPUT,
146 TEEC_NONE, TEEC_NONE);
147
148 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
149 TEEC_InvokeCommand(cs->session,
150 cs->
151 cmd_id_aes256ecb_decrypt,
152 &op,
153 &ret_orig));
154
155 if (!ADBG_EXPECT(cs->c, 0,
156 memcmp(crypt_in, out, sizeof(crypt_in)))) {
157 Do_ADBG_Log("crypt_in:");
158 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
159 Do_ADBG_Log("out:");
160 Do_ADBG_HexLog(out, sizeof(out), 16);
161 }
162 }
163 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
164
165 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
166 {
167 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
168 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
169 static const uint8_t sha256_out[] = {
170 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
171 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
172 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
173 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
174 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100175 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200176
177 op.params[0].tmpref.buffer = (void *)sha256_in;
178 op.params[0].tmpref.size = sizeof(sha256_in);
179 op.params[1].tmpref.buffer = out;
180 op.params[1].tmpref.size = sizeof(out);
181 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
182 TEEC_MEMREF_TEMP_OUTPUT,
183 TEEC_NONE, TEEC_NONE);
184
185 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
186 TEEC_InvokeCommand(cs->session,
187 cs->
188 cmd_id_sha256,
189 &op,
190 &ret_orig));
191
192 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
193 sizeof(sha256_out)))) {
194 Do_ADBG_Log("sha256_out:");
195 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
196 Do_ADBG_Log("out:");
197 Do_ADBG_HexLog(out, sizeof(out), 16);
198 }
199 }
200 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
201
Etienne Carrierea3198522017-10-26 09:48:55 +0200202 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200203 {
204 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
205 static const uint8_t in[] = {
206 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
207 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
208 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
209 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
210 };
211 static const uint8_t exp_out[] = {
212 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
213 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
214 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
215 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
216 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100217 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200218
219 op.params[0].tmpref.buffer = (void *)in;
220 op.params[0].tmpref.size = sizeof(in);
221 op.params[1].tmpref.buffer = out;
222 op.params[1].tmpref.size = sizeof(out);
223 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
224 TEEC_MEMREF_TEMP_OUTPUT,
225 TEEC_NONE, TEEC_NONE);
226
227 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
228 TEEC_InvokeCommand(cs->session,
229 cs->
230 cmd_id_aes256ecb_encrypt,
231 &op,
232 &ret_orig));
233
234 if (!ADBG_EXPECT(cs->c, 0,
235 memcmp(exp_out, out, sizeof(exp_out)))) {
236 Do_ADBG_Log("exp_out:");
237 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
238 Do_ADBG_Log("out:");
239 Do_ADBG_HexLog(out, sizeof(out), 16);
240 }
241 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200242 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200243
Etienne Carrierea3198522017-10-26 09:48:55 +0200244 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200245 {
246 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
247 static const uint8_t in[] = {
248 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
249 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
250 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
251 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
252 };
253 static const uint8_t exp_out[] = {
254 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
255 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
256 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
257 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
258 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100259 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200260
261 op.params[0].tmpref.buffer = (void *)in;
262 op.params[0].tmpref.size = sizeof(in);
263 op.params[1].tmpref.buffer = out;
264 op.params[1].tmpref.size = sizeof(out);
265 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
266 TEEC_MEMREF_TEMP_OUTPUT,
267 TEEC_NONE, TEEC_NONE);
268
269 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
270 TEEC_InvokeCommand(cs->session,
271 cs->
272 cmd_id_aes256ecb_decrypt,
273 &op,
274 &ret_orig));
275
276 if (!ADBG_EXPECT(cs->c, 0,
277 memcmp(exp_out, out, sizeof(exp_out)))) {
278 Do_ADBG_Log("exp_out:");
279 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
280 Do_ADBG_Log("out:");
281 Do_ADBG_HexLog(out, sizeof(out), 16);
282 }
283 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200284 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200285}
286
287static void xtest_tee_test_1001(ADBG_Case_t *c)
288{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100289 TEEC_Result res = TEEC_ERROR_GENERIC;
290 TEEC_Session session = { };
291 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200292
Etienne Carriere11093162017-10-26 09:49:04 +0200293 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100294 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100295 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200296 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
297 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100298 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200299 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100300 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
301 return;
302
303 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200304
Jens Wiklandercf16e842016-02-10 09:07:09 +0100305 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100306 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Etienne Carriere3ea96722022-03-11 09:16:40 +0100307
308 Do_ADBG_EndSubCase(c, "Core self tests");
309
310 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
311
312 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
313 &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL,
314 &ret_orig));
315
316 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
317
Jens Wiklandercf16e842016-02-10 09:07:09 +0100318 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200319}
Jens Wiklander14f48872018-06-29 15:30:13 +0200320ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200321
Jens Wiklander1d70a112017-10-16 15:16:39 +0200322static void xtest_tee_test_1002(ADBG_Case_t *c)
323{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100324 TEEC_Result res = TEEC_ERROR_GENERIC;
325 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200326 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100327 uint32_t ret_orig = 0;
328 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200329 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100330 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200331
Etienne Carriere11093162017-10-26 09:49:04 +0200332 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200333 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
334 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200335 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
336 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200337 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200338 }
339 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200340
341 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
342 TEEC_NONE, TEEC_NONE);
343 op.params[0].tmpref.size = sizeof(buf);
344 op.params[0].tmpref.buffer = buf;
345
346 for (n = 0; n < sizeof(buf); n++)
347 buf[n] = n + 1;
348 for (n = 0; n < sizeof(buf); n++)
349 exp_sum += buf[n];
350
351 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
352 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
353 goto out;
354
355 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
356out:
357 TEEC_CloseSession(&session);
358}
Jens Wiklander14f48872018-06-29 15:30:13 +0200359ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200360
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100361struct test_1003_arg {
362 uint32_t test_type;
363 size_t repeat;
364 size_t max_before_lockers;
365 size_t max_during_lockers;
366 size_t before_lockers;
367 size_t during_lockers;
368 TEEC_Result res;
369 uint32_t error_orig;
370};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200371
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100372static void *test_1003_thread(void *arg)
373{
374 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100375 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100376 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100377 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100378
379 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
380 NULL, &a->error_orig);
381 if (a->res != TEEC_SUCCESS)
382 return NULL;
383
384 for (n = 0; n < a->repeat; n++) {
385 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
386
387 op.params[0].value.a = a->test_type;
388 op.params[0].value.b = rounds;
389
390 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
391 TEEC_VALUE_OUTPUT,
392 TEEC_NONE, TEEC_NONE);
393 a->res = TEEC_InvokeCommand(&session,
394 PTA_INVOKE_TESTS_CMD_MUTEX,
395 &op, &a->error_orig);
396 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
397 op.params[1].value.b != 1) {
398 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
399 a->res = TEEC_ERROR_BAD_STATE;
400 a->error_orig = 42;
401 break;
402 }
403
404 if (a->test_type == PTA_MUTEX_TEST_READER) {
405 if (op.params[1].value.a > a->max_before_lockers)
406 a->max_before_lockers = op.params[1].value.a;
407
408 if (op.params[1].value.b > a->max_during_lockers)
409 a->max_during_lockers = op.params[1].value.b;
410
411 a->before_lockers += op.params[1].value.a;
412 a->during_lockers += op.params[1].value.b;
413 }
414 }
415 TEEC_CloseSession(&session);
416
417 return NULL;
418}
419
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100420#define TEST_1003_THREAD_COUNT (3 * 2)
421
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100422static void xtest_tee_test_1003(ADBG_Case_t *c)
423{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100424 TEEC_Result res = TEEC_ERROR_GENERIC;
425 TEEC_Session session = { };
426 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100427 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100428 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100429 size_t max_read_concurrency = 0;
430 size_t max_read_waiters = 0;
431 size_t num_concurrent_read_lockers = 0;
432 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100433 size_t n = 0;
434 size_t nt = TEST_1003_THREAD_COUNT;
435 double mean_read_concurrency = 0;
436 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100437 size_t num_writers = 0;
438 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100439 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100440
441 /* Pseudo TA is optional: warn and nicely exit if not found */
442 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
443 &ret_orig);
444 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
445 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
446 return;
447 }
448 ADBG_EXPECT_TEEC_SUCCESS(c, res);
449 TEEC_CloseSession(&session);
450
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100451 for (n = 0; n < nt; n++) {
452 if (n % 3) {
453 arg[n].test_type = PTA_MUTEX_TEST_READER;
454 num_readers++;
455 } else {
456 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
457 num_writers++;
458 }
459 arg[n].repeat = repeat;
460 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
461 test_1003_thread, arg + n)))
462 nt = n; /* break loop and start cleanup */
463 }
464
465 for (n = 0; n < nt; n++) {
466 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
467 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
468 Do_ADBG_Log("error origin %" PRIu32,
469 arg[n].error_orig);
470 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
471 if (arg[n].max_during_lockers > max_read_concurrency)
472 max_read_concurrency =
473 arg[n].max_during_lockers;
474
475 if (arg[n].max_before_lockers > max_read_waiters)
476 max_read_waiters = arg[n].max_before_lockers;
477
478 num_concurrent_read_lockers += arg[n].during_lockers;
479 num_concurrent_read_waiters += arg[n].before_lockers;
480 }
481 }
482
483 mean_read_concurrency = (double)num_concurrent_read_lockers /
484 (double)(repeat * num_readers);
485 mean_read_waiters = (double)num_concurrent_read_waiters /
486 (double)(repeat * num_readers);
487
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100488 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
489 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100490 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
491 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
492 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
493 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
494}
Jens Wiklander14f48872018-06-29 15:30:13 +0200495ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
496 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200497
Pascal Brandc639ac82015-07-02 08:53:34 +0200498static void xtest_tee_test_1004(ADBG_Case_t *c)
499{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100500 TEEC_Session session = { };
501 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200502 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
503 TA_CRYPT_CMD_AES256ECB_ENC,
504 TA_CRYPT_CMD_AES256ECB_DEC };
505
506 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
507 &session, &crypt_user_ta_uuid,
508 NULL, &ret_orig)))
509 return;
510
511 /* Run the "complete crypto test suite" */
512 xtest_crypto_test(&cs);
513
514 TEEC_CloseSession(&session);
515}
Jens Wiklander14f48872018-06-29 15:30:13 +0200516ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200517
Etienne Carriere92c34422018-02-09 13:11:40 +0100518static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200519{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100520 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200521 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100522 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200523
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300524 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200525 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300526 &ret_orig)))
527 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200528
529 op.params[0].value.a = n;
530 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
531 TEEC_NONE);
532
533 (void)ADBG_EXPECT_TEEC_RESULT(c,
534 TEEC_ERROR_TARGET_DEAD,
535 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
536 &ret_orig));
537
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300538 (void)ADBG_EXPECT_TEEC_RESULT(c,
539 TEEC_ERROR_TARGET_DEAD,
540 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200541 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300542
Pascal Brandc639ac82015-07-02 08:53:34 +0200543 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
544
545 TEEC_CloseSession(&session);
546}
547
Etienne Carriere92c34422018-02-09 13:11:40 +0100548static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
549 size_t size)
550{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100551 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100552 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100553 uint32_t ret_orig = 0;
554 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100555
Etienne Carriere92c34422018-02-09 13:11:40 +0100556 shm.size = size;
557 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
558 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
559 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
560 return;
561
562 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
563 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
564 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200565 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100566
567 op.params[0].value.a = (uint32_t)n;
568 op.params[1].memref.parent = &shm;
569 op.params[1].memref.size = size;
570 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
571 TEEC_NONE, TEEC_NONE);
572
573 (void)ADBG_EXPECT_TEEC_RESULT(c,
574 TEEC_ERROR_TARGET_DEAD,
575 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
576 &ret_orig));
577
578 (void)ADBG_EXPECT_TEEC_RESULT(c,
579 TEEC_ERROR_TARGET_DEAD,
580 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
581 &ret_orig));
582
583 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
584
585 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200586rel_shm:
587 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100588}
589
Pascal Brandc639ac82015-07-02 08:53:34 +0200590static void xtest_tee_test_1005(ADBG_Case_t *c)
591{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100592 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200593#define MAX_SESSIONS 3
594 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100595 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200596
597 for (i = 0; i < MAX_SESSIONS; i++) {
598 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200599 xtest_teec_open_session(&sessions[i],
600 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200601 NULL, &ret_orig)))
602 break;
603 }
604
605 for (; --i >= 0; )
606 TEEC_CloseSession(&sessions[i]);
607}
Jens Wiklander14f48872018-06-29 15:30:13 +0200608ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200609
610static void xtest_tee_test_1006(ADBG_Case_t *c)
611{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100612 TEEC_Session session = { };
613 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200614 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100615 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200616
617 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
618 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
619 &ret_orig)))
620 return;
621
622 op.params[0].tmpref.buffer = buf;
623 op.params[0].tmpref.size = sizeof(buf);
624 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
625 TEEC_NONE, TEEC_NONE);
626
627 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
628 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
629 &ret_orig));
630
631 TEEC_CloseSession(&session);
632}
Jens Wiklander14f48872018-06-29 15:30:13 +0200633ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
634 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200635
636static void xtest_tee_test_1007(ADBG_Case_t *c)
637{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100638 TEEC_Session session = { };
639 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200640
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300641 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200642 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300643 &ret_orig)))
644 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200645
646 (void)ADBG_EXPECT_TEEC_RESULT(c,
647 TEEC_ERROR_TARGET_DEAD,
648 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
649 &ret_orig));
650
651 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
652
653 (void)ADBG_EXPECT_TEEC_RESULT(c,
654 TEEC_ERROR_TARGET_DEAD,
655 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
656 &ret_orig));
657
658 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
659
660 TEEC_CloseSession(&session);
661}
Jens Wiklander14f48872018-06-29 15:30:13 +0200662ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200663
Jerome Forissierf02a2212015-10-29 14:33:35 +0100664#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000665# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800666#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000667# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100668#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000669# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100670#endif
671
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100672static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600673{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100674 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600675
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100676 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100677 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100678 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200679 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
680 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
681 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600682 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200683
Jens Wiklanderb7940892015-10-23 16:02:40 +0200684 return fopen(buf, mode);
685}
686
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100687static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200688{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100689 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100690 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
691 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100692 TEEC_Result res = TEEC_ERROR_GENERIC;
693 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100694 FILE *f = NULL;
695 bool r = false;
696 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100697 size_t sz = 0;
698 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200699
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100700 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
701 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
702 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200703
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100704 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
705 if (!ADBG_EXPECT_NOT_NULL(c, f))
706 goto out;
707 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
708 goto out;
709 sz = ftell(f);
710 rewind(f);
711
712 buf = malloc(sz);
713 if (!ADBG_EXPECT_NOT_NULL(c, buf))
714 goto out;
715
716 fread_res = fread(buf, 1, sz, f);
717 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
718 goto out;
719
Jens Wiklander4441fe22015-10-23 16:53:02 +0200720 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100721 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200722
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100723 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200724
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100725 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
726 TEEC_NONE, TEEC_NONE);
727 op.params[0].tmpref.buffer = buf;
728 op.params[0].tmpref.size = sz;
729
730 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
731 &ret_orig);
732 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
733out:
734 free(buf);
735 if (f)
736 fclose(f);
737 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200738 return r;
739}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100740
741static void test_1008_corrupt_ta(ADBG_Case_t *c)
742{
743 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
744 TEEC_Result res = TEEC_ERROR_GENERIC;
745 TEEC_Session session = { };
746 uint32_t ret_orig = 0;
747
748 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
749 if (res) {
750 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
751 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200752 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100753 return;
754 }
755 TEEC_CloseSession(&session);
756
757 ADBG_EXPECT_TRUE(c,
758 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
759 ADBG_EXPECT_TRUE(c,
760 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
761 ADBG_EXPECT_TRUE(c,
762 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
763 ADBG_EXPECT_TRUE(c,
764 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
765 ADBG_EXPECT_TRUE(c,
766 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
767 ADBG_EXPECT_TRUE(c,
768 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
769 ADBG_EXPECT_TRUE(c,
770 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
771 ADBG_EXPECT_TRUE(c,
772 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
773 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
774 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
775}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200776
Pascal Brandc639ac82015-07-02 08:53:34 +0200777static void xtest_tee_test_1008(ADBG_Case_t *c)
778{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100779 TEEC_Session session = { };
780 TEEC_Session session_crypt = { };
781 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200782
783 Do_ADBG_BeginSubCase(c, "Invoke command");
784 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300785 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200786 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300787 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200788
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300789 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
790 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
791 NULL, &ret_orig));
792 TEEC_CloseSession(&session);
793 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200794
Pascal Brandc639ac82015-07-02 08:53:34 +0200795 }
796 Do_ADBG_EndSubCase(c, "Invoke command");
797
798 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
799 {
800 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
801
802 op.params[0].value.a = 2000;
803 op.paramTypes = TEEC_PARAM_TYPES(
804 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
805
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300806 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200807 xtest_teec_open_session(&session,
808 &os_test_ta_uuid,
809 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300810 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200811
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300812 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
813 TEEC_InvokeCommand(&session,
814 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
815 &op, &ret_orig));
816 TEEC_CloseSession(&session);
817 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200818 }
819 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
820
821 Do_ADBG_BeginSubCase(c, "Create session fail");
822 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100823 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200824
Pascal Brandc639ac82015-07-02 08:53:34 +0200825 for (n = 0; n < 100; n++) {
826 Do_ADBG_Log("n = %zu", n);
827 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
828 xtest_teec_open_session(&session_crypt,
829 &create_fail_test_ta_uuid,
830 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100831 /* level > 0 may be used to detect/debug memory leaks */
832 if (!level)
833 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200834 }
835 }
836 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200837
Jens Wiklander4441fe22015-10-23 16:53:02 +0200838 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100839 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200840 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200841}
Jens Wiklander14f48872018-06-29 15:30:13 +0200842ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
843 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200844
Pascal Brandc639ac82015-07-02 08:53:34 +0200845static void *cancellation_thread(void *arg)
846{
847 /*
848 * Sleep 0.5 seconds before cancellation to make sure that the other
849 * thread is in RPC_WAIT.
850 */
851 (void)usleep(500000);
852 TEEC_RequestCancellation(arg);
853 return NULL;
854}
Pascal Brandc639ac82015-07-02 08:53:34 +0200855
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300856static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
857 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200858{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100859 TEEC_Session session = { };
860 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300861 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200862
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100863 memset(&thr, 0, sizeof(thr));
864
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300865 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200866 {
867 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
868
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300869 if (ADBG_EXPECT_TEEC_SUCCESS(c,
870 xtest_teec_open_session(&session, &os_test_ta_uuid,
871 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200872
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300873 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
874 TEEC_ORIGIN_TRUSTED_APP,
875 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200876
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300877 op.params[0].value.a = timeout;
878 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
879 TEEC_NONE,
880 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300881 if (cancel) {
882 (void)ADBG_EXPECT(c, 0,
883 pthread_create(&thr, NULL,
884 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200885
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300886 (void)ADBG_EXPECT_TEEC_RESULT(c,
887 TEEC_ERROR_CANCEL,
888 TEEC_InvokeCommand(&session,
889 TA_OS_TEST_CMD_WAIT,
890 &op,
891 &ret_orig));
892 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300893
894 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
895 TEEC_InvokeCommand(&session,
896 TA_OS_TEST_CMD_WAIT,
897 &op,
898 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300899 if (cancel)
900 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300901
902 TEEC_CloseSession(&session);
903 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200904 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300905 Do_ADBG_EndSubCase(c, "%s", subcase);
906}
907
908static void xtest_tee_test_1009(ADBG_Case_t *c)
909{
910 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
911 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300912 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300913 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200914}
Jens Wiklander14f48872018-06-29 15:30:13 +0200915ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200916
917static void xtest_tee_test_1010(ADBG_Case_t *c)
918{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100919 unsigned int n = 0;
920 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100921 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200922
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200923 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200924 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
925 xtest_tee_test_invalid_mem_access(c, n);
926 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
927 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100928
929 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
930 for (n = 1; n <= 5; n++) {
931 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200932 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100933 n, memref_sz[idx]);
934 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
935 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200936 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100937 n, memref_sz[idx]);
938 }
939 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200940}
Jens Wiklander14f48872018-06-29 15:30:13 +0200941ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
942 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200943
944static void xtest_tee_test_1011(ADBG_Case_t *c)
945{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100946 TEEC_Session session = { };
947 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200948 struct xtest_crypto_session cs = {
949 c, &session, TA_RPC_CMD_CRYPT_SHA256,
950 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
951 TA_RPC_CMD_CRYPT_AES256ECB_DEC
952 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100953 struct xtest_crypto_session cs_privmem = {
954 c, &session,
955 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
956 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
957 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
958 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200959 TEEC_UUID uuid = rpc_test_ta_uuid;
960
961 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
962 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
963 return;
964
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100965 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200966 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100967 * Run the "complete crypto test suite" using TA-to-TA
968 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200969 */
970 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100971 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
972
973 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
974 /*
975 * Run the "complete crypto test suite" using TA-to-TA
976 * communication via TA private memory.
977 */
978 xtest_crypto_test(&cs_privmem);
979 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
980
Pascal Brandc639ac82015-07-02 08:53:34 +0200981 TEEC_CloseSession(&session);
982}
Jens Wiklander14f48872018-06-29 15:30:13 +0200983ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
984 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200985
986/*
987 * Note that this test is failing when
988 * - running twice in a raw
989 * - and the user TA is statically linked
990 * This is because the counter is not reseted when opening the first session
991 * in case the TA is statically linked
992 */
993static void xtest_tee_test_1012(ADBG_Case_t *c)
994{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100995 TEEC_Session session1 = { };
996 TEEC_Session session2 = { };
997 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200998 TEEC_UUID uuid = sims_test_ta_uuid;
999
1000 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
1001 {
1002 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1003 static const uint8_t in[] = {
1004 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
1005 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
1006 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
1007 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
1008 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001009 uint8_t out[32] = { };
1010 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +02001011
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001012 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001013 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001014 &ret_orig)))
1015 return;
Pascal Brandc639ac82015-07-02 08:53:34 +02001016
1017 op.params[0].value.a = 0;
1018 op.params[1].tmpref.buffer = (void *)in;
1019 op.params[1].tmpref.size = sizeof(in);
1020 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1021 TEEC_MEMREF_TEMP_INPUT,
1022 TEEC_NONE, TEEC_NONE);
1023
1024 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1025 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
1026 &ret_orig));
1027
Jerome Forissier3cc0a422017-12-14 09:53:24 +01001028 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001029 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001030 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001031 &ret_orig)))
1032 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +02001033
1034 op.params[0].value.a = 0;
1035 op.params[1].tmpref.buffer = out;
1036 op.params[1].tmpref.size = sizeof(out);
1037 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1038 TEEC_MEMREF_TEMP_OUTPUT,
1039 TEEC_NONE, TEEC_NONE);
1040
1041 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1042 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1043 &op, &ret_orig));
1044
1045 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1046 sizeof(out))) {
1047 Do_ADBG_Log("in:");
1048 Do_ADBG_HexLog(in, sizeof(in), 16);
1049 Do_ADBG_Log("out:");
1050 Do_ADBG_HexLog(out, sizeof(out), 16);
1051 }
1052
1053 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1054 TEEC_NONE, TEEC_NONE,
1055 TEEC_NONE);
1056
1057 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1058 TEEC_InvokeCommand(&session1,
1059 TA_SIMS_CMD_GET_COUNTER,
1060 &op, &ret_orig));
1061
1062 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1063
1064 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1065 TEEC_InvokeCommand(&session2,
1066 TA_SIMS_CMD_GET_COUNTER, &op,
1067 &ret_orig));
1068
1069 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1070 TEEC_CloseSession(&session2);
1071 }
1072
1073 memset(out, 0, sizeof(out));
1074 op.params[0].value.a = 0;
1075 op.params[1].tmpref.buffer = out;
1076 op.params[1].tmpref.size = sizeof(out);
1077 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1078 TEEC_MEMREF_TEMP_OUTPUT,
1079 TEEC_NONE, TEEC_NONE);
1080
1081 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1082 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1083 &ret_orig));
1084
1085 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1086 Do_ADBG_Log("in:");
1087 Do_ADBG_HexLog(in, sizeof(in), 16);
1088 Do_ADBG_Log("out:");
1089 Do_ADBG_HexLog(out, sizeof(out), 16);
1090 }
1091
1092 TEEC_CloseSession(&session1);
1093 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001094 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001095}
Jens Wiklander14f48872018-06-29 15:30:13 +02001096ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1097 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001098
1099struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001100 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001101 uint32_t cmd;
1102 uint32_t repeat;
1103 TEEC_SharedMemory *shm;
1104 uint32_t error_orig;
1105 TEEC_Result res;
1106 uint32_t max_concurrency;
1107 const uint8_t *in;
1108 size_t in_len;
1109 uint8_t *out;
1110 size_t out_len;
1111};
1112
1113static void *test_1013_thread(void *arg)
1114{
1115 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001116 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001117 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1118 uint8_t p2 = TEEC_NONE;
1119 uint8_t p3 = TEEC_NONE;
1120
Jens Wiklander70672972016-04-06 00:01:45 +02001121 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001122 &a->error_orig);
1123 if (a->res != TEEC_SUCCESS)
1124 return NULL;
1125
1126 op.params[0].memref.parent = a->shm;
1127 op.params[0].memref.size = a->shm->size;
1128 op.params[0].memref.offset = 0;
1129 op.params[1].value.a = a->repeat;
1130 op.params[1].value.b = 0;
1131 op.params[2].tmpref.buffer = (void *)a->in;
1132 op.params[2].tmpref.size = a->in_len;
1133 op.params[3].tmpref.buffer = a->out;
1134 op.params[3].tmpref.size = a->out_len;
1135
1136 if (a->in_len)
1137 p2 = TEEC_MEMREF_TEMP_INPUT;
1138 if (a->out_len)
1139 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1140
1141 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1142 TEEC_VALUE_INOUT, p2, p3);
1143
1144 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1145 a->max_concurrency = op.params[1].value.b;
1146 a->out_len = op.params[3].tmpref.size;
1147 TEEC_CloseSession(&session);
1148 return NULL;
1149}
1150
Pascal Brand4fa35582015-12-17 10:59:12 +01001151#define NUM_THREADS 3
1152
Jens Wiklander70672972016-04-06 00:01:45 +02001153static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1154 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001155{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001156 size_t nt = 0;
1157 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001158 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001159 TEEC_SharedMemory shm = { };
1160 size_t max_concurrency = 0;
1161 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001162 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1163 static const uint8_t sha256_out[] = {
1164 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1165 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1166 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1167 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1168 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001169 uint8_t out[32] = { };
1170 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001171 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001172
Etienne Carrierec0a61722023-03-07 17:47:42 +01001173 /* Decrease number of loops when pager has a small page pool */
1174 if (level == 0 && optee_pager_with_small_pool())
1175 repeat = 250;
1176
Jens Wiklander70672972016-04-06 00:01:45 +02001177 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001178 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001179
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001180 shm.size = sizeof(struct ta_concurrent_shm);
1181 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1182 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1183 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1184 return;
1185
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001186 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001187 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001188 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001189
1190 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001191 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001192 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001193 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001194 arg[n].shm = &shm;
1195 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1196 test_1013_thread, arg + n)))
1197 nt = n; /* break loop and start cleanup */
1198 }
1199
1200 for (n = 0; n < nt; n++) {
1201 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001202 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1203 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1204 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1205 skip = true;
1206 continue;
1207 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001208 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1209 if (arg[n].max_concurrency > max_concurrency)
1210 max_concurrency = arg[n].max_concurrency;
1211 }
1212
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001213 /*
1214 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001215 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001216 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1217 * best result there).
1218 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001219 if (!skip) {
1220 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1221 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1222 NUM_THREADS);
1223 *mean_concurrency += max_concurrency;
1224 }
Jens Wiklander70672972016-04-06 00:01:45 +02001225 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001226
Jens Wiklander70672972016-04-06 00:01:45 +02001227 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001228 memset(shm.buffer, 0, shm.size);
1229 memset(arg, 0, sizeof(arg));
1230 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001231 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001232
1233 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001234 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001235 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001236 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001237 arg[n].shm = &shm;
1238 arg[n].in = sha256_in;
1239 arg[n].in_len = sizeof(sha256_in);
1240 arg[n].out = out;
1241 arg[n].out_len = sizeof(out);
1242 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1243 test_1013_thread, arg + n)))
1244 nt = n; /* break loop and start cleanup */
1245 }
1246
1247 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001248 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1249 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1250 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1251 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1252 continue;
1253 }
1254 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001255 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1256 arg[n].out, arg[n].out_len);
1257 if (arg[n].max_concurrency > max_concurrency)
1258 max_concurrency = arg[n].max_concurrency;
1259 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001260 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001261 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001262
Pascal Brand4fa35582015-12-17 10:59:12 +01001263 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001264 TEEC_ReleaseSharedMemory(&shm);
1265}
Pascal Brand4fa35582015-12-17 10:59:12 +01001266
1267static void xtest_tee_test_1013(ADBG_Case_t *c)
1268{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001269 int i = 0;
1270 double mean_concurrency = 0;
1271 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001272 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001273
1274 if (level == 0)
1275 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001276
Jens Wiklander70672972016-04-06 00:01:45 +02001277 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001278 mean_concurrency = 0;
1279 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001280 xtest_tee_test_1013_single(c, &concurrency,
1281 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001282 mean_concurrency += concurrency;
1283 }
1284 mean_concurrency /= nb_loops;
1285
1286 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1287 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001288 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001289
Jens Wiklander70672972016-04-06 00:01:45 +02001290 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1291 mean_concurrency = 0;
1292 for (i = 0; i < nb_loops; i++) {
1293 xtest_tee_test_1013_single(c, &concurrency,
1294 &concurrent_large_ta_uuid);
1295 mean_concurrency += concurrency;
1296 }
1297 mean_concurrency /= nb_loops;
1298
1299 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1300 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1301 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1302}
Jens Wiklander14f48872018-06-29 15:30:13 +02001303ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001304 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001305
1306#ifdef CFG_SECURE_DATA_PATH
1307static void xtest_tee_test_1014(ADBG_Case_t *c)
1308{
1309 UNUSED(c);
1310
1311 int size = 17000;
1312 int loop = 10;
Olivier Masseda5282a2022-04-07 11:24:08 +02001313 const char *heap_name = DEFAULT_HEAP_NAME;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001314 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001315 int test = 0;
1316 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001317
1318 test = TEST_NS_TO_TA;
1319 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001320 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001321 ADBG_EXPECT(c, 0, ret);
1322 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1323
1324 test = TEST_TA_TO_TA;
1325 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001326 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001327 ADBG_EXPECT(c, 0, ret);
1328 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1329
1330 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001331 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001332 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001333 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001334 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001335
1336 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001337 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001338 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001339 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001340 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001341
1342 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001343 ret = sdp_out_of_bounds_memref_test(size, heap_name, 0);
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001344 ADBG_EXPECT(c, 0, ret);
1345 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001346}
Jens Wiklander14f48872018-06-29 15:30:13 +02001347ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1348 "Test secure data path against SDP TAs and pTAs");
1349#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001350
Jerome Forissierf4259942022-01-11 14:27:05 +01001351#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001352static void xtest_tee_test_1015(ADBG_Case_t *c)
1353{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001354 TEEC_Result res = TEEC_ERROR_GENERIC;
1355 TEEC_Session session = { };
1356 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001357
Etienne Carriere11093162017-10-26 09:49:04 +02001358 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001359 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1360 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001361 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1362 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001363 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001364 }
1365 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001366
1367 ADBG_EXPECT_TEEC_SUCCESS(c,
1368 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1369 NULL, &ret_orig));
1370 TEEC_CloseSession(&session);
1371}
Jens Wiklander14f48872018-06-29 15:30:13 +02001372ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1373 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001374#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001375
1376static void xtest_tee_test_1016(ADBG_Case_t *c)
1377{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001378 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001379 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001380 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001381
1382 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1383 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1384 &ret_orig)))
1385 return;
1386
1387 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1388 TEEC_NONE);
1389
1390 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1391 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1392 &ret_orig));
1393
1394 TEEC_CloseSession(&session);
1395}
Jens Wiklander14f48872018-06-29 15:30:13 +02001396ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1397 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001398
1399static void xtest_tee_test_1017(ADBG_Case_t *c)
1400{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001401 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001402 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001403 uint32_t ret_orig = 0;
1404 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001405 size_t page_size = 4096;
1406
Jens Wiklander87e81702018-03-20 12:00:00 +08001407 shm.size = 8 * page_size;
1408 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1409 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1410 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1411 return;
1412
1413 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1414 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1415 &ret_orig)))
1416 goto out;
1417
1418 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1419 TEEC_MEMREF_PARTIAL_INPUT,
1420 TEEC_MEMREF_PARTIAL_OUTPUT,
1421 TEEC_MEMREF_PARTIAL_OUTPUT);
1422
1423 /*
1424 * The first two memrefs are supposed to be combined into in
1425 * region and the last two memrefs should have one region each
1426 * when the parameters are mapped for the TA.
1427 */
1428 op.params[0].memref.parent = &shm;
1429 op.params[0].memref.size = page_size;
1430 op.params[0].memref.offset = 0;
1431
1432 op.params[1].memref.parent = &shm;
1433 op.params[1].memref.size = page_size;
1434 op.params[1].memref.offset = page_size;
1435
1436 op.params[2].memref.parent = &shm;
1437 op.params[2].memref.size = page_size;
1438 op.params[2].memref.offset = 4 * page_size;
1439
1440 op.params[3].memref.parent = &shm;
1441 op.params[3].memref.size = 2 * page_size;
1442 op.params[3].memref.offset = 6 * page_size;
1443
1444 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1445 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1446 &ret_orig));
1447
1448 TEEC_CloseSession(&session);
1449out:
1450 TEEC_ReleaseSharedMemory(&shm);
1451}
Jens Wiklander14f48872018-06-29 15:30:13 +02001452ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1453 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001454
Etienne Carriere84382b32018-04-25 18:30:30 +02001455static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1456 TEEC_SharedMemory *shm)
1457{
1458 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1459 TEEC_Result ret = TEEC_ERROR_GENERIC;
1460 uint32_t ret_orig = 0;
1461
1462 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1463 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1464
1465 op.params[0].memref.parent = shm;
1466 op.params[0].memref.size = shm->size / 2;
1467 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1468
1469 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1470 &op, &ret_orig);
1471
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001472 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001473 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1474 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1475 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1476 }
1477}
1478
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001479static void xtest_tee_test_1018(ADBG_Case_t *c)
1480{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001481 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001482 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001483 uint32_t ret_orig = 0;
1484 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001485 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001486 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001487 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001488 uint8_t buffer[6001] = { };
1489
1490 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1491 xtest_teec_open_session(&session,
1492 &os_test_ta_uuid,
1493 NULL,
1494 &ret_orig)))
1495 return;
1496
Joakim Becha1212b62020-04-07 12:06:00 +02001497 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001498
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001499 shm.size = 8 * page_size;
1500 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1501 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001502 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1503 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001504 goto out;
1505
1506 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1507 TEEC_MEMREF_PARTIAL_INPUT,
1508 TEEC_MEMREF_PARTIAL_OUTPUT,
1509 TEEC_MEMREF_PARTIAL_OUTPUT);
1510
1511 /*
1512 * The first two memrefs are supposed to be combined into in
1513 * region and the last two memrefs should have one region each
1514 * when the parameters are mapped for the TA.
1515 */
1516 op.params[0].memref.parent = &shm;
1517 op.params[0].memref.size = page_size;
1518 op.params[0].memref.offset = 0;
1519
1520 op.params[1].memref.parent = &shm;
1521 op.params[1].memref.size = page_size;
1522 op.params[1].memref.offset = page_size;
1523
1524 op.params[2].memref.parent = &shm;
1525 op.params[2].memref.size = page_size;
1526 op.params[2].memref.offset = 4 * page_size;
1527
1528 op.params[3].memref.parent = &shm;
1529 op.params[3].memref.size = 3 * page_size;
1530 op.params[3].memref.offset = 6 * page_size;
1531
Etienne Carriere84382b32018-04-25 18:30:30 +02001532 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1533 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001534
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001535 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001536 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1537 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1538 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1539 }
1540
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001541 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001542 Do_ADBG_EndSubCase(c, NULL);
1543
1544 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1545
1546 memset(&shm, 0, sizeof(shm));
1547 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1548 shm.buffer = buffer;
1549 shm.size = sizeof(buffer);
1550
1551 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1552 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1553 &shm)))
1554 goto out;
1555
1556 invoke_1byte_out_of_bounds(c, &session, &shm);
1557
1558 TEEC_ReleaseSharedMemory(&shm);
1559 Do_ADBG_EndSubCase(c, NULL);
1560
1561 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1562
1563 memset(&shm, 0, sizeof(shm));
1564 shm.size = sizeof(buffer);
1565 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1566 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1567 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1568 &shm)))
1569 goto out;
1570
1571 invoke_1byte_out_of_bounds(c, &session, &shm);
1572
1573 TEEC_ReleaseSharedMemory(&shm);
1574 Do_ADBG_EndSubCase(c, NULL);
1575
1576out:
1577 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001578}
Jens Wiklander14f48872018-06-29 15:30:13 +02001579ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1580 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001581
Jerome Forissier53bde722018-05-31 09:14:54 +02001582static void xtest_tee_test_1019(ADBG_Case_t *c)
1583{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001584 TEEC_Session session = { };
1585 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001586
1587 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1588 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1589 &ret_orig)))
1590 return;
1591
1592 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1593 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1594 &ret_orig));
1595
1596 (void)ADBG_EXPECT_TEEC_RESULT(c,
1597 TEEC_ERROR_TARGET_DEAD,
1598 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1599 NULL, &ret_orig));
1600
1601 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1602
1603 TEEC_CloseSession(&session);
1604}
Jens Wiklander14f48872018-06-29 15:30:13 +02001605ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1606 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001607
1608static void xtest_tee_test_1020(ADBG_Case_t *c)
1609{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001610 TEEC_Result res = TEEC_ERROR_GENERIC;
1611 TEEC_Session session = { };
1612 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001613
1614 /* Pseudo TA is optional: warn and nicely exit if not found */
1615 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1616 &ret_orig);
1617 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1618 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1619 return;
1620 }
1621 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1622
1623 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1624 NULL, &ret_orig);
1625 if (res != TEEC_SUCCESS) {
1626 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1627 ret_orig);
1628 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1629 Do_ADBG_Log(" - 1020 - skip test, feature not "
1630 "implemented");
1631 goto out;
1632 }
1633 /* Error */
1634 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1635 }
1636out:
1637 TEEC_CloseSession(&session);
1638}
1639ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1640 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001641
1642static TEEC_Result open_sec_session(TEEC_Session *session,
1643 const TEEC_UUID *uuid)
1644{
1645 TEEC_Result res = TEEC_ERROR_GENERIC;
1646 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1647 uint32_t ret_orig = 0;
1648
1649 op.params[0].tmpref.buffer = (void *)uuid;
1650 op.params[0].tmpref.size = sizeof(*uuid);
1651 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1652 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1653
1654 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1655 &op, &ret_orig);
1656 if (res != TEEC_SUCCESS)
1657 return TEEC_ERROR_GENERIC;
1658
1659 return res;
1660}
1661
1662static TEEC_Result sims_get_counter(TEEC_Session *session,
1663 uint32_t *counter)
1664{
1665 TEEC_Result res = TEEC_ERROR_GENERIC;
1666 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1667 uint32_t ret_orig = 0;
1668
1669 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1670 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1671
1672 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1673 &op, &ret_orig);
1674 if (res == TEEC_SUCCESS)
1675 *counter = op.params[0].value.a;
1676
1677 return res;
1678}
1679
1680static TEEC_Result trigger_panic(TEEC_Session *session,
1681 const TEEC_UUID *uuid)
1682{
1683 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1684 uint32_t ret_orig = 0;
1685
1686 if (!uuid) {
1687 op.params[0].tmpref.buffer = NULL;
1688 op.params[0].tmpref.size = 0;
1689 } else {
1690 op.params[0].tmpref.buffer = (void *)uuid;
1691 op.params[0].tmpref.size = sizeof(*uuid);
1692 }
1693 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1694 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1695
1696 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1697 &op, &ret_orig);
1698}
1699
1700static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1701 bool multi_instance)
1702{
1703 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1704 uint32_t counter = 0;
1705 uint32_t ret_orig = 0;
1706 uint32_t exp_counter = 0;
1707 TEEC_Session cs[3] = { };
1708
1709 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1710 xtest_teec_open_session(&cs[0], uuid, NULL,
1711 &ret_orig)))
1712 return;
1713
1714 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1715 xtest_teec_open_session(&cs[1], uuid, NULL,
1716 &ret_orig)))
1717 goto bail0;
1718
1719 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1720 goto bail1;
1721
1722 if (!ADBG_EXPECT(c, 0, counter))
1723 goto bail1;
1724
1725 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1726 goto bail1;
1727
1728 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001729 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001730 goto bail1;
1731
1732 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1733 trigger_panic(&cs[1], NULL)))
1734 goto bail1;
1735
1736 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1737 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1738 sims_get_counter(&cs[0], &counter)))
1739 goto bail1;
1740
1741 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1742 sims_get_counter(&cs[1], &counter)))
1743 goto bail1;
1744
1745 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001746 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001747 xtest_teec_open_session(&cs[1], uuid, NULL,
1748 &ret_orig)))
1749 goto bail1;
1750
1751 /* Sanity check of still valid TA context */
1752 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1753 xtest_teec_open_session(&cs[2], uuid, NULL,
1754 &ret_orig)))
1755 goto bail1;
1756
1757 /* Sanity check of still valid TA context */
1758 if (multi_instance) {
1759 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1760 sims_get_counter(&cs[0], &counter)))
1761 goto bail2;
1762
1763 if (!ADBG_EXPECT(c, 0, counter))
1764 goto bail2;
1765 }
1766
1767 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1768 goto bail2;
1769
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001770 exp_counter = multi_instance ? 0 : 1;
1771 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001772 goto bail2;
1773
1774bail2:
1775 TEEC_CloseSession(&cs[2]);
1776bail1:
1777 TEEC_CloseSession(&cs[1]);
1778bail0:
1779 TEEC_CloseSession(&cs[0]);
1780}
1781
1782static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1783 const TEEC_UUID *uuid2)
1784{
1785 uint32_t ret_orig = 0;
1786 uint32_t counter = 0;
1787 TEEC_Session cs[3] = { };
1788
1789 /* Test pre-conditions */
1790 /* 2.1 - CA opens a session toward TA1 */
1791 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1792 xtest_teec_open_session(&cs[0], uuid1, NULL,
1793 &ret_orig)))
1794 return;
1795
1796 /* 2.2 - CA opens a session toward TA2 */
1797 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1798 xtest_teec_open_session(&cs[1], uuid2, NULL,
1799 &ret_orig)))
1800 goto bail0;
1801
1802 /* 2.3 - TA1 opens a session toward TA2 */
1803 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1804 goto bail1;
1805
1806 /* 2.4 - CA invokes TA2 which panics */
1807 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1808 trigger_panic(&cs[1], NULL)))
1809 goto bail1;
1810
1811 /* Expected results */
1812 /* 2.5 - Expect CA->TA1 session is still alive */
1813 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1814 goto bail1;
1815
1816 /* 2.6 - Expect CA->TA2 session is properly released */
1817 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1818 sims_get_counter(&cs[1], &counter)))
1819 goto bail1;
1820
1821bail1:
1822 TEEC_CloseSession(&cs[1]);
1823bail0:
1824 TEEC_CloseSession(&cs[0]);
1825
1826 memset(cs, 0, sizeof(cs));
1827
1828 /* Test pre-conditions */
1829 /* 2.1 - CA opens a session toward TA1 */
1830 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1831 xtest_teec_open_session(&cs[0], uuid1, NULL,
1832 &ret_orig)))
1833 return;
1834
1835 /* 2.2 - CA opens a session toward TA2 */
1836 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1837 xtest_teec_open_session(&cs[1], uuid2, NULL,
1838 &ret_orig)))
1839 goto bail2;
1840
1841 /* 2.3 - TA1 opens a session toward TA2 */
1842 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1843 goto bail3;
1844
1845 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1846 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1847 goto bail3;
1848
1849 /* Expected results */
1850 /* 2.5 - Expect CA->TA1 session is still alive */
1851 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1852 goto bail3;
1853
1854 /* 2.6 - Expect CA->TA2 session is properly released */
1855 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1856 sims_get_counter(&cs[1], &counter)))
1857 goto bail3;
1858
1859bail3:
1860 TEEC_CloseSession(&cs[1]);
1861bail2:
1862 TEEC_CloseSession(&cs[0]);
1863}
1864
1865static void xtest_tee_test_1021(ADBG_Case_t *c)
1866{
1867 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1868 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1869 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1870
1871 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1872 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1873 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1874
1875 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1876 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1877 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1878
1879 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1880 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1881 &sims_keepalive_test_ta_uuid);
1882 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1883}
1884ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1885 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001886
1887static void xtest_tee_test_1022(ADBG_Case_t *c)
1888{
1889 TEEC_Session session = { 0 };
1890 uint32_t ret_orig = 0;
1891
1892 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1893 xtest_teec_open_session(&session, &os_test_ta_uuid,
1894 NULL, &ret_orig)))
1895 return;
1896
1897 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1898 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1899 &ret_orig));
1900
1901 (void)ADBG_EXPECT_TEEC_RESULT(c,
1902 TEEC_ERROR_TARGET_DEAD,
1903 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1904 NULL, &ret_orig));
1905
1906 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1907
1908 TEEC_CloseSession(&session);
1909}
1910ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1911 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001912
1913/*
1914 * Testing the ELF initialization (.init_array)
1915 *
1916 * - The TA has a global variable which can also be accessed by the two shared
1917 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1918 * dlopen())
1919 * - The TA and both libraries have initialization functions (declared with the
1920 * "constructor" attribute) which perform the following:
1921 * * The TA multiplies by 10 then adds 1
1922 * * os_test_lib multiplies by 10 then adds 2
1923 * * os_test_lib_dl multiplies by 10 then adds 3
1924 * By testing the variable value we make sure the initializations occurred in
1925 * the correct order.
1926 */
1927static void xtest_tee_test_1023(ADBG_Case_t *c)
1928{
1929 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1930 TEEC_Session session = { 0 };
1931 uint32_t ret_orig = 0;
1932
1933 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1934 TEEC_NONE, TEEC_NONE);
1935
1936 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1937 xtest_teec_open_session(&session, &os_test_ta_uuid,
1938 NULL, &ret_orig)))
1939 return;
1940
1941 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1942 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1943 &ret_orig));
1944
1945 /* Expected: initialization of os_test_lib, then TA */
1946 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1947
1948 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1949 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1950 &ret_orig));
1951
1952 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1953 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1954 &ret_orig));
1955
1956 /* Expected: initialization of os_test_lib_dl */
1957 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1958
1959 TEEC_CloseSession(&session);
1960}
1961ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1962 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001963
1964#ifdef CFG_CORE_TPM_EVENT_LOG
1965static void xtest_tee_test_1024(ADBG_Case_t *c)
1966{
1967 TEEC_Session session = {};
1968 uint32_t ret_orig = 0;
1969
1970 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
1971 NULL, &ret_orig);
1972
1973 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
1974 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1975 TA_TPM_TEST_GET_LOG,
1976 NULL, &ret_orig));
1977 Do_ADBG_EndSubCase(c, "TPM test service invocation");
1978
1979 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
1980 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
1981 TA_TPM_TEST_SHORT_BUF,
1982 NULL, &ret_orig));
1983 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
1984
1985 TEEC_CloseSession(&session);
1986}
1987
1988ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
1989 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
1990#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01001991
1992static void xtest_tee_test_1025(ADBG_Case_t *c)
1993{
1994 TEEC_Session session = {};
1995 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1996 uint32_t ret_orig = 0;
1997 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02001998 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02001999 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02002000
2001 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
2002
2003 memset(&shm, 0, sizeof(shm));
2004 shm.flags = TEEC_MEM_INPUT;
2005 shm.buffer = NULL;
2006 shm.size = 0;
2007
2008 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2009 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2010
2011 memset(&shm, 0, sizeof(shm));
2012 shm.flags = TEEC_MEM_OUTPUT;
2013 shm.buffer = NULL;
2014 shm.size = 0;
2015
2016 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2017 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2018
2019 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002020
Etienne Carrierec602a522020-04-13 18:53:17 +02002021 if (!xtest_teec_ctx.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02002022 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02002023 return;
2024 }
2025
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002026 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2027 xtest_teec_open_session(&session,
2028 &os_test_ta_uuid,
2029 NULL, &ret_orig)))
2030 return;
2031
2032 empty_buf = malloc(1);
2033 if (!empty_buf) {
2034 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002035 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002036 }
2037
2038 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2039 TEEC_MEMREF_TEMP_INPUT,
2040 TEEC_MEMREF_TEMP_OUTPUT,
2041 TEEC_MEMREF_TEMP_OUTPUT);
2042
2043 Do_ADBG_BeginSubCase(c,
2044 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2045
2046 op.params[0].tmpref.buffer = empty_buf;
2047 op.params[0].tmpref.size = 0;
2048
2049 op.params[1].tmpref.buffer = NULL;
2050 op.params[1].tmpref.size = 0;
2051
2052 op.params[2].tmpref.buffer = empty_buf;
2053 op.params[2].tmpref.size = 0;
2054
2055 op.params[3].tmpref.buffer = NULL;
2056 op.params[3].tmpref.size = 0;
2057
2058 ADBG_EXPECT(c, TEE_SUCCESS,
2059 TEEC_InvokeCommand(&session,
2060 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2061 &ret_orig));
2062
2063 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2064
2065 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2066
2067 op.params[0].tmpref.buffer = empty_buf;
2068 op.params[0].tmpref.size = 1;
2069
2070 op.params[1].tmpref.buffer = NULL;
2071 op.params[1].tmpref.size = 0;
2072
2073 op.params[2].tmpref.buffer = empty_buf;
2074 op.params[2].tmpref.size = 0;
2075
2076 op.params[3].tmpref.buffer = NULL;
2077 op.params[3].tmpref.size = 0;
2078
2079 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2080 TEEC_InvokeCommand(&session,
2081 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2082 &ret_orig));
2083
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002084 TEEC_CloseSession(&session);
2085
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002086 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2087
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002088 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2089
2090 /* Pseudo TA is optional: warn and nicely exit if not found */
2091 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2092 &ret_orig);
2093 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2094 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2095 goto out;
2096 }
2097 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2098 goto out;
2099
2100 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2101 TEEC_NONE, TEEC_NONE);
2102 op.params[0].tmpref.buffer = NULL;
2103 op.params[0].tmpref.size = 0;
2104
2105 ADBG_EXPECT(c, TEE_SUCCESS,
2106 TEEC_InvokeCommand(&session,
2107 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2108 &op, &ret_orig));
2109
2110out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002111 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002112out:
2113 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002114 free(empty_buf);
2115}
2116ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2117 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002118
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002119#define TEE_UUID_NS_NAME_SIZE 128
2120
2121/*
2122 * TEE Client UUID name space identifier (UUIDv4)
2123 *
2124 * Value here is random UUID that is allocated as name space identifier for
2125 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2126 */
2127static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2128
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002129/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2130static TEEC_UUID client_uuid_public = { };
2131
2132static void xtest_tee_test_1026(ADBG_Case_t *c)
2133{
2134 TEEC_Result result = TEEC_ERROR_GENERIC;
2135 uint32_t ret_orig = 0;
2136 TEEC_Session session = { };
2137 uint32_t login = UINT32_MAX;
2138 TEEC_UUID client_uuid = { };
2139
2140 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2141 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2142
2143 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2144 return;
2145
2146 result = ta_os_test_cmd_client_identity(&session, &login,
2147 &client_uuid);
2148
2149 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2150 goto out;
2151
2152 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2153
2154 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2155 sizeof(TEEC_UUID));
2156
2157out:
2158 TEEC_CloseSession(&session);
2159}
2160
2161ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2162 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002163
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002164/*
2165 * regression_1027
2166 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002167 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2168 * login client UUID generation")
2169 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002170 *
2171 * xtest skips the test when not built with OpenSSL.
2172 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002173static void xtest_tee_test_1027(ADBG_Case_t *c)
2174{
Victor Chong8e070bc2020-05-13 09:59:33 +01002175#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002176 TEEC_Result result = TEEC_ERROR_GENERIC;
2177 uint32_t ret_orig = 0;
2178 TEEC_Session session = { };
2179 uint32_t login = UINT32_MAX;
2180 TEEC_UUID client_uuid = { };
2181 TEEC_UUID expected_client_uuid = { };
2182 TEEC_UUID uuid_ns = { };
2183 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2184
2185 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2186
2187 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2188 return;
2189
2190 sprintf(uuid_name, "uid=%x", geteuid());
2191
2192 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2193 strlen(uuid_name));
2194 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2195 return;
2196
2197 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2198 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2199
2200 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2201 return;
2202
2203 result = ta_os_test_cmd_client_identity(&session, &login,
2204 &client_uuid);
2205
2206 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2207 goto out;
2208
2209 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2210
2211 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2212 sizeof(TEEC_UUID));
2213
2214out:
2215 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002216#else /*!OPENSSL_FOUND*/
2217 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002218 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002219 /* xtest_uuid_v5() depends on OpenSSL */
2220 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2221#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002222}
2223
2224ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2225 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002226
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002227/*
2228 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002229 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2230 * login client UUID generation")
2231 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002232 *
2233 * xtest skips the test when not built with OpenSSL.
2234 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002235static void xtest_tee_test_1028(ADBG_Case_t *c)
2236{
Victor Chong8e070bc2020-05-13 09:59:33 +01002237#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002238 TEEC_Result result = TEEC_ERROR_GENERIC;
2239 uint32_t ret_orig = 0;
2240 TEEC_Session session = { };
2241 uint32_t login = UINT32_MAX;
2242 TEEC_UUID client_uuid = { };
2243 TEEC_UUID expected_client_uuid = { };
2244 TEEC_UUID uuid_ns = { };
2245 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2246 uint32_t group = 0;
2247
2248 group = getegid();
2249
2250 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2251
2252 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2253 return;
2254
2255 sprintf(uuid_name, "gid=%x", group);
2256
2257 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2258 strlen(uuid_name));
2259 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2260 return;
2261
2262 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2263 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2264
2265 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2266 return;
2267
2268 result = ta_os_test_cmd_client_identity(&session, &login,
2269 &client_uuid);
2270
2271 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2272 goto out;
2273
2274 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2275
2276 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2277 sizeof(TEEC_UUID));
2278
2279out:
2280 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002281#else /*!OPENSSL_FOUND*/
2282 UNUSED(c);
2283 /* xtest_uuid_v5() depends on OpenSSL */
2284 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2285#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002286}
2287
2288ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2289 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002290
2291static void xtest_tee_test_1029(ADBG_Case_t *c)
2292{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002293 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002294 TEEC_Session session = { 0 };
2295 uint32_t ret_orig = 0;
2296
2297 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2298 xtest_teec_open_session(&session, &os_test_ta_uuid,
2299 NULL, &ret_orig)))
2300 return;
2301
2302 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002303 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2304 &ret_orig);
2305 if (res == TEEC_ERROR_NOT_SUPPORTED)
2306 Do_ADBG_Log(" - 1029 - skip test, "
2307 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2308 else
2309 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002310 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2311
2312 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002313 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2314 &ret_orig);
2315 if (res == TEEC_ERROR_NOT_SUPPORTED)
2316 Do_ADBG_Log(" - 1029 - skip test, "
2317 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2318 else
2319 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002320 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2321
2322 TEEC_CloseSession(&session);
2323}
2324ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2325 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002326
2327static void xtest_tee_test_1030(ADBG_Case_t *c)
2328{
2329 TEEC_Session session = { 0 };
2330 uint32_t ret_orig = 0;
2331
2332 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2333 xtest_teec_open_session(&session, &os_test_ta_uuid,
2334 NULL, &ret_orig)))
2335 return;
2336
2337 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2338 ADBG_EXPECT_TEEC_SUCCESS(c,
2339 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2340 &ret_orig));
2341 Do_ADBG_EndSubCase(c, "Before dlopen()");
2342
2343 Do_ADBG_BeginSubCase(c, "After dlopen()");
2344 ADBG_EXPECT_TEEC_SUCCESS(c,
2345 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2346 &ret_orig));
2347 Do_ADBG_EndSubCase(c, "After dlopen()");
2348
2349 TEEC_CloseSession(&session);
2350}
2351ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2352 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002353
2354#ifndef __clang__
2355static void xtest_tee_test_1031(ADBG_Case_t *c)
2356{
2357 TEEC_Result ret = TEE_SUCCESS;
2358 TEEC_Session session = { 0 };
2359 uint32_t ret_orig = 0;
2360
2361 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2362 xtest_teec_open_session(&session, &os_test_ta_uuid,
2363 NULL, &ret_orig)))
2364 return;
2365
2366 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2367 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2368 &ret_orig);
2369 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2370 printf("TA not built with C++ support, skipping C++ tests\n");
2371 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2372 goto out;
2373
2374 }
2375 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2376 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2377
2378 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2379 ADBG_EXPECT_TEEC_SUCCESS(c,
2380 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2381 NULL, &ret_orig));
2382 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2383
2384 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2385 ADBG_EXPECT_TEEC_SUCCESS(c,
2386 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2387 NULL, &ret_orig));
2388 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2389
2390 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2391 ADBG_EXPECT_TEEC_SUCCESS(c,
2392 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2393 &ret_orig));
2394 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2395
2396 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2397 ADBG_EXPECT_TEEC_SUCCESS(c,
2398 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2399 &ret_orig));
2400 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2401out:
2402 TEEC_CloseSession(&session);
2403}
2404ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2405 "Test C++ features");
2406#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002407
2408static void xtest_tee_test_1032(ADBG_Case_t *c)
2409{
2410 TEEC_Result res = TEEC_SUCCESS;
2411 TEEC_Context ctx = { };
2412 TEEC_SharedMemory shm1 = {
2413 .buffer = xtest_tee_test_1032,
2414 .size = 32,
2415 .flags = TEEC_MEM_INPUT,
2416 };
2417 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2418 TEEC_SharedMemory shm2 = {
2419 .buffer = (void *)dummy_data,
2420 .size = sizeof(dummy_data),
2421 .flags = TEEC_MEM_INPUT,
2422 };
2423
2424 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2425 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2426 return;
2427
2428 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2429 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2430 TEEC_ReleaseSharedMemory(&shm1);
2431
2432 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2433 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2434 TEEC_ReleaseSharedMemory(&shm2);
2435
2436 TEEC_FinalizeContext(&ctx);
2437}
2438ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2439 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002440
2441static void xtest_tee_test_1033(ADBG_Case_t *c)
2442{
2443 TEEC_Session session = { };
2444 uint32_t ret_orig = 0;
2445
2446 /* TA will ping the test plugin during open session operation */
2447 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2448 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2449 NULL, &ret_orig)))
2450 return;
2451
2452 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2453 {
2454 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2455
2456 op.params[0].value.a = 20;
2457 op.params[0].value.b = 10;
2458 op.params[1].value.a = '+';
2459 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2460 TEEC_VALUE_INPUT, TEEC_NONE,
2461 TEEC_NONE);
2462
2463 ADBG_EXPECT_TEEC_SUCCESS(c,
2464 TEEC_InvokeCommand(&session,
2465 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2466 &ret_orig));
2467 ADBG_EXPECT(c, 30, op.params[0].value.a);
2468
2469 /* reassign, because the values was changed during previous op */
2470 op.params[0].value.a = 20;
2471 op.params[0].value.b = 10;
2472 op.params[1].value.a = '-';
2473 ADBG_EXPECT_TEEC_SUCCESS(c,
2474 TEEC_InvokeCommand(&session,
2475 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2476 &ret_orig));
2477 ADBG_EXPECT(c, 10, op.params[0].value.a);
2478 }
2479 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2480
2481 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2482 {
2483 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002484 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002485
2486 op.params[0].tmpref.buffer = to_plugin;
2487 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002488 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002489 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002490 TEEC_NONE, TEEC_NONE);
2491
2492 ADBG_EXPECT_TEEC_SUCCESS(c,
2493 TEEC_InvokeCommand(&session,
2494 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2495 &op, &ret_orig));
2496
2497 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002498 * The test plugin must calculate a sum of the input elements
2499 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002500 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002501 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002502 }
2503 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2504
2505 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2506 {
2507 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2508 char from_plugin[64] = { };
2509 char expected_arr[] = "Array from plugin";
2510 size_t expectes_size = sizeof(expected_arr);
2511
2512 op.params[0].tmpref.buffer = from_plugin;
2513 op.params[0].tmpref.size = sizeof(from_plugin);
2514 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2515 TEEC_VALUE_OUTPUT, TEEC_NONE,
2516 TEEC_NONE);
2517 ADBG_EXPECT_TEEC_SUCCESS(c,
2518 TEEC_InvokeCommand(&session,
2519 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2520 &ret_orig));
2521 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2522 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2523 }
2524 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2525
2526 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2527 {
2528 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2529
2530 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2531 TEEC_NONE, TEEC_NONE);
2532 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2533 TEEC_InvokeCommand(&session,
2534 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2535 &ret_orig));
2536 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2537 TEEC_InvokeCommand(&session,
2538 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2539 &ret_orig));
2540 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2541 TEEC_InvokeCommand(&session,
2542 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2543 &ret_orig));
2544 }
2545 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2546
2547 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2548 {
2549 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2550
2551 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2552 TEEC_NONE, TEEC_NONE);
2553 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2554 TEEC_InvokeCommand(&session,
2555 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2556 &op, &ret_orig));
2557 }
2558 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2559
2560 TEEC_CloseSession(&session);
2561}
2562ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2563 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002564
2565static void xtest_tee_test_1034(ADBG_Case_t *c)
2566{
2567 TEEC_Result res = TEEC_SUCCESS;
2568 TEEC_Session session = { };
2569 uint32_t ret_orig = 0;
2570
2571 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2572 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002573 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2574 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
2575 } else {
2576 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002577 TEEC_CloseSession(&session);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002578 }
Jens Wiklander94fb1582021-05-19 10:14:57 +02002579}
2580ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2581 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302582
2583#if defined(CFG_TA_BTI)
2584struct bti_test {
2585 uint32_t cmd;
2586 uint32_t func;
2587};
2588
2589#define BTI_TEST(caller_func, bti_func) { \
2590 .cmd = caller_func, \
2591 .func = bti_func, \
2592 }
2593
2594static const struct bti_test bti_cases_success[] = {
2595 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2596 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2597 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2598 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2599 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2600 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2601 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2602};
2603
2604static const struct bti_test bti_cases_panic[] = {
2605 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2606 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2607 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2608 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2609 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2610};
2611
2612static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2613{
2614 TEEC_Session session = {};
2615 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2616 uint32_t ret_orig = 0;
2617 TEEC_Result res;
2618
2619 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2620 TEEC_NONE);
2621 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2622 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2623 NULL, &ret_orig)))
2624 return;
2625
2626 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2627 &ret_orig);
2628 if (!res) {
2629 if(op.params[0].value.a)
2630 *bti = true;
2631 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2632 }
2633
2634 TEEC_CloseSession(&session);
2635}
2636
2637static void xtest_tee_test_1035(ADBG_Case_t *c)
2638{
2639 TEEC_Session session = {};
2640 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2641 struct bti_test const *test = NULL;
2642 uint32_t ret_orig = 0;
2643 TEEC_Result res;
2644 unsigned int n = 0;
2645 bool cpu_feature_bti = false;
2646
2647 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2648 get_cpu_feature(c, &cpu_feature_bti);
2649 Do_ADBG_EndSubCase(c, "BTI Implemented");
2650
2651 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2652 TEEC_NONE);
2653
2654 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2655 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2656 NULL, &ret_orig)))
2657 return;
2658
2659 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2660 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2661 test = &bti_cases_success[n];
2662
2663 op.params[0].value.a = test->func;
2664
2665 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2666 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2667 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2668 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2669 TEEC_CloseSession(&session);
2670 return;
2671 }
2672
2673 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2674
2675 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2676
2677 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2678 }
2679 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2680
2681 TEEC_CloseSession(&session);
2682
2683 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2684 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2685 test = &bti_cases_panic[n];
2686 res = TEEC_SUCCESS;
2687
2688 if (cpu_feature_bti)
2689 res = TEEC_ERROR_TARGET_DEAD;
2690
2691 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2692 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2693 NULL, &ret_orig)))
2694 goto out;
2695
2696 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2697 op.params[0].value.a = test->func;
2698
2699 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2700 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2701
2702 if (cpu_feature_bti)
2703 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2704 ret_orig);
2705
2706 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2707
2708 TEEC_CloseSession(&session);
2709 }
2710
2711out:
2712 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2713}
2714ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2715#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302716
2717static void xtest_tee_test_1036(ADBG_Case_t *c)
2718{
2719 TEEC_Session session = { };
2720 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2721 uint32_t ret_orig = 0;
2722 TEEC_Result res = TEEC_SUCCESS;
2723
2724 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2725 TEEC_NONE);
2726
2727 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2728 xtest_teec_open_session(&session, &os_test_ta_uuid,
2729 NULL, &ret_orig)))
2730 return;
2731
2732 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2733
2734 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2735 &ret_orig);
2736 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2737 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2738 goto out;
2739 }
2740
2741 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2742 goto out;
2743
2744 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2745
2746 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2747
2748 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2749 TEEC_InvokeCommand(&session,
2750 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2751 &op, &ret_orig));
2752
2753 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2754
2755 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2756
2757out:
2758 TEEC_CloseSession(&session);
2759}
2760ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2761 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002762
2763#define ATT_MAX_KEYSZ 4096
2764
2765#ifdef OPENSSL_FOUND
2766static RSA *att_key;
2767static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2768
2769/*
2770 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2771 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2772 */
2773static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2774 uint8_t *buf)
2775{
2776 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2777 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2778 unsigned char *ta_hash = buf;
2779 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2780 SHA256_CTX ctx = { };
2781 int salt_len = 32; /* Hard-coded in the PTA */
2782 int st = 0;
2783
2784 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2785 return;
2786
2787 SHA256_Init(&ctx);
2788 SHA256_Update(&ctx, nonce, nonce_size);
2789 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2790 SHA256_Final(digest, &ctx);
2791
2792 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2793 RSA_NO_PADDING);
2794 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2795 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2796 EVP_sha256(), decr, salt_len);
2797 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2798}
2799
2800static void free_att_key(void)
2801{
2802 RSA_free(att_key);
2803 att_key = NULL;
2804}
2805
Clement Faure44a31d02022-03-30 10:50:52 +02002806#if OPENSSL_VERSION_NUMBER < 0x10100000L
2807static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2808{
2809 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2810 return 0;
2811
2812 if (n != NULL) {
2813 BN_free(r->n);
2814 r->n = n;
2815 }
2816
2817 if (e != NULL) {
2818 BN_free(r->e);
2819 r->e = e;
2820 }
2821
2822 if (d != NULL) {
2823 BN_free(r->d);
2824 r->d = d;
2825 }
2826
2827 return 1;
2828}
2829#endif
2830
Jerome Forissier0915b222021-10-27 18:20:59 +02002831static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2832 size_t n_sz)
2833{
2834 BIGNUM *bn_e = NULL;
2835 BIGNUM *bn_n = NULL;
2836 int st = 0;
2837
2838 att_key_size = n_sz;
2839 att_key = RSA_new();
2840 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2841 return;
2842
2843 bn_e = BN_bin2bn(e, e_sz, NULL);
2844 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2845 goto err;
2846 bn_n = BN_bin2bn(n, n_sz, NULL);
2847 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2848 goto err;
2849
2850 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2851 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2852 goto err;
2853 return;
2854err:
2855 free_att_key();
2856}
2857#else
2858#define check_signature(...)
2859#define set_att_key(...)
2860#define free_att_key()
2861#endif
2862
2863/*
2864 * Verification of the output of the attestation PTA
2865 * - (If hash != NULL) check buf contains the expected hash
2866 * - (If OpenSSL is available) Check that the signature is valid
2867 */
2868static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2869 uint8_t *hash, uint8_t *buf)
2870{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002871 (void)nonce;
2872 (void)nonce_size;
2873
Jerome Forissier0915b222021-10-27 18:20:59 +02002874 if (hash)
2875 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2876
2877 check_signature(c, nonce, nonce_size, buf);
2878}
2879
2880/* Invoke attestation PTA to return the public key */
2881static void get_att_public_key(ADBG_Case_t *c)
2882{
2883 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2884 uint8_t e[3] = { }; /* We know e == 65537... */
2885 size_t n_sz = sizeof(n);
2886 size_t e_sz = sizeof(e);
2887 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2888 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2889 TEEC_Result res = TEEC_ERROR_GENERIC;
2890 TEEC_Session session = { };
2891 uint32_t ret_orig = 0;
2892
2893 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2894 TEEC_MEMREF_TEMP_OUTPUT,
2895 TEEC_VALUE_OUTPUT, TEEC_NONE);
2896 op.params[0].tmpref.buffer = e;
2897 op.params[0].tmpref.size = e_sz;
2898 op.params[1].tmpref.buffer = n;
2899 op.params[1].tmpref.size = n_sz;
2900
2901 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2902 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2903 return;
2904
2905 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2906 &ret_orig);
2907
2908 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2909 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2910 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2911 goto out;
2912
2913 e_sz = op.params[0].tmpref.size;
2914 n_sz = op.params[1].tmpref.size;
2915 set_att_key(c, e, e_sz, n, n_sz);
2916out:
2917 TEEC_CloseSession(&session);
2918}
2919
2920/* Invoke attestation PTA to hash the TEE binary */
2921static void attestation_tee(ADBG_Case_t *c)
2922{
2923 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2924 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2925 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
2926 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2927 TEEC_Result res = TEEC_ERROR_GENERIC;
2928 TEEC_Session session = { };
2929 uint32_t ret_orig = 0;
2930
2931 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2932 TEEC_MEMREF_TEMP_OUTPUT,
2933 TEEC_NONE, TEEC_NONE);
2934 op.params[0].tmpref.buffer = nonce;
2935 op.params[0].tmpref.size = sizeof(nonce);
2936 op.params[1].tmpref.buffer = measurement;
2937 op.params[1].tmpref.size = sizeof(measurement);
2938
2939 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2940 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2941 return;
2942
2943 /* Hash TEE and check signature */
2944 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2945 &ret_orig);
2946 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2947 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2948
2949 TEEC_CloseSession(&session);
2950}
2951
2952/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2953static void attestation_ta_shdr(ADBG_Case_t *c)
2954{
2955 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2956 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2957 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2958 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2959 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2960 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2961 TEEC_Result res = TEEC_ERROR_GENERIC;
2962 TEEC_Session session = { };
2963 uint32_t ret_orig = 0;
2964 int cmp = 0;
2965
2966 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2967 TEEC_MEMREF_TEMP_INPUT,
2968 TEEC_MEMREF_TEMP_OUTPUT,
2969 TEEC_NONE);
2970 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
2971 op.params[0].tmpref.size = sizeof(TEEC_UUID);
2972 op.params[1].tmpref.buffer = nonce;
2973 op.params[1].tmpref.size = sizeof(nonce);
2974 op.params[2].tmpref.buffer = measurement;
2975 op.params[2].tmpref.size = sizeof(measurement);
2976
2977 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2978 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2979 return;
2980
2981 /* Hash TA and check signature */
2982 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2983 &op, &ret_orig);
2984 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2985 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2986 /* Save hash */
2987 memcpy(hash1, measurement, 32);
2988
2989 /* Hash TA again */
2990 memset(measurement, 0, sizeof(measurement));
2991 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
2992 &op, &ret_orig);
2993 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2994 /* New hash should be identical */
2995 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
2996
2997 /* Hash another TA */
2998 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
2999 memset(measurement, 0, sizeof(measurement));
3000 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3001 &op, &ret_orig);
3002 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3003 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3004 memcpy(hash2, measurement, 32);
3005 /* Different binaries should have different hashes */
3006 cmp = memcmp(hash1, hash2, sizeof(hash1));
3007 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3008
3009 TEEC_CloseSession(&session);
3010}
3011
3012/*
3013 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
3014 * itself.
3015 */
3016static void attestation_ta_memory(ADBG_Case_t *c)
3017{
3018 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3019 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
3020 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
3021 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
3022 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
3023 TEEC_Result res = TEEC_ERROR_GENERIC;
3024 TEEC_Session session = { };
3025 uint32_t ret_orig = 0;
3026 int cmp = 0;
3027
3028 Do_ADBG_BeginSubCase(c, "Consecutive calls");
3029
3030 /* Open session to os_test TA */
3031 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3032 &ret_orig);
3033 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3034 return;
3035
3036 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3037 TEEC_MEMREF_TEMP_OUTPUT,
3038 TEEC_NONE, TEEC_NONE);
3039 op.params[0].tmpref.buffer = nonce;
3040 op.params[0].tmpref.size = sizeof(nonce);
3041 op.params[1].tmpref.buffer = measurement;
3042 op.params[1].tmpref.size = sizeof(measurement);
3043
3044 /* Hash TA */
3045 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3046 &ret_orig);
3047 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3048
3049 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3050 memcpy(hash1, measurement, 32);
3051
3052 /* Hash TA again */
3053 memset(measurement, 0, sizeof(measurement));
3054 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3055 &ret_orig);
3056 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3057
3058 /* New hash should be identical to hash1 */
3059 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3060
3061 Do_ADBG_EndSubCase(c, "Consecutive calls");
3062
3063 /* Close TA session, will cause unload of TA */
3064 TEEC_CloseSession(&session);
3065
3066 Do_ADBG_BeginSubCase(c, "TA reload");
3067
3068 /* Load TA again and open a new session */
3069 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3070 &ret_orig);
3071 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3072 if (res)
3073 return;
3074
3075 /* Hash TA one more time */
3076 memset(measurement, 0, sizeof(measurement));
3077 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3078 &ret_orig);
3079 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3080
3081 /* Hash after reload should still be the same */
3082 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3083
3084 Do_ADBG_EndSubCase(c, "TA reload");
3085
3086 Do_ADBG_BeginSubCase(c, "Add shared library");
3087
3088 /*
3089 * Invoke a TA command that causes some additional code to be mapped
3090 * (shared library)
3091 */
3092 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3093 &ret_orig);
3094 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3095
3096 /* Hash TA one last time */
3097 memset(measurement, 0, sizeof(measurement));
3098 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3099 &ret_orig);
3100 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3101
3102 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3103 memcpy(hash2, measurement, 32);
3104
3105 /* Different binaries mapped mean different hashes */
3106 cmp = memcmp(hash1, hash2, sizeof(hash1));
3107 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3108
3109 Do_ADBG_EndSubCase(c, "Add shared library");
3110
3111 TEEC_CloseSession(&session);
3112}
3113
3114static void xtest_tee_test_1037(ADBG_Case_t *c)
3115{
3116 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3117 TEEC_Result res = TEEC_ERROR_GENERIC;
3118 TEEC_Session session = { };
3119 uint32_t ret_orig = 0;
3120
3121 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3122 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3123 Do_ADBG_Log(" skip test, pseudo TA not found");
3124 return;
3125 }
3126
chenchaokaiaf751932023-05-29 14:25:50 +08003127 TEEC_CloseSession(&session);
3128
Jerome Forissier0915b222021-10-27 18:20:59 +02003129 Do_ADBG_BeginSubCase(c, "Get public key");
3130 get_att_public_key(c);
3131 Do_ADBG_EndSubCase(c, "Get public key");
3132
3133 Do_ADBG_BeginSubCase(c, "TEE attestation");
3134 attestation_tee(c);
3135 Do_ADBG_EndSubCase(c, "TEE attestation");
3136
3137 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3138 attestation_ta_shdr(c);
3139 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3140
3141 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3142 attestation_ta_memory(c);
3143 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3144
3145 free_att_key();
3146}
3147ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3148 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003149
3150static void xtest_tee_test_1038(ADBG_Case_t *c)
3151{
3152 TEEC_Session session = { };
3153 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3154 uint32_t ret_orig = 0;
3155 TEEC_Result res = TEEC_SUCCESS;
3156
3157 Do_ADBG_BeginSubCase(c, "MTE use after free");
3158
3159 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3160 TEEC_NONE);
3161
3162 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3163 xtest_teec_open_session(&session, &os_test_ta_uuid,
3164 NULL, &ret_orig)))
3165 return;
3166
3167 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3168 &op, &ret_orig);
3169 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3170 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3171 goto out;
3172 }
3173
3174 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3175 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3176 TEEC_CloseSession(&session);
3177
3178 Do_ADBG_EndSubCase(c, "MTE use after free");
3179
3180 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3181
3182 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3183 TEEC_NONE);
3184
3185 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3186 xtest_teec_open_session(&session, &os_test_ta_uuid,
3187 NULL, &ret_orig)))
3188 return;
3189
3190 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3191 &op, &ret_orig);
3192 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3193 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3194 goto out;
3195 }
3196
3197 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3198 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3199
3200 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3201
3202 Do_ADBG_BeginSubCase(c, "MTE double free");
3203
3204 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3205 TEEC_NONE);
3206
3207 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3208 xtest_teec_open_session(&session, &os_test_ta_uuid,
3209 NULL, &ret_orig)))
3210 return;
3211
3212 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3213 &op, &ret_orig);
3214 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3215 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3216 goto out;
3217 }
3218
3219 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3220 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3221
3222 Do_ADBG_EndSubCase(c, "MTE double free");
3223
3224 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3225
3226 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3227 TEEC_NONE);
3228
3229 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3230 xtest_teec_open_session(&session, &os_test_ta_uuid,
3231 NULL, &ret_orig)))
3232 return;
3233
3234 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3235 &op, &ret_orig);
3236 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3237 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3238 goto out;
3239 }
3240
3241 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3242 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3243
3244 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3245
3246
3247out:
3248 TEEC_CloseSession(&session);
3249}
3250ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3251 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003252
3253static void xtest_tee_test_1039(ADBG_Case_t *c)
3254{
3255 TEEC_Session session = { };
3256 uint32_t ret_orig = 0;
3257
3258 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3259 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3260 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3261 NULL, &ret_orig)))
3262 TEEC_CloseSession(&session);
3263 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3264
3265 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3266 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3267 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3268 NULL, &ret_orig)))
3269 TEEC_CloseSession(&session);
3270 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3271
3272}
3273
3274
3275ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3276 "Test subkey verification");