unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / code / Atom feed
* [PATCH v1] nis: Fix leak on realloc failure in nis_getnames
@ 2021-07-28 17:47 Robbie Harwood via Libc-alpha
  2021-07-28 18:03 ` Florian Weimer via Libc-alpha
  2021-07-28 18:06 ` [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha
  0 siblings, 2 replies; 5+ messages in thread
From: Robbie Harwood via Libc-alpha @ 2021-07-28 17:47 UTC (permalink / raw)
  To: libc-alpha

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: v1-0001-nis-Fix-leak-on-realloc-failure-in-nis_getnames.patch --]
[-- Type: text/x-diff, Size: 1354 bytes --]

From 7aa0a9f879d5b2117beb06771bb4fdbaf25699a9 Mon Sep 17 00:00:00 2001
From: Robbie Harwood <rharwood@redhat.com>
Date: Wed, 28 Jul 2021 12:54:44 -0400
Subject: [PATCH v1] nis: Fix leak on realloc failure in nis_getnames
To: libc-alpha@sourceware.org
Cc: kukuk@suse.de

If pos >= count but realloc fails, tmp will not have been placed in
getnames[pos] yet, and so will not be freed in free_null.  Detected
by Coverity.

Also remove misleading comment from nis_getnames(), since it actually
did properly release getnames when out of memory.
---
 nis/nis_subr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/nis/nis_subr.c b/nis/nis_subr.c
index dd0e30071d..6784fc353f 100644
--- a/nis/nis_subr.c
+++ b/nis/nis_subr.c
@@ -103,9 +103,6 @@ count_dots (const_nis_name str)
   return count;
 }
 
