xref: /optee_os/core/kernel/ree_fs_ta.c (revision 4edd96e6d7a7228e907cf498b23e5b5fbdaf39a0)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017, 2019, Linaro Limited
4  * Copyright (c) 2020, Arm Limited.
5  */
6 
7 /*
8  * Security properties of REE-FS TAs
9  * =================================
10  *
11  * Authentication only
12  * -------------------
13  *
14  * Required security properties:
15  * 1. Authentication and non-repudiation of a TA to Service Provider (SP).
16  * 2. Integrity of a TA.
17  *
18  * To satisfy (1) and (2), SP needs to sign TA and OP-TEE core needs to verify
19  * the signature using SP public key with computed hash of the TA.
20  *
21  * Authentication along with Confidentiality
22  * -----------------------------------------
23  *
24  * Required security properties:
25  * 1. Authentication and non-repudiation of a TA to Service Provider (SP).
26  * 2. Confidentiality of a TA.
27  * 3. Integrity of an encrypted TA blob.
28  *
29  * To satisfy (1), SP needs to sign plain TA and OP-TEE core needs to verify the
30  * signature using SP public key with computed hash of the TA.
31  *
32  * To satisfy (2) and (3), SP needs to do authenticated encryption of TA and
33  * OP-TEE core needs to do authenticated decryption of TA to retrieve its
34  * contents. Here encryption provides the confidentiality of TA and MAC tag
35  * provides the integrity of encrypted TA blob.
36  */
37 
38 #include <assert.h>
39 #include <crypto/crypto.h>
40 #include <fault_mitigation.h>
41 #include <initcall.h>
42 #include <kernel/thread.h>
43 #include <kernel/ts_store.h>
44 #include <mm/core_memprot.h>
45 #include <mm/mobj.h>
46 #include <mm/tee_mm.h>
47 #include <optee_rpc_cmd.h>
48 #include <signed_hdr.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <tee_api_defines_extensions.h>
52 #include <tee_api_types.h>
53 #include <tee/tee_pobj.h>
54 #include <tee/tee_svc_storage.h>
55 #include <tee/tee_ta_enc_manager.h>
56 #include <tee/uuid.h>
57 #include <utee_defines.h>
58 
59 struct ree_fs_ta_handle {
60 	struct shdr *nw_ta; /* Non-secure (shared memory) */
61 	size_t nw_ta_size;
62 	struct mobj *mobj;
63 	size_t offs;
64 	struct shdr *shdr; /* Verified secure copy of @nw_ta's signed header */
65 	void *hash_ctx;
66 	void *enc_ctx;
67 	struct shdr_bootstrap_ta *bs_hdr;
68 	struct shdr_encrypted_ta *ehdr;
69 };
70 
71 struct ver_db_entry {
72 	uint8_t uuid[sizeof(TEE_UUID)];
73 	uint32_t version;
74 };
75 
76 struct ver_db_hdr {
77 	uint32_t db_version;
78 	uint32_t nb_entries;
79 };
80 
81 static const char ta_ver_db[] = "ta_ver.db";
82 static const char subkey_ver_db[] = "subkey_ver.db";
83 static struct mutex ver_db_mutex = MUTEX_INITIALIZER;
84 
85 static TEE_Result check_update_version(const char *db_name,
86 				       const uint8_t uuid[sizeof(TEE_UUID)],
87 				       uint32_t version)
88 {
89 	struct ver_db_entry db_entry = { };
90 	const struct tee_file_operations *ops = NULL;
91 	struct tee_file_handle *fh = NULL;
92 	TEE_Result res = TEE_SUCCESS;
93 	bool entry_found = false;
94 	size_t len = 0;
95 	unsigned int i = 0;
96 	struct ver_db_hdr db_hdr = { };
97 	struct tee_pobj pobj = {
98 		.obj_id = (void *)db_name,
99 		.obj_id_len = strlen(db_name) + 1,
100 	};
101 
102 	ops = tee_svc_storage_file_ops(TEE_STORAGE_PRIVATE);
103 	if (!ops)
104 		return TEE_SUCCESS; /* Compiled with no secure storage */
105 
106 	mutex_lock(&ver_db_mutex);
107 
108 	res = ops->open(&pobj, NULL, &fh);
109 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
110 		goto out;
111 
112 	if (res == TEE_ERROR_ITEM_NOT_FOUND) {
113 		res = ops->create(&pobj, false, NULL, 0, NULL, 0, NULL, NULL,
114 				   0, &fh);
115 		if (res != TEE_SUCCESS)
116 			goto out;
117 
118 		res = ops->write(fh, 0, &db_hdr, NULL, sizeof(db_hdr));
119 		if (res != TEE_SUCCESS)
120 			goto out;
121 	} else {
122 		len = sizeof(db_hdr);
123 
124 		res = ops->read(fh, 0, &db_hdr, NULL, &len);
125 		if (res != TEE_SUCCESS) {
126 			goto out;
127 		} else if (len != sizeof(db_hdr)) {
128 			res = TEE_ERROR_BAD_STATE;
129 			goto out;
130 		}
131 	}
132 
133 	for (i = 0; i < db_hdr.nb_entries; i++) {
134 		len = sizeof(db_entry);
135 
136 		res = ops->read(fh, sizeof(db_hdr) + (i * len), &db_entry,
137 				NULL, &len);
138 		if (res != TEE_SUCCESS) {
139 			goto out;
140 		} else if (len != sizeof(db_entry)) {
141 			res = TEE_ERROR_BAD_STATE;
142 			goto out;
143 		}
144 
145 		if (!memcmp(uuid, db_entry.uuid, sizeof(TEE_UUID))) {
146 			entry_found = true;
147 			break;
148 		}
149 	}
150 
151 	if (entry_found) {
152 		if (db_entry.version > version) {
153 			res = TEE_ERROR_ACCESS_CONFLICT;
154 			goto out;
155 		} else if (db_entry.version < version) {
156 			memcpy(db_entry.uuid, uuid, sizeof(TEE_UUID));
157 			db_entry.version = version;
158 			len = sizeof(db_entry);
159 			res = ops->write(fh, sizeof(db_hdr) + (i * len),
160 					 &db_entry, NULL, len);
161 			if (res != TEE_SUCCESS)
162 				goto out;
163 		}
164 	} else {
165 		memcpy(db_entry.uuid, uuid, sizeof(TEE_UUID));
166 		db_entry.version = version;
167 		len = sizeof(db_entry);
168 		res = ops->write(fh, sizeof(db_hdr) + (db_hdr.nb_entries * len),
169 				 &db_entry, NULL, len);
170 		if (res != TEE_SUCCESS)
171 			goto out;
172 
173 		db_hdr.nb_entries++;
174 		res = ops->write(fh, 0, &db_hdr, NULL, sizeof(db_hdr));
175 		if (res != TEE_SUCCESS)
176 			goto out;
177 	}
178 
179 out:
180 	ops->close(&fh);
181 	mutex_unlock(&ver_db_mutex);
182 	return res;
183 }
184 
185 /*
186  * Load a TA via RPC with UUID defined by input param @uuid. The virtual
187  * address of the raw TA binary is received in out parameter @ta.
188  */
189 static TEE_Result rpc_load(const TEE_UUID *uuid, struct shdr **ta,
190 			   size_t *ta_size, struct mobj **mobj)
191 {
192 	TEE_Result res;
193 	struct thread_param params[2];
194 
195 	if (!uuid || !ta || !mobj || !ta_size)
196 		return TEE_ERROR_BAD_PARAMETERS;
197 
198 	memset(params, 0, sizeof(params));
199 	params[0].attr = THREAD_PARAM_ATTR_VALUE_IN;
200 	tee_uuid_to_octets((void *)&params[0].u.value, uuid);
201 	params[1].attr = THREAD_PARAM_ATTR_MEMREF_OUT;
202 
203 	res = thread_rpc_cmd(OPTEE_RPC_CMD_LOAD_TA, 2, params);
204 	if (res != TEE_SUCCESS)
205 		return res;
206 
207 	*mobj = thread_rpc_alloc_payload(params[1].u.memref.size);
208 	if (!*mobj)
209 		return TEE_ERROR_OUT_OF_MEMORY;
210 
211 	*ta = mobj_get_va(*mobj, 0, params[1].u.memref.size);
212 	if (!*ta) {
213 		res = TEE_ERROR_SHORT_BUFFER;
214 		goto exit;
215 	}
216 	/* We don't expect NULL as thread_rpc_alloc_payload() was successful */
217 	assert(*ta);
218 	*ta_size = params[1].u.memref.size;
219 
220 	params[0].attr = THREAD_PARAM_ATTR_VALUE_IN;
221 	tee_uuid_to_octets((void *)&params[0].u.value, uuid);
222 	params[1].attr = THREAD_PARAM_ATTR_MEMREF_OUT;
223 	params[1].u.memref.offs = 0;
224 	params[1].u.memref.mobj = *mobj;
225 
226 	res = thread_rpc_cmd(OPTEE_RPC_CMD_LOAD_TA, 2, params);
227 exit:
228 	if (res != TEE_SUCCESS)
229 		thread_rpc_free_payload(*mobj);
230 
231 	return res;
232 }
233 
234 static TEE_Result ree_fs_ta_open(const TEE_UUID *uuid,
235 				 struct ts_store_handle **h)
236 {
237 	uint8_t next_uuid[sizeof(TEE_UUID)] = { };
238 	struct ree_fs_ta_handle *handle;
239 	uint8_t *next_uuid_ptr = NULL;
240 	struct shdr *shdr = NULL;
241 	struct mobj *mobj = NULL;
242 	void *hash_ctx = NULL;
243 	struct shdr *ta = NULL;
244 	size_t ta_size = 0;
245 	TEE_Result res = TEE_SUCCESS;
246 	size_t offs = 0;
247 	struct shdr_bootstrap_ta *bs_hdr = NULL;
248 	struct shdr_encrypted_ta *ehdr = NULL;
249 	size_t shdr_sz = 0;
250 	uint32_t max_depth = UINT32_MAX;
251 	struct ftmn ftmn = { };
252 	unsigned int incr0_count = 0;
253 
254 	handle = calloc(1, sizeof(*handle));
255 	if (!handle)
256 		return TEE_ERROR_OUT_OF_MEMORY;
257 
258 	/* Request TA from tee-supplicant */
259 	res = rpc_load(uuid, &ta, &ta_size, &mobj);
260 	if (res != TEE_SUCCESS)
261 		goto error;
262 
263 	/* Make secure copy of signed header */
264 	shdr = shdr_alloc_and_copy(0, ta, ta_size);
265 	if (!shdr) {
266 		res = TEE_ERROR_SECURITY;
267 		goto error_free_payload;
268 	}
269 
270 	/* Validate header signature */
271 	FTMN_CALL_FUNC(res, &ftmn, FTMN_INCR0, shdr_verify_signature, shdr);
272 	incr0_count++;
273 	if (res != TEE_SUCCESS)
274 		goto error_free_payload;
275 
276 	shdr_sz = SHDR_GET_SIZE(shdr);
277 	if (!shdr_sz) {
278 		res = TEE_ERROR_SECURITY;
279 		goto error_free_payload;
280 	}
281 	offs = shdr_sz;
282 
283 	while (shdr->img_type == SHDR_SUBKEY) {
284 		struct shdr_pub_key pub_key = { };
285 
286 		if (offs > ta_size) {
287 			res = TEE_ERROR_SECURITY;
288 			goto error_free_payload;
289 		}
290 
291 		res = shdr_load_pub_key(shdr, offs, (const void *)ta,
292 					ta_size, next_uuid_ptr, max_depth,
293 					&pub_key);
294 		if (res)
295 			goto error_free_payload;
296 
297 		if (ADD_OVERFLOW(offs, shdr->img_size, &offs) ||
298 		    ADD_OVERFLOW(offs, pub_key.name_size, &offs) ||
299 		    offs > ta_size) {
300 			res = TEE_ERROR_SECURITY;
301 			goto error_free_payload;
302 		}
303 		max_depth = pub_key.max_depth;
304 		memcpy(next_uuid, pub_key.next_uuid, sizeof(TEE_UUID));
305 		next_uuid_ptr = next_uuid;
306 
307 		res = check_update_version(subkey_ver_db, pub_key.uuid,
308 					   pub_key.version);
309 		if (res) {
310 			res = TEE_ERROR_SECURITY;
311 			shdr_free_pub_key(&pub_key);
312 			goto error_free_payload;
313 		}
314 
315 		shdr_free(shdr);
316 		shdr = shdr_alloc_and_copy(offs, ta, ta_size);
317 		res = TEE_ERROR_SECURITY;
318 		if (shdr) {
319 			FTMN_CALL_FUNC(res, &ftmn, FTMN_INCR0,
320 				       shdr_verify_signature2, &pub_key, shdr);
321 			incr0_count++;
322 		}
323 		shdr_free_pub_key(&pub_key);
324 		if (res)
325 			goto error_free_payload;
326 
327 		shdr_sz = SHDR_GET_SIZE(shdr);
328 		if (!shdr_sz) {
329 			res = TEE_ERROR_SECURITY;
330 			goto error_free_payload;
331 		}
332 		offs += shdr_sz;
333 		if (offs > ta_size) {
334 			res = TEE_ERROR_SECURITY;
335 			goto error_free_payload;
336 		}
337 	}
338 
339 	if (shdr->img_type != SHDR_TA && shdr->img_type != SHDR_BOOTSTRAP_TA &&
340 	    shdr->img_type != SHDR_ENCRYPTED_TA) {
341 		res = TEE_ERROR_SECURITY;
342 		goto error_free_payload;
343 	}
344 
345 	/*
346 	 * If we're verifying this TA using a subkey, make sure that
347 	 * the UUID of the TA belongs to the namespace defined by the subkey.
348 	 * The namespace is defined as in RFC4122, that is, valid UUID
349 	 * is calculated as a V5 UUID SHA-512(subkey UUID, "name string").
350 	 */
351 	if (next_uuid_ptr) {
352 		TEE_UUID check_uuid = { };
353 
354 		tee_uuid_from_octets(&check_uuid, next_uuid_ptr);
355 		if (memcmp(&check_uuid, uuid, sizeof(*uuid))) {
356 			res = TEE_ERROR_SECURITY;
357 			goto error_free_payload;
358 		}
359 	}
360 
361 	/*
362 	 * Initialize a hash context and run the algorithm over the signed
363 	 * header (less the final file hash and its signature of course)
364 	 */
365 	res = crypto_hash_alloc_ctx(&hash_ctx,
366 				    TEE_DIGEST_HASH_TO_ALGO(shdr->algo));
367 	if (res != TEE_SUCCESS)
368 		goto error_free_payload;
369 	res = crypto_hash_init(hash_ctx);
370 	if (res != TEE_SUCCESS)
371 		goto error_free_hash;
372 	res = crypto_hash_update(hash_ctx, (uint8_t *)shdr, sizeof(*shdr));
373 	if (res != TEE_SUCCESS)
374 		goto error_free_hash;
375 
376 	if (shdr->img_type == SHDR_BOOTSTRAP_TA ||
377 	    shdr->img_type == SHDR_ENCRYPTED_TA) {
378 		TEE_UUID bs_uuid = { };
379 		size_t sz = shdr_sz;
380 
381 		if (ADD_OVERFLOW(sz, sizeof(*bs_hdr), &sz) || ta_size < sz) {
382 			res = TEE_ERROR_SECURITY;
383 			goto error_free_hash;
384 		}
385 
386 		bs_hdr = malloc(sizeof(*bs_hdr));
387 		if (!bs_hdr) {
388 			res = TEE_ERROR_OUT_OF_MEMORY;
389 			goto error_free_hash;
390 		}
391 
392 		memcpy(bs_hdr, (uint8_t *)ta + offs, sizeof(*bs_hdr));
393 
394 		/*
395 		 * There's a check later that the UUID embedded inside the
396 		 * ELF is matching, but since we now have easy access to
397 		 * the expected uuid of the TA we check it a bit earlier
398 		 * here.
399 		 */
400 		tee_uuid_from_octets(&bs_uuid, bs_hdr->uuid);
401 		if (memcmp(&bs_uuid, uuid, sizeof(TEE_UUID))) {
402 			res = TEE_ERROR_SECURITY;
403 			goto error_free_hash;
404 		}
405 
406 		res = crypto_hash_update(hash_ctx, (uint8_t *)bs_hdr,
407 					 sizeof(*bs_hdr));
408 		if (res != TEE_SUCCESS)
409 			goto error_free_hash;
410 		offs += sizeof(*bs_hdr);
411 		handle->bs_hdr = bs_hdr;
412 	}
413 
414 	if (shdr->img_type == SHDR_ENCRYPTED_TA) {
415 		struct shdr_encrypted_ta img_ehdr = { };
416 		size_t sz = shdr_sz;
417 		size_t ehdr_sz = 0;
418 
419 		if (ADD_OVERFLOW(sz, sizeof(struct shdr_bootstrap_ta), &sz) ||
420 		    ADD_OVERFLOW(sz, sizeof(img_ehdr), &sz) ||
421 		    ta_size < sz) {
422 			res = TEE_ERROR_SECURITY;
423 			goto error_free_hash;
424 		}
425 
426 		memcpy(&img_ehdr, ((uint8_t *)ta + offs), sizeof(img_ehdr));
427 		ehdr_sz = SHDR_ENC_GET_SIZE(&img_ehdr);
428 		sz -= sizeof(img_ehdr);
429 		if (!ehdr_sz || ADD_OVERFLOW(sz, ehdr_sz, &sz) ||
430 		    ta_size < sz) {
431 			res = TEE_ERROR_SECURITY;
432 			goto error_free_hash;
433 		}
434 
435 		/*
436 		 * This is checked further down too, but we must sanity
437 		 * check shdr->img_size before it's used below.
438 		 */
439 		if (ta_size != offs + ehdr_sz + shdr->img_size) {
440 			res = TEE_ERROR_SECURITY;
441 			goto error_free_hash;
442 		}
443 
444 		ehdr = malloc(ehdr_sz);
445 		if (!ehdr) {
446 			res = TEE_ERROR_OUT_OF_MEMORY;
447 			goto error_free_hash;
448 		}
449 
450 		*ehdr = img_ehdr;
451 		memcpy((uint8_t *)ehdr + sizeof(img_ehdr),
452 		       (uint8_t *)ta + offs + sizeof(img_ehdr),
453 		       ehdr_sz - sizeof(img_ehdr));
454 
455 		res = crypto_hash_update(hash_ctx, (uint8_t *)ehdr, ehdr_sz);
456 		if (res != TEE_SUCCESS)
457 			goto error_free_hash;
458 
459 		res = tee_ta_decrypt_init(&handle->enc_ctx, ehdr,
460 					  shdr->img_size);
461 		if (res != TEE_SUCCESS)
462 			goto error_free_hash;
463 
464 		offs += ehdr_sz;
465 		handle->ehdr = ehdr;
466 	}
467 
468 	if (ta_size != offs + shdr->img_size) {
469 		res = TEE_ERROR_SECURITY;
470 		goto error_free_hash;
471 	}
472 
473 	handle->nw_ta = ta;
474 	handle->nw_ta_size = ta_size;
475 	handle->offs = offs;
476 	handle->hash_ctx = hash_ctx;
477 	handle->shdr = shdr;
478 	handle->mobj = mobj;
479 	*h = (struct ts_store_handle *)handle;
480 	FTMN_CALLEE_DONE_CHECK(&ftmn, FTMN_INCR1,
481 			       FTMN_STEP_COUNT(incr0_count), TEE_SUCCESS);
482 	return TEE_SUCCESS;
483 
484 error_free_hash:
485 	crypto_hash_free_ctx(hash_ctx);
486 error_free_payload:
487 	thread_rpc_free_payload(mobj);
488 error:
489 	free(ehdr);
490 	free(bs_hdr);
491 	shdr_free(shdr);
492 	free(handle);
493 	FTMN_SET_CHECK_RES_NOT_ZERO(&ftmn, FTMN_INCR1, res);
494 	FTMN_CALLEE_DONE_CHECK(&ftmn, FTMN_INCR1,
495 			       FTMN_STEP_COUNT(incr0_count, 1), res);
496 	return res;
497 }
498 
499 static TEE_Result ree_fs_ta_get_size(const struct ts_store_handle *h,
500 				     size_t *size)
501 {
502 	struct ree_fs_ta_handle *handle = (struct ree_fs_ta_handle *)h;
503 
504 	*size = handle->shdr->img_size;
505 	return TEE_SUCCESS;
506 }
507 
508 static TEE_Result ree_fs_ta_get_tag(const struct ts_store_handle *h,
509 				    uint8_t *tag, unsigned int *tag_len)
510 {
511 	struct ree_fs_ta_handle *handle = (struct ree_fs_ta_handle *)h;
512 
513 	if (!tag || *tag_len < handle->shdr->hash_size) {
514 		*tag_len = handle->shdr->hash_size;
515 		return TEE_ERROR_SHORT_BUFFER;
516 	}
517 	*tag_len = handle->shdr->hash_size;
518 
519 	memcpy(tag, SHDR_GET_HASH(handle->shdr), handle->shdr->hash_size);
520 
521 	return TEE_SUCCESS;
522 }
523 
524 static TEE_Result check_digest(struct ree_fs_ta_handle *h)
525 {
526 	void *digest = NULL;
527 	TEE_Result res;
528 
529 	digest = malloc(h->shdr->hash_size);
530 	if (!digest)
531 		return TEE_ERROR_OUT_OF_MEMORY;
532 	res = crypto_hash_final(h->hash_ctx, digest, h->shdr->hash_size);
533 	if (res != TEE_SUCCESS) {
534 		res = TEE_ERROR_SECURITY;
535 		goto out;
536 	}
537 	if (FTMN_CALLEE_DONE_MEMCMP(memcmp, digest, SHDR_GET_HASH(h->shdr),
538 				    h->shdr->hash_size))
539 		res = TEE_ERROR_SECURITY;
540 out:
541 	free(digest);
542 	return res;
543 }
544 
545 static TEE_Result ree_fs_ta_read(struct ts_store_handle *h, void *data,
546 				 size_t len)
547 {
548 	struct ree_fs_ta_handle *handle = (struct ree_fs_ta_handle *)h;
549 
550 	uint8_t *src = (uint8_t *)handle->nw_ta + handle->offs;
551 	size_t next_offs = 0;
552 	uint8_t *dst = src;
553 	TEE_Result res = TEE_SUCCESS;
554 
555 	if (ADD_OVERFLOW(handle->offs, len, &next_offs) ||
556 	    next_offs > handle->nw_ta_size)
557 		return TEE_ERROR_BAD_PARAMETERS;
558 
559 	if (handle->shdr->img_type == SHDR_ENCRYPTED_TA) {
560 		if (data) {
561 			dst = data; /* Hash secure buffer */
562 			res = tee_ta_decrypt_update(handle->enc_ctx, dst, src,
563 						    len);
564 			if (res != TEE_SUCCESS)
565 				return TEE_ERROR_SECURITY;
566 		} else {
567 			size_t num_bytes = 0;
568 			size_t b_size = MIN(1024U, len);
569 			uint8_t *b = malloc(b_size);
570 
571 			if (!b)
572 				return TEE_ERROR_OUT_OF_MEMORY;
573 
574 			dst = NULL;
575 			while (num_bytes < len) {
576 				size_t n = MIN(b_size, len - num_bytes);
577 
578 				res = tee_ta_decrypt_update(handle->enc_ctx, b,
579 							    src + num_bytes, n);
580 				if (res)
581 					break;
582 				num_bytes += n;
583 
584 				res = crypto_hash_update(handle->hash_ctx, b,
585 							 n);
586 				if (res)
587 					break;
588 			}
589 
590 			free(b);
591 			if (res != TEE_SUCCESS)
592 				return TEE_ERROR_SECURITY;
593 		}
594 	} else if (data) {
595 		dst = data; /* Hash secure buffer (shm might be modified) */
596 		memcpy(dst, src, len);
597 	}
598 
599 	if (dst) {
600 		res = crypto_hash_update(handle->hash_ctx, dst, len);
601 		if (res != TEE_SUCCESS)
602 			return TEE_ERROR_SECURITY;
603 	}
604 
605 	handle->offs = next_offs;
606 	if (handle->offs == handle->nw_ta_size) {
607 		if (handle->shdr->img_type == SHDR_ENCRYPTED_TA) {
608 			/*
609 			 * Last read: time to finalize authenticated
610 			 * decryption.
611 			 */
612 			res = tee_ta_decrypt_final(handle->enc_ctx,
613 						   handle->ehdr, NULL, NULL, 0);
614 			if (res != TEE_SUCCESS)
615 				return TEE_ERROR_SECURITY;
616 		}
617 		/*
618 		 * Last read: time to check if our digest matches the expected
619 		 * one (from the signed header)
620 		 */
621 		res = check_digest(handle);
622 		if (res != TEE_SUCCESS)
623 			return res;
624 
625 		if (handle->bs_hdr)
626 			res = check_update_version(ta_ver_db,
627 						   handle->bs_hdr->uuid,
628 						   handle->bs_hdr->ta_version);
629 	}
630 	return res;
631 }
632 
633 static void ree_fs_ta_close(struct ts_store_handle *h)
634 {
635 	struct ree_fs_ta_handle *handle = (struct ree_fs_ta_handle *)h;
636 
637 	if (!handle)
638 		return;
639 	thread_rpc_free_payload(handle->mobj);
640 	crypto_hash_free_ctx(handle->hash_ctx);
641 	free(handle->shdr);
642 	free(handle->ehdr);
643 	free(handle->bs_hdr);
644 	free(handle);
645 }
646 
647 #ifndef CFG_REE_FS_TA_BUFFERED
648 REGISTER_TA_STORE(9) = {
649 	.description = "REE",
650 	.open = ree_fs_ta_open,
651 	.get_size = ree_fs_ta_get_size,
652 	.get_tag = ree_fs_ta_get_tag,
653 	.read = ree_fs_ta_read,
654 	.close = ree_fs_ta_close,
655 };
656 #endif
657 
658 #ifdef CFG_REE_FS_TA_BUFFERED
659 
660 /*
661  * This is a wrapper around the "REE FS" TA store.
662  * The whole TA/library is read into a temporary buffer during .open(). This
663  * allows the binary to be authenticated before any data is read and processed
664  * by the upper layer (ELF loader).
665  */
666 
667 struct buf_ree_fs_ta_handle {
668 	struct ts_store_handle *h; /* Note: a REE FS TA store handle */
669 	size_t ta_size;
670 	tee_mm_entry_t *mm;
671 	uint8_t *buf;
672 	size_t offs;
673 	uint8_t *tag;
674 	unsigned int tag_len;
675 };
676 
677 static TEE_Result buf_ta_open(const TEE_UUID *uuid,
678 			      struct ts_store_handle **h)
679 {
680 	struct buf_ree_fs_ta_handle *handle = NULL;
681 	struct ftmn ftmn = { };
682 	TEE_Result res = TEE_SUCCESS;
683 
684 	handle = calloc(1, sizeof(*handle));
685 	if (!handle)
686 		return TEE_ERROR_OUT_OF_MEMORY;
687 	FTMN_PUSH_LINKED_CALL(&ftmn, FTMN_FUNC_HASH("ree_fs_ta_open"));
688 	res = ree_fs_ta_open(uuid, &handle->h);
689 	if (!res)
690 		FTMN_SET_CHECK_RES_FROM_CALL(&ftmn, FTMN_INCR0, res);
691 	FTMN_POP_LINKED_CALL(&ftmn);
692 	if (res)
693 		goto err_free_handle;
694 	ftmn_checkpoint(&ftmn, FTMN_INCR1);
695 
696 	res = ree_fs_ta_get_size(handle->h, &handle->ta_size);
697 	if (res)
698 		goto err;
699 
700 	res = ree_fs_ta_get_tag(handle->h, NULL, &handle->tag_len);
701 	if (res != TEE_ERROR_SHORT_BUFFER) {
702 		res = TEE_ERROR_GENERIC;
703 		goto err;
704 	}
705 	handle->tag = malloc(handle->tag_len);
706 	if (!handle->tag) {
707 		res = TEE_ERROR_OUT_OF_MEMORY;
708 		goto err;
709 	}
710 	res = ree_fs_ta_get_tag(handle->h, handle->tag, &handle->tag_len);
711 	if (res)
712 		goto err;
713 
714 	handle->mm = tee_mm_alloc(&tee_mm_sec_ddr, handle->ta_size);
715 	if (!handle->mm) {
716 		res = TEE_ERROR_OUT_OF_MEMORY;
717 		goto err;
718 	}
719 	handle->buf = phys_to_virt(tee_mm_get_smem(handle->mm),
720 				   MEM_AREA_TA_RAM, handle->ta_size);
721 	if (!handle->buf) {
722 		res = TEE_ERROR_OUT_OF_MEMORY;
723 		goto err;
724 	}
725 
726 	FTMN_PUSH_LINKED_CALL(&ftmn, FTMN_FUNC_HASH("check_digest"));
727 	res = ree_fs_ta_read(handle->h, handle->buf, handle->ta_size);
728 	if (!res)
729 		FTMN_SET_CHECK_RES_FROM_CALL(&ftmn, FTMN_INCR0, res);
730 	FTMN_POP_LINKED_CALL(&ftmn);
731 	if (res)
732 		goto err;
733 	ftmn_checkpoint(&ftmn, FTMN_INCR1);
734 
735 	*h = (struct ts_store_handle *)handle;
736 	ree_fs_ta_close(handle->h);
737 	return ftmn_return_res(&ftmn, FTMN_STEP_COUNT(2, 2), TEE_SUCCESS);
738 
739 err:
740 	ree_fs_ta_close(handle->h);
741 	tee_mm_free(handle->mm);
742 	free(handle->tag);
743 err_free_handle:
744 	free(handle);
745 	return res;
746 }
747 
748 static TEE_Result buf_ta_get_size(const struct ts_store_handle *h,
749 				  size_t *size)
750 {
751 	struct buf_ree_fs_ta_handle *handle = (struct buf_ree_fs_ta_handle *)h;
752 
753 	*size = handle->ta_size;
754 	return TEE_SUCCESS;
755 }
756 
757 static TEE_Result buf_ta_read(struct ts_store_handle *h, void *data,
758 			      size_t len)
759 {
760 	struct buf_ree_fs_ta_handle *handle = (struct buf_ree_fs_ta_handle *)h;
761 	uint8_t *src = handle->buf + handle->offs;
762 	size_t next_offs = 0;
763 
764 	if (ADD_OVERFLOW(handle->offs, len, &next_offs) ||
765 	    next_offs > handle->ta_size)
766 		return TEE_ERROR_BAD_PARAMETERS;
767 
768 	if (data)
769 		memcpy(data, src, len);
770 	handle->offs = next_offs;
771 	return TEE_SUCCESS;
772 }
773 
774 static TEE_Result buf_ta_get_tag(const struct ts_store_handle *h,
775 				 uint8_t *tag, unsigned int *tag_len)
776 {
777 	struct buf_ree_fs_ta_handle *handle = (struct buf_ree_fs_ta_handle *)h;
778 
779 	*tag_len = handle->tag_len;
780 	if (!tag || *tag_len < handle->tag_len)
781 		return TEE_ERROR_SHORT_BUFFER;
782 
783 	memcpy(tag, handle->tag, handle->tag_len);
784 
785 	return TEE_SUCCESS;
786 }
787 
788 static void buf_ta_close(struct ts_store_handle *h)
789 {
790 	struct buf_ree_fs_ta_handle *handle = (struct buf_ree_fs_ta_handle *)h;
791 
792 	if (!handle)
793 		return;
794 	tee_mm_free(handle->mm);
795 	free(handle->tag);
796 	free(handle);
797 }
798 
799 REGISTER_TA_STORE(9) = {
800 	.description = "REE [buffered]",
801 	.open = buf_ta_open,
802 	.get_size = buf_ta_get_size,
803 	.get_tag = buf_ta_get_tag,
804 	.read = buf_ta_read,
805 	.close = buf_ta_close,
806 };
807 
808 #endif /* CFG_REE_FS_TA_BUFFERED */
809