diff mbox series

[v1,10/11] perf intel-pt: Smatch: Fix potential NULL pointer dereference

Message ID 20190702103420.27540-11-leo.yan@linaro.org
State New
Headers show
Series perf: Fix errors detected by Smatch | expand

Commit Message

Leo Yan July 2, 2019, 10:34 a.m. UTC
Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

  tools/perf/util/intel-pt.c:3200
  intel_pt_process_auxtrace_info() error: we previously assumed
  'session->itrace_synth_opts' could be null (see line 3196)

  tools/perf/util/intel-pt.c:3206
  intel_pt_process_auxtrace_info() warn: variable dereferenced before
  check 'session->itrace_synth_opts' (see line 3200)

tools/perf/util/intel-pt.c
3196         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
3197                 pt->synth_opts = *session->itrace_synth_opts;
3198         } else {
3199                 itrace_synth_opts__set_default(&pt->synth_opts,
3200                                 session->itrace_synth_opts->default_no_sample);
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
3201                 if (!session->itrace_synth_opts->default_no_sample &&
3202                     !session->itrace_synth_opts->inject) {
3203                         pt->synth_opts.branches = false;
3204                         pt->synth_opts.callchain = true;
3205                 }
3206                 if (session->itrace_synth_opts)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
3207                         pt->synth_opts.thread_stack =
3208                                 session->itrace_synth_opts->thread_stack;
3209         }

To dismiss the potential NULL pointer dereference, this patch validates
the pointer 'session->itrace_synth_opts' before access its elements.

Signed-off-by: Leo Yan <leo.yan@linaro.org>

---
 tools/perf/util/intel-pt.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

-- 
2.17.1

Comments

Adrian Hunter July 2, 2019, 11:07 a.m. UTC | #1
On 2/07/19 1:34 PM, Leo Yan wrote:
> Based on the following report from Smatch, fix the potential

> NULL pointer dereference check.


It never is NULL.  Remove the NULL test if you want:

-	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
+	if (session->itrace_synth_opts->set) {

But blindly making changes like below is questionable.

> 

>   tools/perf/util/intel-pt.c:3200

>   intel_pt_process_auxtrace_info() error: we previously assumed

>   'session->itrace_synth_opts' could be null (see line 3196)

> 

>   tools/perf/util/intel-pt.c:3206

>   intel_pt_process_auxtrace_info() warn: variable dereferenced before

>   check 'session->itrace_synth_opts' (see line 3200)

> 

> tools/perf/util/intel-pt.c

> 3196         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> 3197                 pt->synth_opts = *session->itrace_synth_opts;

> 3198         } else {

> 3199                 itrace_synth_opts__set_default(&pt->synth_opts,

> 3200                                 session->itrace_synth_opts->default_no_sample);

>                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^

> 3201                 if (!session->itrace_synth_opts->default_no_sample &&

> 3202                     !session->itrace_synth_opts->inject) {

> 3203                         pt->synth_opts.branches = false;

> 3204                         pt->synth_opts.callchain = true;

> 3205                 }

> 3206                 if (session->itrace_synth_opts)

>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^

> 3207                         pt->synth_opts.thread_stack =

> 3208                                 session->itrace_synth_opts->thread_stack;

> 3209         }

> 

> To dismiss the potential NULL pointer dereference, this patch validates

> the pointer 'session->itrace_synth_opts' before access its elements.

> 

> Signed-off-by: Leo Yan <leo.yan@linaro.org>

> ---

>  tools/perf/util/intel-pt.c | 5 ++---

>  1 file changed, 2 insertions(+), 3 deletions(-)

> 

> diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c

> index 550db6e77968..88b567bdf1f9 100644

> --- a/tools/perf/util/intel-pt.c

> +++ b/tools/perf/util/intel-pt.c

> @@ -3195,7 +3195,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

>  

>  	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

>  		pt->synth_opts = *session->itrace_synth_opts;

> -	} else {

> +	} else if (session->itrace_synth_opts) {

>  		itrace_synth_opts__set_default(&pt->synth_opts,

>  				session->itrace_synth_opts->default_no_sample);

>  		if (!session->itrace_synth_opts->default_no_sample &&

> @@ -3203,8 +3203,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

>  			pt->synth_opts.branches = false;

>  			pt->synth_opts.callchain = true;

>  		}

> -		if (session->itrace_synth_opts)

> -			pt->synth_opts.thread_stack =

> +		pt->synth_opts.thread_stack =

>  				session->itrace_synth_opts->thread_stack;

>  	}

