• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include "ssa_enumerator.h"
13 #include "string.h"
14 #include "securec.h"
15 #include "tee_defines.h"
16 #include "tee_mem_mgmt_api.h"
17 #include "tee_log.h"
18 #include "sfs_internal.h"
19 #include "sfs.h"
20 #include "ssa_helper.h"
21 #include "ssa_fs.h"
22 
23 #define MIN_TWO_DIGIT 10
hex2asc(char n)24 static char hex2asc(char n)
25 {
26     if (n >= MIN_TWO_DIGIT)
27         return 'a' + (n - MIN_TWO_DIGIT);
28     else
29         return '0' + n;
30 }
31 
32 #define BYTE_LEN              8U
33 #define HALF_BYTE_OFFSET      4U
34 #define EVEN_NUM_CHECKER      2
35 #define LOW_BIT_MASK          0x0f
36 #define TIME_LOW_OFFSET_MAX   24U
37 #define TIME_MID_OFFSET_MAX   8U
38 #define TIME_HI_OFFSET_MAX    8U
39 #define POS_ARRAY_LEN         5
40 #define HALF_OF(x)            ((x) / 2)
convert_uuid_to_str(const TEE_UUID * uuid,char * buff,int buffsize)41 static TEE_Result convert_uuid_to_str(const TEE_UUID *uuid, char *buff, int buffsize)
42 {
43     const uint8_t add_pos_array[POS_ARRAY_LEN] = { 8, 12, 16, 20, 64 }; /* add_pos_array[4]=64 is not used */
44     uint32_t i;
45     uint8_t add_pos = 0;
46     unsigned char uuid_buff[sizeof(TEE_UUID)];
47 
48     if (uuid == NULL || buff == NULL)
49         return TEE_ERROR_GENERIC;
50 
51     if (buffsize < UUID_STR_LEN) {
52         tloge("buffer size is not big enough for uuid string.\n");
53         return TEE_ERROR_GENERIC;
54     }
55 
56     char *buff_pos  = buff;
57     uint32_t offset = 0;
58     for (i = 0; i < sizeof(uint32_t); i++)
59         uuid_buff[i] = uuid->timeLow >> (TIME_LOW_OFFSET_MAX - i * BYTE_LEN);
60 
61     offset += sizeof(uint32_t);
62     for (i = 0; i < sizeof(uint16_t); i++)
63         uuid_buff[offset + i] = uuid->timeMid >> (TIME_MID_OFFSET_MAX - i * BYTE_LEN);
64 
65     offset += sizeof(uint16_t);
66     for (i = 0; i < sizeof(uint16_t); i++)
67         uuid_buff[offset + i] = uuid->timeHiAndVersion >> (TIME_HI_OFFSET_MAX - i * BYTE_LEN);
68 
69     offset += sizeof(uint16_t);
70     for (i = 0; i < sizeof(uuid->clockSeqAndNode); i++)
71         uuid_buff[offset + i] = uuid->clockSeqAndNode[i];
72 
73     for (i = 0; i < sizeof(TEE_UUID); i++) {
74         if (i == add_pos_array[add_pos]) {
75             *buff_pos = '-';
76             buff_pos++;
77             add_pos++;
78         }
79 
80         if (i % EVEN_NUM_CHECKER != 0)
81             *buff_pos = hex2asc((*(uuid_buff + HALF_OF(i))) & LOW_BIT_MASK);
82         else
83             *buff_pos = hex2asc((*(uuid_buff + HALF_OF(i))) >> HALF_BYTE_OFFSET);
84 
85         buff_pos++;
86     }
87 
88     return TEE_SUCCESS;
89 }
90 
gen_enum_file_name(const char * partition,const TEE_UUID * uuid,char * name_buf,uint32_t name_buf_len)91 static TEE_Result gen_enum_file_name(const char *partition, const TEE_UUID *uuid,
92                                      char *name_buf, uint32_t name_buf_len)
93 {
94     errno_t ret_s;
95     TEE_Result ret;
96     uint32_t offset = 0;
97 
98     if (strstr(partition, SFS_PARTITION_TRANSIENT) == partition) {
99         ret_s = memcpy_s(name_buf, name_buf_len, SFS_PARTITION_TRANSIENT, strlen(SFS_PARTITION_TRANSIENT));
100         if (ret_s != EOK) {
101             tloge("memcpy ENUM_FILE_NAME_PREFIX into name buffer failed.\n");
102             return TEE_ERROR_SECURITY;
103         }
104         offset += strlen(SFS_PARTITION_TRANSIENT);
105     }
106 
107     ret_s = memcpy_s(name_buf + offset, name_buf_len - offset, ENUM_FILE_NAME_PREFIX, strlen(ENUM_FILE_NAME_PREFIX));
108     if (ret_s != EOK) {
109         tloge("memcpy ENUM_FILE_NAME_PREFIX into name buffer failed.\n");
110         return TEE_ERROR_SECURITY;
111     }
112 
113     offset += strlen(ENUM_FILE_NAME_PREFIX);
114 
115     ret = convert_uuid_to_str(uuid, name_buf + offset, name_buf_len - offset);
116     if (ret != TEE_SUCCESS) {
117         tloge("failed to convert uuid to str.\n");
118         return ret;
119     }
120 
121     name_buf[name_buf_len - 1] = '\0';
122 
123     return TEE_SUCCESS;
124 }
125 
internal_open_enum_file(const char * partition,uint32_t sndr,struct sfd_t ** sfd)126 static TEE_Result internal_open_enum_file(const char *partition, uint32_t sndr, struct sfd_t **sfd)
127 {
128     TEE_Result ret;
129     char enum_file_name[ENUM_FILE_NAME_LEN] = { 0 };
130 
131     TEE_UUID *uuid = get_sender_uuid(sndr);
132     if (uuid == NULL) {
133         tloge("get uuid fail");
134         return TEE_ERROR_GENERIC;
135     }
136 
137     ret = gen_enum_file_name(partition, uuid, enum_file_name, sizeof(enum_file_name));
138     if (ret != TEE_SUCCESS) {
139         tloge("failed to generate the enum file name.\n");
140         return ret;
141     }
142 
143     ret = ssa_internal_fopen(enum_file_name, uuid, sfd);
144     if (ret != TEE_SUCCESS) {
145         tloge("open enum file failed ret=0x%x\n", ret);
146         return ret;
147     }
148 
149     if ((*sfd)->size % sizeof(struct object_enum_info) != 0) {
150         tloge("file size error, file size:%u", (*sfd)->size);
151         ssa_internal_fclose(*sfd);
152         return TEE_ERROR_GENERIC;
153     }
154 
155     return TEE_SUCCESS;
156 }
157 
get_enum_file_size(const char * partition,uint32_t sndr,uint32_t * file_size)158 static TEE_Result get_enum_file_size(const char *partition, uint32_t sndr, uint32_t *file_size)
159 {
160     struct sfd_t *sfd = NULL;
161     TEE_Result ret;
162 
163     ret = internal_open_enum_file(partition, sndr, &sfd);
164     if (ret == TEE_SUCCESS) {
165         *file_size = sfd->size;
166         ssa_internal_fclose(sfd);
167         return TEE_SUCCESS;
168     } else if (ret == TEE_ERROR_ITEM_NOT_FOUND) {
169         *file_size = 0;
170         return TEE_SUCCESS;
171     } else {
172         return ret;
173     }
174 }
175 
ssa_get_enum_file_size(union ssa_agent_msg * msg,uint32_t sndr,struct ssa_agent_rsp * rsp)176 void ssa_get_enum_file_size(union ssa_agent_msg *msg, uint32_t sndr, struct ssa_agent_rsp *rsp)
177 {
178     TEE_Result ret;
179     uint32_t transient_file_size = 0;
180     uint32_t persist_file_size   = 0;
181 
182     if (rsp == NULL) {
183         tloge("get enum file size invalid rsp\n");
184         return;
185     }
186 
187     if (msg == NULL) {
188         tloge("get enum file size invalid msg\n");
189         rsp->ret = TEE_ERROR_BAD_PARAMETERS;
190         return;
191     }
192 
193     ret = get_enum_file_size(SFS_PARTITION_TRANSIENT, sndr, &transient_file_size);
194     if (ret != TEE_SUCCESS) {
195         tloge("get transient enum_file size fail, ret=%x ", ret);
196         rsp->ret = ret;
197         return;
198     }
199 
200     ret = get_enum_file_size(SFS_PARTITION_PERSISTENT, sndr, &persist_file_size);
201     if (ret != TEE_SUCCESS) {
202         rsp->ret = ret;
203         tloge("get persiset enum_file size fail, ret=%x ", ret);
204         return;
205     }
206 
207     rsp->get_info_obj.len = transient_file_size + persist_file_size;
208     rsp->ret              = ret;
209 }
210 
start_enumerator(const char * partition,uint32_t sndr,uint8_t * buff,uint32_t buff_len,TEE_Result * ret)211 static uint32_t start_enumerator(const char *partition, uint32_t sndr, uint8_t *buff, uint32_t buff_len,
212                                  TEE_Result *ret)
213 {
214     struct sfd_t *sfd = NULL;
215     uint32_t count;
216 
217     *ret = internal_open_enum_file(partition, sndr, &sfd);
218     if (*ret == TEE_ERROR_ITEM_NOT_FOUND) {
219         *ret = TEE_SUCCESS;
220         return 0;
221     } else if (*ret != TEE_SUCCESS) {
222         return 0;
223     }
224 
225     count = ssa_read(buff, buff_len, sfd, ret);
226     if (*ret != TEE_SUCCESS)
227         tloge("read enumerator file fail %x\n", *ret);
228 
229     ssa_internal_fclose(sfd);
230     return count;
231 }
232 
ssa_start_enumerator(union ssa_agent_msg * msg,uint32_t sndr,struct ssa_agent_rsp * rsp)233 void ssa_start_enumerator(union ssa_agent_msg *msg, uint32_t sndr, struct ssa_agent_rsp *rsp)
234 {
235     TEE_Result ret;
236     mem_map_info_t read_buf_info;
237     uint32_t count;
238 
239     if (rsp == NULL) {
240         tloge("start enumerator invalid rsp\n");
241         return;
242     }
243 
244     if (msg == NULL || msg->read_obj.buffer == 0) {
245         tloge("start enumerator invalid msg\n");
246         rsp->ret = TEE_ERROR_BAD_PARAMETERS;
247         return;
248     }
249 
250     read_buf_info.vm_addr = 0;
251     read_buf_info.size    = msg->read_obj.len;
252     read_buf_info.mapped  = false;
253     if (ssa_map_from_task(sndr, msg->read_obj.buffer, msg->read_obj.len, g_ssagent_handle,
254                           &read_buf_info.vm_addr)) {
255         tloge("map buffer from %u fail\n", sndr);
256         rsp->ret = TEE_ERROR_GENERIC;
257         return;
258     }
259     read_buf_info.mapped = true;
260 
261     count = start_enumerator(SFS_PARTITION_TRANSIENT, sndr, (uint8_t *)read_buf_info.vm_addr, msg->read_obj.len, &ret);
262     if (ret != TEE_SUCCESS)
263         goto clean;
264 
265     if (msg->read_obj.len < count) {
266         tloge("read obj len is error, msg->readObj.len:%u, count:%u", msg->read_obj.len, count);
267         ret = TEE_ERROR_GENERIC;
268         goto clean;
269     }
270 
271     uint32_t count2 = start_enumerator(SFS_PARTITION_PERSISTENT, sndr, (uint8_t *)(read_buf_info.vm_addr + count),
272         msg->read_obj.len - count, &ret);
273     if (ret != TEE_SUCCESS)
274         goto clean;
275 
276     if (msg->read_obj.len - count < count2) {
277         tloge("read obj len is error, expect len:%u, count:%u", msg->read_obj.len - count, count2);
278         ret = TEE_ERROR_GENERIC;
279     }
280 
281 clean:
282     ssa_unmap_from_task(g_ssagent_handle, read_buf_info.vm_addr, read_buf_info.size, read_buf_info.mapped);
283     rsp->ret = ret;
284 }
285 
find_obj(const uint8_t * file_buf,uint32_t file_buf_len,const uint8_t * object_id,uint32_t object_id_len)286 static struct object_enum_info *find_obj(const uint8_t *file_buf, uint32_t file_buf_len, const uint8_t *object_id,
287     uint32_t object_id_len)
288 {
289     struct object_enum_info *obj_info     = NULL;
290     struct object_enum_info *obj_info_end = NULL;
291     int32_t ret;
292 
293     obj_info     = (struct object_enum_info *)file_buf;
294     obj_info_end = (struct object_enum_info *)(file_buf + file_buf_len);
295 
296     while (obj_info < obj_info_end) {
297         if (obj_info->object_id_len != object_id_len) {
298             obj_info++;
299             continue;
300         }
301 
302         ret = TEE_MemCompare(obj_info->object_id, object_id, object_id_len);
303         if (ret == 0)
304             return obj_info;
305 
306         obj_info++;
307     }
308 
309     return NULL;
310 }
311 
internal_create_enum_file(const char * partition,uint32_t sndr,struct sfd_t ** sfd,uint32_t * oper_flag,uint32_t flags)312 static TEE_Result internal_create_enum_file(const char *partition, uint32_t sndr, struct sfd_t **sfd,
313                                             uint32_t *oper_flag, uint32_t flags)
314 {
315     meta_data_t *meta = NULL;
316     TEE_Result ret;
317     char enum_file_name[ENUM_FILE_NAME_LEN] = { 0 };
318 
319     tlogd("internal_create_enum_file enter");
320 
321     TEE_UUID *uuid = get_sender_uuid(sndr);
322     if (uuid == NULL) {
323         tloge("get uuid fail");
324         return TEE_ERROR_GENERIC;
325     }
326 
327     ret = gen_enum_file_name(partition, uuid, enum_file_name, sizeof(enum_file_name));
328     if (ret != TEE_SUCCESS) {
329         tloge("failed to generate the enum file name.\n");
330         return ret;
331     }
332 
333     meta = create_meta_data((uint8_t *)enum_file_name, strlen(enum_file_name), TEE_OBJECT_STORAGE_PRIVATE,
334                             flags, uuid, &ret, SFS_ARCH_VERSION_SSA);
335     if (meta == NULL) {
336         tloge("create_sec_enum_file:meta create fail\n");
337         return ret;
338     }
339 
340     if (meta->encrypted_file_id == NULL)
341         goto clean;
342 
343     if (ssa_fs_faccess((char *)meta->encrypted_file_id, F_OK, meta->storage_id) == 0) {
344         /* secure enumerator file has already been created. */
345         tlogd("enumerator file already exists, filename is %s\n", meta->encrypted_file_id);
346         *sfd = ssa_open(meta, TEE_DATA_FLAG_ACCESS_READ | TEE_DATA_FLAG_ACCESS_WRITE, &ret);
347         if (*sfd != NULL) {
348             *oper_flag = ENUM_OPEN;
349             return TEE_SUCCESS;
350         }
351 
352         if (ret != TEE_ERROR_ITEM_NOT_FOUND)
353             goto clean;
354     }
355 
356     /* file not exit, or open file return TEE_ERROR_ITEM_NOT_FOUND, create file */
357     *sfd = ssa_create(meta, TEE_DATA_FLAG_ACCESS_WRITE, &ret);
358     if (*sfd == NULL) {
359         tloge("create fail ret=0x%x\n", ret);
360         goto clean;
361     }
362 
363     *oper_flag = ENUM_CREATE;
364     return TEE_SUCCESS;
365 
366 clean:
367     free_meta_data(&meta);
368     return ret;
369 }
370 
get_obj_enum_info(const struct create_obj_msg_t * create_obj,uint32_t data_size,struct object_enum_info * obj_enum_info)371 static TEE_Result get_obj_enum_info(const struct create_obj_msg_t *create_obj, uint32_t data_size,
372                                     struct object_enum_info *obj_enum_info)
373 {
374     struct saved_attr_info_t *attrinfo = NULL;
375     errno_t ret_s;
376 
377     attrinfo = (struct saved_attr_info_t *)(uintptr_t)create_obj->attributes;
378 
379     obj_enum_info->obj_info.objectType = attrinfo->object_info.objectType;
380 #ifndef GP_SUPPORT
381     obj_enum_info->obj_info.objectSize    = attrinfo->object_info.objectSize;
382     obj_enum_info->obj_info.maxObjectSize = attrinfo->object_info.maxObjectSize;
383 #else
384     obj_enum_info->obj_info.keySize    = attrinfo->object_info.keySize;
385     obj_enum_info->obj_info.maxKeySize = attrinfo->object_info.maxKeySize;
386 #endif
387     obj_enum_info->obj_info.objectUsage  = attrinfo->object_info.objectUsage;
388     obj_enum_info->obj_info.dataSize     = data_size;
389     obj_enum_info->obj_info.dataPosition = attrinfo->object_info.dataPosition;
390     obj_enum_info->obj_info.handleFlags  = attrinfo->object_info.handleFlags;
391 
392     ret_s = memcpy_s(obj_enum_info->object_id, HASH_NAME_BUFF_LEN, (uint8_t *)(uintptr_t)create_obj->object_id,
393                      create_obj->obj_id_len);
394     if (ret_s != EOK) {
395         tloge("copy objId failed\n");
396         return TEE_ERROR_SECURITY;
397     }
398 
399     obj_enum_info->object_id_len = create_obj->obj_id_len;
400     obj_enum_info->storage_id    = create_obj->storage_id;
401 
402     return TEE_SUCCESS;
403 }
404 
update_obj_info(const struct create_obj_msg_t * create_obj,uint32_t data_size,uint8_t * file_buf,uint32_t old_file_len,uint32_t * new_file_len)405 static TEE_Result update_obj_info(const struct create_obj_msg_t *create_obj, uint32_t data_size, uint8_t *file_buf,
406                                   uint32_t old_file_len, uint32_t *new_file_len)
407 {
408     struct object_enum_info *origin_obj_enum_info = NULL;
409     errno_t ret_s;
410     int32_t res;
411     struct object_enum_info obj_enum_info = { 0 };
412 
413     /* eunmarator file content */
414     TEE_Result ret = get_obj_enum_info(create_obj, data_size, &obj_enum_info);
415     if (ret != TEE_SUCCESS) {
416         tloge("Failed to get the memory for obj_enum_info.\n");
417         return TEE_ERROR_GENERIC;
418     }
419 
420     origin_obj_enum_info = find_obj(file_buf, old_file_len, obj_enum_info.object_id, obj_enum_info.object_id_len);
421     if (origin_obj_enum_info == NULL) {
422         ret_s = memcpy_s(file_buf + old_file_len, *new_file_len - old_file_len, &obj_enum_info,
423             sizeof(struct object_enum_info));
424         if (ret_s != EOK) {
425             tloge("copy enum info failed\n");
426             return TEE_ERROR_SECURITY;
427         }
428     } else {
429         *new_file_len = old_file_len;
430         res           = TEE_MemCompare(origin_obj_enum_info, &obj_enum_info, sizeof(struct object_enum_info));
431         if (res == 0) {
432             tlogd("The info of obj_enum_info is not changed\n");
433             return TEE_SUCCESS;
434         }
435 
436         if ((uint64_t)(uintptr_t)origin_obj_enum_info - (uint64_t)(uintptr_t)file_buf > 0xFFFFFFFF)
437             return TEE_ERROR_GENERIC;
438 
439         uint32_t offset = (uint64_t)(uintptr_t)origin_obj_enum_info - (uint64_t)(uintptr_t)file_buf;
440         ret_s = memcpy_s(origin_obj_enum_info, old_file_len - offset, &obj_enum_info, sizeof(struct object_enum_info));
441         if (ret_s != EOK) {
442             tloge("Failed to copy obj_enum_info\n");
443             return TEE_ERROR_SECURITY;
444         }
445     }
446 
447     return TEE_SUCCESS;
448 }
449 
add_objinfo_into_enum_file(const struct create_obj_msg_t * create_obj,uint32_t data_size,uint32_t sndr)450 TEE_Result add_objinfo_into_enum_file(const struct create_obj_msg_t *create_obj, uint32_t data_size, uint32_t sndr)
451 {
452     TEE_Result ret;
453     struct sfd_t *sfd = NULL;
454     uint8_t *file_buf = NULL;
455     uint32_t count;
456     uint32_t oper_flag = ENUM_INVALID_OPER;
457 
458     if (create_obj == NULL)
459         return TEE_ERROR_BAD_PARAMETERS;
460 
461     ret = internal_create_enum_file((const char *)(uintptr_t)create_obj->object_id, sndr,
462                                     &sfd, &oper_flag, create_obj->flags);
463     if (ret != TEE_SUCCESS)
464         return ret;
465 
466     if (sfd->size % sizeof(struct object_enum_info) != 0) {
467         tloge("file size error, file size:%u", sfd->size);
468         ret = TEE_ERROR_GENERIC;
469         goto close_or_delete;
470     }
471 
472     uint32_t old_file_len = sfd->size;
473     uint32_t new_file_len = sfd->size + sizeof(struct object_enum_info);
474     file_buf              = TEE_Malloc(new_file_len, 0);
475     if (file_buf == NULL) {
476         tloge("malloc memory failed size=0x%x\n", new_file_len);
477         ret = TEE_ERROR_OUT_OF_MEMORY;
478         goto close_or_delete;
479     }
480 
481     count = ssa_read(file_buf, old_file_len, sfd, &ret);
482     if (count != old_file_len) {
483         tloge("read enum file failed\n");
484         ret = TEE_ERROR_GENERIC;
485         goto clean;
486     }
487 
488     ret = update_obj_info(create_obj, data_size, file_buf, old_file_len, &new_file_len);
489     if (ret != TEE_SUCCESS)
490         goto clean;
491 
492     count = ssa_internal_fwrite(sfd, file_buf, new_file_len);
493     if (count != new_file_len) {
494         tloge("write enum file failed, count=%u\n", count);
495         ret = TEE_ERROR_GENERIC;
496         goto clean;
497     }
498 
499     oper_flag = ENUM_INVALID_OPER;
500 
501 clean:
502     TEE_Free(file_buf);
503 close_or_delete:
504     if (oper_flag == ENUM_CREATE)
505         ssa_internal_fremove(sfd);
506     else
507         ssa_internal_fclose(sfd);
508     return ret;
509 }
510 
update_objinfo_in_enum_file(const uint8_t * object_id,uint32_t object_id_len,uint32_t new_size,uint32_t new_pos,uint32_t sndr)511 TEE_Result update_objinfo_in_enum_file(const uint8_t *object_id, uint32_t object_id_len,
512                                        uint32_t new_size, uint32_t new_pos, uint32_t sndr)
513 {
514     struct object_enum_info *obj_enum_info = NULL;
515     struct sfd_t *sfd               = NULL;
516     TEE_Result ret;
517     uint8_t *file_buf = NULL;
518     uint32_t file_len;
519     uint32_t count;
520 
521     if (object_id == NULL)
522         return TEE_ERROR_BAD_PARAMETERS;
523 
524     ret = internal_open_enum_file((char *)object_id, sndr, &sfd);
525     if (ret != TEE_SUCCESS)
526         return ret;
527 
528     file_len = sfd->size;
529     file_buf = TEE_Malloc(file_len, 0);
530     if (file_buf == NULL) {
531         tloge("malloc memory failed size=0x%x\n", file_len);
532         ssa_internal_fclose(sfd);
533         return TEE_ERROR_OUT_OF_MEMORY;
534     }
535 
536     count = ssa_read(file_buf, file_len, sfd, &ret);
537     if (count != sfd->size) {
538         tloge("read enum file failed\n");
539         ret = TEE_ERROR_GENERIC;
540         goto clean;
541     }
542 
543     obj_enum_info = find_obj(file_buf, file_len, object_id, object_id_len);
544     if (obj_enum_info == NULL) {
545         tloge("Failed to find the obj info about %s\n", object_id);
546         ret = TEE_ERROR_GENERIC;
547         goto clean;
548     }
549 
550     obj_enum_info->obj_info.dataSize     = new_size;
551     obj_enum_info->obj_info.dataPosition = new_pos;
552 
553     count = ssa_internal_fwrite(sfd, file_buf, file_len);
554     if (count != file_len) {
555         tloge("write enum file failed, count=%u\n", count);
556         ret = TEE_ERROR_GENERIC;
557     }
558 
559 clean:
560     ssa_internal_fclose(sfd);
561     TEE_Free(file_buf);
562     return ret;
563 }
564 
rename_obj_in_enum_file(const uint8_t * origin_obj_id,const uint8_t * new_obj_id,uint32_t new_obj_id_len,uint32_t sndr)565 TEE_Result rename_obj_in_enum_file(const uint8_t *origin_obj_id, const uint8_t *new_obj_id, uint32_t new_obj_id_len,
566                                    uint32_t sndr)
567 {
568     struct object_enum_info *obj_enum_info = NULL;
569     TEE_Result ret;
570     struct sfd_t *sfd = NULL;
571     uint32_t count;
572 
573     if (origin_obj_id == NULL || new_obj_id == NULL)
574         return TEE_ERROR_BAD_PARAMETERS;
575 
576     ret = internal_open_enum_file((char *)origin_obj_id, sndr, &sfd);
577     if (ret != TEE_SUCCESS)
578         return ret;
579 
580     uint32_t file_len = sfd->size;
581     uint8_t *file_buf = TEE_Malloc(file_len, 0);
582     if (file_buf == NULL) {
583         ssa_internal_fclose(sfd);
584         return TEE_ERROR_OUT_OF_MEMORY;
585     }
586 
587     count = ssa_read(file_buf, file_len, sfd, &ret);
588     if (count != sfd->size) {
589         tloge("read enum file failed\n");
590         ret = TEE_ERROR_GENERIC;
591         goto clean;
592     }
593 
594     uint32_t origin_id_len = strlen((const char *)origin_obj_id);
595     obj_enum_info          = find_obj(file_buf, file_len, origin_obj_id, origin_id_len);
596     if (obj_enum_info == NULL) {
597         ret = TEE_ERROR_GENERIC;
598         goto clean;
599     }
600 
601     if (memcpy_s(obj_enum_info->object_id, HASH_NAME_BUFF_LEN,
602         new_obj_id, new_obj_id_len) != EOK) {
603         ret = TEE_ERROR_SECURITY;
604         goto clean;
605     }
606 
607     obj_enum_info->object_id_len = new_obj_id_len;
608 
609     count = ssa_internal_fwrite(sfd, file_buf, file_len);
610     if (count != sfd->size) {
611         tloge("write enum file failed, count=%u\n", count);
612         ret = TEE_ERROR_GENERIC;
613     }
614 
615 clean:
616     TEE_Free(file_buf);
617     ssa_internal_fclose(sfd);
618     return ret;
619 }
620 
delete_enum_obj(const uint8_t * file_buf,uint32_t file_buf_len,const uint8_t * object_id,uint32_t object_id_len)621 static TEE_Result delete_enum_obj(const uint8_t *file_buf, uint32_t file_buf_len, const uint8_t *object_id,
622                                   uint32_t object_id_len)
623 {
624     errno_t ret_s;
625     struct object_enum_info *obj_enum_info = NULL;
626     struct object_enum_info *obj_info_last = NULL;
627 
628     obj_enum_info = find_obj(file_buf, file_buf_len, object_id, object_id_len);
629     if (obj_enum_info == NULL) {
630         tloge("Failed to find the obj info about %s\n", object_id);
631         return TEE_ERROR_GENERIC;
632     }
633 
634     obj_info_last = (struct object_enum_info *)(file_buf + file_buf_len - sizeof(struct object_enum_info));
635     if (obj_enum_info != obj_info_last) {
636         ret_s = memmove_s(obj_enum_info, sizeof(struct object_enum_info), obj_info_last,
637             sizeof(struct object_enum_info));
638         if (ret_s != EOK) {
639             tloge("Failed to delete objinfo from file buf.\n");
640             return TEE_ERROR_SECURITY;
641         }
642     }
643 
644     return TEE_SUCCESS;
645 }
646 
delete_obj_in_enum_file(const uint8_t * object_id,uint32_t object_id_len,uint32_t sndr)647 TEE_Result delete_obj_in_enum_file(const uint8_t *object_id, uint32_t object_id_len, uint32_t sndr)
648 {
649     TEE_Result ret;
650     struct sfd_t *sfd = NULL;
651     bool del_flag     = false;
652 
653     if (object_id == NULL)
654         return TEE_ERROR_BAD_PARAMETERS;
655 
656     ret = internal_open_enum_file((char *)object_id, sndr, &sfd);
657     if (ret != TEE_SUCCESS)
658         return ret;
659 
660     uint32_t origin_file_len = sfd->size;
661     uint8_t *file_buf        = TEE_Malloc(origin_file_len, 0);
662     if (file_buf == NULL) {
663         tloge("malloc memory failed size=0x%x\n", origin_file_len);
664         ssa_internal_fclose(sfd);
665         return TEE_ERROR_OUT_OF_MEMORY;
666     }
667 
668     uint32_t count = ssa_read(file_buf, origin_file_len, sfd, &ret);
669     if (count != origin_file_len || ret != TEE_SUCCESS) {
670         tloge("read enum file failed\n");
671         ret = TEE_ERROR_GENERIC;
672         goto clean;
673     }
674 
675     ret = delete_enum_obj(file_buf, origin_file_len, object_id, object_id_len);
676     if (ret != TEE_SUCCESS) {
677         tloge("delete enum obj fail!");
678         goto clean;
679     }
680 
681     uint32_t new_file_len = origin_file_len - sizeof(struct object_enum_info);
682     if (new_file_len == 0) {
683         del_flag = true;
684         goto clean;
685     }
686 
687     count = ssa_internal_fwrite(sfd, file_buf, new_file_len);
688     if (count != new_file_len) {
689         tloge("write enum file failed, count=%u\n", count);
690         ret = TEE_ERROR_GENERIC;
691     }
692 
693 clean:
694     if (del_flag)
695         ssa_internal_fremove(sfd);
696     else
697         ssa_internal_fclose(sfd);
698     sfd = NULL;
699     TEE_Free(file_buf);
700     return ret;
701 }
702