Etienne Carriere | 9b7b70d | 2020-05-16 10:27:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 2 | /* |
Javier Almansa Sobrino | cddc000 | 2020-02-10 13:35:37 +0000 | [diff] [blame] | 3 | * Copyright (c) 2020, ARM Limited. All rights reserved. |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 4 | * Copyright (c) 2014, STMicroelectronics International N.V. |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 7 | #include <errno.h> |
Etienne Carriere | a465355 | 2017-01-11 10:04:24 +0100 | [diff] [blame] | 8 | #include <limits.h> |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 9 | #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 Carriere | a465355 | 2017-01-11 10:04:24 +0100 | [diff] [blame] | 19 | #include <pthread.h> |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 20 | #ifdef CFG_SECURE_DATA_PATH |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 21 | #include <sdp_basic.h> |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 22 | #endif |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 23 | #include <signed_hdr.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 26 | #include <string.h> |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 27 | #include <sys/types.h> |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 28 | #include <ta_arm_bti.h> |
| 29 | #include <ta_concurrent.h> |
| 30 | #include <ta_create_fail_test.h> |
| 31 | #include <ta_crypt.h> |
| 32 | #include <ta_miss_test.h> |
| 33 | #include <ta_os_test.h> |
| 34 | #include <ta_rpc_test.h> |
| 35 | #include <ta_sims_keepalive_test.h> |
| 36 | #include <ta_sims_test.h> |
| 37 | #include <ta_supp_plugin.h> |
| 38 | #include <ta_tpm_log_test.h> |
| 39 | #include <test_supp_plugin.h> |
Etienne Carriere | a465355 | 2017-01-11 10:04:24 +0100 | [diff] [blame] | 40 | #include <unistd.h> |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 41 | #include <utee_defines.h> |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 42 | #include <util.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 43 | |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 44 | #include "xtest_helpers.h" |
| 45 | #include "xtest_test.h" |
| 46 | #include "xtest_uuid_helpers.h" |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 47 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 48 | #ifndef MIN |
| 49 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 50 | #endif |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 51 | |
Etienne Carriere | c0a6172 | 2023-03-07 17:47:42 +0100 | [diff] [blame] | 52 | #define STATS_UUID \ |
| 53 | { 0xd96a5b40, 0xe2c7, 0xb1af, \ |
| 54 | { 0x87, 0x94, 0x10, 0x02, 0xa5, 0xd5, 0xc6, 0x1b } } |
| 55 | |
| 56 | #define STATS_CMD_PAGER_STATS 0 |
| 57 | |
| 58 | #define PAGER_PAGE_COUNT_THRESHOLD ((128 * 1024) / 4096) |
| 59 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 60 | struct xtest_crypto_session { |
| 61 | ADBG_Case_t *c; |
| 62 | TEEC_Session *session; |
| 63 | uint32_t cmd_id_sha256; |
| 64 | uint32_t cmd_id_aes256ecb_encrypt; |
| 65 | uint32_t cmd_id_aes256ecb_decrypt; |
| 66 | }; |
| 67 | |
Etienne Carriere | c0a6172 | 2023-03-07 17:47:42 +0100 | [diff] [blame] | 68 | static bool optee_pager_with_small_pool(void) |
| 69 | { |
| 70 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 71 | TEEC_UUID uuid = STATS_UUID; |
| 72 | TEEC_Context ctx = { }; |
| 73 | TEEC_Session sess = { }; |
| 74 | TEEC_Operation op = { }; |
| 75 | uint32_t eo = 0; |
| 76 | bool rc = false; |
| 77 | |
| 78 | res = TEEC_InitializeContext(NULL, &ctx); |
| 79 | if (res) |
| 80 | return false; |
| 81 | |
| 82 | res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 83 | NULL, &eo); |
| 84 | if (res) |
| 85 | goto out_ctx; |
| 86 | |
| 87 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_VALUE_OUTPUT, |
| 88 | TEEC_VALUE_OUTPUT, TEEC_NONE); |
| 89 | res = TEEC_InvokeCommand(&sess, STATS_CMD_PAGER_STATS, &op, &eo); |
| 90 | if (res) |
| 91 | goto out_sess; |
| 92 | |
| 93 | if (op.params[0].value.b && |
| 94 | op.params[0].value.b <= PAGER_PAGE_COUNT_THRESHOLD) |
| 95 | rc = true; |
| 96 | |
| 97 | out_sess: |
| 98 | TEEC_CloseSession(&sess); |
| 99 | out_ctx: |
| 100 | TEEC_FinalizeContext(&ctx); |
| 101 | |
| 102 | return rc; |
| 103 | } |
| 104 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 105 | static void xtest_crypto_test(struct xtest_crypto_session *cs) |
| 106 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 107 | uint32_t ret_orig = 0; |
| 108 | uint8_t crypt_out[16] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 109 | uint8_t crypt_in[16] = { 22, 17 }; |
| 110 | |
| 111 | crypt_in[15] = 60; |
| 112 | |
| 113 | Do_ADBG_BeginSubCase(cs->c, "AES encrypt"); |
| 114 | { |
| 115 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 116 | |
| 117 | op.params[0].tmpref.buffer = crypt_in; |
| 118 | op.params[0].tmpref.size = sizeof(crypt_in); |
| 119 | op.params[1].tmpref.buffer = crypt_out; |
| 120 | op.params[1].tmpref.size = sizeof(crypt_out); |
| 121 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 122 | TEEC_MEMREF_TEMP_OUTPUT, |
| 123 | TEEC_NONE, TEEC_NONE); |
| 124 | |
| 125 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 126 | TEEC_InvokeCommand(cs->session, |
| 127 | cs-> |
| 128 | cmd_id_aes256ecb_encrypt, |
| 129 | &op, |
| 130 | &ret_orig)); |
| 131 | } |
| 132 | Do_ADBG_EndSubCase(cs->c, "AES encrypt"); |
| 133 | |
| 134 | Do_ADBG_BeginSubCase(cs->c, "AES decrypt"); |
| 135 | { |
| 136 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 137 | uint8_t out[16] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 138 | |
| 139 | op.params[0].tmpref.buffer = crypt_out; |
| 140 | op.params[0].tmpref.size = sizeof(crypt_out); |
| 141 | op.params[1].tmpref.buffer = out; |
| 142 | op.params[1].tmpref.size = sizeof(out); |
| 143 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 144 | TEEC_MEMREF_TEMP_OUTPUT, |
| 145 | TEEC_NONE, TEEC_NONE); |
| 146 | |
| 147 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 148 | TEEC_InvokeCommand(cs->session, |
| 149 | cs-> |
| 150 | cmd_id_aes256ecb_decrypt, |
| 151 | &op, |
| 152 | &ret_orig)); |
| 153 | |
| 154 | if (!ADBG_EXPECT(cs->c, 0, |
| 155 | memcmp(crypt_in, out, sizeof(crypt_in)))) { |
| 156 | Do_ADBG_Log("crypt_in:"); |
| 157 | Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16); |
| 158 | Do_ADBG_Log("out:"); |
| 159 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 160 | } |
| 161 | } |
| 162 | Do_ADBG_EndSubCase(cs->c, "AES decrypt"); |
| 163 | |
| 164 | Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input"); |
| 165 | { |
| 166 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 167 | static const uint8_t sha256_in[] = { 'a', 'b', 'c' }; |
| 168 | static const uint8_t sha256_out[] = { |
| 169 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
| 170 | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 171 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 172 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 173 | }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 174 | uint8_t out[32] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 175 | |
| 176 | op.params[0].tmpref.buffer = (void *)sha256_in; |
| 177 | op.params[0].tmpref.size = sizeof(sha256_in); |
| 178 | op.params[1].tmpref.buffer = out; |
| 179 | op.params[1].tmpref.size = sizeof(out); |
| 180 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 181 | TEEC_MEMREF_TEMP_OUTPUT, |
| 182 | TEEC_NONE, TEEC_NONE); |
| 183 | |
| 184 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 185 | TEEC_InvokeCommand(cs->session, |
| 186 | cs-> |
| 187 | cmd_id_sha256, |
| 188 | &op, |
| 189 | &ret_orig)); |
| 190 | |
| 191 | if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out, |
| 192 | sizeof(sha256_out)))) { |
| 193 | Do_ADBG_Log("sha256_out:"); |
| 194 | Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16); |
| 195 | Do_ADBG_Log("out:"); |
| 196 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 197 | } |
| 198 | } |
| 199 | Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input"); |
| 200 | |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 201 | Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 202 | { |
| 203 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 204 | static const uint8_t in[] = { |
| 205 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 206 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 207 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 208 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 209 | }; |
| 210 | static const uint8_t exp_out[] = { |
| 211 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 212 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 213 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 214 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 215 | }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 216 | uint8_t out[sizeof(exp_out)] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 217 | |
| 218 | op.params[0].tmpref.buffer = (void *)in; |
| 219 | op.params[0].tmpref.size = sizeof(in); |
| 220 | op.params[1].tmpref.buffer = out; |
| 221 | op.params[1].tmpref.size = sizeof(out); |
| 222 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 223 | TEEC_MEMREF_TEMP_OUTPUT, |
| 224 | TEEC_NONE, TEEC_NONE); |
| 225 | |
| 226 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 227 | TEEC_InvokeCommand(cs->session, |
| 228 | cs-> |
| 229 | cmd_id_aes256ecb_encrypt, |
| 230 | &op, |
| 231 | &ret_orig)); |
| 232 | |
| 233 | if (!ADBG_EXPECT(cs->c, 0, |
| 234 | memcmp(exp_out, out, sizeof(exp_out)))) { |
| 235 | Do_ADBG_Log("exp_out:"); |
| 236 | Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16); |
| 237 | Do_ADBG_Log("out:"); |
| 238 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 239 | } |
| 240 | } |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 241 | Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 242 | |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 243 | Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 244 | { |
| 245 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 246 | static const uint8_t in[] = { |
| 247 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 248 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 249 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 250 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 251 | }; |
| 252 | static const uint8_t exp_out[] = { |
| 253 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 254 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 255 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 256 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 257 | }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 258 | uint8_t out[sizeof(exp_out)] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 259 | |
| 260 | op.params[0].tmpref.buffer = (void *)in; |
| 261 | op.params[0].tmpref.size = sizeof(in); |
| 262 | op.params[1].tmpref.buffer = out; |
| 263 | op.params[1].tmpref.size = sizeof(out); |
| 264 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 265 | TEEC_MEMREF_TEMP_OUTPUT, |
| 266 | TEEC_NONE, TEEC_NONE); |
| 267 | |
| 268 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 269 | TEEC_InvokeCommand(cs->session, |
| 270 | cs-> |
| 271 | cmd_id_aes256ecb_decrypt, |
| 272 | &op, |
| 273 | &ret_orig)); |
| 274 | |
| 275 | if (!ADBG_EXPECT(cs->c, 0, |
| 276 | memcmp(exp_out, out, sizeof(exp_out)))) { |
| 277 | Do_ADBG_Log("exp_out:"); |
| 278 | Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16); |
| 279 | Do_ADBG_Log("out:"); |
| 280 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 281 | } |
| 282 | } |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 283 | Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void xtest_tee_test_1001(ADBG_Case_t *c) |
| 287 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 288 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 289 | TEEC_Session session = { }; |
| 290 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 291 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 292 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Etienne Carriere | 726d8bc | 2017-03-21 15:45:59 +0100 | [diff] [blame] | 293 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 294 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 295 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 296 | Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found"); |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 297 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 298 | } |
Etienne Carriere | 3ea9672 | 2022-03-11 09:16:40 +0100 | [diff] [blame] | 299 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 300 | return; |
| 301 | |
| 302 | Do_ADBG_BeginSubCase(c, "Core self tests"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 303 | |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 304 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand( |
Etienne Carriere | 726d8bc | 2017-03-21 15:45:59 +0100 | [diff] [blame] | 305 | &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig)); |
Etienne Carriere | 3ea9672 | 2022-03-11 09:16:40 +0100 | [diff] [blame] | 306 | |
| 307 | Do_ADBG_EndSubCase(c, "Core self tests"); |
| 308 | |
| 309 | Do_ADBG_BeginSubCase(c, "Core dt_driver self tests"); |
| 310 | |
| 311 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand( |
| 312 | &session, PTA_INVOKE_TESTS_CMD_DT_DRIVER_TESTS, NULL, |
| 313 | &ret_orig)); |
| 314 | |
| 315 | Do_ADBG_EndSubCase(c, "Core dt_driver self tests"); |
| 316 | |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 317 | TEEC_CloseSession(&session); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 318 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 319 | ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 320 | |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 321 | static void xtest_tee_test_1002(ADBG_Case_t *c) |
| 322 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 323 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 324 | TEEC_Session session = { }; |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 325 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 326 | uint32_t ret_orig = 0; |
| 327 | uint8_t buf[16 * 1024] = { }; |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 328 | uint8_t exp_sum = 0; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 329 | size_t n = 0; |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 330 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 331 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 332 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 333 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 334 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 335 | Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found"); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 336 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 337 | } |
| 338 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 339 | |
| 340 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE, |
| 341 | TEEC_NONE, TEEC_NONE); |
| 342 | op.params[0].tmpref.size = sizeof(buf); |
| 343 | op.params[0].tmpref.buffer = buf; |
| 344 | |
| 345 | for (n = 0; n < sizeof(buf); n++) |
| 346 | buf[n] = n + 1; |
| 347 | for (n = 0; n < sizeof(buf); n++) |
| 348 | exp_sum += buf[n]; |
| 349 | |
| 350 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand( |
| 351 | &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig))) |
| 352 | goto out; |
| 353 | |
| 354 | ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]); |
| 355 | out: |
| 356 | TEEC_CloseSession(&session); |
| 357 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 358 | ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters"); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 359 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 360 | struct test_1003_arg { |
| 361 | uint32_t test_type; |
| 362 | size_t repeat; |
| 363 | size_t max_before_lockers; |
| 364 | size_t max_during_lockers; |
| 365 | size_t before_lockers; |
| 366 | size_t during_lockers; |
| 367 | TEEC_Result res; |
| 368 | uint32_t error_orig; |
| 369 | }; |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 370 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 371 | static void *test_1003_thread(void *arg) |
| 372 | { |
| 373 | struct test_1003_arg *a = arg; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 374 | TEEC_Session session = { }; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 375 | size_t rounds = 64 * 1024; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 376 | size_t n = 0; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 377 | |
| 378 | a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, |
| 379 | NULL, &a->error_orig); |
| 380 | if (a->res != TEEC_SUCCESS) |
| 381 | return NULL; |
| 382 | |
| 383 | for (n = 0; n < a->repeat; n++) { |
| 384 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 385 | |
| 386 | op.params[0].value.a = a->test_type; |
| 387 | op.params[0].value.b = rounds; |
| 388 | |
| 389 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 390 | TEEC_VALUE_OUTPUT, |
| 391 | TEEC_NONE, TEEC_NONE); |
| 392 | a->res = TEEC_InvokeCommand(&session, |
| 393 | PTA_INVOKE_TESTS_CMD_MUTEX, |
| 394 | &op, &a->error_orig); |
| 395 | if (a->test_type == PTA_MUTEX_TEST_WRITER && |
| 396 | op.params[1].value.b != 1) { |
| 397 | Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b); |
| 398 | a->res = TEEC_ERROR_BAD_STATE; |
| 399 | a->error_orig = 42; |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | if (a->test_type == PTA_MUTEX_TEST_READER) { |
| 404 | if (op.params[1].value.a > a->max_before_lockers) |
| 405 | a->max_before_lockers = op.params[1].value.a; |
| 406 | |
| 407 | if (op.params[1].value.b > a->max_during_lockers) |
| 408 | a->max_during_lockers = op.params[1].value.b; |
| 409 | |
| 410 | a->before_lockers += op.params[1].value.a; |
| 411 | a->during_lockers += op.params[1].value.b; |
| 412 | } |
| 413 | } |
| 414 | TEEC_CloseSession(&session); |
| 415 | |
| 416 | return NULL; |
| 417 | } |
| 418 | |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 419 | #define TEST_1003_THREAD_COUNT (3 * 2) |
| 420 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 421 | static void xtest_tee_test_1003(ADBG_Case_t *c) |
| 422 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 423 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 424 | TEEC_Session session = { }; |
| 425 | uint32_t ret_orig = 0; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 426 | size_t repeat = 20; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 427 | struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { }; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 428 | size_t max_read_concurrency = 0; |
| 429 | size_t max_read_waiters = 0; |
| 430 | size_t num_concurrent_read_lockers = 0; |
| 431 | size_t num_concurrent_read_waiters = 0; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 432 | size_t n = 0; |
| 433 | size_t nt = TEST_1003_THREAD_COUNT; |
| 434 | double mean_read_concurrency = 0; |
| 435 | double mean_read_waiters = 0; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 436 | size_t num_writers = 0; |
| 437 | size_t num_readers = 0; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 438 | pthread_t thr[TEST_1003_THREAD_COUNT] = { }; |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 439 | |
| 440 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
| 441 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 442 | &ret_orig); |
| 443 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 444 | Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found"); |
| 445 | return; |
| 446 | } |
| 447 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 448 | TEEC_CloseSession(&session); |
| 449 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 450 | for (n = 0; n < nt; n++) { |
| 451 | if (n % 3) { |
| 452 | arg[n].test_type = PTA_MUTEX_TEST_READER; |
| 453 | num_readers++; |
| 454 | } else { |
| 455 | arg[n].test_type = PTA_MUTEX_TEST_WRITER; |
| 456 | num_writers++; |
| 457 | } |
| 458 | arg[n].repeat = repeat; |
| 459 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 460 | test_1003_thread, arg + n))) |
| 461 | nt = n; /* break loop and start cleanup */ |
| 462 | } |
| 463 | |
| 464 | for (n = 0; n < nt; n++) { |
| 465 | ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)); |
| 466 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res)) |
| 467 | Do_ADBG_Log("error origin %" PRIu32, |
| 468 | arg[n].error_orig); |
| 469 | if (arg[n].test_type == PTA_MUTEX_TEST_READER) { |
| 470 | if (arg[n].max_during_lockers > max_read_concurrency) |
| 471 | max_read_concurrency = |
| 472 | arg[n].max_during_lockers; |
| 473 | |
| 474 | if (arg[n].max_before_lockers > max_read_waiters) |
| 475 | max_read_waiters = arg[n].max_before_lockers; |
| 476 | |
| 477 | num_concurrent_read_lockers += arg[n].during_lockers; |
| 478 | num_concurrent_read_waiters += arg[n].before_lockers; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | mean_read_concurrency = (double)num_concurrent_read_lockers / |
| 483 | (double)(repeat * num_readers); |
| 484 | mean_read_waiters = (double)num_concurrent_read_waiters / |
| 485 | (double)(repeat * num_readers); |
| 486 | |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 487 | Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)", |
| 488 | TEST_1003_THREAD_COUNT, num_writers, num_readers); |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 489 | Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency); |
| 490 | Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters); |
| 491 | Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency); |
| 492 | Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters); |
| 493 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 494 | ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003, |
| 495 | "Core internal read/write mutex"); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 496 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 497 | static void xtest_tee_test_1004(ADBG_Case_t *c) |
| 498 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 499 | TEEC_Session session = { }; |
| 500 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 501 | struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256, |
| 502 | TA_CRYPT_CMD_AES256ECB_ENC, |
| 503 | TA_CRYPT_CMD_AES256ECB_DEC }; |
| 504 | |
| 505 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 506 | &session, &crypt_user_ta_uuid, |
| 507 | NULL, &ret_orig))) |
| 508 | return; |
| 509 | |
| 510 | /* Run the "complete crypto test suite" */ |
| 511 | xtest_crypto_test(&cs); |
| 512 | |
| 513 | TEEC_CloseSession(&session); |
| 514 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 515 | ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 516 | |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 517 | static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 518 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 519 | TEEC_Session session = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 520 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 521 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 522 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 523 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 524 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 525 | &ret_orig))) |
| 526 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 527 | |
| 528 | op.params[0].value.a = n; |
| 529 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, |
| 530 | TEEC_NONE); |
| 531 | |
| 532 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 533 | TEEC_ERROR_TARGET_DEAD, |
| 534 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
| 535 | &ret_orig)); |
| 536 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 537 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 538 | TEEC_ERROR_TARGET_DEAD, |
| 539 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 540 | &ret_orig)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 541 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 542 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 543 | |
| 544 | TEEC_CloseSession(&session); |
| 545 | } |
| 546 | |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 547 | static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n, |
| 548 | size_t size) |
| 549 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 550 | TEEC_Session session = { }; |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 551 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 552 | uint32_t ret_orig = 0; |
| 553 | TEEC_SharedMemory shm = { }; |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 554 | |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 555 | shm.size = size; |
| 556 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 557 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 558 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm))) |
| 559 | return; |
| 560 | |
| 561 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 562 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 563 | &ret_orig))) |
Jens Wiklander | 6987a82 | 2019-06-28 12:47:42 +0200 | [diff] [blame] | 564 | goto rel_shm; |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 565 | |
| 566 | op.params[0].value.a = (uint32_t)n; |
| 567 | op.params[1].memref.parent = &shm; |
| 568 | op.params[1].memref.size = size; |
| 569 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE, |
| 570 | TEEC_NONE, TEEC_NONE); |
| 571 | |
| 572 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 573 | TEEC_ERROR_TARGET_DEAD, |
| 574 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
| 575 | &ret_orig)); |
| 576 | |
| 577 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 578 | TEEC_ERROR_TARGET_DEAD, |
| 579 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
| 580 | &ret_orig)); |
| 581 | |
| 582 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 583 | |
| 584 | TEEC_CloseSession(&session); |
Jens Wiklander | 6987a82 | 2019-06-28 12:47:42 +0200 | [diff] [blame] | 585 | rel_shm: |
| 586 | TEEC_ReleaseSharedMemory(&shm); |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 589 | static void xtest_tee_test_1005(ADBG_Case_t *c) |
| 590 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 591 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 592 | #define MAX_SESSIONS 3 |
| 593 | TEEC_Session sessions[MAX_SESSIONS]; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 594 | int i = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 595 | |
| 596 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 597 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Jens Wiklander | eb6bce7 | 2016-09-23 11:37:33 +0200 | [diff] [blame] | 598 | xtest_teec_open_session(&sessions[i], |
| 599 | &concurrent_ta_uuid, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 600 | NULL, &ret_orig))) |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | for (; --i >= 0; ) |
| 605 | TEEC_CloseSession(&sessions[i]); |
| 606 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 607 | ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 608 | |
| 609 | static void xtest_tee_test_1006(ADBG_Case_t *c) |
| 610 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 611 | TEEC_Session session = { }; |
| 612 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 613 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 614 | uint8_t buf[32] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 615 | |
| 616 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 617 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 618 | &ret_orig))) |
| 619 | return; |
| 620 | |
| 621 | op.params[0].tmpref.buffer = buf; |
| 622 | op.params[0].tmpref.size = sizeof(buf); |
| 623 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, |
| 624 | TEEC_NONE, TEEC_NONE); |
| 625 | |
| 626 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 627 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op, |
| 628 | &ret_orig)); |
| 629 | |
| 630 | TEEC_CloseSession(&session); |
| 631 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 632 | ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006, |
| 633 | "Test Basic OS features"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 634 | |
| 635 | static void xtest_tee_test_1007(ADBG_Case_t *c) |
| 636 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 637 | TEEC_Session session = { }; |
| 638 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 639 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 640 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 641 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 642 | &ret_orig))) |
| 643 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 644 | |
| 645 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 646 | TEEC_ERROR_TARGET_DEAD, |
| 647 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL, |
| 648 | &ret_orig)); |
| 649 | |
| 650 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 651 | |
| 652 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 653 | TEEC_ERROR_TARGET_DEAD, |
| 654 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL, |
| 655 | &ret_orig)); |
| 656 | |
| 657 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 658 | |
| 659 | TEEC_CloseSession(&session); |
| 660 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 661 | ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 662 | |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 663 | #ifndef TA_DIR |
Victor Chong | 3d8798f | 2017-03-01 18:31:48 +0000 | [diff] [blame] | 664 | # ifdef __ANDROID__ |
Yongqin Liu | 286f1fc | 2018-06-21 22:09:15 +0800 | [diff] [blame] | 665 | #define TA_DIR "/vendor/lib/optee_armtz" |
Victor Chong | 3d8798f | 2017-03-01 18:31:48 +0000 | [diff] [blame] | 666 | # else |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 667 | #define TA_DIR "/lib/optee_armtz" |
Victor Chong | 3d8798f | 2017-03-01 18:31:48 +0000 | [diff] [blame] | 668 | # endif |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 669 | #endif |
| 670 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 671 | static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode) |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 672 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 673 | char buf[PATH_MAX] = { }; |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 674 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 675 | snprintf(buf, sizeof(buf), |
Jens Wiklander | 6203b87 | 2016-12-08 19:18:29 +0100 | [diff] [blame] | 676 | "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta", |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 677 | TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion, |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 678 | uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1], |
| 679 | uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3], |
| 680 | uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5], |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 681 | uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]); |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 682 | |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 683 | return fopen(buf, mode); |
| 684 | } |
| 685 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 686 | static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask) |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 687 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 688 | TEEC_Session session = { }; |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 689 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 690 | TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 691 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 692 | uint32_t ret_orig = 0; |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 693 | FILE *f = NULL; |
| 694 | bool r = false; |
| 695 | uint8_t *buf = NULL; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 696 | size_t sz = 0; |
| 697 | size_t fread_res = 0; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 698 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 699 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 700 | xtest_teec_open_session(&session, &uuid, NULL, &ret_orig))) |
| 701 | goto out; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 702 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 703 | f = open_ta_file(&create_fail_test_ta_uuid, "rb"); |
| 704 | if (!ADBG_EXPECT_NOT_NULL(c, f)) |
| 705 | goto out; |
| 706 | if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END))) |
| 707 | goto out; |
| 708 | sz = ftell(f); |
| 709 | rewind(f); |
| 710 | |
| 711 | buf = malloc(sz); |
| 712 | if (!ADBG_EXPECT_NOT_NULL(c, buf)) |
| 713 | goto out; |
| 714 | |
| 715 | fread_res = fread(buf, 1, sz, f); |
| 716 | if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz)) |
| 717 | goto out; |
| 718 | |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 719 | fclose(f); |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 720 | f = NULL; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 721 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 722 | buf[MIN(offs, sz)] ^= mask; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 723 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 724 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, |
| 725 | TEEC_NONE, TEEC_NONE); |
| 726 | op.params[0].tmpref.buffer = buf; |
| 727 | op.params[0].tmpref.size = sz; |
| 728 | |
| 729 | res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op, |
| 730 | &ret_orig); |
| 731 | r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res); |
| 732 | out: |
| 733 | free(buf); |
| 734 | if (f) |
| 735 | fclose(f); |
| 736 | TEEC_CloseSession(&session); |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 737 | return r; |
| 738 | } |
Jens Wiklander | e1a201d | 2020-02-12 18:36:20 +0100 | [diff] [blame] | 739 | |
| 740 | static void test_1008_corrupt_ta(ADBG_Case_t *c) |
| 741 | { |
| 742 | TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID; |
| 743 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 744 | TEEC_Session session = { }; |
| 745 | uint32_t ret_orig = 0; |
| 746 | |
| 747 | res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig); |
| 748 | if (res) { |
| 749 | if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND, |
| 750 | res)) |
Joakim Bech | a1212b6 | 2020-04-07 12:06:00 +0200 | [diff] [blame] | 751 | Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test"); |
Jens Wiklander | e1a201d | 2020-02-12 18:36:20 +0100 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | TEEC_CloseSession(&session); |
| 755 | |
| 756 | ADBG_EXPECT_TRUE(c, |
| 757 | load_corrupt_ta(c, offsetof(struct shdr, magic), 1)); |
| 758 | ADBG_EXPECT_TRUE(c, |
| 759 | load_corrupt_ta(c, offsetof(struct shdr, img_type), 1)); |
| 760 | ADBG_EXPECT_TRUE(c, |
| 761 | load_corrupt_ta(c, offsetof(struct shdr, img_size), 1)); |
| 762 | ADBG_EXPECT_TRUE(c, |
| 763 | load_corrupt_ta(c, offsetof(struct shdr, algo), 1)); |
| 764 | ADBG_EXPECT_TRUE(c, |
| 765 | load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1)); |
| 766 | ADBG_EXPECT_TRUE(c, |
| 767 | load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1)); |
| 768 | ADBG_EXPECT_TRUE(c, |
| 769 | load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */ |
| 770 | ADBG_EXPECT_TRUE(c, |
| 771 | load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */ |
| 772 | ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */ |
| 773 | ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */ |
| 774 | } |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 775 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 776 | static void xtest_tee_test_1008(ADBG_Case_t *c) |
| 777 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 778 | TEEC_Session session = { }; |
| 779 | TEEC_Session session_crypt = { }; |
| 780 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 781 | |
| 782 | Do_ADBG_BeginSubCase(c, "Invoke command"); |
| 783 | { |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 784 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 785 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 786 | NULL, &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 787 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 788 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 789 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT, |
| 790 | NULL, &ret_orig)); |
| 791 | TEEC_CloseSession(&session); |
| 792 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 793 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 794 | } |
| 795 | Do_ADBG_EndSubCase(c, "Invoke command"); |
| 796 | |
| 797 | Do_ADBG_BeginSubCase(c, "Invoke command with timeout"); |
| 798 | { |
| 799 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 800 | |
| 801 | op.params[0].value.a = 2000; |
| 802 | op.paramTypes = TEEC_PARAM_TYPES( |
| 803 | TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 804 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 805 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 806 | xtest_teec_open_session(&session, |
| 807 | &os_test_ta_uuid, |
| 808 | NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 809 | &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 810 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 811 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 812 | TEEC_InvokeCommand(&session, |
| 813 | TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT, |
| 814 | &op, &ret_orig)); |
| 815 | TEEC_CloseSession(&session); |
| 816 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 817 | } |
| 818 | Do_ADBG_EndSubCase(c, "Invoke command with timeout"); |
| 819 | |
| 820 | Do_ADBG_BeginSubCase(c, "Create session fail"); |
| 821 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 822 | size_t n = 0; |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 823 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 824 | for (n = 0; n < 100; n++) { |
| 825 | Do_ADBG_Log("n = %zu", n); |
| 826 | (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC, |
| 827 | xtest_teec_open_session(&session_crypt, |
| 828 | &create_fail_test_ta_uuid, |
| 829 | NULL, &ret_orig)); |
Jerome Forissier | ec7a31f | 2020-11-20 11:30:06 +0100 | [diff] [blame] | 830 | /* level > 0 may be used to detect/debug memory leaks */ |
| 831 | if (!level) |
| 832 | break; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | Do_ADBG_EndSubCase(c, "Create session fail"); |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 836 | |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 837 | Do_ADBG_BeginSubCase(c, "Load corrupt TA"); |
Jens Wiklander | e1a201d | 2020-02-12 18:36:20 +0100 | [diff] [blame] | 838 | test_1008_corrupt_ta(c); |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 839 | Do_ADBG_EndSubCase(c, "Load corrupt TA"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 840 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 841 | ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008, |
| 842 | "TEE internal client API"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 843 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 844 | static void *cancellation_thread(void *arg) |
| 845 | { |
| 846 | /* |
| 847 | * Sleep 0.5 seconds before cancellation to make sure that the other |
| 848 | * thread is in RPC_WAIT. |
| 849 | */ |
| 850 | (void)usleep(500000); |
| 851 | TEEC_RequestCancellation(arg); |
| 852 | return NULL; |
| 853 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 854 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 855 | static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase, |
| 856 | uint32_t timeout, bool cancel) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 857 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 858 | TEEC_Session session = { }; |
| 859 | uint32_t ret_orig = 0; |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 860 | pthread_t thr; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 861 | |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 862 | memset(&thr, 0, sizeof(thr)); |
| 863 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 864 | Do_ADBG_BeginSubCase(c, "%s", subcase); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 865 | { |
| 866 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 867 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 868 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
| 869 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 870 | NULL, &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 871 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 872 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, |
| 873 | TEEC_ORIGIN_TRUSTED_APP, |
| 874 | ret_orig); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 875 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 876 | op.params[0].value.a = timeout; |
| 877 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 878 | TEEC_NONE, |
| 879 | TEEC_NONE, TEEC_NONE); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 880 | if (cancel) { |
| 881 | (void)ADBG_EXPECT(c, 0, |
| 882 | pthread_create(&thr, NULL, |
| 883 | cancellation_thread, &op)); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 884 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 885 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 886 | TEEC_ERROR_CANCEL, |
| 887 | TEEC_InvokeCommand(&session, |
| 888 | TA_OS_TEST_CMD_WAIT, |
| 889 | &op, |
| 890 | &ret_orig)); |
| 891 | } else |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 892 | |
| 893 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 894 | TEEC_InvokeCommand(&session, |
| 895 | TA_OS_TEST_CMD_WAIT, |
| 896 | &op, |
| 897 | &ret_orig)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 898 | if (cancel) |
| 899 | (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 900 | |
| 901 | TEEC_CloseSession(&session); |
| 902 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 903 | } |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 904 | Do_ADBG_EndSubCase(c, "%s", subcase); |
| 905 | } |
| 906 | |
| 907 | static void xtest_tee_test_1009(ADBG_Case_t *c) |
| 908 | { |
| 909 | xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false); |
| 910 | xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 911 | xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 912 | xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 913 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 914 | ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 915 | |
| 916 | static void xtest_tee_test_1010(ADBG_Case_t *c) |
| 917 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 918 | unsigned int n = 0; |
| 919 | unsigned int idx = 0; |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 920 | size_t memref_sz[] = { 1024, 65536 }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 921 | |
Jerome Forissier | e4c33f4 | 2021-09-21 11:26:17 +0200 | [diff] [blame] | 922 | for (n = 1; n <= 7; n++) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 923 | Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n); |
| 924 | xtest_tee_test_invalid_mem_access(c, n); |
| 925 | Do_ADBG_EndSubCase(c, "Invalid memory access %u", n); |
| 926 | } |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 927 | |
| 928 | for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) { |
| 929 | for (n = 1; n <= 5; n++) { |
| 930 | Do_ADBG_BeginSubCase(c, |
Igor Opaniuk | e8236ac | 2018-02-12 12:26:25 +0200 | [diff] [blame] | 931 | "Invalid memory access %u with %zu bytes memref", |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 932 | n, memref_sz[idx]); |
| 933 | xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]); |
| 934 | Do_ADBG_EndSubCase(c, |
Igor Opaniuk | e8236ac | 2018-02-12 12:26:25 +0200 | [diff] [blame] | 935 | "Invalid memory access %u with %zu bytes memref", |
Etienne Carriere | 92c3442 | 2018-02-09 13:11:40 +0100 | [diff] [blame] | 936 | n, memref_sz[idx]); |
| 937 | } |
| 938 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 939 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 940 | ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010, |
| 941 | "Invalid memory access"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 942 | |
| 943 | static void xtest_tee_test_1011(ADBG_Case_t *c) |
| 944 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 945 | TEEC_Session session = { }; |
| 946 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 947 | struct xtest_crypto_session cs = { |
| 948 | c, &session, TA_RPC_CMD_CRYPT_SHA256, |
| 949 | TA_RPC_CMD_CRYPT_AES256ECB_ENC, |
| 950 | TA_RPC_CMD_CRYPT_AES256ECB_DEC |
| 951 | }; |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 952 | struct xtest_crypto_session cs_privmem = { |
| 953 | c, &session, |
| 954 | TA_RPC_CMD_CRYPT_PRIVMEM_SHA256, |
| 955 | TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC, |
| 956 | TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC |
| 957 | }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 958 | TEEC_UUID uuid = rpc_test_ta_uuid; |
| 959 | |
| 960 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 961 | xtest_teec_open_session(&session, &uuid, NULL, &ret_orig))) |
| 962 | return; |
| 963 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 964 | Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 965 | /* |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 966 | * Run the "complete crypto test suite" using TA-to-TA |
| 967 | * communication |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 968 | */ |
| 969 | xtest_crypto_test(&cs); |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 970 | Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory"); |
| 971 | |
| 972 | Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory"); |
| 973 | /* |
| 974 | * Run the "complete crypto test suite" using TA-to-TA |
| 975 | * communication via TA private memory. |
| 976 | */ |
| 977 | xtest_crypto_test(&cs_privmem); |
| 978 | Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory"); |
| 979 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 980 | TEEC_CloseSession(&session); |
| 981 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 982 | ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011, |
| 983 | "Test TA-to-TA features with User Crypt TA"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * Note that this test is failing when |
| 987 | * - running twice in a raw |
| 988 | * - and the user TA is statically linked |
| 989 | * This is because the counter is not reseted when opening the first session |
| 990 | * in case the TA is statically linked |
| 991 | */ |
| 992 | static void xtest_tee_test_1012(ADBG_Case_t *c) |
| 993 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 994 | TEEC_Session session1 = { }; |
| 995 | TEEC_Session session2 = { }; |
| 996 | uint32_t ret_orig = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 997 | TEEC_UUID uuid = sims_test_ta_uuid; |
| 998 | |
| 999 | Do_ADBG_BeginSubCase(c, "Single Instance Multi Session"); |
| 1000 | { |
| 1001 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1002 | static const uint8_t in[] = { |
| 1003 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 1004 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 1005 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 1006 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 1007 | }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1008 | uint8_t out[32] = { }; |
| 1009 | int i = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1010 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 1011 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1012 | xtest_teec_open_session(&session1, &uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 1013 | &ret_orig))) |
| 1014 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1015 | |
| 1016 | op.params[0].value.a = 0; |
| 1017 | op.params[1].tmpref.buffer = (void *)in; |
| 1018 | op.params[1].tmpref.size = sizeof(in); |
| 1019 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 1020 | TEEC_MEMREF_TEMP_INPUT, |
| 1021 | TEEC_NONE, TEEC_NONE); |
| 1022 | |
| 1023 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1024 | TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op, |
| 1025 | &ret_orig)); |
| 1026 | |
Jerome Forissier | 3cc0a42 | 2017-12-14 09:53:24 +0100 | [diff] [blame] | 1027 | for (i = 1; i < 3; i++) { |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 1028 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1029 | xtest_teec_open_session(&session2, &uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 1030 | &ret_orig))) |
| 1031 | continue; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1032 | |
| 1033 | op.params[0].value.a = 0; |
| 1034 | op.params[1].tmpref.buffer = out; |
| 1035 | op.params[1].tmpref.size = sizeof(out); |
| 1036 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 1037 | TEEC_MEMREF_TEMP_OUTPUT, |
| 1038 | TEEC_NONE, TEEC_NONE); |
| 1039 | |
| 1040 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1041 | TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ, |
| 1042 | &op, &ret_orig)); |
| 1043 | |
| 1044 | if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out, |
| 1045 | sizeof(out))) { |
| 1046 | Do_ADBG_Log("in:"); |
| 1047 | Do_ADBG_HexLog(in, sizeof(in), 16); |
| 1048 | Do_ADBG_Log("out:"); |
| 1049 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 1050 | } |
| 1051 | |
| 1052 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, |
| 1053 | TEEC_NONE, TEEC_NONE, |
| 1054 | TEEC_NONE); |
| 1055 | |
| 1056 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1057 | TEEC_InvokeCommand(&session1, |
| 1058 | TA_SIMS_CMD_GET_COUNTER, |
| 1059 | &op, &ret_orig)); |
| 1060 | |
| 1061 | (void)ADBG_EXPECT(c, 0, op.params[0].value.a); |
| 1062 | |
| 1063 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1064 | TEEC_InvokeCommand(&session2, |
| 1065 | TA_SIMS_CMD_GET_COUNTER, &op, |
| 1066 | &ret_orig)); |
| 1067 | |
| 1068 | (void)ADBG_EXPECT(c, i, op.params[0].value.a); |
| 1069 | TEEC_CloseSession(&session2); |
| 1070 | } |
| 1071 | |
| 1072 | memset(out, 0, sizeof(out)); |
| 1073 | op.params[0].value.a = 0; |
| 1074 | op.params[1].tmpref.buffer = out; |
| 1075 | op.params[1].tmpref.size = sizeof(out); |
| 1076 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 1077 | TEEC_MEMREF_TEMP_OUTPUT, |
| 1078 | TEEC_NONE, TEEC_NONE); |
| 1079 | |
| 1080 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1081 | TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op, |
| 1082 | &ret_orig)); |
| 1083 | |
| 1084 | if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) { |
| 1085 | Do_ADBG_Log("in:"); |
| 1086 | Do_ADBG_HexLog(in, sizeof(in), 16); |
| 1087 | Do_ADBG_Log("out:"); |
| 1088 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 1089 | } |
| 1090 | |
| 1091 | TEEC_CloseSession(&session1); |
| 1092 | } |
Ovidiu Mihalachi | 3e6183e | 2019-04-25 10:23:21 +0300 | [diff] [blame] | 1093 | Do_ADBG_EndSubCase(c, "Single Instance Multi Session"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1094 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1095 | ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012, |
| 1096 | "Test Single Instance Multi Session features with SIMS TA"); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1097 | |
| 1098 | struct test_1013_thread_arg { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1099 | const TEEC_UUID *uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1100 | uint32_t cmd; |
| 1101 | uint32_t repeat; |
| 1102 | TEEC_SharedMemory *shm; |
| 1103 | uint32_t error_orig; |
| 1104 | TEEC_Result res; |
| 1105 | uint32_t max_concurrency; |
| 1106 | const uint8_t *in; |
| 1107 | size_t in_len; |
| 1108 | uint8_t *out; |
| 1109 | size_t out_len; |
| 1110 | }; |
| 1111 | |
| 1112 | static void *test_1013_thread(void *arg) |
| 1113 | { |
| 1114 | struct test_1013_thread_arg *a = arg; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1115 | TEEC_Session session = { }; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1116 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1117 | uint8_t p2 = TEEC_NONE; |
| 1118 | uint8_t p3 = TEEC_NONE; |
| 1119 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1120 | a->res = xtest_teec_open_session(&session, a->uuid, NULL, |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1121 | &a->error_orig); |
| 1122 | if (a->res != TEEC_SUCCESS) |
| 1123 | return NULL; |
| 1124 | |
| 1125 | op.params[0].memref.parent = a->shm; |
| 1126 | op.params[0].memref.size = a->shm->size; |
| 1127 | op.params[0].memref.offset = 0; |
| 1128 | op.params[1].value.a = a->repeat; |
| 1129 | op.params[1].value.b = 0; |
| 1130 | op.params[2].tmpref.buffer = (void *)a->in; |
| 1131 | op.params[2].tmpref.size = a->in_len; |
| 1132 | op.params[3].tmpref.buffer = a->out; |
| 1133 | op.params[3].tmpref.size = a->out_len; |
| 1134 | |
| 1135 | if (a->in_len) |
| 1136 | p2 = TEEC_MEMREF_TEMP_INPUT; |
| 1137 | if (a->out_len) |
| 1138 | p3 = TEEC_MEMREF_TEMP_OUTPUT; |
| 1139 | |
| 1140 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, |
| 1141 | TEEC_VALUE_INOUT, p2, p3); |
| 1142 | |
| 1143 | a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig); |
| 1144 | a->max_concurrency = op.params[1].value.b; |
| 1145 | a->out_len = op.params[3].tmpref.size; |
| 1146 | TEEC_CloseSession(&session); |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1150 | #define NUM_THREADS 3 |
| 1151 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1152 | static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency, |
| 1153 | const TEEC_UUID *uuid) |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1154 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1155 | size_t nt = 0; |
| 1156 | size_t n = 0; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1157 | size_t repeat = 1000; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1158 | TEEC_SharedMemory shm = { }; |
| 1159 | size_t max_concurrency = 0; |
| 1160 | struct test_1013_thread_arg arg[NUM_THREADS] = { }; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1161 | static const uint8_t sha256_in[] = { 'a', 'b', 'c' }; |
| 1162 | static const uint8_t sha256_out[] = { |
| 1163 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
| 1164 | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 1165 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 1166 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 1167 | }; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1168 | uint8_t out[32] = { }; |
| 1169 | pthread_t thr[NUM_THREADS] = { }; |
Jerome Forissier | fbdc175 | 2021-06-29 11:25:35 +0200 | [diff] [blame] | 1170 | bool skip = false; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1171 | |
Etienne Carriere | c0a6172 | 2023-03-07 17:47:42 +0100 | [diff] [blame] | 1172 | /* Decrease number of loops when pager has a small page pool */ |
| 1173 | if (level == 0 && optee_pager_with_small_pool()) |
| 1174 | repeat = 250; |
| 1175 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1176 | Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1177 | *mean_concurrency = 0; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1178 | |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1179 | shm.size = sizeof(struct ta_concurrent_shm); |
| 1180 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1181 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1182 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm))) |
| 1183 | return; |
| 1184 | |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1185 | memset(shm.buffer, 0, shm.size); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1186 | max_concurrency = 0; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1187 | nt = NUM_THREADS; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1188 | |
| 1189 | for (n = 0; n < nt; n++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1190 | arg[n].uuid = uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1191 | arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1192 | arg[n].repeat = repeat * 10; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1193 | arg[n].shm = &shm; |
| 1194 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 1195 | test_1013_thread, arg + n))) |
| 1196 | nt = n; /* break loop and start cleanup */ |
| 1197 | } |
| 1198 | |
| 1199 | for (n = 0; n < nt; n++) { |
| 1200 | ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)); |
Jerome Forissier | fbdc175 | 2021-06-29 11:25:35 +0200 | [diff] [blame] | 1201 | if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY && |
| 1202 | !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) { |
| 1203 | Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored"); |
| 1204 | skip = true; |
| 1205 | continue; |
| 1206 | } |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1207 | ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res); |
| 1208 | if (arg[n].max_concurrency > max_concurrency) |
| 1209 | max_concurrency = arg[n].max_concurrency; |
| 1210 | } |
| 1211 | |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1212 | /* |
| 1213 | * Concurrency can be limited by several factors, for instance in a |
Joakim Bech | a1212b6 | 2020-04-07 12:06:00 +0200 | [diff] [blame] | 1214 | * single CPU system it's dependent on the Preemption Model used by |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1215 | * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the |
| 1216 | * best result there). |
| 1217 | */ |
Jerome Forissier | fbdc175 | 2021-06-29 11:25:35 +0200 | [diff] [blame] | 1218 | if (!skip) { |
| 1219 | (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0); |
| 1220 | (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, |
| 1221 | NUM_THREADS); |
| 1222 | *mean_concurrency += max_concurrency; |
| 1223 | } |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1224 | Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1225 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1226 | Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1227 | memset(shm.buffer, 0, shm.size); |
| 1228 | memset(arg, 0, sizeof(arg)); |
| 1229 | max_concurrency = 0; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1230 | nt = NUM_THREADS; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1231 | |
| 1232 | for (n = 0; n < nt; n++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1233 | arg[n].uuid = uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1234 | arg[n].cmd = TA_CONCURRENT_CMD_SHA256; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1235 | arg[n].repeat = repeat; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1236 | arg[n].shm = &shm; |
| 1237 | arg[n].in = sha256_in; |
| 1238 | arg[n].in_len = sizeof(sha256_in); |
| 1239 | arg[n].out = out; |
| 1240 | arg[n].out_len = sizeof(out); |
| 1241 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 1242 | test_1013_thread, arg + n))) |
| 1243 | nt = n; /* break loop and start cleanup */ |
| 1244 | } |
| 1245 | |
| 1246 | for (n = 0; n < nt; n++) { |
Jerome Forissier | fbdc175 | 2021-06-29 11:25:35 +0200 | [diff] [blame] | 1247 | ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)); |
| 1248 | if (arg[n].res == TEEC_ERROR_OUT_OF_MEMORY && |
| 1249 | !memcmp(uuid, &concurrent_large_ta_uuid, sizeof(*uuid))) { |
| 1250 | Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored"); |
| 1251 | continue; |
| 1252 | } |
| 1253 | if (ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res)) |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1254 | ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out), |
| 1255 | arg[n].out, arg[n].out_len); |
| 1256 | if (arg[n].max_concurrency > max_concurrency) |
| 1257 | max_concurrency = arg[n].max_concurrency; |
| 1258 | } |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1259 | *mean_concurrency += max_concurrency; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1260 | Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1261 | |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1262 | *mean_concurrency /= 2.0; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1263 | TEEC_ReleaseSharedMemory(&shm); |
| 1264 | } |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1265 | |
| 1266 | static void xtest_tee_test_1013(ADBG_Case_t *c) |
| 1267 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1268 | int i = 0; |
| 1269 | double mean_concurrency = 0; |
| 1270 | double concurrency = 0; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1271 | int nb_loops = 24; |
Jens Wiklander | 6a9d15a | 2016-02-01 10:29:42 +0100 | [diff] [blame] | 1272 | |
| 1273 | if (level == 0) |
| 1274 | nb_loops /= 2; |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1275 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1276 | Do_ADBG_BeginSubCase(c, "Using small concurrency TA"); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1277 | mean_concurrency = 0; |
| 1278 | for (i = 0; i < nb_loops; i++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1279 | xtest_tee_test_1013_single(c, &concurrency, |
| 1280 | &concurrent_ta_uuid); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1281 | mean_concurrency += concurrency; |
| 1282 | } |
| 1283 | mean_concurrency /= nb_loops; |
| 1284 | |
| 1285 | Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS); |
| 1286 | Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency); |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1287 | Do_ADBG_EndSubCase(c, "Using small concurrency TA"); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1288 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1289 | Do_ADBG_BeginSubCase(c, "Using large concurrency TA"); |
| 1290 | mean_concurrency = 0; |
| 1291 | for (i = 0; i < nb_loops; i++) { |
| 1292 | xtest_tee_test_1013_single(c, &concurrency, |
| 1293 | &concurrent_large_ta_uuid); |
| 1294 | mean_concurrency += concurrency; |
| 1295 | } |
| 1296 | mean_concurrency /= nb_loops; |
| 1297 | |
| 1298 | Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS); |
| 1299 | Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency); |
| 1300 | Do_ADBG_EndSubCase(c, "Using large concurrency TA"); |
| 1301 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1302 | ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013, |
Joakim Bech | a1212b6 | 2020-04-07 12:06:00 +0200 | [diff] [blame] | 1303 | "Test concurrency with concurrent TA"); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1304 | |
| 1305 | #ifdef CFG_SECURE_DATA_PATH |
| 1306 | static void xtest_tee_test_1014(ADBG_Case_t *c) |
| 1307 | { |
| 1308 | UNUSED(c); |
| 1309 | |
| 1310 | int size = 17000; |
| 1311 | int loop = 10; |
Olivier Masse | da5282a | 2022-04-07 11:24:08 +0200 | [diff] [blame] | 1312 | const char *heap_name = DEFAULT_HEAP_NAME; |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1313 | int rnd_offset = 1; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1314 | int test = 0; |
| 1315 | int ret = 0; |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1316 | |
| 1317 | test = TEST_NS_TO_TA; |
| 1318 | Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA"); |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 1319 | ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1320 | ADBG_EXPECT(c, 0, ret); |
| 1321 | Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA"); |
| 1322 | |
| 1323 | test = TEST_TA_TO_TA; |
| 1324 | Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA"); |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 1325 | ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1326 | ADBG_EXPECT(c, 0, ret); |
| 1327 | Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA"); |
| 1328 | |
| 1329 | test = TEST_TA_TO_PTA; |
Igor Opaniuk | 2e0a679 | 2021-01-09 00:51:19 +0200 | [diff] [blame] | 1330 | Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)"); |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 1331 | ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1332 | ADBG_EXPECT(c, 0, ret); |
Igor Opaniuk | 2e0a679 | 2021-01-09 00:51:19 +0200 | [diff] [blame] | 1333 | Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)"); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1334 | |
| 1335 | test = TEST_NS_TO_PTA; |
Igor Opaniuk | 2e0a679 | 2021-01-09 00:51:19 +0200 | [diff] [blame] | 1336 | Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)"); |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 1337 | ret = sdp_basic_test(test, size, loop, heap_name, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1338 | ADBG_EXPECT(c, 1, ret); |
Igor Opaniuk | 2e0a679 | 2021-01-09 00:51:19 +0200 | [diff] [blame] | 1339 | Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)"); |
Etienne Carriere | d9be3dc | 2018-04-25 18:30:19 +0200 | [diff] [blame] | 1340 | |
| 1341 | Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref"); |
Jerome Forissier | 03aa279 | 2023-02-07 14:22:24 +0100 | [diff] [blame] | 1342 | ret = sdp_out_of_bounds_memref_test(size, heap_name, 0); |
Etienne Carriere | d9be3dc | 2018-04-25 18:30:19 +0200 | [diff] [blame] | 1343 | ADBG_EXPECT(c, 0, ret); |
| 1344 | Do_ADBG_EndSubCase(c, NULL); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1345 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1346 | ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014, |
| 1347 | "Test secure data path against SDP TAs and pTAs"); |
| 1348 | #endif /*CFG_SECURE_DATA_PATH*/ |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1349 | |
Jerome Forissier | f425994 | 2022-01-11 14:27:05 +0100 | [diff] [blame] | 1350 | #ifdef CFG_REE_FS |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1351 | static void xtest_tee_test_1015(ADBG_Case_t *c) |
| 1352 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1353 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 1354 | TEEC_Session session = { }; |
| 1355 | uint32_t ret_orig = 0; |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1356 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1357 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1358 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 1359 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1360 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 1361 | Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found"); |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1362 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1363 | } |
| 1364 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1365 | |
| 1366 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1367 | TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE, |
| 1368 | NULL, &ret_orig)); |
| 1369 | TEEC_CloseSession(&session); |
| 1370 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1371 | ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015, |
| 1372 | "FS hash-tree corner cases"); |
Jerome Forissier | f425994 | 2022-01-11 14:27:05 +0100 | [diff] [blame] | 1373 | #endif /*CFG_REE_FS*/ |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 1374 | |
| 1375 | static void xtest_tee_test_1016(ADBG_Case_t *c) |
| 1376 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1377 | TEEC_Session session = { }; |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 1378 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1379 | uint32_t ret_orig = 0; |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 1380 | |
| 1381 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1382 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 1383 | &ret_orig))) |
| 1384 | return; |
| 1385 | |
| 1386 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 1387 | TEEC_NONE); |
| 1388 | |
| 1389 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1390 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op, |
| 1391 | &ret_orig)); |
| 1392 | |
| 1393 | TEEC_CloseSession(&session); |
| 1394 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1395 | ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016, |
| 1396 | "Test TA to TA transfers (in/out/inout memrefs on the stack)"); |
Jens Wiklander | 87e8170 | 2018-03-20 12:00:00 +0800 | [diff] [blame] | 1397 | |
| 1398 | static void xtest_tee_test_1017(ADBG_Case_t *c) |
| 1399 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1400 | TEEC_Session session = { }; |
Jens Wiklander | 87e8170 | 2018-03-20 12:00:00 +0800 | [diff] [blame] | 1401 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1402 | uint32_t ret_orig = 0; |
| 1403 | TEEC_SharedMemory shm = { }; |
Jens Wiklander | 87e8170 | 2018-03-20 12:00:00 +0800 | [diff] [blame] | 1404 | size_t page_size = 4096; |
| 1405 | |
Jens Wiklander | 87e8170 | 2018-03-20 12:00:00 +0800 | [diff] [blame] | 1406 | shm.size = 8 * page_size; |
| 1407 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1408 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1409 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm))) |
| 1410 | return; |
| 1411 | |
| 1412 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1413 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 1414 | &ret_orig))) |
| 1415 | goto out; |
| 1416 | |
| 1417 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, |
| 1418 | TEEC_MEMREF_PARTIAL_INPUT, |
| 1419 | TEEC_MEMREF_PARTIAL_OUTPUT, |
| 1420 | TEEC_MEMREF_PARTIAL_OUTPUT); |
| 1421 | |
| 1422 | /* |
| 1423 | * The first two memrefs are supposed to be combined into in |
| 1424 | * region and the last two memrefs should have one region each |
| 1425 | * when the parameters are mapped for the TA. |
| 1426 | */ |
| 1427 | op.params[0].memref.parent = &shm; |
| 1428 | op.params[0].memref.size = page_size; |
| 1429 | op.params[0].memref.offset = 0; |
| 1430 | |
| 1431 | op.params[1].memref.parent = &shm; |
| 1432 | op.params[1].memref.size = page_size; |
| 1433 | op.params[1].memref.offset = page_size; |
| 1434 | |
| 1435 | op.params[2].memref.parent = &shm; |
| 1436 | op.params[2].memref.size = page_size; |
| 1437 | op.params[2].memref.offset = 4 * page_size; |
| 1438 | |
| 1439 | op.params[3].memref.parent = &shm; |
| 1440 | op.params[3].memref.size = 2 * page_size; |
| 1441 | op.params[3].memref.offset = 6 * page_size; |
| 1442 | |
| 1443 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1444 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op, |
| 1445 | &ret_orig)); |
| 1446 | |
| 1447 | TEEC_CloseSession(&session); |
| 1448 | out: |
| 1449 | TEEC_ReleaseSharedMemory(&shm); |
| 1450 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1451 | ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017, |
| 1452 | "Test coalescing memrefs"); |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1453 | |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1454 | static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session, |
| 1455 | TEEC_SharedMemory *shm) |
| 1456 | { |
| 1457 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1458 | TEEC_Result ret = TEEC_ERROR_GENERIC; |
| 1459 | uint32_t ret_orig = 0; |
| 1460 | |
| 1461 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, |
| 1462 | TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 1463 | |
| 1464 | op.params[0].memref.parent = shm; |
| 1465 | op.params[0].memref.size = shm->size / 2; |
| 1466 | op.params[0].memref.offset = shm->size - (shm->size / 2) + 1; |
| 1467 | |
| 1468 | ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS, |
| 1469 | &op, &ret_orig); |
| 1470 | |
Etienne Carriere | 48fa1b2 | 2020-04-08 13:42:01 +0200 | [diff] [blame] | 1471 | ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP); |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1472 | if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) { |
| 1473 | ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret); |
| 1474 | ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret); |
| 1475 | } |
| 1476 | } |
| 1477 | |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1478 | static void xtest_tee_test_1018(ADBG_Case_t *c) |
| 1479 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1480 | TEEC_Session session = { }; |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1481 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1482 | uint32_t ret_orig = 0; |
| 1483 | TEEC_SharedMemory shm = { }; |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1484 | TEEC_Result ret = TEEC_ERROR_GENERIC; |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1485 | size_t page_size = 4096; |
Joakim Bech | a1212b6 | 2020-04-07 12:06:00 +0200 | [diff] [blame] | 1486 | /* Intentionally not 4kB aligned and odd */ |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1487 | uint8_t buffer[6001] = { }; |
| 1488 | |
| 1489 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1490 | xtest_teec_open_session(&session, |
| 1491 | &os_test_ta_uuid, |
| 1492 | NULL, |
| 1493 | &ret_orig))) |
| 1494 | return; |
| 1495 | |
Joakim Bech | a1212b6 | 2020-04-07 12:06:00 +0200 | [diff] [blame] | 1496 | Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm"); |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1497 | |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1498 | shm.size = 8 * page_size; |
| 1499 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1500 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1501 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, |
| 1502 | &shm))) |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1503 | goto out; |
| 1504 | |
| 1505 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, |
| 1506 | TEEC_MEMREF_PARTIAL_INPUT, |
| 1507 | TEEC_MEMREF_PARTIAL_OUTPUT, |
| 1508 | TEEC_MEMREF_PARTIAL_OUTPUT); |
| 1509 | |
| 1510 | /* |
| 1511 | * The first two memrefs are supposed to be combined into in |
| 1512 | * region and the last two memrefs should have one region each |
| 1513 | * when the parameters are mapped for the TA. |
| 1514 | */ |
| 1515 | op.params[0].memref.parent = &shm; |
| 1516 | op.params[0].memref.size = page_size; |
| 1517 | op.params[0].memref.offset = 0; |
| 1518 | |
| 1519 | op.params[1].memref.parent = &shm; |
| 1520 | op.params[1].memref.size = page_size; |
| 1521 | op.params[1].memref.offset = page_size; |
| 1522 | |
| 1523 | op.params[2].memref.parent = &shm; |
| 1524 | op.params[2].memref.size = page_size; |
| 1525 | op.params[2].memref.offset = 4 * page_size; |
| 1526 | |
| 1527 | op.params[3].memref.parent = &shm; |
| 1528 | op.params[3].memref.size = 3 * page_size; |
| 1529 | op.params[3].memref.offset = 6 * page_size; |
| 1530 | |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1531 | ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op, |
| 1532 | &ret_orig); |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1533 | |
Etienne Carriere | 48fa1b2 | 2020-04-08 13:42:01 +0200 | [diff] [blame] | 1534 | ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP); |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1535 | if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) { |
| 1536 | ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret); |
| 1537 | ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret); |
| 1538 | } |
| 1539 | |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1540 | TEEC_ReleaseSharedMemory(&shm); |
Etienne Carriere | 84382b3 | 2018-04-25 18:30:30 +0200 | [diff] [blame] | 1541 | Do_ADBG_EndSubCase(c, NULL); |
| 1542 | |
| 1543 | Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm"); |
| 1544 | |
| 1545 | memset(&shm, 0, sizeof(shm)); |
| 1546 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1547 | shm.buffer = buffer; |
| 1548 | shm.size = sizeof(buffer); |
| 1549 | |
| 1550 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1551 | TEEC_RegisterSharedMemory(&xtest_teec_ctx, |
| 1552 | &shm))) |
| 1553 | goto out; |
| 1554 | |
| 1555 | invoke_1byte_out_of_bounds(c, &session, &shm); |
| 1556 | |
| 1557 | TEEC_ReleaseSharedMemory(&shm); |
| 1558 | Do_ADBG_EndSubCase(c, NULL); |
| 1559 | |
| 1560 | Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm"); |
| 1561 | |
| 1562 | memset(&shm, 0, sizeof(shm)); |
| 1563 | shm.size = sizeof(buffer); |
| 1564 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1565 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1566 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, |
| 1567 | &shm))) |
| 1568 | goto out; |
| 1569 | |
| 1570 | invoke_1byte_out_of_bounds(c, &session, &shm); |
| 1571 | |
| 1572 | TEEC_ReleaseSharedMemory(&shm); |
| 1573 | Do_ADBG_EndSubCase(c, NULL); |
| 1574 | |
| 1575 | out: |
| 1576 | TEEC_CloseSession(&session); |
Jens Wiklander | bb10bcd | 2018-03-20 12:50:58 +0800 | [diff] [blame] | 1577 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1578 | ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018, |
| 1579 | "Test memref out of bounds"); |
Jerome Forissier | 53bde72 | 2018-05-31 09:14:54 +0200 | [diff] [blame] | 1580 | |
Jerome Forissier | 53bde72 | 2018-05-31 09:14:54 +0200 | [diff] [blame] | 1581 | static void xtest_tee_test_1019(ADBG_Case_t *c) |
| 1582 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1583 | TEEC_Session session = { }; |
| 1584 | uint32_t ret_orig = 0; |
Jerome Forissier | 53bde72 | 2018-05-31 09:14:54 +0200 | [diff] [blame] | 1585 | |
| 1586 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1587 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 1588 | &ret_orig))) |
| 1589 | return; |
| 1590 | |
| 1591 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1592 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL, |
| 1593 | &ret_orig)); |
| 1594 | |
| 1595 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 1596 | TEEC_ERROR_TARGET_DEAD, |
| 1597 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC, |
| 1598 | NULL, &ret_orig)); |
| 1599 | |
| 1600 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 1601 | |
| 1602 | TEEC_CloseSession(&session); |
| 1603 | } |
Jens Wiklander | 14f4887 | 2018-06-29 15:30:13 +0200 | [diff] [blame] | 1604 | ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019, |
| 1605 | "Test dynamically linked TA"); |
Jerome Forissier | 3357f87 | 2018-10-05 15:13:44 +0200 | [diff] [blame] | 1606 | |
| 1607 | static void xtest_tee_test_1020(ADBG_Case_t *c) |
| 1608 | { |
Etienne Carriere | e4ec9e4 | 2019-03-28 13:30:21 +0100 | [diff] [blame] | 1609 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 1610 | TEEC_Session session = { }; |
| 1611 | uint32_t ret_orig = 0; |
Jerome Forissier | 3357f87 | 2018-10-05 15:13:44 +0200 | [diff] [blame] | 1612 | |
| 1613 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
| 1614 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 1615 | &ret_orig); |
| 1616 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 1617 | Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found"); |
| 1618 | return; |
| 1619 | } |
| 1620 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 1621 | |
| 1622 | res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP, |
| 1623 | NULL, &ret_orig); |
| 1624 | if (res != TEEC_SUCCESS) { |
| 1625 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP, |
| 1626 | ret_orig); |
| 1627 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 1628 | Do_ADBG_Log(" - 1020 - skip test, feature not " |
| 1629 | "implemented"); |
| 1630 | goto out; |
| 1631 | } |
| 1632 | /* Error */ |
| 1633 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 1634 | } |
| 1635 | out: |
| 1636 | TEEC_CloseSession(&session); |
| 1637 | } |
| 1638 | ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020, |
| 1639 | "Test lockdep algorithm"); |
Ovidiu Mihalachi | 15cecff | 2019-04-02 16:36:31 +0300 | [diff] [blame] | 1640 | |
| 1641 | static TEEC_Result open_sec_session(TEEC_Session *session, |
| 1642 | const TEEC_UUID *uuid) |
| 1643 | { |
| 1644 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 1645 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1646 | uint32_t ret_orig = 0; |
| 1647 | |
| 1648 | op.params[0].tmpref.buffer = (void *)uuid; |
| 1649 | op.params[0].tmpref.size = sizeof(*uuid); |
| 1650 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 1651 | TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 1652 | |
| 1653 | res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION, |
| 1654 | &op, &ret_orig); |
| 1655 | if (res != TEEC_SUCCESS) |
| 1656 | return TEEC_ERROR_GENERIC; |
| 1657 | |
| 1658 | return res; |
| 1659 | } |
| 1660 | |
| 1661 | static TEEC_Result sims_get_counter(TEEC_Session *session, |
| 1662 | uint32_t *counter) |
| 1663 | { |
| 1664 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 1665 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1666 | uint32_t ret_orig = 0; |
| 1667 | |
| 1668 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, |
| 1669 | TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 1670 | |
| 1671 | res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER, |
| 1672 | &op, &ret_orig); |
| 1673 | if (res == TEEC_SUCCESS) |
| 1674 | *counter = op.params[0].value.a; |
| 1675 | |
| 1676 | return res; |
| 1677 | } |
| 1678 | |
| 1679 | static TEEC_Result trigger_panic(TEEC_Session *session, |
| 1680 | const TEEC_UUID *uuid) |
| 1681 | { |
| 1682 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1683 | uint32_t ret_orig = 0; |
| 1684 | |
| 1685 | if (!uuid) { |
| 1686 | op.params[0].tmpref.buffer = NULL; |
| 1687 | op.params[0].tmpref.size = 0; |
| 1688 | } else { |
| 1689 | op.params[0].tmpref.buffer = (void *)uuid; |
| 1690 | op.params[0].tmpref.size = sizeof(*uuid); |
| 1691 | } |
| 1692 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 1693 | TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 1694 | |
| 1695 | return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC, |
| 1696 | &op, &ret_orig); |
| 1697 | } |
| 1698 | |
| 1699 | static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid, |
| 1700 | bool multi_instance) |
| 1701 | { |
| 1702 | TEEC_Result exp_res = TEEC_ERROR_GENERIC; |
| 1703 | uint32_t counter = 0; |
| 1704 | uint32_t ret_orig = 0; |
| 1705 | uint32_t exp_counter = 0; |
| 1706 | TEEC_Session cs[3] = { }; |
| 1707 | |
| 1708 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1709 | xtest_teec_open_session(&cs[0], uuid, NULL, |
| 1710 | &ret_orig))) |
| 1711 | return; |
| 1712 | |
| 1713 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1714 | xtest_teec_open_session(&cs[1], uuid, NULL, |
| 1715 | &ret_orig))) |
| 1716 | goto bail0; |
| 1717 | |
| 1718 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter))) |
| 1719 | goto bail1; |
| 1720 | |
| 1721 | if (!ADBG_EXPECT(c, 0, counter)) |
| 1722 | goto bail1; |
| 1723 | |
| 1724 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter))) |
| 1725 | goto bail1; |
| 1726 | |
| 1727 | exp_counter = multi_instance ? 0 : 1; |
Ovidiu Mihalachi | 110aac4 | 2019-09-11 14:08:01 +0300 | [diff] [blame] | 1728 | if (!ADBG_EXPECT(c, exp_counter, counter)) |
Ovidiu Mihalachi | 15cecff | 2019-04-02 16:36:31 +0300 | [diff] [blame] | 1729 | goto bail1; |
| 1730 | |
| 1731 | if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 1732 | trigger_panic(&cs[1], NULL))) |
| 1733 | goto bail1; |
| 1734 | |
| 1735 | exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD; |
| 1736 | if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res, |
| 1737 | sims_get_counter(&cs[0], &counter))) |
| 1738 | goto bail1; |
| 1739 | |
| 1740 | if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 1741 | sims_get_counter(&cs[1], &counter))) |
| 1742 | goto bail1; |
| 1743 | |
| 1744 | /* Attempt to open a session on panicked context */ |
Ovidiu Mihalachi | 110aac4 | 2019-09-11 14:08:01 +0300 | [diff] [blame] | 1745 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Ovidiu Mihalachi | 15cecff | 2019-04-02 16:36:31 +0300 | [diff] [blame] | 1746 | xtest_teec_open_session(&cs[1], uuid, NULL, |
| 1747 | &ret_orig))) |
| 1748 | goto bail1; |
| 1749 | |
| 1750 | /* Sanity check of still valid TA context */ |
| 1751 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1752 | xtest_teec_open_session(&cs[2], uuid, NULL, |
| 1753 | &ret_orig))) |
| 1754 | goto bail1; |
| 1755 | |
| 1756 | /* Sanity check of still valid TA context */ |
| 1757 | if (multi_instance) { |
| 1758 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1759 | sims_get_counter(&cs[0], &counter))) |
| 1760 | goto bail2; |
| 1761 | |
| 1762 | if (!ADBG_EXPECT(c, 0, counter)) |
| 1763 | goto bail2; |
| 1764 | } |
| 1765 | |
| 1766 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter))) |
| 1767 | goto bail2; |
| 1768 | |
Ovidiu Mihalachi | 110aac4 | 2019-09-11 14:08:01 +0300 | [diff] [blame] | 1769 | exp_counter = multi_instance ? 0 : 1; |
| 1770 | if (!ADBG_EXPECT(c, exp_counter, counter)) |
Ovidiu Mihalachi | 15cecff | 2019-04-02 16:36:31 +0300 | [diff] [blame] | 1771 | goto bail2; |
| 1772 | |
| 1773 | bail2: |
| 1774 | TEEC_CloseSession(&cs[2]); |
| 1775 | bail1: |
| 1776 | TEEC_CloseSession(&cs[1]); |
| 1777 | bail0: |
| 1778 | TEEC_CloseSession(&cs[0]); |
| 1779 | } |
| 1780 | |
| 1781 | static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1, |
| 1782 | const TEEC_UUID *uuid2) |
| 1783 | { |
| 1784 | uint32_t ret_orig = 0; |
| 1785 | uint32_t counter = 0; |
| 1786 | TEEC_Session cs[3] = { }; |
| 1787 | |
| 1788 | /* Test pre-conditions */ |
| 1789 | /* 2.1 - CA opens a session toward TA1 */ |
| 1790 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1791 | xtest_teec_open_session(&cs[0], uuid1, NULL, |
| 1792 | &ret_orig))) |
| 1793 | return; |
| 1794 | |
| 1795 | /* 2.2 - CA opens a session toward TA2 */ |
| 1796 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1797 | xtest_teec_open_session(&cs[1], uuid2, NULL, |
| 1798 | &ret_orig))) |
| 1799 | goto bail0; |
| 1800 | |
| 1801 | /* 2.3 - TA1 opens a session toward TA2 */ |
| 1802 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2))) |
| 1803 | goto bail1; |
| 1804 | |
| 1805 | /* 2.4 - CA invokes TA2 which panics */ |
| 1806 | if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 1807 | trigger_panic(&cs[1], NULL))) |
| 1808 | goto bail1; |
| 1809 | |
| 1810 | /* Expected results */ |
| 1811 | /* 2.5 - Expect CA->TA1 session is still alive */ |
| 1812 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter))) |
| 1813 | goto bail1; |
| 1814 | |
| 1815 | /* 2.6 - Expect CA->TA2 session is properly released */ |
| 1816 | if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 1817 | sims_get_counter(&cs[1], &counter))) |
| 1818 | goto bail1; |
| 1819 | |
| 1820 | bail1: |
| 1821 | TEEC_CloseSession(&cs[1]); |
| 1822 | bail0: |
| 1823 | TEEC_CloseSession(&cs[0]); |
| 1824 | |
| 1825 | memset(cs, 0, sizeof(cs)); |
| 1826 | |
| 1827 | /* Test pre-conditions */ |
| 1828 | /* 2.1 - CA opens a session toward TA1 */ |
| 1829 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1830 | xtest_teec_open_session(&cs[0], uuid1, NULL, |
| 1831 | &ret_orig))) |
| 1832 | return; |
| 1833 | |
| 1834 | /* 2.2 - CA opens a session toward TA2 */ |
| 1835 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1836 | xtest_teec_open_session(&cs[1], uuid2, NULL, |
| 1837 | &ret_orig))) |
| 1838 | goto bail2; |
| 1839 | |
| 1840 | /* 2.3 - TA1 opens a session toward TA2 */ |
| 1841 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2))) |
| 1842 | goto bail3; |
| 1843 | |
| 1844 | /* 2.4 - CA invokes TA1 which invokes TA2 which panics */ |
| 1845 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2))) |
| 1846 | goto bail3; |
| 1847 | |
| 1848 | /* Expected results */ |
| 1849 | /* 2.5 - Expect CA->TA1 session is still alive */ |
| 1850 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter))) |
| 1851 | goto bail3; |
| 1852 | |
| 1853 | /* 2.6 - Expect CA->TA2 session is properly released */ |
| 1854 | if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 1855 | sims_get_counter(&cs[1], &counter))) |
| 1856 | goto bail3; |
| 1857 | |
| 1858 | bail3: |
| 1859 | TEEC_CloseSession(&cs[1]); |
| 1860 | bail2: |
| 1861 | TEEC_CloseSession(&cs[0]); |
| 1862 | } |
| 1863 | |
| 1864 | static void xtest_tee_test_1021(ADBG_Case_t *c) |
| 1865 | { |
| 1866 | Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session"); |
| 1867 | test_panic_ca_to_ta(c, &miss_test_ta_uuid, true); |
| 1868 | Do_ADBG_EndSubCase(c, "Multiple Instances Single Session"); |
| 1869 | |
| 1870 | Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions"); |
| 1871 | test_panic_ca_to_ta(c, &sims_test_ta_uuid, false); |
| 1872 | Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions"); |
| 1873 | |
| 1874 | Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive"); |
| 1875 | test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false); |
| 1876 | Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive"); |
| 1877 | |
| 1878 | Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA"); |
| 1879 | test_panic_ta_to_ta(c, &sims_test_ta_uuid, |
| 1880 | &sims_keepalive_test_ta_uuid); |
| 1881 | Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA"); |
| 1882 | } |
| 1883 | ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021, |
| 1884 | "Test panic context release"); |
Jerome Forissier | a9ab5d0 | 2019-03-17 21:14:06 +0100 | [diff] [blame] | 1885 | |
| 1886 | static void xtest_tee_test_1022(ADBG_Case_t *c) |
| 1887 | { |
| 1888 | TEEC_Session session = { 0 }; |
| 1889 | uint32_t ret_orig = 0; |
| 1890 | |
| 1891 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1892 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 1893 | NULL, &ret_orig))) |
| 1894 | return; |
| 1895 | |
| 1896 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1897 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL, |
| 1898 | &ret_orig)); |
| 1899 | |
| 1900 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 1901 | TEEC_ERROR_TARGET_DEAD, |
| 1902 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC, |
| 1903 | NULL, &ret_orig)); |
| 1904 | |
| 1905 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 1906 | |
| 1907 | TEEC_CloseSession(&session); |
| 1908 | } |
| 1909 | ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022, |
| 1910 | "Test dlopen()/dlsym()/dlclose() API"); |
Jerome Forissier | e9571e8 | 2020-02-18 15:26:20 +0100 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * Testing the ELF initialization (.init_array) |
| 1914 | * |
| 1915 | * - The TA has a global variable which can also be accessed by the two shared |
| 1916 | * libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via |
| 1917 | * dlopen()) |
| 1918 | * - The TA and both libraries have initialization functions (declared with the |
| 1919 | * "constructor" attribute) which perform the following: |
| 1920 | * * The TA multiplies by 10 then adds 1 |
| 1921 | * * os_test_lib multiplies by 10 then adds 2 |
| 1922 | * * os_test_lib_dl multiplies by 10 then adds 3 |
| 1923 | * By testing the variable value we make sure the initializations occurred in |
| 1924 | * the correct order. |
| 1925 | */ |
| 1926 | static void xtest_tee_test_1023(ADBG_Case_t *c) |
| 1927 | { |
| 1928 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1929 | TEEC_Session session = { 0 }; |
| 1930 | uint32_t ret_orig = 0; |
| 1931 | |
| 1932 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, |
| 1933 | TEEC_NONE, TEEC_NONE); |
| 1934 | |
| 1935 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1936 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 1937 | NULL, &ret_orig))) |
| 1938 | return; |
| 1939 | |
| 1940 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1941 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op, |
| 1942 | &ret_orig)); |
| 1943 | |
| 1944 | /* Expected: initialization of os_test_lib, then TA */ |
| 1945 | (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21); |
| 1946 | |
| 1947 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1948 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL, |
| 1949 | &ret_orig)); |
| 1950 | |
| 1951 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1952 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op, |
| 1953 | &ret_orig)); |
| 1954 | |
| 1955 | /* Expected: initialization of os_test_lib_dl */ |
| 1956 | (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213); |
| 1957 | |
| 1958 | TEEC_CloseSession(&session); |
| 1959 | } |
| 1960 | ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023, |
| 1961 | "Test ELF initialization (.init_array)"); |
Javier Almansa Sobrino | cddc000 | 2020-02-10 13:35:37 +0000 | [diff] [blame] | 1962 | |
| 1963 | #ifdef CFG_CORE_TPM_EVENT_LOG |
| 1964 | static void xtest_tee_test_1024(ADBG_Case_t *c) |
| 1965 | { |
| 1966 | TEEC_Session session = {}; |
| 1967 | uint32_t ret_orig = 0; |
| 1968 | |
| 1969 | xtest_teec_open_session(&session, &tpm_log_test_ta_uuid, |
| 1970 | NULL, &ret_orig); |
| 1971 | |
| 1972 | Do_ADBG_BeginSubCase(c, "TPM test service invocation"); |
| 1973 | ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session, |
| 1974 | TA_TPM_TEST_GET_LOG, |
| 1975 | NULL, &ret_orig)); |
| 1976 | Do_ADBG_EndSubCase(c, "TPM test service invocation"); |
| 1977 | |
| 1978 | Do_ADBG_BeginSubCase(c, "TPM test passing short buffer"); |
| 1979 | ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session, |
| 1980 | TA_TPM_TEST_SHORT_BUF, |
| 1981 | NULL, &ret_orig)); |
| 1982 | Do_ADBG_EndSubCase(c, "TPM test passing short buffer"); |
| 1983 | |
| 1984 | TEEC_CloseSession(&session); |
| 1985 | } |
| 1986 | |
| 1987 | ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024, |
| 1988 | "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service"); |
| 1989 | #endif /* CFG_CORE_TPM_EVENT_LOG */ |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 1990 | |
| 1991 | static void xtest_tee_test_1025(ADBG_Case_t *c) |
| 1992 | { |
| 1993 | TEEC_Session session = {}; |
| 1994 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1995 | uint32_t ret_orig = 0; |
| 1996 | uint8_t *empty_buf = NULL; |
Etienne Carriere | 71383d4 | 2020-04-14 12:31:25 +0200 | [diff] [blame] | 1997 | TEEC_SharedMemory shm = { }; |
Etienne Carriere | dba3f3b | 2020-04-08 17:08:16 +0200 | [diff] [blame] | 1998 | TEEC_Result res = TEEC_ERROR_GENERIC; |
Etienne Carriere | 71383d4 | 2020-04-14 12:31:25 +0200 | [diff] [blame] | 1999 | |
| 2000 | Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration"); |
| 2001 | |
| 2002 | memset(&shm, 0, sizeof(shm)); |
| 2003 | shm.flags = TEEC_MEM_INPUT; |
| 2004 | shm.buffer = NULL; |
| 2005 | shm.size = 0; |
| 2006 | |
| 2007 | ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, |
| 2008 | TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm)); |
| 2009 | |
| 2010 | memset(&shm, 0, sizeof(shm)); |
| 2011 | shm.flags = TEEC_MEM_OUTPUT; |
| 2012 | shm.buffer = NULL; |
| 2013 | shm.size = 0; |
| 2014 | |
| 2015 | ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, |
| 2016 | TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm)); |
| 2017 | |
| 2018 | Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration"); |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2019 | |
Etienne Carriere | c602a52 | 2020-04-13 18:53:17 +0200 | [diff] [blame] | 2020 | if (!xtest_teec_ctx.memref_null) { |
Etienne Carriere | 71383d4 | 2020-04-14 12:31:25 +0200 | [diff] [blame] | 2021 | Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported"); |
Etienne Carriere | c602a52 | 2020-04-13 18:53:17 +0200 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2025 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2026 | xtest_teec_open_session(&session, |
| 2027 | &os_test_ta_uuid, |
| 2028 | NULL, &ret_orig))) |
| 2029 | return; |
| 2030 | |
| 2031 | empty_buf = malloc(1); |
| 2032 | if (!empty_buf) { |
| 2033 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY); |
Etienne Carriere | dba3f3b | 2020-04-08 17:08:16 +0200 | [diff] [blame] | 2034 | goto out_session; |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 2038 | TEEC_MEMREF_TEMP_INPUT, |
| 2039 | TEEC_MEMREF_TEMP_OUTPUT, |
| 2040 | TEEC_MEMREF_TEMP_OUTPUT); |
| 2041 | |
| 2042 | Do_ADBG_BeginSubCase(c, |
| 2043 | "Input/Output MEMREF Buffer NULL - Size 0 bytes"); |
| 2044 | |
| 2045 | op.params[0].tmpref.buffer = empty_buf; |
| 2046 | op.params[0].tmpref.size = 0; |
| 2047 | |
| 2048 | op.params[1].tmpref.buffer = NULL; |
| 2049 | op.params[1].tmpref.size = 0; |
| 2050 | |
| 2051 | op.params[2].tmpref.buffer = empty_buf; |
| 2052 | op.params[2].tmpref.size = 0; |
| 2053 | |
| 2054 | op.params[3].tmpref.buffer = NULL; |
| 2055 | op.params[3].tmpref.size = 0; |
| 2056 | |
| 2057 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 2058 | TEEC_InvokeCommand(&session, |
| 2059 | TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op, |
| 2060 | &ret_orig)); |
| 2061 | |
| 2062 | Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes"); |
| 2063 | |
| 2064 | Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes"); |
| 2065 | |
| 2066 | op.params[0].tmpref.buffer = empty_buf; |
| 2067 | op.params[0].tmpref.size = 1; |
| 2068 | |
| 2069 | op.params[1].tmpref.buffer = NULL; |
| 2070 | op.params[1].tmpref.size = 0; |
| 2071 | |
| 2072 | op.params[2].tmpref.buffer = empty_buf; |
| 2073 | op.params[2].tmpref.size = 0; |
| 2074 | |
| 2075 | op.params[3].tmpref.buffer = NULL; |
| 2076 | op.params[3].tmpref.size = 0; |
| 2077 | |
| 2078 | ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS, |
| 2079 | TEEC_InvokeCommand(&session, |
| 2080 | TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op, |
| 2081 | &ret_orig)); |
| 2082 | |
Etienne Carriere | dba3f3b | 2020-04-08 17:08:16 +0200 | [diff] [blame] | 2083 | TEEC_CloseSession(&session); |
| 2084 | |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2085 | Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes"); |
| 2086 | |
Etienne Carriere | dba3f3b | 2020-04-08 17:08:16 +0200 | [diff] [blame] | 2087 | Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation"); |
| 2088 | |
| 2089 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
| 2090 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 2091 | &ret_orig); |
| 2092 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 2093 | Do_ADBG_Log(" - 1025 - skip test, pseudo TA not found"); |
| 2094 | goto out; |
| 2095 | } |
| 2096 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2097 | goto out; |
| 2098 | |
| 2099 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE, |
| 2100 | TEEC_NONE, TEEC_NONE); |
| 2101 | op.params[0].tmpref.buffer = NULL; |
| 2102 | op.params[0].tmpref.size = 0; |
| 2103 | |
| 2104 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 2105 | TEEC_InvokeCommand(&session, |
| 2106 | PTA_INVOKE_TESTS_CMD_MEMREF_NULL, |
| 2107 | &op, &ret_orig)); |
| 2108 | |
| 2109 | out_session: |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2110 | TEEC_CloseSession(&session); |
Etienne Carriere | dba3f3b | 2020-04-08 17:08:16 +0200 | [diff] [blame] | 2111 | out: |
| 2112 | Do_ADBG_EndSubCase(c, NULL); |
Cedric Neveux | 9f483bb | 2019-03-04 08:58:06 +0100 | [diff] [blame] | 2113 | free(empty_buf); |
| 2114 | } |
| 2115 | ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025, |
| 2116 | "Test memref NULL and/or 0 bytes size"); |
Vesa Jääskeläinen | e8c0c8d | 2020-04-05 20:11:53 +0300 | [diff] [blame] | 2117 | |
Vesa Jääskeläinen | 502c549 | 2020-04-05 20:12:02 +0300 | [diff] [blame] | 2118 | #define TEE_UUID_NS_NAME_SIZE 128 |
| 2119 | |
| 2120 | /* |
| 2121 | * TEE Client UUID name space identifier (UUIDv4) |
| 2122 | * |
| 2123 | * Value here is random UUID that is allocated as name space identifier for |
| 2124 | * forming Client UUID's for TEE environment using UUIDv5 scheme. |
| 2125 | */ |
| 2126 | static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6"; |
| 2127 | |
Vesa Jääskeläinen | e8c0c8d | 2020-04-05 20:11:53 +0300 | [diff] [blame] | 2128 | /* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */ |
| 2129 | static TEEC_UUID client_uuid_public = { }; |
| 2130 | |
| 2131 | static void xtest_tee_test_1026(ADBG_Case_t *c) |
| 2132 | { |
| 2133 | TEEC_Result result = TEEC_ERROR_GENERIC; |
| 2134 | uint32_t ret_orig = 0; |
| 2135 | TEEC_Session session = { }; |
| 2136 | uint32_t login = UINT32_MAX; |
| 2137 | TEEC_UUID client_uuid = { }; |
| 2138 | |
| 2139 | result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid, |
| 2140 | TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig); |
| 2141 | |
| 2142 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2143 | return; |
| 2144 | |
| 2145 | result = ta_os_test_cmd_client_identity(&session, &login, |
| 2146 | &client_uuid); |
| 2147 | |
| 2148 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2149 | goto out; |
| 2150 | |
| 2151 | ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC); |
| 2152 | |
| 2153 | ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid, |
| 2154 | sizeof(TEEC_UUID)); |
| 2155 | |
| 2156 | out: |
| 2157 | TEEC_CloseSession(&session); |
| 2158 | } |
| 2159 | |
| 2160 | ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026, |
| 2161 | "Session: public login"); |
Vesa Jääskeläinen | 502c549 | 2020-04-05 20:12:02 +0300 | [diff] [blame] | 2162 | |
Etienne Carriere | f461e1d | 2020-05-19 12:42:08 +0200 | [diff] [blame] | 2163 | /* |
| 2164 | * regression_1027 |
| 2165 | * Depends on OpenSSL |
Jerome Forissier | 81e71a8 | 2021-06-16 10:39:12 +0200 | [diff] [blame] | 2166 | * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session |
| 2167 | * login client UUID generation") |
| 2168 | * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d |
Etienne Carriere | f461e1d | 2020-05-19 12:42:08 +0200 | [diff] [blame] | 2169 | * |
| 2170 | * xtest skips the test when not built with OpenSSL. |
| 2171 | */ |
Vesa Jääskeläinen | 502c549 | 2020-04-05 20:12:02 +0300 | [diff] [blame] | 2172 | static void xtest_tee_test_1027(ADBG_Case_t *c) |
| 2173 | { |
Victor Chong | 8e070bc | 2020-05-13 09:59:33 +0100 | [diff] [blame] | 2174 | #ifdef OPENSSL_FOUND |
Vesa Jääskeläinen | 502c549 | 2020-04-05 20:12:02 +0300 | [diff] [blame] | 2175 | TEEC_Result result = TEEC_ERROR_GENERIC; |
| 2176 | uint32_t ret_orig = 0; |
| 2177 | TEEC_Session session = { }; |
| 2178 | uint32_t login = UINT32_MAX; |
| 2179 | TEEC_UUID client_uuid = { }; |
| 2180 | TEEC_UUID expected_client_uuid = { }; |
| 2181 | TEEC_UUID uuid_ns = { }; |
| 2182 | char uuid_name[TEE_UUID_NS_NAME_SIZE] = { }; |
| 2183 | |
| 2184 | result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns); |
| 2185 | |
| 2186 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2187 | return; |
| 2188 | |
| 2189 | sprintf(uuid_name, "uid=%x", geteuid()); |
| 2190 | |
| 2191 | result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name, |
| 2192 | strlen(uuid_name)); |
| 2193 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2194 | return; |
| 2195 | |
| 2196 | result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid, |
| 2197 | TEEC_LOGIN_USER, NULL, NULL, &ret_orig); |
| 2198 | |
| 2199 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2200 | return; |
| 2201 | |
| 2202 | result = ta_os_test_cmd_client_identity(&session, &login, |
| 2203 | &client_uuid); |
| 2204 | |
| 2205 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2206 | goto out; |
| 2207 | |
| 2208 | ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER); |
| 2209 | |
| 2210 | ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid, |
| 2211 | sizeof(TEEC_UUID)); |
| 2212 | |
| 2213 | out: |
| 2214 | TEEC_CloseSession(&session); |
Victor Chong | 8e070bc | 2020-05-13 09:59:33 +0100 | [diff] [blame] | 2215 | #else /*!OPENSSL_FOUND*/ |
| 2216 | UNUSED(c); |
Etienne Carriere | 359b003 | 2020-05-16 05:56:48 +0200 | [diff] [blame] | 2217 | UNUSED(client_uuid_linux_ns); |
Victor Chong | 8e070bc | 2020-05-13 09:59:33 +0100 | [diff] [blame] | 2218 | /* xtest_uuid_v5() depends on OpenSSL */ |
| 2219 | Do_ADBG_Log("OpenSSL not available, skipping test 1027"); |
| 2220 | #endif |
Vesa Jääskeläinen | 502c549 | 2020-04-05 20:12:02 +0300 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027, |
| 2224 | "Session: user login for current user"); |
Vesa Jääskeläinen | 30dd087 | 2020-04-05 20:12:06 +0300 | [diff] [blame] | 2225 | |
Etienne Carriere | f461e1d | 2020-05-19 12:42:08 +0200 | [diff] [blame] | 2226 | /* |
| 2227 | * regression_1028 |
Jerome Forissier | 81e71a8 | 2021-06-16 10:39:12 +0200 | [diff] [blame] | 2228 | * Depends on kernel commit c5b4312bea5d ("tee: optee: Add support for session |
| 2229 | * login client UUID generation") |
| 2230 | * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c5b4312bea5d |
Etienne Carriere | f461e1d | 2020-05-19 12:42:08 +0200 | [diff] [blame] | 2231 | * |
| 2232 | * xtest skips the test when not built with OpenSSL. |
| 2233 | */ |
Vesa Jääskeläinen | 30dd087 | 2020-04-05 20:12:06 +0300 | [diff] [blame] | 2234 | static void xtest_tee_test_1028(ADBG_Case_t *c) |
| 2235 | { |
Victor Chong | 8e070bc | 2020-05-13 09:59:33 +0100 | [diff] [blame] | 2236 | #ifdef OPENSSL_FOUND |
Vesa Jääskeläinen | 30dd087 | 2020-04-05 20:12:06 +0300 | [diff] [blame] | 2237 | TEEC_Result result = TEEC_ERROR_GENERIC; |
| 2238 | uint32_t ret_orig = 0; |
| 2239 | TEEC_Session session = { }; |
| 2240 | uint32_t login = UINT32_MAX; |
| 2241 | TEEC_UUID client_uuid = { }; |
| 2242 | TEEC_UUID expected_client_uuid = { }; |
| 2243 | TEEC_UUID uuid_ns = { }; |
| 2244 | char uuid_name[TEE_UUID_NS_NAME_SIZE] = { }; |
| 2245 | uint32_t group = 0; |
| 2246 | |
| 2247 | group = getegid(); |
| 2248 | |
| 2249 | result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns); |
| 2250 | |
| 2251 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2252 | return; |
| 2253 | |
| 2254 | sprintf(uuid_name, "gid=%x", group); |
| 2255 | |
| 2256 | result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name, |
| 2257 | strlen(uuid_name)); |
| 2258 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2259 | return; |
| 2260 | |
| 2261 | result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid, |
| 2262 | TEEC_LOGIN_GROUP, &group, NULL, &ret_orig); |
| 2263 | |
| 2264 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2265 | return; |
| 2266 | |
| 2267 | result = ta_os_test_cmd_client_identity(&session, &login, |
| 2268 | &client_uuid); |
| 2269 | |
| 2270 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, result)) |
| 2271 | goto out; |
| 2272 | |
| 2273 | ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP); |
| 2274 | |
| 2275 | ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid, |
| 2276 | sizeof(TEEC_UUID)); |
| 2277 | |
| 2278 | out: |
| 2279 | TEEC_CloseSession(&session); |
Victor Chong | 8e070bc | 2020-05-13 09:59:33 +0100 | [diff] [blame] | 2280 | #else /*!OPENSSL_FOUND*/ |
| 2281 | UNUSED(c); |
| 2282 | /* xtest_uuid_v5() depends on OpenSSL */ |
| 2283 | Do_ADBG_Log("OpenSSL not available, skipping test 1028"); |
| 2284 | #endif |
Vesa Jääskeläinen | 30dd087 | 2020-04-05 20:12:06 +0300 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028, |
| 2288 | "Session: group login for current user's effective group"); |
Jerome Forissier | 9a7101b | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2289 | |
| 2290 | static void xtest_tee_test_1029(ADBG_Case_t *c) |
| 2291 | { |
Jerome Forissier | 82e87bb | 2020-08-19 14:00:09 +0200 | [diff] [blame] | 2292 | TEEC_Result res = TEEC_SUCCESS; |
Jerome Forissier | 9a7101b | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2293 | TEEC_Session session = { 0 }; |
| 2294 | uint32_t ret_orig = 0; |
| 2295 | |
| 2296 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2297 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 2298 | NULL, &ret_orig))) |
| 2299 | return; |
| 2300 | |
| 2301 | Do_ADBG_BeginSubCase(c, "TLS variables (main program)"); |
Jerome Forissier | 82e87bb | 2020-08-19 14:00:09 +0200 | [diff] [blame] | 2302 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL, |
| 2303 | &ret_orig); |
| 2304 | if (res == TEEC_ERROR_NOT_SUPPORTED) |
| 2305 | Do_ADBG_Log(" - 1029 - skip test, " |
| 2306 | "TA returned TEEC_ERROR_NOT_SUPPORTED"); |
| 2307 | else |
| 2308 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jerome Forissier | 9a7101b | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2309 | Do_ADBG_EndSubCase(c, "TLS variables (main program)"); |
| 2310 | |
| 2311 | Do_ADBG_BeginSubCase(c, "TLS variables (shared library)"); |
Jerome Forissier | 82e87bb | 2020-08-19 14:00:09 +0200 | [diff] [blame] | 2312 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL, |
| 2313 | &ret_orig); |
| 2314 | if (res == TEEC_ERROR_NOT_SUPPORTED) |
| 2315 | Do_ADBG_Log(" - 1029 - skip test, " |
| 2316 | "TA returned TEEC_ERROR_NOT_SUPPORTED"); |
| 2317 | else |
| 2318 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jerome Forissier | 9a7101b | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2319 | Do_ADBG_EndSubCase(c, "TLS variables (shared library)"); |
| 2320 | |
| 2321 | TEEC_CloseSession(&session); |
| 2322 | } |
| 2323 | ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029, |
| 2324 | "Test __thread attribute"); |
Jerome Forissier | 4565c45 | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2325 | |
| 2326 | static void xtest_tee_test_1030(ADBG_Case_t *c) |
| 2327 | { |
| 2328 | TEEC_Session session = { 0 }; |
| 2329 | uint32_t ret_orig = 0; |
| 2330 | |
| 2331 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2332 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 2333 | NULL, &ret_orig))) |
| 2334 | return; |
| 2335 | |
| 2336 | Do_ADBG_BeginSubCase(c, "Before dlopen()"); |
| 2337 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2338 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL, |
| 2339 | &ret_orig)); |
| 2340 | Do_ADBG_EndSubCase(c, "Before dlopen()"); |
| 2341 | |
| 2342 | Do_ADBG_BeginSubCase(c, "After dlopen()"); |
| 2343 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2344 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL, |
| 2345 | &ret_orig)); |
| 2346 | Do_ADBG_EndSubCase(c, "After dlopen()"); |
| 2347 | |
| 2348 | TEEC_CloseSession(&session); |
| 2349 | } |
| 2350 | ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030, |
| 2351 | "Test dl_iterate_phdr()"); |
Jerome Forissier | 1a205ae | 2020-06-17 17:55:00 +0200 | [diff] [blame] | 2352 | |
| 2353 | #ifndef __clang__ |
| 2354 | static void xtest_tee_test_1031(ADBG_Case_t *c) |
| 2355 | { |
| 2356 | TEEC_Result ret = TEE_SUCCESS; |
| 2357 | TEEC_Session session = { 0 }; |
| 2358 | uint32_t ret_orig = 0; |
| 2359 | |
| 2360 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2361 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 2362 | NULL, &ret_orig))) |
| 2363 | return; |
| 2364 | |
| 2365 | Do_ADBG_BeginSubCase(c, "Global object constructor (main program)"); |
| 2366 | ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL, |
| 2367 | &ret_orig); |
| 2368 | if (ret == TEEC_ERROR_NOT_SUPPORTED) { |
| 2369 | printf("TA not built with C++ support, skipping C++ tests\n"); |
| 2370 | Do_ADBG_EndSubCase(c, "Global object constructor (main program)"); |
| 2371 | goto out; |
| 2372 | |
| 2373 | } |
| 2374 | ADBG_EXPECT_TEEC_SUCCESS(c, ret); |
| 2375 | Do_ADBG_EndSubCase(c, "Global object constructor (main program)"); |
| 2376 | |
| 2377 | Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)"); |
| 2378 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2379 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB, |
| 2380 | NULL, &ret_orig)); |
| 2381 | Do_ADBG_EndSubCase(c, "Global object constructor (shared library)"); |
| 2382 | |
| 2383 | Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)"); |
| 2384 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2385 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL, |
| 2386 | NULL, &ret_orig)); |
| 2387 | Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)"); |
| 2388 | |
| 2389 | Do_ADBG_BeginSubCase(c, "Exceptions (simple)"); |
| 2390 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2391 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL, |
| 2392 | &ret_orig)); |
| 2393 | Do_ADBG_EndSubCase(c, "Exceptions (simple)"); |
| 2394 | |
| 2395 | Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)"); |
| 2396 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2397 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL, |
| 2398 | &ret_orig)); |
| 2399 | Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)"); |
| 2400 | out: |
| 2401 | TEEC_CloseSession(&session); |
| 2402 | } |
| 2403 | ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031, |
| 2404 | "Test C++ features"); |
| 2405 | #endif |
Jens Wiklander | e16da89 | 2020-09-04 09:24:16 +0200 | [diff] [blame] | 2406 | |
| 2407 | static void xtest_tee_test_1032(ADBG_Case_t *c) |
| 2408 | { |
| 2409 | TEEC_Result res = TEEC_SUCCESS; |
| 2410 | TEEC_Context ctx = { }; |
| 2411 | TEEC_SharedMemory shm1 = { |
| 2412 | .buffer = xtest_tee_test_1032, |
| 2413 | .size = 32, |
| 2414 | .flags = TEEC_MEM_INPUT, |
| 2415 | }; |
| 2416 | static const uint8_t dummy_data[32] = { 1, 2, 3, 4, }; |
| 2417 | TEEC_SharedMemory shm2 = { |
| 2418 | .buffer = (void *)dummy_data, |
| 2419 | .size = sizeof(dummy_data), |
| 2420 | .flags = TEEC_MEM_INPUT, |
| 2421 | }; |
| 2422 | |
| 2423 | res = TEEC_InitializeContext(xtest_tee_name, &ctx); |
| 2424 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2425 | return; |
| 2426 | |
| 2427 | res = TEEC_RegisterSharedMemory(&ctx, &shm1); |
| 2428 | if (ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2429 | TEEC_ReleaseSharedMemory(&shm1); |
| 2430 | |
| 2431 | res = TEEC_RegisterSharedMemory(&ctx, &shm2); |
| 2432 | if (ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2433 | TEEC_ReleaseSharedMemory(&shm2); |
| 2434 | |
| 2435 | TEEC_FinalizeContext(&ctx); |
| 2436 | } |
| 2437 | ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032, |
| 2438 | "Register read-only shared memory"); |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 2439 | |
| 2440 | static void xtest_tee_test_1033(ADBG_Case_t *c) |
| 2441 | { |
| 2442 | TEEC_Session session = { }; |
| 2443 | uint32_t ret_orig = 0; |
| 2444 | |
| 2445 | /* TA will ping the test plugin during open session operation */ |
| 2446 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2447 | xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid, |
| 2448 | NULL, &ret_orig))) |
| 2449 | return; |
| 2450 | |
| 2451 | Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin"); |
| 2452 | { |
| 2453 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2454 | |
| 2455 | op.params[0].value.a = 20; |
| 2456 | op.params[0].value.b = 10; |
| 2457 | op.params[1].value.a = '+'; |
| 2458 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, |
| 2459 | TEEC_VALUE_INPUT, TEEC_NONE, |
| 2460 | TEEC_NONE); |
| 2461 | |
| 2462 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2463 | TEEC_InvokeCommand(&session, |
| 2464 | TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op, |
| 2465 | &ret_orig)); |
| 2466 | ADBG_EXPECT(c, 30, op.params[0].value.a); |
| 2467 | |
| 2468 | /* reassign, because the values was changed during previous op */ |
| 2469 | op.params[0].value.a = 20; |
| 2470 | op.params[0].value.b = 10; |
| 2471 | op.params[1].value.a = '-'; |
| 2472 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2473 | TEEC_InvokeCommand(&session, |
| 2474 | TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op, |
| 2475 | &ret_orig)); |
| 2476 | ADBG_EXPECT(c, 10, op.params[0].value.a); |
| 2477 | } |
| 2478 | Do_ADBG_EndSubCase(c, "Pass values to/from a plugin"); |
| 2479 | |
| 2480 | Do_ADBG_BeginSubCase(c, "Pass array to a plugin"); |
| 2481 | { |
| 2482 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
Aleksandr Anisimov | 8076839 | 2021-03-05 19:36:20 +0300 | [diff] [blame] | 2483 | uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 }; |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 2484 | |
| 2485 | op.params[0].tmpref.buffer = to_plugin; |
| 2486 | op.params[0].tmpref.size = sizeof(to_plugin); |
Jens Wiklander | 104cd06 | 2022-12-09 13:44:26 +0100 | [diff] [blame] | 2487 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, |
Aleksandr Anisimov | 8076839 | 2021-03-05 19:36:20 +0300 | [diff] [blame] | 2488 | TEEC_VALUE_OUTPUT, |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 2489 | TEEC_NONE, TEEC_NONE); |
| 2490 | |
| 2491 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2492 | TEEC_InvokeCommand(&session, |
| 2493 | TA_SUPP_PLUGIN_CMD_WRITE_ARR, |
| 2494 | &op, &ret_orig)); |
| 2495 | |
| 2496 | /* |
Aleksandr Anisimov | 8076839 | 2021-03-05 19:36:20 +0300 | [diff] [blame] | 2497 | * The test plugin must calculate a sum of the input elements |
| 2498 | * and store it to 'op.params[1].value.a' |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 2499 | */ |
Aleksandr Anisimov | 8076839 | 2021-03-05 19:36:20 +0300 | [diff] [blame] | 2500 | ADBG_EXPECT(c, 15, op.params[1].value.a); |
Aleksandr Anisimov | 01f6f06 | 2021-01-19 11:02:25 +0300 | [diff] [blame] | 2501 | } |
| 2502 | Do_ADBG_EndSubCase(c, "Pass array to a plugin"); |
| 2503 | |
| 2504 | Do_ADBG_BeginSubCase(c, "Get array from a plugin"); |
| 2505 | { |
| 2506 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2507 | char from_plugin[64] = { }; |
| 2508 | char expected_arr[] = "Array from plugin"; |
| 2509 | size_t expectes_size = sizeof(expected_arr); |
| 2510 | |
| 2511 | op.params[0].tmpref.buffer = from_plugin; |
| 2512 | op.params[0].tmpref.size = sizeof(from_plugin); |
| 2513 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, |
| 2514 | TEEC_VALUE_OUTPUT, TEEC_NONE, |
| 2515 | TEEC_NONE); |
| 2516 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2517 | TEEC_InvokeCommand(&session, |
| 2518 | TA_SUPP_PLUGIN_CMD_GET_ARR, &op, |
| 2519 | &ret_orig)); |
| 2520 | ADBG_EXPECT(c, expectes_size, op.params[1].value.a); |
| 2521 | ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size); |
| 2522 | } |
| 2523 | Do_ADBG_EndSubCase(c, "Get array from a plugin"); |
| 2524 | |
| 2525 | Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin"); |
| 2526 | { |
| 2527 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2528 | |
| 2529 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, |
| 2530 | TEEC_NONE, TEEC_NONE); |
| 2531 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS, |
| 2532 | TEEC_InvokeCommand(&session, |
| 2533 | TA_SUPP_PLUGIN_CMD_BAD_UUID, &op, |
| 2534 | &ret_orig)); |
| 2535 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS, |
| 2536 | TEEC_InvokeCommand(&session, |
| 2537 | TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op, |
| 2538 | &ret_orig)); |
| 2539 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS, |
| 2540 | TEEC_InvokeCommand(&session, |
| 2541 | TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op, |
| 2542 | &ret_orig)); |
| 2543 | } |
| 2544 | Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin"); |
| 2545 | |
| 2546 | Do_ADBG_BeginSubCase(c, "Call an unknown plugin"); |
| 2547 | { |
| 2548 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2549 | |
| 2550 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, |
| 2551 | TEEC_NONE, TEEC_NONE); |
| 2552 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND, |
| 2553 | TEEC_InvokeCommand(&session, |
| 2554 | TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID, |
| 2555 | &op, &ret_orig)); |
| 2556 | } |
| 2557 | Do_ADBG_EndSubCase(c, "Call an unknown plugin"); |
| 2558 | |
| 2559 | TEEC_CloseSession(&session); |
| 2560 | } |
| 2561 | ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033, |
| 2562 | "Test the supplicant plugin framework"); |
Jens Wiklander | 94fb158 | 2021-05-19 10:14:57 +0200 | [diff] [blame] | 2563 | |
| 2564 | static void xtest_tee_test_1034(ADBG_Case_t *c) |
| 2565 | { |
| 2566 | TEEC_Result res = TEEC_SUCCESS; |
| 2567 | TEEC_Session session = { }; |
| 2568 | uint32_t ret_orig = 0; |
| 2569 | |
| 2570 | res = xtest_teec_open_session(&session, &large_ta_uuid, NULL, |
| 2571 | &ret_orig); |
Jerome Forissier | e8be5b5 | 2021-06-29 16:29:06 +0200 | [diff] [blame] | 2572 | if (res == TEEC_ERROR_OUT_OF_MEMORY) { |
| 2573 | Do_ADBG_Log("TEEC_ERROR_OUT_OF_MEMORY - ignored"); |
Tim Lee | 5502a99 | 2023-07-12 16:48:47 +0800 | [diff] [blame] | 2574 | return; |
Jerome Forissier | e8be5b5 | 2021-06-29 16:29:06 +0200 | [diff] [blame] | 2575 | } |
Tim Lee | 5502a99 | 2023-07-12 16:48:47 +0800 | [diff] [blame] | 2576 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 2577 | if (!res) |
| 2578 | TEEC_CloseSession(&session); |
Jens Wiklander | 94fb158 | 2021-05-19 10:14:57 +0200 | [diff] [blame] | 2579 | } |
| 2580 | ADBG_CASE_DEFINE(regression, 1034, xtest_tee_test_1034, |
| 2581 | "Test loading a large TA"); |
Ruchika Gupta | 813b6d4 | 2021-12-01 10:44:14 +0530 | [diff] [blame] | 2582 | |
| 2583 | #if defined(CFG_TA_BTI) |
| 2584 | struct bti_test { |
| 2585 | uint32_t cmd; |
| 2586 | uint32_t func; |
| 2587 | }; |
| 2588 | |
| 2589 | #define BTI_TEST(caller_func, bti_func) { \ |
| 2590 | .cmd = caller_func, \ |
| 2591 | .func = bti_func, \ |
| 2592 | } |
| 2593 | |
| 2594 | static const struct bti_test bti_cases_success[] = { |
| 2595 | BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_C), |
| 2596 | BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_JC), |
| 2597 | BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_J), |
| 2598 | BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_JC), |
| 2599 | BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_C), |
| 2600 | BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_J), |
| 2601 | BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_JC), |
| 2602 | }; |
| 2603 | |
| 2604 | static const struct bti_test bti_cases_panic[] = { |
| 2605 | BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_J), |
| 2606 | BTI_TEST(TA_TEST_USING_BLR, TA_FUNC_BTI_NONE), |
| 2607 | BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_C), |
| 2608 | BTI_TEST(TA_TEST_USING_BR, TA_FUNC_BTI_NONE), |
| 2609 | BTI_TEST(TA_TEST_USING_BR_X16, TA_FUNC_BTI_NONE), |
| 2610 | }; |
| 2611 | |
| 2612 | static void get_cpu_feature(ADBG_Case_t *c, bool *bti) |
| 2613 | { |
| 2614 | TEEC_Session session = {}; |
| 2615 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2616 | uint32_t ret_orig = 0; |
| 2617 | TEEC_Result res; |
| 2618 | |
| 2619 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE, TEEC_NONE, |
| 2620 | TEEC_NONE); |
| 2621 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2622 | xtest_teec_open_session(&session, &bti_test_ta_uuid, |
| 2623 | NULL, &ret_orig))) |
| 2624 | return; |
| 2625 | |
| 2626 | res = TEEC_InvokeCommand(&session, TA_FEAT_BTI_IMPLEMENTED, &op, |
| 2627 | &ret_orig); |
| 2628 | if (!res) { |
| 2629 | if(op.params[0].value.a) |
| 2630 | *bti = true; |
| 2631 | Do_ADBG_Log("FEAT_BTI is %simplemented", *bti ? "" : "NOT "); |
| 2632 | } |
| 2633 | |
| 2634 | TEEC_CloseSession(&session); |
| 2635 | } |
| 2636 | |
| 2637 | static void xtest_tee_test_1035(ADBG_Case_t *c) |
| 2638 | { |
| 2639 | TEEC_Session session = {}; |
| 2640 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2641 | struct bti_test const *test = NULL; |
| 2642 | uint32_t ret_orig = 0; |
| 2643 | TEEC_Result res; |
| 2644 | unsigned int n = 0; |
| 2645 | bool cpu_feature_bti = false; |
| 2646 | |
| 2647 | Do_ADBG_BeginSubCase(c, "BTI Implemented"); |
| 2648 | get_cpu_feature(c, &cpu_feature_bti); |
| 2649 | Do_ADBG_EndSubCase(c, "BTI Implemented"); |
| 2650 | |
| 2651 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, |
| 2652 | TEEC_NONE); |
| 2653 | |
| 2654 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2655 | xtest_teec_open_session(&session, &bti_test_ta_uuid, |
| 2656 | NULL, &ret_orig))) |
| 2657 | return; |
| 2658 | |
| 2659 | Do_ADBG_BeginSubCase(c, "BTI Pass Cases"); |
| 2660 | for (n = 0; n < ARRAY_SIZE(bti_cases_success); n++) { |
| 2661 | test = &bti_cases_success[n]; |
| 2662 | |
| 2663 | op.params[0].value.a = test->func; |
| 2664 | |
| 2665 | res = TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig); |
| 2666 | if (res == TEEC_ERROR_NOT_IMPLEMENTED) { |
| 2667 | Do_ADBG_Log("Binary doesn't support BTI - skip tests"); |
| 2668 | Do_ADBG_EndSubCase(c, "BTI Pass Cases"); |
| 2669 | TEEC_CloseSession(&session); |
| 2670 | return; |
| 2671 | } |
| 2672 | |
| 2673 | Do_ADBG_BeginSubCase(c, "BTI Case %u", n); |
| 2674 | |
| 2675 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 2676 | |
| 2677 | Do_ADBG_EndSubCase(c, "BTI Case %u", n); |
| 2678 | } |
| 2679 | Do_ADBG_EndSubCase(c, "BTI Pass Cases"); |
| 2680 | |
| 2681 | TEEC_CloseSession(&session); |
| 2682 | |
| 2683 | Do_ADBG_BeginSubCase(c, "BTI Exception Generation"); |
| 2684 | for (n = 0; n < ARRAY_SIZE(bti_cases_panic); n++) { |
| 2685 | test = &bti_cases_panic[n]; |
| 2686 | res = TEEC_SUCCESS; |
| 2687 | |
| 2688 | if (cpu_feature_bti) |
| 2689 | res = TEEC_ERROR_TARGET_DEAD; |
| 2690 | |
| 2691 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2692 | xtest_teec_open_session(&session, &bti_test_ta_uuid, |
| 2693 | NULL, &ret_orig))) |
| 2694 | goto out; |
| 2695 | |
| 2696 | Do_ADBG_BeginSubCase(c, "BTI Case %u", n); |
| 2697 | op.params[0].value.a = test->func; |
| 2698 | |
| 2699 | (void)ADBG_EXPECT_TEEC_RESULT(c, res, |
| 2700 | TEEC_InvokeCommand(&session, test->cmd, &op, &ret_orig)); |
| 2701 | |
| 2702 | if (cpu_feature_bti) |
| 2703 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, |
| 2704 | ret_orig); |
| 2705 | |
| 2706 | Do_ADBG_EndSubCase(c, "BTI Case %u", n); |
| 2707 | |
| 2708 | TEEC_CloseSession(&session); |
| 2709 | } |
| 2710 | |
| 2711 | out: |
| 2712 | Do_ADBG_EndSubCase(c, "BTI Exception Generation"); |
| 2713 | } |
| 2714 | ADBG_CASE_DEFINE(regression, 1035, xtest_tee_test_1035, "Test BTI"); |
| 2715 | #endif |
Ruchika Gupta | 4808e38 | 2022-01-28 12:41:21 +0530 | [diff] [blame] | 2716 | |
| 2717 | static void xtest_tee_test_1036(ADBG_Case_t *c) |
| 2718 | { |
| 2719 | TEEC_Session session = { }; |
| 2720 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2721 | uint32_t ret_orig = 0; |
| 2722 | TEEC_Result res = TEEC_SUCCESS; |
| 2723 | |
| 2724 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 2725 | TEEC_NONE); |
| 2726 | |
| 2727 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 2728 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 2729 | NULL, &ret_orig))) |
| 2730 | return; |
| 2731 | |
| 2732 | Do_ADBG_BeginSubCase(c, "PAuth NOP test"); |
| 2733 | |
| 2734 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PAUTH_NOP, &op, |
| 2735 | &ret_orig); |
| 2736 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 2737 | Do_ADBG_Log("Binary doesn't support PAuth - skip tests"); |
| 2738 | goto out; |
| 2739 | } |
| 2740 | |
| 2741 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2742 | goto out; |
| 2743 | |
| 2744 | Do_ADBG_EndSubCase(c, "PAuth NOP test"); |
| 2745 | |
| 2746 | Do_ADBG_BeginSubCase(c, "PAuth PAC corruption"); |
| 2747 | |
| 2748 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, |
| 2749 | TEEC_InvokeCommand(&session, |
| 2750 | TA_OS_TEST_CMD_PAUTH_CORRUPT_PAC, |
| 2751 | &op, &ret_orig)); |
| 2752 | |
| 2753 | ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 2754 | |
| 2755 | Do_ADBG_EndSubCase(c, "PAuth PAC corruption"); |
| 2756 | |
| 2757 | out: |
| 2758 | TEEC_CloseSession(&session); |
| 2759 | } |
| 2760 | ADBG_CASE_DEFINE(regression, 1036, xtest_tee_test_1036, |
| 2761 | "Test PAuth (Pointer Authentication)"); |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 2762 | |
| 2763 | #define ATT_MAX_KEYSZ 4096 |
| 2764 | |
| 2765 | #ifdef OPENSSL_FOUND |
| 2766 | static RSA *att_key; |
| 2767 | static size_t att_key_size; /* Actual key size (modulus size) in bytes */ |
| 2768 | |
| 2769 | /* |
| 2770 | * buf = [ TA hash (32 bytes SHA256) | Signature (att_key_size bytes) ] |
| 2771 | * Signature = RSA_SSA_PKCS1_PSS_MGF1(SHA256([nonce | TA hash]) |
| 2772 | */ |
| 2773 | static void check_signature(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size, |
| 2774 | uint8_t *buf) |
| 2775 | { |
| 2776 | unsigned char digest[SHA256_DIGEST_LENGTH] = { }; |
| 2777 | unsigned char *sig = buf + SHA256_DIGEST_LENGTH; |
| 2778 | unsigned char *ta_hash = buf; |
| 2779 | uint8_t decr[ATT_MAX_KEYSZ / 8] = { }; |
| 2780 | SHA256_CTX ctx = { }; |
| 2781 | int salt_len = 32; /* Hard-coded in the PTA */ |
| 2782 | int st = 0; |
| 2783 | |
| 2784 | if (!ADBG_EXPECT_NOT_NULL(c, att_key)) |
| 2785 | return; |
| 2786 | |
| 2787 | SHA256_Init(&ctx); |
| 2788 | SHA256_Update(&ctx, nonce, nonce_size); |
| 2789 | SHA256_Update(&ctx, ta_hash, SHA256_DIGEST_LENGTH); |
| 2790 | SHA256_Final(digest, &ctx); |
| 2791 | |
| 2792 | st = RSA_public_decrypt(att_key_size, sig, decr, att_key, |
| 2793 | RSA_NO_PADDING); |
| 2794 | ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0); |
| 2795 | st = RSA_verify_PKCS1_PSS_mgf1(att_key, digest, EVP_sha256(), |
| 2796 | EVP_sha256(), decr, salt_len); |
| 2797 | ADBG_EXPECT_COMPARE_SIGNED(c, st, >, 0); |
| 2798 | } |
| 2799 | |
| 2800 | static void free_att_key(void) |
| 2801 | { |
| 2802 | RSA_free(att_key); |
| 2803 | att_key = NULL; |
| 2804 | } |
| 2805 | |
Clement Faure | 44a31d0 | 2022-03-30 10:50:52 +0200 | [diff] [blame] | 2806 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 2807 | static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
| 2808 | { |
| 2809 | if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) |
| 2810 | return 0; |
| 2811 | |
| 2812 | if (n != NULL) { |
| 2813 | BN_free(r->n); |
| 2814 | r->n = n; |
| 2815 | } |
| 2816 | |
| 2817 | if (e != NULL) { |
| 2818 | BN_free(r->e); |
| 2819 | r->e = e; |
| 2820 | } |
| 2821 | |
| 2822 | if (d != NULL) { |
| 2823 | BN_free(r->d); |
| 2824 | r->d = d; |
| 2825 | } |
| 2826 | |
| 2827 | return 1; |
| 2828 | } |
| 2829 | #endif |
| 2830 | |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 2831 | static void set_att_key(ADBG_Case_t *c, uint8_t *e, size_t e_sz, uint8_t *n, |
| 2832 | size_t n_sz) |
| 2833 | { |
| 2834 | BIGNUM *bn_e = NULL; |
| 2835 | BIGNUM *bn_n = NULL; |
| 2836 | int st = 0; |
| 2837 | |
| 2838 | att_key_size = n_sz; |
| 2839 | att_key = RSA_new(); |
| 2840 | if (!ADBG_EXPECT_NOT_NULL(c, att_key)) |
| 2841 | return; |
| 2842 | |
| 2843 | bn_e = BN_bin2bn(e, e_sz, NULL); |
| 2844 | if (!ADBG_EXPECT_NOT_NULL(c, bn_e)) |
| 2845 | goto err; |
| 2846 | bn_n = BN_bin2bn(n, n_sz, NULL); |
| 2847 | if (!ADBG_EXPECT_NOT_NULL(c, bn_n)) |
| 2848 | goto err; |
| 2849 | |
| 2850 | st = RSA_set0_key(att_key, bn_n, bn_e, BN_new()); |
| 2851 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, st, !=, 0)) |
| 2852 | goto err; |
| 2853 | return; |
| 2854 | err: |
| 2855 | free_att_key(); |
| 2856 | } |
| 2857 | #else |
| 2858 | #define check_signature(...) |
| 2859 | #define set_att_key(...) |
| 2860 | #define free_att_key() |
| 2861 | #endif |
| 2862 | |
| 2863 | /* |
| 2864 | * Verification of the output of the attestation PTA |
| 2865 | * - (If hash != NULL) check buf contains the expected hash |
| 2866 | * - (If OpenSSL is available) Check that the signature is valid |
| 2867 | */ |
| 2868 | static void check_measurement(ADBG_Case_t *c, uint8_t *nonce, size_t nonce_size, |
| 2869 | uint8_t *hash, uint8_t *buf) |
| 2870 | { |
Pierre Moos | c7f733c | 2022-08-22 15:39:22 +0200 | [diff] [blame] | 2871 | (void)nonce; |
| 2872 | (void)nonce_size; |
| 2873 | |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 2874 | if (hash) |
| 2875 | ADBG_EXPECT_BUFFER(c, hash, 32, buf, 32); |
| 2876 | |
| 2877 | check_signature(c, nonce, nonce_size, buf); |
| 2878 | } |
| 2879 | |
| 2880 | /* Invoke attestation PTA to return the public key */ |
| 2881 | static void get_att_public_key(ADBG_Case_t *c) |
| 2882 | { |
| 2883 | uint8_t n[ATT_MAX_KEYSZ / 8] = { }; |
| 2884 | uint8_t e[3] = { }; /* We know e == 65537... */ |
| 2885 | size_t n_sz = sizeof(n); |
| 2886 | size_t e_sz = sizeof(e); |
| 2887 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2888 | TEEC_UUID att_uuid = PTA_ATTESTATION_UUID; |
| 2889 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 2890 | TEEC_Session session = { }; |
| 2891 | uint32_t ret_orig = 0; |
| 2892 | |
| 2893 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, |
| 2894 | TEEC_MEMREF_TEMP_OUTPUT, |
| 2895 | TEEC_VALUE_OUTPUT, TEEC_NONE); |
| 2896 | op.params[0].tmpref.buffer = e; |
| 2897 | op.params[0].tmpref.size = e_sz; |
| 2898 | op.params[1].tmpref.buffer = n; |
| 2899 | op.params[1].tmpref.size = n_sz; |
| 2900 | |
| 2901 | res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig); |
| 2902 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2903 | return; |
| 2904 | |
| 2905 | res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_PUBKEY, &op, |
| 2906 | &ret_orig); |
| 2907 | |
| 2908 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res) || |
| 2909 | !ADBG_EXPECT_COMPARE_UNSIGNED(c, op.params[2].value.a, ==, |
| 2910 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256)) |
| 2911 | goto out; |
| 2912 | |
| 2913 | e_sz = op.params[0].tmpref.size; |
| 2914 | n_sz = op.params[1].tmpref.size; |
| 2915 | set_att_key(c, e, e_sz, n, n_sz); |
| 2916 | out: |
| 2917 | TEEC_CloseSession(&session); |
| 2918 | } |
| 2919 | |
| 2920 | /* Invoke attestation PTA to hash the TEE binary */ |
| 2921 | static void attestation_tee(ADBG_Case_t *c) |
| 2922 | { |
| 2923 | uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { }; |
| 2924 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2925 | uint8_t nonce[4] = { 0x12, 0x34, 0x56, 0x78 }; |
| 2926 | TEEC_UUID att_uuid = PTA_ATTESTATION_UUID; |
| 2927 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 2928 | TEEC_Session session = { }; |
| 2929 | uint32_t ret_orig = 0; |
| 2930 | |
| 2931 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 2932 | TEEC_MEMREF_TEMP_OUTPUT, |
| 2933 | TEEC_NONE, TEEC_NONE); |
| 2934 | op.params[0].tmpref.buffer = nonce; |
| 2935 | op.params[0].tmpref.size = sizeof(nonce); |
| 2936 | op.params[1].tmpref.buffer = measurement; |
| 2937 | op.params[1].tmpref.size = sizeof(measurement); |
| 2938 | |
| 2939 | res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig); |
| 2940 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2941 | return; |
| 2942 | |
| 2943 | /* Hash TEE and check signature */ |
| 2944 | res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_HASH_TEE_MEMORY, &op, |
| 2945 | &ret_orig); |
| 2946 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 2947 | check_measurement(c, nonce, sizeof(nonce), NULL, measurement); |
| 2948 | |
| 2949 | TEEC_CloseSession(&session); |
| 2950 | } |
| 2951 | |
| 2952 | /* Invoke attestation PTA to obtain the digest contained in the some TA shdr */ |
| 2953 | static void attestation_ta_shdr(ADBG_Case_t *c) |
| 2954 | { |
| 2955 | uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { }; |
| 2956 | uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 }; |
| 2957 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 2958 | uint8_t hash1[TEE_SHA256_HASH_SIZE] = { }; |
| 2959 | uint8_t hash2[TEE_SHA256_HASH_SIZE] = { }; |
| 2960 | TEEC_UUID att_uuid = PTA_ATTESTATION_UUID; |
| 2961 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 2962 | TEEC_Session session = { }; |
| 2963 | uint32_t ret_orig = 0; |
| 2964 | int cmp = 0; |
| 2965 | |
| 2966 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 2967 | TEEC_MEMREF_TEMP_INPUT, |
| 2968 | TEEC_MEMREF_TEMP_OUTPUT, |
| 2969 | TEEC_NONE); |
| 2970 | op.params[0].tmpref.buffer = (void *)&os_test_ta_uuid; |
| 2971 | op.params[0].tmpref.size = sizeof(TEEC_UUID); |
| 2972 | op.params[1].tmpref.buffer = nonce; |
| 2973 | op.params[1].tmpref.size = sizeof(nonce); |
| 2974 | op.params[2].tmpref.buffer = measurement; |
| 2975 | op.params[2].tmpref.size = sizeof(measurement); |
| 2976 | |
| 2977 | res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig); |
| 2978 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 2979 | return; |
| 2980 | |
| 2981 | /* Hash TA and check signature */ |
| 2982 | res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST, |
| 2983 | &op, &ret_orig); |
| 2984 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 2985 | check_measurement(c, nonce, sizeof(nonce), NULL, measurement); |
| 2986 | /* Save hash */ |
| 2987 | memcpy(hash1, measurement, 32); |
| 2988 | |
| 2989 | /* Hash TA again */ |
| 2990 | memset(measurement, 0, sizeof(measurement)); |
| 2991 | res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST, |
| 2992 | &op, &ret_orig); |
| 2993 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 2994 | /* New hash should be identical */ |
| 2995 | check_measurement(c, nonce, sizeof(nonce), hash1, measurement); |
| 2996 | |
| 2997 | /* Hash another TA */ |
| 2998 | op.params[0].tmpref.buffer = (void *)&crypt_user_ta_uuid; |
| 2999 | memset(measurement, 0, sizeof(measurement)); |
| 3000 | res = TEEC_InvokeCommand(&session, PTA_ATTESTATION_GET_TA_SHDR_DIGEST, |
| 3001 | &op, &ret_orig); |
| 3002 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3003 | check_measurement(c, nonce, sizeof(nonce), NULL, measurement); |
| 3004 | memcpy(hash2, measurement, 32); |
| 3005 | /* Different binaries should have different hashes */ |
| 3006 | cmp = memcmp(hash1, hash2, sizeof(hash1)); |
| 3007 | ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0); |
| 3008 | |
| 3009 | TEEC_CloseSession(&session); |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | * Invoke os_test TA which will invoke attestation PTA to obtain a hash of |
| 3014 | * itself. |
| 3015 | */ |
| 3016 | static void attestation_ta_memory(ADBG_Case_t *c) |
| 3017 | { |
| 3018 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 3019 | uint8_t nonce[6] = { 0xa0, 0x98, 0x76, 0x54, 0x32, 0x10 }; |
| 3020 | uint8_t hash1[TEE_SHA256_HASH_SIZE] = { }; |
| 3021 | uint8_t hash2[TEE_SHA256_HASH_SIZE] = { }; |
| 3022 | uint8_t measurement[TEE_SHA256_HASH_SIZE + ATT_MAX_KEYSZ / 8] = { }; |
| 3023 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 3024 | TEEC_Session session = { }; |
| 3025 | uint32_t ret_orig = 0; |
| 3026 | int cmp = 0; |
| 3027 | |
| 3028 | Do_ADBG_BeginSubCase(c, "Consecutive calls"); |
| 3029 | |
| 3030 | /* Open session to os_test TA */ |
| 3031 | res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 3032 | &ret_orig); |
| 3033 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 3034 | return; |
| 3035 | |
| 3036 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 3037 | TEEC_MEMREF_TEMP_OUTPUT, |
| 3038 | TEEC_NONE, TEEC_NONE); |
| 3039 | op.params[0].tmpref.buffer = nonce; |
| 3040 | op.params[0].tmpref.size = sizeof(nonce); |
| 3041 | op.params[1].tmpref.buffer = measurement; |
| 3042 | op.params[1].tmpref.size = sizeof(measurement); |
| 3043 | |
| 3044 | /* Hash TA */ |
| 3045 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op, |
| 3046 | &ret_orig); |
| 3047 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3048 | |
| 3049 | check_measurement(c, nonce, sizeof(nonce), NULL, measurement); |
| 3050 | memcpy(hash1, measurement, 32); |
| 3051 | |
| 3052 | /* Hash TA again */ |
| 3053 | memset(measurement, 0, sizeof(measurement)); |
| 3054 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op, |
| 3055 | &ret_orig); |
| 3056 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3057 | |
| 3058 | /* New hash should be identical to hash1 */ |
| 3059 | check_measurement(c, nonce, sizeof(nonce), hash1, measurement); |
| 3060 | |
| 3061 | Do_ADBG_EndSubCase(c, "Consecutive calls"); |
| 3062 | |
| 3063 | /* Close TA session, will cause unload of TA */ |
| 3064 | TEEC_CloseSession(&session); |
| 3065 | |
| 3066 | Do_ADBG_BeginSubCase(c, "TA reload"); |
| 3067 | |
| 3068 | /* Load TA again and open a new session */ |
| 3069 | res = xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 3070 | &ret_orig); |
| 3071 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3072 | if (res) |
| 3073 | return; |
| 3074 | |
| 3075 | /* Hash TA one more time */ |
| 3076 | memset(measurement, 0, sizeof(measurement)); |
| 3077 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op, |
| 3078 | &ret_orig); |
| 3079 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3080 | |
| 3081 | /* Hash after reload should still be the same */ |
| 3082 | check_measurement(c, nonce, sizeof(nonce), hash1, measurement); |
| 3083 | |
| 3084 | Do_ADBG_EndSubCase(c, "TA reload"); |
| 3085 | |
| 3086 | Do_ADBG_BeginSubCase(c, "Add shared library"); |
| 3087 | |
| 3088 | /* |
| 3089 | * Invoke a TA command that causes some additional code to be mapped |
| 3090 | * (shared library) |
| 3091 | */ |
| 3092 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL, |
| 3093 | &ret_orig); |
| 3094 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3095 | |
| 3096 | /* Hash TA one last time */ |
| 3097 | memset(measurement, 0, sizeof(measurement)); |
| 3098 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_ATTESTATION, &op, |
| 3099 | &ret_orig); |
| 3100 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 3101 | |
| 3102 | check_measurement(c, nonce, sizeof(nonce), NULL, measurement); |
| 3103 | memcpy(hash2, measurement, 32); |
| 3104 | |
| 3105 | /* Different binaries mapped mean different hashes */ |
| 3106 | cmp = memcmp(hash1, hash2, sizeof(hash1)); |
| 3107 | ADBG_EXPECT_COMPARE_SIGNED(c, cmp, !=, 0); |
| 3108 | |
| 3109 | Do_ADBG_EndSubCase(c, "Add shared library"); |
| 3110 | |
| 3111 | TEEC_CloseSession(&session); |
| 3112 | } |
| 3113 | |
| 3114 | static void xtest_tee_test_1037(ADBG_Case_t *c) |
| 3115 | { |
| 3116 | TEEC_UUID att_uuid = PTA_ATTESTATION_UUID; |
| 3117 | TEEC_Result res = TEEC_ERROR_GENERIC; |
| 3118 | TEEC_Session session = { }; |
| 3119 | uint32_t ret_orig = 0; |
| 3120 | |
| 3121 | res = xtest_teec_open_session(&session, &att_uuid, NULL, &ret_orig); |
| 3122 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 3123 | Do_ADBG_Log(" skip test, pseudo TA not found"); |
| 3124 | return; |
| 3125 | } |
| 3126 | |
chenchaokai | af75193 | 2023-05-29 14:25:50 +0800 | [diff] [blame] | 3127 | TEEC_CloseSession(&session); |
| 3128 | |
Jerome Forissier | 0915b22 | 2021-10-27 18:20:59 +0200 | [diff] [blame] | 3129 | Do_ADBG_BeginSubCase(c, "Get public key"); |
| 3130 | get_att_public_key(c); |
| 3131 | Do_ADBG_EndSubCase(c, "Get public key"); |
| 3132 | |
| 3133 | Do_ADBG_BeginSubCase(c, "TEE attestation"); |
| 3134 | attestation_tee(c); |
| 3135 | Do_ADBG_EndSubCase(c, "TEE attestation"); |
| 3136 | |
| 3137 | Do_ADBG_BeginSubCase(c, "TA attestation (shdr)"); |
| 3138 | attestation_ta_shdr(c); |
| 3139 | Do_ADBG_EndSubCase(c, "TA attestation (shdr)"); |
| 3140 | |
| 3141 | Do_ADBG_BeginSubCase(c, "TA attestation (memory)"); |
| 3142 | attestation_ta_memory(c); |
| 3143 | Do_ADBG_EndSubCase(c, "TA attestation (memory)"); |
| 3144 | |
| 3145 | free_att_key(); |
| 3146 | } |
| 3147 | ADBG_CASE_DEFINE(regression, 1037, xtest_tee_test_1037, |
| 3148 | "Remote attestation"); |
Jens Wiklander | 50339ef | 2022-04-12 20:47:27 +0200 | [diff] [blame] | 3149 | |
| 3150 | static void xtest_tee_test_1038(ADBG_Case_t *c) |
| 3151 | { |
| 3152 | TEEC_Session session = { }; |
| 3153 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 3154 | uint32_t ret_orig = 0; |
| 3155 | TEEC_Result res = TEEC_SUCCESS; |
| 3156 | |
| 3157 | Do_ADBG_BeginSubCase(c, "MTE use after free"); |
| 3158 | |
| 3159 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 3160 | TEEC_NONE); |
| 3161 | |
| 3162 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3163 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 3164 | NULL, &ret_orig))) |
| 3165 | return; |
| 3166 | |
| 3167 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_USE_AFTER_FREE, |
| 3168 | &op, &ret_orig); |
| 3169 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 3170 | Do_ADBG_Log("Binary doesn't support MTE - skip tests"); |
| 3171 | goto out; |
| 3172 | } |
| 3173 | |
| 3174 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res); |
| 3175 | ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 3176 | TEEC_CloseSession(&session); |
| 3177 | |
| 3178 | Do_ADBG_EndSubCase(c, "MTE use after free"); |
| 3179 | |
| 3180 | Do_ADBG_BeginSubCase(c, "MTE invalid tag"); |
| 3181 | |
| 3182 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 3183 | TEEC_NONE); |
| 3184 | |
| 3185 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3186 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 3187 | NULL, &ret_orig))) |
| 3188 | return; |
| 3189 | |
| 3190 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_INVALID_TAG, |
| 3191 | &op, &ret_orig); |
| 3192 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 3193 | Do_ADBG_Log("Binary doesn't support MTE - skip tests"); |
| 3194 | goto out; |
| 3195 | } |
| 3196 | |
| 3197 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res); |
| 3198 | ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 3199 | |
| 3200 | Do_ADBG_EndSubCase(c, "MTE invalid tag"); |
| 3201 | |
| 3202 | Do_ADBG_BeginSubCase(c, "MTE double free"); |
| 3203 | |
| 3204 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 3205 | TEEC_NONE); |
| 3206 | |
| 3207 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3208 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 3209 | NULL, &ret_orig))) |
| 3210 | return; |
| 3211 | |
| 3212 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_DOUBLE_FREE, |
| 3213 | &op, &ret_orig); |
| 3214 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 3215 | Do_ADBG_Log("Binary doesn't support MTE - skip tests"); |
| 3216 | goto out; |
| 3217 | } |
| 3218 | |
| 3219 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res); |
| 3220 | ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 3221 | |
| 3222 | Do_ADBG_EndSubCase(c, "MTE double free"); |
| 3223 | |
| 3224 | Do_ADBG_BeginSubCase(c, "MTE buffer overrun"); |
| 3225 | |
| 3226 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 3227 | TEEC_NONE); |
| 3228 | |
| 3229 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3230 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 3231 | NULL, &ret_orig))) |
| 3232 | return; |
| 3233 | |
| 3234 | res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_MEMTAG_BUFFER_OVERRUN, |
| 3235 | &op, &ret_orig); |
| 3236 | if (res == TEEC_ERROR_NOT_SUPPORTED) { |
| 3237 | Do_ADBG_Log("Binary doesn't support MTE - skip tests"); |
| 3238 | goto out; |
| 3239 | } |
| 3240 | |
| 3241 | ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD, res); |
| 3242 | ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 3243 | |
| 3244 | Do_ADBG_EndSubCase(c, "MTE buffer overrun"); |
| 3245 | |
| 3246 | |
| 3247 | out: |
| 3248 | TEEC_CloseSession(&session); |
| 3249 | } |
| 3250 | ADBG_CASE_DEFINE(regression, 1038, xtest_tee_test_1038, |
| 3251 | "Test MTE (Memory Tag Extension)"); |
Jens Wiklander | dd336da | 2022-09-02 13:57:54 +0200 | [diff] [blame] | 3252 | |
| 3253 | static void xtest_tee_test_1039(ADBG_Case_t *c) |
| 3254 | { |
| 3255 | TEEC_Session session = { }; |
| 3256 | uint32_t ret_orig = 0; |
| 3257 | |
| 3258 | Do_ADBG_BeginSubCase(c, "Load TA with two levels of subkeys"); |
| 3259 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3260 | xtest_teec_open_session(&session, &subkey1_ta_uuid, |
| 3261 | NULL, &ret_orig))) |
| 3262 | TEEC_CloseSession(&session); |
| 3263 | Do_ADBG_EndSubCase(c, "Load TA with two levels of subkeys"); |
| 3264 | |
| 3265 | Do_ADBG_BeginSubCase(c, "Load TA with identity subkey"); |
| 3266 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
| 3267 | xtest_teec_open_session(&session, &subkey2_ta_uuid, |
| 3268 | NULL, &ret_orig))) |
| 3269 | TEEC_CloseSession(&session); |
| 3270 | Do_ADBG_EndSubCase(c, "Load TA with identity subkey"); |
| 3271 | |
| 3272 | } |
Jens Wiklander | dd336da | 2022-09-02 13:57:54 +0200 | [diff] [blame] | 3273 | ADBG_CASE_DEFINE(regression, 1039, xtest_tee_test_1039, |
| 3274 | "Test subkey verification"); |
Jens Wiklander | e1b6445 | 2023-09-01 20:21:16 +0200 | [diff] [blame^] | 3275 | |
| 3276 | struct test_1040_thread_arg { |
| 3277 | TEEC_Result res; |
| 3278 | pthread_t thr; |
| 3279 | }; |
| 3280 | |
| 3281 | static void *test_1040_thread(void *arg) |
| 3282 | { |
| 3283 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 3284 | struct test_1040_thread_arg *a = arg; |
| 3285 | TEEC_Result res = TEEC_SUCCESS; |
| 3286 | uint32_t err_orig = 0; |
| 3287 | TEEC_Session session = { }; |
| 3288 | size_t loop_count = 100; |
| 3289 | size_t n = 0; |
| 3290 | |
| 3291 | if (level == 0) |
| 3292 | loop_count /= 2; |
| 3293 | |
| 3294 | while (n < loop_count) { |
| 3295 | res = xtest_teec_open_session(&session, &sims_test_ta_uuid, |
| 3296 | NULL, &err_orig); |
| 3297 | if (res) { |
| 3298 | if (res == TEEC_ERROR_TARGET_DEAD) |
| 3299 | continue; |
| 3300 | a->res = res; |
| 3301 | return NULL; |
| 3302 | } |
| 3303 | |
| 3304 | memset(&op, 0, sizeof(op)); |
| 3305 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 3306 | TEEC_NONE, TEEC_NONE, |
| 3307 | TEEC_NONE); |
| 3308 | res = TEEC_InvokeCommand(&session, TA_SIMS_CMD_PANIC, &op, |
| 3309 | &err_orig); |
| 3310 | TEEC_CloseSession(&session); |
| 3311 | if (res != TEEC_ERROR_TARGET_DEAD) { |
| 3312 | if (res) |
| 3313 | a->res = res; |
| 3314 | else |
| 3315 | a->res = TEEC_ERROR_GENERIC; |
| 3316 | return NULL; |
| 3317 | } |
| 3318 | n++; |
| 3319 | } |
| 3320 | a->res = TEEC_SUCCESS; |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | |
| 3324 | static void xtest_tee_test_1040(ADBG_Case_t *c) |
| 3325 | { |
| 3326 | struct test_1040_thread_arg arg[NUM_THREADS] = { }; |
| 3327 | size_t nt = NUM_THREADS; |
| 3328 | size_t n = 0; |
| 3329 | |
| 3330 | Do_ADBG_BeginSubCase(c, "Concurent invoke with panic in TA"); |
| 3331 | for (n = 0; n < nt; n++) { |
| 3332 | if (!ADBG_EXPECT(c, 0, pthread_create(&arg[n].thr, NULL, |
| 3333 | test_1040_thread, |
| 3334 | arg + n))) |
| 3335 | nt = n; /* break loop and start cleanup */ |
| 3336 | } |
| 3337 | for (n = 0; n < nt; n++) { |
| 3338 | ADBG_EXPECT(c, 0, pthread_join(arg[n].thr, NULL)); |
| 3339 | ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res); |
| 3340 | } |
| 3341 | Do_ADBG_EndSubCase(c, "Concurent invoke with panic in TA"); |
| 3342 | } |
| 3343 | ADBG_CASE_DEFINE(regression, 1040, xtest_tee_test_1040, |
| 3344 | "Test panic in concurrent open/invoke/close session"); |