From: "Omar Polo" Subject: trying to make getrrsetbyname usable To: tech@openbsd.org Date: Wed, 11 Nov 2026 14:21:26 +0100 my end goal is to delete unpack_dns.* from smtpd; I have a tree with smtpd converted but it needs a sane getrrsetbyname first. the problem of getrrsetbyname (and its _async() version) is clearly stated by the BUGS section of the manpage: : BUGS : The data in *rdi_data should be returned in uncompressed wire format. : Currently, the data is in compressed format and the caller can't : uncompress since it doesn't have the full message. so my plan is to have getrrsetbyname uncompressing the data, before returning it to the caller. quite simple hopefully :-) diff below is two commits really, expose _asr_dname_expand and the actual uncompression. I'm sending them together, but single diffs are available on request. with this diff in, getrrsetbyname{,_async}() becomes, IMHO, finally usable. I have a small program i used to test this, in ~op/mxtest if you want to give it a quick spin. Play with the Makefile to point it to a copy of libc built with this diff (don't need to install it system wide if you don't want) to see the difference and intended usage I want to bring to smptd later. I don't think this is going to have any impact an current code, from what i see the callers in base are isakmpd for KEY, ssh for SSHFP and traceroute for TXT, all of them who are uncompressed and so not changed by my diff. MX, CNAME, SRV, PTR, ecc instead may be compressed, and are finally consumable via this interface. p.s. there's a set of unpack routines in asr/, but they also convert from network byte order and not just uncompress, so they're not usable here I think. thoughts? diff refs/remotes/op/master 816e626b0b94b60718bda6d7281b7f41df0af6cd commit - 3dbbc6f9ce77759ee37eeaaf98eb4c7326b83e8f commit + 816e626b0b94b60718bda6d7281b7f41df0af6cd blob - 27f5421a946888769a68cba1fb618b8c8c177af7 blob + 097e297e44bbf5af07e1af8050db0c41317adea1 --- lib/libc/asr/asr_private.h +++ lib/libc/asr/asr_private.h @@ -298,6 +298,8 @@ int _asr_unpack_rr(struct asr_unpack *, struct asr_dns int _asr_sockaddr_from_str(struct sockaddr *, int, const char *); ssize_t _asr_dname_from_fqdn(const char *, char *, size_t); ssize_t _asr_addr_as_fqdn(const char *, int, char *, size_t); +ssize_t _asr_dname_expand(const unsigned char *, size_t, size_t, size_t *, + char *, size_t); int hnok_lenient(const char *); int _asr_is_localhost(const char*); blob - 6ed2f5e0cb4efadf9f55d29ccee6734808af16e8 blob + d595755c575afd477b5b1e411423e34678a2c077 --- lib/libc/asr/asr_utils.c +++ lib/libc/asr/asr_utils.c @@ -35,8 +35,6 @@ #include "asr_private.h" static int dname_check_label(const char *, size_t); -static ssize_t dname_expand(const unsigned char *, size_t, size_t, size_t *, - char *, size_t); static int unpack_data(struct asr_unpack *, void *, size_t); static int unpack_u16(struct asr_unpack *, uint16_t *); @@ -108,8 +106,8 @@ _asr_dname_from_fqdn(const char *str, char *dst, size_ return (res + 1); } -static ssize_t -dname_expand(const unsigned char *data, size_t len, size_t offset, +ssize_t +_asr_dname_expand(const unsigned char *data, size_t len, size_t offset, size_t *newoffset, char *dst, size_t max) { size_t n, end, ptr, start; @@ -242,7 +240,7 @@ unpack_dname(struct asr_unpack *p, char *dst, size_t m if (p->err) return (-1); - e = dname_expand(p->buf, p->len, p->offset, &p->offset, dst, max); + e = _asr_dname_expand(p->buf, p->len, p->offset, &p->offset, dst, max); if (e == -1) { p->err = EINVAL; return (-1); blob - a3328abfb55e2cc584bcd91737863dda82b2e28f blob + 3fca63e7c8e20e2b12eba2099f979170c2780c83 --- lib/libc/asr/getrrsetbyname_async.c +++ lib/libc/asr/getrrsetbyname_async.c @@ -486,12 +486,98 @@ parse_dns_qsection(const u_char *answer, int size, con return (head); } +static int +rdata_uncompress(struct dns_rr *rr, const u_char *answer, int size, + const u_char *cp) +{ + static const struct rtype { + int type; + size_t prefix; + size_t suffix; + size_t names; + } rtypes[] = { + // RFC 1035 + { .type = T_CNAME, .names = 1 }, + { .type = T_MB, .names = 1 }, + { .type = T_MD, .names = 1 }, + { .type = T_MF, .names = 1 }, + { .type = T_MG, .names = 1 }, + { .type = T_MR, .names = 1 }, + { .type = T_NS, .names = 1 }, + { .type = T_PTR, .names = 1 }, + { .type = T_MINFO, .names = 2 }, + { .type = T_MX, .prefix = INT16SZ, .names = 1 }, + { .type = T_SOA, .names = 2, .suffix = 5 * INT32SZ }, + + // RFC 1183 + { .type = T_AFSDB, .prefix = INT16SZ, .names = 1 }, + { .type = T_RP, .names = 2 }, + { .type = T_RT, .prefix = INT16SZ, .names = 1 }, + + // RFC 2163 + { .type = T_PX, .prefix = INT16SZ, .names = 2 }, + + // RFC 2782 + { .type = T_SRV, .prefix = 3 * INT16SZ, .names = 1}, + + // TODO: NAPTR, SIG, NXT ? + }; + const struct rtype *rt = NULL; + char buf[3*INT16SZ + 2*MAXCDNAME + 5*INT32SZ]; + size_t off, end, len, i; + ssize_t r; + + for (i = 0; i < sizeof(rtypes)/sizeof(rtypes[0]); ++i) { + if (rtypes[i].type == rr->type) { + rt = &rtypes[i]; + break; + } + } + + if (rt == NULL) { /* nothing to uncompress */ + if ((rr->rdata = malloc(rr->size)) == NULL) + return (-1); + memcpy(rr->rdata, cp, rr->size); + return (0); + } + + off = cp - answer; + end = off + rr->size; + if (rt->prefix > rr->size) + return (-1); + + memcpy(buf, cp, rt->prefix); + len = rt->prefix; + off += rt->prefix; + + for (i = 0; i < rt->names; ++i) { + r = _asr_dname_expand(answer, size, off, &off, buf + len, + sizeof(buf) - len); + if (r == -1 || r > MAXCDNAME || off > end) + return (-1); + len += r; + } + + if (end - off != rt->suffix) + return (-1); + + memcpy(buf + len, answer + off, rt->suffix); + len += rt->suffix; + + if ((rr->rdata = malloc(len)) == NULL) + return (-1); + memcpy(rr->rdata, buf, len); + rr->size = len; + return (0); +} + static struct dns_rr * parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count) { struct dns_rr *head, *curr, *prev; int i, length; + u_int16_t cursiz; char name[MAXDNAME]; #define NEED(need) \ @@ -553,14 +639,14 @@ parse_dns_rrsection(const u_char *answer, int size, co /* rdata itself */ NEED(curr->size); + cursiz = curr->size; /* rdata_uncompress changes this */ if (curr->size != 0) { - if ((curr->rdata = malloc(curr->size)) == NULL) { + if (rdata_uncompress(curr, answer, size, *cp) == -1) { free_dns_rr(head); return (NULL); } - memcpy(curr->rdata, *cp, curr->size); } - *cp += curr->size; + *cp += cursiz; } #undef NEED