>  

>
Leo Yan July 3, 2019, 1:35 a.m. UTC | #2
Hi Adrian,

On Tue, Jul 02, 2019 at 02:07:40PM +0300, Adrian Hunter wrote:
> On 2/07/19 1:34 PM, Leo Yan wrote:

> > Based on the following report from Smatch, fix the potential

> > NULL pointer dereference check.

> 

> It never is NULL.  Remove the NULL test if you want:

> 

> -	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> +	if (session->itrace_synth_opts->set) {

> 

> But blindly making changes like below is questionable.


Thanks for suggestions.

I checked report and script commands, as you said, both command will
always set session->itrace_synth_opts.  For these two commands, we can
safely remove the NULL test.

Because perf tool contains many sub commands, so I don't have much
confidence it's very safe to remove the NULL test for all cases; e.g.
there have cases which will process aux trace buffer but without
itrace options; for this case, session->itrace_synth_opts might be NULL.

For either way (remove NULL test or keep NULL test), I don't want to
introduce regression and extra efforts by my patch.  So want to double
confirm with you for this :)

Thanks,
Leo Yan

> >   tools/perf/util/intel-pt.c:3200

> >   intel_pt_process_auxtrace_info() error: we previously assumed

> >   'session->itrace_synth_opts' could be null (see line 3196)

> > 

> >   tools/perf/util/intel-pt.c:3206

> >   intel_pt_process_auxtrace_info() warn: variable dereferenced before

> >   check 'session->itrace_synth_opts' (see line 3200)

> > 

> > tools/perf/util/intel-pt.c

> > 3196         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > 3197                 pt->synth_opts = *session->itrace_synth_opts;

> > 3198         } else {

> > 3199                 itrace_synth_opts__set_default(&pt->synth_opts,

> > 3200                                 session->itrace_synth_opts->default_no_sample);

> >                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > 3201                 if (!session->itrace_synth_opts->default_no_sample &&

> > 3202                     !session->itrace_synth_opts->inject) {

> > 3203                         pt->synth_opts.branches = false;

> > 3204                         pt->synth_opts.callchain = true;

> > 3205                 }

> > 3206                 if (session->itrace_synth_opts)

> >                          ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > 3207                         pt->synth_opts.thread_stack =

> > 3208                                 session->itrace_synth_opts->thread_stack;

> > 3209         }

> > 

> > To dismiss the potential NULL pointer dereference, this patch validates

> > the pointer 'session->itrace_synth_opts' before access its elements.

> > 

> > Signed-off-by: Leo Yan <leo.yan@linaro.org>

> > ---

> >  tools/perf/util/intel-pt.c | 5 ++---

> >  1 file changed, 2 insertions(+), 3 deletions(-)

> > 

> > diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c

> > index 550db6e77968..88b567bdf1f9 100644

> > --- a/tools/perf/util/intel-pt.c

> > +++ b/tools/perf/util/intel-pt.c

> > @@ -3195,7 +3195,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> >  

> >  	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> >  		pt->synth_opts = *session->itrace_synth_opts;

> > -	} else {

> > +	} else if (session->itrace_synth_opts) {

> >  		itrace_synth_opts__set_default(&pt->synth_opts,

> >  				session->itrace_synth_opts->default_no_sample);

> >  		if (!session->itrace_synth_opts->default_no_sample &&

> > @@ -3203,8 +3203,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> >  			pt->synth_opts.branches = false;

> >  			pt->synth_opts.callchain = true;

> >  		}

> > -		if (session->itrace_synth_opts)

> > -			pt->synth_opts.thread_stack =

> > +		pt->synth_opts.thread_stack =

> >  				session->itrace_synth_opts->thread_stack;

> >  	}

> >  

> > 

>
Adrian Hunter July 3, 2019, 5:19 a.m. UTC | #3
On 3/07/19 4:35 AM, Leo Yan wrote:
> Hi Adrian,

> 

> On Tue, Jul 02, 2019 at 02:07:40PM +0300, Adrian Hunter wrote:

>> On 2/07/19 1:34 PM, Leo Yan wrote:

>>> Based on the following report from Smatch, fix the potential

>>> NULL pointer dereference check.

>>

