4
0
Эх сурвалжийг харах

Be careful when sending too much OPT data

This is another integer conversion fix. Even though no data is sent in OPT packets in requests right now, there was a conversion from usize to 16 as it wrote the size of the data. The best thing to do would be to change the design so that request!OPT and response!OPT don't share the same type (with the request kind not even having the data field to send), but at least with this, the intent is made clear.
Benjamin Sago 4 жил өмнө
parent
commit
29acfc601e

+ 7 - 1
dns/src/record/opt.rs

@@ -1,3 +1,4 @@
+use std::convert::TryFrom;
 use std::io;
 use std::io;
 
 
 use log::*;
 use log::*;
@@ -97,7 +98,12 @@ impl OPT {
         bytes.write_u8(self.higher_bits)?;
         bytes.write_u8(self.higher_bits)?;
         bytes.write_u8(self.edns0_version)?;
         bytes.write_u8(self.edns0_version)?;
         bytes.write_u16::<BigEndian>(self.flags)?;
         bytes.write_u16::<BigEndian>(self.flags)?;
-        bytes.write_u16::<BigEndian>(self.data.len() as u16)?;
+
+        // We should not be sending any data at all in the request, really,
+        // so sending too much data is downright nonsensical
+        let data_len = u16::try_from(self.data.len()).expect("Sending too much data");
+        bytes.write_u16::<BigEndian>(data_len)?;
+
         for b in &self.data {
         for b in &self.data {
             bytes.write_u8(*b)?;
             bytes.write_u8(*b)?;
         }
         }