diff mbox

[U-Boot] tools/proftool: fix use-after-free

Message ID 20151007141929.GO3829@bill-the-cat
State Rejected
Delegated to: Tom Rini
Headers show

Commit Message

Tom Rini Oct. 7, 2015, 2:19 p.m. UTC
On Wed, Oct 07, 2015 at 03:48:48PM +0200, Vincent Stehlé wrote:

> The read_trace_config() can dereference the line pointer after freeing
> it on its error path. Avoid that.
> 
> This was found by Coverity Scan.
> 
> Signed-off-by: Vincent Stehlé <vincent.stehle@freescale.com>
> Cc: Simon Glass <sjg@chromium.org>

Were you in the Coverity talk too? :)  I saw this error as well today
now.  I was actually thinking along the lines of:

Comments

Vincent Stehlé Oct. 7, 2015, 2:35 p.m. UTC | #1
On 10/07/2015 04:19 PM, Tom Rini wrote:
..
> Were you in the Coverity talk too? :)

Hi Tom,

No, I was not following that talk, sorry.

..
>                         free(line);
> -                       return regex_report_error(&line->regex, err, "compile",
> +                       err = regex_report_error(&line->regex, err, "compile",
>                                                   tok);
> +                       return err;

I am not sure you solve the problem this way. Indeed the structure
pointed to by the line pointer will still have been freed before use
even this way. Who knows what the memory contains when regerror() will
access &line->regex, which is contained into the freed structure?

Best regards,

V.
Tom Rini Oct. 7, 2015, 3:42 p.m. UTC | #2
On Wed, Oct 07, 2015 at 04:35:53PM +0200, Vincent Stehlé wrote:
> On 10/07/2015 04:19 PM, Tom Rini wrote:
> ..
> > Were you in the Coverity talk too? :)
> 
> Hi Tom,
> 
> No, I was not following that talk, sorry.

Ah, coincidence then.

> ..
> >                         free(line);
> > -                       return regex_report_error(&line->regex, err, "compile",
> > +                       err = regex_report_error(&line->regex, err, "compile",
> >                                                   tok);
> > +                       return err;
> 
> I am not sure you solve the problem this way. Indeed the structure
> pointed to by the line pointer will still have been freed before use
> even this way. Who knows what the memory contains when regerror() will
> access &line->regex, which is contained into the freed structure?

Er, bah.  That's what I get for writing something in the middle of
listening to a talk too.  I meant to also move the free() to after the
regex_report_error call and just avoid adding another variable.
diff mbox

Patch

diff --git a/tools/proftool.c b/tools/proftool.c
index 9ce7a77..b3d3057 100644
--- a/tools/proftool.c
+++ b/tools/proftool.c
@@ -433,8 +433,9 @@  static int read_trace_config(FILE *fin)
                err = regcomp(&line->regex, tok, REG_NOSUB);
                if (err) {
                        free(line);
-                       return regex_report_error(&line->regex, err, "compile",
+                       err = regex_report_error(&line->regex, err, "compile",
                                                  tok);
+                       return err;
                }
 
                /* link this new one to the end of the list */