Prevent potential stall on dns proxy operations.
Update wire protocol to return and process error code first.
This will make sure dns proxy operations do not stall when
an internal error happens.
Also fix a compiler warning.
Also fix a potential buffer overflow.
And use correct types (uint32_t) rather than int when reading from network.
Change-Id: I9f99c16d6fd5e9137491a4d1b293a7c78e31b9c3
diff --git a/libc/netbsd/net/getaddrinfo.c b/libc/netbsd/net/getaddrinfo.c
index 6ae6e3e..7dd65ff 100644
--- a/libc/netbsd/net/getaddrinfo.c
+++ b/libc/netbsd/net/getaddrinfo.c
@@ -130,6 +130,9 @@
};
#endif
+// This should be synchronized to ResponseCode.h
+static const int DnsProxyQueryResult = 222;
+
static const struct afd {
int a_af;
int a_addrlen;
@@ -476,12 +479,15 @@
goto exit;
}
- int remote_rv;
- if (fread(&remote_rv, sizeof(int), 1, proxy) != 1) {
+ char buf[5];
+ // read result code for gethostbyaddr
+ if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) {
goto exit;
}
- if (remote_rv != 0) {
+ int result_code = (int)strtol(buf, NULL, 10);
+ // verify the code itself
+ if (result_code != DnsProxyQueryResult ) {
goto exit;
}