Skip to content

Commit bc729aa

Browse files
committed
revocation: cap index table allocation size
The binary index is considered trusted data, but it's sensible to try and cap the allocation for the table data by the file's overall size to avoid corrupted files with incorrect counts causing oversized allocations.
1 parent 0447cb0 commit bc729aa

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

upki/src/revocation/index.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ impl Index {
8585
// Read 2: filename table + log table
8686
let logs_offset = num_filenames * FILENAME_SIZE;
8787
let tables_len = logs_offset + num_logs * LOG_DIR_ENTRY_SIZE;
88+
89+
// A corrupt `num_log_ids` could demand an unreasonable sized allocation. Cap the table
90+
// allocation to the file's overall size.
91+
let file_len = file
92+
.metadata()
93+
.map_err(|error| Error::FileRead {
94+
error,
95+
path: Some(index_path),
96+
})?
97+
.len();
98+
if (HEADER_SIZE + tables_len) as u64 > file_len {
99+
return Err(Error::IndexDecode("index tables truncated".into()));
100+
}
101+
88102
let mut tables = vec![0u8; tables_len];
89103
file.read_exact(&mut tables)
90104
.map_err(|e| Error::IndexDecode(Box::new(e)))?;
@@ -441,6 +455,20 @@ mod tests {
441455
assert!(matches!(err, Error::IndexDecode(_)));
442456
}
443457

458+
// A valid header whose counts imply tables far larger than the file must be
459+
// rejected before the table allocation is made.
460+
#[test]
461+
fn oversized_table_counts() {
462+
let dir = tempfile::tempdir().unwrap();
463+
let config = test_config(dir.path());
464+
let mut data = INDEX_MAGIC.to_vec();
465+
data.push(u8::MAX);
466+
data.extend_from_slice(&u32::MAX.to_be_bytes());
467+
write_file(dir.path(), INDEX_BIN, &data);
468+
let err = Index::from_cache(&config).unwrap_err();
469+
assert!(matches!(err, Error::IndexDecode(_)));
470+
}
471+
444472
#[test]
445473
fn missing_index() {
446474
let dir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)