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