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