diff --git a/src/adapters/asl_store_index_fs/asl_store_index_fs.c b/src/adapters/asl_store_index_fs/asl_store_index_fs.c index 40ba2dd..6115c38 100644 --- a/src/adapters/asl_store_index_fs/asl_store_index_fs.c +++ b/src/adapters/asl_store_index_fs/asl_store_index_fs.c @@ -673,11 +673,12 @@ amduat_asl_store_index_fs_write_replace(const char *temp_dir, static void amduat_asl_store_index_fs_fill_index_state( amduat_asl_index_state_t *out_state, + amduat_asl_snapshot_id_t snapshot_id, uint64_t log_position) { if (out_state == NULL) { return; } - out_state->snapshot_id = 0u; + out_state->snapshot_id = snapshot_id; out_state->log_position = log_position; } @@ -1977,7 +1978,7 @@ static bool amduat_asl_store_index_fs_parse_snapshot_anchor( if (record == NULL || out_snapshot_id == NULL || out_root_hash == NULL) { return false; } - if (record->payload.len < 8u + 32u || record->payload.data == NULL) { + if (record->payload.len != 8u + 32u || record->payload.data == NULL) { return false; } *out_snapshot_id = @@ -2925,22 +2926,6 @@ static amduat_asl_store_error_t amduat_asl_store_index_fs_get_indexed_impl( replay_start = amduat_asl_store_index_fs_find_log_start(records, record_count, anchor_logseq); - } else if (amduat_asl_store_index_fs_find_latest_snapshot_id(fs, - &snapshot_id) && - amduat_asl_store_index_fs_load_snapshot_replay(fs->root_path, - snapshot_id, - NULL, - &replay_state, - &anchor_logseq)) { - if (state.log_position < anchor_logseq || - replay_state.state.log_position != anchor_logseq) { - amduat_asl_replay_free(&replay_state); - amduat_enc_asl_log_free(records, record_count); - return AMDUAT_ASL_STORE_ERR_INTEGRITY; - } - replay_start = amduat_asl_store_index_fs_find_log_start(records, - record_count, - anchor_logseq); } else { if (!amduat_asl_replay_init(&replay_state)) { amduat_enc_asl_log_free(records, record_count); @@ -3039,6 +3024,7 @@ static amduat_asl_store_error_t amduat_asl_store_index_fs_put_indexed_impl( amduat_artifact_free(&existing_artifact); *out_ref = derived_ref; amduat_asl_store_index_fs_fill_index_state(out_state, + current_state.snapshot_id, current_state.log_position); amduat_octets_free(&artifact_bytes); return AMDUAT_ASL_STORE_OK; @@ -3371,7 +3357,9 @@ static amduat_asl_store_error_t amduat_asl_store_index_fs_put_indexed_impl( } *out_ref = derived_ref; - amduat_asl_store_index_fs_fill_index_state(out_state, new_logseq); + amduat_asl_store_index_fs_fill_index_state(out_state, + current_state.snapshot_id, + new_logseq); amduat_asl_store_index_fs_update_ingest_state(fs, artifact_len); amduat_asl_store_index_fs_maybe_snapshot_size(fs); return AMDUAT_ASL_STORE_OK; diff --git a/src/near_core/asl/index_replay.c b/src/near_core/asl/index_replay.c index 46ee4ba..4d2f6c8 100644 --- a/src/near_core/asl/index_replay.c +++ b/src/near_core/asl/index_replay.c @@ -85,7 +85,7 @@ static bool amduat_asl_replay_parse_segment_seal( amduat_asl_replay_cursor_t cur; uint64_t segment_id; - if (payload.len < 8 + 32 || payload.data == NULL || out == NULL) { + if (payload.len != 8u + 32u || payload.data == NULL || out == NULL) { return false; } cur.data = payload.data; @@ -124,7 +124,7 @@ static bool amduat_asl_replay_parse_tombstone( } (void)scope; (void)reason; - return true; + return cur.offset == cur.len; } static bool amduat_asl_replay_parse_tombstone_lift( @@ -146,7 +146,7 @@ static bool amduat_asl_replay_parse_tombstone_lift( return false; } *out_logseq = tombstone_logseq; - return true; + return cur.offset == cur.len; } static bool amduat_asl_replay_parse_snapshot_anchor( @@ -155,7 +155,7 @@ static bool amduat_asl_replay_parse_snapshot_anchor( amduat_asl_replay_cursor_t cur; uint64_t snapshot_id; - if (payload.len < 8u + 32u || payload.data == NULL || + if (payload.len != 8u + 32u || payload.data == NULL || out_snapshot_id == NULL) { return false; } diff --git a/src/near_core/enc/asl_core_index.c b/src/near_core/enc/asl_core_index.c index 3e97f19..ffa04ea 100644 --- a/src/near_core/enc/asl_core_index.c +++ b/src/near_core/enc/asl_core_index.c @@ -1,5 +1,7 @@ #include "amduat/enc/asl_core_index.h" +#include "amduat/hash/asl1.h" + #include #include #include @@ -93,6 +95,7 @@ static bool amduat_asl_core_index_validate_record( size_t start; uint64_t total_len; bool is_tombstone; + const amduat_hash_asl1_desc_t *hash_desc; if (record->reserved0 != 0 || record->reserved1 != 0) { return false; @@ -113,6 +116,17 @@ static bool amduat_asl_core_index_validate_record( if (record->digest_len == 0) { return false; } + if ((record->digest_len % 8u) != 0u) { + return false; + } + if (record->hash_id <= UINT16_MAX) { + hash_desc = amduat_hash_asl1_desc_lookup( + (amduat_hash_id_t)record->hash_id); + if (hash_desc != NULL && hash_desc->digest_len != 0 && + record->digest_len != hash_desc->digest_len) { + return false; + } + } if ((size_t)record->digest_len > digests_len - *digest_cursor) { return false; } @@ -703,6 +717,15 @@ bool amduat_enc_asl_core_index_decode_v1( amduat_enc_asl_core_index_free(out_segment); return false; } + if (record->hash_id <= UINT16_MAX) { + const amduat_hash_asl1_desc_t *hash_desc = + amduat_hash_asl1_desc_lookup((amduat_hash_id_t)record->hash_id); + if (hash_desc != NULL && hash_desc->digest_len != 0 && + record->digest_len != hash_desc->digest_len) { + amduat_enc_asl_core_index_free(out_segment); + return false; + } + } if (record->digest_len == 0) { amduat_enc_asl_core_index_free(out_segment); return false; @@ -711,6 +734,11 @@ bool amduat_enc_asl_core_index_decode_v1( amduat_enc_asl_core_index_free(out_segment); return false; } + if (!legacy_defaults && + !amduat_asl_core_index_is_aligned8(record->digest_offset)) { + amduat_enc_asl_core_index_free(out_segment); + return false; + } if (record->digest_offset < header.digests_offset) { amduat_enc_asl_core_index_free(out_segment); return false;