Skip to content

Commit

Permalink
pe(certificate_table): reinforce checks when writing aligned attribut…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
RaitoBezarius committed Jan 25, 2024
1 parent 63aab1b commit fd3cf62
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/pe/certificate_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,22 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
/// Writes an aligned attribute certificate in the buffer.
fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> {
let offset = &mut 0;
debug_assert!(
(self.length - ATTRIBUTE_CERTIFICATE_HEADER_SIZEOF as u32) % 16 == 0,
"Attribute certificate's length field is unaligned"
);
debug_assert!(
bytes.len() >= self.length as usize,
"Insufficient buffer to write an aligned certificate"
);
bytes.gwrite_with(self.length, offset, ctx)?;
bytes.gwrite_with(self.revision as u16, offset, ctx)?;
bytes.gwrite_with(self.certificate_type as u16, offset, ctx)?;
// Extend by zero the buffer until it is aligned on a quadword (16 bytes).
// Extend by zero the buffer until it is aligned on a quadword (16 bytes), according to
// spec:
// > If the bCertificate content does not end on a quadword boundary, the attribute
// > certificate entry is padded with zeros, from the end of bCertificate to the next
// > quadword boundary.
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
bytes.gwrite(self.certificate, offset)?;
if let Some(cert_padding) = maybe_certificate_padding {
Expand Down

0 comments on commit fd3cf62

Please sign in to comment.