xref: /optee_os/core/tee/tee_svc_storage.c (revision 822203a8e9898ce65d7dea0e642ad4c1dc026473)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <kernel/mutex.h>
29 #include <kernel/tee_misc.h>
30 #include <kernel/tee_ta_manager.h>
31 #include <mm/tee_mmu.h>
32 #include <string.h>
33 #include <tee_api_defines_extensions.h>
34 #include <tee_api_defines.h>
35 #include <tee/tee_fs_defs.h>
36 #include <tee/tee_fs.h>
37 #include <tee/tee_obj.h>
38 #include <tee/tee_pobj.h>
39 #include <tee/tee_svc_cryp.h>
40 #include <tee/tee_svc.h>
41 #include <tee/tee_svc_storage.h>
42 #include <trace.h>
43 
44 /*
45  * Returns the appropriate tee_file_operations for the specified storage ID.
46  * The value TEE_STORAGE_PRIVATE will select the REE FS if available, otherwise
47  * RPMB.
48  */
49 static const struct tee_file_operations *file_ops(uint32_t storage_id)
50 {
51 
52 	switch (storage_id) {
53 	case TEE_STORAGE_PRIVATE:
54 #if defined(CFG_REE_FS)
55 		return &ree_fs_ops;
56 #elif defined(CFG_RPMB_FS)
57 		return &rpmb_fs_ops;
58 #elif defined(CFG_SQL_FS)
59 		return &sql_fs_ops;
60 #else
61 #error At least one filesystem must be enabled.
62 #endif
63 #ifdef CFG_REE_FS
64 	case TEE_STORAGE_PRIVATE_REE:
65 		return &ree_fs_ops;
66 #endif
67 #ifdef CFG_RPMB_FS
68 	case TEE_STORAGE_PRIVATE_RPMB:
69 		return &rpmb_fs_ops;
70 #endif
71 #ifdef CFG_SQL_FS
72 	case TEE_STORAGE_PRIVATE_SQL:
73 		return &sql_fs_ops;
74 #endif
75 	default:
76 		return NULL;
77 	}
78 }
79 
80 /* SSF (Secure Storage File version 00 */
81 #define TEE_SVC_STORAGE_MAGIC 0x53534600;
82 
83 /* Header of GP formated secure storage files */
84 struct tee_svc_storage_head {
85 	uint32_t magic;
86 	uint32_t head_size;
87 	uint32_t meta_size;
88 	uint32_t ds_size;
89 	uint32_t keySize;
90 	uint32_t maxKeySize;
91 	uint32_t objectUsage;
92 	uint32_t objectType;
93 	uint32_t have_attrs;
94 };
95 
96 struct tee_storage_enum {
97 	TAILQ_ENTRY(tee_storage_enum) link;
98 	struct tee_fs_dir *dir;
99 	const struct tee_file_operations *fops;
100 };
101 
102 /*
103  * Protect TA storage directory: avoid race conditions between (create
104  * directory + create file) and (remove directory)
105  */
106 static struct mutex ta_dir_mutex = MUTEX_INITIALIZER;
107 
108 static TEE_Result tee_svc_storage_get_enum(struct user_ta_ctx *utc,
109 					   uint32_t enum_id,
110 					   struct tee_storage_enum **e_out)
111 {
112 	struct tee_storage_enum *e;
113 
114 	TAILQ_FOREACH(e, &utc->storage_enums, link) {
115 		if (enum_id == (vaddr_t)e) {
116 			*e_out = e;
117 			return TEE_SUCCESS;
118 		}
119 	}
120 	return TEE_ERROR_BAD_PARAMETERS;
121 }
122 
123 static TEE_Result tee_svc_close_enum(struct user_ta_ctx *utc,
124 				     struct tee_storage_enum *e)
125 {
126 	if (e == NULL || utc == NULL)
127 		return TEE_ERROR_BAD_PARAMETERS;
128 
129 	TAILQ_REMOVE(&utc->storage_enums, e, link);
130 
131 	if (!e->fops)
132 		return TEE_ERROR_ITEM_NOT_FOUND;
133 
134 	e->fops->closedir(e->dir);
135 	e->dir = NULL;
136 	e->fops = NULL;
137 
138 	free(e);
139 
140 	return TEE_SUCCESS;
141 }
142 
143 /* "/TA_uuid/object_id" or "/TA_uuid/.object_id" */
144 char *tee_svc_storage_create_filename(struct tee_ta_session *sess,
145 				      void *object_id,
146 				      uint32_t object_id_len,
147 				      bool transient)
148 {
149 	uint8_t *file;
150 	uint32_t pos = 0;
151 	uint32_t hslen = 1 /* Leading slash */
152 			+ TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID) + object_id_len)
153 			+ 1; /* Intermediate slash */
154 
155 	/* +1 for the '.' (temporary persistent object) */
156 	if (transient)
157 		hslen++;
158 
159 	file = malloc(hslen);
160 	if (!file)
161 		return NULL;
162 
163 	file[pos++] = '/';
164 	pos += tee_b2hs((uint8_t *)&sess->ctx->uuid, &file[pos],
165 			sizeof(TEE_UUID), hslen);
166 	file[pos++] = '/';
167 
168 	if (transient)
169 		file[pos++] = '.';
170 
171 	tee_b2hs(object_id, file + pos, object_id_len, hslen - pos);
172 
173 	return (char *)file;
174 }
175 
176 /* "/TA_uuid" */
177 char *tee_svc_storage_create_dirname(struct tee_ta_session *sess)
178 {
179 	uint8_t *dir;
180 	uint32_t hslen = TEE_B2HS_HSBUF_SIZE(sizeof(TEE_UUID)) + 1;
181 
182 	dir = malloc(hslen);
183 	if (!dir)
184 		return NULL;
185 
186 	dir[0] = '/';
187 	tee_b2hs((uint8_t *)&sess->ctx->uuid, dir + 1, sizeof(TEE_UUID),
188 		 hslen);
189 
190 	return (char *)dir;
191 }
192 
193 static TEE_Result tee_svc_storage_remove_corrupt_obj(
194 					struct tee_ta_session *sess,
195 					struct tee_obj *o)
196 {
197 	TEE_Result res;
198 	char *file = NULL;
199 	const struct tee_file_operations *fops = o->pobj->fops;
200 
201 	file = tee_svc_storage_create_filename(sess,
202 					       o->pobj->obj_id,
203 					       o->pobj->obj_id_len,
204 					       false);
205 	if (file == NULL) {
206 		res = TEE_ERROR_OUT_OF_MEMORY;
207 		goto exit;
208 	}
209 
210 	tee_obj_close(to_user_ta_ctx(sess->ctx), o);
211 	fops->remove(file);
212 	free(file);
213 
214 	res = TEE_SUCCESS;
215 
216 exit:
217 	return res;
218 }
219 
220 static TEE_Result tee_svc_storage_create_file(struct tee_ta_session *sess,
221 			char *file,
222 			const struct tee_file_operations *fops,
223 			struct tee_file_handle **fh)
224 {
225 	TEE_Result res = TEE_SUCCESS;
226 	char *dir = NULL;
227 
228 	assert(!*fh);
229 	dir = tee_svc_storage_create_dirname(sess);
230 	if (dir == NULL) {
231 		res = TEE_ERROR_OUT_OF_MEMORY;
232 		goto exit;
233 	}
234 
235 	mutex_lock(&ta_dir_mutex);
236 
237 	res = fops->create(file, true /* currently always overwrite */, fh);
238 
239 	mutex_unlock(&ta_dir_mutex);
240 exit:
241 	free(dir);
242 
243 	return res;
244 }
245 
246 static TEE_Result tee_svc_storage_read_head(struct tee_ta_session *sess,
247 				     struct tee_obj *o)
248 {
249 	TEE_Result res = TEE_SUCCESS;
250 	size_t bytes;
251 	struct tee_svc_storage_head head;
252 	char *file = NULL;
253 	const struct tee_file_operations *fops;
254 	void *attr = NULL;
255 
256 	if (o == NULL || o->pobj == NULL)
257 		return TEE_ERROR_BAD_PARAMETERS;
258 
259 	fops = o->pobj->fops;
260 
261 	file = tee_svc_storage_create_filename(sess,
262 					       o->pobj->obj_id,
263 					       o->pobj->obj_id_len,
264 					       false);
265 	if (file == NULL) {
266 		res = TEE_ERROR_OUT_OF_MEMORY;
267 		goto exit;
268 	}
269 
270 	if (fops->access(file, TEE_FS_F_OK)) {
271 		/* file not found */
272 		res = TEE_ERROR_ITEM_NOT_FOUND;
273 		goto exit;
274 	}
275 
276 	assert(!o->fh);
277 	res = fops->open(file, &o->fh);
278 	if (res != TEE_SUCCESS)
279 		goto exit;
280 
281 	/* read head */
282 	bytes = sizeof(struct tee_svc_storage_head);
283 	res = fops->read(o->fh, &head, &bytes);
284 	if (res != TEE_SUCCESS) {
285 		if (res == TEE_ERROR_CORRUPT_OBJECT)
286 			EMSG("Head corrupt\n");
287 		goto exit;
288 	}
289 
290 	if (bytes != sizeof(struct tee_svc_storage_head)) {
291 		res = TEE_ERROR_BAD_FORMAT;
292 		goto exit;
293 	}
294 
295 	res = tee_obj_set_type(o, head.objectType, head.maxKeySize);
296 	if (res != TEE_SUCCESS)
297 		goto exit;
298 
299 	if (head.meta_size) {
300 		attr = malloc(head.meta_size);
301 		if (!attr) {
302 			res = TEE_ERROR_OUT_OF_MEMORY;
303 			goto exit;
304 		}
305 
306 		/* read meta */
307 		bytes = head.meta_size;
308 		res = fops->read(o->fh, attr, &bytes);
309 		if (res != TEE_SUCCESS || bytes != head.meta_size) {
310 			res = TEE_ERROR_CORRUPT_OBJECT;
311 			goto exit;
312 		}
313 	}
314 
315 	res = tee_obj_attr_from_binary(o, attr, head.meta_size);
316 	if (res != TEE_SUCCESS)
317 		goto exit;
318 
319 	o->info.dataSize = head.ds_size;
320 	o->info.keySize = head.keySize;
321 	o->info.objectUsage = head.objectUsage;
322 	o->info.objectType = head.objectType;
323 	o->have_attrs = head.have_attrs;
324 
325 exit:
326 	free(attr);
327 	free(file);
328 
329 	return res;
330 }
331 
332 
333 static TEE_Result tee_svc_storage_init_file(struct tee_ta_session *sess,
334 					    struct tee_obj *o,
335 					    struct tee_obj *attr_o, void *data,
336 					    uint32_t len)
337 {
338 	TEE_Result res = TEE_SUCCESS;
339 	struct tee_svc_storage_head head;
340 	char *tmpfile = NULL;
341 	const struct tee_file_operations *fops;
342 	void *attr = NULL;
343 	size_t attr_size = 0;
344 
345 	if (o == NULL || o->pobj == NULL)
346 		return TEE_ERROR_BAD_PARAMETERS;
347 
348 	fops = o->pobj->fops;
349 
350 	/* create temporary persistent object filename */
351 	tmpfile = tee_svc_storage_create_filename(sess,
352 						   o->pobj->obj_id,
353 						   o->pobj->obj_id_len,
354 						   true);
355 
356 	if (tmpfile == NULL) {
357 		res = TEE_ERROR_OUT_OF_MEMORY;
358 		goto exit;
359 	}
360 
361 	res = tee_svc_storage_create_file(sess, tmpfile, fops, &o->fh);
362 	if (res != TEE_SUCCESS)
363 		goto exit;
364 
365 	if (attr_o) {
366 		res = tee_obj_set_type(o, attr_o->info.objectType,
367 				       attr_o->info.maxKeySize);
368 		if (res != TEE_SUCCESS)
369 			goto exit;
370 		res = tee_obj_attr_copy_from(o, attr_o);
371 		if (res != TEE_SUCCESS)
372 			goto exit;
373 		o->have_attrs = attr_o->have_attrs;
374 		o->info.objectUsage = attr_o->info.objectUsage;
375 		o->info.keySize = attr_o->info.keySize;
376 		res = tee_obj_attr_to_binary(o, NULL, &attr_size);
377 		if (res != TEE_SUCCESS)
378 			goto exit;
379 		if (attr_size) {
380 			attr = malloc(attr_size);
381 			if (!attr) {
382 				res = TEE_ERROR_OUT_OF_MEMORY;
383 				goto exit;
384 			}
385 			res = tee_obj_attr_to_binary(o, attr, &attr_size);
386 			if (res != TEE_SUCCESS)
387 				goto exit;
388 		}
389 	} else {
390 		res = tee_obj_set_type(o, TEE_TYPE_DATA, 0);
391 		if (res != TEE_SUCCESS)
392 			goto exit;
393 	}
394 
395 	/* write head */
396 	head.magic = TEE_SVC_STORAGE_MAGIC;
397 	head.head_size = sizeof(struct tee_svc_storage_head);
398 	head.meta_size = attr_size;
399 	head.ds_size = len;
400 	head.keySize = o->info.keySize;
401 	head.maxKeySize = o->info.maxKeySize;
402 	head.objectUsage = o->info.objectUsage;
403 	head.objectType = o->info.objectType;
404 	head.have_attrs = o->have_attrs;
405 
406 	/* write head */
407 	res = fops->write(o->fh, &head, sizeof(struct tee_svc_storage_head));
408 	if (res != TEE_SUCCESS)
409 		goto exit;
410 
411 	/* write meta */
412 	res = fops->write(o->fh, attr, attr_size);
413 	if (res != TEE_SUCCESS)
414 		goto exit;
415 
416 	/* write init data */
417 	o->info.dataSize = len;
418 
419 	/* write data to fs if needed */
420 	if (data && len)
421 		res = fops->write(o->fh, data, len);
422 
423 exit:
424 	free(attr);
425 	free(tmpfile);
426 	fops->close(&o->fh);
427 
428 	return res;
429 }
430 
431 TEE_Result syscall_storage_obj_open(unsigned long storage_id, void *object_id,
432 			size_t object_id_len, unsigned long flags,
433 			uint32_t *obj)
434 {
435 	TEE_Result res;
436 	struct tee_ta_session *sess;
437 	struct tee_obj *o = NULL;
438 	char *file = NULL;
439 	struct tee_pobj *po = NULL;
440 	struct user_ta_ctx *utc;
441 	const struct tee_file_operations *fops = file_ops(storage_id);
442 	size_t attr_size;
443 
444 	if (!fops) {
445 		res = TEE_ERROR_ITEM_NOT_FOUND;
446 		goto exit;
447 	}
448 
449 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN) {
450 		res = TEE_ERROR_BAD_PARAMETERS;
451 		goto exit;
452 	}
453 
454 	res = tee_ta_get_current_session(&sess);
455 	if (res != TEE_SUCCESS)
456 		goto err;
457 	utc = to_user_ta_ctx(sess->ctx);
458 
459 	res = tee_mmu_check_access_rights(utc,
460 					  TEE_MEMORY_ACCESS_READ |
461 					  TEE_MEMORY_ACCESS_ANY_OWNER,
462 					  (uaddr_t) object_id,
463 					  object_id_len);
464 	if (res != TEE_SUCCESS)
465 		goto err;
466 
467 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
468 			   object_id_len, flags, fops, &po);
469 	if (res != TEE_SUCCESS)
470 		goto err;
471 
472 	o = tee_obj_alloc();
473 	if (o == NULL) {
474 		tee_pobj_release(po);
475 		res = TEE_ERROR_OUT_OF_MEMORY;
476 		goto err;
477 	}
478 
479 	o->info.handleFlags =
480 	    TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED;
481 	o->flags = flags;
482 	o->pobj = po;
483 	tee_obj_add(utc, o);
484 
485 	res = tee_svc_storage_read_head(sess, o);
486 	if (res != TEE_SUCCESS) {
487 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
488 			EMSG("Object corrupt");
489 			goto err;
490 		}
491 		goto oclose;
492 	}
493 
494 	res = tee_svc_copy_kaddr_to_uref(obj, o);
495 	if (res != TEE_SUCCESS)
496 		goto oclose;
497 
498 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
499 	if (res != TEE_SUCCESS)
500 		goto oclose;
501 
502 	res = fops->seek(o->fh, sizeof(struct tee_svc_storage_head) + attr_size,
503 			 TEE_DATA_SEEK_SET, NULL);
504 	if (res  != TEE_SUCCESS)
505 		goto err;
506 
507 	goto exit;
508 
509 oclose:
510 	tee_obj_close(utc, o);
511 	o = NULL;
512 
513 err:
514 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
515 		res = TEE_ERROR_CORRUPT_OBJECT;
516 	if (res == TEE_ERROR_CORRUPT_OBJECT && o)
517 		tee_svc_storage_remove_corrupt_obj(sess, o);
518 
519 exit:
520 	free(file);
521 	file = NULL;
522 	return res;
523 }
524 
525 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id,
526 			size_t object_id_len, unsigned long flags,
527 			unsigned long attr, void *data, size_t len,
528 			uint32_t *obj)
529 {
530 	TEE_Result res;
531 	struct tee_ta_session *sess;
532 	struct tee_obj *o = NULL;
533 	struct tee_obj *attr_o = NULL;
534 	char *file = NULL;
535 	struct tee_pobj *po = NULL;
536 	char *tmpfile = NULL;
537 	struct user_ta_ctx *utc;
538 	const struct tee_file_operations *fops = file_ops(storage_id);
539 	size_t attr_size;
540 
541 	if (!fops)
542 		return TEE_ERROR_ITEM_NOT_FOUND;
543 
544 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
545 		return TEE_ERROR_BAD_PARAMETERS;
546 
547 	res = tee_ta_get_current_session(&sess);
548 	if (res != TEE_SUCCESS)
549 		return res;
550 	utc = to_user_ta_ctx(sess->ctx);
551 
552 	res = tee_mmu_check_access_rights(utc,
553 					  TEE_MEMORY_ACCESS_READ |
554 					  TEE_MEMORY_ACCESS_ANY_OWNER,
555 					  (uaddr_t) object_id,
556 					  object_id_len);
557 	if (res != TEE_SUCCESS)
558 		goto err;
559 
560 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
561 			   object_id_len, flags, fops, &po);
562 	if (res != TEE_SUCCESS)
563 		goto err;
564 
565 	/* check rights of the provided buffer */
566 	if (data && len) {
567 		res = tee_mmu_check_access_rights(utc,
568 						  TEE_MEMORY_ACCESS_READ |
569 						  TEE_MEMORY_ACCESS_ANY_OWNER,
570 						  (uaddr_t) data, len);
571 
572 		if (res != TEE_SUCCESS)
573 			goto err;
574 	}
575 
576 	o = tee_obj_alloc();
577 	if (o == NULL) {
578 		res = TEE_ERROR_OUT_OF_MEMORY;
579 		goto err;
580 	}
581 
582 	o->info.handleFlags =
583 	    TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED;
584 	o->flags = flags;
585 	o->pobj = po;
586 
587 	if (attr != TEE_HANDLE_NULL) {
588 		res = tee_obj_get(utc, tee_svc_uref_to_vaddr(attr),
589 				  &attr_o);
590 		if (res != TEE_SUCCESS)
591 			goto err;
592 	}
593 
594 	res = tee_svc_storage_init_file(sess, o, attr_o, data, len);
595 	if (res != TEE_SUCCESS)
596 		goto err;
597 
598 	/* create persistent object filename */
599 	file = tee_svc_storage_create_filename(sess, object_id,
600 					       object_id_len, false);
601 	if (file == NULL) {
602 		res = TEE_ERROR_OUT_OF_MEMORY;
603 		goto err;
604 	}
605 
606 	/* create temporary persistent object filename */
607 	tmpfile = tee_svc_storage_create_filename(sess, object_id,
608 						  object_id_len,
609 						  true);
610 	if (tmpfile == NULL) {
611 		res = TEE_ERROR_OUT_OF_MEMORY;
612 		goto err;
613 	}
614 
615 	/* rename temporary persistent object filename */
616 	res = fops->rename(tmpfile, file, !!(flags & TEE_DATA_FLAG_OVERWRITE));
617 	if (res != TEE_SUCCESS)
618 		goto rmfile;
619 
620 	res = fops->open(file, &o->fh);
621 	if (res != TEE_SUCCESS)
622 		goto err;
623 
624 	tee_obj_add(utc, o);
625 
626 	res = tee_svc_copy_kaddr_to_uref(obj, o);
627 	if (res != TEE_SUCCESS)
628 		goto oclose;
629 
630 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
631 	if (res != TEE_SUCCESS)
632 		goto oclose;
633 
634 	res = fops->seek(o->fh, sizeof(struct tee_svc_storage_head) + attr_size,
635 			 TEE_DATA_SEEK_SET, NULL);
636 	if (res != TEE_SUCCESS)
637 		goto oclose;
638 
639 	goto exit;
640 
641 oclose:
642 	tee_obj_close(utc, o);
643 	goto exit;
644 
645 rmfile:
646 	fops->remove(tmpfile);
647 
648 err:
649 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
650 		res = TEE_ERROR_CORRUPT_OBJECT;
651 	if (res == TEE_ERROR_CORRUPT_OBJECT && file)
652 		fops->remove(file);
653 	if (o)
654 		fops->close(&o->fh);
655 	if (po)
656 		tee_pobj_release(po);
657 	free(o);
658 
659 exit:
660 	free(file);
661 	free(tmpfile);
662 
663 	return res;
664 }
665 
666 TEE_Result syscall_storage_obj_del(unsigned long obj)
667 {
668 	TEE_Result res;
669 	struct tee_ta_session *sess;
670 	struct tee_obj *o;
671 	int err;
672 	char *file;
673 	struct user_ta_ctx *utc;
674 	const struct tee_file_operations *fops;
675 
676 	res = tee_ta_get_current_session(&sess);
677 	if (res != TEE_SUCCESS)
678 		return res;
679 	utc = to_user_ta_ctx(sess->ctx);
680 
681 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
682 	if (res != TEE_SUCCESS)
683 		return res;
684 
685 	if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META))
686 		return TEE_ERROR_ACCESS_CONFLICT;
687 
688 	if (o->pobj == NULL || o->pobj->obj_id == NULL)
689 		return TEE_ERROR_BAD_STATE;
690 
691 	file = tee_svc_storage_create_filename(sess, o->pobj->obj_id,
692 						o->pobj->obj_id_len, false);
693 	if (file == NULL)
694 		return TEE_ERROR_OUT_OF_MEMORY;
695 
696 	fops = o->pobj->fops;
697 	tee_obj_close(utc, o);
698 
699 	err = fops->access(file, TEE_FS_F_OK);
700 	if (err)
701 		/* file not found */
702 		return TEE_ERROR_STORAGE_NOT_AVAILABLE;
703 
704 	res = fops->remove(file);
705 	free(file);
706 	if (res != TEE_SUCCESS)
707 		return res;
708 
709 	return TEE_SUCCESS;
710 }
711 
712 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id,
713 			size_t object_id_len)
714 {
715 	TEE_Result res;
716 	struct tee_ta_session *sess;
717 	struct tee_obj *o;
718 	struct tee_pobj *po = NULL;
719 	char *new_file = NULL;
720 	char *old_file = NULL;
721 	struct user_ta_ctx *utc;
722 	const struct tee_file_operations *fops;
723 
724 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
725 		return TEE_ERROR_BAD_PARAMETERS;
726 
727 	res = tee_ta_get_current_session(&sess);
728 	if (res != TEE_SUCCESS)
729 		return res;
730 	utc = to_user_ta_ctx(sess->ctx);
731 
732 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
733 	if (res != TEE_SUCCESS)
734 		return res;
735 
736 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
737 		res = TEE_ERROR_BAD_STATE;
738 		goto exit;
739 	}
740 
741 	if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE_META)) {
742 		res = TEE_ERROR_BAD_STATE;
743 		goto exit;
744 	}
745 
746 	if (o->pobj == NULL || o->pobj->obj_id == NULL) {
747 		res = TEE_ERROR_BAD_STATE;
748 		goto exit;
749 	}
750 
751 	res = tee_mmu_check_access_rights(utc,
752 					TEE_MEMORY_ACCESS_READ |
753 					TEE_MEMORY_ACCESS_ANY_OWNER,
754 					(uaddr_t) object_id, object_id_len);
755 	if (res != TEE_SUCCESS)
756 		goto exit;
757 
758 	/* get new ds name */
759 	new_file = tee_svc_storage_create_filename(sess, object_id,
760 						   object_id_len, false);
761 	if (new_file == NULL) {
762 		res = TEE_ERROR_OUT_OF_MEMORY;
763 		goto exit;
764 	}
765 
766 	old_file = tee_svc_storage_create_filename(sess, o->pobj->obj_id,
767 						   o->pobj->obj_id_len, false);
768 	if (old_file == NULL) {
769 		res = TEE_ERROR_OUT_OF_MEMORY;
770 		goto exit;
771 	}
772 
773 	/* reserve dest name */
774 	fops = o->pobj->fops;
775 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
776 			   object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META,
777 			   fops, &po);
778 	if (res != TEE_SUCCESS)
779 		goto exit;
780 
781 	/* move */
782 	res = fops->rename(old_file, new_file, false /* no overwrite */);
783 	if (res == TEE_ERROR_GENERIC)
784 		goto exit;
785 
786 	res = tee_pobj_rename(o->pobj, object_id, object_id_len);
787 
788 exit:
789 	tee_pobj_release(po);
790 
791 	free(new_file);
792 	free(old_file);
793 
794 	return res;
795 }
796 
797 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum)
798 {
799 	struct tee_storage_enum *e;
800 	struct tee_ta_session *sess;
801 	TEE_Result res;
802 	struct user_ta_ctx *utc;
803 
804 	if (obj_enum == NULL)
805 		return TEE_ERROR_BAD_PARAMETERS;
806 
807 	res = tee_ta_get_current_session(&sess);
808 	if (res != TEE_SUCCESS)
809 		return res;
810 	utc = to_user_ta_ctx(sess->ctx);
811 
812 	e = malloc(sizeof(struct tee_storage_enum));
813 	if (e == NULL)
814 		return TEE_ERROR_OUT_OF_MEMORY;
815 
816 	e->dir = NULL;
817 	e->fops = NULL;
818 	TAILQ_INSERT_TAIL(&utc->storage_enums, e, link);
819 
820 	return tee_svc_copy_kaddr_to_uref(obj_enum, e);
821 }
822 
823 TEE_Result syscall_storage_free_enum(unsigned long obj_enum)
824 {
825 	struct tee_storage_enum *e;
826 	TEE_Result res;
827 	struct tee_ta_session *sess;
828 	struct user_ta_ctx *utc;
829 
830 	res = tee_ta_get_current_session(&sess);
831 	if (res != TEE_SUCCESS)
832 		return res;
833 	utc = to_user_ta_ctx(sess->ctx);
834 
835 	res = tee_svc_storage_get_enum(utc,
836 			tee_svc_uref_to_vaddr(obj_enum), &e);
837 	if (res != TEE_SUCCESS)
838 		return res;
839 
840 	return tee_svc_close_enum(utc, e);
841 }
842 
843 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum)
844 {
845 	struct tee_storage_enum *e;
846 	TEE_Result res;
847 	struct tee_ta_session *sess;
848 
849 	res = tee_ta_get_current_session(&sess);
850 	if (res != TEE_SUCCESS)
851 		return res;
852 
853 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
854 			tee_svc_uref_to_vaddr(obj_enum), &e);
855 	if (res != TEE_SUCCESS)
856 		return res;
857 
858 	e->fops->closedir(e->dir);
859 	e->dir = NULL;
860 
861 	return TEE_SUCCESS;
862 }
863 
864 static TEE_Result tee_svc_storage_set_enum(char *d_name,
865 			const struct tee_file_operations *fops,
866 			struct tee_obj *o)
867 {
868 	TEE_Result res;
869 	uint32_t blen;
870 	uint32_t hslen;
871 
872 	o->info.handleFlags =
873 	    TEE_HANDLE_FLAG_PERSISTENT | TEE_HANDLE_FLAG_INITIALIZED;
874 	o->info.objectUsage = TEE_USAGE_DEFAULT;
875 
876 	hslen = strlen(d_name);
877 	blen = TEE_HS2B_BBUF_SIZE(hslen);
878 	o->pobj->obj_id = malloc(blen);
879 	if (!o->pobj->obj_id) {
880 		res = TEE_ERROR_OUT_OF_MEMORY;
881 		goto exit;
882 	}
883 	tee_hs2b((uint8_t *)d_name, o->pobj->obj_id, hslen, blen);
884 	o->pobj->obj_id_len = blen;
885 	o->pobj->fops = fops;
886 
887 	res = TEE_SUCCESS;
888 
889 exit:
890 	return res;
891 
892 }
893 
894 TEE_Result syscall_storage_start_enum(unsigned long obj_enum,
895 				      unsigned long storage_id)
896 {
897 	struct tee_storage_enum *e;
898 	char *dir;
899 	TEE_Result res;
900 	struct tee_ta_session *sess;
901 	const struct tee_file_operations *fops = file_ops(storage_id);
902 
903 	res = tee_ta_get_current_session(&sess);
904 	if (res != TEE_SUCCESS)
905 		return res;
906 
907 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
908 			tee_svc_uref_to_vaddr(obj_enum), &e);
909 	if (res != TEE_SUCCESS)
910 		return res;
911 
912 	if (!fops)
913 		return TEE_ERROR_ITEM_NOT_FOUND;
914 
915 	e->fops = fops;
916 	dir = tee_svc_storage_create_dirname(sess);
917 	if (dir == NULL) {
918 		res = TEE_ERROR_OUT_OF_MEMORY;
919 		goto exit;
920 	}
921 
922 	assert(!e->dir);
923 	res = fops->opendir(dir, &e->dir);
924 	free(dir);
925 exit:
926 	return res;
927 }
928 
929 TEE_Result syscall_storage_next_enum(unsigned long obj_enum,
930 			TEE_ObjectInfo *info, void *obj_id, uint64_t *len)
931 {
932 	struct tee_storage_enum *e;
933 	struct tee_fs_dirent *d;
934 	TEE_Result res = TEE_SUCCESS;
935 	struct tee_ta_session *sess;
936 	struct tee_obj *o = NULL;
937 	uint64_t l;
938 	struct user_ta_ctx *utc;
939 
940 	res = tee_ta_get_current_session(&sess);
941 	if (res != TEE_SUCCESS)
942 		goto exit;
943 	utc = to_user_ta_ctx(sess->ctx);
944 
945 	res = tee_svc_storage_get_enum(utc,
946 			tee_svc_uref_to_vaddr(obj_enum), &e);
947 	if (res != TEE_SUCCESS)
948 		goto exit;
949 
950 	/* check rights of the provided buffers */
951 	res = tee_mmu_check_access_rights(utc,
952 					TEE_MEMORY_ACCESS_WRITE |
953 					TEE_MEMORY_ACCESS_ANY_OWNER,
954 					(uaddr_t) info,
955 					sizeof(TEE_ObjectInfo));
956 	if (res != TEE_SUCCESS)
957 		goto exit;
958 
959 	res = tee_mmu_check_access_rights(utc,
960 					TEE_MEMORY_ACCESS_WRITE |
961 					TEE_MEMORY_ACCESS_ANY_OWNER,
962 					(uaddr_t) obj_id,
963 					TEE_OBJECT_ID_MAX_LEN);
964 	if (res != TEE_SUCCESS)
965 		goto exit;
966 
967 	if (!e->fops) {
968 		res = TEE_ERROR_ITEM_NOT_FOUND;
969 		goto exit;
970 	}
971 
972 	res = e->fops->readdir(e->dir, &d);
973 	if (res != TEE_SUCCESS)
974 		goto exit;
975 
976 	o = tee_obj_alloc();
977 	if (o == NULL) {
978 		res = TEE_ERROR_OUT_OF_MEMORY;
979 		goto exit;
980 	}
981 	o->flags = TEE_DATA_FLAG_SHARE_READ;
982 
983 	o->pobj = calloc(1, sizeof(struct tee_pobj));
984 	if (!o->pobj) {
985 		res = TEE_ERROR_OUT_OF_MEMORY;
986 		goto exit;
987 	}
988 
989 	res = tee_svc_storage_set_enum(d->d_name, e->fops, o);
990 	if (res != TEE_SUCCESS)
991 		goto exit;
992 
993 	res = tee_svc_storage_read_head(sess, o);
994 	if (res != TEE_SUCCESS)
995 		goto exit;
996 
997 	memcpy(info, &o->info, sizeof(TEE_ObjectInfo));
998 	memcpy(obj_id, o->pobj->obj_id, o->pobj->obj_id_len);
999 
1000 	l = o->pobj->obj_id_len;
1001 	res = tee_svc_copy_to_user(len, &l, sizeof(*len));
1002 
1003 exit:
1004 	if (o) {
1005 		if (o->pobj) {
1006 			o->pobj->fops->close(&o->fh);
1007 			free(o->pobj->obj_id);
1008 		}
1009 		free(o->pobj);
1010 		tee_obj_free(o);
1011 	}
1012 
1013 	return res;
1014 }
1015 
1016 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len,
1017 			uint64_t *count)
1018 {
1019 	TEE_Result res;
1020 	struct tee_ta_session *sess;
1021 	struct tee_obj *o;
1022 	uint64_t u_count;
1023 	struct user_ta_ctx *utc;
1024 	size_t bytes;
1025 
1026 	res = tee_ta_get_current_session(&sess);
1027 	if (res != TEE_SUCCESS)
1028 		goto exit;
1029 	utc = to_user_ta_ctx(sess->ctx);
1030 
1031 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
1032 	if (res != TEE_SUCCESS)
1033 		goto exit;
1034 
1035 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
1036 		res = TEE_ERROR_BAD_STATE;
1037 		goto exit;
1038 	}
1039 
1040 	if (!(o->flags & TEE_DATA_FLAG_ACCESS_READ)) {
1041 		res = TEE_ERROR_ACCESS_CONFLICT;
1042 		goto exit;
1043 	}
1044 
1045 	/* check rights of the provided buffer */
1046 	res = tee_mmu_check_access_rights(utc,
1047 					TEE_MEMORY_ACCESS_WRITE |
1048 					TEE_MEMORY_ACCESS_ANY_OWNER,
1049 					(uaddr_t) data, len);
1050 	if (res != TEE_SUCCESS)
1051 		goto exit;
1052 
1053 	bytes = len;
1054 	res = o->pobj->fops->read(o->fh, data, &bytes);
1055 	if (res != TEE_SUCCESS) {
1056 		EMSG("Error code=%x\n", (uint32_t)res);
1057 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
1058 			EMSG("Object corrupt\n");
1059 			tee_svc_storage_remove_corrupt_obj(sess, o);
1060 		}
1061 		goto exit;
1062 	}
1063 
1064 	o->info.dataPosition += bytes;
1065 
1066 	u_count = bytes;
1067 	res = tee_svc_copy_to_user(count, &u_count, sizeof(*count));
1068 exit:
1069 	return res;
1070 }
1071 
1072 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len)
1073 {
1074 	TEE_Result res;
1075 	struct tee_ta_session *sess;
1076 	struct tee_obj *o;
1077 	struct user_ta_ctx *utc;
1078 
1079 	res = tee_ta_get_current_session(&sess);
1080 	if (res != TEE_SUCCESS)
1081 		goto exit;
1082 	utc = to_user_ta_ctx(sess->ctx);
1083 
1084 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
1085 	if (res != TEE_SUCCESS)
1086 		goto exit;
1087 
1088 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
1089 		res = TEE_ERROR_BAD_STATE;
1090 		goto exit;
1091 	}
1092 
1093 	if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) {
1094 		res = TEE_ERROR_ACCESS_CONFLICT;
1095 		goto exit;
1096 	}
1097 
1098 	/* check rights of the provided buffer */
1099 	res = tee_mmu_check_access_rights(utc,
1100 					TEE_MEMORY_ACCESS_READ |
1101 					TEE_MEMORY_ACCESS_ANY_OWNER,
1102 					(uaddr_t) data, len);
1103 	if (res != TEE_SUCCESS)
1104 		goto exit;
1105 
1106 	res = o->pobj->fops->write(o->fh, data, len);
1107 	if (res != TEE_SUCCESS)
1108 		goto exit;
1109 
1110 	o->info.dataPosition += len;
1111 	if (o->info.dataPosition > o->info.dataSize)
1112 		o->info.dataSize = o->info.dataPosition;
1113 
1114 exit:
1115 	return res;
1116 }
1117 
1118 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len)
1119 {
1120 	TEE_Result res;
1121 	struct tee_ta_session *sess;
1122 	struct tee_obj *o;
1123 	size_t off;
1124 	size_t attr_size;
1125 
1126 	res = tee_ta_get_current_session(&sess);
1127 	if (res != TEE_SUCCESS)
1128 		goto exit;
1129 
1130 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1131 			  tee_svc_uref_to_vaddr(obj), &o);
1132 	if (res != TEE_SUCCESS)
1133 		goto exit;
1134 
1135 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
1136 		res = TEE_ERROR_BAD_STATE;
1137 		goto exit;
1138 	}
1139 
1140 	if (!(o->flags & TEE_DATA_FLAG_ACCESS_WRITE)) {
1141 		res = TEE_ERROR_ACCESS_CONFLICT;
1142 		goto exit;
1143 	}
1144 
1145 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
1146 	if (res != TEE_SUCCESS)
1147 		goto exit;
1148 
1149 	off = sizeof(struct tee_svc_storage_head) + attr_size;
1150 	res = o->pobj->fops->truncate(o->fh, len + off);
1151 	if (res != TEE_SUCCESS) {
1152 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
1153 			EMSG("Object corrupt\n");
1154 			res = tee_svc_storage_remove_corrupt_obj(sess, o);
1155 			if (res != TEE_SUCCESS)
1156 				goto exit;
1157 			res = TEE_ERROR_CORRUPT_OBJECT;
1158 			goto exit;
1159 		} else
1160 			res = TEE_ERROR_GENERIC;
1161 	}
1162 
1163 exit:
1164 	return res;
1165 }
1166 
1167 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset,
1168 				    unsigned long whence)
1169 {
1170 	TEE_Result res;
1171 	struct tee_ta_session *sess;
1172 	struct tee_obj *o;
1173 	int32_t off;
1174 	size_t attr_size;
1175 
1176 	res = tee_ta_get_current_session(&sess);
1177 	if (res != TEE_SUCCESS)
1178 		goto exit;
1179 
1180 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1181 			  tee_svc_uref_to_vaddr(obj), &o);
1182 	if (res != TEE_SUCCESS)
1183 		goto exit;
1184 
1185 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
1186 		res = TEE_ERROR_BAD_STATE;
1187 		goto exit;
1188 	}
1189 
1190 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
1191 	if (res != TEE_SUCCESS)
1192 		goto exit;
1193 
1194 	off = offset;
1195 	if (whence == TEE_DATA_SEEK_SET)
1196 		off += sizeof(struct tee_svc_storage_head) + attr_size;
1197 
1198 	res = o->pobj->fops->seek(o->fh, off, whence, &off);
1199 	if (res != TEE_SUCCESS)
1200 		goto exit;
1201 	o->info.dataPosition = off - (sizeof(struct tee_svc_storage_head) +
1202 				      attr_size);
1203 
1204 exit:
1205 	return res;
1206 }
1207 
1208 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc)
1209 {
1210 	struct tee_storage_enum_head *eh = &utc->storage_enums;
1211 
1212 	/* disregard return value */
1213 	while (!TAILQ_EMPTY(eh))
1214 		tee_svc_close_enum(utc, TAILQ_FIRST(eh));
1215 }
1216