소스 검색

Crash when request would be too long

Integer conversions again. This crashes dog when the domain name being queried pushes the request over 65536 bytes, which is still better than sending a malformed packet.
Benjamin Sago 4 년 전
부모
커밋
9f895ef94b
2개의 변경된 파일6개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 1
      dns-transport/src/tcp.rs
  2. 3 1
      dns-transport/src/tls.rs

+ 3 - 1
dns-transport/src/tcp.rs

@@ -1,3 +1,5 @@
+use std::convert::TryFrom;
+
 use async_trait::async_trait;
 use log::*;
 use tokio::net::TcpStream;
@@ -65,7 +67,7 @@ impl Transport for TcpTransport {
         // The message is prepended with the length when sent over TCP,
         // so the server knows how long it is (RFC 1035 §4.2.2)
         let mut bytes = request.to_bytes().expect("failed to serialise request");
-        let len_bytes = (bytes.len() as u16).to_be_bytes();
+        let len_bytes = u16::try_from(bytes.len()).expect("request too long").to_be_bytes();
         bytes.insert(0, len_bytes[0]);
         bytes.insert(1, len_bytes[1]);
 

+ 3 - 1
dns-transport/src/tls.rs

@@ -1,3 +1,5 @@
+use std::convert::TryFrom;
+
 use async_trait::async_trait;
 use log::*;
 use native_tls::TlsConnector;
@@ -66,7 +68,7 @@ impl Transport for TlsTransport {
 
         // As with TCP, we need to prepend the message with its length.
         let mut bytes = request.to_bytes().expect("failed to serialise request");
-        let len_bytes = (bytes.len() as u16).to_be_bytes();
+        let len_bytes = u16::try_from(bytes.len()).expect("request too long").to_be_bytes();
         bytes.insert(0, len_bytes[0]);
         bytes.insert(1, len_bytes[1]);