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