Tighten ASL index/log conformance checks

This commit is contained in:
Carl Niklas Rydberg 2026-01-18 05:04:55 +01:00
parent 4d2fb250cd
commit 0a118b9841
3 changed files with 39 additions and 23 deletions

View file

@ -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( static void amduat_asl_store_index_fs_fill_index_state(
amduat_asl_index_state_t *out_state, amduat_asl_index_state_t *out_state,
amduat_asl_snapshot_id_t snapshot_id,
uint64_t log_position) { uint64_t log_position) {
if (out_state == NULL) { if (out_state == NULL) {
return; return;
} }
out_state->snapshot_id = 0u; out_state->snapshot_id = snapshot_id;
out_state->log_position = log_position; 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) { if (record == NULL || out_snapshot_id == NULL || out_root_hash == NULL) {
return false; return false;
} }
if (record->payload.len < 8u + 32u || record->payload.data == NULL) { if (record->payload.len != 8u + 32u || record->payload.data == NULL) {
return false; return false;
} }
*out_snapshot_id = *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, replay_start = amduat_asl_store_index_fs_find_log_start(records,
record_count, record_count,
anchor_logseq); 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 { } else {
if (!amduat_asl_replay_init(&replay_state)) { if (!amduat_asl_replay_init(&replay_state)) {
amduat_enc_asl_log_free(records, record_count); 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); amduat_artifact_free(&existing_artifact);
*out_ref = derived_ref; *out_ref = derived_ref;
amduat_asl_store_index_fs_fill_index_state(out_state, amduat_asl_store_index_fs_fill_index_state(out_state,
current_state.snapshot_id,
current_state.log_position); current_state.log_position);
amduat_octets_free(&artifact_bytes); amduat_octets_free(&artifact_bytes);
return AMDUAT_ASL_STORE_OK; 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; *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_update_ingest_state(fs, artifact_len);
amduat_asl_store_index_fs_maybe_snapshot_size(fs); amduat_asl_store_index_fs_maybe_snapshot_size(fs);
return AMDUAT_ASL_STORE_OK; return AMDUAT_ASL_STORE_OK;

View file

@ -85,7 +85,7 @@ static bool amduat_asl_replay_parse_segment_seal(
amduat_asl_replay_cursor_t cur; amduat_asl_replay_cursor_t cur;
uint64_t segment_id; 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; return false;
} }
cur.data = payload.data; cur.data = payload.data;
@ -124,7 +124,7 @@ static bool amduat_asl_replay_parse_tombstone(
} }
(void)scope; (void)scope;
(void)reason; (void)reason;
return true; return cur.offset == cur.len;
} }
static bool amduat_asl_replay_parse_tombstone_lift( static bool amduat_asl_replay_parse_tombstone_lift(
@ -146,7 +146,7 @@ static bool amduat_asl_replay_parse_tombstone_lift(
return false; return false;
} }
*out_logseq = tombstone_logseq; *out_logseq = tombstone_logseq;
return true; return cur.offset == cur.len;
} }
static bool amduat_asl_replay_parse_snapshot_anchor( 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; amduat_asl_replay_cursor_t cur;
uint64_t snapshot_id; uint64_t snapshot_id;
if (payload.len < 8u + 32u || payload.data == NULL || if (payload.len != 8u + 32u || payload.data == NULL ||
out_snapshot_id == NULL) { out_snapshot_id == NULL) {
return false; return false;
} }

View file

@ -1,5 +1,7 @@
#include "amduat/enc/asl_core_index.h" #include "amduat/enc/asl_core_index.h"
#include "amduat/hash/asl1.h"
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -93,6 +95,7 @@ static bool amduat_asl_core_index_validate_record(
size_t start; size_t start;
uint64_t total_len; uint64_t total_len;
bool is_tombstone; bool is_tombstone;
const amduat_hash_asl1_desc_t *hash_desc;
if (record->reserved0 != 0 || record->reserved1 != 0) { if (record->reserved0 != 0 || record->reserved1 != 0) {
return false; return false;
@ -113,6 +116,17 @@ static bool amduat_asl_core_index_validate_record(
if (record->digest_len == 0) { if (record->digest_len == 0) {
return false; 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) { if ((size_t)record->digest_len > digests_len - *digest_cursor) {
return false; return false;
} }
@ -703,6 +717,15 @@ bool amduat_enc_asl_core_index_decode_v1(
amduat_enc_asl_core_index_free(out_segment); amduat_enc_asl_core_index_free(out_segment);
return false; 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) { if (record->digest_len == 0) {
amduat_enc_asl_core_index_free(out_segment); amduat_enc_asl_core_index_free(out_segment);
return false; return false;
@ -711,6 +734,11 @@ bool amduat_enc_asl_core_index_decode_v1(
amduat_enc_asl_core_index_free(out_segment); amduat_enc_asl_core_index_free(out_segment);
return false; 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) { if (record->digest_offset < header.digests_offset) {
amduat_enc_asl_core_index_free(out_segment); amduat_enc_asl_core_index_free(out_segment);
return false; return false;