>> It never is NULL.  Remove the NULL test if you want:

>>

>> -	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

>> +	if (session->itrace_synth_opts->set) {

>>

>> But blindly making changes like below is questionable.

> 

> Thanks for suggestions.

> 

> I checked report and script commands, as you said, both command will

> always set session->itrace_synth_opts.  For these two commands, we can

> safely remove the NULL test.

> 

> Because perf tool contains many sub commands, so I don't have much

> confidence it's very safe to remove the NULL test for all cases; e.g.

> there have cases which will process aux trace buffer but without

> itrace options; for this case, session->itrace_synth_opts might be NULL.

> 

> For either way (remove NULL test or keep NULL test), I don't want to

> introduce regression and extra efforts by my patch.  So want to double

> confirm with you for this :)


Yes, intel_pt_process_auxtrace_info() only gets called if a tool sets up the
tools->auxtrace_info() callback.  The tools that do that also set
session->itrace_synth_opts.
Leo Yan July 3, 2019, 8:16 a.m. UTC | #4
On Wed, Jul 03, 2019 at 08:19:19AM +0300, Adrian Hunter wrote:
> On 3/07/19 4:35 AM, Leo Yan wrote:

> > Hi Adrian,

> > 

> > On Tue, Jul 02, 2019 at 02:07:40PM +0300, Adrian Hunter wrote:

> >> On 2/07/19 1:34 PM, Leo Yan wrote:

> >>> Based on the following report from Smatch, fix the potential

> >>> NULL pointer dereference check.

> >>

> >> It never is NULL.  Remove the NULL test if you want:

> >>

> >> -	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> >> +	if (session->itrace_synth_opts->set) {

> >>

> >> But blindly making changes like below is questionable.

> > 

> > Thanks for suggestions.

> > 

> > I checked report and script commands, as you said, both command will

> > always set session->itrace_synth_opts.  For these two commands, we can

> > safely remove the NULL test.

> > 

> > Because perf tool contains many sub commands, so I don't have much

> > confidence it's very safe to remove the NULL test for all cases; e.g.

> > there have cases which will process aux trace buffer but without

> > itrace options; for this case, session->itrace_synth_opts might be NULL.

> > 

> > For either way (remove NULL test or keep NULL test), I don't want to

> > introduce regression and extra efforts by my patch.  So want to double

> > confirm with you for this :)

> 

> Yes, intel_pt_process_auxtrace_info() only gets called if a tool sets up the

> tools->auxtrace_info() callback.  The tools that do that also set

> session->itrace_synth_opts.


Yes.

I also checked the another case for 'perf inject', just as you said,
it sets both inject->tool.auxtrace_info and session->itrace_synth_opts
if we use 'itrace' option.  So it's safe to remove the NULL test for
session->itrace_synth_opts.

Will follow your suggestion for new patches.  Thanks a lot for
confirmation and suggestions.

Thanks,
Leo Yan
Daniel Thompson July 3, 2019, 10 a.m. UTC | #5
On Wed, Jul 03, 2019 at 09:35:54AM +0800, Leo Yan wrote:
> Hi Adrian,

> 

> On Tue, Jul 02, 2019 at 02:07:40PM +0300, Adrian Hunter wrote:

> > On 2/07/19 1:34 PM, Leo Yan wrote:

> > > Based on the following report from Smatch, fix the potential

> > > NULL pointer dereference check.

> > 

> > It never is NULL.  Remove the NULL test if you want:

> > 

> > -	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > +	if (session->itrace_synth_opts->set) {

> > 

> > But blindly making changes like below is questionable.

> 

> Thanks for suggestions.

> 

> I checked report and script commands, as you said, both command will

> always set session->itrace_synth_opts.  For these two commands, we can

> safely remove the NULL test.

> 

> Because perf tool contains many sub commands, so I don't have much

> confidence it's very safe to remove the NULL test for all cases; e.g.

> there have cases which will process aux trace buffer but without

> itrace options; for this case, session->itrace_synth_opts might be NULL.

> 

> For either way (remove NULL test or keep NULL test), I don't want to

> introduce regression and extra efforts by my patch.  So want to double

> confirm with you for this :)


Review is useful to ensure the chosen solution is correct but
unless I missed something the non-regression reasoning here is easy
easy. In its original form and despite the check, the code will
always dereference session->itrace_synth_opts, therefore removing
the check cannot makes things worse.


