]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
dns: refactor function to get rrname to be safe
authorJason Ish <jason.ish@oisf.net>
Tue, 28 Jan 2025 22:34:07 +0000 (16:34 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 5 Mar 2025 14:59:58 +0000 (15:59 +0100)
Make the function safe by returning a reference to the DNSName object,
the unsafe C wrapper can do the conversion to pointers.

rust/src/dns/dns.rs

index f4f81a0d138b7d67e18b8ea1f8facfba5a5db0d1..dc8b60411ce7dc00b72d4eab8041810f06a56ebc 100644 (file)
@@ -1063,31 +1063,21 @@ pub unsafe extern "C" fn SCDnsTxGetAdditionalName(
     false
 }
 
-#[inline]
-unsafe fn dns_get_record_rdata(data: &DNSRData, buf: *mut *const u8, len: *mut u32) -> bool {
+fn get_rdata_name(data: &DNSRData) -> Option<&DNSName> {
     match data {
-        DNSRData::CNAME(bytes)
-        | DNSRData::PTR(bytes)
-        | DNSRData::MX(bytes)
-        | DNSRData::NS(bytes) => {
-            if !bytes.value.is_empty() {
-                *len = bytes.value.len() as u32;
-                *buf = bytes.value.as_ptr();
-                return true;
-            }
+        DNSRData::CNAME(name)
+        | DNSRData::PTR(name)
+        | DNSRData::MX(name)
+        | DNSRData::NS(name) => {
+            Some(name)
         }
         DNSRData::SOA(soa) => {
-            if !soa.mname.value.is_empty() {
-                *len = soa.mname.value.len() as u32;
-                *buf = soa.mname.value.as_ptr();
-                return true;
-            }
+            Some(&soa.mname)
         }
         _ => {
-            return false;
+            None
         }
     }
-    return false;
 }
 
 /// Get the DNS response answer rdata at index i that could be a domain name.
@@ -1099,7 +1089,13 @@ pub unsafe extern "C" fn SCDnsTxGetAnswerRdata(
 
     if let Some(response) = &tx.response {
         if let Some(record) = response.answers.get(index) {
-            return dns_get_record_rdata(&record.data, buf, len);
+            if let Some(name) = get_rdata_name(&record.data) {
+                if !name.value.is_empty() {
+                    *buf = name.value.as_ptr();
+                    *len = name.value.len() as u32;
+                    return true;
+                }
+            }
         }
     }
 
@@ -1115,7 +1111,13 @@ pub unsafe extern "C" fn SCDnsTxGetAuthorityRdata(
 
     if let Some(response) = &tx.response {
         if let Some(record) = response.authorities.get(index) {
-            return dns_get_record_rdata(&record.data, buf, len);
+            if let Some(name) = get_rdata_name(&record.data) {
+                if !name.value.is_empty() {
+                    *buf = name.value.as_ptr();
+                    *len = name.value.len() as u32;
+                    return true;
+                }
+            }
         }
     }
 
@@ -1131,7 +1133,13 @@ pub unsafe extern "C" fn SCDnsTxGetAdditionalRdata(
 
     if let Some(response) = &tx.response {
         if let Some(record) = response.additionals.get(index) {
-            return dns_get_record_rdata(&record.data, buf, len);
+            if let Some(name) = get_rdata_name(&record.data) {
+                if !name.value.is_empty() {
+                    *buf = name.value.as_ptr();
+                    *len = name.value.len() as u32;
+                    return true;
+                }
+            }
         }
     }