xref: /optee_os/core/tee/tee_svc.c (revision 00b3b9a25e768c935ac2ed2fa8f50157390e7204)
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/tee_mmu.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 (tee_mmu_check_access_rights(&utc->uctx, flags, a,
525 							b))
526 				return TEE_ERROR_ACCESS_DENIED;
527 			break;
528 		case TEE_PARAM_TYPE_VALUE_INPUT:
529 		case TEE_PARAM_TYPE_VALUE_INOUT:
530 			p->u[n].val.a = a;
531 			p->u[n].val.b = b;
532 			break;
533 		default:
534 			memset(&p->u[n], 0, sizeof(p->u[n]));
535 			break;
536 		}
537 	}
538 
539 	return TEE_SUCCESS;
540 }
541 
542 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
543 				     uint8_t **va)
544 {
545 	/* Allocate section in secure DDR */
546 #ifdef CFG_PAGED_USER_TA
547 	*mobj = mobj_seccpy_shm_alloc(size);
548 #else
549 	*mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
550 #endif
551 	if (!*mobj)
552 		return TEE_ERROR_GENERIC;
553 
554 	*va = mobj_get_va(*mobj, 0);
555 	return TEE_SUCCESS;
556 }
557 
558 /*
559  * TA invokes some TA with parameter.
560  * If some parameters are memory references:
561  * - either the memref is inside TA private RAM: TA is not allowed to expose
562  *   its private RAM: use a temporary memory buffer and copy the data.
563  * - or the memref is not in the TA private RAM:
564  *   - if the memref was mapped to the TA, TA is allowed to expose it.
565  *   - if so, converts memref virtual address into a physical address.
566  */
567 static TEE_Result tee_svc_copy_param(struct ts_session *sess,
568 				     struct ts_session *called_sess,
569 				     struct utee_params *callee_params,
570 				     struct tee_ta_param *param,
571 				     void *tmp_buf_va[TEE_NUM_PARAMS],
572 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
573 				     struct mobj **mobj_tmp)
574 {
575 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
576 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
577 	TEE_Result res = TEE_SUCCESS;
578 	size_t dst_offs = 0;
579 	size_t req_mem = 0;
580 	uint8_t *dst = 0;
581 	void *va = NULL;
582 	size_t n = 0;
583 	size_t s = 0;
584 
585 	/* fill 'param' input struct with caller params description buffer */
586 	if (!callee_params) {
587 		memset(param, 0, sizeof(*param));
588 	} else {
589 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
590 				 TEE_MEMORY_ACCESS_WRITE |
591 				 TEE_MEMORY_ACCESS_ANY_OWNER;
592 
593 		res = tee_mmu_check_access_rights(&utc->uctx, flags,
594 						  (uaddr_t)callee_params,
595 						  sizeof(struct utee_params));
596 		if (res != TEE_SUCCESS)
597 			return res;
598 		res = utee_param_to_param(utc, param, callee_params);
599 		if (res != TEE_SUCCESS)
600 			return res;
601 	}
602 
603 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
604 		/* pseudo TA borrows the mapping of the calling TA */
605 		return TEE_SUCCESS;
606 	}
607 
608 	/* All mobj in param are of type MOJB_TYPE_VIRT */
609 
610 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
611 
612 		ta_private_memref[n] = false;
613 
614 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
615 		case TEE_PARAM_TYPE_MEMREF_INPUT:
616 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
617 		case TEE_PARAM_TYPE_MEMREF_INOUT:
618 			va = (void *)param->u[n].mem.offs;
619 			s = param->u[n].mem.size;
620 			if (!va) {
621 				if (s)
622 					return TEE_ERROR_BAD_PARAMETERS;
623 				break;
624 			}
625 			/* uTA cannot expose its private memory */
626 			if (tee_mmu_is_vbuf_inside_um_private(&utc->uctx, va,
627 							      s)) {
628 
629 				s = ROUNDUP(s, sizeof(uint32_t));
630 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
631 					return TEE_ERROR_BAD_PARAMETERS;
632 				ta_private_memref[n] = true;
633 				break;
634 			}
635 
636 			res = tee_mmu_vbuf_to_mobj_offs(&utc->uctx, va, s,
637 							&param->u[n].mem.mobj,
638 							&param->u[n].mem.offs);
639 			if (res != TEE_SUCCESS)
640 				return res;
641 			break;
642 		default:
643 			break;
644 		}
645 	}
646 
647 	if (req_mem == 0)
648 		return TEE_SUCCESS;
649 
650 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
651 	if (res != TEE_SUCCESS)
652 		return res;
653 	dst_offs = 0;
654 
655 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
656 
657 		if (!ta_private_memref[n])
658 			continue;
659 
660 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
661 
662 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
663 		case TEE_PARAM_TYPE_MEMREF_INPUT:
664 		case TEE_PARAM_TYPE_MEMREF_INOUT:
665 			va = (void *)param->u[n].mem.offs;
666 			if (va) {
667 				res = copy_from_user(dst, va,
668 						     param->u[n].mem.size);
669 				if (res != TEE_SUCCESS)
670 					return res;
671 				param->u[n].mem.offs = dst_offs;
672 				param->u[n].mem.mobj = *mobj_tmp;
673 				tmp_buf_va[n] = dst;
674 				tmp_buf_size[n] = param->u[n].mem.size;
675 				dst += s;
676 				dst_offs += s;
677 			}
678 			break;
679 
680 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
681 			va = (void *)param->u[n].mem.offs;
682 			if (va) {
683 				param->u[n].mem.offs = dst_offs;
684 				param->u[n].mem.mobj = *mobj_tmp;
685 				tmp_buf_va[n] = dst;
686 				tmp_buf_size[n] = param->u[n].mem.size;
687 				dst += s;
688 				dst_offs += s;
689 			}
690 			break;
691 
692 		default:
693 			continue;
694 		}
695 	}
696 
697 	return TEE_SUCCESS;
698 }
699 
700 /*
701  * Back from execution of service: update parameters passed from TA:
702  * If some parameters were memory references:
703  * - either the memref was temporary: copy back data and update size
704  * - or it was the original TA memref: update only the size value.
705  */
706 static TEE_Result tee_svc_update_out_param(
707 		struct tee_ta_param *param,
708 		void *tmp_buf_va[TEE_NUM_PARAMS],
709 		size_t tmp_buf_size[TEE_NUM_PARAMS],
710 		struct utee_params *usr_param)
711 {
712 	size_t n;
713 	uint64_t *vals = usr_param->vals;
714 	size_t sz = 0;
715 
716 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
717 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
718 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
719 		case TEE_PARAM_TYPE_MEMREF_INOUT:
720 			/*
721 			 * Memory copy is only needed if there's a temporary
722 			 * buffer involved, tmp_buf_va[n] is only update if
723 			 * a temporary buffer is used. Otherwise only the
724 			 * size needs to be updated.
725 			 */
726 			sz = param->u[n].mem.size;
727 			if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) {
728 				void *src = tmp_buf_va[n];
729 				void *dst = (void *)(uintptr_t)vals[n * 2];
730 				TEE_Result res = TEE_SUCCESS;
731 
732 				/*
733 				 * TA is allowed to return a size larger than
734 				 * the original size. However, in such cases no
735 				 * data should be synchronized as per TEE Client
736 				 * API spec.
737 				 */
738 				if (sz <= tmp_buf_size[n]) {
739 					res = copy_to_user(dst, src, sz);
740 					if (res != TEE_SUCCESS)
741 						return res;
742 				}
743 			}
744 			usr_param->vals[n * 2 + 1] = sz;
745 			break;
746 
747 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
748 		case TEE_PARAM_TYPE_VALUE_INOUT:
749 			vals[n * 2] = param->u[n].val.a;
750 			vals[n * 2 + 1] = param->u[n].val.b;
751 			break;
752 
753 		default:
754 			continue;
755 		}
756 	}
757 
758 	return TEE_SUCCESS;
759 }
760 
761 /* Called when a TA calls an OpenSession on another TA */
762 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
763 			unsigned long cancel_req_to,
764 			struct utee_params *usr_param, uint32_t *ta_sess,
765 			uint32_t *ret_orig)
766 {
767 	struct ts_session *sess = ts_get_current_session();
768 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
769 	TEE_Result res = TEE_SUCCESS;
770 	uint32_t ret_o = TEE_ORIGIN_TEE;
771 	struct tee_ta_session *s = NULL;
772 	struct mobj *mobj_param = NULL;
773 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
774 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
775 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
776 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
777 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
778 
779 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
780 		res = TEE_ERROR_OUT_OF_MEMORY;
781 		goto out_free_only;
782 	}
783 
784 	memset(param, 0, sizeof(struct tee_ta_param));
785 
786 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
787 	if (res != TEE_SUCCESS)
788 		goto function_exit;
789 
790 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
791 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
792 
793 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
794 				 tmp_buf_size, &mobj_param);
795 	if (res != TEE_SUCCESS)
796 		goto function_exit;
797 
798 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
799 				  clnt_id, cancel_req_to, param);
800 	tee_mmu_set_ctx(&utc->uctx.ctx);
801 	if (res != TEE_SUCCESS)
802 		goto function_exit;
803 
804 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
805 				       usr_param);
806 
807 function_exit:
808 	mobj_put_wipe(mobj_param);
809 	if (res == TEE_SUCCESS)
810 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
811 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
812 
813 out_free_only:
814 	free_wipe(param);
815 	free_wipe(uuid);
816 	free_wipe(clnt_id);
817 	return res;
818 }
819 
820 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
821 {
822 	struct ts_session *sess = ts_get_current_session();
823 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
824 	TEE_Identity clnt_id = { };
825 	struct tee_ta_session *s = NULL;
826 
827 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
828 
829 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
830 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
831 
832 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
833 }
834 
835 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
836 			unsigned long cancel_req_to, unsigned long cmd_id,
837 			struct utee_params *usr_param, uint32_t *ret_orig)
838 {
839 	struct ts_session *sess = ts_get_current_session();
840 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
841 	TEE_Result res = TEE_SUCCESS;
842 	TEE_Result res2 = TEE_SUCCESS;
843 	uint32_t ret_o = TEE_ORIGIN_TEE;
844 	struct tee_ta_param param = { 0 };
845 	TEE_Identity clnt_id = { };
846 	struct tee_ta_session *called_sess = NULL;
847 	struct mobj *mobj_param = NULL;
848 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
849 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
850 
851 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
852 				&utc->open_sessions);
853 	if (!called_sess)
854 		return TEE_ERROR_BAD_PARAMETERS;
855 
856 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
857 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
858 
859 	res = tee_svc_copy_param(sess, &called_sess->ts_sess, usr_param, &param,
860 				 tmp_buf_va, tmp_buf_size, &mobj_param);
861 	if (res != TEE_SUCCESS)
862 		goto function_exit;
863 
864 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
865 				    cancel_req_to, cmd_id, &param);
866 	if (res == TEE_ERROR_TARGET_DEAD)
867 		goto function_exit;
868 
869 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
870 					usr_param);
871 	if (res2 != TEE_SUCCESS) {
872 		/*
873 		 * Spec for TEE_InvokeTACommand() says:
874 		 * "If the return origin is different from
875 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
876 		 * before it could reach the destination Trusted
877 		 * Application."
878 		 *
879 		 * But if we can't update params to the caller we have no
880 		 * choice we need to return some error to indicate that
881 		 * parameters aren't updated as expected.
882 		 */
883 		ret_o = TEE_ORIGIN_TEE;
884 		res = res2;
885 	}
886 
887 function_exit:
888 	tee_ta_put_session(called_sess);
889 	mobj_put_wipe(mobj_param);
890 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
891 	return res;
892 }
893 
894 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
895 				       size_t len)
896 {
897 	struct ts_session *s = ts_get_current_session();
898 
899 	return tee_mmu_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
900 					   (uaddr_t)buf, len);
901 }
902 
903 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
904 {
905 	struct ts_session *s = ts_get_current_session();
906 	uint32_t c = 0;
907 
908 	c = tee_ta_session_is_cancelled(to_ta_session(s), NULL);
909 
910 	return copy_to_user(cancel, &c, sizeof(c));
911 }
912 
913 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
914 {
915 	struct ts_session *s = ts_get_current_session();
916 	struct tee_ta_session *sess = NULL;
917 	uint32_t m = 0;
918 
919 	sess = to_ta_session(s);
920 	m = sess->cancel_mask;
921 	sess->cancel_mask = false;
922 	return copy_to_user(old_mask, &m, sizeof(m));
923 }
924 
925 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
926 {
927 	struct ts_session *s = ts_get_current_session();
928 	struct tee_ta_session *sess = NULL;
929 	uint32_t m = 0;
930 
931 	sess = to_ta_session(s);
932 	m = sess->cancel_mask;
933 	sess->cancel_mask = true;
934 	return copy_to_user(old_mask, &m, sizeof(m));
935 }
936 
937 TEE_Result syscall_wait(unsigned long timeout)
938 {
939 	struct ts_session *s = ts_get_current_session();
940 	TEE_Result res = TEE_SUCCESS;
941 	uint32_t mytime = 0;
942 	TEE_Time base_time = { };
943 	TEE_Time current_time = { };
944 
945 	res = tee_time_get_sys_time(&base_time);
946 	if (res != TEE_SUCCESS)
947 		return res;
948 
949 	while (true) {
950 		res = tee_time_get_sys_time(&current_time);
951 		if (res != TEE_SUCCESS)
952 			return res;
953 
954 		if (tee_ta_session_is_cancelled(to_ta_session(s),
955 						&current_time))
956 			return TEE_ERROR_CANCEL;
957 
958 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
959 		    (int)current_time.millis - (int)base_time.millis;
960 		if (mytime >= timeout)
961 			return TEE_SUCCESS;
962 
963 		tee_time_wait(timeout - mytime);
964 	}
965 
966 	return res;
967 }
968 
969 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
970 {
971 	struct ts_session *s = ts_get_current_session();
972 	TEE_Result res = TEE_SUCCESS;
973 	TEE_Result res2 = TEE_SUCCESS;
974 	TEE_Time t = { };
975 
976 	switch (cat) {
977 	case UTEE_TIME_CAT_SYSTEM:
978 		res = tee_time_get_sys_time(&t);
979 		break;
980 	case UTEE_TIME_CAT_TA_PERSISTENT:
981 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
982 		break;
983 	case UTEE_TIME_CAT_REE:
984 		res = tee_time_get_ree_time(&t);
985 		break;
986 	default:
987 		res = TEE_ERROR_BAD_PARAMETERS;
988 		break;
989 	}
990 
991 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
992 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
993 		if (res2 != TEE_SUCCESS)
994 			res = res2;
995 	}
996 
997 	return res;
998 }
999 
1000 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1001 {
1002 	struct ts_session *s = ts_get_current_session();
1003 	TEE_Result res = TEE_SUCCESS;
1004 	TEE_Time t = { };
1005 
1006 	res = copy_from_user_private(&t, mytime, sizeof(t));
1007 	if (res != TEE_SUCCESS)
1008 		return res;
1009 
1010 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1011 }
1012