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