Daniel.


PS Of course we do also have to check that
   itrace_synth_opts__set_default() isn't a macro... but it isn't.


> > >   tools/perf/util/intel-pt.c:3200

> > >   intel_pt_process_auxtrace_info() error: we previously assumed

> > >   'session->itrace_synth_opts' could be null (see line 3196)

> > > 

> > >   tools/perf/util/intel-pt.c:3206

> > >   intel_pt_process_auxtrace_info() warn: variable dereferenced before

> > >   check 'session->itrace_synth_opts' (see line 3200)

> > > 

> > > tools/perf/util/intel-pt.c

> > > 3196         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > > 3197                 pt->synth_opts = *session->itrace_synth_opts;

> > > 3198         } else {

> > > 3199                 itrace_synth_opts__set_default(&pt->synth_opts,

> > > 3200                                 session->itrace_synth_opts->default_no_sample);

> > >                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > > 3201                 if (!session->itrace_synth_opts->default_no_sample &&

> > > 3202                     !session->itrace_synth_opts->inject) {

> > > 3203                         pt->synth_opts.branches = false;

> > > 3204                         pt->synth_opts.callchain = true;

> > > 3205                 }

> > > 3206                 if (session->itrace_synth_opts)

> > >                          ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > > 3207                         pt->synth_opts.thread_stack =

> > > 3208                                 session->itrace_synth_opts->thread_stack;

> > > 3209         }

> > > 

> > > To dismiss the potential NULL pointer dereference, this patch validates

> > > the pointer 'session->itrace_synth_opts' before access its elements.

> > > 

> > > Signed-off-by: Leo Yan <leo.yan@linaro.org>

> > > ---

> > >  tools/perf/util/intel-pt.c | 5 ++---

> > >  1 file changed, 2 insertions(+), 3 deletions(-)

> > > 

> > > diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c

> > > index 550db6e77968..88b567bdf1f9 100644

> > > --- a/tools/perf/util/intel-pt.c

> > > +++ b/tools/perf/util/intel-pt.c

> > > @@ -3195,7 +3195,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> > >  

> > >  	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > >  		pt->synth_opts = *session->itrace_synth_opts;

> > > -	} else {

> > > +	} else if (session->itrace_synth_opts) {

> > >  		itrace_synth_opts__set_default(&pt->synth_opts,

> > >  				session->itrace_synth_opts->default_no_sample);

> > >  		if (!session->itrace_synth_opts->default_no_sample &&

> > > @@ -3203,8 +3203,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> > >  			pt->synth_opts.branches = false;

> > >  			pt->synth_opts.callchain = true;

> > >  		}

> > > -		if (session->itrace_synth_opts)

> > > -			pt->synth_opts.thread_stack =

> > > +		pt->synth_opts.thread_stack =

> > >  				session->itrace_synth_opts->thread_stack;

> > >  	}

> > >  

> > > 

> >
Leo Yan July 3, 2019, 10:28 a.m. UTC | #6
On Wed, Jul 03, 2019 at 11:00:32AM +0100, Daniel Thompson wrote:
> On Wed, Jul 03, 2019 at 09:35:54AM +0800, Leo Yan wrote:

> > Hi Adrian,

> > 

> > On Tue, Jul 02, 2019 at 02:07:40PM +0300, Adrian Hunter wrote:

> > > On 2/07/19 1:34 PM, Leo Yan wrote:

> > > > Based on the following report from Smatch, fix the potential

> > > > NULL pointer dereference check.

> > > 

> > > It never is NULL.  Remove the NULL test if you want:

> > > 

> > > -	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > > +	if (session->itrace_synth_opts->set) {

> > > 

> > > But blindly making changes like below is questionable.

> > 

> > Thanks for suggestions.

> > 

> > I checked report and script commands, as you said, both command will

> > always set session->itrace_synth_opts.  For these two commands, we can

> > safely remove the NULL test.

> > 

> > Because perf tool contains many sub commands, so I don't have much

> > confidence it's very safe to remove the NULL test for all cases; e.g.

> > there have cases which will process aux trace buffer but without

> > itrace options; for this case, session->itrace_synth_opts might be NULL.

> > 

> > For either way (remove NULL test or keep NULL test), I don't want to

> > introduce regression and extra efforts by my patch.  So want to double

> > confirm with you for this :)

> 

> Review is useful to ensure the chosen solution is correct but

