Skip to content

Commit 6faccbf

Browse files
Fix data err (#402)
* Fix data process error * fix error * fix error * fix error * fix data error Co-authored-by: Checkcheckthis <2452504518@qq.com>
1 parent 8a7afd0 commit 6faccbf

File tree

3 files changed

+116
-73
lines changed

3 files changed

+116
-73
lines changed

src/app/http/ApiHandler.h

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,79 +1040,90 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/,
10401040
// ----- Unseal file ----- //
10411041
crust_status_t crust_status = CRUST_SUCCESS;
10421042
sgx_status_t sgx_status = SGX_SUCCESS;
1043-
if (!is_file_exist(index_path.c_str(), STORE_TYPE_FILE))
1043+
if (index_path == EMPTY_BLOCK_FLAG)
10441044
{
1045-
std::string cid_header = index_path.substr(0, index_path.find_last_of('/'));
1046-
if (cid_header.size() <= UUID_LENGTH * 2)
1047-
{
1048-
ret_info = "Malwared index path:" + index_path;
1049-
ret_code = 404;
1050-
}
1051-
else
1045+
ret_info = "Unseal data successfully!";
1046+
ret_code = 200;
1047+
res.body().clear();
1048+
res.result(ret_code);
1049+
}
1050+
else
1051+
{
1052+
if (!is_file_exist(index_path.c_str(), STORE_TYPE_FILE))
10521053
{
1053-
std::string cid = cid_header.substr(UUID_LENGTH * 2, cid_header.size() - (UUID_LENGTH * 2));
1054-
std::string type;
1055-
bool exist = ed->find_file_type(cid, type);
1056-
if (!exist || (exist && type.compare(FILE_TYPE_PENDING) == 0))
1054+
std::string cid_header = index_path.substr(0, index_path.find_last_of('/'));
1055+
if (cid_header.size() <= UUID_LENGTH * 2)
10571056
{
1058-
ret_info = "Requested cid:'" + cid + "' is not existed.";
1057+
ret_info = "Malwared index path:" + index_path;
10591058
ret_code = 404;
10601059
}
10611060
else
10621061
{
1063-
ret_info = "File block:'" + index_path + "' is lost";
1064-
ret_code = 410;
1062+
std::string cid = cid_header.substr(UUID_LENGTH * 2, cid_header.size() - (UUID_LENGTH * 2));
1063+
std::string type;
1064+
bool exist = ed->find_file_type(cid, type);
1065+
if (!exist || (exist && type.compare(FILE_TYPE_PENDING) == 0))
1066+
{
1067+
ret_info = "Requested cid:'" + cid + "' is not existed.";
1068+
ret_code = 404;
1069+
}
1070+
else
1071+
{
1072+
ret_info = "File block:'" + index_path + "' is lost";
1073+
ret_code = 410;
1074+
}
10651075
}
1066-
}
1067-
p_log->debug("%s\n", ret_info.c_str());
1068-
}
1069-
else
1070-
{
1071-
size_t decrypted_data_sz = get_file_size(index_path.c_str(), STORE_TYPE_FILE);
1072-
uint8_t *p_decrypted_data = (uint8_t *)malloc(decrypted_data_sz);
1073-
size_t decrypted_data_sz_r = 0;
1074-
memset(p_decrypted_data, 0, decrypted_data_sz);
1075-
Defer def_decrypted_data([&p_decrypted_data](void) { free(p_decrypted_data); });
1076-
if (SGX_SUCCESS != (sgx_status = Ecall_unseal_file(global_eid, &crust_status, index_path.c_str(), p_decrypted_data, decrypted_data_sz, &decrypted_data_sz_r)))
1077-
{
1078-
ret_info = "Unseal failed! Invoke SGX API failed! Error code:" + num_to_hexstring(sgx_status);
1079-
p_log->err("%s\n", ret_info.c_str());
1080-
ret_code = 500;
1076+
p_log->debug("%s\n", ret_info.c_str());
10811077
}
10821078
else
10831079
{
1084-
if (CRUST_SUCCESS == crust_status)
1080+
size_t decrypted_data_sz = get_file_size(index_path.c_str(), STORE_TYPE_FILE);
1081+
uint8_t *p_decrypted_data = (uint8_t *)malloc(decrypted_data_sz);
1082+
size_t decrypted_data_sz_r = 0;
1083+
memset(p_decrypted_data, 0, decrypted_data_sz);
1084+
Defer def_decrypted_data([&p_decrypted_data](void) { free(p_decrypted_data); });
1085+
if (SGX_SUCCESS != (sgx_status = Ecall_unseal_file(global_eid, &crust_status, index_path.c_str(), p_decrypted_data, decrypted_data_sz, &decrypted_data_sz_r)))
10851086
{
1086-
ret_info = "Unseal data successfully!";
1087-
ret_code = 200;
1088-
//p_log->info("%s\n", ret_info.c_str());
1089-
res.body().clear();
1090-
res.body().append(reinterpret_cast<char *>(p_decrypted_data), decrypted_data_sz_r);
1091-
res.result(ret_code);
1087+
ret_info = "Unseal failed! Invoke SGX API failed! Error code:" + num_to_hexstring(sgx_status);
1088+
p_log->err("%s\n", ret_info.c_str());
1089+
ret_code = 500;
10921090
}
10931091
else
10941092
{
1095-
switch (crust_status)
1093+
if (CRUST_SUCCESS == crust_status)
10961094
{
1097-
case CRUST_UNSEAL_DATA_FAILED:
1098-
ret_info = "Unseal data failed! SGX unseal data failed!";
1099-
p_log->err("%s\n", ret_info.c_str());
1100-
ret_code = 400;
1101-
break;
1102-
case CRUST_UPGRADE_IS_UPGRADING:
1103-
ret_info = "Unseal file stoped due to upgrading or exiting";
1104-
p_log->info("%s\n", ret_info.c_str());
1105-
ret_code = 503;
1106-
break;
1107-
default:
1108-
ret_info = "Unseal data failed! Error code:" + num_to_hexstring(crust_status);
1109-
p_log->err("%s\n", ret_info.c_str());
1110-
ret_code = 404;
1095+
ret_info = "Unseal data successfully!";
1096+
ret_code = 200;
1097+
//p_log->info("%s\n", ret_info.c_str());
1098+
res.body().clear();
1099+
res.body().append(reinterpret_cast<char *>(p_decrypted_data), decrypted_data_sz_r);
1100+
res.result(ret_code);
1101+
}
1102+
else
1103+
{
1104+
switch (crust_status)
1105+
{
1106+
case CRUST_UNSEAL_DATA_FAILED:
1107+
ret_info = "Unseal data failed! SGX unseal data failed!";
1108+
p_log->err("%s\n", ret_info.c_str());
1109+
ret_code = 400;
1110+
break;
1111+
case CRUST_UPGRADE_IS_UPGRADING:
1112+
ret_info = "Unseal file stoped due to upgrading or exiting";
1113+
p_log->info("%s\n", ret_info.c_str());
1114+
ret_code = 503;
1115+
break;
1116+
default:
1117+
ret_info = "Unseal data failed! Error code:" + num_to_hexstring(crust_status);
1118+
p_log->err("%s\n", ret_info.c_str());
1119+
ret_code = 404;
1120+
}
11111121
}
11121122
}
11131123
}
11141124
}
11151125
}
1126+
11161127
if (200 != ret_code)
11171128
{
11181129
json::JSON ret_body;

src/enclave/include/Parameter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ typedef sgx_status_t (*ocall_get2_f)(crust_status_t *status, uint8_t *u, size_t
1212
// For all
1313
#define SWORKER_VERSION "1.1.0"
1414
#define LEAF_SEPARATOR "+leaf+"
15+
#define EMPTY_BLOCK_CID "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
16+
#define EMPTY_BLOCK_FLAG "empty"
1517

1618
// For enclave metadata
1719
#define ID_METADATA "metadata"

src/enclave/storage/Storage.cpp

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ crust_status_t storage_seal_file(const char *root,
9393
// If file transfer completed
9494
if (p_plain_data == NULL || plain_data_sz == 0)
9595
{
96-
return CRUST_STORAGE_UNEXPECTED_FILE_BLOCK;
96+
SafeLock sl_files_info(wl->pending_files_um_mutex);
97+
sl_files_info.lock();
98+
if (wl->pending_files_um.find(rcid) == wl->pending_files_um.end())
99+
{
100+
return CRUST_STORAGE_NEW_FILE_NOTFOUND;
101+
}
102+
wl->pending_files_um[rcid][FILE_BLOCKS][EMPTY_BLOCK_CID].AddNum(-1);
103+
sl_files_info.unlock();
104+
memcpy(path, EMPTY_BLOCK_FLAG, std::strlen(EMPTY_BLOCK_FLAG));
105+
return CRUST_SUCCESS;
97106
}
98107

99108
crust_status_t seal_ret = CRUST_UNEXPECTED_ERROR;
@@ -576,14 +585,28 @@ crust_status_t check_seal_file_dup(std::string cid)
576585
* @param hashs -> Return hashs, which need to be released when used up
577586
* @return: Status
578587
*/
579-
crust_status_t get_hashs_from_block(const uint8_t *block_data, size_t block_size, std::vector<uint8_t *> &hashs)
588+
crust_status_t get_hashs_from_block(const uint8_t *block_data, size_t bs, std::vector<uint8_t *> &hashs)
580589
{
581-
if (block_data == NULL || block_size == 0)
590+
if (block_data == NULL || bs == 0)
582591
{
583-
return CRUST_STORAGE_EMPTY_BLOCK;
592+
return CRUST_SUCCESS;
584593
}
585594

586-
size_t index = 0;
595+
int block_size = int(bs);
596+
int index = 0;
597+
bool is_err = false;
598+
599+
Defer def_plain_data([&hashs, &is_err](void) {
600+
if (is_err)
601+
{
602+
for (size_t i = 0; i < hashs.size(); i++)
603+
{
604+
free(hashs[i]);
605+
}
606+
hashs.clear();
607+
}
608+
});
609+
587610
while (index < block_size)
588611
{
589612
// Skip link header
@@ -594,33 +617,35 @@ crust_status_t get_hashs_from_block(const uint8_t *block_data, size_t block_size
594617
index++;
595618

596619
// Get all link size
597-
uint32_t link_size = 0;
620+
int link_size = 0;
598621
for (uint8_t shift = 0;;shift += 7)
599622
{
600623
if(shift >= 64)
601624
{
602-
return CRUST_UNEXPECTED_ERROR;
625+
is_err = true;
626+
return CRUST_SUCCESS;
603627
}
604628

605629
if(index >= block_size)
606630
{
607-
return CRUST_UNEXPECTED_ERROR;
631+
is_err = true;
632+
return CRUST_SUCCESS;
608633
}
609634

610635
uint8_t b = block_data[index];
611636
index++;
612-
link_size |= uint32_t(b&0x7F) << shift;
637+
link_size |= int(b&0x7F) << shift;
613638
if(b < 0x80)
614639
{
615640
break;
616641
}
617642
}
618643

619-
if (index + link_size >= block_size)
644+
if (link_size < 0 || index + link_size < 0 || index + link_size >= block_size)
620645
{
646+
is_err = true;
621647
break;
622648
}
623-
624649
size_t index_header = index;
625650

626651
// Skip link hash header
@@ -631,41 +656,46 @@ crust_status_t get_hashs_from_block(const uint8_t *block_data, size_t block_size
631656
index++;
632657

633658
// Get link hash size
634-
uint32_t hash_with_prefix_size = 0;
659+
int hash_with_prefix_size = 0;
635660
for (uint8_t shift = 0;;shift += 7)
636661
{
637662
if (shift >= 64)
638663
{
639-
return CRUST_UNEXPECTED_ERROR;
664+
is_err = true;
665+
return CRUST_SUCCESS;
640666
}
641667

642668
if(index >= block_size)
643669
{
644-
return CRUST_UNEXPECTED_ERROR;
670+
is_err = true;
671+
return CRUST_SUCCESS;
645672
}
646673

647674
uint8_t b = block_data[index];
648675
index++;
649-
hash_with_prefix_size |= uint32_t(b&0x7F) << shift;
676+
hash_with_prefix_size |= int(b&0x7F) << shift;
650677
if(b < 0x80)
651678
{
652679
break;
653680
}
654681
}
655682

683+
if (hash_with_prefix_size < 0 || index + hash_with_prefix_size < HASH_LENGTH + 2 || index + hash_with_prefix_size >= block_size)
684+
{
685+
is_err = true;
686+
break;
687+
}
688+
656689
if (block_data[index + hash_with_prefix_size - HASH_LENGTH - 1] != 0x20 || block_data[index + hash_with_prefix_size - HASH_LENGTH - 2] != 0x12)
657690
{
691+
is_err = true;
658692
break;
659693
}
660-
694+
661695
uint8_t* hash = (uint8_t *)enc_malloc(HASH_LENGTH);
662696
if (hash == NULL)
663697
{
664-
for (size_t i = 0; i < hashs.size(); i++)
665-
{
666-
free(hashs[i]);
667-
}
668-
hashs.clear();
698+
is_err = true;
669699
return CRUST_MALLOC_FAILED;
670700
}
671701

0 commit comments

Comments
 (0)