commit 636c6dc3faa5bb93b0f31b2821870ef152af9a7b
parent eb503b8ee40a0b9b6ee28ecee7382b9fc579ab95
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 21 Jun 2021 10:29:04 -0400
net::dns: validate names are ASCII
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/net/dns/encoding.ha b/net/dns/encoding.ha
@@ -1,3 +1,4 @@
+use ascii;
use endian;
use fmt;
use net::ip;
@@ -123,8 +124,14 @@ export fn decode_name(dec: *decoder, buf: []u8) (([]u8, str) | format) = {
let offs = endian::begetu16(buf) & ~0b1100000000000000u16;
return decode_name(dec, dec.buf[offs..]);
};
- // TODO: Validate ASCII here
- return (buf[z + 1..], strings::fromutf8(buf[1..z + 1]));
+ let name = buf[1..z + 1];
+ buf = buf[z + 1..];
+ for (let i = 0z; i < len(name); i += 1) {
+ if (!ascii::isascii(name[i]: u32: rune)) {
+ return format;
+ };
+ };
+ return (buf, strings::fromutf8(name));
};
// Decodes the rdata field of an A (address) record. The return value is
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -555,7 +555,7 @@ net_dns() {
error.ha \
encoding.ha \
types.ha
- gen_ssa net::dns endian net net::udp net::ip fmt
+ gen_ssa net::dns ascii endian net net::udp net::ip fmt
}
gensrcs_net_ip() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -826,7 +826,7 @@ stdlib_net_dns_srcs= \
$(STDLIB)/net/dns/encoding.ha \
$(STDLIB)/net/dns/types.ha
-$(HARECACHE)/net/dns/net_dns.ssa: $(stdlib_net_dns_srcs) $(stdlib_rt) $(stdlib_endian) $(stdlib_net) $(stdlib_net_udp) $(stdlib_net_ip) $(stdlib_fmt)
+$(HARECACHE)/net/dns/net_dns.ssa: $(stdlib_net_dns_srcs) $(stdlib_rt) $(stdlib_ascii) $(stdlib_endian) $(stdlib_net) $(stdlib_net_udp) $(stdlib_net_ip) $(stdlib_fmt)
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/net/dns
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nnet::dns \
@@ -1947,7 +1947,7 @@ testlib_net_dns_srcs= \
$(STDLIB)/net/dns/encoding.ha \
$(STDLIB)/net/dns/types.ha
-$(TESTCACHE)/net/dns/net_dns.ssa: $(testlib_net_dns_srcs) $(testlib_rt) $(testlib_endian) $(testlib_net) $(testlib_net_udp) $(testlib_net_ip) $(testlib_fmt)
+$(TESTCACHE)/net/dns/net_dns.ssa: $(testlib_net_dns_srcs) $(testlib_rt) $(testlib_ascii) $(testlib_endian) $(testlib_net) $(testlib_net_udp) $(testlib_net_ip) $(testlib_fmt)
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/net/dns
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nnet::dns \