xref: /optee_os/core/tee/tee_svc.c (revision a8d59198fdc30b2d06667ea0129b983a89d35b02)
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 tee_ta_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 get_prop_tee_sys_time_prot_level(
155 			struct tee_ta_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 tee_ta_session *sess __unused,
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, &sess->clnt_id, sizeof(TEE_Identity));
178 }
179 
180 static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess,
181 				     void *buf, size_t *blen)
182 {
183 	if (*blen < sizeof(TEE_UUID)) {
184 		*blen = sizeof(TEE_UUID);
185 		return TEE_ERROR_SHORT_BUFFER;
186 	}
187 	*blen = sizeof(TEE_UUID);
188 	return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID));
189 }
190 
191 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
192 const struct tee_props tee_propset_client[] = {
193 	{
194 		.name = "gpd.client.identity",
195 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
196 		.get_prop_func = get_prop_client_id
197 	},
198 };
199 
200 /* Properties of the set TEE_PROPSET_CURRENT_TA */
201 const struct tee_props tee_propset_ta[] = {
202 	{
203 		.name = "gpd.ta.appID",
204 		.prop_type = USER_TA_PROP_TYPE_UUID,
205 		.get_prop_func = get_prop_ta_app_id
206 	},
207 
208 	/*
209 	 * Following properties are processed directly in libutee:
210 	 *	TA_PROP_STR_SINGLE_INSTANCE
211 	 *	TA_PROP_STR_MULTI_SESSION
212 	 *	TA_PROP_STR_KEEP_ALIVE
213 	 *	TA_PROP_STR_DATA_SIZE
214 	 *	TA_PROP_STR_STACK_SIZE
215 	 *	TA_PROP_STR_VERSION
216 	 *	TA_PROP_STR_DESCRIPTION
217 	 *	USER_TA_PROP_TYPE_STRING,
218 	 *	TA_DESCRIPTION
219 	 */
220 };
221 
222 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
223 const struct tee_props tee_propset_tee[] = {
224 	{
225 		.name = "gpd.tee.apiversion",
226 		.prop_type = USER_TA_PROP_TYPE_STRING,
227 		.data = api_vers,
228 		.len = sizeof(api_vers),
229 	},
230 	{
231 		.name = "gpd.tee.description",
232 		.prop_type = USER_TA_PROP_TYPE_STRING,
233 		.data = descr, .len = sizeof(descr)
234 	},
235 	{
236 		.name = "gpd.tee.deviceID",
237 		.prop_type = USER_TA_PROP_TYPE_UUID,
238 		.get_prop_func = get_prop_tee_dev_id
239 	},
240 	{
241 		.name = "gpd.tee.systemTime.protectionLevel",
242 		.prop_type = USER_TA_PROP_TYPE_U32,
243 		.get_prop_func = get_prop_tee_sys_time_prot_level
244 	},
245 	{
246 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
247 		.prop_type = USER_TA_PROP_TYPE_U32,
248 		.data = &ta_time_prot_lvl,
249 		.len = sizeof(ta_time_prot_lvl)
250 	},
251 	{
252 		.name = "gpd.tee.cryptography.ecc",
253 		.prop_type = USER_TA_PROP_TYPE_BOOL,
254 		.data = &crypto_ecc_en,
255 		.len = sizeof(crypto_ecc_en)
256 	},
257 	{
258 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
259 		.prop_type = USER_TA_PROP_TYPE_U32,
260 		.data = &ts_antiroll_prot_lvl,
261 		.len = sizeof(ts_antiroll_prot_lvl)
262 	},
263 	{
264 		.name = "gpd.tee.trustedos.implementation.version",
265 		.prop_type = USER_TA_PROP_TYPE_STRING,
266 		.data = trustedos_impl_version,
267 		.len = sizeof(trustedos_impl_version)
268 	},
269 	{
270 		.name = "gpd.tee.trustedos.implementation.binaryversion",
271 		.prop_type = USER_TA_PROP_TYPE_U32,
272 		.data = &trustedos_impl_bin_version,
273 		.len = sizeof(trustedos_impl_bin_version)
274 	},
275 	{
276 		.name = "gpd.tee.trustedos.manufacturer",
277 		.prop_type = USER_TA_PROP_TYPE_STRING,
278 		.data = trustedos_manufacturer,
279 		.len = sizeof(trustedos_manufacturer)
280 	},
281 	{
282 		.name = "gpd.tee.firmware.implementation.version",
283 		.prop_type = USER_TA_PROP_TYPE_STRING,
284 		.data = fw_impl_version,
285 		.len = sizeof(fw_impl_version)
286 	},
287 	{
288 		.name = "gpd.tee.firmware.implementation.binaryversion",
289 		.prop_type = USER_TA_PROP_TYPE_U32,
290 		.data = &fw_impl_bin_version,
291 		.len = sizeof(fw_impl_bin_version)
292 	},
293 	{
294 		.name = "gpd.tee.firmware.manufacturer",
295 		.prop_type = USER_TA_PROP_TYPE_STRING,
296 		.data = fw_manufacturer,
297 		.len = sizeof(fw_manufacturer)
298 	},
299 
300 	/*
301 	 * Following properties are processed directly in libutee:
302 	 *	gpd.tee.arith.maxBigIntSize
303 	 */
304 };
305 
306 __weak const struct tee_vendor_props vendor_props_client;
307 __weak const struct tee_vendor_props vendor_props_ta;
308 __weak const struct tee_vendor_props vendor_props_tee;
309 
310 static void get_prop_set(unsigned long prop_set,
311 			 const struct tee_props **props,
312 			 size_t *size,
313 			 const struct tee_props **vendor_props,
314 			 size_t *vendor_size)
315 {
316 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
317 		*props = tee_propset_client;
318 		*size = ARRAY_SIZE(tee_propset_client);
319 		*vendor_props = vendor_props_client.props;
320 		*vendor_size = vendor_props_client.len;
321 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
322 		*props = tee_propset_ta;
323 		*size = ARRAY_SIZE(tee_propset_ta);
324 		*vendor_props = vendor_props_ta.props;
325 		*vendor_size = vendor_props_ta.len;
326 	} else if ((TEE_PropSetHandle)prop_set ==
327 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
328 		*props = tee_propset_tee;
329 		*size = ARRAY_SIZE(tee_propset_tee);
330 		*vendor_props = vendor_props_tee.props;
331 		*vendor_size = vendor_props_tee.len;
332 	} else {
333 		*props = NULL;
334 		*size = 0;
335 		*vendor_props = NULL;
336 		*vendor_size = 0;
337 	}
338 }
339 
340 static const struct tee_props *get_prop_struct(unsigned long prop_set,
341 					       unsigned long index)
342 {
343 	const struct tee_props *props;
344 	const struct tee_props *vendor_props;
345 	size_t size;
346 	size_t vendor_size;
347 
348 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
349 
350 	if (index < size)
351 		return &(props[index]);
352 	index -= size;
353 
354 	if (index < vendor_size)
355 		return &(vendor_props[index]);
356 
357 	return NULL;
358 }
359 
360 /*
361  * prop_set is part of TEE_PROPSET_xxx
362  * index is the index in the Property Set to retrieve
363  * if name is not NULL, the name of "index" property is returned
364  * if buf is not NULL, the property is returned
365  */
366 TEE_Result syscall_get_property(unsigned long prop_set,
367 				unsigned long index,
368 				void *name, uint32_t *name_len,
369 				void *buf, uint32_t *blen,
370 				uint32_t *prop_type)
371 {
372 	struct tee_ta_session *sess;
373 	TEE_Result res;
374 	TEE_Result res2;
375 	const struct tee_props *prop;
376 	uint32_t klen;
377 	size_t klen_size;
378 	uint32_t elen;
379 
380 	prop = get_prop_struct(prop_set, index);
381 	if (!prop)
382 		return TEE_ERROR_ITEM_NOT_FOUND;
383 
384 	res = tee_ta_get_current_session(&sess);
385 	if (res != TEE_SUCCESS)
386 		return res;
387 
388 	/* Get the property type */
389 	if (prop_type) {
390 		res = copy_to_user(prop_type, &prop->prop_type,
391 				   sizeof(*prop_type));
392 		if (res != TEE_SUCCESS)
393 			return res;
394 	}
395 
396 	/* Get the property */
397 	if (buf && blen) {
398 		res = copy_from_user(&klen, blen, sizeof(klen));
399 		if (res != TEE_SUCCESS)
400 			return res;
401 
402 		if (prop->get_prop_func) {
403 			klen_size = klen;
404 			res = prop->get_prop_func(sess, buf, &klen_size);
405 			klen = klen_size;
406 			res2 = copy_to_user(blen, &klen, sizeof(*blen));
407 		} else {
408 			if (klen < prop->len)
409 				res = TEE_ERROR_SHORT_BUFFER;
410 			else
411 				res = copy_to_user(buf, prop->data, prop->len);
412 			res2 = copy_to_user(blen, &prop->len, sizeof(*blen));
413 		}
414 		if (res2 != TEE_SUCCESS)
415 			return res2;
416 		if (res != TEE_SUCCESS)
417 			return res;
418 	}
419 
420 	/* Get the property name */
421 	if (name && name_len) {
422 		res = copy_from_user(&klen, name_len, sizeof(klen));
423 		if (res != TEE_SUCCESS)
424 			return res;
425 
426 		elen = strlen(prop->name) + 1;
427 
428 		if (klen < elen)
429 			res = TEE_ERROR_SHORT_BUFFER;
430 		else
431 			res = copy_to_user(name, prop->name, elen);
432 		res2 = copy_to_user(name_len, &elen, sizeof(*name_len));
433 		if (res2 != TEE_SUCCESS)
434 			return res2;
435 		if (res != TEE_SUCCESS)
436 			return res;
437 	}
438 
439 	return res;
440 }
441 
442 /*
443  * prop_set is part of TEE_PROPSET_xxx
444  */
445 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
446 					      void *name,
447 					      unsigned long name_len,
448 					      uint32_t *index)
449 {
450 	TEE_Result res;
451 	struct tee_ta_session *sess;
452 	const struct tee_props *props;
453 	size_t size;
454 	const struct tee_props *vendor_props;
455 	size_t vendor_size;
456 	char *kname = 0;
457 	uint32_t i;
458 
459 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
460 	if (!props)
461 		return TEE_ERROR_ITEM_NOT_FOUND;
462 
463 	res = tee_ta_get_current_session(&sess);
464 	if (res != TEE_SUCCESS)
465 		goto out;
466 
467 	if (!name || !name_len) {
468 		res = TEE_ERROR_BAD_PARAMETERS;
469 		goto out;
470 	}
471 
472 	kname = malloc(name_len);
473 	if (!kname)
474 		return TEE_ERROR_OUT_OF_MEMORY;
475 	res = copy_from_user(kname, name, name_len);
476 	if (res != TEE_SUCCESS)
477 		goto out;
478 	kname[name_len - 1] = 0;
479 
480 	res = TEE_ERROR_ITEM_NOT_FOUND;
481 	for (i = 0; i < size; i++) {
482 		if (!strcmp(kname, props[i].name)) {
483 			res = copy_to_user(index, &i, sizeof(*index));
484 			goto out;
485 		}
486 	}
487 	for (i = size; i < size + vendor_size; i++) {
488 		if (!strcmp(kname, vendor_props[i - size].name)) {
489 			res = copy_to_user(index, &i, sizeof(*index));
490 			goto out;
491 		}
492 	}
493 
494 out:
495 	free_wipe(kname);
496 	return res;
497 }
498 
499 static TEE_Result utee_param_to_param(struct user_ta_ctx *utc,
500 				      struct tee_ta_param *p,
501 				      struct utee_params *up)
502 {
503 	size_t n = 0;
504 	uint32_t types = up->types;
505 
506 	p->types = types;
507 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
508 		uintptr_t a = up->vals[n * 2];
509 		size_t b = up->vals[n * 2 + 1];
510 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
511 				 TEE_MEMORY_ACCESS_ANY_OWNER;
512 
513 		switch (TEE_PARAM_TYPE_GET(types, n)) {
514 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
515 		case TEE_PARAM_TYPE_MEMREF_INOUT:
516 			flags |= TEE_MEMORY_ACCESS_WRITE;
517 			fallthrough;
518 		case TEE_PARAM_TYPE_MEMREF_INPUT:
519 			p->u[n].mem.offs = a;
520 			p->u[n].mem.size = b;
521 
522 			if (!p->u[n].mem.offs) {
523 				/* Allow NULL memrefs if of size 0 */
524 				if (p->u[n].mem.size)
525 					return TEE_ERROR_BAD_PARAMETERS;
526 				p->u[n].mem.mobj = NULL;
527 				break;
528 			}
529 
530 			p->u[n].mem.mobj = &mobj_virt;
531 
532 			if (tee_mmu_check_access_rights(&utc->uctx, flags, a,
533 							b))
534 				return TEE_ERROR_ACCESS_DENIED;
535 			break;
536 		case TEE_PARAM_TYPE_VALUE_INPUT:
537 		case TEE_PARAM_TYPE_VALUE_INOUT:
538 			p->u[n].val.a = a;
539 			p->u[n].val.b = b;
540 			break;
541 		default:
542 			memset(&p->u[n], 0, sizeof(p->u[n]));
543 			break;
544 		}
545 	}
546 
547 	return TEE_SUCCESS;
548 }
549 
550 static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
551 				     uint8_t **va)
552 {
553 	/* Allocate section in secure DDR */
554 #ifdef CFG_PAGED_USER_TA
555 	*mobj = mobj_seccpy_shm_alloc(size);
556 #else
557 	*mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
558 #endif
559 	if (!*mobj)
560 		return TEE_ERROR_GENERIC;
561 
562 	*va = mobj_get_va(*mobj, 0);
563 	return TEE_SUCCESS;
564 }
565 
566 /*
567  * TA invokes some TA with parameter.
568  * If some parameters are memory references:
569  * - either the memref is inside TA private RAM: TA is not allowed to expose
570  *   its private RAM: use a temporary memory buffer and copy the data.
571  * - or the memref is not in the TA private RAM:
572  *   - if the memref was mapped to the TA, TA is allowed to expose it.
573  *   - if so, converts memref virtual address into a physical address.
574  */
575 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
576 				     struct tee_ta_session *called_sess,
577 				     struct utee_params *callee_params,
578 				     struct tee_ta_param *param,
579 				     void *tmp_buf_va[TEE_NUM_PARAMS],
580 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
581 				     struct mobj **mobj_tmp)
582 {
583 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
584 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
585 	TEE_Result res = TEE_SUCCESS;
586 	size_t dst_offs = 0;
587 	size_t req_mem = 0;
588 	uint8_t *dst = 0;
589 	void *va = NULL;
590 	size_t n = 0;
591 	size_t s = 0;
592 
593 	/* fill 'param' input struct with caller params description buffer */
594 	if (!callee_params) {
595 		memset(param, 0, sizeof(*param));
596 	} else {
597 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
598 				 TEE_MEMORY_ACCESS_WRITE |
599 				 TEE_MEMORY_ACCESS_ANY_OWNER;
600 
601 		res = tee_mmu_check_access_rights(&utc->uctx, flags,
602 						  (uaddr_t)callee_params,
603 						  sizeof(struct utee_params));
604 		if (res != TEE_SUCCESS)
605 			return res;
606 		res = utee_param_to_param(utc, param, callee_params);
607 		if (res != TEE_SUCCESS)
608 			return res;
609 	}
610 
611 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
612 		/* pseudo TA borrows the mapping of the calling TA */
613 		return TEE_SUCCESS;
614 	}
615 
616 	/* All mobj in param are of type MOJB_TYPE_VIRT */
617 
618 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
619 
620 		ta_private_memref[n] = false;
621 
622 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
623 		case TEE_PARAM_TYPE_MEMREF_INPUT:
624 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
625 		case TEE_PARAM_TYPE_MEMREF_INOUT:
626 			va = (void *)param->u[n].mem.offs;
627 			s = param->u[n].mem.size;
628 			if (!va) {
629 				if (s)
630 					return TEE_ERROR_BAD_PARAMETERS;
631 				break;
632 			}
633 			/* uTA cannot expose its private memory */
634 			if (tee_mmu_is_vbuf_inside_um_private(&utc->uctx, va,
635 							      s)) {
636 
637 				s = ROUNDUP(s, sizeof(uint32_t));
638 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
639 					return TEE_ERROR_BAD_PARAMETERS;
640 				ta_private_memref[n] = true;
641 				break;
642 			}
643 
644 			res = tee_mmu_vbuf_to_mobj_offs(&utc->uctx, va, s,
645 							&param->u[n].mem.mobj,
646 							&param->u[n].mem.offs);
647 			if (res != TEE_SUCCESS)
648 				return res;
649 			break;
650 		default:
651 			break;
652 		}
653 	}
654 
655 	if (req_mem == 0)
656 		return TEE_SUCCESS;
657 
658 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
659 	if (res != TEE_SUCCESS)
660 		return res;
661 	dst_offs = 0;
662 
663 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
664 
665 		if (!ta_private_memref[n])
666 			continue;
667 
668 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
669 
670 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
671 		case TEE_PARAM_TYPE_MEMREF_INPUT:
672 		case TEE_PARAM_TYPE_MEMREF_INOUT:
673 			va = (void *)param->u[n].mem.offs;
674 			if (va) {
675 				res = copy_from_user(dst, va,
676 						     param->u[n].mem.size);
677 				if (res != TEE_SUCCESS)
678 					return res;
679 				param->u[n].mem.offs = dst_offs;
680 				param->u[n].mem.mobj = *mobj_tmp;
681 				tmp_buf_va[n] = dst;
682 				tmp_buf_size[n] = param->u[n].mem.size;
683 				dst += s;
684 				dst_offs += s;
685 			}
686 			break;
687 
688 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
689 			va = (void *)param->u[n].mem.offs;
690 			if (va) {
691 				param->u[n].mem.offs = dst_offs;
692 				param->u[n].mem.mobj = *mobj_tmp;
693 				tmp_buf_va[n] = dst;
694 				tmp_buf_size[n] = param->u[n].mem.size;
695 				dst += s;
696 				dst_offs += s;
697 			}
698 			break;
699 
700 		default:
701 			continue;
702 		}
703 	}
704 
705 	return TEE_SUCCESS;
706 }
707 
708 /*
709  * Back from execution of service: update parameters passed from TA:
710  * If some parameters were memory references:
711  * - either the memref was temporary: copy back data and update size
712  * - or it was the original TA memref: update only the size value.
713  */
714 static TEE_Result tee_svc_update_out_param(
715 		struct tee_ta_param *param,
716 		void *tmp_buf_va[TEE_NUM_PARAMS],
717 		size_t tmp_buf_size[TEE_NUM_PARAMS],
718 		struct utee_params *usr_param)
719 {
720 	size_t n;
721 	uint64_t *vals = usr_param->vals;
722 	size_t sz = 0;
723 
724 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
725 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
726 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
727 		case TEE_PARAM_TYPE_MEMREF_INOUT:
728 			/*
729 			 * Memory copy is only needed if there's a temporary
730 			 * buffer involved, tmp_buf_va[n] is only update if
731 			 * a temporary buffer is used. Otherwise only the
732 			 * size needs to be updated.
733 			 */
734 			sz = param->u[n].mem.size;
735 			if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) {
736 				void *src = tmp_buf_va[n];
737 				void *dst = (void *)(uintptr_t)vals[n * 2];
738 				TEE_Result res = TEE_SUCCESS;
739 
740 				/*
741 				 * TA is allowed to return a size larger than
742 				 * the original size. However, in such cases no
743 				 * data should be synchronized as per TEE Client
744 				 * API spec.
745 				 */
746 				if (sz <= tmp_buf_size[n]) {
747 					res = copy_to_user(dst, src, sz);
748 					if (res != TEE_SUCCESS)
749 						return res;
750 				}
751 			}
752 			usr_param->vals[n * 2 + 1] = sz;
753 			break;
754 
755 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
756 		case TEE_PARAM_TYPE_VALUE_INOUT:
757 			vals[n * 2] = param->u[n].val.a;
758 			vals[n * 2 + 1] = param->u[n].val.b;
759 			break;
760 
761 		default:
762 			continue;
763 		}
764 	}
765 
766 	return TEE_SUCCESS;
767 }
768 
769 /* Called when a TA calls an OpenSession on another TA */
770 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
771 			unsigned long cancel_req_to,
772 			struct utee_params *usr_param, uint32_t *ta_sess,
773 			uint32_t *ret_orig)
774 {
775 	TEE_Result res;
776 	uint32_t ret_o = TEE_ORIGIN_TEE;
777 	struct tee_ta_session *s = NULL;
778 	struct tee_ta_session *sess;
779 	struct mobj *mobj_param = NULL;
780 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
781 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
782 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
783 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
784 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
785 	struct user_ta_ctx *utc;
786 
787 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
788 		res = TEE_ERROR_OUT_OF_MEMORY;
789 		goto out_free_only;
790 	}
791 
792 	memset(param, 0, sizeof(struct tee_ta_param));
793 
794 	res = tee_ta_get_current_session(&sess);
795 	if (res != TEE_SUCCESS)
796 		goto out_free_only;
797 	utc = to_user_ta_ctx(sess->ctx);
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, tmp_buf_va,
807 				 tmp_buf_size, &mobj_param);
808 	if (res != TEE_SUCCESS)
809 		goto function_exit;
810 
811 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
812 				  clnt_id, cancel_req_to, param);
813 	tee_mmu_set_ctx(&utc->uctx.ctx);
814 	if (res != TEE_SUCCESS)
815 		goto function_exit;
816 
817 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
818 				       usr_param);
819 
820 function_exit:
821 	mobj_put_wipe(mobj_param);
822 	if (res == TEE_SUCCESS)
823 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
824 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
825 
826 out_free_only:
827 	free_wipe(param);
828 	free_wipe(uuid);
829 	free_wipe(clnt_id);
830 	return res;
831 }
832 
833 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
834 {
835 	TEE_Result res;
836 	struct tee_ta_session *sess;
837 	TEE_Identity clnt_id;
838 	struct tee_ta_session *s = NULL;
839 	struct user_ta_ctx *utc;
840 
841 	res = tee_ta_get_current_session(&sess);
842 	if (res != TEE_SUCCESS)
843 		return res;
844 	utc = to_user_ta_ctx(sess->ctx);
845 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
846 
847 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
848 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
849 
850 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
851 }
852 
853 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
854 			unsigned long cancel_req_to, unsigned long cmd_id,
855 			struct utee_params *usr_param, uint32_t *ret_orig)
856 {
857 	TEE_Result res;
858 	TEE_Result res2;
859 	uint32_t ret_o = TEE_ORIGIN_TEE;
860 	struct tee_ta_param param = { 0 };
861 	TEE_Identity clnt_id;
862 	struct tee_ta_session *sess;
863 	struct tee_ta_session *called_sess;
864 	struct mobj *mobj_param = NULL;
865 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
866 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
867 	struct user_ta_ctx *utc;
868 
869 	res = tee_ta_get_current_session(&sess);
870 	if (res != TEE_SUCCESS)
871 		return res;
872 	utc = to_user_ta_ctx(sess->ctx);
873 
874 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
875 				&utc->open_sessions);
876 	if (!called_sess)
877 		return TEE_ERROR_BAD_PARAMETERS;
878 
879 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
880 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
881 
882 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
883 				 tmp_buf_va, tmp_buf_size, &mobj_param);
884 	if (res != TEE_SUCCESS)
885 		goto function_exit;
886 
887 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
888 				    cancel_req_to, cmd_id, &param);
889 	if (res == TEE_ERROR_TARGET_DEAD)
890 		goto function_exit;
891 
892 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
893 					usr_param);
894 	if (res2 != TEE_SUCCESS) {
895 		/*
896 		 * Spec for TEE_InvokeTACommand() says:
897 		 * "If the return origin is different from
898 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
899 		 * before it could reach the destination Trusted
900 		 * Application."
901 		 *
902 		 * But if we can't update params to the caller we have no
903 		 * choice we need to return some error to indicate that
904 		 * parameters aren't updated as expected.
905 		 */
906 		ret_o = TEE_ORIGIN_TEE;
907 		res = res2;
908 	}
909 
910 function_exit:
911 	tee_ta_put_session(called_sess);
912 	mobj_put_wipe(mobj_param);
913 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
914 	return res;
915 }
916 
917 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
918 				       size_t len)
919 {
920 	struct tee_ta_session *s = NULL;
921 	TEE_Result res = TEE_SUCCESS;
922 
923 	res = tee_ta_get_current_session(&s);
924 	if (res != TEE_SUCCESS)
925 		return res;
926 
927 	return tee_mmu_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
928 					   (uaddr_t)buf, len);
929 }
930 
931 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
932 {
933 	TEE_Result res;
934 	struct tee_ta_session *s = NULL;
935 	uint32_t c;
936 
937 	res = tee_ta_get_current_session(&s);
938 	if (res != TEE_SUCCESS)
939 		return res;
940 
941 	c = tee_ta_session_is_cancelled(s, NULL);
942 
943 	return copy_to_user(cancel, &c, sizeof(c));
944 }
945 
946 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
947 {
948 	TEE_Result res;
949 	struct tee_ta_session *s = NULL;
950 	uint32_t m;
951 
952 	res = tee_ta_get_current_session(&s);
953 	if (res != TEE_SUCCESS)
954 		return res;
955 
956 	m = s->cancel_mask;
957 	s->cancel_mask = false;
958 	return copy_to_user(old_mask, &m, sizeof(m));
959 }
960 
961 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
962 {
963 	TEE_Result res;
964 	struct tee_ta_session *s = NULL;
965 	uint32_t m;
966 
967 	res = tee_ta_get_current_session(&s);
968 	if (res != TEE_SUCCESS)
969 		return res;
970 
971 	m = s->cancel_mask;
972 	s->cancel_mask = true;
973 	return copy_to_user(old_mask, &m, sizeof(m));
974 }
975 
976 TEE_Result syscall_wait(unsigned long timeout)
977 {
978 	TEE_Result res = TEE_SUCCESS;
979 	uint32_t mytime = 0;
980 	struct tee_ta_session *s;
981 	TEE_Time base_time;
982 	TEE_Time current_time;
983 
984 	res = tee_ta_get_current_session(&s);
985 	if (res != TEE_SUCCESS)
986 		return res;
987 
988 	res = tee_time_get_sys_time(&base_time);
989 	if (res != TEE_SUCCESS)
990 		return res;
991 
992 	while (true) {
993 		res = tee_time_get_sys_time(&current_time);
994 		if (res != TEE_SUCCESS)
995 			return res;
996 
997 		if (tee_ta_session_is_cancelled(s, &current_time))
998 			return TEE_ERROR_CANCEL;
999 
1000 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1001 		    (int)current_time.millis - (int)base_time.millis;
1002 		if (mytime >= timeout)
1003 			return TEE_SUCCESS;
1004 
1005 		tee_time_wait(timeout - mytime);
1006 	}
1007 
1008 	return res;
1009 }
1010 
1011 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1012 {
1013 	TEE_Result res, res2;
1014 	struct tee_ta_session *s = NULL;
1015 	TEE_Time t;
1016 
1017 	res = tee_ta_get_current_session(&s);
1018 	if (res != TEE_SUCCESS)
1019 		return res;
1020 
1021 	switch (cat) {
1022 	case UTEE_TIME_CAT_SYSTEM:
1023 		res = tee_time_get_sys_time(&t);
1024 		break;
1025 	case UTEE_TIME_CAT_TA_PERSISTENT:
1026 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1027 		break;
1028 	case UTEE_TIME_CAT_REE:
1029 		res = tee_time_get_ree_time(&t);
1030 		break;
1031 	default:
1032 		res = TEE_ERROR_BAD_PARAMETERS;
1033 		break;
1034 	}
1035 
1036 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1037 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
1038 		if (res2 != TEE_SUCCESS)
1039 			res = res2;
1040 	}
1041 
1042 	return res;
1043 }
1044 
1045 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1046 {
1047 	TEE_Result res;
1048 	struct tee_ta_session *s = NULL;
1049 	TEE_Time t;
1050 
1051 	res = tee_ta_get_current_session(&s);
1052 	if (res != TEE_SUCCESS)
1053 		return res;
1054 
1055 	res = copy_from_user_private(&t, mytime, sizeof(t));
1056 	if (res != TEE_SUCCESS)
1057 		return res;
1058 
1059 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1060 }
1061