Browse Source

fix issues in ethers.c

Old version manages strings the regular way (i.e. counting on zero-ended
sequences). In fact strings captured from the /etc/ethers file are
'\n'-ended. So, for example, using strchr function could lead to buffer
overflow.

Reported-by: "Andrey V. Zhmurin" <zhmurin_a@mcst.ru
Waldemar Brodkorb 6 years ago
parent
commit
365d19dfec
1 changed files with 4 additions and 6 deletions
  1. 4 6
      libc/inet/ethers.c

+ 4 - 6
libc/inet/ethers.c

@@ -32,11 +32,11 @@ static const char *__ether_line(const char *line, struct ether_addr *addr)
 	if (!res)
 		return NULL;
 
-	while (*line && (*line != ' ') && (*line != '\t'))
+	while (*line && (*line != '\n') && (*line != ' ') && (*line != '\t'))
 		line++;
-	while (*line && ((*line == ' ')	|| (*line == '\t')))
+	while (*line && (*line != '\n') && ((*line == ' ') || (*line == '\t')))
 		line++;
-	return (*line) ? line : NULL;
+	return (*line && (*line != '\n')) ? line : NULL;
 }
 
 /*
@@ -45,9 +45,7 @@ static const char *__ether_line(const char *line, struct ether_addr *addr)
  */
 static const char *__ether_line_w(char *line, struct ether_addr *addr)
 {
-	char *end = strchr(line, '#');
-	if (!end)
-		end = strchr(line, '\n');
+	char *end = strpbrk(line, "#\n");
 	if (end)
 		*end = '\0';
 	return __ether_line(line, addr);