[PATCH net-next v3 3/5] netconsole: add support for strings with new line in netpoll_parse_ip_addr

Breno Leitao posted 5 patches 2 months, 2 weeks ago
There is a newer version of this series
[PATCH net-next v3 3/5] netconsole: add support for strings with new line in netpoll_parse_ip_addr
Posted by Breno Leitao 2 months, 2 weeks ago
The current IP address parsing logic fails when the input string
contains a trailing newline character. This can occur when IP
addresses are provided through configfs, which contains newlines in
a const buffer.

Teach netpoll_parse_ip_addr() how to ignore newlines at the end of the
IPs. Also, simplify the code by:

 * No need to check for separators. Try to parse ipv4, if it fails try
   ipv6 similarly to ceph_pton()
 * If ipv6 is not supported, don't call in6_pton() at all.

Signed-off-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Simon Horman <horms@kernel.org>
---
 drivers/net/netconsole.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 8d1b93264e0fd..3188cc180a934 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -303,20 +303,20 @@ static void netconsole_print_banner(struct netpoll *np)
 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
 {
 	const char *end;
+	int len;
 
-	if (!strchr(str, ':') &&
-	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
-		if (!*end)
-			return 0;
-	}
-	if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
-#if IS_ENABLED(CONFIG_IPV6)
-		if (!*end)
-			return 1;
-#else
+	len = strlen(str);
+	if (!len)
 		return -1;
-#endif
-	}
+
+	if (str[len - 1] == '\n')
+		len -= 1;
+
+	if (in4_pton(str, len, (void *)addr, -1, &end) > 0)
+		return 0;
+	if (IS_ENABLED(CONFIG_IPV6) &&
+	    in6_pton(str, len, addr->in6.s6_addr, -1, &end) > 0)
+		return 1;
 	return -1;
 }
 

-- 
2.47.1
Re: [PATCH net-next v3 3/5] netconsole: add support for strings with new line in netpoll_parse_ip_addr
Posted by Jakub Kicinski 2 months, 1 week ago
On Wed, 23 Jul 2025 10:20:31 -0700 Breno Leitao wrote:
>  	const char *end;
> +	int len;
>  
> -	if (!strchr(str, ':') &&
> -	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
> -		if (!*end)
> -			return 0;
> -	}
> -	if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
> -#if IS_ENABLED(CONFIG_IPV6)
> -		if (!*end)
> -			return 1;
> -#else
> +	len = strlen(str);
> +	if (!len)
>  		return -1;
> -#endif
> -	}
> +
> +	if (str[len - 1] == '\n')
> +		len -= 1;
> +
> +	if (in4_pton(str, len, (void *)addr, -1, &end) > 0)
> +		return 0;
> +	if (IS_ENABLED(CONFIG_IPV6) &&
> +	    in6_pton(str, len, addr->in6.s6_addr, -1, &end) > 0)
> +		return 1;
>  	return -1;

Looks like we're removing the validation for reaching the end of 
the buffer? This should at least be explained in the commit message.
AFAICT for IPv4 for example 1.2.3.4:xyz would have been rejected,
now it's accepted. Personally I find the

	if (*end && *end != '\n')

removed by subsequent patch cleaner than modifying the input string.

I'll apply the first patch of the series..