Parsing logic for /etc/resolv.conf currently does not take into account line breaks and can erroneously interpret e.g. comments in the file as valid nameservers. Fix the parser to scan line by line and explicitly expect a bounded key-val pair to resolve the issue. Signed-off-by: Kajetan Puchalski --- net/uip/dhcp.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/uip/dhcp.c b/net/uip/dhcp.c index 9de5588..a949a6b 100644 --- a/net/uip/dhcp.c +++ b/net/uip/dhcp.c @@ -42,13 +42,15 @@ int uip_dhcp_get_dns(struct uip_info *info) int n = 0; FILE *fp; u32 ip; + char *line = NULL; + size_t capacity = 0; fp = fopen("/etc/resolv.conf", "r"); if (!fp) return ret; - while (!feof(fp)) { - if (fscanf(fp, "%s %s\n", key, val) != 2) + while (getline(&line, &capacity, fp) >= 0) { + if (sscanf(line, "%255s %255s", key, val) != 2) continue; if (strncmp("domain", key, 6) == 0) info->domain_name = strndup(val, UIP_DHCP_MAX_DOMAIN_NAME_LEN); @@ -62,6 +64,7 @@ int uip_dhcp_get_dns(struct uip_info *info) } } + free(line); fclose(fp); return ret; } -- 2.56.1