Skip to content

Commit 8f5d671

Browse files
authored
fix(buffer): bound alignment exponents read from untrusted input (#9155)
## Rationale for this change Found a few places where untrusted alignments from input files are not properly validated and can lead to excessive allocation sizes / OOMs. Alignments in the file format are a power-of-two exponent in a `u8`, and `try_from_exponent` only rejects exponents that would overflow a `usize` shift. Satisfying an alignment can require copying the buffer into an allocation of `len + alignment` bytes, so an exponent of 40 on a 4-byte buffer asks the reader for 1 TiB. This cannot be contained by the caller: Rust aborts via `handle_alloc_error` when an allocation fails rather than unwinding, so it is a process abort rather than an error. Fuzzing the reader found it from two directions, array-tree buffer descriptors and footer segment specs, with exponents from 32 to 61. - Related: #8819 ## What changes are included in this PR? - `Alignment::MAX_UNTRUSTED` (64K) and `Alignment::try_from_untrusted_exponent`, which rejects anything above it. - Use it wherever an exponent comes from a file or message: array-tree buffer descriptors, footer segment specs, postscript segments, and IPC buffer messages. The IPC path used the panicking `from_exponent`. - Regression tests for the array-tree and footer-segment paths, and for the constructor's bounds. ## What APIs are changed? Are there any user-facing changes? `Alignment` gains a public const and constructor. Reading a file that declares a buffer or segment alignment above 65536 now errors where it previously succeeded. 64K is what some ARM chips use as page alignment, but larger alignments aren't useful here. I used Claude Code to investigate this and draw up the initial fix and write the tests. The issue itself was found using a libFuzzer-based harness. --------- Signed-off-by: Lorenz Hübschle <[email protected]>
1 parent 4030e01 commit 8f5d671

5 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎vortex-array/src/serde.rs‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ impl SerializedArray {
633633

634634
// The alignment exponent comes from the flatbuffer and may be corrupt, so validate
635635
// it rather than panicking on a too-large shift (see issue #8819).
636-
let alignment = Alignment::try_from_exponent(fb_buf.alignment_exponent())?;
636+
let alignment =
637+
Alignment::try_from_untrusted_exponent(fb_buf.alignment_exponent())?;
637638
let handle = if let Some(host_data) = buffer_overrides.get(&idx) {
638639
BufferHandle::new_host(host_data.clone()).ensure_aligned(alignment)?
639640
} else {
@@ -765,4 +766,38 @@ mod tests {
765766

766767
Ok(())
767768
}
769+
770+
/// A corrupt array tree can declare a buffer alignment of up to 2^63, which the copy that
771+
/// satisfies it allocates as slack. It must be rejected (see issue #8819).
772+
#[test]
773+
fn from_flatbuffer_and_segment_rejects_excessive_buffer_alignment() -> VortexResult<()> {
774+
// Padding and length fit the segment exactly, so the exponent is the only defect.
775+
// `validate_array_tree` only requires a root node to be present, so an empty one will do.
776+
let mut fbb = FlatBufferBuilder::new();
777+
let fb_root = fba::ArrayNode::create(&mut fbb, &fba::ArrayNodeArgs::default());
778+
let fb_buffers = fbb.create_vector(&[fba::Buffer::new(0, 40, Compression::None, 4)]);
779+
let fb_array = fba::Array::create(
780+
&mut fbb,
781+
&fba::ArrayArgs {
782+
root: Some(fb_root),
783+
buffers: Some(fb_buffers),
784+
},
785+
);
786+
fbb.finish_minimal(fb_array);
787+
let (fb_vec, fb_start) = fbb.collapse();
788+
let fb_end = fb_vec.len();
789+
let array_tree = ByteBuffer::from(fb_vec).slice(fb_start..fb_end);
790+
791+
let segment = BufferHandle::new_host(ByteBuffer::from(vec![0u8; 4]));
792+
let Some(err) = SerializedArray::from_flatbuffer_and_segment(array_tree, segment).err()
793+
else {
794+
vortex_bail!("excessive buffer alignment must be rejected");
795+
};
796+
assert!(
797+
err.to_string().contains("exceeds"),
798+
"unexpected error: {err}"
799+
);
800+
801+
Ok(())
802+
}
768803
}

‎vortex-buffer/src/alignment.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ use vortex_error::vortex_err;
1717
pub struct Alignment(usize);
1818

1919
impl Alignment {
20+
/// Largest alignment accepted from untrusted serialized input.
21+
///
22+
/// This admits 64KiB page alignment, as used on some ARM systems, while bounding the extra
23+
/// allocation required to satisfy an alignment from untrusted input.
24+
pub const MAX_UNTRUSTED: Self = Alignment::new(64 * 1024);
25+
2026
/// Default alignment for device-to-host buffer copies.
2127
pub const HOST_COPY: Self = Alignment::new(256);
2228

@@ -143,6 +149,23 @@ impl Alignment {
143149
}
144150
Ok(Self::new(1 << exponent))
145151
}
152+
153+
/// Create an alignment from an exponent in untrusted serialized input.
154+
///
155+
/// In addition to rejecting exponents that do not fit in `usize`, this rejects alignments
156+
/// large enough to cause an unreasonable allocation when a buffer needs to be copied to
157+
/// satisfy the alignment.
158+
#[inline]
159+
pub fn try_from_untrusted_exponent(exponent: u8) -> VortexResult<Self> {
160+
let alignment = Self::try_from_exponent(exponent)?;
161+
if alignment > Self::MAX_UNTRUSTED {
162+
vortex_bail!(
163+
"Untrusted alignment {alignment} exceeds the {}-byte maximum",
164+
Self::MAX_UNTRUSTED
165+
);
166+
}
167+
Ok(alignment)
168+
}
146169
}
147170

148171
impl Display for Alignment {
@@ -271,6 +294,16 @@ mod test {
271294
assert!(Alignment::try_from_exponent(u8::MAX).is_err());
272295
}
273296

297+
#[test]
298+
fn try_from_untrusted_exponent() {
299+
assert_eq!(
300+
Alignment::try_from_untrusted_exponent(16).unwrap(),
301+
Alignment::new(64 * 1024)
302+
);
303+
assert!(Alignment::try_from_untrusted_exponent(17).is_err());
304+
assert!(Alignment::try_from_untrusted_exponent(u8::MAX).is_err());
305+
}
306+
274307
#[test]
275308
fn into_u32() {
276309
let alignment = Alignment::new(64);

‎vortex-file/src/footer/postscript.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl ReadFlatBuffer for PostscriptSegment {
267267
length: fb.length(),
268268
// The alignment exponent comes from the file and may be corrupt, so validate it rather
269269
// than panicking on a too-large shift (see issue #8819).
270-
alignment: Alignment::try_from_exponent(fb.alignment_exponent())?,
270+
alignment: Alignment::try_from_untrusted_exponent(fb.alignment_exponent())?,
271271
})
272272
}
273273
}

