bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: "Tim Rühsen" <tim.ruehsen@gmx.de>, bug-gnulib@gnu.org
Subject: Re: heap-use-after-free in rpl_glob
Date: Fri, 17 Jan 2020 22:00:35 +0100	[thread overview]
Message-ID: <2031383.ETbFSIWNRB@omega> (raw)
In-Reply-To: <3c926e49-6d61-21d5-7688-d830b029341c@cs.ucla.edu>

[-- Attachment #1: Type: text/plain, Size: 512 bytes --]

Hi Paul,

> It seems that your patch is incomplete, as there's a memory leak in the 
> unlikely case that the malloc call fails.

Oops, indeed. Thanks for the review!

> Something like the attached patch instead, perhaps?

Hmm, this patch is freeing the same object through the variable 'dirname'
on one branch and through the variable 'previous_dirname' on the other
branch. A bit hard to understand, and thus likely to introduce more bugs
in the future, I would say. I prefer the attached one. Pushed.

Bruno


[-- Attachment #2: 0001-glob-Fix-use-after-free-bug.patch --]
[-- Type: text/x-patch, Size: 2740 bytes --]

From 717766da8926e36cf86015c4a49554baa854e8e6 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Fri, 17 Jan 2020 21:56:01 +0100
Subject: [PATCH] glob: Fix use-after-free bug.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reported by Tim Rühsen <tim.ruehsen@gmx.de> in
<https://lists.gnu.org/archive/html/bug-gnulib/2020-01/msg00102.html>.

* lib/glob.c (__glob): Delay freeing dirname until after the use of
end_name.
---
 ChangeLog  | 9 +++++++++
 lib/glob.c | 8 ++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 643dba3..4f4718a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-01-17  Bruno Haible  <bruno@clisp.org>
+            Paul Eggert  <eggert@cs.ucla.edu>
+
+	glob: Fix use-after-free bug.
+	Reported by Tim Rühsen <tim.ruehsen@gmx.de> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2020-01/msg00102.html>.
+	* lib/glob.c (__glob): Delay freeing dirname until after the use of
+	end_name.
+
 2020-01-16  Siddhesh Poyarekar  <siddhesh@gotplt.org>
 
 	vcs-to-changelog: Fix parsing of fndecl without args.
diff --git a/lib/glob.c b/lib/glob.c
index a67cbb6..add5d93 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -843,10 +843,11 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
               {
                 size_t home_len = strlen (p->pw_dir);
                 size_t rest_len = end_name == NULL ? 0 : strlen (end_name);
+                /* dirname contains end_name; we can't free it now.  */
+                char *prev_dirname =
+                  (__glibc_unlikely (malloc_dirname) ? dirname : NULL);
                 char *d;
 
-                if (__glibc_unlikely (malloc_dirname))
-                  free (dirname);
                 malloc_dirname = 0;
 
                 if (glob_use_alloca (alloca_used, home_len + rest_len + 1))
@@ -857,6 +858,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
                     dirname = malloc (home_len + rest_len + 1);
                     if (dirname == NULL)
                       {
+                        free (prev_dirname);
                         scratch_buffer_free (&pwtmpbuf);
                         retval = GLOB_NOSPACE;
                         goto out;
@@ -868,6 +870,8 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
                   d = mempcpy (d, end_name, rest_len);
                 *d = '\0';
 
+                free (prev_dirname);
+
                 dirlen = home_len + rest_len;
                 dirname_modified = 1;
               }
-- 
2.7.4


      reply	other threads:[~2020-01-17 21:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-17 15:50 heap-use-after-free in rpl_glob Tim Rühsen
2020-01-17 17:00 ` Bruno Haible
2020-01-17 19:29   ` Tim Rühsen
2020-01-17 19:52   ` Paul Eggert
2020-01-17 21:00     ` Bruno Haible [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2031383.ETbFSIWNRB@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=tim.ruehsen@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).