From ac2c530abb5c3493795c083127ba4e80c5caea16 Mon Sep 17 00:00:00 2001 From: Hannah Neary Date: Thu, 25 Jul 2024 12:13:22 +0100 Subject: [PATCH] Use cached DNS records --- Cargo.lock | 5 ++-- data-plane/Cargo.toml | 1 + data-plane/src/dns/enclavedns.rs | 47 ++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0904a0a3..67eeaf82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1370,6 +1370,7 @@ dependencies = [ "tokio-vsock", "tower", "tower-http", + "trust-dns-proto", "uuid", "webpki-roots", "yup-hyper-mock", @@ -3428,9 +3429,9 @@ dependencies = [ [[package]] name = "trust-dns-proto" -version = "0.23.0" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dc775440033cb114085f6f2437682b194fa7546466024b1037e82a48a052a69" +checksum = "3119112651c157f4488931a01e586aa459736e9d6046d3bd9105ffb69352d374" dependencies = [ "async-trait", "cfg-if", diff --git a/data-plane/Cargo.toml b/data-plane/Cargo.toml index 79629429..aa7dd1a9 100644 --- a/data-plane/Cargo.toml +++ b/data-plane/Cargo.toml @@ -47,6 +47,7 @@ tower = { version = "0.4.13", features = ["util"] } tower-http = { version = "0.5.0", features = ["catch-panic"] } libc = "0.2.150" serial_test = "3.0.0" +trust-dns-proto = "0.23.2" [dev-dependencies] diff --git a/data-plane/src/dns/enclavedns.rs b/data-plane/src/dns/enclavedns.rs index a50aabcc..417e0d02 100644 --- a/data-plane/src/dns/enclavedns.rs +++ b/data-plane/src/dns/enclavedns.rs @@ -5,13 +5,18 @@ use shared::server::egress::{cache_ip_for_allowlist, EgressDestinations}; use shared::server::get_vsock_client; use shared::server::CID::Parent; use shared::DNS_PROXY_VSOCK_PORT; +use std::net::Ipv4Addr; use std::net::SocketAddr; +use std::str::FromStr; use std::sync::Arc; use std::time::Duration; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::UdpSocket; use tokio::sync::{mpsc::Receiver, Semaphore}; use tokio::time::timeout; +use trust_dns_proto::op::{Message, MessageType, OpCode, ResponseCode}; +use trust_dns_proto::rr::{DNSClass, Name, RData, Record, RecordType}; +use trust_dns_proto::serialize::binary::{BinEncodable, BinEncoder}; /// Empty struct for the DNS proxy that runs in the data plane pub struct EnclaveDnsProxy; @@ -121,6 +126,36 @@ impl EnclaveDnsDriver { } } + fn test() -> Vec { + let mut message = Message::new(); + message.set_id(1); // Set the message ID + message.set_message_type(MessageType::Response); + message.set_op_code(OpCode::Query); + message.set_authoritative(true); + message.set_recursion_desired(true); + message.set_response_code(ResponseCode::NoError); + + // Create an answer + let mut record: Record = Record::new(); + record.set_name(Name::from_str("www.example").unwrap()); + record.set_record_type(RecordType::A); + record.set_dns_class(DNSClass::IN); + record.set_ttl(300); + record.set_data(Some(RData::A(trust_dns_proto::rr::rdata::A( + Ipv4Addr::new(93, 184, 216, 34), + )))); + message.add_answer(record); + + // Encode the message to binary format + let mut buf = Vec::new(); + { + let mut encoder = BinEncoder::new(&mut buf); + message.emit(&mut encoder).unwrap(); + } + + return buf; + } + /// Perform a DNS lookup using the proxy running on the Host async fn perform_dns_lookup( dns_packet: Bytes, @@ -128,12 +163,12 @@ impl EnclaveDnsDriver { allowed_destinations: EgressDestinations, ) -> Result { // Check domain is allowed before proxying lookup - check_dns_allowed_for_domain(&dns_packet.clone(), &allowed_destinations)?; - // Attempt DNS lookup wth a timeout, flatten timeout errors into a DNS Error - let dns_response = - timeout(request_upper_bound, Self::forward_dns_lookup(dns_packet)).await??; - cache_ip_for_allowlist(&dns_response.clone())?; - Ok(dns_response) + // check_dns_allowed_for_domain(&dns_packet.clone(), &allowed_destinations)?; + // // Attempt DNS lookup wth a timeout, flatten timeout errors into a DNS Error + // let dns_response = + // timeout(request_upper_bound, Self::forward_dns_lookup(dns_packet)).await??; + // cache_ip_for_allowlist(&dns_response.clone())?; + Ok(Self::test().into()) } /// Takes a DNS lookup as `Bytes` and sends forwards it over VSock to the host process to be sent to