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 authored and raito committed Feb 12, 2024
1 parent 8d97d84 commit 025a5f6
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/pe/certificate_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,23 @@ 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) % 8 == 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).
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
// 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(8usize));
bytes.gwrite(self.certificate, offset)?;
if let Some(cert_padding) = maybe_certificate_padding {
debug!(
Expand Down

0 comments on commit 025a5f6

Please sign in to comment.