xref: /optee_os/core/tee/tee_svc.c (revision 4edd96e6d7a7228e907cf498b23e5b5fbdaf39a0)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020-2022 Linaro Limited
5  */
6 
7 #include <compiler.h>
8 #include <kernel/chip_services.h>
9 #include <kernel/pseudo_ta.h>
10 #include <kernel/tee_common.h>
11 #include <kernel/tee_common_otp.h>
12 #include <kernel/tee_ta_manager.h>
13 #include <kernel/tee_time.h>
14 #include <kernel/trace_ta.h>
15 #include <kernel/user_access.h>
16 #include <memtag.h>
17 #include <mm/core_memprot.h>
18 #include <mm/mobj.h>
19 #include <mm/tee_mm.h>
20 #include <mm/vm.h>
21 #include <stdlib_ext.h>
22 #include <tee_api_types.h>
23 #include <tee/tee_cryp_utl.h>
24 #include <tee/tee_svc.h>
25 #include <trace.h>
26 #include <user_ta_header.h>
27 #include <utee_types.h>
28 #include <util.h>
29 
30 vaddr_t tee_svc_uref_base;
31 
32 void syscall_log(const void *buf, size_t len)
33 {
34 	if (IS_ENABLED(CFG_TEE_CORE_TA_TRACE)) {
35 		char *kbuf = NULL;
36 		size_t sz = 0;
37 
38 		if (!len || ADD_OVERFLOW(len, 1, &sz))
39 			return;
40 
41 		kbuf = malloc(sz);
42 		if (!kbuf)
43 			return;
44 
45 		if (copy_from_user(kbuf, buf, len) == TEE_SUCCESS) {
46 			kbuf[len] = '\0';
47 			trace_ext_puts(kbuf);
48 		}
49 
50 		free_wipe(kbuf);
51 	}
52 }
53 
54 TEE_Result syscall_not_supported(void)
55 {
56 	return TEE_ERROR_NOT_SUPPORTED;
57 }
58 
59 /* Configuration properties */
60 /* API implementation version */
61 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
62 
63 /* Implementation description (implementation-dependent) */
64 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
65 
66 /*
67  * TA persistent time protection level
68  * 100: Persistent time based on an REE-controlled real-time clock
69  * and on the TEE Trusted Storage for the storage of origins (default).
70  * 1000: Persistent time based on a TEE-controlled real-time clock
71  * and the TEE Trusted Storage.
72  * The real-time clock MUST be out of reach of software attacks
73  * from the REE.
74  */
75 static const uint32_t ta_time_prot_lvl = 100;
76 
77 /* Elliptic Curve Cryptographic support */
78 #ifdef CFG_CRYPTO_ECC
79 static const bool crypto_ecc_en = 1;
80 #else
81 static const bool crypto_ecc_en;
82 #endif
83 
84 /*
85  * Trusted storage anti rollback protection level
86  * 100: Antirollback enforced at REE level
87  * 1000: Antirollback TEE-controlled hardware
88  */
89 #ifdef CFG_RPMB_FS
90 static const uint32_t ts_antiroll_prot_lvl = 1000;
91 #else
92 static const uint32_t ts_antiroll_prot_lvl = 100;
93 #endif
94 
95 /* Trusted OS implementation version */
96 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
97 
98 /* Trusted OS implementation version (binary value) */
99 static const uint32_t trustedos_impl_bin_version; /* 0 by default */
100 
101 /* Trusted OS implementation manufacturer name */
102 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
103 
104 /* Trusted firmware version */
105 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
106 
107 /* Trusted firmware version (binary value) */
108 static const uint32_t fw_impl_bin_version; /* 0 by default */
109 
110 /* Trusted firmware manufacturer name */
111 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
112 
113 static TEE_Result get_prop_tee_dev_id(struct ts_session *sess __unused,
114 				      void *buf, size_t *blen)
115 {
116 	TEE_Result res;
117 	TEE_UUID uuid;
118 	const size_t nslen = 5;
119 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
120 	    'O', 'P', 'T', 'E', 'E' };
121 
122 	if (*blen < sizeof(uuid)) {
123 		*blen = sizeof(uuid);
124 		return TEE_ERROR_SHORT_BUFFER;
125 	}
126 	*blen = sizeof(uuid);
127 
128 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
129 		return TEE_ERROR_BAD_STATE;
130 
131 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
132 				    (uint8_t *)&uuid, sizeof(uuid));
133 	if (res != TEE_SUCCESS)
134 		return TEE_ERROR_BAD_STATE;
135 
136 	/*
137 	 * Changes the random value into and UUID as specifiec
138 	 * in RFC 4122. The magic values are from the example
139 	 * code in the RFC.
140 	 *
141 	 * TEE_UUID is defined slightly different from the RFC,
142 	 * but close enough for our purpose.
143 	 */
144 
145 	uuid.timeHiAndVersion &= 0x0fff;
146 	uuid.timeHiAndVersion |= 5 << 12;
147 
148 	/* uuid.clock_seq_hi_and_reserved in the RFC */
149 	uuid.clockSeqAndNode[0] &= 0x3f;
150 	uuid.clockSeqAndNode[0] |= 0x80;
151 
152 	return copy_to_user(buf, &uuid, sizeof(TEE_UUID));
153 }
154 
155 static TEE_Result
156 get_prop_tee_sys_time_prot_level(struct ts_session *sess __unused,
157 				 void *buf, size_t *blen)
158 {
159 	uint32_t prot;
160 
161 	if (*blen < sizeof(prot)) {
162 		*blen = sizeof(prot);
163 		return TEE_ERROR_SHORT_BUFFER;
164 	}
165 	*blen = sizeof(prot);
166 	prot = tee_time_get_sys_time_protection_level();
167 	return copy_to_user(buf, &prot, sizeof(prot));
168 }
169 
170 static TEE_Result get_prop_client_id(struct ts_session *sess,
171 				     void *buf, size_t *blen)
172 {
173 	if (*blen < sizeof(TEE_Identity)) {
174 		*blen = sizeof(TEE_Identity);
175 		return TEE_ERROR_SHORT_BUFFER;
176 	}
177 	*blen = sizeof(TEE_Identity);
178 	return copy_to_user(buf, &to_ta_session(sess)->clnt_id,
179 			    sizeof(TEE_Identity));
180 }
181 
182 static TEE_Result get_prop_client_endian(struct ts_session *sess __unused,
183 					 void *buf, size_t *blen)
184 {
185 	const uint32_t endian = 0; /* assume little-endian */
186 
187 	if (*blen < sizeof(endian)) {
188 		*blen = sizeof(endian);
189 		return TEE_ERROR_SHORT_BUFFER;
190 	}
191 	*blen = sizeof(endian);
192 	return copy_to_user(buf, &endian, sizeof(endian));
193 }
194 
195 static TEE_Result get_prop_ta_app_id(struct ts_session *sess,
196 				     void *buf, size_t *blen)
197 {
198 	if (*blen < sizeof(TEE_UUID)) {
199 		*blen = sizeof(TEE_UUID);
200 		return TEE_ERROR_SHORT_BUFFER;
201 	}
202 	*blen = sizeof(TEE_UUID);
203 	return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID));
204 }
205 
206 #ifdef CFG_TA_BTI
207 static TEE_Result
208 get_prop_feat_bti_implemented(struct ts_session *sess __unused, void *buf,
209 			      size_t *blen)
210 {
211 	bool bti_impl = false;
212 
213 	if (*blen < sizeof(bti_impl)) {
214 		*blen = sizeof(bti_impl);
215 		return TEE_ERROR_SHORT_BUFFER;
216 	}
217 	*blen = sizeof(bti_impl);
218 	bti_impl = feat_bti_is_implemented();
219 
220 	return copy_to_user(buf, &bti_impl, sizeof(bti_impl));
221 }
222 #endif
223 
224 #ifdef CFG_TA_PAUTH
225 static TEE_Result
226 get_prop_feat_pauth_implemented(struct ts_session *sess __unused, void *buf,
227 				size_t *blen)
228 {
229 	bool pauth_impl = false;
230 
231 	if (*blen < sizeof(pauth_impl)) {
232 		*blen = sizeof(pauth_impl);
233 		return TEE_ERROR_SHORT_BUFFER;
234 	}
235 	*blen = sizeof(pauth_impl);
236 	pauth_impl = feat_pauth_is_implemented();
237 
238 	return copy_to_user(buf, &pauth_impl, sizeof(pauth_impl));
239 }
240 #endif
241 
242 #if MEMTAG_IS_ENABLED
243 static TEE_Result
244 get_prop_feat_memtag_implemented(struct ts_session *sess __unused, void *buf,
245 				 size_t *blen)
246 {
247 	uint32_t v = 0;
248 
249 	if (*blen < sizeof(v)) {
250 		*blen = sizeof(v);
251 		return TEE_ERROR_SHORT_BUFFER;
252 	}
253 	*blen = sizeof(v);
254 	if (memtag_is_enabled())
255 		v = feat_mte_implemented();
256 
257 	return copy_to_user(buf, &v, sizeof(v));
258 }
259 #endif
260 
261 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
262 const struct tee_props tee_propset_client[] = {
263 	{
264 		.name = "gpd.client.identity",
265 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
266 		.get_prop_func = get_prop_client_id
267 	},
268 	{
269 		.name = "gpd.client.endian",
270 		.prop_type = USER_TA_PROP_TYPE_U32,
271 		.get_prop_func = get_prop_client_endian
272 	},
273 };
274 
275 /* Properties of the set TEE_PROPSET_CURRENT_TA */
276 const struct tee_props tee_propset_ta[] = {
277 	{
278 		.name = "gpd.ta.appID",
279 		.prop_type = USER_TA_PROP_TYPE_UUID,
280 		.get_prop_func = get_prop_ta_app_id
281 	},
282 
283 	/*
284 	 * Following properties are processed directly in libutee:
285 	 *	TA_PROP_STR_SINGLE_INSTANCE
286 	 *	TA_PROP_STR_MULTI_SESSION
287 	 *	TA_PROP_STR_KEEP_ALIVE
288 	 *	TA_PROP_STR_DATA_SIZE
289 	 *	TA_PROP_STR_STACK_SIZE
290 	 *	TA_PROP_STR_VERSION
291 	 *	TA_PROP_STR_DESCRIPTION
292 	 *	USER_TA_PROP_TYPE_STRING,
293 	 *	TA_DESCRIPTION
294 	 */
295 };
296 
297 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
298 const struct tee_props tee_propset_tee[] = {
299 	{
300 		.name = "gpd.tee.apiversion",
301 		.prop_type = USER_TA_PROP_TYPE_STRING,
302 		.data = api_vers,
303 		.len = sizeof(api_vers),
304 	},
305 	{
306 		.name = "gpd.tee.description",
307 		.prop_type = USER_TA_PROP_TYPE_STRING,
308 		.data = descr, .len = sizeof(descr)
309 	},
310 	{
311 		.name = "gpd.tee.deviceID",
312 		.prop_type = USER_TA_PROP_TYPE_UUID,
313 		.get_prop_func = get_prop_tee_dev_id
314 	},
315 	{
316 		.name = "gpd.tee.systemTime.protectionLevel",
317 		.prop_type = USER_TA_PROP_TYPE_U32,
318 		.get_prop_func = get_prop_tee_sys_time_prot_level
319 	},
320 	{
321 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
322 		.prop_type = USER_TA_PROP_TYPE_U32,
323 		.data = &ta_time_prot_lvl,
324 		.len = sizeof(ta_time_prot_lvl)
325 	},
326 	{
327 		.name = "gpd.tee.cryptography.ecc",
328 		.prop_type = USER_TA_PROP_TYPE_BOOL,
329 		.data = &crypto_ecc_en,
330 		.len = sizeof(crypto_ecc_en)
331 	},
332 	{
333 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
334 		.prop_type = USER_TA_PROP_TYPE_U32,
335 		.data = &ts_antiroll_prot_lvl,
336 		.len = sizeof(ts_antiroll_prot_lvl)
337 	},
338 	{
339 		.name = "gpd.tee.trustedos.implementation.version",
340 		.prop_type = USER_TA_PROP_TYPE_STRING,
341 		.data = trustedos_impl_version,
342 		.len = sizeof(trustedos_impl_version)
343 	},
344 	{
345 		.name = "gpd.tee.trustedos.implementation.binaryversion",
346 		.prop_type = USER_TA_PROP_TYPE_U32,
347 		.data = &trustedos_impl_bin_version,
348 		.len = sizeof(trustedos_impl_bin_version)
349 	},
350 	{
351 		.name = "gpd.tee.trustedos.manufacturer",
352 		.prop_type = USER_TA_PROP_TYPE_STRING,
353 		.data = trustedos_manufacturer,
354 		.len = sizeof(trustedos_manufacturer)
355 	},
356 	{
357 		.name = "gpd.tee.firmware.implementation.version",
358 		.prop_type = USER_TA_PROP_TYPE_STRING,
359 		.data = fw_impl_version,
360 		.len = sizeof(fw_impl_version)
361 	},
362 	{
363 		.name = "gpd.tee.firmware.implementation.binaryversion",
364 		.prop_type = USER_TA_PROP_TYPE_U32,
365 		.data = &fw_impl_bin_version,
366 		.len = sizeof(fw_impl_bin_version)
367 	},
368 	{
369 		.name = "gpd.tee.firmware.manufacturer",
370 		.prop_type = USER_TA_PROP_TYPE_STRING,
371 		.data = fw_manufacturer,
372 		.len = sizeof(fw_manufacturer)
373 	},
374 #ifdef CFG_TA_BTI
375 	{
376 		.name = "org.trustedfirmware.optee.cpu.feat_bti_implemented",
377 		.prop_type = USER_TA_PROP_TYPE_BOOL,
378 		.get_prop_func = get_prop_feat_bti_implemented
379 	},
380 #endif
381 #ifdef CFG_TA_PAUTH
382 	{
383 		.name = "org.trustedfirmware.optee.cpu.feat_pauth_implemented",
384 		.prop_type = USER_TA_PROP_TYPE_BOOL,
385 		.get_prop_func = get_prop_feat_pauth_implemented
386 	},
387 #endif
388 #if MEMTAG_IS_ENABLED
389 	{
390 		.name = "org.trustedfirmware.optee.cpu.feat_memtag_implemented",
391 		.prop_type = USER_TA_PROP_TYPE_U32,
392 		.get_prop_func = get_prop_feat_memtag_implemented
393 	}
394 #endif
395 
396 	/*
397 	 * Following properties are processed directly in libutee:
398 	 *	gpd.tee.arith.maxBigIntSize
399 	 */
400 };
401 
402 __weak const struct tee_vendor_props vendor_props_client;
403 __weak const struct tee_vendor_props vendor_props_ta;
404 __weak const struct tee_vendor_props vendor_props_tee;
405 
406 static void get_prop_set(unsigned long prop_set,
407 			 const struct tee_props **props,
408 			 size_t *size,
409 			 const struct tee_props **vendor_props,
410 			 size_t *vendor_size)
411 {
412 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
413 		*props = tee_propset_client;
414 		*size = ARRAY_SIZE(tee_propset_client);
415 		*vendor_props = vendor_props_client.props;
416 		*vendor_size = vendor_props_client.len;
417 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
418 		*props = tee_propset_ta;
419 		*size = ARRAY_SIZE(tee_propset_ta);
420 		*vendor_props = vendor_props_ta.props;
421 		*vendor_size = vendor_props_ta.len;
422 	} else if ((TEE_PropSetHandle)prop_set ==
423 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
424 		*props = tee_propset_tee;
425 		*size = ARRAY_SIZE(tee_propset_tee);
426 		*vendor_props = vendor_props_tee.props;
427 		*vendor_size = vendor_props_tee.len;
428 	} else {
429 		*props = NULL;
430 		*size = 0;
431 		*vendor_props = NULL;
432 		*vendor_size = 0;
433 	}
434 }
435 
436 static const struct tee_props *get_prop_struct(unsigned long prop_set,
437 					       unsigned long index)
438 {
439 	const struct tee_props *props;
440 	const struct tee_props *vendor_props;
441 	size_t size;
442 	size_t vendor_size;
443 
444 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
445 
446 	if (index < size)
447 		return &(props[index]);
448 	index -= size;
449 
450 	if (index < vendor_size)
451 		return &(vendor_props[index]);
452 
453 	return NULL;
454 }
455 
456 /*
457  * prop_set is part of TEE_PROPSET_xxx
458  * index is the index in the Property Set to retrieve
459  * if name is not NULL, the name of "index" property is returned
460  * if buf is not NULL, the property is returned
461  */
462 TEE_Result syscall_get_property(unsigned long prop_set,
463 				unsigned long index,
464 				void *name, uint32_t *name_len,
465 				void *buf, uint32_t *blen,
466 				uint32_t *prop_type)
467 {
468 	struct ts_session *sess = ts_get_current_session();
469 	TEE_Result res = TEE_SUCCESS;
470 	TEE_Result res2 = TEE_SUCCESS;
471 	const struct tee_props *prop = NULL;
472 	uint32_t klen = 0;
473 	size_t klen_size = 0;
474 	uint32_t elen = 0;
475 
476 	prop = get_prop_struct(prop_set, index);
477 	if (!prop)
478 		return TEE_ERROR_ITEM_NOT_FOUND;
479 
480 	/* Get the property type */
481 	if (prop_type) {
482 		res = copy_to_user(prop_type, &prop->prop_type,
483 				   sizeof(*prop_type));
484 		if (res != TEE_SUCCESS)
485 			return res;
486 	}
487 
488 	/* Get the property */
489 	if (buf && blen) {
490 		res = copy_from_user(&klen, blen, sizeof(klen));
491 		if (res != TEE_SUCCESS)
492 			return res;
493 
494 		if (prop->get_prop_func) {
495 			klen_size = klen;
496 			res = prop->get_prop_func(sess, buf, &klen_size);
497 			klen = klen_size;
498 			res2 = copy_to_user(blen, &klen, sizeof(*blen));
499 		} else {
500 			if (klen < prop->len)
501 				res = TEE_ERROR_SHORT_BUFFER;
502 			else
503 				res = copy_to_user(buf, prop->data, prop->len);
504 			res2 = copy_to_user(blen, &prop->len, sizeof(*blen));
505 		}
506 		if (res2 != TEE_SUCCESS)
507 			return res2;
508 		if (res != TEE_SUCCESS)
509 			return res;
510 	}
511 
512 	/* Get the property name */
513 	if (name && name_len) {
514 		res = copy_from_user(&klen, name_len, sizeof(klen));
515 		if (res != TEE_SUCCESS)
516 			return res;
517 
518 		elen = strlen(prop->name) + 1;
519 
520 		if (klen < elen)
521 			res = TEE_ERROR_SHORT_BUFFER;
522 		else
523 			res = copy_to_user(name, prop->name, elen);
524 		res2 = copy_to_user(name_len, &elen, sizeof(*name_len));
525 		if (res2 != TEE_SUCCESS)
526 			return res2;
527 		if (res != TEE_SUCCESS)
528 			return res;
529 	}
530 
531 	return res;
532 }
533 
534 /*
535  * prop_set is part of TEE_PROPSET_xxx
536  */
537 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
538 					      void *name,
539 					      unsigned long name_len,
540 					      uint32_t *index)
541 {
542 	TEE_Result res = TEE_SUCCESS;
543 	const struct tee_props *props = NULL;
544 	size_t size = 0;
545 	const struct tee_props *vendor_props = NULL;
546 	size_t vendor_size = 0;
547 	char *kname = NULL;
548 	uint32_t i = 0;
549 
550 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
551 	if (!props)
552 		return TEE_ERROR_ITEM_NOT_FOUND;
553 
554 	if (!name || !name_len) {
555 		res = TEE_ERROR_BAD_PARAMETERS;
556 		goto out;
557 	}
558 
559 	kname = malloc(name_len);
560 	if (!kname)
561 		return TEE_ERROR_OUT_OF_MEMORY;
562 	res = copy_from_user(kname, name, name_len);
563 	if (res != TEE_SUCCESS)
564 		goto out;
565 	kname[name_len - 1] = 0;
566 
567 	res = TEE_ERROR_ITEM_NOT_FOUND;
568 	for (i = 0; i < size; i++) {
569 		if (!strcmp(kname, props[i].name)) {
570 			res = copy_to_user(index, &i, sizeof(*index));
571 			goto out;
572 		}
573 	}
574 	for (i = size; i < size + vendor_size; i++) {
575 		if (!strcmp(kname, vendor_props[i - size].name)) {
576 			res = copy_to_user(index, &i, sizeof(*index));
577 			goto out;
578 		}
579 	}
580 
581 out:
582 	free_wipe(kname);
583 	return res;
584 }
585 
586 static TEE_Result utee_param_to_param(struct user_ta_ctx *utc,
587 				      struct tee_ta_param *p,
588 				      struct utee_params *up)
589 {
590 	TEE_Result res = TEE_SUCCESS;
591 	size_t n = 0;
592 	uint64_t types = 0;
593 	struct utee_params *up_bbuf = NULL;
594 
595 	up_bbuf = bb_alloc(sizeof(struct utee_params));
596 	if (!up_bbuf)
597 		return TEE_ERROR_OUT_OF_MEMORY;
598 
599 	res = copy_from_user(up_bbuf, up, sizeof(struct utee_params));
600 	if (res)
601 		goto out;
602 
603 	types = up_bbuf->types;
604 
605 	p->types = types;
606 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
607 		uintptr_t a = up_bbuf->vals[n * 2];
608 		size_t b = up_bbuf->vals[n * 2 + 1];
609 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
610 				 TEE_MEMORY_ACCESS_ANY_OWNER;
611 
612 		switch (TEE_PARAM_TYPE_GET(types, n)) {
613 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
614 		case TEE_PARAM_TYPE_MEMREF_INOUT:
615 			flags |= TEE_MEMORY_ACCESS_WRITE;
616 			fallthrough;
617 		case TEE_PARAM_TYPE_MEMREF_INPUT:
618 			p->u[n].mem.offs = memtag_strip_tag_vaddr((void *)a);
619 			p->u[n].mem.size = b;
620 
621 			if (!p->u[n].mem.offs) {
622 				/* Allow NULL memrefs if of size 0 */
623 				if (p->u[n].mem.size) {
624 					res = TEE_ERROR_BAD_PARAMETERS;
625 					goto out;
626 				}
627 				p->u[n].mem.mobj = NULL;
628 				break;
629 			}
630 
631 			p->u[n].mem.mobj = &mobj_virt;
632 
633 			res = vm_check_access_rights(&utc->uctx, flags, a, b);
634 			if (res)
635 				goto out;
636 			break;
637 		case TEE_PARAM_TYPE_VALUE_INPUT:
638 		case TEE_PARAM_TYPE_VALUE_INOUT:
639 			p->u[n].val.a = a;
640 			p->u[n].val.b = b;
641 			break;
642 		default:
643 			memset(&p->u[n], 0, sizeof(p->u[n]));
644 			break;
645 		}
646 	}
647 
648 out:
649 	bb_free(up_bbuf, sizeof(struct utee_params));
650 	return res;
651 }
652 
653 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
654 				     uint8_t **va)
655 {
656 	struct mobj *m = NULL;
657 	void *v = NULL;
658 
659 	/* Allocate section in secure DDR */
660 #ifdef CFG_PAGED_USER_TA
661 	m = mobj_seccpy_shm_alloc(size);
662 #else
663 	m = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
664 #endif
665 	if (!m)
666 		return TEE_ERROR_GENERIC;
667 
668 	v = mobj_get_va(*mobj, 0, size);
669 	if (!v) {
670 		mobj_put(m);
671 		return TEE_ERROR_GENERIC;
672 	}
673 
674 	*mobj = m;
675 	*va = v;
676 	return TEE_SUCCESS;
677 }
678 
679 /*
680  * TA invokes some TA with parameter.
681  * If some parameters are memory references:
682  * - either the memref is inside TA private RAM: TA is not allowed to expose
683  *   its private RAM: use a temporary memory buffer and copy the data.
684  * - or the memref is not in the TA private RAM:
685  *   - if the memref was mapped to the TA, TA is allowed to expose it.
686  *   - if so, converts memref virtual address into a physical address.
687  */
688 static TEE_Result tee_svc_copy_param(struct ts_session *sess,
689 				     struct ts_session *called_sess,
690 				     struct utee_params *callee_params,
691 				     struct tee_ta_param *param,
692 				     void *tmp_buf_va[TEE_NUM_PARAMS],
693 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
694 				     struct mobj **mobj_tmp)
695 {
696 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
697 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
698 	TEE_Result res = TEE_SUCCESS;
699 	size_t dst_offs = 0;
700 	size_t req_mem = 0;
701 	uint8_t *dst = 0;
702 	void *va = NULL;
703 	size_t n = 0;
704 	size_t s = 0;
705 
706 	callee_params = memtag_strip_tag(callee_params);
707 
708 	/* fill 'param' input struct with caller params description buffer */
709 	if (!callee_params) {
710 		memset(param, 0, sizeof(*param));
711 	} else {
712 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
713 				 TEE_MEMORY_ACCESS_WRITE |
714 				 TEE_MEMORY_ACCESS_ANY_OWNER;
715 
716 		res = vm_check_access_rights(&utc->uctx, flags,
717 					     (uaddr_t)callee_params,
718 					     sizeof(struct utee_params));
719 		if (res != TEE_SUCCESS)
720 			return res;
721 		res = utee_param_to_param(utc, param, callee_params);
722 		if (res != TEE_SUCCESS)
723 			return res;
724 	}
725 
726 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
727 		/* pseudo TA borrows the mapping of the calling TA */
728 		return TEE_SUCCESS;
729 	}
730 
731 	/* All mobj in param are of type MOJB_TYPE_VIRT */
732 
733 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
734 
735 		ta_private_memref[n] = false;
736 
737 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
738 		case TEE_PARAM_TYPE_MEMREF_INPUT:
739 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
740 		case TEE_PARAM_TYPE_MEMREF_INOUT:
741 			va = (void *)param->u[n].mem.offs;
742 			s = param->u[n].mem.size;
743 			if (!va) {
744 				if (s)
745 					return TEE_ERROR_BAD_PARAMETERS;
746 				break;
747 			}
748 			/* uTA cannot expose its private memory */
749 			if (vm_buf_is_inside_um_private(&utc->uctx, va, s)) {
750 				s = ROUNDUP(s, sizeof(uint32_t));
751 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
752 					return TEE_ERROR_BAD_PARAMETERS;
753 				ta_private_memref[n] = true;
754 				break;
755 			}
756 
757 			res = vm_buf_to_mboj_offs(&utc->uctx, va, s,
758 						  &param->u[n].mem.mobj,
759 						  &param->u[n].mem.offs);
760 			if (res != TEE_SUCCESS)
761 				return res;
762 			break;
763 		default:
764 			break;
765 		}
766 	}
767 
768 	if (req_mem == 0)
769 		return TEE_SUCCESS;
770 
771 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
772 	if (res != TEE_SUCCESS)
773 		return res;
774 	dst_offs = 0;
775 
776 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
777 
778 		if (!ta_private_memref[n])
779 			continue;
780 
781 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
782 
783 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
784 		case TEE_PARAM_TYPE_MEMREF_INPUT:
785 		case TEE_PARAM_TYPE_MEMREF_INOUT:
786 			va = (void *)param->u[n].mem.offs;
787 			if (va) {
788 				res = copy_from_user(dst, va,
789 						     param->u[n].mem.size);
790 				if (res != TEE_SUCCESS)
791 					return res;
792 				param->u[n].mem.offs = dst_offs;
793 				param->u[n].mem.mobj = *mobj_tmp;
794 				tmp_buf_va[n] = dst;
795 				tmp_buf_size[n] = param->u[n].mem.size;
796 				dst += s;
797 				dst_offs += s;
798 			}
799 			break;
800 
801 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
802 			va = (void *)param->u[n].mem.offs;
803 			if (va) {
804 				param->u[n].mem.offs = dst_offs;
805 				param->u[n].mem.mobj = *mobj_tmp;
806 				tmp_buf_va[n] = dst;
807 				tmp_buf_size[n] = param->u[n].mem.size;
808 				dst += s;
809 				dst_offs += s;
810 			}
811 			break;
812 
813 		default:
814 			continue;
815 		}
816 	}
817 
818 	return TEE_SUCCESS;
819 }
820 
821 /*
822  * Back from execution of service: update parameters passed from TA:
823  * If some parameters were memory references:
824  * - either the memref was temporary: copy back data and update size
825  * - or it was the original TA memref: update only the size value.
826  */
827 static TEE_Result tee_svc_update_out_param(
828 		struct tee_ta_param *param,
829 		void *tmp_buf_va[TEE_NUM_PARAMS],
830 		size_t tmp_buf_size[TEE_NUM_PARAMS],
831 		struct utee_params *usr_param)
832 {
833 	size_t n = 0;
834 	uint64_t *vals = usr_param->vals;
835 	uint64_t sz = 0;
836 	uint64_t in_sz = 0;
837 
838 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
839 		TEE_Result res = TEE_SUCCESS;
840 		uint64_t val_buf[2] = { };
841 
842 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
843 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
844 		case TEE_PARAM_TYPE_MEMREF_INOUT:
845 			/*
846 			 * Memory copy is only needed if there's a temporary
847 			 * buffer involved, tmp_buf_va[n] is only update if
848 			 * a temporary buffer is used. Otherwise only the
849 			 * size needs to be updated.
850 			 */
851 			sz = param->u[n].mem.size;
852 
853 			res = GET_USER_SCALAR(in_sz, &vals[n * 2 + 1]);
854 			if (res)
855 				return res;
856 
857 			if (tmp_buf_va[n] && sz <= in_sz) {
858 				void *src = tmp_buf_va[n];
859 				uint64_t dst = 0;
860 
861 				res = GET_USER_SCALAR(dst, &vals[n * 2]);
862 				if (res)
863 					return res;
864 
865 				/*
866 				 * TA is allowed to return a size larger than
867 				 * the original size. However, in such cases no
868 				 * data should be synchronized as per TEE Client
869 				 * API spec.
870 				 */
871 				if (sz <= tmp_buf_size[n]) {
872 					res = copy_to_user((void *)(vaddr_t)dst,
873 							   src, sz);
874 					if (res != TEE_SUCCESS)
875 						return res;
876 				}
877 			}
878 			res = PUT_USER_SCALAR(sz, &usr_param->vals[n * 2 + 1]);
879 			if (res)
880 				return res;
881 
882 			break;
883 
884 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
885 		case TEE_PARAM_TYPE_VALUE_INOUT:
886 			val_buf[0] = param->u[n].val.a;
887 			val_buf[1] = param->u[n].val.b;
888 
889 			res = copy_to_user(&vals[n * 2], val_buf,
890 					   2 * sizeof(uint64_t));
891 			if (res)
892 				return res;
893 
894 			break;
895 
896 		default:
897 			continue;
898 		}
899 	}
900 
901 	return TEE_SUCCESS;
902 }
903 
904 /* Called when a TA calls an OpenSession on another TA */
905 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
906 			unsigned long cancel_req_to,
907 			struct utee_params *usr_param, uint32_t *ta_sess,
908 			uint32_t *ret_orig)
909 {
910 	struct ts_session *sess = ts_get_current_session();
911 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
912 	TEE_Result res = TEE_SUCCESS;
913 	uint32_t ret_o = TEE_ORIGIN_TEE;
914 	struct tee_ta_session *s = NULL;
915 	struct mobj *mobj_param = NULL;
916 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
917 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
918 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
919 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
920 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
921 
922 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
923 		res = TEE_ERROR_OUT_OF_MEMORY;
924 		goto out_free_only;
925 	}
926 
927 	memset(param, 0, sizeof(struct tee_ta_param));
928 
929 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
930 	if (res != TEE_SUCCESS)
931 		goto function_exit;
932 
933 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
934 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
935 
936 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
937 				 tmp_buf_size, &mobj_param);
938 	if (res != TEE_SUCCESS)
939 		goto function_exit;
940 
941 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
942 				  clnt_id, cancel_req_to, param);
943 	vm_set_ctx(&utc->ta_ctx.ts_ctx);
944 	if (res != TEE_SUCCESS)
945 		goto function_exit;
946 
947 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
948 				       usr_param);
949 
950 function_exit:
951 	mobj_put_wipe(mobj_param);
952 	if (res == TEE_SUCCESS)
953 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
954 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
955 
956 out_free_only:
957 	free_wipe(param);
958 	free_wipe(uuid);
959 	free_wipe(clnt_id);
960 	return res;
961 }
962 
963 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
964 {
965 	struct ts_session *sess = ts_get_current_session();
966 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
967 	TEE_Identity clnt_id = { };
968 	struct tee_ta_session *s = NULL;
969 
970 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
971 
972 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
973 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
974 
975 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
976 }
977 
978 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
979 			unsigned long cancel_req_to, unsigned long cmd_id,
980 			struct utee_params *usr_param, uint32_t *ret_orig)
981 {
982 	struct ts_session *sess = ts_get_current_session();
983 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
984 	TEE_Result res = TEE_SUCCESS;
985 	TEE_Result res2 = TEE_SUCCESS;
986 	uint32_t ret_o = TEE_ORIGIN_TEE;
987 	struct tee_ta_param param = { 0 };
988 	TEE_Identity clnt_id = { };
989 	struct tee_ta_session *called_sess = NULL;
990 	struct mobj *mobj_param = NULL;
991 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
992 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
993 
994 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
995 				&utc->open_sessions);
996 	if (!called_sess)
997 		return TEE_ERROR_BAD_PARAMETERS;
998 
999 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
1000 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
1001 
1002 	res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param, &param,
1003 				 tmp_buf_va, tmp_buf_size, &mobj_param);
1004 	if (res != TEE_SUCCESS)
1005 		goto function_exit;
1006 
1007 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
1008 				    cancel_req_to, cmd_id, &param);
1009 	if (res == TEE_ERROR_TARGET_DEAD)
1010 		goto function_exit;
1011 
1012 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
1013 					usr_param);
1014 	if (res2 != TEE_SUCCESS) {
1015 		/*
1016 		 * Spec for TEE_InvokeTACommand() says:
1017 		 * "If the return origin is different from
1018 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
1019 		 * before it could reach the destination Trusted
1020 		 * Application."
1021 		 *
1022 		 * But if we can't update params to the caller we have no
1023 		 * choice we need to return some error to indicate that
1024 		 * parameters aren't updated as expected.
1025 		 */
1026 		ret_o = TEE_ORIGIN_TEE;
1027 		res = res2;
1028 	}
1029 
1030 function_exit:
1031 	tee_ta_put_session(called_sess);
1032 	mobj_put_wipe(mobj_param);
1033 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
1034 	return res;
1035 }
1036 
1037 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
1038 				       size_t len)
1039 {
1040 	struct ts_session *s = ts_get_current_session();
1041 
1042 	return vm_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
1043 				      memtag_strip_tag_vaddr(buf), len);
1044 }
1045 
1046 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
1047 {
1048 	struct ts_session *s = ts_get_current_session();
1049 	uint32_t c = 0;
1050 
1051 	c = tee_ta_session_is_cancelled(to_ta_session(s), NULL);
1052 
1053 	return copy_to_user(cancel, &c, sizeof(c));
1054 }
1055 
1056 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1057 {
1058 	struct ts_session *s = ts_get_current_session();
1059 	struct tee_ta_session *sess = NULL;
1060 	uint32_t m = 0;
1061 
1062 	sess = to_ta_session(s);
1063 	m = sess->cancel_mask;
1064 	sess->cancel_mask = false;
1065 	return copy_to_user(old_mask, &m, sizeof(m));
1066 }
1067 
1068 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1069 {
1070 	struct ts_session *s = ts_get_current_session();
1071 	struct tee_ta_session *sess = NULL;
1072 	uint32_t m = 0;
1073 
1074 	sess = to_ta_session(s);
1075 	m = sess->cancel_mask;
1076 	sess->cancel_mask = true;
1077 	return copy_to_user(old_mask, &m, sizeof(m));
1078 }
1079 
1080 TEE_Result syscall_wait(unsigned long timeout)
1081 {
1082 	struct ts_session *s = ts_get_current_session();
1083 	TEE_Result res = TEE_SUCCESS;
1084 	uint32_t mytime = 0;
1085 	TEE_Time base_time = { };
1086 	TEE_Time current_time = { };
1087 
1088 	res = tee_time_get_sys_time(&base_time);
1089 	if (res != TEE_SUCCESS)
1090 		return res;
1091 
1092 	while (true) {
1093 		res = tee_time_get_sys_time(&current_time);
1094 		if (res != TEE_SUCCESS)
1095 			return res;
1096 
1097 		if (tee_ta_session_is_cancelled(to_ta_session(s),
1098 						&current_time))
1099 			return TEE_ERROR_CANCEL;
1100 
1101 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1102 		    (int)current_time.millis - (int)base_time.millis;
1103 		if (mytime >= timeout)
1104 			return TEE_SUCCESS;
1105 
1106 		tee_time_wait(timeout - mytime);
1107 	}
1108 
1109 	return res;
1110 }
1111 
1112 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1113 {
1114 	struct ts_session *s = ts_get_current_session();
1115 	TEE_Result res = TEE_SUCCESS;
1116 	TEE_Result res2 = TEE_SUCCESS;
1117 	TEE_Time t = { };
1118 
1119 	switch (cat) {
1120 	case UTEE_TIME_CAT_SYSTEM:
1121 		res = tee_time_get_sys_time(&t);
1122 		break;
1123 	case UTEE_TIME_CAT_TA_PERSISTENT:
1124 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1125 		break;
1126 	case UTEE_TIME_CAT_REE:
1127 		res = tee_time_get_ree_time(&t);
1128 		break;
1129 	default:
1130 		res = TEE_ERROR_BAD_PARAMETERS;
1131 		break;
1132 	}
1133 
1134 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1135 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
1136 		if (res2 != TEE_SUCCESS)
1137 			res = res2;
1138 	}
1139 
1140 	return res;
1141 }
1142 
1143 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1144 {
1145 	struct ts_session *s = ts_get_current_session();
1146 	TEE_Result res = TEE_SUCCESS;
1147 	TEE_Time t = { };
1148 
1149 	res = copy_from_user_private(&t, mytime, sizeof(t));
1150 	if (res != TEE_SUCCESS)
1151 		return res;
1152 
1153 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1154 }
1155