Skip to content

Commit e2b0631

Browse files
committed
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 63aab1b commit e2b0631

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
@@ -177,11 +177,23 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
177177
/// Writes an aligned attribute certificate in the buffer.
178178
fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> {
179179
let offset = &mut 0;
180+
debug_assert!(
181+
(self.length - ATTRIBUTE_CERTIFICATE_HEADER_SIZEOF as u32) % 8 == 0,
182+
"Attribute certificate's length field is unaligned"
183+
);
184+
debug_assert!(
185+
bytes.len() >= self.length as usize,
186+
"Insufficient buffer to write an aligned certificate"
187+
);
180188
bytes.gwrite_with(self.length, offset, ctx)?;
181189
bytes.gwrite_with(self.revision as u16, offset, ctx)?;
182190
bytes.gwrite_with(self.certificate_type as u16, offset, ctx)?;
183-
// Extend by zero the buffer until it is aligned on a quadword (16 bytes).
184-
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
191+
// Extend by zero the buffer until it is aligned on a quadword (16 bytes), according to
192+
// spec:
193+
// > If the bCertificate content does not end on a quadword boundary, the attribute
194+
// > certificate entry is padded with zeros, from the end of bCertificate to the next
195+
// > quadword boundary.
196+
let maybe_certificate_padding = pad(self.certificate.len(), Some(8usize));
185197
bytes.gwrite(self.certificate, offset)?;
186198
if let Some(cert_padding) = maybe_certificate_padding {
187199
debug!(

0 commit comments

Comments
 (0)