‎vortex-file/src/footer/segment.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl TryFrom<&fb::SegmentSpec> for SegmentSpec {
4545
length: value.length(),
4646
// The alignment exponent comes from the file and may be corrupt, so validate it rather
4747
// than panicking on a too-large shift (see issue #8819).
48-
alignment: Alignment::try_from_exponent(value.alignment_exponent())?,
48+
alignment: Alignment::try_from_untrusted_exponent(value.alignment_exponent())?,
4949
})
5050
}
5151
}
@@ -62,4 +62,13 @@ mod tests {
6262
let err = SegmentSpec::try_from(&fb_spec).unwrap_err();
6363
assert!(err.to_string().contains("too large"), "{err}");
6464
}
65+
66+
#[test]
67+
fn rejects_excessive_alignment_exponent() {
68+
// A representable alignment can still cause an unreasonable allocation when a segment is
69+
// copied to satisfy it.
70+
let fb_spec = fb::SegmentSpec::new(0, 0, 17, 0, 0);
71+
let err = SegmentSpec::try_from(&fb_spec).unwrap_err();
72+
assert!(err.to_string().contains("exceeds"), "{err}");
73+
}
6574
}

‎vortex-ipc/src/messages/decoder.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ impl MessageDecoder {
140140
MessageHeader::BufferMessage => {
141141
let body = bytes.copy_to_aligned(
142142
body_length,
143-
Alignment::from_exponent(
143+
Alignment::try_from_untrusted_exponent(
144144
msg.header_as_buffer_message()
145145
.vortex_expect("header is buffer")
146146
.alignment_exponent(),
147-
),
147+
)?,
148148
);
149149

150150
self.state = Default::default();

0 commit comments

Comments
 (0)