From 778d6694eb0fa78e6bf4cad12f6047118873753a Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 14 Nov 2025 10:37:33 +0100 Subject: [PATCH] dnsheader_aligned: Prevent copies The `dnsheader_aligned` object contains a pointer that references either the `dnsheader` passed to the constructor if it is properly aligned, or the internal `dnsheader` member. In the second case, making a copy would mean we can reference an object that has been destructed, which is a serious problem. This commit also ensures copy elision is done `DNSQuestion:getHeader`, as otherwise the compiler might refuse to compile. Signed-off-by: Remi Gacogne --- pdns/dns.hh | 5 +++++ pdns/dnsdistdist/dnsdist.hh | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pdns/dns.hh b/pdns/dns.hh index 19afdbc841..b946f4fe1b 100644 --- a/pdns/dns.hh +++ b/pdns/dns.hh @@ -205,6 +205,11 @@ public: d_p = &d_h; } } + dnsheader_aligned(const dnsheader_aligned&) = delete; + dnsheader_aligned(dnsheader_aligned&&) = delete; + dnsheader_aligned& operator=(const dnsheader_aligned&) = delete; + dnsheader_aligned& operator=(dnsheader_aligned&&) = delete; + ~dnsheader_aligned() = default; [[nodiscard]] const dnsheader* get() const { diff --git a/pdns/dnsdistdist/dnsdist.hh b/pdns/dnsdistdist/dnsdist.hh index 6b6cc73f0f..355d67f4ed 100644 --- a/pdns/dnsdistdist/dnsdist.hh +++ b/pdns/dnsdistdist/dnsdist.hh @@ -87,8 +87,7 @@ struct DNSQuestion if (data.size() < sizeof(dnsheader)) { throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer"); } - dnsheader_aligned dh(data.data()); - return dh; + return dnsheader_aligned(data.data()); } /* this function is not safe against unaligned access, you should -- 2.47.3