> unless I missed something the non-regression reasoning here is easy

> easy. In its original form and despite the check, the code will

> always dereference session->itrace_synth_opts, therefore removing

> the check cannot makes things worse.


Fair point and it's smart to connect with function
itrace_synth_opts__set_default(). :)

Thanks, Daniel.

> PS Of course we do also have to check that

>    itrace_synth_opts__set_default() isn't a macro... but it isn't.

> 

> 

> > > >   tools/perf/util/intel-pt.c:3200

> > > >   intel_pt_process_auxtrace_info() error: we previously assumed

> > > >   'session->itrace_synth_opts' could be null (see line 3196)

> > > > 

> > > >   tools/perf/util/intel-pt.c:3206

> > > >   intel_pt_process_auxtrace_info() warn: variable dereferenced before

> > > >   check 'session->itrace_synth_opts' (see line 3200)

> > > > 

> > > > tools/perf/util/intel-pt.c

> > > > 3196         if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > > > 3197                 pt->synth_opts = *session->itrace_synth_opts;

> > > > 3198         } else {

> > > > 3199                 itrace_synth_opts__set_default(&pt->synth_opts,

> > > > 3200                                 session->itrace_synth_opts->default_no_sample);

> > > >                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > > > 3201                 if (!session->itrace_synth_opts->default_no_sample &&

> > > > 3202                     !session->itrace_synth_opts->inject) {

> > > > 3203                         pt->synth_opts.branches = false;

> > > > 3204                         pt->synth_opts.callchain = true;

> > > > 3205                 }

> > > > 3206                 if (session->itrace_synth_opts)

> > > >                          ^^^^^^^^^^^^^^^^^^^^^^^^^^

> > > > 3207                         pt->synth_opts.thread_stack =

> > > > 3208                                 session->itrace_synth_opts->thread_stack;

> > > > 3209         }

> > > > 

> > > > To dismiss the potential NULL pointer dereference, this patch validates

> > > > the pointer 'session->itrace_synth_opts' before access its elements.

> > > > 

> > > > Signed-off-by: Leo Yan <leo.yan@linaro.org>

> > > > ---

> > > >  tools/perf/util/intel-pt.c | 5 ++---

> > > >  1 file changed, 2 insertions(+), 3 deletions(-)

> > > > 

> > > > diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c

> > > > index 550db6e77968..88b567bdf1f9 100644

> > > > --- a/tools/perf/util/intel-pt.c

> > > > +++ b/tools/perf/util/intel-pt.c

> > > > @@ -3195,7 +3195,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> > > >  

> > > >  	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {

> > > >  		pt->synth_opts = *session->itrace_synth_opts;

> > > > -	} else {

> > > > +	} else if (session->itrace_synth_opts) {

> > > >  		itrace_synth_opts__set_default(&pt->synth_opts,

> > > >  				session->itrace_synth_opts->default_no_sample);

> > > >  		if (!session->itrace_synth_opts->default_no_sample &&

> > > > @@ -3203,8 +3203,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,

> > > >  			pt->synth_opts.branches = false;

> > > >  			pt->synth_opts.callchain = true;

> > > >  		}

> > > > -		if (session->itrace_synth_opts)

> > > > -			pt->synth_opts.thread_stack =

> > > > +		pt->synth_opts.thread_stack =

> > > >  				session->itrace_synth_opts->thread_stack;

> > > >  	}

> > > >  

> > > > 

> > >
diff mbox series

Patch

diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index 550db6e77968..88b567bdf1f9 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3195,7 +3195,7 @@  int intel_pt_process_auxtrace_info(union perf_event *event,
 
 	if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
 		pt->synth_opts = *session->itrace_synth_opts;
-	} else {
+	} else if (session->itrace_synth_opts) {
 		itrace_synth_opts__set_default(&pt->synth_opts,
 				session->itrace_synth_opts->default_no_sample);
 		if (!session->itrace_synth_opts->default_no_sample &&
@@ -3203,8 +3203,7 @@  int intel_pt_process_auxtrace_info(union perf_event *event,
 			pt->synth_opts.branches = false;
 			pt->synth_opts.callchain = true;
 		}
-		if (session->itrace_synth_opts)
-			pt->synth_opts.thread_stack =
+		pt->synth_opts.thread_stack =
 				session->itrace_synth_opts->thread_stack;
 	}