blob: a4277890d8e74a89f13738e9719f65b07d6d99ee [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
Clément Faurea15be9e2025-05-07 13:54:36 +000023#include <sys/stat.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020024#include <signed_hdr.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020025#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010026#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020027#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060028#include <sys/types.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020029#include <ta_arm_bti.h>
30#include <ta_concurrent.h>
31#include <ta_create_fail_test.h>
32#include <ta_crypt.h>
33#include <ta_miss_test.h>
34#include <ta_os_test.h>
35#include <ta_rpc_test.h>
36#include <ta_sims_keepalive_test.h>
37#include <ta_sims_test.h>
38#include <ta_supp_plugin.h>
39#include <ta_tpm_log_test.h>
40#include <test_supp_plugin.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010041#include <unistd.h>
Jerome Forissier0915b222021-10-27 18:20:59 +020042#include <utee_defines.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010043#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020044
Jens Wiklander6b5d9a32024-12-10 17:43:47 +010045#include "ftpm_ta.h"
Jerome Forissier0915b222021-10-27 18:20:59 +020046#include "xtest_helpers.h"
47#include "xtest_test.h"
48#include "xtest_uuid_helpers.h"
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +030049
Jens Wiklanderec545fb2017-11-24 16:58:07 +010050#ifndef MIN
51#define MIN(a, b) ((a) < (b) ? (a) : (b))
52#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020053
Etienne Carrierec0a61722023-03-07 17:47:42 +010054#define STATS_UUID \
55 { 0xd96a5b40, 0xe2c7, 0xb1af, \
56 { 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } }
57
58#define STATS_CMD_PAGER_STATS 0
59
60#define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096)
61
Pascal Brandc639ac82015-07-02 08:53:34 +020062struct xtest_crypto_session {
63 ADBG_Case_t *c;
64 TEEC_Session *session;
65 uint32_t cmd_id_sha256;
66 uint32_t cmd_id_aes256ecb_encrypt;
67 uint32_t cmd_id_aes256ecb_decrypt;
68};
69
Etienne Carrierec0a61722023-03-07 17:47:42 +010070static bool optee_pager_with_small_pool(void)
71{
72 TEEC_Result res = TEEC_ERROR_GENERIC;
73 TEEC_UUID uuid = STATS_UUID;
74 TEEC_Context ctx = { };
75 TEEC_Session sess = { };
76 TEEC_Operation op = { };
77 uint32_t eo = 0;
78 bool rc = false;
79
80 res = TEEC_InitializeContext(NULL, &ctx);
81 if (res)
82 return false;
83
84 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
85 NULL, &eo);
86 if (res)
87 goto out_ctx;
88
89 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_VALUE_OUTPUT,
90 TEEC_VALUE_OUTPUT, TEEC_NONE);
91 res = TEEC_InvokeCommand(&sess, STATS_CMD_PAGER_STATS, &op, &eo);
92 if (res)
93 goto out_sess;
94
95 if (op.params[0].value.b &&
96 op.params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD)
97 rc = true;
98
99out_sess:
100 TEEC_CloseSession(&sess);
101out_ctx:
102 TEEC_FinalizeContext(&ctx);
103
104 return rc;
105}
106
Pascal Brandc639ac82015-07-02 08:53:34 +0200107static void xtest_crypto_test(struct xtest_crypto_session *cs)
108{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100109 uint32_t ret_orig = 0;
110 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200111 uint8_t crypt_in[16] = { 22, 17 };
112
113 crypt_in[15] = 60;
114
115 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
116 {
117 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
118
119 op.params[0].tmpref.buffer = crypt_in;
120 op.params[0].tmpref.size = sizeof(crypt_in);
121 op.params[1].tmpref.buffer = crypt_out;
122 op.params[1].tmpref.size = sizeof(crypt_out);
123 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
124 TEEC_MEMREF_TEMP_OUTPUT,
125 TEEC_NONE, TEEC_NONE);
126
127 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
128 TEEC_InvokeCommand(cs->session,
129 cs->
130 cmd_id_aes256ecb_encrypt,
131 &op,
132 &ret_orig));
133 }
134 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
135
136 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
137 {
138 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100139 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200140
141 op.params[0].tmpref.buffer = crypt_out;
142 op.params[0].tmpref.size = sizeof(crypt_out);
143 op.params[1].tmpref.buffer = out;
144 op.params[1].tmpref.size = sizeof(out);
145 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
146 TEEC_MEMREF_TEMP_OUTPUT,
147 TEEC_NONE, TEEC_NONE);
148
149 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
150 TEEC_InvokeCommand(cs->session,
151 cs->
152 cmd_id_aes256ecb_decrypt,
153 &op,
154 &ret_orig));
155
156 if (!ADBG_EXPECT(cs->c, 0,
157 memcmp(crypt_in, out, sizeof(crypt_in)))) {
158 Do_ADBG_Log("crypt_in:");
159 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
160 Do_ADBG_Log("out:");
161 Do_ADBG_HexLog(out, sizeof(out), 16);
162 }
163 }
164 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
165
166 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
167 {
168 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
169 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
170 static const uint8_t sha256_out[] = {
171 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
172 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
173 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
174 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
175 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100176 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200177
178 op.params[0].tmpref.buffer = (void *)sha256_in;
179 op.params[0].tmpref.size = sizeof(sha256_in);
180 op.params[1].tmpref.buffer = out;
181 op.params[1].tmpref.size = sizeof(out);
182 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
183 TEEC_MEMREF_TEMP_OUTPUT,
184 TEEC_NONE, TEEC_NONE);
185
186 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
187 TEEC_InvokeCommand(cs->session,
188 cs->
189 cmd_id_sha256,
190 &op,
191 &ret_orig));
192
193 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
194 sizeof(sha256_out)))) {
195 Do_ADBG_Log("sha256_out:");
196 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
197 Do_ADBG_Log("out:");
198 Do_ADBG_HexLog(out, sizeof(out), 16);
199 }
200 }
201 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
202
Etienne Carrierea3198522017-10-26 09:48:55 +0200203 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200204 {
205 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
206 static const uint8_t in[] = {
207 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
208 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
209 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
210 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
211 };
212 static const uint8_t exp_out[] = {
213 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
214 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
215 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
216 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
217 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100218 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200219
220 op.params[0].tmpref.buffer = (void *)in;
221 op.params[0].tmpref.size = sizeof(in);
222 op.params[1].tmpref.buffer = out;
223 op.params[1].tmpref.size = sizeof(out);
224 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
225 TEEC_MEMREF_TEMP_OUTPUT,
226 TEEC_NONE, TEEC_NONE);
227
228 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
229 TEEC_InvokeCommand(cs->session,
230 cs->
231 cmd_id_aes256ecb_encrypt,
232 &op,
233 &ret_orig));
234
235 if (!ADBG_EXPECT(cs->c, 0,
236 memcmp(exp_out, out, sizeof(exp_out)))) {
237 Do_ADBG_Log("exp_out:");
238 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
239 Do_ADBG_Log("out:");
240 Do_ADBG_HexLog(out, sizeof(out), 16);
241 }
242 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200243 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200244
Etienne Carrierea3198522017-10-26 09:48:55 +0200245 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200246 {
247 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
248 static const uint8_t in[] = {
249 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
250 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
251 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
252 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
253 };
254 static const uint8_t exp_out[] = {
255 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
256 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
257 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
258 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
259 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100260 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200261
262 op.params[0].tmpref.buffer = (void *)in;
263 op.params[0].tmpref.size = sizeof(in);
264 op.params[1].tmpref.buffer = out;
265 op.params[1].tmpref.size = sizeof(out);
266 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
267 TEEC_MEMREF_TEMP_OUTPUT,
268 TEEC_NONE, TEEC_NONE);
269
270 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
271 TEEC_InvokeCommand(cs->session,
272 cs->
273 cmd_id_aes256ecb_decrypt,
274 &op,
275 &ret_orig));
276
277 if (!ADBG_EXPECT(cs->c, 0,
278 memcmp(exp_out, out, sizeof(exp_out)))) {
279 Do_ADBG_Log("exp_out:");
280 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
281 Do_ADBG_Log("out:");
282 Do_ADBG_HexLog(out, sizeof(out), 16);
283 }
284 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200285 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200286}
287
288static void xtest_tee_test_1001(ADBG_Case_t *c)
289{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100290 TEEC_Result res = TEEC_ERROR_GENERIC;
291 TEEC_Session session = { };
292 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200293
Etienne Carriere11093162017-10-26 09:49:04 +0200294 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100295 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100296 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200297 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
298 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100299 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200300 }
Etienne Carriere3ea96722022-03-11 09:16:40 +0100301 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
302 return;
303
304 Do_ADBG_BeginSubCase(c, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200305
Raymond Mao658859f2025-03-25 09:59:47 -0700306 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_SELF_TESTS,
307 NULL, &ret_orig);
308 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Etienne Carriere3ea96722022-03-11 09:16:40 +0100309
310 Do_ADBG_EndSubCase(c, "Core self tests");
311
312 Do_ADBG_BeginSubCase(c, "Core dt_driver self tests");
313
Raymond Mao658859f2025-03-25 09:59:47 -0700314 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS,
315 NULL, &ret_orig);
316 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Etienne Carriere3ea96722022-03-11 09:16:40 +0100317
318 Do_ADBG_EndSubCase(c, "Core dt_driver self tests");
319
Raymond Mao658859f2025-03-25 09:59:47 -0700320 Do_ADBG_BeginSubCase(c, "Core transfer list self tests");
321
322 res = TEEC_InvokeCommand(&session,
323 PTA_INVOKE_TESTS_CMD_TRANSFER_LIST_TESTS, NULL,
324 &ret_orig);
325 if (res == TEE_ERROR_NOT_SUPPORTED)
326 Do_ADBG_Log("Transfer List tests not supported, skipping");
327 else
328 ADBG_EXPECT_TEEC_SUCCESS(c, res);
329
330 Do_ADBG_EndSubCase(c, "Core transfer list self tests");
331
Jens Wiklandercf16e842016-02-10 09:07:09 +0100332 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200333}
Jens Wiklander14f48872018-06-29 15:30:13 +0200334ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200335
Jens Wiklander1d70a112017-10-16 15:16:39 +0200336static void xtest_tee_test_1002(ADBG_Case_t *c)
337{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100338 TEEC_Result res = TEEC_ERROR_GENERIC;
339 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200340 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100341 uint32_t ret_orig = 0;
342 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200343 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100344 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200345
Etienne Carriere11093162017-10-26 09:49:04 +0200346 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200347 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
348 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200349 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
350 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200351 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200352 }
353 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200354
355 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
356 TEEC_NONE, TEEC_NONE);
357 op.params[0].tmpref.size = sizeof(buf);
358 op.params[0].tmpref.buffer = buf;
359
360 for (n = 0; n < sizeof(buf); n++)
361 buf[n] = n + 1;
362 for (n = 0; n < sizeof(buf); n++)
363 exp_sum += buf[n];
364
365 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
366 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
367 goto out;
368
369 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
370out:
371 TEEC_CloseSession(&session);
372}
Jens Wiklander14f48872018-06-29 15:30:13 +0200373ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200374
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100375struct test_1003_arg {
376 uint32_t test_type;
377 size_t repeat;
378 size_t max_before_lockers;
379 size_t max_during_lockers;
380 size_t before_lockers;
381 size_t during_lockers;
382 TEEC_Result res;
383 uint32_t error_orig;
384};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200385
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100386static void *test_1003_thread(void *arg)
387{
388 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100389 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100390 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100391 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100392
393 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
394 NULL, &a->error_orig);
395 if (a->res != TEEC_SUCCESS)
396 return NULL;
397
398 for (n = 0; n < a->repeat; n++) {
399 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
400
401 op.params[0].value.a = a->test_type;
402 op.params[0].value.b = rounds;
403
404 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
405 TEEC_VALUE_OUTPUT,
406 TEEC_NONE, TEEC_NONE);
407 a->res = TEEC_InvokeCommand(&session,
408 PTA_INVOKE_TESTS_CMD_MUTEX,
409 &op, &a->error_orig);
410 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
411 op.params[1].value.b != 1) {
412 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
413 a->res = TEEC_ERROR_BAD_STATE;
414 a->error_orig = 42;
415 break;
416 }
417
418 if (a->test_type == PTA_MUTEX_TEST_READER) {
419 if (op.params[1].value.a > a->max_before_lockers)
420 a->max_before_lockers = op.params[1].value.a;
421
422 if (op.params[1].value.b > a->max_during_lockers)
423 a->max_during_lockers = op.params[1].value.b;
424
425 a->before_lockers += op.params[1].value.a;
426 a->during_lockers += op.params[1].value.b;
427 }
428 }
429 TEEC_CloseSession(&session);
430
431 return NULL;
432}
433
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100434#define TEST_1003_THREAD_COUNT (3 * 2)
435
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100436static void xtest_tee_test_1003(ADBG_Case_t *c)
437{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100438 TEEC_Result res = TEEC_ERROR_GENERIC;
439 TEEC_Session session = { };
440 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100441 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100442 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100443 size_t max_read_concurrency = 0;
444 size_t max_read_waiters = 0;
445 size_t num_concurrent_read_lockers = 0;
446 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100447 size_t n = 0;
448 size_t nt = TEST_1003_THREAD_COUNT;
449 double mean_read_concurrency = 0;
450 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100451 size_t num_writers = 0;
452 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100453 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100454
455 /* Pseudo TA is optional: warn and nicely exit if not found */
456 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
457 &ret_orig);
458 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
459 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
460 return;
461 }
462 ADBG_EXPECT_TEEC_SUCCESS(c, res);
463 TEEC_CloseSession(&session);
464
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100465 for (n = 0; n < nt; n++) {
466 if (n % 3) {
467 arg[n].test_type = PTA_MUTEX_TEST_READER;
468 num_readers++;
469 } else {
470 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
471 num_writers++;
472 }
473 arg[n].repeat = repeat;
474 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
475 test_1003_thread, arg + n)))
476 nt = n; /* break loop and start cleanup */
477 }
478
479 for (n = 0; n < nt; n++) {
480 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
481 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
482 Do_ADBG_Log("error origin %" PRIu32,
483 arg[n].error_orig);
484 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
485 if (arg[n].max_during_lockers > max_read_concurrency)
486 max_read_concurrency =
487 arg[n].max_during_lockers;
488
489 if (arg[n].max_before_lockers > max_read_waiters)
490 max_read_waiters = arg[n].max_before_lockers;
491
492 num_concurrent_read_lockers += arg[n].during_lockers;
493 num_concurrent_read_waiters += arg[n].before_lockers;
494 }
495 }
496
497 mean_read_concurrency = (double)num_concurrent_read_lockers /
498 (double)(repeat * num_readers);
499 mean_read_waiters = (double)num_concurrent_read_waiters /
500 (double)(repeat * num_readers);
501
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100502 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
503 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100504 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
505 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
506 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
507 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
508}
Jens Wiklander14f48872018-06-29 15:30:13 +0200509ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
510 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200511
Pascal Brandc639ac82015-07-02 08:53:34 +0200512static void xtest_tee_test_1004(ADBG_Case_t *c)
513{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100514 TEEC_Session session = { };
515 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200516 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
517 TA_CRYPT_CMD_AES256ECB_ENC,
518 TA_CRYPT_CMD_AES256ECB_DEC };
519
520 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
521 &session, &crypt_user_ta_uuid,
522 NULL, &ret_orig)))
523 return;
524
525 /* Run the "complete crypto test suite" */
526 xtest_crypto_test(&cs);
527
528 TEEC_CloseSession(&session);
529}
Jens Wiklander14f48872018-06-29 15:30:13 +0200530ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200531
Etienne Carriere92c34422018-02-09 13:11:40 +0100532static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200533{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100534 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200535 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100536 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200537
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300538 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200539 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300540 &ret_orig)))
541 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200542
543 op.params[0].value.a = n;
544 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
545 TEEC_NONE);
546
547 (void)ADBG_EXPECT_TEEC_RESULT(c,
548 TEEC_ERROR_TARGET_DEAD,
549 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
550 &ret_orig));
551
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300552 (void)ADBG_EXPECT_TEEC_RESULT(c,
553 TEEC_ERROR_TARGET_DEAD,
554 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200555 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300556
Pascal Brandc639ac82015-07-02 08:53:34 +0200557 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
558
559 TEEC_CloseSession(&session);
560}
561
Etienne Carriere92c34422018-02-09 13:11:40 +0100562static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
563 size_t size)
564{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100565 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100566 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100567 uint32_t ret_orig = 0;
568 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100569
Etienne Carriere92c34422018-02-09 13:11:40 +0100570 shm.size = size;
571 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
572 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
573 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
574 return;
575
576 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
577 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
578 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200579 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100580
581 op.params[0].value.a = (uint32_t)n;
582 op.params[1].memref.parent = &shm;
583 op.params[1].memref.size = size;
584 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
585 TEEC_NONE, TEEC_NONE);
586
587 (void)ADBG_EXPECT_TEEC_RESULT(c,
588 TEEC_ERROR_TARGET_DEAD,
589 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
590 &ret_orig));
591
592 (void)ADBG_EXPECT_TEEC_RESULT(c,
593 TEEC_ERROR_TARGET_DEAD,
594 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
595 &ret_orig));
596
597 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
598
599 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200600rel_shm:
601 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100602}
603
Pascal Brandc639ac82015-07-02 08:53:34 +0200604static void xtest_tee_test_1005(ADBG_Case_t *c)
605{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100606 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200607#define MAX_SESSIONS 3
608 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100609 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200610
611 for (i = 0; i < MAX_SESSIONS; i++) {
612 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200613 xtest_teec_open_session(&sessions[i],
614 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200615 NULL, &ret_orig)))
616 break;
617 }
618
619 for (; --i >= 0; )
620 TEEC_CloseSession(&sessions[i]);
621}
Jens Wiklander14f48872018-06-29 15:30:13 +0200622ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200623
624static void xtest_tee_test_1006(ADBG_Case_t *c)
625{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100626 TEEC_Session session = { };
627 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200628 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100629 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200630
631 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
632 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
633 &ret_orig)))
634 return;
635
636 op.params[0].tmpref.buffer = buf;
637 op.params[0].tmpref.size = sizeof(buf);
638 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
639 TEEC_NONE, TEEC_NONE);
640
641 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
642 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
643 &ret_orig));
644
645 TEEC_CloseSession(&session);
646}
Jens Wiklander14f48872018-06-29 15:30:13 +0200647ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
648 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200649
650static void xtest_tee_test_1007(ADBG_Case_t *c)
651{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100652 TEEC_Session session = { };
653 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200654
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300655 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200656 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300657 &ret_orig)))
658 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200659
660 (void)ADBG_EXPECT_TEEC_RESULT(c,
661 TEEC_ERROR_TARGET_DEAD,
662 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
663 &ret_orig));
664
665 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
666
667 (void)ADBG_EXPECT_TEEC_RESULT(c,
668 TEEC_ERROR_TARGET_DEAD,
669 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
670 &ret_orig));
671
672 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
673
674 TEEC_CloseSession(&session);
675}
Jens Wiklander14f48872018-06-29 15:30:13 +0200676ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200677
Jerome Forissierf02a2212015-10-29 14:33:35 +0100678#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000679# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800680#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000681# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100682#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000683# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100684#endif
685
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100686static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600687{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100688 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600689
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100690 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100691 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100692 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200693 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
694 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
695 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600696 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200697
Jens Wiklanderb7940892015-10-23 16:02:40 +0200698 return fopen(buf, mode);
699}
700
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100701static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200702{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100703 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100704 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
705 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100706 TEEC_Result res = TEEC_ERROR_GENERIC;
707 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100708 FILE *f = NULL;
709 bool r = false;
710 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100711 size_t sz = 0;
712 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200713
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100714 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
715 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
716 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200717
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100718 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
719 if (!ADBG_EXPECT_NOT_NULL(c, f))
720 goto out;
721 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
722 goto out;
723 sz = ftell(f);
724 rewind(f);
725
726 buf = malloc(sz);
727 if (!ADBG_EXPECT_NOT_NULL(c, buf))
728 goto out;
729
730 fread_res = fread(buf, 1, sz, f);
731 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
732 goto out;
733
Jens Wiklander4441fe22015-10-23 16:53:02 +0200734 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100735 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200736
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100737 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200738
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100739 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
740 TEEC_NONE, TEEC_NONE);
741 op.params[0].tmpref.buffer = buf;
742 op.params[0].tmpref.size = sz;
743
744 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
745 &ret_orig);
746 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
747out:
748 free(buf);
749 if (f)
750 fclose(f);
751 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200752 return r;
753}
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100754
755static void test_1008_corrupt_ta(ADBG_Case_t *c)
756{
757 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
758 TEEC_Result res = TEEC_ERROR_GENERIC;
759 TEEC_Session session = { };
760 uint32_t ret_orig = 0;
761
762 res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
763 if (res) {
764 if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
765 res))
Joakim Becha1212b62020-04-07 12:06:00 +0200766 Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100767 return;
768 }
769 TEEC_CloseSession(&session);
770
771 ADBG_EXPECT_TRUE(c,
772 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
773 ADBG_EXPECT_TRUE(c,
774 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
775 ADBG_EXPECT_TRUE(c,
776 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
777 ADBG_EXPECT_TRUE(c,
778 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
779 ADBG_EXPECT_TRUE(c,
780 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
781 ADBG_EXPECT_TRUE(c,
782 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
783 ADBG_EXPECT_TRUE(c,
784 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
785 ADBG_EXPECT_TRUE(c,
786 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
787 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
788 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
789}
Jens Wiklander4441fe22015-10-23 16:53:02 +0200790
Pascal Brandc639ac82015-07-02 08:53:34 +0200791static void xtest_tee_test_1008(ADBG_Case_t *c)
792{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100793 TEEC_Session session = { };
794 TEEC_Session session_crypt = { };
795 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200796
797 Do_ADBG_BeginSubCase(c, "Invoke command");
798 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300799 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200800 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300801 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200802
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300803 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
804 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
805 NULL, &ret_orig));
806 TEEC_CloseSession(&session);
807 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200808
Pascal Brandc639ac82015-07-02 08:53:34 +0200809 }
810 Do_ADBG_EndSubCase(c, "Invoke command");
811
812 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
813 {
814 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
815
816 op.params[0].value.a = 2000;
817 op.paramTypes = TEEC_PARAM_TYPES(
818 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
819
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300820 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200821 xtest_teec_open_session(&session,
822 &os_test_ta_uuid,
823 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300824 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200825
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300826 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
827 TEEC_InvokeCommand(&session,
828 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
829 &op, &ret_orig));
830 TEEC_CloseSession(&session);
831 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200832 }
833 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
834
835 Do_ADBG_BeginSubCase(c, "Create session fail");
836 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100837 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200838
Pascal Brandc639ac82015-07-02 08:53:34 +0200839 for (n = 0; n < 100; n++) {
840 Do_ADBG_Log("n = %zu", n);
841 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
842 xtest_teec_open_session(&session_crypt,
843 &create_fail_test_ta_uuid,
844 NULL, &ret_orig));
Jerome Forissierec7a31f2020-11-20 11:30:06 +0100845 /* level > 0 may be used to detect/debug memory leaks */
846 if (!level)
847 break;
Pascal Brandc639ac82015-07-02 08:53:34 +0200848 }
849 }
850 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200851
Jens Wiklander4441fe22015-10-23 16:53:02 +0200852 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
Jens Wiklandere1a201d2020-02-12 18:36:20 +0100853 test_1008_corrupt_ta(c);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200854 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200855}
Jens Wiklander14f48872018-06-29 15:30:13 +0200856ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
857 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200858
Pascal Brandc639ac82015-07-02 08:53:34 +0200859static void *cancellation_thread(void *arg)
860{
861 /*
862 * Sleep 0.5 seconds before cancellation to make sure that the other
863 * thread is in RPC_WAIT.
864 */
865 (void)usleep(500000);
866 TEEC_RequestCancellation(arg);
867 return NULL;
868}
Pascal Brandc639ac82015-07-02 08:53:34 +0200869
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300870static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
871 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200872{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100873 TEEC_Session session = { };
874 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300875 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200876
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100877 memset(&thr, 0, sizeof(thr));
878
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300879 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200880 {
881 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
882
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300883 if (ADBG_EXPECT_TEEC_SUCCESS(c,
884 xtest_teec_open_session(&session, &os_test_ta_uuid,
885 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200886
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300887 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
888 TEEC_ORIGIN_TRUSTED_APP,
889 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200890
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300891 op.params[0].value.a = timeout;
892 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
893 TEEC_NONE,
894 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300895 if (cancel) {
896 (void)ADBG_EXPECT(c, 0,
897 pthread_create(&thr, NULL,
898 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200899
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300900 (void)ADBG_EXPECT_TEEC_RESULT(c,
901 TEEC_ERROR_CANCEL,
902 TEEC_InvokeCommand(&session,
903 TA_OS_TEST_CMD_WAIT,
904 &op,
905 &ret_orig));
906 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300907
908 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
909 TEEC_InvokeCommand(&session,
910 TA_OS_TEST_CMD_WAIT,
911 &op,
912 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300913 if (cancel)
914 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300915
916 TEEC_CloseSession(&session);
917 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200918 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300919 Do_ADBG_EndSubCase(c, "%s", subcase);
920}
921
922static void xtest_tee_test_1009(ADBG_Case_t *c)
923{
924 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
925 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300926 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300927 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200928}
Jens Wiklander14f48872018-06-29 15:30:13 +0200929ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200930
931static void xtest_tee_test_1010(ADBG_Case_t *c)
932{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100933 unsigned int n = 0;
934 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100935 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200936
Jerome Forissiere4c33f42021-09-21 11:26:17 +0200937 for (n = 1; n <= 7; n++) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200938 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
939 xtest_tee_test_invalid_mem_access(c, n);
940 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
941 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100942
943 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
944 for (n = 1; n <= 5; n++) {
945 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200946 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100947 n, memref_sz[idx]);
948 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
949 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200950 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100951 n, memref_sz[idx]);
952 }
953 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200954}
Jens Wiklander14f48872018-06-29 15:30:13 +0200955ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
956 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200957
958static void xtest_tee_test_1011(ADBG_Case_t *c)
959{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100960 TEEC_Session session = { };
961 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200962 struct xtest_crypto_session cs = {
963 c, &session, TA_RPC_CMD_CRYPT_SHA256,
964 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
965 TA_RPC_CMD_CRYPT_AES256ECB_DEC
966 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100967 struct xtest_crypto_session cs_privmem = {
968 c, &session,
969 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
970 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
971 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
972 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200973 TEEC_UUID uuid = rpc_test_ta_uuid;
974
975 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
976 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
977 return;
978
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100979 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200980 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100981 * Run the "complete crypto test suite" using TA-to-TA
982 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200983 */
984 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100985 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
986
987 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
988 /*
989 * Run the "complete crypto test suite" using TA-to-TA
990 * communication via TA private memory.
991 */
992 xtest_crypto_test(&cs_privmem);
993 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
994
Pascal Brandc639ac82015-07-02 08:53:34 +0200995 TEEC_CloseSession(&session);
996}
Jens Wiklander14f48872018-06-29 15:30:13 +0200997ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
998 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200999
1000/*
1001 * Note that this test is failing when
1002 * - running twice in a raw
1003 * - and the user TA is statically linked
1004 * This is because the counter is not reseted when opening the first session
1005 * in case the TA is statically linked
1006 */
1007static void xtest_tee_test_1012(ADBG_Case_t *c)
1008{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001009 TEEC_Session session1 = { };
1010 TEEC_Session session2 = { };
1011 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +02001012 TEEC_UUID uuid = sims_test_ta_uuid;
1013
1014 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
1015 {
1016 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1017 static const uint8_t in[] = {
1018 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
1019 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
1020 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
1021 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
1022 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001023 uint8_t out[32] = { };
1024 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +02001025
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001026 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001027 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001028 &ret_orig)))
1029 return;
Pascal Brandc639ac82015-07-02 08:53:34 +02001030
1031 op.params[0].value.a = 0;
1032 op.params[1].tmpref.buffer = (void *)in;
1033 op.params[1].tmpref.size = sizeof(in);
1034 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1035 TEEC_MEMREF_TEMP_INPUT,
1036 TEEC_NONE, TEEC_NONE);
1037
1038 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1039 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
1040 &ret_orig));
1041
Jerome Forissier3cc0a422017-12-14 09:53:24 +01001042 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001043 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +02001044 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +03001045 &ret_orig)))
1046 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +02001047
1048 op.params[0].value.a = 0;
1049 op.params[1].tmpref.buffer = out;
1050 op.params[1].tmpref.size = sizeof(out);
1051 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1052 TEEC_MEMREF_TEMP_OUTPUT,
1053 TEEC_NONE, TEEC_NONE);
1054
1055 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1056 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
1057 &op, &ret_orig));
1058
1059 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
1060 sizeof(out))) {
1061 Do_ADBG_Log("in:");
1062 Do_ADBG_HexLog(in, sizeof(in), 16);
1063 Do_ADBG_Log("out:");
1064 Do_ADBG_HexLog(out, sizeof(out), 16);
1065 }
1066
1067 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1068 TEEC_NONE, TEEC_NONE,
1069 TEEC_NONE);
1070
1071 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1072 TEEC_InvokeCommand(&session1,
1073 TA_SIMS_CMD_GET_COUNTER,
1074 &op, &ret_orig));
1075
1076 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
1077
1078 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1079 TEEC_InvokeCommand(&session2,
1080 TA_SIMS_CMD_GET_COUNTER, &op,
1081 &ret_orig));
1082
1083 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
1084 TEEC_CloseSession(&session2);
1085 }
1086
1087 memset(out, 0, sizeof(out));
1088 op.params[0].value.a = 0;
1089 op.params[1].tmpref.buffer = out;
1090 op.params[1].tmpref.size = sizeof(out);
1091 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1092 TEEC_MEMREF_TEMP_OUTPUT,
1093 TEEC_NONE, TEEC_NONE);
1094
1095 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1096 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1097 &ret_orig));
1098
1099 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1100 Do_ADBG_Log("in:");
1101 Do_ADBG_HexLog(in, sizeof(in), 16);
1102 Do_ADBG_Log("out:");
1103 Do_ADBG_HexLog(out, sizeof(out), 16);
1104 }
1105
1106 TEEC_CloseSession(&session1);
1107 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001108 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001109}
Jens Wiklander14f48872018-06-29 15:30:13 +02001110ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1111 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001112
1113struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001114 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001115 uint32_t cmd;
1116 uint32_t repeat;
1117 TEEC_SharedMemory *shm;
1118 uint32_t error_orig;
1119 TEEC_Result res;
1120 uint32_t max_concurrency;
1121 const uint8_t *in;
1122 size_t in_len;
1123 uint8_t *out;
1124 size_t out_len;
1125};
1126
1127static void *test_1013_thread(void *arg)
1128{
1129 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001130 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001131 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1132 uint8_t p2 = TEEC_NONE;
1133 uint8_t p3 = TEEC_NONE;
1134
Jens Wiklander70672972016-04-06 00:01:45 +02001135 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001136 &a->error_orig);
1137 if (a->res != TEEC_SUCCESS)
1138 return NULL;
1139
1140 op.params[0].memref.parent = a->shm;
1141 op.params[0].memref.size = a->shm->size;
1142 op.params[0].memref.offset = 0;
1143 op.params[1].value.a = a->repeat;
1144 op.params[1].value.b = 0;
1145 op.params[2].tmpref.buffer = (void *)a->in;
1146 op.params[2].tmpref.size = a->in_len;
1147 op.params[3].tmpref.buffer = a->out;
1148 op.params[3].tmpref.size = a->out_len;
1149
1150 if (a->in_len)
1151 p2 = TEEC_MEMREF_TEMP_INPUT;
1152 if (a->out_len)
1153 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1154
1155 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1156 TEEC_VALUE_INOUT, p2, p3);
1157
1158 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1159 a->max_concurrency = op.params[1].value.b;
1160 a->out_len = op.params[3].tmpref.size;
1161 TEEC_CloseSession(&session);
1162 return NULL;
1163}
1164
Pascal Brand4fa35582015-12-17 10:59:12 +01001165#define NUM_THREADS 3
1166
Jens Wiklander70672972016-04-06 00:01:45 +02001167static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1168 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001169{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001170 size_t nt = 0;
1171 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001172 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001173 TEEC_SharedMemory shm = { };
1174 size_t max_concurrency = 0;
1175 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001176 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1177 static const uint8_t sha256_out[] = {
1178 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1179 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1180 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1181 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1182 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001183 uint8_t out[32] = { };
1184 pthread_t thr[NUM_THREADS] = { };
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001185 bool skip = false;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001186
Etienne Carrierec0a61722023-03-07 17:47:42 +01001187 /* Decrease number of loops when pager has a small page pool */
1188 if (level == 0 && optee_pager_with_small_pool())
1189 repeat = 250;
1190
Jens Wiklander70672972016-04-06 00:01:45 +02001191 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001192 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001193
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001194 shm.size = sizeof(struct ta_concurrent_shm);
1195 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1196 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1197 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1198 return;
1199
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001200 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001201 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001202 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001203
1204 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001205 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001206 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001207 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001208 arg[n].shm = &shm;
1209 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1210 test_1013_thread, arg + n)))
1211 nt = n; /* break loop and start cleanup */
1212 }
1213
1214 for (n = 0; n < nt; n++) {
1215 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001216 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1217 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1218 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1219 skip = true;
1220 continue;
1221 }
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001222 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1223 if (arg[n].max_concurrency > max_concurrency)
1224 max_concurrency = arg[n].max_concurrency;
1225 }
1226
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001227 /*
1228 * Concurrency can be limited by several factors, for instance in a
Joakim Becha1212b62020-04-07 12:06:00 +02001229 * single CPU system it's dependent on the Preemption Model used by
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001230 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1231 * best result there).
1232 */
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001233 if (!skip) {
1234 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
1235 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=,
1236 NUM_THREADS);
1237 *mean_concurrency += max_concurrency;
1238 }
Jens Wiklander70672972016-04-06 00:01:45 +02001239 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001240
Jens Wiklander70672972016-04-06 00:01:45 +02001241 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001242 memset(shm.buffer, 0, shm.size);
1243 memset(arg, 0, sizeof(arg));
1244 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001245 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001246
1247 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001248 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001249 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001250 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001251 arg[n].shm = &shm;
1252 arg[n].in = sha256_in;
1253 arg[n].in_len = sizeof(sha256_in);
1254 arg[n].out = out;
1255 arg[n].out_len = sizeof(out);
1256 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1257 test_1013_thread, arg + n)))
1258 nt = n; /* break loop and start cleanup */
1259 }
1260
1261 for (n = 0; n < nt; n++) {
Jerome Forissierfbdc1752021-06-29 11:25:35 +02001262 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1263 if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY &&
1264 !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) {
1265 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
1266 continue;
1267 }
1268 if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001269 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1270 arg[n].out, arg[n].out_len);
1271 if (arg[n].max_concurrency > max_concurrency)
1272 max_concurrency = arg[n].max_concurrency;
1273 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001274 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001275 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001276
Pascal Brand4fa35582015-12-17 10:59:12 +01001277 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001278 TEEC_ReleaseSharedMemory(&shm);
1279}
Pascal Brand4fa35582015-12-17 10:59:12 +01001280
1281static void xtest_tee_test_1013(ADBG_Case_t *c)
1282{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001283 int i = 0;
1284 double mean_concurrency = 0;
1285 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001286 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001287
1288 if (level == 0)
1289 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001290
Jens Wiklander70672972016-04-06 00:01:45 +02001291 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001292 mean_concurrency = 0;
1293 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001294 xtest_tee_test_1013_single(c, &concurrency,
1295 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001296 mean_concurrency += concurrency;
1297 }
1298 mean_concurrency /= nb_loops;
1299
1300 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1301 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001302 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001303
Jens Wiklander70672972016-04-06 00:01:45 +02001304 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1305 mean_concurrency = 0;
1306 for (i = 0; i < nb_loops; i++) {
1307 xtest_tee_test_1013_single(c, &concurrency,
1308 &concurrent_large_ta_uuid);
1309 mean_concurrency += concurrency;
1310 }
1311 mean_concurrency /= nb_loops;
1312
1313 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1314 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1315 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
1316}
Jens Wiklander14f48872018-06-29 15:30:13 +02001317ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
Joakim Becha1212b62020-04-07 12:06:00 +02001318 "Test concurrency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001319
1320#ifdef CFG_SECURE_DATA_PATH
1321static void xtest_tee_test_1014(ADBG_Case_t *c)
1322{
1323 UNUSED(c);
1324
1325 int size = 17000;
1326 int loop = 10;
Olivier Masseda5282a2022-04-07 11:24:08 +02001327 const char *heap_name = DEFAULT_HEAP_NAME;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001328 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001329 int test = 0;
1330 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001331
1332 test = TEST_NS_TO_TA;
1333 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001334 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001335 ADBG_EXPECT(c, 0, ret);
1336 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1337
1338 test = TEST_TA_TO_TA;
1339 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001340 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001341 ADBG_EXPECT(c, 0, ret);
1342 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1343
1344 test = TEST_TA_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001345 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001346 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001347 ADBG_EXPECT(c, 0, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001348 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001349
1350 test = TEST_NS_TO_PTA;
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001351 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001352 ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001353 ADBG_EXPECT(c, 1, ret);
Igor Opaniuk2e0a6792021-01-09 00:51:19 +02001354 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001355
1356 Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
Jerome Forissier03aa2792023-02-07 14:22:24 +01001357 ret = sdp_out_of_bounds_memref_test(size, heap_name, 0);
Etienne Carriered9be3dc2018-04-25 18:30:19 +02001358 ADBG_EXPECT(c, 0, ret);
1359 Do_ADBG_EndSubCase(c, NULL);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001360}
Jens Wiklander14f48872018-06-29 15:30:13 +02001361ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1362 "Test secure data path against SDP TAs and pTAs");
1363#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001364
Jerome Forissierf4259942022-01-11 14:27:05 +01001365#ifdef CFG_REE_FS
Jens Wiklander272d3642017-04-03 13:03:47 +02001366static void xtest_tee_test_1015(ADBG_Case_t *c)
1367{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001368 TEEC_Result res = TEEC_ERROR_GENERIC;
1369 TEEC_Session session = { };
1370 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001371
Etienne Carriere11093162017-10-26 09:49:04 +02001372 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001373 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1374 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001375 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1376 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001377 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001378 }
1379 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001380
1381 ADBG_EXPECT_TEEC_SUCCESS(c,
1382 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1383 NULL, &ret_orig));
1384 TEEC_CloseSession(&session);
1385}
Jens Wiklander14f48872018-06-29 15:30:13 +02001386ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1387 "FS hash-tree corner cases");
Jerome Forissierf4259942022-01-11 14:27:05 +01001388#endif /*CFG_REE_FS*/
Jerome Forissiere916b102017-06-07 17:55:52 +02001389
1390static void xtest_tee_test_1016(ADBG_Case_t *c)
1391{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001392 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001393 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001394 uint32_t ret_orig = 0;
Vincent Mailholda741cd2023-11-16 20:20:54 +09001395 int dummy = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001396
1397 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1398 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1399 &ret_orig)))
1400 return;
1401
1402 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1403 TEEC_NONE);
1404
1405 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1406 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1407 &ret_orig));
1408
Vincent Mailholda741cd2023-11-16 20:20:54 +09001409 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1410 TEEC_MEMREF_TEMP_INOUT,
1411 TEEC_MEMREF_TEMP_OUTPUT,
1412 TEEC_NONE);
1413
1414 op.params[0].tmpref.buffer = &dummy;
1415 op.params[0].tmpref.size = 0;
1416
1417 op.params[1].tmpref.buffer = &dummy;
1418 op.params[1].tmpref.size = 0;
1419
1420 op.params[2].tmpref.buffer = &dummy;
1421 op.params[2].tmpref.size = 0;
1422
1423 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1424 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF_SIZE0,
1425 &op, &ret_orig));
1426
Jerome Forissiere916b102017-06-07 17:55:52 +02001427 TEEC_CloseSession(&session);
1428}
Jens Wiklander14f48872018-06-29 15:30:13 +02001429ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1430 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001431
1432static void xtest_tee_test_1017(ADBG_Case_t *c)
1433{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001434 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001435 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001436 uint32_t ret_orig = 0;
1437 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001438 size_t page_size = 4096;
1439
Jens Wiklander87e81702018-03-20 12:00:00 +08001440 shm.size = 8 * page_size;
1441 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1442 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1443 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1444 return;
1445
1446 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1447 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1448 &ret_orig)))
1449 goto out;
1450
1451 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1452 TEEC_MEMREF_PARTIAL_INPUT,
1453 TEEC_MEMREF_PARTIAL_OUTPUT,
1454 TEEC_MEMREF_PARTIAL_OUTPUT);
1455
1456 /*
1457 * The first two memrefs are supposed to be combined into in
1458 * region and the last two memrefs should have one region each
1459 * when the parameters are mapped for the TA.
1460 */
1461 op.params[0].memref.parent = &shm;
1462 op.params[0].memref.size = page_size;
1463 op.params[0].memref.offset = 0;
1464
1465 op.params[1].memref.parent = &shm;
1466 op.params[1].memref.size = page_size;
1467 op.params[1].memref.offset = page_size;
1468
1469 op.params[2].memref.parent = &shm;
1470 op.params[2].memref.size = page_size;
1471 op.params[2].memref.offset = 4 * page_size;
1472
1473 op.params[3].memref.parent = &shm;
1474 op.params[3].memref.size = 2 * page_size;
1475 op.params[3].memref.offset = 6 * page_size;
1476
1477 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1478 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1479 &ret_orig));
1480
1481 TEEC_CloseSession(&session);
1482out:
1483 TEEC_ReleaseSharedMemory(&shm);
1484}
Jens Wiklander14f48872018-06-29 15:30:13 +02001485ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1486 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001487
Etienne Carriere84382b32018-04-25 18:30:30 +02001488static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
1489 TEEC_SharedMemory *shm)
1490{
1491 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1492 TEEC_Result ret = TEEC_ERROR_GENERIC;
1493 uint32_t ret_orig = 0;
1494
1495 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1496 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1497
1498 op.params[0].memref.parent = shm;
1499 op.params[0].memref.size = shm->size / 2;
1500 op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
1501
1502 ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
1503 &op, &ret_orig);
1504
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001505 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001506 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1507 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1508 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1509 }
1510}
1511
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001512static void xtest_tee_test_1018(ADBG_Case_t *c)
1513{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001514 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001515 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001516 uint32_t ret_orig = 0;
1517 TEEC_SharedMemory shm = { };
Etienne Carriere84382b32018-04-25 18:30:30 +02001518 TEEC_Result ret = TEEC_ERROR_GENERIC;
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001519 size_t page_size = 4096;
Joakim Becha1212b62020-04-07 12:06:00 +02001520 /* Intentionally not 4kB aligned and odd */
Etienne Carriere84382b32018-04-25 18:30:30 +02001521 uint8_t buffer[6001] = { };
1522
1523 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1524 xtest_teec_open_session(&session,
1525 &os_test_ta_uuid,
1526 NULL,
1527 &ret_orig)))
1528 return;
1529
Joakim Becha1212b62020-04-07 12:06:00 +02001530 Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001531
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001532 shm.size = 8 * page_size;
1533 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1534 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Etienne Carriere84382b32018-04-25 18:30:30 +02001535 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1536 &shm)))
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001537 goto out;
1538
1539 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1540 TEEC_MEMREF_PARTIAL_INPUT,
1541 TEEC_MEMREF_PARTIAL_OUTPUT,
1542 TEEC_MEMREF_PARTIAL_OUTPUT);
1543
1544 /*
1545 * The first two memrefs are supposed to be combined into in
1546 * region and the last two memrefs should have one region each
1547 * when the parameters are mapped for the TA.
1548 */
1549 op.params[0].memref.parent = &shm;
1550 op.params[0].memref.size = page_size;
1551 op.params[0].memref.offset = 0;
1552
1553 op.params[1].memref.parent = &shm;
1554 op.params[1].memref.size = page_size;
1555 op.params[1].memref.offset = page_size;
1556
1557 op.params[2].memref.parent = &shm;
1558 op.params[2].memref.size = page_size;
1559 op.params[2].memref.offset = 4 * page_size;
1560
1561 op.params[3].memref.parent = &shm;
1562 op.params[3].memref.size = 3 * page_size;
1563 op.params[3].memref.offset = 6 * page_size;
1564
Etienne Carriere84382b32018-04-25 18:30:30 +02001565 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1566 &ret_orig);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001567
Etienne Carriere48fa1b22020-04-08 13:42:01 +02001568 ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
Etienne Carriere84382b32018-04-25 18:30:30 +02001569 if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
1570 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
1571 ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
1572 }
1573
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001574 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere84382b32018-04-25 18:30:30 +02001575 Do_ADBG_EndSubCase(c, NULL);
1576
1577 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
1578
1579 memset(&shm, 0, sizeof(shm));
1580 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1581 shm.buffer = buffer;
1582 shm.size = sizeof(buffer);
1583
1584 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1585 TEEC_RegisterSharedMemory(&xtest_teec_ctx,
1586 &shm)))
1587 goto out;
1588
1589 invoke_1byte_out_of_bounds(c, &session, &shm);
1590
1591 TEEC_ReleaseSharedMemory(&shm);
1592 Do_ADBG_EndSubCase(c, NULL);
1593
1594 Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
1595
1596 memset(&shm, 0, sizeof(shm));
1597 shm.size = sizeof(buffer);
1598 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1599 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1600 TEEC_AllocateSharedMemory(&xtest_teec_ctx,
1601 &shm)))
1602 goto out;
1603
1604 invoke_1byte_out_of_bounds(c, &session, &shm);
1605
1606 TEEC_ReleaseSharedMemory(&shm);
1607 Do_ADBG_EndSubCase(c, NULL);
1608
1609out:
1610 TEEC_CloseSession(&session);
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001611}
Jens Wiklander14f48872018-06-29 15:30:13 +02001612ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1613 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001614
Jerome Forissier53bde722018-05-31 09:14:54 +02001615static void xtest_tee_test_1019(ADBG_Case_t *c)
1616{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001617 TEEC_Session session = { };
1618 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001619
1620 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1621 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1622 &ret_orig)))
1623 return;
1624
1625 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1626 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1627 &ret_orig));
1628
1629 (void)ADBG_EXPECT_TEEC_RESULT(c,
1630 TEEC_ERROR_TARGET_DEAD,
1631 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1632 NULL, &ret_orig));
1633
1634 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1635
1636 TEEC_CloseSession(&session);
1637}
Jens Wiklander14f48872018-06-29 15:30:13 +02001638ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1639 "Test dynamically linked TA");
Jerome Forissier3357f872018-10-05 15:13:44 +02001640
1641static void xtest_tee_test_1020(ADBG_Case_t *c)
1642{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001643 TEEC_Result res = TEEC_ERROR_GENERIC;
1644 TEEC_Session session = { };
1645 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001646
1647 /* Pseudo TA is optional: warn and nicely exit if not found */
1648 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1649 &ret_orig);
1650 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1651 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1652 return;
1653 }
1654 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1655
1656 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1657 NULL, &ret_orig);
1658 if (res != TEEC_SUCCESS) {
1659 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1660 ret_orig);
1661 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1662 Do_ADBG_Log(" - 1020 - skip test, feature not "
1663 "implemented");
1664 goto out;
1665 }
1666 /* Error */
1667 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1668 }
1669out:
1670 TEEC_CloseSession(&session);
1671}
1672ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1673 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001674
1675static TEEC_Result open_sec_session(TEEC_Session *session,
1676 const TEEC_UUID *uuid)
1677{
1678 TEEC_Result res = TEEC_ERROR_GENERIC;
1679 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1680 uint32_t ret_orig = 0;
1681
1682 op.params[0].tmpref.buffer = (void *)uuid;
1683 op.params[0].tmpref.size = sizeof(*uuid);
1684 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1685 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1686
1687 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1688 &op, &ret_orig);
1689 if (res != TEEC_SUCCESS)
1690 return TEEC_ERROR_GENERIC;
1691
1692 return res;
1693}
1694
1695static TEEC_Result sims_get_counter(TEEC_Session *session,
1696 uint32_t *counter)
1697{
1698 TEEC_Result res = TEEC_ERROR_GENERIC;
1699 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1700 uint32_t ret_orig = 0;
1701
1702 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1703 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1704
1705 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1706 &op, &ret_orig);
1707 if (res == TEEC_SUCCESS)
1708 *counter = op.params[0].value.a;
1709
1710 return res;
1711}
1712
1713static TEEC_Result trigger_panic(TEEC_Session *session,
1714 const TEEC_UUID *uuid)
1715{
1716 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1717 uint32_t ret_orig = 0;
1718
1719 if (!uuid) {
1720 op.params[0].tmpref.buffer = NULL;
1721 op.params[0].tmpref.size = 0;
1722 } else {
1723 op.params[0].tmpref.buffer = (void *)uuid;
1724 op.params[0].tmpref.size = sizeof(*uuid);
1725 }
1726 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1727 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1728
1729 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1730 &op, &ret_orig);
1731}
1732
1733static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1734 bool multi_instance)
1735{
1736 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1737 uint32_t counter = 0;
1738 uint32_t ret_orig = 0;
1739 uint32_t exp_counter = 0;
1740 TEEC_Session cs[3] = { };
1741
1742 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1743 xtest_teec_open_session(&cs[0], uuid, NULL,
1744 &ret_orig)))
1745 return;
1746
1747 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1748 xtest_teec_open_session(&cs[1], uuid, NULL,
1749 &ret_orig)))
1750 goto bail0;
1751
1752 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1753 goto bail1;
1754
1755 if (!ADBG_EXPECT(c, 0, counter))
1756 goto bail1;
1757
1758 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1759 goto bail1;
1760
1761 exp_counter = multi_instance ? 0 : 1;
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001762 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001763 goto bail1;
1764
1765 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1766 trigger_panic(&cs[1], NULL)))
1767 goto bail1;
1768
1769 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1770 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1771 sims_get_counter(&cs[0], &counter)))
1772 goto bail1;
1773
1774 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1775 sims_get_counter(&cs[1], &counter)))
1776 goto bail1;
1777
1778 /* Attempt to open a session on panicked context */
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001779 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001780 xtest_teec_open_session(&cs[1], uuid, NULL,
1781 &ret_orig)))
1782 goto bail1;
1783
1784 /* Sanity check of still valid TA context */
1785 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1786 xtest_teec_open_session(&cs[2], uuid, NULL,
1787 &ret_orig)))
1788 goto bail1;
1789
1790 /* Sanity check of still valid TA context */
1791 if (multi_instance) {
1792 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1793 sims_get_counter(&cs[0], &counter)))
1794 goto bail2;
1795
1796 if (!ADBG_EXPECT(c, 0, counter))
1797 goto bail2;
1798 }
1799
1800 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1801 goto bail2;
1802
Ovidiu Mihalachi110aac42019-09-11 14:08:01 +03001803 exp_counter = multi_instance ? 0 : 1;
1804 if (!ADBG_EXPECT(c, exp_counter, counter))
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001805 goto bail2;
1806
1807bail2:
1808 TEEC_CloseSession(&cs[2]);
1809bail1:
1810 TEEC_CloseSession(&cs[1]);
1811bail0:
1812 TEEC_CloseSession(&cs[0]);
1813}
1814
1815static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1816 const TEEC_UUID *uuid2)
1817{
1818 uint32_t ret_orig = 0;
1819 uint32_t counter = 0;
1820 TEEC_Session cs[3] = { };
1821
1822 /* Test pre-conditions */
1823 /* 2.1 - CA opens a session toward TA1 */
1824 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1825 xtest_teec_open_session(&cs[0], uuid1, NULL,
1826 &ret_orig)))
1827 return;
1828
1829 /* 2.2 - CA opens a session toward TA2 */
1830 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1831 xtest_teec_open_session(&cs[1], uuid2, NULL,
1832 &ret_orig)))
1833 goto bail0;
1834
1835 /* 2.3 - TA1 opens a session toward TA2 */
1836 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1837 goto bail1;
1838
1839 /* 2.4 - CA invokes TA2 which panics */
1840 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1841 trigger_panic(&cs[1], NULL)))
1842 goto bail1;
1843
1844 /* Expected results */
1845 /* 2.5 - Expect CA->TA1 session is still alive */
1846 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1847 goto bail1;
1848
1849 /* 2.6 - Expect CA->TA2 session is properly released */
1850 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1851 sims_get_counter(&cs[1], &counter)))
1852 goto bail1;
1853
1854bail1:
1855 TEEC_CloseSession(&cs[1]);
1856bail0:
1857 TEEC_CloseSession(&cs[0]);
1858
1859 memset(cs, 0, sizeof(cs));
1860
1861 /* Test pre-conditions */
1862 /* 2.1 - CA opens a session toward TA1 */
1863 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1864 xtest_teec_open_session(&cs[0], uuid1, NULL,
1865 &ret_orig)))
1866 return;
1867
1868 /* 2.2 - CA opens a session toward TA2 */
1869 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1870 xtest_teec_open_session(&cs[1], uuid2, NULL,
1871 &ret_orig)))
1872 goto bail2;
1873
1874 /* 2.3 - TA1 opens a session toward TA2 */
1875 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1876 goto bail3;
1877
1878 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1879 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1880 goto bail3;
1881
1882 /* Expected results */
1883 /* 2.5 - Expect CA->TA1 session is still alive */
1884 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1885 goto bail3;
1886
1887 /* 2.6 - Expect CA->TA2 session is properly released */
1888 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1889 sims_get_counter(&cs[1], &counter)))
1890 goto bail3;
1891
1892bail3:
1893 TEEC_CloseSession(&cs[1]);
1894bail2:
1895 TEEC_CloseSession(&cs[0]);
1896}
1897
1898static void xtest_tee_test_1021(ADBG_Case_t *c)
1899{
1900 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1901 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1902 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1903
1904 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1905 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1906 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1907
1908 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1909 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1910 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1911
1912 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1913 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1914 &sims_keepalive_test_ta_uuid);
1915 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1916}
1917ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1918 "Test panic context release");
Jerome Forissiera9ab5d02019-03-17 21:14:06 +01001919
1920static void xtest_tee_test_1022(ADBG_Case_t *c)
1921{
1922 TEEC_Session session = { 0 };
1923 uint32_t ret_orig = 0;
1924
1925 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1926 xtest_teec_open_session(&session, &os_test_ta_uuid,
1927 NULL, &ret_orig)))
1928 return;
1929
1930 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1931 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1932 &ret_orig));
1933
1934 (void)ADBG_EXPECT_TEEC_RESULT(c,
1935 TEEC_ERROR_TARGET_DEAD,
1936 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
1937 NULL, &ret_orig));
1938
1939 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1940
1941 TEEC_CloseSession(&session);
1942}
1943ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
1944 "Test dlopen()/dlsym()/dlclose() API");
Jerome Forissiere9571e82020-02-18 15:26:20 +01001945
1946/*
1947 * Testing the ELF initialization (.init_array)
1948 *
1949 * - The TA has a global variable which can also be accessed by the two shared
1950 * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
1951 * dlopen())
1952 * - The TA and both libraries have initialization functions (declared with the
1953 * "constructor" attribute) which perform the following:
1954 * * The TA multiplies by 10 then adds 1
1955 * * os_test_lib multiplies by 10 then adds 2
1956 * * os_test_lib_dl multiplies by 10 then adds 3
1957 * By testing the variable value we make sure the initializations occurred in
1958 * the correct order.
1959 */
1960static void xtest_tee_test_1023(ADBG_Case_t *c)
1961{
1962 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1963 TEEC_Session session = { 0 };
1964 uint32_t ret_orig = 0;
1965
1966 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
1967 TEEC_NONE, TEEC_NONE);
1968
1969 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1970 xtest_teec_open_session(&session, &os_test_ta_uuid,
1971 NULL, &ret_orig)))
1972 return;
1973
1974 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1975 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1976 &ret_orig));
1977
1978 /* Expected: initialization of os_test_lib, then TA */
1979 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
1980
1981 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1982 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
1983 &ret_orig));
1984
1985 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1986 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
1987 &ret_orig));
1988
1989 /* Expected: initialization of os_test_lib_dl */
1990 (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
1991
1992 TEEC_CloseSession(&session);
1993}
1994ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
1995 "Test ELF initialization (.init_array)");
Javier Almansa Sobrinocddc0002020-02-10 13:35:37 +00001996
1997#ifdef CFG_CORE_TPM_EVENT_LOG
1998static void xtest_tee_test_1024(ADBG_Case_t *c)
1999{
2000 TEEC_Session session = {};
2001 uint32_t ret_orig = 0;
2002
2003 xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
2004 NULL, &ret_orig);
2005
2006 Do_ADBG_BeginSubCase(c, "TPM test service invocation");
2007 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
2008 TA_TPM_TEST_GET_LOG,
2009 NULL, &ret_orig));
2010 Do_ADBG_EndSubCase(c, "TPM test service invocation");
2011
2012 Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
2013 ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
2014 TA_TPM_TEST_SHORT_BUF,
2015 NULL, &ret_orig));
2016 Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
2017
2018 TEEC_CloseSession(&session);
2019}
2020
2021ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
2022 "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
2023#endif /* CFG_CORE_TPM_EVENT_LOG */
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002024
2025static void xtest_tee_test_1025(ADBG_Case_t *c)
2026{
2027 TEEC_Session session = {};
2028 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2029 uint32_t ret_orig = 0;
2030 uint8_t *empty_buf = NULL;
Etienne Carriere71383d42020-04-14 12:31:25 +02002031 TEEC_SharedMemory shm = { };
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002032 TEEC_Result res = TEEC_ERROR_GENERIC;
Etienne Carriere71383d42020-04-14 12:31:25 +02002033
2034 Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
2035
2036 memset(&shm, 0, sizeof(shm));
2037 shm.flags = TEEC_MEM_INPUT;
2038 shm.buffer = NULL;
2039 shm.size = 0;
2040
2041 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2042 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2043
2044 memset(&shm, 0, sizeof(shm));
2045 shm.flags = TEEC_MEM_OUTPUT;
2046 shm.buffer = NULL;
2047 shm.size = 0;
2048
2049 ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
2050 TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
2051
2052 Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002053
Julianus Larsona79e6ee2023-05-17 11:35:03 +02002054 if (!xtest_teec_ctx.imp.memref_null) {
Etienne Carriere71383d42020-04-14 12:31:25 +02002055 Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
Etienne Carrierec602a522020-04-13 18:53:17 +02002056 return;
2057 }
2058
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002059 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2060 xtest_teec_open_session(&session,
2061 &os_test_ta_uuid,
2062 NULL, &ret_orig)))
2063 return;
2064
2065 empty_buf = malloc(1);
2066 if (!empty_buf) {
2067 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002068 goto out_session;
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002069 }
2070
2071 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2072 TEEC_MEMREF_TEMP_INPUT,
2073 TEEC_MEMREF_TEMP_OUTPUT,
2074 TEEC_MEMREF_TEMP_OUTPUT);
2075
2076 Do_ADBG_BeginSubCase(c,
2077 "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2078
2079 op.params[0].tmpref.buffer = empty_buf;
2080 op.params[0].tmpref.size = 0;
2081
2082 op.params[1].tmpref.buffer = NULL;
2083 op.params[1].tmpref.size = 0;
2084
2085 op.params[2].tmpref.buffer = empty_buf;
2086 op.params[2].tmpref.size = 0;
2087
2088 op.params[3].tmpref.buffer = NULL;
2089 op.params[3].tmpref.size = 0;
2090
2091 ADBG_EXPECT(c, TEE_SUCCESS,
2092 TEEC_InvokeCommand(&session,
2093 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2094 &ret_orig));
2095
2096 Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
2097
2098 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2099
2100 op.params[0].tmpref.buffer = empty_buf;
2101 op.params[0].tmpref.size = 1;
2102
2103 op.params[1].tmpref.buffer = NULL;
2104 op.params[1].tmpref.size = 0;
2105
2106 op.params[2].tmpref.buffer = empty_buf;
2107 op.params[2].tmpref.size = 0;
2108
2109 op.params[3].tmpref.buffer = NULL;
2110 op.params[3].tmpref.size = 0;
2111
2112 ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
2113 TEEC_InvokeCommand(&session,
2114 TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
2115 &ret_orig));
2116
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002117 TEEC_CloseSession(&session);
2118
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002119 Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
2120
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002121 Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
2122
2123 /* Pseudo TA is optional: warn and nicely exit if not found */
2124 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
2125 &ret_orig);
2126 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
2127 Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found");
2128 goto out;
2129 }
2130 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2131 goto out;
2132
2133 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
2134 TEEC_NONE, TEEC_NONE);
2135 op.params[0].tmpref.buffer = NULL;
2136 op.params[0].tmpref.size = 0;
2137
2138 ADBG_EXPECT(c, TEE_SUCCESS,
2139 TEEC_InvokeCommand(&session,
2140 PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
2141 &op, &ret_orig));
2142
2143out_session:
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002144 TEEC_CloseSession(&session);
Etienne Carrieredba3f3b2020-04-08 17:08:16 +02002145out:
2146 Do_ADBG_EndSubCase(c, NULL);
Cedric Neveux9f483bb2019-03-04 08:58:06 +01002147 free(empty_buf);
2148}
2149ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
2150 "Test memref NULL and/or 0 bytes size");
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002151
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002152#define TEE_UUID_NS_NAME_SIZE 128
2153
2154/*
2155 * TEE Client UUID name space identifier (UUIDv4)
2156 *
2157 * Value here is random UUID that is allocated as name space identifier for
2158 * forming Client UUID's for TEE environment using UUIDv5 scheme.
2159 */
2160static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
2161
Vesa Jääskeläinene8c0c8d2020-04-05 20:11:53 +03002162/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
2163static TEEC_UUID client_uuid_public = { };
2164
2165static void xtest_tee_test_1026(ADBG_Case_t *c)
2166{
2167 TEEC_Result result = TEEC_ERROR_GENERIC;
2168 uint32_t ret_orig = 0;
2169 TEEC_Session session = { };
2170 uint32_t login = UINT32_MAX;
2171 TEEC_UUID client_uuid = { };
2172
2173 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2174 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
2175
2176 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2177 return;
2178
2179 result = ta_os_test_cmd_client_identity(&session, &login,
2180 &client_uuid);
2181
2182 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2183 goto out;
2184
2185 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
2186
2187 ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
2188 sizeof(TEEC_UUID));
2189
2190out:
2191 TEEC_CloseSession(&session);
2192}
2193
2194ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
2195 "Session: public login");
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002196
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002197/*
2198 * regression_1027
2199 * Depends on OpenSSL
Jerome Forissier81e71a82021-06-16 10:39:12 +02002200 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2201 * login client UUID generation")
2202 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002203 *
2204 * xtest skips the test when not built with OpenSSL.
2205 */
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002206static void xtest_tee_test_1027(ADBG_Case_t *c)
2207{
Victor Chong8e070bc2020-05-13 09:59:33 +01002208#ifdef OPENSSL_FOUND
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002209 TEEC_Result result = TEEC_ERROR_GENERIC;
2210 uint32_t ret_orig = 0;
2211 TEEC_Session session = { };
2212 uint32_t login = UINT32_MAX;
2213 TEEC_UUID client_uuid = { };
2214 TEEC_UUID expected_client_uuid = { };
2215 TEEC_UUID uuid_ns = { };
2216 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2217
2218 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2219
2220 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2221 return;
2222
2223 sprintf(uuid_name, "uid=%x", geteuid());
2224
2225 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2226 strlen(uuid_name));
2227 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2228 return;
2229
2230 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2231 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
2232
2233 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2234 return;
2235
2236 result = ta_os_test_cmd_client_identity(&session, &login,
2237 &client_uuid);
2238
2239 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2240 goto out;
2241
2242 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
2243
2244 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2245 sizeof(TEEC_UUID));
2246
2247out:
2248 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002249#else /*!OPENSSL_FOUND*/
2250 UNUSED(c);
Etienne Carriere359b0032020-05-16 05:56:48 +02002251 UNUSED(client_uuid_linux_ns);
Victor Chong8e070bc2020-05-13 09:59:33 +01002252 /* xtest_uuid_v5() depends on OpenSSL */
2253 Do_ADBG_Log("OpenSSL not available, skipping test 1027");
2254#endif
Vesa Jääskeläinen502c5492020-04-05 20:12:02 +03002255}
2256
2257ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
2258 "Session: user login for current user");
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002259
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002260/*
2261 * regression_1028
Jerome Forissier81e71a82021-06-16 10:39:12 +02002262 * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session
2263 * login client UUID generation")
2264 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d
Etienne Carrieref461e1d2020-05-19 12:42:08 +02002265 *
2266 * xtest skips the test when not built with OpenSSL.
2267 */
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002268static void xtest_tee_test_1028(ADBG_Case_t *c)
2269{
Victor Chong8e070bc2020-05-13 09:59:33 +01002270#ifdef OPENSSL_FOUND
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002271 TEEC_Result result = TEEC_ERROR_GENERIC;
2272 uint32_t ret_orig = 0;
2273 TEEC_Session session = { };
2274 uint32_t login = UINT32_MAX;
2275 TEEC_UUID client_uuid = { };
2276 TEEC_UUID expected_client_uuid = { };
2277 TEEC_UUID uuid_ns = { };
2278 char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
2279 uint32_t group = 0;
2280
2281 group = getegid();
2282
2283 result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
2284
2285 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2286 return;
2287
2288 sprintf(uuid_name, "gid=%x", group);
2289
2290 result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
2291 strlen(uuid_name));
2292 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2293 return;
2294
2295 result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
2296 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
2297
2298 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2299 return;
2300
2301 result = ta_os_test_cmd_client_identity(&session, &login,
2302 &client_uuid);
2303
2304 if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
2305 goto out;
2306
2307 ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
2308
2309 ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
2310 sizeof(TEEC_UUID));
2311
2312out:
2313 TEEC_CloseSession(&session);
Victor Chong8e070bc2020-05-13 09:59:33 +01002314#else /*!OPENSSL_FOUND*/
2315 UNUSED(c);
2316 /* xtest_uuid_v5() depends on OpenSSL */
2317 Do_ADBG_Log("OpenSSL not available, skipping test 1028");
2318#endif
Vesa Jääskeläinen30dd0872020-04-05 20:12:06 +03002319}
2320
2321ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
2322 "Session: group login for current user's effective group");
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002323
2324static void xtest_tee_test_1029(ADBG_Case_t *c)
2325{
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002326 TEEC_Result res = TEEC_SUCCESS;
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002327 TEEC_Session session = { 0 };
2328 uint32_t ret_orig = 0;
2329
2330 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2331 xtest_teec_open_session(&session, &os_test_ta_uuid,
2332 NULL, &ret_orig)))
2333 return;
2334
2335 Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002336 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
2337 &ret_orig);
2338 if (res == TEEC_ERROR_NOT_SUPPORTED)
2339 Do_ADBG_Log(" - 1029 - skip test, "
2340 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2341 else
2342 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002343 Do_ADBG_EndSubCase(c, "TLS variables (main program)");
2344
2345 Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
Jerome Forissier82e87bb2020-08-19 14:00:09 +02002346 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
2347 &ret_orig);
2348 if (res == TEEC_ERROR_NOT_SUPPORTED)
2349 Do_ADBG_Log(" - 1029 - skip test, "
2350 "TA returned TEEC_ERROR_NOT_SUPPORTED");
2351 else
2352 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jerome Forissier9a7101b2020-06-17 17:55:00 +02002353 Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
2354
2355 TEEC_CloseSession(&session);
2356}
2357ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
2358 "Test __thread attribute");
Jerome Forissier4565c452020-06-17 17:55:00 +02002359
2360static void xtest_tee_test_1030(ADBG_Case_t *c)
2361{
2362 TEEC_Session session = { 0 };
2363 uint32_t ret_orig = 0;
2364
2365 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2366 xtest_teec_open_session(&session, &os_test_ta_uuid,
2367 NULL, &ret_orig)))
2368 return;
2369
2370 Do_ADBG_BeginSubCase(c, "Before dlopen()");
2371 ADBG_EXPECT_TEEC_SUCCESS(c,
2372 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
2373 &ret_orig));
2374 Do_ADBG_EndSubCase(c, "Before dlopen()");
2375
2376 Do_ADBG_BeginSubCase(c, "After dlopen()");
2377 ADBG_EXPECT_TEEC_SUCCESS(c,
2378 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
2379 &ret_orig));
2380 Do_ADBG_EndSubCase(c, "After dlopen()");
2381
2382 TEEC_CloseSession(&session);
2383}
2384ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
2385 "Test dl_iterate_phdr()");
Jerome Forissier1a205ae2020-06-17 17:55:00 +02002386
2387#ifndef __clang__
2388static void xtest_tee_test_1031(ADBG_Case_t *c)
2389{
2390 TEEC_Result ret = TEE_SUCCESS;
2391 TEEC_Session session = { 0 };
2392 uint32_t ret_orig = 0;
2393
2394 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2395 xtest_teec_open_session(&session, &os_test_ta_uuid,
2396 NULL, &ret_orig)))
2397 return;
2398
2399 Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
2400 ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
2401 &ret_orig);
2402 if (ret == TEEC_ERROR_NOT_SUPPORTED) {
2403 printf("TA not built with C++ support, skipping C++ tests\n");
2404 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2405 goto out;
2406
2407 }
2408 ADBG_EXPECT_TEEC_SUCCESS(c, ret);
2409 Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
2410
2411 Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
2412 ADBG_EXPECT_TEEC_SUCCESS(c,
2413 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
2414 NULL, &ret_orig));
2415 Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
2416
2417 Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
2418 ADBG_EXPECT_TEEC_SUCCESS(c,
2419 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
2420 NULL, &ret_orig));
2421 Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
2422
2423 Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
2424 ADBG_EXPECT_TEEC_SUCCESS(c,
2425 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
2426 &ret_orig));
2427 Do_ADBG_EndSubCase(c, "Exceptions (simple)");
2428
2429 Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
2430 ADBG_EXPECT_TEEC_SUCCESS(c,
2431 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
2432 &ret_orig));
2433 Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
2434out:
2435 TEEC_CloseSession(&session);
2436}
2437ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
2438 "Test C++ features");
2439#endif
Jens Wiklandere16da892020-09-04 09:24:16 +02002440
2441static void xtest_tee_test_1032(ADBG_Case_t *c)
2442{
2443 TEEC_Result res = TEEC_SUCCESS;
2444 TEEC_Context ctx = { };
2445 TEEC_SharedMemory shm1 = {
2446 .buffer = xtest_tee_test_1032,
2447 .size = 32,
2448 .flags = TEEC_MEM_INPUT,
2449 };
2450 static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
2451 TEEC_SharedMemory shm2 = {
2452 .buffer = (void *)dummy_data,
2453 .size = sizeof(dummy_data),
2454 .flags = TEEC_MEM_INPUT,
2455 };
2456
2457 res = TEEC_InitializeContext(xtest_tee_name, &ctx);
2458 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2459 return;
2460
2461 res = TEEC_RegisterSharedMemory(&ctx, &shm1);
2462 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2463 TEEC_ReleaseSharedMemory(&shm1);
2464
2465 res = TEEC_RegisterSharedMemory(&ctx, &shm2);
2466 if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
2467 TEEC_ReleaseSharedMemory(&shm2);
2468
2469 TEEC_FinalizeContext(&ctx);
2470}
2471ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
2472 "Register read-only shared memory");
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002473
2474static void xtest_tee_test_1033(ADBG_Case_t *c)
2475{
2476 TEEC_Session session = { };
2477 uint32_t ret_orig = 0;
2478
2479 /* TA will ping the test plugin during open session operation */
2480 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2481 xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
2482 NULL, &ret_orig)))
2483 return;
2484
2485 Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
2486 {
2487 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2488
2489 op.params[0].value.a = 20;
2490 op.params[0].value.b = 10;
2491 op.params[1].value.a = '+';
2492 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
2493 TEEC_VALUE_INPUT, TEEC_NONE,
2494 TEEC_NONE);
2495
2496 ADBG_EXPECT_TEEC_SUCCESS(c,
2497 TEEC_InvokeCommand(&session,
2498 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2499 &ret_orig));
2500 ADBG_EXPECT(c, 30, op.params[0].value.a);
2501
2502 /* reassign, because the values was changed during previous op */
2503 op.params[0].value.a = 20;
2504 op.params[0].value.b = 10;
2505 op.params[1].value.a = '-';
2506 ADBG_EXPECT_TEEC_SUCCESS(c,
2507 TEEC_InvokeCommand(&session,
2508 TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
2509 &ret_orig));
2510 ADBG_EXPECT(c, 10, op.params[0].value.a);
2511 }
2512 Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
2513
2514 Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
2515 {
2516 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002517 uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002518
2519 op.params[0].tmpref.buffer = to_plugin;
2520 op.params[0].tmpref.size = sizeof(to_plugin);
Jens Wiklander104cd062022-12-09 13:44:26 +01002521 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT,
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002522 TEEC_VALUE_OUTPUT,
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002523 TEEC_NONE, TEEC_NONE);
2524
2525 ADBG_EXPECT_TEEC_SUCCESS(c,
2526 TEEC_InvokeCommand(&session,
2527 TA_SUPP_PLUGIN_CMD_WRITE_ARR,
2528 &op, &ret_orig));
2529
2530 /*
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002531 * The test plugin must calculate a sum of the input elements
2532 * and store it to 'op.params[1].value.a'
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002533 */
Aleksandr Anisimov80768392021-03-05 19:36:20 +03002534 ADBG_EXPECT(c, 15, op.params[1].value.a);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002535 }
2536 Do_ADBG_EndSubCase(c, "Pass array to a plugin");
2537
2538 Do_ADBG_BeginSubCase(c, "Get array from a plugin");
2539 {
2540 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2541 char from_plugin[64] = { };
2542 char expected_arr[] = "Array from plugin";
2543 size_t expectes_size = sizeof(expected_arr);
2544
2545 op.params[0].tmpref.buffer = from_plugin;
2546 op.params[0].tmpref.size = sizeof(from_plugin);
2547 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2548 TEEC_VALUE_OUTPUT, TEEC_NONE,
2549 TEEC_NONE);
2550 ADBG_EXPECT_TEEC_SUCCESS(c,
2551 TEEC_InvokeCommand(&session,
2552 TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
2553 &ret_orig));
2554 ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
2555 ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
2556 }
2557 Do_ADBG_EndSubCase(c, "Get array from a plugin");
2558
2559 Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
2560 {
2561 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2562
2563 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
2564 TEEC_NONE, TEEC_NONE);
2565 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2566 TEEC_InvokeCommand(&session,
2567 TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
2568 &ret_orig));
2569 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2570 TEEC_InvokeCommand(&session,
2571 TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
2572 &ret_orig));
2573 ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
2574 TEEC_InvokeCommand(&session,
2575 TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
2576 &ret_orig));
2577 }
2578 Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
2579
2580 Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
2581 {
2582 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2583
2584 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
Vincent Mailhol2ea0aa72023-11-16 20:20:54 +09002585 TEEC_NONE, TEEC_NONE);
Aleksandr Anisimov01f6f062021-01-19 11:02:25 +03002586 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
2587 TEEC_InvokeCommand(&session,
2588 TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
2589 &op, &ret_orig));
2590 }
2591 Do_ADBG_EndSubCase(c, "Call an unknown plugin");
2592
2593 TEEC_CloseSession(&session);
2594}
2595ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
2596 "Test the supplicant plugin framework");
Jens Wiklander94fb1582021-05-19 10:14:57 +02002597
2598static void xtest_tee_test_1034(ADBG_Case_t *c)
2599{
2600 TEEC_Result res = TEEC_SUCCESS;
2601 TEEC_Session session = { };
2602 uint32_t ret_orig = 0;
2603
2604 res = xtest_teec_open_session(&session, &large_ta_uuid, NULL,
2605 &ret_orig);
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002606 if (res == TEEC_ERROR_OUT_OF_MEMORY) {
2607 Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored");
Tim Lee5502a992023-07-12 16:48:47 +08002608 return;
Jerome Forissiere8be5b52021-06-29 16:29:06 +02002609 }
Tim Lee5502a992023-07-12 16:48:47 +08002610 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2611 if (!res)
2612 TEEC_CloseSession(&session);
Jens Wiklander94fb1582021-05-19 10:14:57 +02002613}
2614ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034,
2615 "Test loading a large TA");
Ruchika Gupta813b6d42021-12-01 10:44:14 +05302616
2617#if defined(CFG_TA_BTI)
2618struct bti_test {
2619 uint32_t cmd;
2620 uint32_t func;
2621};
2622
2623#define BTI_TEST(caller_func, bti_func) { \
2624 .cmd = caller_func, \
2625 .func = bti_func, \
2626 }
2627
2628static const struct bti_test bti_cases_success[] = {
2629 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C),
2630 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC),
2631 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J),
2632 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC),
2633 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C),
2634 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J),
2635 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC),
2636};
2637
2638static const struct bti_test bti_cases_panic[] = {
2639 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J),
2640 BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE),
2641 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C),
2642 BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE),
2643 BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE),
2644};
2645
2646static void get_cpu_feature(ADBG_Case_t *c, bool *bti)
2647{
2648 TEEC_Session session = {};
2649 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2650 uint32_t ret_orig = 0;
2651 TEEC_Result res;
2652
2653 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE,
2654 TEEC_NONE);
2655 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2656 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2657 NULL, &ret_orig)))
2658 return;
2659
2660 res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op,
2661 &ret_orig);
2662 if (!res) {
2663 if(op.params[0].value.a)
2664 *bti = true;
2665 Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT ");
2666 }
2667
2668 TEEC_CloseSession(&session);
2669}
2670
2671static void xtest_tee_test_1035(ADBG_Case_t *c)
2672{
2673 TEEC_Session session = {};
2674 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2675 struct bti_test const *test = NULL;
2676 uint32_t ret_orig = 0;
2677 TEEC_Result res;
2678 unsigned int n = 0;
2679 bool cpu_feature_bti = false;
2680
2681 Do_ADBG_BeginSubCase(c, "BTI Implemented");
2682 get_cpu_feature(c, &cpu_feature_bti);
2683 Do_ADBG_EndSubCase(c, "BTI Implemented");
2684
2685 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
2686 TEEC_NONE);
2687
2688 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2689 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2690 NULL, &ret_orig)))
2691 return;
2692
2693 Do_ADBG_BeginSubCase(c, "BTI Pass Cases");
2694 for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) {
2695 test = &bti_cases_success[n];
2696
2697 op.params[0].value.a = test->func;
2698
2699 res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig);
2700 if (res == TEEC_ERROR_NOT_IMPLEMENTED) {
2701 Do_ADBG_Log("Binary doesn't support BTI - skip tests");
2702 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2703 TEEC_CloseSession(&session);
2704 return;
2705 }
2706
2707 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2708
2709 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2710
2711 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2712 }
2713 Do_ADBG_EndSubCase(c, "BTI Pass Cases");
2714
2715 TEEC_CloseSession(&session);
2716
2717 Do_ADBG_BeginSubCase(c, "BTI Exception Generation");
2718 for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) {
2719 test = &bti_cases_panic[n];
2720 res = TEEC_SUCCESS;
2721
2722 if (cpu_feature_bti)
2723 res = TEEC_ERROR_TARGET_DEAD;
2724
2725 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2726 xtest_teec_open_session(&session, &bti_test_ta_uuid,
2727 NULL, &ret_orig)))
2728 goto out;
2729
2730 Do_ADBG_BeginSubCase(c, "BTI Case %u", n);
2731 op.params[0].value.a = test->func;
2732
2733 (void)ADBG_EXPECT_TEEC_RESULT(c, res,
2734 TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig));
2735
2736 if (cpu_feature_bti)
2737 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE,
2738 ret_orig);
2739
2740 Do_ADBG_EndSubCase(c, "BTI Case %u", n);
2741
2742 TEEC_CloseSession(&session);
2743 }
2744
2745out:
2746 Do_ADBG_EndSubCase(c, "BTI Exception Generation");
2747}
2748ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI");
2749#endif
Ruchika Gupta4808e382022-01-28 12:41:21 +05302750
2751static void xtest_tee_test_1036(ADBG_Case_t *c)
2752{
2753 TEEC_Session session = { };
2754 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2755 uint32_t ret_orig = 0;
2756 TEEC_Result res = TEEC_SUCCESS;
2757
2758 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
2759 TEEC_NONE);
2760
2761 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
2762 xtest_teec_open_session(&session, &os_test_ta_uuid,
2763 NULL, &ret_orig)))
2764 return;
2765
2766 Do_ADBG_BeginSubCase(c, "PAuth NOP test");
2767
2768 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op,
2769 &ret_orig);
2770 if (res == TEEC_ERROR_NOT_SUPPORTED) {
2771 Do_ADBG_Log("Binary doesn't support PAuth - skip tests");
2772 goto out;
2773 }
2774
2775 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2776 goto out;
2777
2778 Do_ADBG_EndSubCase(c, "PAuth NOP test");
2779
2780 Do_ADBG_BeginSubCase(c, "PAuth PAC corruption");
2781
2782 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
2783 TEEC_InvokeCommand(&session,
2784 TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC,
2785 &op, &ret_orig));
2786
2787 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
2788
2789 Do_ADBG_EndSubCase(c, "PAuth PAC corruption");
2790
2791out:
2792 TEEC_CloseSession(&session);
2793}
2794ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036,
2795 "Test PAuth (Pointer Authentication)");
Jerome Forissier0915b222021-10-27 18:20:59 +02002796
2797#define ATT_MAX_KEYSZ 4096
2798
2799#ifdef OPENSSL_FOUND
2800static RSA *att_key;
2801static size_t att_key_size; /* Actual key size (modulus size) in bytes */
2802
2803/*
2804 * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ]
2805 * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash])
2806 */
2807static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2808 uint8_t *buf)
2809{
2810 unsigned char digest[SHA256_DIGEST_LENGTH] = { };
2811 unsigned char *sig = buf + SHA256_DIGEST_LENGTH;
2812 unsigned char *ta_hash = buf;
2813 uint8_t decr[ATT_MAX_KEYSZ / 8] = { };
2814 SHA256_CTX ctx = { };
2815 int salt_len = 32; /* Hard-coded in the PTA */
2816 int st = 0;
2817
2818 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2819 return;
2820
2821 SHA256_Init(&ctx);
2822 SHA256_Update(&ctx, nonce, nonce_size);
2823 SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH);
2824 SHA256_Final(digest, &ctx);
2825
2826 st = RSA_public_decrypt(att_key_size, sig, decr, att_key,
2827 RSA_NO_PADDING);
2828 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2829 st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(),
2830 EVP_sha256(), decr, salt_len);
2831 ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0);
2832}
2833
2834static void free_att_key(void)
2835{
2836 RSA_free(att_key);
2837 att_key = NULL;
2838}
2839
Clement Faure44a31d02022-03-30 10:50:52 +02002840#if OPENSSL_VERSION_NUMBER < 0x10100000L
2841static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
2842{
2843 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
2844 return 0;
2845
2846 if (n != NULL) {
2847 BN_free(r->n);
2848 r->n = n;
2849 }
2850
2851 if (e != NULL) {
2852 BN_free(r->e);
2853 r->e = e;
2854 }
2855
2856 if (d != NULL) {
2857 BN_free(r->d);
2858 r->d = d;
2859 }
2860
2861 return 1;
2862}
2863#endif
2864
Jerome Forissier0915b222021-10-27 18:20:59 +02002865static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n,
2866 size_t n_sz)
2867{
2868 BIGNUM *bn_e = NULL;
2869 BIGNUM *bn_n = NULL;
2870 int st = 0;
2871
2872 att_key_size = n_sz;
2873 att_key = RSA_new();
2874 if (!ADBG_EXPECT_NOT_NULL(c, att_key))
2875 return;
2876
2877 bn_e = BN_bin2bn(e, e_sz, NULL);
2878 if (!ADBG_EXPECT_NOT_NULL(c, bn_e))
2879 goto err;
2880 bn_n = BN_bin2bn(n, n_sz, NULL);
2881 if (!ADBG_EXPECT_NOT_NULL(c, bn_n))
2882 goto err;
2883
2884 st = RSA_set0_key(att_key, bn_n, bn_e, BN_new());
2885 if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0))
2886 goto err;
2887 return;
2888err:
2889 free_att_key();
2890}
2891#else
2892#define check_signature(...)
2893#define set_att_key(...)
2894#define free_att_key()
2895#endif
2896
2897/*
2898 * Verification of the output of the attestation PTA
2899 * - (If hash != NULL) check buf contains the expected hash
2900 * - (If OpenSSL is available) Check that the signature is valid
2901 */
2902static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size,
2903 uint8_t *hash, uint8_t *buf)
2904{
Pierre Moosc7f733c2022-08-22 15:39:22 +02002905 (void)nonce;
2906 (void)nonce_size;
2907
Jerome Forissier0915b222021-10-27 18:20:59 +02002908 if (hash)
2909 ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32);
2910
2911 check_signature(c, nonce, nonce_size, buf);
2912}
2913
2914/* Invoke attestation PTA to return the public key */
2915static void get_att_public_key(ADBG_Case_t *c)
2916{
2917 uint8_t n[ATT_MAX_KEYSZ / 8] = { };
2918 uint8_t e[3] = { }; /* We know e == 65537... */
2919 size_t n_sz = sizeof(n);
2920 size_t e_sz = sizeof(e);
2921 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2922 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2923 TEEC_Result res = TEEC_ERROR_GENERIC;
2924 TEEC_Session session = { };
2925 uint32_t ret_orig = 0;
2926
2927 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
2928 TEEC_MEMREF_TEMP_OUTPUT,
2929 TEEC_VALUE_OUTPUT, TEEC_NONE);
2930 op.params[0].tmpref.buffer = e;
2931 op.params[0].tmpref.size = e_sz;
2932 op.params[1].tmpref.buffer = n;
2933 op.params[1].tmpref.size = n_sz;
2934
2935 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2936 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2937 return;
2938
2939 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op,
2940 &ret_orig);
2941
2942 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) ||
2943 !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==,
2944 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256))
2945 goto out;
2946
2947 e_sz = op.params[0].tmpref.size;
2948 n_sz = op.params[1].tmpref.size;
2949 set_att_key(c, e, e_sz, n, n_sz);
2950out:
2951 TEEC_CloseSession(&session);
2952}
2953
2954/* Invoke attestation PTA to hash the TEE binary */
2955static void attestation_tee(ADBG_Case_t *c)
2956{
2957 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2958 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2959 uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 };
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
2965 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
2966 TEEC_MEMREF_TEMP_OUTPUT,
2967 TEEC_NONE, TEEC_NONE);
2968 op.params[0].tmpref.buffer = nonce;
2969 op.params[0].tmpref.size = sizeof(nonce);
2970 op.params[1].tmpref.buffer = measurement;
2971 op.params[1].tmpref.size = sizeof(measurement);
2972
2973 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
2974 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
2975 return;
2976
2977 /* Hash TEE and check signature */
2978 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op,
2979 &ret_orig);
2980 ADBG_EXPECT_TEEC_SUCCESS(c, res);
2981 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
2982
2983 TEEC_CloseSession(&session);
2984}
2985
2986/* Invoke attestation PTA to obtain the digest contained in the some TA shdr */
2987static void attestation_ta_shdr(ADBG_Case_t *c)
2988{
2989 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
2990 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
2991 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
2992 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
2993 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
2994 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
2995 TEEC_Result res = TEEC_ERROR_GENERIC;
2996 TEEC_Session session = { };
2997 uint32_t ret_orig = 0;
2998 int cmp = 0;
2999
3000 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3001 TEEC_MEMREF_TEMP_INPUT,
3002 TEEC_MEMREF_TEMP_OUTPUT,
3003 TEEC_NONE);
3004 op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid;
3005 op.params[0].tmpref.size = sizeof(TEEC_UUID);
3006 op.params[1].tmpref.buffer = nonce;
3007 op.params[1].tmpref.size = sizeof(nonce);
3008 op.params[2].tmpref.buffer = measurement;
3009 op.params[2].tmpref.size = sizeof(measurement);
3010
3011 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3012 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3013 return;
3014
3015 /* Hash TA and check signature */
3016 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3017 &op, &ret_orig);
3018 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3019 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3020 /* Save hash */
3021 memcpy(hash1, measurement, 32);
3022
3023 /* Hash TA again */
3024 memset(measurement, 0, sizeof(measurement));
3025 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3026 &op, &ret_orig);
3027 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3028 /* New hash should be identical */
3029 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3030
3031 /* Hash another TA */
3032 op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid;
3033 memset(measurement, 0, sizeof(measurement));
3034 res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST,
3035 &op, &ret_orig);
3036 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3037 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3038 memcpy(hash2, measurement, 32);
3039 /* Different binaries should have different hashes */
3040 cmp = memcmp(hash1, hash2, sizeof(hash1));
3041 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3042
3043 TEEC_CloseSession(&session);
3044}
3045
3046/*
3047 * Invoke os_test TA which will invoke attestation PTA to obtain a hash of
3048 * itself.
3049 */
3050static void attestation_ta_memory(ADBG_Case_t *c)
3051{
3052 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3053 uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 };
3054 uint8_t hash1[TEE_SHA256_HASH_SIZE] = { };
3055 uint8_t hash2[TEE_SHA256_HASH_SIZE] = { };
3056 uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { };
3057 TEEC_Result res = TEEC_ERROR_GENERIC;
3058 TEEC_Session session = { };
3059 uint32_t ret_orig = 0;
3060 int cmp = 0;
3061
3062 Do_ADBG_BeginSubCase(c, "Consecutive calls");
3063
3064 /* Open session to os_test TA */
3065 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3066 &ret_orig);
3067 if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
3068 return;
3069
3070 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3071 TEEC_MEMREF_TEMP_OUTPUT,
3072 TEEC_NONE, TEEC_NONE);
3073 op.params[0].tmpref.buffer = nonce;
3074 op.params[0].tmpref.size = sizeof(nonce);
3075 op.params[1].tmpref.buffer = measurement;
3076 op.params[1].tmpref.size = sizeof(measurement);
3077
3078 /* Hash TA */
3079 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3080 &ret_orig);
3081 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3082
3083 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3084 memcpy(hash1, measurement, 32);
3085
3086 /* Hash TA again */
3087 memset(measurement, 0, sizeof(measurement));
3088 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3089 &ret_orig);
3090 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3091
3092 /* New hash should be identical to hash1 */
3093 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3094
3095 Do_ADBG_EndSubCase(c, "Consecutive calls");
3096
3097 /* Close TA session, will cause unload of TA */
3098 TEEC_CloseSession(&session);
3099
3100 Do_ADBG_BeginSubCase(c, "TA reload");
3101
3102 /* Load TA again and open a new session */
3103 res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
3104 &ret_orig);
3105 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3106 if (res)
3107 return;
3108
3109 /* Hash TA one more time */
3110 memset(measurement, 0, sizeof(measurement));
3111 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3112 &ret_orig);
3113 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3114
3115 /* Hash after reload should still be the same */
3116 check_measurement(c, nonce, sizeof(nonce), hash1, measurement);
3117
3118 Do_ADBG_EndSubCase(c, "TA reload");
3119
3120 Do_ADBG_BeginSubCase(c, "Add shared library");
3121
3122 /*
3123 * Invoke a TA command that causes some additional code to be mapped
3124 * (shared library)
3125 */
3126 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
3127 &ret_orig);
3128 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3129
3130 /* Hash TA one last time */
3131 memset(measurement, 0, sizeof(measurement));
3132 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op,
3133 &ret_orig);
3134 ADBG_EXPECT_TEEC_SUCCESS(c, res);
3135
3136 check_measurement(c, nonce, sizeof(nonce), NULL, measurement);
3137 memcpy(hash2, measurement, 32);
3138
3139 /* Different binaries mapped mean different hashes */
3140 cmp = memcmp(hash1, hash2, sizeof(hash1));
3141 ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0);
3142
3143 Do_ADBG_EndSubCase(c, "Add shared library");
3144
3145 TEEC_CloseSession(&session);
3146}
3147
3148static void xtest_tee_test_1037(ADBG_Case_t *c)
3149{
3150 TEEC_UUID att_uuid = PTA_ATTESTATION_UUID;
3151 TEEC_Result res = TEEC_ERROR_GENERIC;
3152 TEEC_Session session = { };
3153 uint32_t ret_orig = 0;
3154
3155 res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig);
3156 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3157 Do_ADBG_Log(" skip test, pseudo TA not found");
3158 return;
3159 }
3160
chenchaokaiaf751932023-05-29 14:25:50 +08003161 TEEC_CloseSession(&session);
3162
Jerome Forissier0915b222021-10-27 18:20:59 +02003163 Do_ADBG_BeginSubCase(c, "Get public key");
3164 get_att_public_key(c);
3165 Do_ADBG_EndSubCase(c, "Get public key");
3166
3167 Do_ADBG_BeginSubCase(c, "TEE attestation");
3168 attestation_tee(c);
3169 Do_ADBG_EndSubCase(c, "TEE attestation");
3170
3171 Do_ADBG_BeginSubCase(c, "TA attestation (shdr)");
3172 attestation_ta_shdr(c);
3173 Do_ADBG_EndSubCase(c, "TA attestation (shdr)");
3174
3175 Do_ADBG_BeginSubCase(c, "TA attestation (memory)");
3176 attestation_ta_memory(c);
3177 Do_ADBG_EndSubCase(c, "TA attestation (memory)");
3178
3179 free_att_key();
3180}
3181ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037,
3182 "Remote attestation");
Jens Wiklander50339ef2022-04-12 20:47:27 +02003183
3184static void xtest_tee_test_1038(ADBG_Case_t *c)
3185{
3186 TEEC_Session session = { };
3187 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3188 uint32_t ret_orig = 0;
3189 TEEC_Result res = TEEC_SUCCESS;
3190
3191 Do_ADBG_BeginSubCase(c, "MTE use after free");
3192
3193 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3194 TEEC_NONE);
3195
3196 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3197 xtest_teec_open_session(&session, &os_test_ta_uuid,
3198 NULL, &ret_orig)))
3199 return;
3200
3201 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE,
3202 &op, &ret_orig);
3203 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3204 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3205 goto out;
3206 }
3207
3208 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3209 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3210 TEEC_CloseSession(&session);
3211
3212 Do_ADBG_EndSubCase(c, "MTE use after free");
3213
3214 Do_ADBG_BeginSubCase(c, "MTE invalid tag");
3215
3216 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3217 TEEC_NONE);
3218
3219 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3220 xtest_teec_open_session(&session, &os_test_ta_uuid,
3221 NULL, &ret_orig)))
3222 return;
3223
3224 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG,
3225 &op, &ret_orig);
3226 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3227 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3228 goto out;
3229 }
3230
3231 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3232 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3233
3234 Do_ADBG_EndSubCase(c, "MTE invalid tag");
3235
3236 Do_ADBG_BeginSubCase(c, "MTE double free");
3237
3238 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3239 TEEC_NONE);
3240
3241 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3242 xtest_teec_open_session(&session, &os_test_ta_uuid,
3243 NULL, &ret_orig)))
3244 return;
3245
3246 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE,
3247 &op, &ret_orig);
3248 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3249 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3250 goto out;
3251 }
3252
3253 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3254 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3255
3256 Do_ADBG_EndSubCase(c, "MTE double free");
3257
3258 Do_ADBG_BeginSubCase(c, "MTE buffer overrun");
3259
3260 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
3261 TEEC_NONE);
3262
3263 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
3264 xtest_teec_open_session(&session, &os_test_ta_uuid,
3265 NULL, &ret_orig)))
3266 return;
3267
3268 res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN,
3269 &op, &ret_orig);
3270 if (res == TEEC_ERROR_NOT_SUPPORTED) {
3271 Do_ADBG_Log("Binary doesn't support MTE - skip tests");
3272 goto out;
3273 }
3274
3275 ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res);
3276 ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
3277
3278 Do_ADBG_EndSubCase(c, "MTE buffer overrun");
3279
3280
3281out:
3282 TEEC_CloseSession(&session);
3283}
3284ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038,
3285 "Test MTE (Memory Tag Extension)");
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003286
3287static void xtest_tee_test_1039(ADBG_Case_t *c)
3288{
3289 TEEC_Session session = { };
3290 uint32_t ret_orig = 0;
3291
3292 Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys");
3293 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3294 xtest_teec_open_session(&session, &subkey1_ta_uuid,
3295 NULL, &ret_orig)))
3296 TEEC_CloseSession(&session);
3297 Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys");
3298
3299 Do_ADBG_BeginSubCase(c, "Load TA with identity subkey");
3300 if (ADBG_EXPECT_TEEC_SUCCESS(c,
3301 xtest_teec_open_session(&session, &subkey2_ta_uuid,
3302 NULL, &ret_orig)))
3303 TEEC_CloseSession(&session);
3304 Do_ADBG_EndSubCase(c, "Load TA with identity subkey");
3305
3306}
Jens Wiklanderdd336da2022-09-02 13:57:54 +02003307ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039,
3308 "Test subkey verification");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003309
3310struct test_1040_thread_arg {
3311 TEEC_Result res;
3312 pthread_t thr;
3313};
3314
3315static void *test_1040_thread(void *arg)
3316{
3317 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
3318 struct test_1040_thread_arg *a = arg;
3319 TEEC_Result res = TEEC_SUCCESS;
3320 uint32_t err_orig = 0;
3321 TEEC_Session session = { };
3322 size_t loop_count = 100;
3323 size_t n = 0;
3324
3325 if (level == 0)
3326 loop_count /= 2;
3327
3328 while (n < loop_count) {
3329 res = xtest_teec_open_session(&session, &sims_test_ta_uuid,
3330 NULL, &err_orig);
3331 if (res) {
3332 if (res == TEEC_ERROR_TARGET_DEAD)
3333 continue;
3334 a->res = res;
3335 return NULL;
3336 }
3337
3338 memset(&op, 0, sizeof(op));
3339 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
3340 TEEC_NONE, TEEC_NONE,
3341 TEEC_NONE);
3342 res = TEEC_InvokeCommand(&session, TA_SIMS_CMD_PANIC, &op,
3343 &err_orig);
3344 TEEC_CloseSession(&session);
3345 if (res != TEEC_ERROR_TARGET_DEAD) {
3346 if (res)
3347 a->res = res;
3348 else
3349 a->res = TEEC_ERROR_GENERIC;
3350 return NULL;
3351 }
3352 n++;
3353 }
3354 a->res = TEEC_SUCCESS;
3355 return NULL;
3356}
3357
3358static void xtest_tee_test_1040(ADBG_Case_t *c)
3359{
3360 struct test_1040_thread_arg arg[NUM_THREADS] = { };
3361 size_t nt = NUM_THREADS;
3362 size_t n = 0;
3363
Alvin Chang5fae7bd2025-03-20 17:14:12 +08003364 Do_ADBG_BeginSubCase(c, "Concurrent invoke with panic in TA");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003365 for (n = 0; n < nt; n++) {
3366 if (!ADBG_EXPECT(c, 0, pthread_create(&arg[n].thr, NULL,
3367 test_1040_thread,
3368 arg + n)))
3369 nt = n; /* break loop and start cleanup */
3370 }
3371 for (n = 0; n < nt; n++) {
3372 ADBG_EXPECT(c, 0, pthread_join(arg[n].thr, NULL));
3373 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
3374 }
Alvin Chang5fae7bd2025-03-20 17:14:12 +08003375 Do_ADBG_EndSubCase(c, "Concurrent invoke with panic in TA");
Jens Wiklandere1b64452023-09-01 20:21:16 +02003376}
3377ADBG_CASE_DEFINE(regression, 1040, xtest_tee_test_1040,
3378 "Test panic in concurrent open/invoke/close session");
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003379
3380static void xtest_tee_test_1041(ADBG_Case_t *c)
3381{
3382 const char fname[] = "/dev/tpm0";
3383 TEE_Result res = TEEC_SUCCESS;
3384 TEEC_Session sess = { };
3385 uint32_t ret_orig = 0;
3386 struct stat sb = { };
Jerome Forissiera1739a12025-01-15 10:10:36 +01003387 bool found = false;
Jens Wiklander237d2782025-02-04 14:43:20 +01003388 int max_wait = 20;
Jerome Forissiera1739a12025-01-15 10:10:36 +01003389 int i = 0;
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003390
3391 res = xtest_teec_open_session(&sess, &(const TEEC_UUID)TA_FTPM_UUID,
3392 NULL, &ret_orig);
3393 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
3394 Do_ADBG_Log("skip test, fTPM TA not present");
3395 return;
3396 }
3397 if (res != TEEC_ERROR_BUSY && ADBG_EXPECT_TEEC_SUCCESS(c, res))
3398 TEEC_CloseSession(&sess);
3399
Jerome Forissiera1739a12025-01-15 10:10:36 +01003400 /*
3401 * Check for the presence of the TPM device. Give the kernel some time
3402 * (5 seconds) otherwise the test may fail if run immediately after
3403 * boot.
3404 */
Jens Wiklander237d2782025-02-04 14:43:20 +01003405 for (i = 0; i < max_wait; i++) {
Jerome Forissiera1739a12025-01-15 10:10:36 +01003406 if (stat(fname, &sb) == 0) {
3407 found = true;
3408 break;
3409 }
Jens Wiklander237d2782025-02-04 14:43:20 +01003410 Do_ADBG_Log("Waiting for TPM device %d / %d", i, max_wait);
Jerome Forissiera1739a12025-01-15 10:10:36 +01003411 sleep(1);
3412 }
3413 if (!ADBG_EXPECT_TRUE(c, found)) {
Jens Wiklander6b5d9a32024-12-10 17:43:47 +01003414 Do_ADBG_Log("stat(\"%s\"): %s", fname, strerror(errno));
3415 if (res != TEEC_ERROR_BUSY)
3416 Do_ADBG_Log("Perhaps fTPM hasn't finished probing");
3417 return;
3418 }
3419
3420 if (!ADBG_EXPECT_TRUE(c, S_ISCHR(sb.st_mode)))
3421 Do_ADBG_Log("Expected \"%s\" to be a character device", fname);
3422}
3423ADBG_CASE_DEFINE(regression, 1041, xtest_tee_test_1041, "Test fTPM sanity");