[2/3] nss: Improve network file parser (bz 32573)

Message ID 4fznlwotb7fmqhxm2pmjvpvnc7ggkoeqemrx4qjqmfhg2guvlp@zdhp5aulh5ej (mailing list archive)
State Changes Requested
Delegated to: Adhemerval Zanella Netto
Headers
Series [1/3] Check octets more often in inet_network (bz 32575) |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Tobias Stoeckmann Jan. 19, 2025, 3:04 p.m. UTC
  Avoid malloca because /etc/networks lines can be arbitrarily long.
Instead of handcrafting the input for inet_network by adding ".0"
octets if they are missing, just left shift the result.

Also do not accept invalid entries, but ignore the line instead.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 nss/nss_files/files-network.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

--
2.48.1
  

Comments

Adhemerval Zanella Netto Feb. 4, 2025, 2:27 p.m. UTC | #1
On 19/01/25 12:04, Tobias Stoeckmann wrote:
> Avoid malloca because /etc/networks lines can be arbitrarily long.
> Instead of handcrafting the input for inet_network by adding ".0"
> octets if they are missing, just left shift the result.
> 
> Also do not accept invalid entries, but ignore the line instead.
> 
> Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>

The patch looks good, although I think it should be squashed with the rest
the series.

> ---
>  nss/nss_files/files-network.c | 20 ++++++--------------
>  1 file changed, 6 insertions(+), 14 deletions(-)
> 
> diff --git a/nss/nss_files/files-network.c b/nss/nss_files/files-network.c
> index ca94372024..f08daaf55f 100644
> --- a/nss/nss_files/files-network.c
> +++ b/nss/nss_files/files-network.c
> @@ -42,7 +42,8 @@ LINE_PARSER
> 
>     STRING_FIELD (addr, isspace, 1);
>     /* 'inet_network' does not add zeroes at the end if the network number
> -      does not four byte values.  We add them ourselves if necessary.  */
> +      does not contain four byte values.  We shift result ourselves if
> +      necessary.  */
>     cp = strchr (addr, '.');
>     if (cp != NULL)
>       {
> @@ -56,20 +57,11 @@ LINE_PARSER
>  	     ++n;
>  	 }
>       }
> -   if (n < 4)
> -     {
> -       char *newp = (char *) alloca (strlen (addr) + (4 - n) * 2 + 1);
> -       cp = stpcpy (newp, addr);
> -       do
> -	 {
> -	   *cp++ = '.';
> -	   *cp++ = '0';
> -	 }
> -       while (++n < 4);
> -       *cp = '\0';
> -       addr = newp;
> -     }
>     result->n_net = __inet_network (addr);
> +   if (result->n_net == INADDR_NONE)
> +     return 0;
> +   if (n < 4)
> +     result->n_net <<= 8 * (4 - n);
>     result->n_addrtype = AF_INET;
> 
>   })
> --
> 2.48.1
>
  

Patch

diff --git a/nss/nss_files/files-network.c b/nss/nss_files/files-network.c
index ca94372024..f08daaf55f 100644
--- a/nss/nss_files/files-network.c
+++ b/nss/nss_files/files-network.c
@@ -42,7 +42,8 @@  LINE_PARSER

    STRING_FIELD (addr, isspace, 1);
    /* 'inet_network' does not add zeroes at the end if the network number
-      does not four byte values.  We add them ourselves if necessary.  */
+      does not contain four byte values.  We shift result ourselves if
+      necessary.  */
    cp = strchr (addr, '.');
    if (cp != NULL)
      {
@@ -56,20 +57,11 @@  LINE_PARSER
 	     ++n;
 	 }
      }
-   if (n < 4)
-     {
-       char *newp = (char *) alloca (strlen (addr) + (4 - n) * 2 + 1);
-       cp = stpcpy (newp, addr);
-       do
-	 {
-	   *cp++ = '.';
-	   *cp++ = '0';
-	 }
-       while (++n < 4);
-       *cp = '\0';
-       addr = newp;
-     }
    result->n_net = __inet_network (addr);
+   if (result->n_net == INADDR_NONE)
+     return 0;
+   if (n < 4)
+     result->n_net <<= 8 * (4 - n);
    result->n_addrtype = AF_INET;

  })