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