xref: /optee_os/core/tee/tee_svc.c (revision 8d541aee2e0fe7242ec6fb57aabc7a04471b2237)
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 /*
650  * TA invokes some TA with parameter.
651  * If some parameters are memory references:
652  * - either the memref is inside TA private RAM: TA is not allowed to expose
653  *   its private RAM: use a temporary memory buffer and copy the data.
654  * - or the memref is not in the TA private RAM:
655  *   - if the memref was mapped to the TA, TA is allowed to expose it.
656  *   - if so, converts memref virtual address into a physical address.
657  */
658 static TEE_Result tee_svc_copy_param(struct ts_session *sess,
659 				     struct ts_session *called_sess,
660 				     struct utee_params *callee_params,
661 				     struct tee_ta_param *param)
662 {
663 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
664 	TEE_Result res = TEE_SUCCESS;
665 	void *va = NULL;
666 	size_t n = 0;
667 	size_t s = 0;
668 
669 	callee_params = memtag_strip_tag(callee_params);
670 
671 	/* fill 'param' input struct with caller params description buffer */
672 	if (!callee_params) {
673 		memset(param, 0, sizeof(*param));
674 	} else {
675 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
676 				 TEE_MEMORY_ACCESS_WRITE |
677 				 TEE_MEMORY_ACCESS_ANY_OWNER;
678 
679 		res = vm_check_access_rights(&utc->uctx, flags,
680 					     (uaddr_t)callee_params,
681 					     sizeof(struct utee_params));
682 		if (res != TEE_SUCCESS)
683 			return res;
684 		res = utee_param_to_param(utc, param, callee_params);
685 		if (res != TEE_SUCCESS)
686 			return res;
687 	}
688 
689 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
690 		/* pseudo TA borrows the mapping of the calling TA */
691 		return TEE_SUCCESS;
692 	}
693 
694 	/* All mobj in param are of type MOJB_TYPE_VIRT */
695 
696 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
697 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
698 		case TEE_PARAM_TYPE_MEMREF_INPUT:
699 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
700 		case TEE_PARAM_TYPE_MEMREF_INOUT:
701 			va = (void *)param->u[n].mem.offs;
702 			s = param->u[n].mem.size;
703 			if (!va) {
704 				if (s)
705 					return TEE_ERROR_BAD_PARAMETERS;
706 				break;
707 			}
708 			/* uTA cannot expose its private memory */
709 			if (vm_buf_is_inside_um_private(&utc->uctx, va, s))
710 				return TEE_ERROR_BAD_PARAMETERS;
711 
712 			res = vm_buf_to_mboj_offs(&utc->uctx, va, s,
713 						  &param->u[n].mem.mobj,
714 						  &param->u[n].mem.offs);
715 			if (res != TEE_SUCCESS)
716 				return res;
717 			break;
718 		default:
719 			break;
720 		}
721 	}
722 
723 	return TEE_SUCCESS;
724 }
725 
726 /*
727  * Back from execution of service: update parameters passed from TA:
728  * If some parameters were memory references:
729  * - either the memref was temporary: copy back data and update size
730  * - or it was the original TA memref: update only the size value.
731  */
732 static TEE_Result tee_svc_update_out_param(
733 		struct tee_ta_param *param,
734 		struct utee_params *usr_param)
735 {
736 	size_t n = 0;
737 	uint64_t *vals = usr_param->vals;
738 	uint64_t sz = 0;
739 
740 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
741 		TEE_Result res = TEE_SUCCESS;
742 		uint64_t val_buf[2] = { };
743 
744 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
745 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
746 		case TEE_PARAM_TYPE_MEMREF_INOUT:
747 			sz = param->u[n].mem.size;
748 
749 			res = PUT_USER_SCALAR(sz, &usr_param->vals[n * 2 + 1]);
750 			if (res)
751 				return res;
752 
753 			break;
754 
755 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
756 		case TEE_PARAM_TYPE_VALUE_INOUT:
757 			val_buf[0] = param->u[n].val.a;
758 			val_buf[1] = param->u[n].val.b;
759 
760 			res = copy_to_user(&vals[n * 2], val_buf,
761 					   2 * sizeof(uint64_t));
762 			if (res)
763 				return res;
764 
765 			break;
766 
767 		default:
768 			continue;
769 		}
770 	}
771 
772 	return TEE_SUCCESS;
773 }
774 
775 /* Called when a TA calls an OpenSession on another TA */
776 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
777 			unsigned long cancel_req_to,
778 			struct utee_params *usr_param, uint32_t *ta_sess,
779 			uint32_t *ret_orig)
780 {
781 	struct ts_session *sess = ts_get_current_session();
782 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
783 	TEE_Result res = TEE_SUCCESS;
784 	uint32_t ret_o = TEE_ORIGIN_TEE;
785 	struct tee_ta_session *s = NULL;
786 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
787 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
788 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
789 
790 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
791 		res = TEE_ERROR_OUT_OF_MEMORY;
792 		goto out_free_only;
793 	}
794 
795 	memset(param, 0, sizeof(struct tee_ta_param));
796 
797 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
798 	if (res != TEE_SUCCESS)
799 		goto function_exit;
800 
801 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
802 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
803 
804 	res = tee_svc_copy_param(sess, NULL, usr_param, param);
805 	if (res != TEE_SUCCESS)
806 		goto function_exit;
807 
808 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
809 				  clnt_id, cancel_req_to, param);
810 	vm_set_ctx(&utc->ta_ctx.ts_ctx);
811 	if (res != TEE_SUCCESS)
812 		goto function_exit;
813 
814 	res = tee_svc_update_out_param(param, usr_param);
815 
816 function_exit:
817 	if (res == TEE_SUCCESS)
818 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
819 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
820 
821 out_free_only:
822 	free_wipe(param);
823 	free_wipe(uuid);
824 	free_wipe(clnt_id);
825 	return res;
826 }
827 
828 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
829 {
830 	struct ts_session *sess = ts_get_current_session();
831 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
832 	TEE_Identity clnt_id = { };
833 	struct tee_ta_session *s = NULL;
834 
835 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
836 
837 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
838 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
839 
840 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
841 }
842 
843 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
844 			unsigned long cancel_req_to, unsigned long cmd_id,
845 			struct utee_params *usr_param, uint32_t *ret_orig)
846 {
847 	struct ts_session *sess = ts_get_current_session();
848 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
849 	TEE_Result res = TEE_SUCCESS;
850 	TEE_Result res2 = TEE_SUCCESS;
851 	uint32_t ret_o = TEE_ORIGIN_TEE;
852 	struct tee_ta_param param = { 0 };
853 	TEE_Identity clnt_id = { };
854 	struct tee_ta_session *called_sess = NULL;
855 
856 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
857 				&utc->open_sessions);
858 	if (!called_sess)
859 		return TEE_ERROR_BAD_PARAMETERS;
860 
861 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
862 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
863 
864 	res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param,
865 				 &param);
866 	if (res != TEE_SUCCESS)
867 		goto function_exit;
868 
869 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
870 				    cancel_req_to, cmd_id, &param);
871 	if (res == TEE_ERROR_TARGET_DEAD)
872 		goto function_exit;
873 
874 	res2 = tee_svc_update_out_param(&param, usr_param);
875 	if (res2 != TEE_SUCCESS) {
876 		/*
877 		 * Spec for TEE_InvokeTACommand() says:
878 		 * "If the return origin is different from
879 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
880 		 * before it could reach the destination Trusted
881 		 * Application."
882 		 *
883 		 * But if we can't update params to the caller we have no
884 		 * choice we need to return some error to indicate that
885 		 * parameters aren't updated as expected.
886 		 */
887 		ret_o = TEE_ORIGIN_TEE;
888 		res = res2;
889 	}
890 
891 function_exit:
892 	tee_ta_put_session(called_sess);
893 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
894 	return res;
895 }
896 
897 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
898 				       size_t len)
899 {
900 	struct ts_session *s = ts_get_current_session();
901 
902 	return vm_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
903 				      memtag_strip_tag_vaddr(buf), len);
904 }
905 
906 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
907 {
908 	struct ts_session *s = ts_get_current_session();
909 	uint32_t c = 0;
910 
911 	c = tee_ta_session_is_cancelled(to_ta_session(s), NULL);
912 
913 	return copy_to_user(cancel, &c, sizeof(c));
914 }
915 
916 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
917 {
918 	struct ts_session *s = ts_get_current_session();
919 	struct tee_ta_session *sess = NULL;
920 	uint32_t m = 0;
921 
922 	sess = to_ta_session(s);
923 	m = sess->cancel_mask;
924 	sess->cancel_mask = false;
925 	return copy_to_user(old_mask, &m, sizeof(m));
926 }
927 
928 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
929 {
930 	struct ts_session *s = ts_get_current_session();
931 	struct tee_ta_session *sess = NULL;
932 	uint32_t m = 0;
933 
934 	sess = to_ta_session(s);
935 	m = sess->cancel_mask;
936 	sess->cancel_mask = true;
937 	return copy_to_user(old_mask, &m, sizeof(m));
938 }
939 
940 TEE_Result syscall_wait(unsigned long timeout)
941 {
942 	struct ts_session *s = ts_get_current_session();
943 	TEE_Result res = TEE_SUCCESS;
944 	uint32_t mytime = 0;
945 	TEE_Time base_time = { };
946 	TEE_Time current_time = { };
947 
948 	res = tee_time_get_sys_time(&base_time);
949 	if (res != TEE_SUCCESS)
950 		return res;
951 
952 	while (true) {
953 		res = tee_time_get_sys_time(&current_time);
954 		if (res != TEE_SUCCESS)
955 			return res;
956 
957 		if (tee_ta_session_is_cancelled(to_ta_session(s),
958 						&current_time))
959 			return TEE_ERROR_CANCEL;
960 
961 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
962 		    (int)current_time.millis - (int)base_time.millis;
963 		if (mytime >= timeout)
964 			return TEE_SUCCESS;
965 
966 		tee_time_wait(timeout - mytime);
967 	}
968 
969 	return res;
970 }
971 
972 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
973 {
974 	struct ts_session *s = ts_get_current_session();
975 	TEE_Result res = TEE_SUCCESS;
976 	TEE_Result res2 = TEE_SUCCESS;
977 	TEE_Time t = { };
978 
979 	switch (cat) {
980 	case UTEE_TIME_CAT_SYSTEM:
981 		res = tee_time_get_sys_time(&t);
982 		break;
983 	case UTEE_TIME_CAT_TA_PERSISTENT:
984 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
985 		break;
986 	case UTEE_TIME_CAT_REE:
987 		res = tee_time_get_ree_time(&t);
988 		break;
989 	default:
990 		res = TEE_ERROR_BAD_PARAMETERS;
991 		break;
992 	}
993 
994 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
995 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
996 		if (res2 != TEE_SUCCESS)
997 			res = res2;
998 	}
999 
1000 	return res;
1001 }
1002 
1003 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1004 {
1005 	struct ts_session *s = ts_get_current_session();
1006 	TEE_Result res = TEE_SUCCESS;
1007 	TEE_Time t = { };
1008 
1009 	res = copy_from_user_private(&t, mytime, sizeof(t));
1010 	if (res != TEE_SUCCESS)
1011 		return res;
1012 
1013 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1014 }
1015