xref: /optee_os/core/tee/tee_svc_storage.c (revision 3f6ed0a62ed0264f05743f692202e7fc1b98d6c7)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020, 2022 Linaro Limited
5  */
6 
7 #include <config.h>
8 #include <crypto/crypto.h>
9 #include <kernel/mutex.h>
10 #include <kernel/tee_misc.h>
11 #include <kernel/tee_ta_manager.h>
12 #include <kernel/ts_manager.h>
13 #include <kernel/user_access.h>
14 #include <memtag.h>
15 #include <mm/vm.h>
16 #include <string.h>
17 #include <tee_api_defines_extensions.h>
18 #include <tee_api_defines.h>
19 #include <tee/tee_fs.h>
20 #include <tee/tee_obj.h>
21 #include <tee/tee_pobj.h>
22 #include <tee/tee_svc_cryp.h>
23 #include <tee/tee_svc.h>
24 #include <tee/tee_svc_storage.h>
25 #include <trace.h>
26 
27 /* Header of GP formated secure storage files */
28 struct tee_svc_storage_head {
29 	uint32_t attr_size;
30 	uint32_t objectSize;
31 	uint32_t maxObjectSize;
32 	uint32_t objectUsage;
33 	uint32_t objectType;
34 	uint32_t have_attrs;
35 };
36 
37 struct tee_storage_enum {
38 	TAILQ_ENTRY(tee_storage_enum) link;
39 	struct tee_fs_dir *dir;
40 	const struct tee_file_operations *fops;
41 };
42 
43 static TEE_Result tee_svc_storage_get_enum(struct user_ta_ctx *utc,
44 					   vaddr_t enum_id,
45 					   struct tee_storage_enum **e_out)
46 {
47 	struct tee_storage_enum *e;
48 
49 	TAILQ_FOREACH(e, &utc->storage_enums, link) {
50 		if (enum_id == (vaddr_t)e) {
51 			*e_out = e;
52 			return TEE_SUCCESS;
53 		}
54 	}
55 	return TEE_ERROR_BAD_PARAMETERS;
56 }
57 
58 static TEE_Result tee_svc_close_enum(struct user_ta_ctx *utc,
59 				     struct tee_storage_enum *e)
60 {
61 	if (e == NULL || utc == NULL)
62 		return TEE_ERROR_BAD_PARAMETERS;
63 
64 	TAILQ_REMOVE(&utc->storage_enums, e, link);
65 
66 	if (e->fops)
67 		e->fops->closedir(e->dir);
68 
69 	e->dir = NULL;
70 	e->fops = NULL;
71 
72 	free(e);
73 
74 	return TEE_SUCCESS;
75 }
76 
77 static void remove_corrupt_obj(struct user_ta_ctx *utc, struct tee_obj *o)
78 {
79 	o->pobj->fops->remove(o->pobj);
80 	if (!(utc->ta_ctx.flags & TA_FLAG_DONT_CLOSE_HANDLE_ON_CORRUPT_OBJECT))
81 		tee_obj_close(utc, o);
82 }
83 
84 static TEE_Result tee_svc_storage_read_head(struct tee_obj *o)
85 {
86 	TEE_Result res = TEE_SUCCESS;
87 	size_t bytes;
88 	struct tee_svc_storage_head head;
89 	const struct tee_file_operations *fops = o->pobj->fops;
90 	void *attr = NULL;
91 	size_t size;
92 	size_t tmp = 0;
93 
94 	assert(!o->fh);
95 	res = fops->open(o->pobj, &size, &o->fh);
96 	if (res != TEE_SUCCESS)
97 		goto exit;
98 
99 	/* read head */
100 	bytes = sizeof(struct tee_svc_storage_head);
101 	res = fops->read(o->fh, 0, &head, NULL, &bytes);
102 	if (res != TEE_SUCCESS) {
103 		if (res == TEE_ERROR_CORRUPT_OBJECT)
104 			EMSG("Head corrupt");
105 		goto exit;
106 	}
107 
108 	if (ADD_OVERFLOW(sizeof(head), head.attr_size, &tmp)) {
109 		res = TEE_ERROR_OVERFLOW;
110 		goto exit;
111 	}
112 	if (tmp > size) {
113 		res = TEE_ERROR_CORRUPT_OBJECT;
114 		goto exit;
115 	}
116 
117 	if (bytes != sizeof(struct tee_svc_storage_head)) {
118 		res = TEE_ERROR_BAD_FORMAT;
119 		goto exit;
120 	}
121 
122 	res = tee_obj_set_type(o, head.objectType, head.maxObjectSize);
123 	if (res != TEE_SUCCESS)
124 		goto exit;
125 
126 	o->ds_pos = tmp;
127 
128 	if (head.attr_size) {
129 		attr = malloc(head.attr_size);
130 		if (!attr) {
131 			res = TEE_ERROR_OUT_OF_MEMORY;
132 			goto exit;
133 		}
134 
135 		/* read meta */
136 		bytes = head.attr_size;
137 		res = fops->read(o->fh, sizeof(struct tee_svc_storage_head),
138 				 attr, NULL, &bytes);
139 		if (res == TEE_ERROR_OUT_OF_MEMORY)
140 			goto exit;
141 		if (res != TEE_SUCCESS || bytes != head.attr_size)
142 			res = TEE_ERROR_CORRUPT_OBJECT;
143 		if (res)
144 			goto exit;
145 	}
146 
147 	res = tee_obj_attr_from_binary(o, attr, head.attr_size);
148 	if (res != TEE_SUCCESS)
149 		goto exit;
150 
151 	o->info.dataSize = size - sizeof(head) - head.attr_size;
152 	o->info.objectSize = head.objectSize;
153 	o->info.objectUsage = head.objectUsage;
154 	o->info.objectType = head.objectType;
155 	o->have_attrs = head.have_attrs;
156 
157 exit:
158 	free(attr);
159 
160 	return res;
161 }
162 
163 TEE_Result syscall_storage_obj_open(unsigned long storage_id, void *object_id,
164 				    size_t object_id_len, unsigned long flags,
165 				    uint32_t *obj)
166 {
167 	const unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ |
168 					  TEE_DATA_FLAG_ACCESS_WRITE |
169 					  TEE_DATA_FLAG_ACCESS_WRITE_META |
170 					  TEE_DATA_FLAG_SHARE_READ |
171 					  TEE_DATA_FLAG_SHARE_WRITE;
172 	const struct tee_file_operations *fops =
173 			tee_svc_storage_file_ops(storage_id);
174 	struct ts_session *sess = ts_get_current_session();
175 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
176 	TEE_Result res = TEE_SUCCESS;
177 	struct tee_pobj *po = NULL;
178 	struct tee_obj *o = NULL;
179 	void *oid_bbuf = NULL;
180 
181 	if (flags & ~valid_flags)
182 		return TEE_ERROR_BAD_PARAMETERS;
183 
184 	if (!fops) {
185 		res = TEE_ERROR_ITEM_NOT_FOUND;
186 		goto exit;
187 	}
188 
189 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN) {
190 		res = TEE_ERROR_BAD_PARAMETERS;
191 		goto exit;
192 	}
193 
194 	object_id = memtag_strip_tag(object_id);
195 	if (object_id_len) {
196 		res = bb_memdup_user_private(object_id, object_id_len,
197 					     &oid_bbuf);
198 		if (res)
199 			goto exit;
200 	}
201 
202 	res = tee_pobj_get((void *)&sess->ctx->uuid, oid_bbuf,
203 			   object_id_len, flags, TEE_POBJ_USAGE_OPEN, fops,
204 			   &po);
205 	bb_free(oid_bbuf, object_id_len);
206 	if (res != TEE_SUCCESS)
207 		goto err;
208 
209 	o = tee_obj_alloc();
210 	if (o == NULL) {
211 		tee_pobj_release(po);
212 		res = TEE_ERROR_OUT_OF_MEMORY;
213 		goto err;
214 	}
215 
216 	o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT |
217 			      TEE_HANDLE_FLAG_INITIALIZED | flags;
218 	o->pobj = po;
219 	tee_obj_add(utc, o);
220 
221 	res = tee_svc_storage_read_head(o);
222 	if (res != TEE_SUCCESS) {
223 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
224 			EMSG("Object corrupt");
225 			goto err;
226 		}
227 		goto oclose;
228 	}
229 
230 	res = copy_kaddr_to_uref(obj, o);
231 	if (res != TEE_SUCCESS)
232 		goto oclose;
233 
234 	goto exit;
235 
236 oclose:
237 	tee_obj_close(utc, o);
238 	o = NULL;
239 
240 err:
241 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
242 		res = TEE_ERROR_CORRUPT_OBJECT;
243 	if (res == TEE_ERROR_CORRUPT_OBJECT && o)
244 		remove_corrupt_obj(utc, o);
245 
246 exit:
247 	return res;
248 }
249 
250 static TEE_Result tee_svc_storage_init_file(struct tee_obj *o, bool overwrite,
251 					    struct tee_obj *attr_o,
252 					    void *data, uint32_t len)
253 {
254 	TEE_Result res = TEE_SUCCESS;
255 	struct tee_svc_storage_head head = { };
256 	const struct tee_file_operations *fops = o->pobj->fops;
257 	void *attr = NULL;
258 	size_t attr_size = 0;
259 
260 	if (attr_o) {
261 		if (o != attr_o) {
262 			res = tee_obj_set_type(o, attr_o->info.objectType,
263 					       attr_o->info.maxObjectSize);
264 			if (res)
265 				return res;
266 			res = tee_obj_attr_copy_from(o, attr_o);
267 			if (res)
268 				return res;
269 			o->have_attrs = attr_o->have_attrs;
270 			o->info.objectUsage = attr_o->info.objectUsage;
271 			o->info.objectSize = attr_o->info.objectSize;
272 		}
273 		res = tee_obj_attr_to_binary(o, NULL, &attr_size);
274 		if (res)
275 			return res;
276 		if (attr_size) {
277 			attr = malloc(attr_size);
278 			if (!attr)
279 				return TEE_ERROR_OUT_OF_MEMORY;
280 			res = tee_obj_attr_to_binary(o, attr, &attr_size);
281 			if (res != TEE_SUCCESS)
282 				goto exit;
283 		}
284 	} else {
285 		res = tee_obj_set_type(o, TEE_TYPE_DATA, 0);
286 		if (res != TEE_SUCCESS)
287 			goto exit;
288 	}
289 
290 	o->ds_pos = sizeof(struct tee_svc_storage_head) + attr_size;
291 
292 	/* write head */
293 	head.attr_size = attr_size;
294 	head.objectSize = o->info.objectSize;
295 	head.maxObjectSize = o->info.maxObjectSize;
296 	head.objectUsage = o->info.objectUsage;
297 	head.objectType = o->info.objectType;
298 	head.have_attrs = o->have_attrs;
299 
300 	res = fops->create(o->pobj, overwrite, &head, sizeof(head), attr,
301 			   attr_size, NULL, data, len, &o->fh);
302 
303 	if (res)
304 		o->ds_pos = 0;
305 	else
306 		o->info.dataSize = len;
307 exit:
308 	free(attr);
309 	return res;
310 }
311 
312 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id,
313 			size_t object_id_len, unsigned long flags,
314 			unsigned long attr, void *data, size_t len,
315 			uint32_t *obj)
316 {
317 	const unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ |
318 					  TEE_DATA_FLAG_ACCESS_WRITE |
319 					  TEE_DATA_FLAG_ACCESS_WRITE_META |
320 					  TEE_DATA_FLAG_SHARE_READ |
321 					  TEE_DATA_FLAG_SHARE_WRITE |
322 					  TEE_DATA_FLAG_OVERWRITE;
323 	const struct tee_file_operations *fops =
324 			tee_svc_storage_file_ops(storage_id);
325 	struct ts_session *sess = ts_get_current_session();
326 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
327 	struct tee_obj *attr_o = NULL;
328 	TEE_Result res = TEE_SUCCESS;
329 	struct tee_pobj *po = NULL;
330 	struct tee_obj *o = NULL;
331 	void *oid_bbuf = NULL;
332 
333 	if (flags & ~valid_flags)
334 		return TEE_ERROR_BAD_PARAMETERS;
335 
336 	if (!fops)
337 		return TEE_ERROR_ITEM_NOT_FOUND;
338 
339 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
340 		return TEE_ERROR_BAD_PARAMETERS;
341 
342 	object_id = memtag_strip_tag(object_id);
343 	data = memtag_strip_tag(data);
344 
345 	/* Check presence of optional buffer */
346 	if (len && !data)
347 		return TEE_ERROR_BAD_PARAMETERS;
348 
349 	if (object_id_len) {
350 		res = bb_memdup_user_private(object_id, object_id_len,
351 					     &oid_bbuf);
352 		if (res)
353 			return res;
354 	}
355 
356 	res = tee_pobj_get((void *)&sess->ctx->uuid, oid_bbuf,
357 			   object_id_len, flags, TEE_POBJ_USAGE_CREATE,
358 			   fops, &po);
359 	bb_free(oid_bbuf, object_id_len);
360 	if (res != TEE_SUCCESS)
361 		goto err;
362 
363 	if (attr != TEE_HANDLE_NULL) {
364 		res = tee_obj_get(utc, uref_to_vaddr(attr), &attr_o);
365 		if (res != TEE_SUCCESS)
366 			goto err;
367 		/* The supplied handle must be one of an initialized object */
368 		if (!(attr_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) {
369 			res = TEE_ERROR_BAD_PARAMETERS;
370 			goto err;
371 		}
372 	}
373 
374 	if (!obj && attr_o &&
375 	    !(attr_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
376 		/*
377 		 * The caller expects the supplied attributes handle to be
378 		 * transformed into a persistent object.
379 		 */
380 		uint32_t saved_flags = attr_o->info.handleFlags;
381 
382 		attr_o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT |
383 					   TEE_HANDLE_FLAG_INITIALIZED | flags;
384 		attr_o->pobj = po;
385 		res = tee_svc_storage_init_file(attr_o,
386 						flags & TEE_DATA_FLAG_OVERWRITE,
387 						attr_o, data, len);
388 		if (res) {
389 			attr_o->info.handleFlags = saved_flags;
390 			attr_o->pobj = NULL;
391 			goto err;
392 		}
393 	} else {
394 		o = tee_obj_alloc();
395 		if (!o) {
396 			res = TEE_ERROR_OUT_OF_MEMORY;
397 			goto err;
398 		}
399 
400 		o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT |
401 				      TEE_HANDLE_FLAG_INITIALIZED | flags;
402 		o->pobj = po;
403 
404 		res = tee_svc_storage_init_file(o,
405 						flags & TEE_DATA_FLAG_OVERWRITE,
406 						attr_o, data, len);
407 		if (res != TEE_SUCCESS)
408 			goto err;
409 
410 		po = NULL; /* o owns it from now on */
411 		tee_obj_add(utc, o);
412 
413 		if (obj) {
414 			res = copy_kaddr_to_uref(obj, o);
415 			if (res != TEE_SUCCESS)
416 				goto oclose;
417 		}
418 
419 		tee_pobj_create_final(o->pobj);
420 
421 		if (!obj)
422 			tee_obj_close(utc, o);
423 	}
424 
425 	return TEE_SUCCESS;
426 
427 oclose:
428 	tee_obj_close(utc, o);
429 	return res;
430 
431 err:
432 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
433 		res = TEE_ERROR_CORRUPT_OBJECT;
434 	if (res == TEE_ERROR_CORRUPT_OBJECT && po)
435 		fops->remove(po);
436 	if (o) {
437 		fops->close(&o->fh);
438 		tee_obj_free(o);
439 	}
440 	if (po)
441 		tee_pobj_release(po);
442 
443 	return res;
444 }
445 
446 TEE_Result syscall_storage_obj_del(unsigned long obj)
447 {
448 	struct ts_session *sess = ts_get_current_session();
449 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
450 	TEE_Result res = TEE_SUCCESS;
451 	struct tee_obj *o = NULL;
452 
453 	res = tee_obj_get(utc, uref_to_vaddr(obj), &o);
454 	if (res != TEE_SUCCESS)
455 		return res;
456 
457 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META))
458 		return TEE_ERROR_ACCESS_CONFLICT;
459 
460 	if (o->pobj == NULL || o->pobj->obj_id == NULL)
461 		return TEE_ERROR_BAD_STATE;
462 
463 	if (IS_ENABLED(CFG_NXP_SE05X)) {
464 		/* Cryptographic layer house-keeping */
465 		res = crypto_storage_obj_del(o);
466 		if (res)
467 			return res;
468 	}
469 
470 	res = o->pobj->fops->remove(o->pobj);
471 	tee_obj_close(utc, o);
472 
473 	return res;
474 }
475 
476 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id,
477 				      size_t object_id_len)
478 {
479 	const struct tee_file_operations *fops = NULL;
480 	struct ts_session *sess = ts_get_current_session();
481 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
482 	TEE_Result res = TEE_SUCCESS;
483 	struct tee_pobj *po = NULL;
484 	struct tee_obj *o = NULL;
485 	char *new_file = NULL;
486 	char *old_file = NULL;
487 	void *oid_bbuf = NULL;
488 
489 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
490 		return TEE_ERROR_BAD_PARAMETERS;
491 
492 	res = tee_obj_get(utc, uref_to_vaddr(obj), &o);
493 	if (res != TEE_SUCCESS)
494 		return res;
495 
496 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
497 		res = TEE_ERROR_BAD_STATE;
498 		goto exit;
499 	}
500 
501 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META)) {
502 		res = TEE_ERROR_BAD_STATE;
503 		goto exit;
504 	}
505 
506 	if (o->pobj == NULL || o->pobj->obj_id == NULL) {
507 		res = TEE_ERROR_BAD_STATE;
508 		goto exit;
509 	}
510 
511 	object_id = memtag_strip_tag(object_id);
512 	if (object_id_len) {
513 		res = bb_memdup_user_private(object_id, object_id_len,
514 					     &oid_bbuf);
515 		if (res)
516 			goto exit;
517 	}
518 
519 	/* reserve dest name */
520 	fops = o->pobj->fops;
521 	res = tee_pobj_get((void *)&sess->ctx->uuid, oid_bbuf,
522 			   object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META,
523 			   TEE_POBJ_USAGE_RENAME, fops, &po);
524 	bb_free(oid_bbuf, object_id_len);
525 	if (res != TEE_SUCCESS)
526 		goto exit;
527 
528 	/* move */
529 	res = fops->rename(o->pobj, po, false /* no overwrite */);
530 	if (res)
531 		goto exit;
532 
533 	res = tee_pobj_rename(o->pobj, po->obj_id, po->obj_id_len);
534 
535 exit:
536 	tee_pobj_release(po);
537 
538 	free(new_file);
539 	free(old_file);
540 
541 	return res;
542 }
543 
544 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum)
545 {
546 	struct ts_session *sess = ts_get_current_session();
547 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
548 	struct tee_storage_enum *e = NULL;
549 
550 	if (obj_enum == NULL)
551 		return TEE_ERROR_BAD_PARAMETERS;
552 
553 	e = malloc(sizeof(struct tee_storage_enum));
554 	if (e == NULL)
555 		return TEE_ERROR_OUT_OF_MEMORY;
556 
557 	e->dir = NULL;
558 	e->fops = NULL;
559 	TAILQ_INSERT_TAIL(&utc->storage_enums, e, link);
560 
561 	return copy_kaddr_to_uref(obj_enum, e);
562 }
563 
564 TEE_Result syscall_storage_free_enum(unsigned long obj_enum)
565 {
566 	struct ts_session *sess = ts_get_current_session();
567 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
568 	struct tee_storage_enum *e = NULL;
569 	TEE_Result res = TEE_SUCCESS;
570 
571 	res = tee_svc_storage_get_enum(utc,
572 			uref_to_vaddr(obj_enum), &e);
573 	if (res != TEE_SUCCESS)
574 		return res;
575 
576 	return tee_svc_close_enum(utc, e);
577 }
578 
579 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum)
580 {
581 	struct ts_session *sess = ts_get_current_session();
582 	struct tee_storage_enum *e = NULL;
583 	TEE_Result res = TEE_SUCCESS;
584 
585 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
586 				       uref_to_vaddr(obj_enum), &e);
587 	if (res != TEE_SUCCESS)
588 		return res;
589 
590 	if (e->fops) {
591 		e->fops->closedir(e->dir);
592 		e->fops = NULL;
593 		e->dir = NULL;
594 	}
595 	assert(!e->dir);
596 
597 	return TEE_SUCCESS;
598 }
599 
600 TEE_Result syscall_storage_start_enum(unsigned long obj_enum,
601 				      unsigned long storage_id)
602 {
603 	struct ts_session *sess = ts_get_current_session();
604 	struct tee_storage_enum *e = NULL;
605 	TEE_Result res = TEE_SUCCESS;
606 	const struct tee_file_operations *fops =
607 			tee_svc_storage_file_ops(storage_id);
608 
609 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
610 				       uref_to_vaddr(obj_enum), &e);
611 	if (res != TEE_SUCCESS)
612 		return res;
613 
614 	if (e->dir) {
615 		e->fops->closedir(e->dir);
616 		e->dir = NULL;
617 	}
618 
619 	if (!fops)
620 		return TEE_ERROR_ITEM_NOT_FOUND;
621 
622 	e->fops = fops;
623 
624 	return fops->opendir(&sess->ctx->uuid, &e->dir);
625 }
626 
627 TEE_Result syscall_storage_next_enum(unsigned long obj_enum,
628 				     struct utee_object_info *info,
629 				     void *obj_id, uint64_t *len)
630 {
631 	struct ts_session *sess = ts_get_current_session();
632 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
633 	struct tee_storage_enum *e = NULL;
634 	struct tee_fs_dirent *d = NULL;
635 	TEE_Result res = TEE_SUCCESS;
636 	struct tee_obj *o = NULL;
637 	uint64_t l = 0;
638 	struct utee_object_info bbuf = { };
639 
640 	res = tee_svc_storage_get_enum(utc, uref_to_vaddr(obj_enum), &e);
641 	if (res != TEE_SUCCESS)
642 		goto exit;
643 
644 	info = memtag_strip_tag(info);
645 	obj_id = memtag_strip_tag(obj_id);
646 
647 	/* check rights of the provided buffers */
648 	res = vm_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_WRITE,
649 				     (uaddr_t)info, sizeof(*info));
650 	if (res != TEE_SUCCESS)
651 		goto exit;
652 
653 	res = vm_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_WRITE,
654 				     (uaddr_t)obj_id, TEE_OBJECT_ID_MAX_LEN);
655 	if (res != TEE_SUCCESS)
656 		goto exit;
657 
658 	if (!e->fops) {
659 		res = TEE_ERROR_ITEM_NOT_FOUND;
660 		goto exit;
661 	}
662 
663 	res = e->fops->readdir(e->dir, &d);
664 	if (res != TEE_SUCCESS)
665 		goto exit;
666 
667 	o = tee_obj_alloc();
668 	if (o == NULL) {
669 		res = TEE_ERROR_OUT_OF_MEMORY;
670 		goto exit;
671 	}
672 
673 	res = tee_pobj_get(&sess->ctx->uuid, d->oid, d->oidlen, 0,
674 			   TEE_POBJ_USAGE_ENUM, e->fops, &o->pobj);
675 	if (res)
676 		goto exit;
677 
678 	o->info.handleFlags = o->pobj->flags | TEE_HANDLE_FLAG_PERSISTENT |
679 			      TEE_HANDLE_FLAG_INITIALIZED;
680 
681 	res = tee_svc_storage_read_head(o);
682 	if (res != TEE_SUCCESS)
683 		goto exit;
684 
685 	bbuf = (struct utee_object_info){
686 		.obj_type = o->info.objectType,
687 		.obj_size = o->info.objectSize,
688 		.max_obj_size = o->info.maxObjectSize,
689 		.obj_usage = o->info.objectUsage,
690 		.data_size = o->info.dataSize,
691 		.data_pos = o->info.dataPosition,
692 		.handle_flags = o->info.handleFlags,
693 	};
694 	res = copy_to_user(info, &bbuf, sizeof(bbuf));
695 	if (res)
696 		goto exit;
697 
698 	res = copy_to_user(obj_id, o->pobj->obj_id, o->pobj->obj_id_len);
699 	if (res)
700 		goto exit;
701 
702 	l = o->pobj->obj_id_len;
703 	res = copy_to_user_private(len, &l, sizeof(*len));
704 
705 exit:
706 	if (o) {
707 		if (o->pobj) {
708 			o->pobj->fops->close(&o->fh);
709 			tee_pobj_release(o->pobj);
710 		}
711 		tee_obj_free(o);
712 	}
713 
714 	return res;
715 }
716 
717 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len,
718 				    uint64_t *count)
719 {
720 	struct ts_session *sess = ts_get_current_session();
721 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
722 	TEE_Result res = TEE_SUCCESS;
723 	struct tee_obj *o = NULL;
724 	uint64_t u_count = 0;
725 	size_t pos_tmp = 0;
726 	size_t bytes = 0;
727 
728 	res = tee_obj_get(utc, uref_to_vaddr(obj), &o);
729 	if (res != TEE_SUCCESS)
730 		goto exit;
731 
732 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
733 		res = TEE_ERROR_BAD_STATE;
734 		goto exit;
735 	}
736 
737 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_READ)) {
738 		res = TEE_ERROR_ACCESS_CONFLICT;
739 		goto exit;
740 	}
741 
742 	/* Guard o->info.dataPosition += bytes below from overflowing */
743 	if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) {
744 		res = TEE_ERROR_OVERFLOW;
745 		goto exit;
746 	}
747 
748 	data = memtag_strip_tag(data);
749 
750 	bytes = len;
751 	if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) {
752 		res = TEE_ERROR_OVERFLOW;
753 		goto exit;
754 	}
755 	res = o->pobj->fops->read(o->fh, pos_tmp, NULL, data, &bytes);
756 	if (res != TEE_SUCCESS) {
757 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
758 			EMSG("Object corrupt");
759 			remove_corrupt_obj(utc, o);
760 		}
761 		goto exit;
762 	}
763 
764 	o->info.dataPosition += bytes;
765 
766 	u_count = bytes;
767 	res = copy_to_user_private(count, &u_count, sizeof(*count));
768 exit:
769 	return res;
770 }
771 
772 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len)
773 {
774 	struct ts_session *sess = ts_get_current_session();
775 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
776 	TEE_Result res = TEE_SUCCESS;
777 	struct tee_obj *o = NULL;
778 	size_t pos_tmp = 0;
779 
780 	res = tee_obj_get(utc, uref_to_vaddr(obj), &o);
781 	if (res != TEE_SUCCESS)
782 		goto exit;
783 
784 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
785 		res = TEE_ERROR_BAD_STATE;
786 		goto exit;
787 	}
788 
789 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) {
790 		res = TEE_ERROR_ACCESS_CONFLICT;
791 		goto exit;
792 	}
793 
794 	/* Guard o->info.dataPosition += bytes below from overflowing */
795 	if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) {
796 		res = TEE_ERROR_OVERFLOW;
797 		goto exit;
798 	}
799 
800 	data = memtag_strip_tag(data);
801 
802 	if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) {
803 		res = TEE_ERROR_ACCESS_CONFLICT;
804 		goto exit;
805 	}
806 	res = o->pobj->fops->write(o->fh, pos_tmp, NULL, data, len);
807 	if (res != TEE_SUCCESS) {
808 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
809 			EMSG("Object corrupt");
810 			remove_corrupt_obj(utc, o);
811 		}
812 		goto exit;
813 	}
814 
815 	o->info.dataPosition += len;
816 	if (o->info.dataPosition > o->info.dataSize)
817 		o->info.dataSize = o->info.dataPosition;
818 
819 exit:
820 	return res;
821 }
822 
823 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len)
824 {
825 	struct ts_session *sess = ts_get_current_session();
826 	TEE_Result res = TEE_SUCCESS;
827 	struct tee_obj *o = NULL;
828 	size_t off = 0;
829 	size_t attr_size = 0;
830 
831 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
832 	if (res != TEE_SUCCESS)
833 		goto exit;
834 
835 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
836 		res = TEE_ERROR_BAD_STATE;
837 		goto exit;
838 	}
839 
840 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) {
841 		res = TEE_ERROR_ACCESS_CONFLICT;
842 		goto exit;
843 	}
844 
845 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
846 	if (res != TEE_SUCCESS)
847 		goto exit;
848 
849 	if (ADD_OVERFLOW(sizeof(struct tee_svc_storage_head), attr_size,
850 				&off)) {
851 		res = TEE_ERROR_OVERFLOW;
852 		goto exit;
853 	}
854 	if (ADD_OVERFLOW(len, off, &off)) {
855 		res = TEE_ERROR_OVERFLOW;
856 		goto exit;
857 	}
858 	res = o->pobj->fops->truncate(o->fh, off);
859 	switch (res) {
860 	case TEE_SUCCESS:
861 		o->info.dataSize = len;
862 		break;
863 	case TEE_ERROR_CORRUPT_OBJECT:
864 		EMSG("Object corruption");
865 		remove_corrupt_obj(to_user_ta_ctx(sess->ctx), o);
866 		break;
867 	default:
868 		res = TEE_ERROR_GENERIC;
869 		break;
870 	}
871 
872 exit:
873 	return res;
874 }
875 
876 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset,
877 				    unsigned long whence)
878 {
879 	struct ts_session *sess = ts_get_current_session();
880 	TEE_Result res = TEE_SUCCESS;
881 	struct tee_obj *o = NULL;
882 	tee_fs_off_t new_pos = 0;
883 
884 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
885 	if (res != TEE_SUCCESS)
886 		return res;
887 
888 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT))
889 		return TEE_ERROR_BAD_STATE;
890 
891 	switch (whence) {
892 	case TEE_DATA_SEEK_SET:
893 		new_pos = offset;
894 		break;
895 	case TEE_DATA_SEEK_CUR:
896 		if (ADD_OVERFLOW(o->info.dataPosition, offset, &new_pos))
897 			return TEE_ERROR_OVERFLOW;
898 		break;
899 	case TEE_DATA_SEEK_END:
900 		if (ADD_OVERFLOW(o->info.dataSize, offset, &new_pos))
901 			return TEE_ERROR_OVERFLOW;
902 		break;
903 	default:
904 		return TEE_ERROR_BAD_PARAMETERS;
905 	}
906 
907 	if (new_pos < 0)
908 		new_pos = 0;
909 
910 	if (new_pos > TEE_DATA_MAX_POSITION) {
911 		EMSG("Position is beyond TEE_DATA_MAX_POSITION");
912 		return TEE_ERROR_BAD_PARAMETERS;
913 	}
914 
915 	o->info.dataPosition = new_pos;
916 
917 	return TEE_SUCCESS;
918 }
919 
920 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc)
921 {
922 	struct tee_storage_enum_head *eh = &utc->storage_enums;
923 
924 	/* disregard return value */
925 	while (!TAILQ_EMPTY(eh))
926 		tee_svc_close_enum(utc, TAILQ_FIRST(eh));
927 }
928