-/* If we run out of memory, we don't give already allocated memory
-   free. The overhead for bringing getnames back in a safe state to
-   free it is to big. */
 nis_name *
 nis_getnames (const_nis_name name)
 {
@@ -271,7 +268,10 @@ nis_getnames (const_nis_name name)
 	      nis_name *newp = realloc (getnames,
 					(count + 1) * sizeof (char *));
 	      if (__glibc_unlikely (newp == NULL))
-		goto free_null;
+		{
+		  free (tmp);
+		  goto free_null;
+		}
 	      getnames = newp;
 	    }
 	  getnames[pos] = tmp;
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] nis: Fix leak on realloc failure in nis_getnames
  2021-07-28 17:47 [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha
@ 2021-07-28 18:03 ` Florian Weimer via Libc-alpha
  2021-07-28 18:23   ` [PATCH v2 1/1] nis: Fix leak on realloc failure in nis_getnames [BZ #28150] Robbie Harwood via Libc-alpha
  2021-07-28 18:06 ` [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha
  1 sibling, 1 reply; 5+ messages in thread
From: Florian Weimer via Libc-alpha @ 2021-07-28 18:03 UTC (permalink / raw)
  To: Robbie Harwood via Libc-alpha; +Cc: Robbie Harwood

* Robbie Harwood via Libc-alpha:

> From 7aa0a9f879d5b2117beb06771bb4fdbaf25699a9 Mon Sep 17 00:00:00 2001
> From: Robbie Harwood <rharwood@redhat.com>
> Date: Wed, 28 Jul 2021 12:54:44 -0400
> Subject: [PATCH v1] nis: Fix leak on realloc failure in nis_getnames
> To: libc-alpha@sourceware.org
> Cc: kukuk@suse.de
>
> If pos >= count but realloc fails, tmp will not have been placed in
> getnames[pos] yet, and so will not be freed in free_null.  Detected
> by Coverity.
>
> Also remove misleading comment from nis_getnames(), since it actually
> did properly release getnames when out of memory.
> ---
>  nis/nis_subr.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/nis/nis_subr.c b/nis/nis_subr.c
> index dd0e30071d..6784fc353f 100644
> --- a/nis/nis_subr.c
> +++ b/nis/nis_subr.c
> @@ -103,9 +103,6 @@ count_dots (const_nis_name str)
>    return count;
>  }
>  
> -/* If we run out of memory, we don't give already allocated memory
> -   free. The overhead for bringing getnames back in a safe state to
> -   free it is to big. */
>  nis_name *
>  nis_getnames (const_nis_name name)
>  {
> @@ -271,7 +268,10 @@ nis_getnames (const_nis_name name)
>  	      nis_name *newp = realloc (getnames,
>  					(count + 1) * sizeof (char *));
>  	      if (__glibc_unlikely (newp == NULL))
> -		goto free_null;
> +		{
> +		  free (tmp);
> +		  goto free_null;
> +		}
>  	      getnames = newp;
>  	    }
>  	  getnames[pos] = tmp;

The patch looks correct to me.  (There some similar code above, but it
is already correct because the tmp ownership transfer is different.)

Would you please open a bug in Bugzilla and reference it in the commit
message.

I don't think we need to treat this as a security vulnerability (denial
of service) because since bug only happens after a memory allocation
failure, and at that point, the service is already denied, so to speak.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v1] nis: Fix leak on realloc failure in nis_getnames
  2021-07-28 17:47 [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha
  2021-07-28 18:03 ` Florian Weimer via Libc-alpha
@ 2021-07-28 18:06 ` Robbie Harwood via Libc-alpha
  1 sibling, 0 replies; 5+ messages in thread
From: Robbie Harwood via Libc-alpha @ 2021-07-28 18:06 UTC (permalink / raw)
  To: libc-alpha; +Cc: Robbie Harwood

If pos >= count but realloc fails, tmp will not have been placed in
getnames[pos] yet, and so will not be freed in free_null.  Detected
by Coverity.

Also remove misleading comment from nis_getnames(), since it actually
did properly release getnames when out of memory.
---
 nis/nis_subr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/nis/nis_subr.c b/nis/nis_subr.c
index dd0e30071d..6784fc353f 100644
--- a/nis/nis_subr.c
+++ b/nis/nis_subr.c
@@ -103,9 +103,6 @@ count_dots (const_nis_name str)
   return count;
 }
 
-/* If we run out of memory, we don't give already allocated memory
-   free. The overhead for bringing getnames back in a safe state to
-   free it is to big. */
 nis_name *
 nis_getnames (const_nis_name name)
 {
@@ -271,7 +268,10 @@ nis_getnames (const_nis_name name)
 	      nis_name *newp = realloc (getnames,
 					(count + 1) * sizeof (char *));
 	      if (__glibc_unlikely (newp == NULL))
-		goto free_null;
+		{
+		  free (tmp);
+		  goto free_null;
+		}
 	      getnames = newp;
 	    }
 	  getnames[pos] = tmp;
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/1] nis: Fix leak on realloc failure in nis_getnames [BZ #28150]
  2021-07-28 18:03 ` Florian Weimer via Libc-alpha
@ 2021-07-28 18:23   ` Robbie Harwood via Libc-alpha
  2021-08-02 15:24     ` Carlos O'Donell via Libc-alpha
  0 siblings, 1 reply; 5+ messages in thread
From: Robbie Harwood via Libc-alpha @ 2021-07-28 18:23 UTC (permalink / raw)
  To: libc-alpha, fweimer; +Cc: Robbie Harwood

If pos >= count but realloc fails, tmp will not have been placed in
getnames[pos] yet, and so will not be freed in free_null.  Detected
by Coverity.

Also remove misleading comment from nis_getnames(), since it actually
did properly release getnames when out of memory.
---
 nis/nis_subr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/nis/nis_subr.c b/nis/nis_subr.c
index dd0e30071d..6784fc353f 100644
--- a/nis/nis_subr.c
+++ b/nis/nis_subr.c
@@ -103,9 +103,6 @@ count_dots (const_nis_name str)
   return count;
 }
 
-/* If we run out of memory, we don't give already allocated memory
-   free. The overhead for bringing getnames back in a safe state to
-   free it is to big. */
 nis_name *
 nis_getnames (const_nis_name name)
 {
@@ -271,7 +268,10 @@ nis_getnames (const_nis_name name)
 	      nis_name *newp = realloc (getnames,
 					(count + 1) * sizeof (char *));
 	      if (__glibc_unlikely (newp == NULL))
-		goto free_null;
+		{
+		  free (tmp);
+		  goto free_null;
+		}
 	      getnames = newp;
 	    }
 	  getnames[pos] = tmp;
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/1] nis: Fix leak on realloc failure in nis_getnames [BZ #28150]
  2021-07-28 18:23   ` [PATCH v2 1/1] nis: Fix leak on realloc failure in nis_getnames [BZ #28150] Robbie Harwood via Libc-alpha
@ 2021-08-02 15:24     ` Carlos O'Donell via Libc-alpha
  0 siblings, 0 replies; 5+ messages in thread
From: Carlos O'Donell via Libc-alpha @ 2021-08-02 15:24 UTC (permalink / raw)
  To: Robbie Harwood, libc-alpha, fweimer

On 7/28/21 2:23 PM, Robbie Harwood via Libc-alpha wrote:
> If pos >= count but realloc fails, tmp will not have been placed in
> getnames[pos] yet, and so will not be freed in free_null.  Detected
> by Coverity.
> 
> Also remove misleading comment from nis_getnames(), since it actually
> did properly release getnames when out of memory.

The CI/CD patchwork trybot didn't trigger for this patch and I need
to review that with DJ.

I tested on x86_64 and i686 without regression. Florian reviewed.
I'm pushing this fix.

Tested-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  nis/nis_subr.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/nis/nis_subr.c b/nis/nis_subr.c
> index dd0e30071d..6784fc353f 100644
> --- a/nis/nis_subr.c
> +++ b/nis/nis_subr.c
> @@ -103,9 +103,6 @@ count_dots (const_nis_name str)
>    return count;
>  }
>  
> -/* If we run out of memory, we don't give already allocated memory
> -   free. The overhead for bringing getnames back in a safe state to
> -   free it is to big. */
>  nis_name *
>  nis_getnames (const_nis_name name)
>  {
> @@ -271,7 +268,10 @@ nis_getnames (const_nis_name name)
>  	      nis_name *newp = realloc (getnames,
>  					(count + 1) * sizeof (char *));
>  	      if (__glibc_unlikely (newp == NULL))
> -		goto free_null;
> +		{
> +		  free (tmp);
> +		  goto free_null;
> +		}
>  	      getnames = newp;
>  	    }
>  	  getnames[pos] = tmp;
> 


-- 
Cheers,
Carlos.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-02 15:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-28 17:47 [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha
2021-07-28 18:03 ` Florian Weimer via Libc-alpha
2021-07-28 18:23   ` [PATCH v2 1/1] nis: Fix leak on realloc failure in nis_getnames [BZ #28150] Robbie Harwood via Libc-alpha
2021-08-02 15:24     ` Carlos O'Donell via Libc-alpha
2021-07-28 18:06 ` [PATCH v1] nis: Fix leak on realloc failure in nis_getnames Robbie Harwood via Libc-alpha

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/glibc.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).