xref: /optee_os/core/tee/tee_svc.c (revision 6cfa381e534b362afbd103f526b132048e54ba47)
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 	res = BB_MEMDUP_USER(up, sizeof(*up), &up_bbuf);
596 	if (res)
597 		goto out;
598 
599 	types = up_bbuf->types;
600 
601 	p->types = types;
602 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
603 		uintptr_t a = up_bbuf->vals[n * 2];
604 		size_t b = up_bbuf->vals[n * 2 + 1];
605 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
606 				 TEE_MEMORY_ACCESS_ANY_OWNER;
607 
608 		switch (TEE_PARAM_TYPE_GET(types, n)) {
609 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
610 		case TEE_PARAM_TYPE_MEMREF_INOUT:
611 			flags |= TEE_MEMORY_ACCESS_WRITE;
612 			fallthrough;
613 		case TEE_PARAM_TYPE_MEMREF_INPUT:
614 			p->u[n].mem.offs = memtag_strip_tag_vaddr((void *)a);
615 			p->u[n].mem.size = b;
616 
617 			if (!p->u[n].mem.offs) {
618 				/* Allow NULL memrefs if of size 0 */
619 				if (p->u[n].mem.size) {
620 					res = TEE_ERROR_BAD_PARAMETERS;
621 					goto out;
622 				}
623 				p->u[n].mem.mobj = NULL;
624 				break;
625 			}
626 
627 			p->u[n].mem.mobj = &mobj_virt;
628 
629 			res = vm_check_access_rights(&utc->uctx, flags, a, b);
630 			if (res)
631 				goto out;
632 			break;
633 		case TEE_PARAM_TYPE_VALUE_INPUT:
634 		case TEE_PARAM_TYPE_VALUE_INOUT:
635 			p->u[n].val.a = a;
636 			p->u[n].val.b = b;
637 			break;
638 		default:
639 			memset(&p->u[n], 0, sizeof(p->u[n]));
640 			break;
641 		}
642 	}
643 
644 out:
645 	bb_free(up_bbuf, sizeof(struct utee_params));
646 	return res;
647 }
648 
649 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
650 				     uint8_t **va)
651 {
652 	struct mobj *m = NULL;
653 	void *v = NULL;
654 
655 	/* Allocate section in secure DDR */
656 #ifdef CFG_PAGED_USER_TA
657 	m = mobj_seccpy_shm_alloc(size);
658 #else
659 	m = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
660 #endif
661 	if (!m)
662 		return TEE_ERROR_GENERIC;
663 
664 	v = mobj_get_va(*mobj, 0, size);
665 	if (!v) {
666 		mobj_put(m);
667 		return TEE_ERROR_GENERIC;
668 	}
669 
670 	*mobj = m;
671 	*va = v;
672 	return TEE_SUCCESS;
673 }
674 
675 /*
676  * TA invokes some TA with parameter.
677  * If some parameters are memory references:
678  * - either the memref is inside TA private RAM: TA is not allowed to expose
679  *   its private RAM: use a temporary memory buffer and copy the data.
680  * - or the memref is not in the TA private RAM:
681  *   - if the memref was mapped to the TA, TA is allowed to expose it.
682  *   - if so, converts memref virtual address into a physical address.
683  */
684 static TEE_Result tee_svc_copy_param(struct ts_session *sess,
685 				     struct ts_session *called_sess,
686 				     struct utee_params *callee_params,
687 				     struct tee_ta_param *param,
688 				     void *tmp_buf_va[TEE_NUM_PARAMS],
689 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
690 				     struct mobj **mobj_tmp)
691 {
692 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
693 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
694 	TEE_Result res = TEE_SUCCESS;
695 	size_t dst_offs = 0;
696 	size_t req_mem = 0;
697 	uint8_t *dst = 0;
698 	void *va = NULL;
699 	size_t n = 0;
700 	size_t s = 0;
701 
702 	callee_params = memtag_strip_tag(callee_params);
703 
704 	/* fill 'param' input struct with caller params description buffer */
705 	if (!callee_params) {
706 		memset(param, 0, sizeof(*param));
707 	} else {
708 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
709 				 TEE_MEMORY_ACCESS_WRITE |
710 				 TEE_MEMORY_ACCESS_ANY_OWNER;
711 
712 		res = vm_check_access_rights(&utc->uctx, flags,
713 					     (uaddr_t)callee_params,
714 					     sizeof(struct utee_params));
715 		if (res != TEE_SUCCESS)
716 			return res;
717 		res = utee_param_to_param(utc, param, callee_params);
718 		if (res != TEE_SUCCESS)
719 			return res;
720 	}
721 
722 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
723 		/* pseudo TA borrows the mapping of the calling TA */
724 		return TEE_SUCCESS;
725 	}
726 
727 	/* All mobj in param are of type MOJB_TYPE_VIRT */
728 
729 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
730 
731 		ta_private_memref[n] = false;
732 
733 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
734 		case TEE_PARAM_TYPE_MEMREF_INPUT:
735 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
736 		case TEE_PARAM_TYPE_MEMREF_INOUT:
737 			va = (void *)param->u[n].mem.offs;
738 			s = param->u[n].mem.size;
739 			if (!va) {
740 				if (s)
741 					return TEE_ERROR_BAD_PARAMETERS;
742 				break;
743 			}
744 			/* uTA cannot expose its private memory */
745 			if (vm_buf_is_inside_um_private(&utc->uctx, va, s)) {
746 				s = ROUNDUP(s, sizeof(uint32_t));
747 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
748 					return TEE_ERROR_BAD_PARAMETERS;
749 				ta_private_memref[n] = true;
750 				break;
751 			}
752 
753 			res = vm_buf_to_mboj_offs(&utc->uctx, va, s,
754 						  &param->u[n].mem.mobj,
755 						  &param->u[n].mem.offs);
756 			if (res != TEE_SUCCESS)
757 				return res;
758 			break;
759 		default:
760 			break;
761 		}
762 	}
763 
764 	if (req_mem == 0)
765 		return TEE_SUCCESS;
766 
767 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
768 	if (res != TEE_SUCCESS)
769 		return res;
770 	dst_offs = 0;
771 
772 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
773 
774 		if (!ta_private_memref[n])
775 			continue;
776 
777 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
778 
779 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
780 		case TEE_PARAM_TYPE_MEMREF_INPUT:
781 		case TEE_PARAM_TYPE_MEMREF_INOUT:
782 			va = (void *)param->u[n].mem.offs;
783 			if (va) {
784 				res = copy_from_user(dst, va,
785 						     param->u[n].mem.size);
786 				if (res != TEE_SUCCESS)
787 					return res;
788 				param->u[n].mem.offs = dst_offs;
789 				param->u[n].mem.mobj = *mobj_tmp;
790 				tmp_buf_va[n] = dst;
791 				tmp_buf_size[n] = param->u[n].mem.size;
792 				dst += s;
793 				dst_offs += s;
794 			}
795 			break;
796 
797 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
798 			va = (void *)param->u[n].mem.offs;
799 			if (va) {
800 				param->u[n].mem.offs = dst_offs;
801 				param->u[n].mem.mobj = *mobj_tmp;
802 				tmp_buf_va[n] = dst;
803 				tmp_buf_size[n] = param->u[n].mem.size;
804 				dst += s;
805 				dst_offs += s;
806 			}
807 			break;
808 
809 		default:
810 			continue;
811 		}
812 	}
813 
814 	return TEE_SUCCESS;
815 }
816 
817 /*
818  * Back from execution of service: update parameters passed from TA:
819  * If some parameters were memory references:
820  * - either the memref was temporary: copy back data and update size
821  * - or it was the original TA memref: update only the size value.
822  */
823 static TEE_Result tee_svc_update_out_param(
824 		struct tee_ta_param *param,
825 		void *tmp_buf_va[TEE_NUM_PARAMS],
826 		size_t tmp_buf_size[TEE_NUM_PARAMS],
827 		struct utee_params *usr_param)
828 {
829 	size_t n = 0;
830 	uint64_t *vals = usr_param->vals;
831 	uint64_t sz = 0;
832 	uint64_t in_sz = 0;
833 
834 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
835 		TEE_Result res = TEE_SUCCESS;
836 		uint64_t val_buf[2] = { };
837 
838 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
839 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
840 		case TEE_PARAM_TYPE_MEMREF_INOUT:
841 			/*
842 			 * Memory copy is only needed if there's a temporary
843 			 * buffer involved, tmp_buf_va[n] is only update if
844 			 * a temporary buffer is used. Otherwise only the
845 			 * size needs to be updated.
846 			 */
847 			sz = param->u[n].mem.size;
848 
849 			res = GET_USER_SCALAR(in_sz, &vals[n * 2 + 1]);
850 			if (res)
851 				return res;
852 
853 			if (tmp_buf_va[n] && sz <= in_sz) {
854 				void *src = tmp_buf_va[n];
855 				uint64_t dst = 0;
856 
857 				res = GET_USER_SCALAR(dst, &vals[n * 2]);
858 				if (res)
859 					return res;
860 
861 				/*
862 				 * TA is allowed to return a size larger than
863 				 * the original size. However, in such cases no
864 				 * data should be synchronized as per TEE Client
865 				 * API spec.
866 				 */
867 				if (sz <= tmp_buf_size[n]) {
868 					res = copy_to_user((void *)(vaddr_t)dst,
869 							   src, sz);
870 					if (res != TEE_SUCCESS)
871 						return res;
872 				}
873 			}
874 			res = PUT_USER_SCALAR(sz, &usr_param->vals[n * 2 + 1]);
875 			if (res)
876 				return res;
877 
878 			break;
879 
880 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
881 		case TEE_PARAM_TYPE_VALUE_INOUT:
882 			val_buf[0] = param->u[n].val.a;
883 			val_buf[1] = param->u[n].val.b;
884 
885 			res = copy_to_user(&vals[n * 2], val_buf,
886 					   2 * sizeof(uint64_t));
887 			if (res)
888 				return res;
889 
890 			break;
891 
892 		default:
893 			continue;
894 		}
895 	}
896 
897 	return TEE_SUCCESS;
898 }
899 
900 /* Called when a TA calls an OpenSession on another TA */
901 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
902 			unsigned long cancel_req_to,
903 			struct utee_params *usr_param, uint32_t *ta_sess,
904 			uint32_t *ret_orig)
905 {
906 	struct ts_session *sess = ts_get_current_session();
907 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
908 	TEE_Result res = TEE_SUCCESS;
909 	uint32_t ret_o = TEE_ORIGIN_TEE;
910 	struct tee_ta_session *s = NULL;
911 	struct mobj *mobj_param = NULL;
912 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
913 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
914 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
915 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
916 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
917 
918 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
919 		res = TEE_ERROR_OUT_OF_MEMORY;
920 		goto out_free_only;
921 	}
922 
923 	memset(param, 0, sizeof(struct tee_ta_param));
924 
925 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
926 	if (res != TEE_SUCCESS)
927 		goto function_exit;
928 
929 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
930 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
931 
932 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
933 				 tmp_buf_size, &mobj_param);
934 	if (res != TEE_SUCCESS)
935 		goto function_exit;
936 
937 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
938 				  clnt_id, cancel_req_to, param);
939 	vm_set_ctx(&utc->ta_ctx.ts_ctx);
940 	if (res != TEE_SUCCESS)
941 		goto function_exit;
942 
943 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
944 				       usr_param);
945 
946 function_exit:
947 	mobj_put_wipe(mobj_param);
948 	if (res == TEE_SUCCESS)
949 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
950 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
951 
952 out_free_only:
953 	free_wipe(param);
954 	free_wipe(uuid);
955 	free_wipe(clnt_id);
956 	return res;
957 }
958 
959 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
960 {
961 	struct ts_session *sess = ts_get_current_session();
962 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
963 	TEE_Identity clnt_id = { };
964 	struct tee_ta_session *s = NULL;
965 
966 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
967 
968 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
969 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
970 
971 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
972 }
973 
974 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
975 			unsigned long cancel_req_to, unsigned long cmd_id,
976 			struct utee_params *usr_param, uint32_t *ret_orig)
977 {
978 	struct ts_session *sess = ts_get_current_session();
979 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
980 	TEE_Result res = TEE_SUCCESS;
981 	TEE_Result res2 = TEE_SUCCESS;
982 	uint32_t ret_o = TEE_ORIGIN_TEE;
983 	struct tee_ta_param param = { 0 };
984 	TEE_Identity clnt_id = { };
985 	struct tee_ta_session *called_sess = NULL;
986 	struct mobj *mobj_param = NULL;
987 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
988 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
989 
990 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
991 				&utc->open_sessions);
992 	if (!called_sess)
993 		return TEE_ERROR_BAD_PARAMETERS;
994 
995 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
996 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
997 
998 	res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param, &param,
999 				 tmp_buf_va, tmp_buf_size, &mobj_param);
1000 	if (res != TEE_SUCCESS)
1001 		goto function_exit;
1002 
1003 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
1004 				    cancel_req_to, cmd_id, &param);
1005 	if (res == TEE_ERROR_TARGET_DEAD)
1006 		goto function_exit;
1007 
1008 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
1009 					usr_param);
1010 	if (res2 != TEE_SUCCESS) {
1011 		/*
1012 		 * Spec for TEE_InvokeTACommand() says:
1013 		 * "If the return origin is different from
1014 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
1015 		 * before it could reach the destination Trusted
1016 		 * Application."
1017 		 *
1018 		 * But if we can't update params to the caller we have no
1019 		 * choice we need to return some error to indicate that
1020 		 * parameters aren't updated as expected.
1021 		 */
1022 		ret_o = TEE_ORIGIN_TEE;
1023 		res = res2;
1024 	}
1025 
1026 function_exit:
1027 	tee_ta_put_session(called_sess);
1028 	mobj_put_wipe(mobj_param);
1029 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
1030 	return res;
1031 }
1032 
1033 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
1034 				       size_t len)
1035 {
1036 	struct ts_session *s = ts_get_current_session();
1037 
1038 	return vm_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
1039 				      memtag_strip_tag_vaddr(buf), len);
1040 }
1041 
1042 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
1043 {
1044 	struct ts_session *s = ts_get_current_session();
1045 	uint32_t c = 0;
1046 
1047 	c = tee_ta_session_is_cancelled(to_ta_session(s), NULL);
1048 
1049 	return copy_to_user(cancel, &c, sizeof(c));
1050 }
1051 
1052 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1053 {
1054 	struct ts_session *s = ts_get_current_session();
1055 	struct tee_ta_session *sess = NULL;
1056 	uint32_t m = 0;
1057 
1058 	sess = to_ta_session(s);
1059 	m = sess->cancel_mask;
1060 	sess->cancel_mask = false;
1061 	return copy_to_user(old_mask, &m, sizeof(m));
1062 }
1063 
1064 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1065 {
1066 	struct ts_session *s = ts_get_current_session();
1067 	struct tee_ta_session *sess = NULL;
1068 	uint32_t m = 0;
1069 
1070 	sess = to_ta_session(s);
1071 	m = sess->cancel_mask;
1072 	sess->cancel_mask = true;
1073 	return copy_to_user(old_mask, &m, sizeof(m));
1074 }
1075 
1076 TEE_Result syscall_wait(unsigned long timeout)
1077 {
1078 	struct ts_session *s = ts_get_current_session();
1079 	TEE_Result res = TEE_SUCCESS;
1080 	uint32_t mytime = 0;
1081 	TEE_Time base_time = { };
1082 	TEE_Time current_time = { };
1083 
1084 	res = tee_time_get_sys_time(&base_time);
1085 	if (res != TEE_SUCCESS)
1086 		return res;
1087 
1088 	while (true) {
1089 		res = tee_time_get_sys_time(&current_time);
1090 		if (res != TEE_SUCCESS)
1091 			return res;
1092 
1093 		if (tee_ta_session_is_cancelled(to_ta_session(s),
1094 						&current_time))
1095 			return TEE_ERROR_CANCEL;
1096 
1097 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1098 		    (int)current_time.millis - (int)base_time.millis;
1099 		if (mytime >= timeout)
1100 			return TEE_SUCCESS;
1101 
1102 		tee_time_wait(timeout - mytime);
1103 	}
1104 
1105 	return res;
1106 }
1107 
1108 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1109 {
1110 	struct ts_session *s = ts_get_current_session();
1111 	TEE_Result res = TEE_SUCCESS;
1112 	TEE_Result res2 = TEE_SUCCESS;
1113 	TEE_Time t = { };
1114 
1115 	switch (cat) {
1116 	case UTEE_TIME_CAT_SYSTEM:
1117 		res = tee_time_get_sys_time(&t);
1118 		break;
1119 	case UTEE_TIME_CAT_TA_PERSISTENT:
1120 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1121 		break;
1122 	case UTEE_TIME_CAT_REE:
1123 		res = tee_time_get_ree_time(&t);
1124 		break;
1125 	default:
1126 		res = TEE_ERROR_BAD_PARAMETERS;
1127 		break;
1128 	}
1129 
1130 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1131 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
1132 		if (res2 != TEE_SUCCESS)
1133 			res = res2;
1134 	}
1135 
1136 	return res;
1137 }
1138 
1139 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1140 {
1141 	struct ts_session *s = ts_get_current_session();
1142 	TEE_Result res = TEE_SUCCESS;
1143 	TEE_Time t = { };
1144 
1145 	res = copy_from_user_private(&t, mytime, sizeof(t));
1146 	if (res != TEE_SUCCESS)
1147 		return res;
1148 
1149 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1150 }
1151