xref: /optee_os/core/tee/tee_svc_storage.c (revision a1d5c81f8834a9d2c6f4372cce2e59e70e709121)
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 unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ |
260 					  TEE_DATA_FLAG_ACCESS_WRITE |
261 					  TEE_DATA_FLAG_ACCESS_WRITE_META |
262 					  TEE_DATA_FLAG_SHARE_READ |
263 					  TEE_DATA_FLAG_SHARE_WRITE;
264 	const struct tee_file_operations *fops =
265 			tee_svc_storage_file_ops(storage_id);
266 	struct tee_ta_session *sess = NULL;
267 	struct user_ta_ctx *utc = NULL;
268 	TEE_Result res = TEE_SUCCESS;
269 	struct tee_pobj *po = NULL;
270 	struct tee_obj *o = NULL;
271 	char *file = NULL;
272 
273 	if (flags & ~valid_flags)
274 		return TEE_ERROR_BAD_PARAMETERS;
275 
276 	if (!fops) {
277 		res = TEE_ERROR_ITEM_NOT_FOUND;
278 		goto exit;
279 	}
280 
281 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN) {
282 		res = TEE_ERROR_BAD_PARAMETERS;
283 		goto exit;
284 	}
285 
286 	res = tee_ta_get_current_session(&sess);
287 	if (res != TEE_SUCCESS)
288 		goto err;
289 	utc = to_user_ta_ctx(sess->ctx);
290 
291 	res = tee_mmu_check_access_rights(&utc->uctx,
292 					  TEE_MEMORY_ACCESS_READ,
293 					  (uaddr_t) object_id,
294 					  object_id_len);
295 	if (res != TEE_SUCCESS)
296 		goto err;
297 
298 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
299 			   object_id_len, flags, TEE_POBJ_USAGE_OPEN, fops,
300 			   &po);
301 	if (res != TEE_SUCCESS)
302 		goto err;
303 
304 	o = tee_obj_alloc();
305 	if (o == NULL) {
306 		tee_pobj_release(po);
307 		res = TEE_ERROR_OUT_OF_MEMORY;
308 		goto err;
309 	}
310 
311 	o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT |
312 			      TEE_HANDLE_FLAG_INITIALIZED | flags;
313 	o->pobj = po;
314 	tee_obj_add(utc, o);
315 
316 	res = tee_svc_storage_read_head(o);
317 	if (res != TEE_SUCCESS) {
318 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
319 			EMSG("Object corrupt");
320 			goto err;
321 		}
322 		goto oclose;
323 	}
324 
325 	res = tee_svc_copy_kaddr_to_uref(obj, o);
326 	if (res != TEE_SUCCESS)
327 		goto oclose;
328 
329 	goto exit;
330 
331 oclose:
332 	tee_obj_close(utc, o);
333 	o = NULL;
334 
335 err:
336 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
337 		res = TEE_ERROR_CORRUPT_OBJECT;
338 	if (res == TEE_ERROR_CORRUPT_OBJECT && o)
339 		tee_svc_storage_remove_corrupt_obj(sess, o);
340 
341 exit:
342 	free(file);
343 	file = NULL;
344 	return res;
345 }
346 
347 static TEE_Result tee_svc_storage_init_file(struct tee_obj *o, bool overwrite,
348 					    struct tee_obj *attr_o, void *data,
349 					    uint32_t len)
350 {
351 	TEE_Result res = TEE_SUCCESS;
352 	struct tee_svc_storage_head head;
353 	const struct tee_file_operations *fops = o->pobj->fops;
354 	void *attr = NULL;
355 	size_t attr_size = 0;
356 
357 	if (attr_o) {
358 		res = tee_obj_set_type(o, attr_o->info.objectType,
359 				       attr_o->info.maxKeySize);
360 		if (res)
361 			return res;
362 		res = tee_obj_attr_copy_from(o, attr_o);
363 		if (res)
364 			return res;
365 		o->have_attrs = attr_o->have_attrs;
366 		o->info.objectUsage = attr_o->info.objectUsage;
367 		o->info.keySize = attr_o->info.keySize;
368 		res = tee_obj_attr_to_binary(o, NULL, &attr_size);
369 		if (res)
370 			return res;
371 		if (attr_size) {
372 			attr = malloc(attr_size);
373 			if (!attr)
374 				return TEE_ERROR_OUT_OF_MEMORY;
375 			res = tee_obj_attr_to_binary(o, attr, &attr_size);
376 			if (res != TEE_SUCCESS)
377 				goto exit;
378 		}
379 	} else {
380 		res = tee_obj_set_type(o, TEE_TYPE_DATA, 0);
381 		if (res != TEE_SUCCESS)
382 			goto exit;
383 	}
384 
385 	o->ds_pos = sizeof(struct tee_svc_storage_head) + attr_size;
386 
387 	/* write head */
388 	head.attr_size = attr_size;
389 	head.keySize = o->info.keySize;
390 	head.maxKeySize = o->info.maxKeySize;
391 	head.objectUsage = o->info.objectUsage;
392 	head.objectType = o->info.objectType;
393 	head.have_attrs = o->have_attrs;
394 
395 	res = fops->create(o->pobj, overwrite, &head, sizeof(head), attr,
396 			   attr_size, data, len, &o->fh);
397 
398 	if (!res)
399 		o->info.dataSize = len;
400 exit:
401 	free(attr);
402 	return res;
403 }
404 
405 TEE_Result syscall_storage_obj_create(unsigned long storage_id, void *object_id,
406 			size_t object_id_len, unsigned long flags,
407 			unsigned long attr, void *data, size_t len,
408 			uint32_t *obj)
409 {
410 	const unsigned long valid_flags = TEE_DATA_FLAG_ACCESS_READ |
411 					  TEE_DATA_FLAG_ACCESS_WRITE |
412 					  TEE_DATA_FLAG_ACCESS_WRITE_META |
413 					  TEE_DATA_FLAG_SHARE_READ |
414 					  TEE_DATA_FLAG_SHARE_WRITE |
415 					  TEE_DATA_FLAG_OVERWRITE;
416 	const struct tee_file_operations *fops =
417 			tee_svc_storage_file_ops(storage_id);
418 	struct tee_ta_session *sess = NULL;
419 	struct user_ta_ctx *utc = NULL;
420 	struct tee_obj *attr_o = NULL;
421 	TEE_Result res = TEE_SUCCESS;
422 	struct tee_pobj *po = NULL;
423 	struct tee_obj *o = NULL;
424 
425 	if (flags & ~valid_flags)
426 		return TEE_ERROR_BAD_PARAMETERS;
427 
428 	if (!fops)
429 		return TEE_ERROR_ITEM_NOT_FOUND;
430 
431 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
432 		return TEE_ERROR_BAD_PARAMETERS;
433 
434 	res = tee_ta_get_current_session(&sess);
435 	if (res != TEE_SUCCESS)
436 		return res;
437 	utc = to_user_ta_ctx(sess->ctx);
438 
439 	res = tee_mmu_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_READ,
440 					  (uaddr_t)object_id, object_id_len);
441 	if (res != TEE_SUCCESS)
442 		goto err;
443 
444 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
445 			   object_id_len, flags, TEE_POBJ_USAGE_CREATE,
446 			   fops, &po);
447 	if (res != TEE_SUCCESS)
448 		goto err;
449 
450 	/* check rights of the provided buffer */
451 	if (len) {
452 		if (data) {
453 			uint32_t f = TEE_MEMORY_ACCESS_READ |
454 				     TEE_MEMORY_ACCESS_ANY_OWNER;
455 
456 			res = tee_mmu_check_access_rights(&utc->uctx, f,
457 							  (uaddr_t)data, len);
458 
459 			if (res != TEE_SUCCESS)
460 				goto err;
461 		} else {
462 			res = TEE_ERROR_BAD_PARAMETERS;
463 			goto err;
464 		}
465 	}
466 
467 	o = tee_obj_alloc();
468 	if (o == NULL) {
469 		res = TEE_ERROR_OUT_OF_MEMORY;
470 		goto err;
471 	}
472 
473 	o->info.handleFlags = TEE_HANDLE_FLAG_PERSISTENT |
474 			      TEE_HANDLE_FLAG_INITIALIZED | flags;
475 	o->pobj = po;
476 
477 	if (attr != TEE_HANDLE_NULL) {
478 		res = tee_obj_get(utc, tee_svc_uref_to_vaddr(attr),
479 				  &attr_o);
480 		if (res != TEE_SUCCESS)
481 			goto err;
482 		/* The supplied handle must be one of an initialized object */
483 		if (!(attr_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED)) {
484 			res = TEE_ERROR_BAD_PARAMETERS;
485 			goto err;
486 		}
487 	}
488 
489 	res = tee_svc_storage_init_file(o, flags & TEE_DATA_FLAG_OVERWRITE,
490 					attr_o, data, len);
491 	if (res != TEE_SUCCESS)
492 		goto err;
493 
494 	po = NULL; /* o owns it from now on */
495 	tee_obj_add(utc, o);
496 
497 	res = tee_svc_copy_kaddr_to_uref(obj, o);
498 	if (res != TEE_SUCCESS)
499 		goto oclose;
500 
501 	tee_pobj_create_final(o->pobj);
502 	return TEE_SUCCESS;
503 
504 oclose:
505 	tee_obj_close(utc, o);
506 	return res;
507 
508 err:
509 	if (res == TEE_ERROR_NO_DATA || res == TEE_ERROR_BAD_FORMAT)
510 		res = TEE_ERROR_CORRUPT_OBJECT;
511 	if (res == TEE_ERROR_CORRUPT_OBJECT && po)
512 		fops->remove(po);
513 	if (o) {
514 		fops->close(&o->fh);
515 		tee_obj_free(o);
516 	}
517 	if (po)
518 		tee_pobj_release(po);
519 
520 	return res;
521 }
522 
523 TEE_Result syscall_storage_obj_del(unsigned long obj)
524 {
525 	TEE_Result res;
526 	struct tee_ta_session *sess;
527 	struct tee_obj *o;
528 	struct user_ta_ctx *utc;
529 
530 	res = tee_ta_get_current_session(&sess);
531 	if (res != TEE_SUCCESS)
532 		return res;
533 	utc = to_user_ta_ctx(sess->ctx);
534 
535 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
536 	if (res != TEE_SUCCESS)
537 		return res;
538 
539 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META))
540 		return TEE_ERROR_ACCESS_CONFLICT;
541 
542 	if (o->pobj == NULL || o->pobj->obj_id == NULL)
543 		return TEE_ERROR_BAD_STATE;
544 
545 	res = o->pobj->fops->remove(o->pobj);
546 	tee_obj_close(utc, o);
547 
548 	return res;
549 }
550 
551 TEE_Result syscall_storage_obj_rename(unsigned long obj, void *object_id,
552 				      size_t object_id_len)
553 {
554 	const struct tee_file_operations *fops = NULL;
555 	struct tee_ta_session *sess = NULL;
556 	struct user_ta_ctx *utc = NULL;
557 	TEE_Result res = TEE_SUCCESS;
558 	struct tee_pobj *po = NULL;
559 	struct tee_obj *o = NULL;
560 	char *new_file = NULL;
561 	char *old_file = NULL;
562 
563 	if (object_id_len > TEE_OBJECT_ID_MAX_LEN)
564 		return TEE_ERROR_BAD_PARAMETERS;
565 
566 	res = tee_ta_get_current_session(&sess);
567 	if (res != TEE_SUCCESS)
568 		return res;
569 	utc = to_user_ta_ctx(sess->ctx);
570 
571 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
572 	if (res != TEE_SUCCESS)
573 		return res;
574 
575 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
576 		res = TEE_ERROR_BAD_STATE;
577 		goto exit;
578 	}
579 
580 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE_META)) {
581 		res = TEE_ERROR_BAD_STATE;
582 		goto exit;
583 	}
584 
585 	if (o->pobj == NULL || o->pobj->obj_id == NULL) {
586 		res = TEE_ERROR_BAD_STATE;
587 		goto exit;
588 	}
589 
590 	res = tee_mmu_check_access_rights(&utc->uctx, TEE_MEMORY_ACCESS_READ,
591 					  (uaddr_t)object_id, object_id_len);
592 	if (res != TEE_SUCCESS)
593 		goto exit;
594 
595 	/* reserve dest name */
596 	fops = o->pobj->fops;
597 	res = tee_pobj_get((void *)&sess->ctx->uuid, object_id,
598 			   object_id_len, TEE_DATA_FLAG_ACCESS_WRITE_META,
599 			   TEE_POBJ_USAGE_RENAME, fops, &po);
600 	if (res != TEE_SUCCESS)
601 		goto exit;
602 
603 	/* move */
604 	res = fops->rename(o->pobj, po, false /* no overwrite */);
605 	if (res)
606 		goto exit;
607 
608 	res = tee_pobj_rename(o->pobj, object_id, object_id_len);
609 
610 exit:
611 	tee_pobj_release(po);
612 
613 	free(new_file);
614 	free(old_file);
615 
616 	return res;
617 }
618 
619 TEE_Result syscall_storage_alloc_enum(uint32_t *obj_enum)
620 {
621 	struct tee_storage_enum *e;
622 	struct tee_ta_session *sess;
623 	TEE_Result res;
624 	struct user_ta_ctx *utc;
625 
626 	if (obj_enum == NULL)
627 		return TEE_ERROR_BAD_PARAMETERS;
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 	e = malloc(sizeof(struct tee_storage_enum));
635 	if (e == NULL)
636 		return TEE_ERROR_OUT_OF_MEMORY;
637 
638 	e->dir = NULL;
639 	e->fops = NULL;
640 	TAILQ_INSERT_TAIL(&utc->storage_enums, e, link);
641 
642 	return tee_svc_copy_kaddr_to_uref(obj_enum, e);
643 }
644 
645 TEE_Result syscall_storage_free_enum(unsigned long obj_enum)
646 {
647 	struct tee_storage_enum *e;
648 	TEE_Result res;
649 	struct tee_ta_session *sess;
650 	struct user_ta_ctx *utc;
651 
652 	res = tee_ta_get_current_session(&sess);
653 	if (res != TEE_SUCCESS)
654 		return res;
655 	utc = to_user_ta_ctx(sess->ctx);
656 
657 	res = tee_svc_storage_get_enum(utc,
658 			tee_svc_uref_to_vaddr(obj_enum), &e);
659 	if (res != TEE_SUCCESS)
660 		return res;
661 
662 	return tee_svc_close_enum(utc, e);
663 }
664 
665 TEE_Result syscall_storage_reset_enum(unsigned long obj_enum)
666 {
667 	struct tee_storage_enum *e;
668 	TEE_Result res;
669 	struct tee_ta_session *sess;
670 
671 	res = tee_ta_get_current_session(&sess);
672 	if (res != TEE_SUCCESS)
673 		return res;
674 
675 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
676 			tee_svc_uref_to_vaddr(obj_enum), &e);
677 	if (res != TEE_SUCCESS)
678 		return res;
679 
680 	if (e->fops) {
681 		e->fops->closedir(e->dir);
682 		e->fops = NULL;
683 		e->dir = NULL;
684 	}
685 	assert(!e->dir);
686 
687 	return TEE_SUCCESS;
688 }
689 
690 TEE_Result syscall_storage_start_enum(unsigned long obj_enum,
691 				      unsigned long storage_id)
692 {
693 	struct tee_storage_enum *e;
694 	TEE_Result res;
695 	struct tee_ta_session *sess;
696 	const struct tee_file_operations *fops =
697 			tee_svc_storage_file_ops(storage_id);
698 
699 	res = tee_ta_get_current_session(&sess);
700 	if (res != TEE_SUCCESS)
701 		return res;
702 
703 	res = tee_svc_storage_get_enum(to_user_ta_ctx(sess->ctx),
704 			tee_svc_uref_to_vaddr(obj_enum), &e);
705 	if (res != TEE_SUCCESS)
706 		return res;
707 
708 	if (e->dir) {
709 		e->fops->closedir(e->dir);
710 		e->dir = NULL;
711 	}
712 
713 	if (!fops)
714 		return TEE_ERROR_ITEM_NOT_FOUND;
715 
716 	e->fops = fops;
717 
718 	return fops->opendir(&sess->ctx->uuid, &e->dir);
719 }
720 
721 TEE_Result syscall_storage_next_enum(unsigned long obj_enum,
722 			TEE_ObjectInfo *info, void *obj_id, uint64_t *len)
723 {
724 	struct tee_ta_session *sess = NULL;
725 	struct tee_storage_enum *e = NULL;
726 	struct tee_fs_dirent *d = NULL;
727 	struct user_ta_ctx *utc = NULL;
728 	TEE_Result res = TEE_SUCCESS;
729 	struct tee_obj *o = NULL;
730 	uint64_t l = 0;
731 
732 	res = tee_ta_get_current_session(&sess);
733 	if (res != TEE_SUCCESS)
734 		goto exit;
735 	utc = to_user_ta_ctx(sess->ctx);
736 
737 	res = tee_svc_storage_get_enum(utc,
738 			tee_svc_uref_to_vaddr(obj_enum), &e);
739 	if (res != TEE_SUCCESS)
740 		goto exit;
741 
742 	/* check rights of the provided buffers */
743 	res = tee_mmu_check_access_rights(&utc->uctx,
744 					  TEE_MEMORY_ACCESS_WRITE |
745 					  TEE_MEMORY_ACCESS_ANY_OWNER,
746 					  (uaddr_t)info,
747 					  sizeof(TEE_ObjectInfo));
748 	if (res != TEE_SUCCESS)
749 		goto exit;
750 
751 	res = tee_mmu_check_access_rights(&utc->uctx,
752 					  TEE_MEMORY_ACCESS_WRITE |
753 					  TEE_MEMORY_ACCESS_ANY_OWNER,
754 					  (uaddr_t)obj_id,
755 					  TEE_OBJECT_ID_MAX_LEN);
756 	if (res != TEE_SUCCESS)
757 		goto exit;
758 
759 	if (!e->fops) {
760 		res = TEE_ERROR_ITEM_NOT_FOUND;
761 		goto exit;
762 	}
763 
764 	res = e->fops->readdir(e->dir, &d);
765 	if (res != TEE_SUCCESS)
766 		goto exit;
767 
768 	o = tee_obj_alloc();
769 	if (o == NULL) {
770 		res = TEE_ERROR_OUT_OF_MEMORY;
771 		goto exit;
772 	}
773 
774 	res = tee_pobj_get(&sess->ctx->uuid, d->oid, d->oidlen, 0,
775 			   TEE_POBJ_USAGE_ENUM, e->fops, &o->pobj);
776 	if (res)
777 		goto exit;
778 
779 	o->info.handleFlags = o->pobj->flags | TEE_HANDLE_FLAG_PERSISTENT |
780 			      TEE_HANDLE_FLAG_INITIALIZED;
781 
782 	res = tee_svc_storage_read_head(o);
783 	if (res != TEE_SUCCESS)
784 		goto exit;
785 
786 	memcpy(info, &o->info, sizeof(TEE_ObjectInfo));
787 	memcpy(obj_id, o->pobj->obj_id, o->pobj->obj_id_len);
788 
789 	l = o->pobj->obj_id_len;
790 	res = tee_svc_copy_to_user(len, &l, sizeof(*len));
791 
792 exit:
793 	if (o) {
794 		tee_pobj_release(o->pobj);
795 		tee_obj_free(o);
796 	}
797 
798 	return res;
799 }
800 
801 TEE_Result syscall_storage_obj_read(unsigned long obj, void *data, size_t len,
802 				    uint64_t *count)
803 {
804 	struct tee_ta_session *sess = NULL;
805 	struct user_ta_ctx *utc = NULL;
806 	TEE_Result res = TEE_SUCCESS;
807 	struct tee_obj *o = NULL;
808 	uint64_t u_count = 0;
809 	size_t pos_tmp = 0;
810 	size_t bytes = 0;
811 
812 	res = tee_ta_get_current_session(&sess);
813 	if (res != TEE_SUCCESS)
814 		goto exit;
815 	utc = to_user_ta_ctx(sess->ctx);
816 
817 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
818 	if (res != TEE_SUCCESS)
819 		goto exit;
820 
821 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
822 		res = TEE_ERROR_BAD_STATE;
823 		goto exit;
824 	}
825 
826 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_READ)) {
827 		res = TEE_ERROR_ACCESS_CONFLICT;
828 		goto exit;
829 	}
830 
831 	/* Guard o->info.dataPosition += bytes below from overflowing */
832 	if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) {
833 		res = TEE_ERROR_OVERFLOW;
834 		goto exit;
835 	}
836 
837 	/* check rights of the provided buffer */
838 	res = tee_mmu_check_access_rights(&utc->uctx,
839 					  TEE_MEMORY_ACCESS_WRITE |
840 					  TEE_MEMORY_ACCESS_ANY_OWNER,
841 					  (uaddr_t)data, len);
842 	if (res != TEE_SUCCESS)
843 		goto exit;
844 
845 	bytes = len;
846 	if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) {
847 		res = TEE_ERROR_OVERFLOW;
848 		goto exit;
849 	}
850 	res = o->pobj->fops->read(o->fh, pos_tmp, data, &bytes);
851 	if (res != TEE_SUCCESS) {
852 		if (res == TEE_ERROR_CORRUPT_OBJECT) {
853 			EMSG("Object corrupt");
854 			tee_svc_storage_remove_corrupt_obj(sess, o);
855 		}
856 		goto exit;
857 	}
858 
859 	o->info.dataPosition += bytes;
860 
861 	u_count = bytes;
862 	res = tee_svc_copy_to_user(count, &u_count, sizeof(*count));
863 exit:
864 	return res;
865 }
866 
867 TEE_Result syscall_storage_obj_write(unsigned long obj, void *data, size_t len)
868 {
869 	struct tee_ta_session *sess = NULL;
870 	struct user_ta_ctx *utc = NULL;
871 	TEE_Result res = TEE_SUCCESS;
872 	struct tee_obj *o = NULL;
873 	size_t pos_tmp = 0;
874 
875 	res = tee_ta_get_current_session(&sess);
876 	if (res != TEE_SUCCESS)
877 		goto exit;
878 	utc = to_user_ta_ctx(sess->ctx);
879 
880 	res = tee_obj_get(utc, tee_svc_uref_to_vaddr(obj), &o);
881 	if (res != TEE_SUCCESS)
882 		goto exit;
883 
884 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
885 		res = TEE_ERROR_BAD_STATE;
886 		goto exit;
887 	}
888 
889 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) {
890 		res = TEE_ERROR_ACCESS_CONFLICT;
891 		goto exit;
892 	}
893 
894 	/* Guard o->info.dataPosition += bytes below from overflowing */
895 	if (ADD_OVERFLOW(o->info.dataPosition, len, &pos_tmp)) {
896 		res = TEE_ERROR_OVERFLOW;
897 		goto exit;
898 	}
899 
900 	/* check rights of the provided buffer */
901 	res = tee_mmu_check_access_rights(&utc->uctx,
902 					  TEE_MEMORY_ACCESS_READ |
903 					  TEE_MEMORY_ACCESS_ANY_OWNER,
904 					  (uaddr_t)data, len);
905 	if (res != TEE_SUCCESS)
906 		goto exit;
907 
908 	if (ADD_OVERFLOW(o->ds_pos, o->info.dataPosition, &pos_tmp)) {
909 		res = TEE_ERROR_ACCESS_CONFLICT;
910 		goto exit;
911 	}
912 	res = o->pobj->fops->write(o->fh, pos_tmp, data, len);
913 	if (res != TEE_SUCCESS)
914 		goto exit;
915 
916 	o->info.dataPosition += len;
917 	if (o->info.dataPosition > o->info.dataSize)
918 		o->info.dataSize = o->info.dataPosition;
919 
920 exit:
921 	return res;
922 }
923 
924 TEE_Result syscall_storage_obj_trunc(unsigned long obj, size_t len)
925 {
926 	TEE_Result res;
927 	struct tee_ta_session *sess;
928 	struct tee_obj *o;
929 	size_t off;
930 	size_t attr_size;
931 
932 	res = tee_ta_get_current_session(&sess);
933 	if (res != TEE_SUCCESS)
934 		goto exit;
935 
936 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
937 			  tee_svc_uref_to_vaddr(obj), &o);
938 	if (res != TEE_SUCCESS)
939 		goto exit;
940 
941 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT)) {
942 		res = TEE_ERROR_BAD_STATE;
943 		goto exit;
944 	}
945 
946 	if (!(o->info.handleFlags & TEE_DATA_FLAG_ACCESS_WRITE)) {
947 		res = TEE_ERROR_ACCESS_CONFLICT;
948 		goto exit;
949 	}
950 
951 	res = tee_obj_attr_to_binary(o, NULL, &attr_size);
952 	if (res != TEE_SUCCESS)
953 		goto exit;
954 
955 	if (ADD_OVERFLOW(sizeof(struct tee_svc_storage_head), attr_size,
956 				&off)) {
957 		res = TEE_ERROR_OVERFLOW;
958 		goto exit;
959 	}
960 	if (ADD_OVERFLOW(len, off, &off)) {
961 		res = TEE_ERROR_OVERFLOW;
962 		goto exit;
963 	}
964 	res = o->pobj->fops->truncate(o->fh, off);
965 	switch (res) {
966 	case TEE_SUCCESS:
967 		o->info.dataSize = len;
968 		break;
969 	case TEE_ERROR_CORRUPT_OBJECT:
970 		EMSG("Object corruption");
971 		(void)tee_svc_storage_remove_corrupt_obj(sess, o);
972 		break;
973 	default:
974 		res = TEE_ERROR_GENERIC;
975 		break;
976 	}
977 
978 exit:
979 	return res;
980 }
981 
982 TEE_Result syscall_storage_obj_seek(unsigned long obj, int32_t offset,
983 				    unsigned long whence)
984 {
985 	TEE_Result res;
986 	struct tee_ta_session *sess;
987 	struct tee_obj *o;
988 	tee_fs_off_t new_pos;
989 
990 	res = tee_ta_get_current_session(&sess);
991 	if (res != TEE_SUCCESS)
992 		return res;
993 
994 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
995 			  tee_svc_uref_to_vaddr(obj), &o);
996 	if (res != TEE_SUCCESS)
997 		return res;
998 
999 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT))
1000 		return TEE_ERROR_BAD_STATE;
1001 
1002 	switch (whence) {
1003 	case TEE_DATA_SEEK_SET:
1004 		new_pos = offset;
1005 		break;
1006 	case TEE_DATA_SEEK_CUR:
1007 		if (ADD_OVERFLOW(o->info.dataPosition, offset, &new_pos))
1008 			return TEE_ERROR_OVERFLOW;
1009 		break;
1010 	case TEE_DATA_SEEK_END:
1011 		if (ADD_OVERFLOW(o->info.dataSize, offset, &new_pos))
1012 			return TEE_ERROR_OVERFLOW;
1013 		break;
1014 	default:
1015 		return TEE_ERROR_BAD_PARAMETERS;
1016 	}
1017 
1018 	if (new_pos < 0)
1019 		new_pos = 0;
1020 
1021 	if (new_pos > TEE_DATA_MAX_POSITION) {
1022 		EMSG("Position is beyond TEE_DATA_MAX_POSITION");
1023 		return TEE_ERROR_BAD_PARAMETERS;
1024 	}
1025 
1026 	o->info.dataPosition = new_pos;
1027 
1028 	return TEE_SUCCESS;
1029 }
1030 
1031 void tee_svc_storage_close_all_enum(struct user_ta_ctx *utc)
1032 {
1033 	struct tee_storage_enum_head *eh = &utc->storage_enums;
1034 
1035 	/* disregard return value */
1036 	while (!TAILQ_EMPTY(eh))
1037 		tee_svc_close_enum(utc, TAILQ_FIRST(eh));
1038 }
1039