Skip to content

Commit 2465eaa

Browse files
RaitoBezariusraito
authored andcommitted
pe(certificate_table): reinforce checks when writing aligned attribute certificates
It's possible that a user may pass an improperly created attribute certificate and the write will cause all sorts of failure. We sprinkle some `debug_assert!` to avoid this.
1 parent 5a2ce0a commit 2465eaa

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/pe/certificate_table.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,23 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
190190
/// Writes an aligned attribute certificate in the buffer.
191191
fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> {
192192
let offset = &mut 0;
193+
debug_assert!(
194+
(self.length - ATTRIBUTE_CERTIFICATE_HEADER_SIZEOF as u32) % 8 == 0,
195+
"Attribute certificate's length field is unaligned"
196+
);
197+
debug_assert!(
198+
bytes.len() >= self.length as usize,
199+
"Insufficient buffer to write an aligned certificate"
200+
);
193201
bytes.gwrite_with(self.length, offset, ctx)?;
194202
bytes.gwrite_with(self.revision as u16, offset, ctx)?;
195203
bytes.gwrite_with(self.certificate_type as u16, offset, ctx)?;
196-
// Extend by zero the buffer until it is aligned on a quadword (16 bytes).
197-
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
204+
// Extend by zero the buffer until it is aligned on a quadword (16 bytes), according to
205+
// spec:
206+
// > If the bCertificate content does not end on a quadword boundary, the attribute
207+
// > certificate entry is padded with zeros, from the end of bCertificate to the next
208+
// > quadword boundary.
209+
let maybe_certificate_padding = pad(self.certificate.len(), Some(8usize));
198210
bytes.gwrite(self.certificate, offset)?;
199211
if let Some(cert_padding) = maybe_certificate_padding {
200212
debug!(

0 commit comments

Comments
 (0)