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