linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1
@ 2018-07-10 18:27 William Cohen
  2018-07-10 22:58 ` Jiri Olsa
  0 siblings, 1 reply; 3+ messages in thread
From: William Cohen @ 2018-07-10 18:27 UTC (permalink / raw)
  To: peterz, mingo, acme, linux-kernel
  Cc: alexander.shishkin, jolsa, namhyung, William Cohen

Newer versions of GCC perform static analysis to determine whether
string truncation is possible with functions such as snprintf and
provide a warning if truncation could occur.  The make for
jvmti_agent.c uses the compiler option that treats any compiler
warnings as compiler errors.  For GCC-8.1.1 in Fedora 28 this causes
the build to fail.  The return value of the snprint is now checked to
ensure snprintf produced a NULL-terminated string.  If the string for
the path is invalid, the code does attempt to use the string.

Signed-off-by: William Cohen <wcohen@redhat.com>
---
 tools/perf/jvmti/jvmti_agent.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
index 0c6d1002b524..30f14eafe4b3 100644
--- a/tools/perf/jvmti/jvmti_agent.c
+++ b/tools/perf/jvmti/jvmti_agent.c
@@ -227,7 +227,7 @@ void *jvmti_open(void)
 {
 	char dump_path[PATH_MAX];
 	struct jitheader header;
-	int fd;
+	int retlen, fd;
 	FILE *fp;
 
 	init_arch_timestamp();
@@ -249,7 +249,10 @@ void *jvmti_open(void)
 	/*
 	 * jitdump file name
 	 */
-	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
+	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
+			  jit_path, getpid());
+	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
+		return NULL;
 
 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
 	if (fd == -1)
-- 
2.17.1


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

* Re: [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1
  2018-07-10 18:27 [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1 William Cohen
@ 2018-07-10 22:58 ` Jiri Olsa
  2018-07-11  0:10   ` William Cohen
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Olsa @ 2018-07-10 22:58 UTC (permalink / raw)
  To: William Cohen
  Cc: peterz, mingo, acme, linux-kernel, alexander.shishkin, namhyung

On Tue, Jul 10, 2018 at 02:27:16PM -0400, William Cohen wrote:
> Newer versions of GCC perform static analysis to determine whether
> string truncation is possible with functions such as snprintf and
> provide a warning if truncation could occur.  The make for
> jvmti_agent.c uses the compiler option that treats any compiler
> warnings as compiler errors.  For GCC-8.1.1 in Fedora 28 this causes
> the build to fail.  The return value of the snprint is now checked to
> ensure snprintf produced a NULL-terminated string.  If the string for
> the path is invalid, the code does attempt to use the string.

hi,
I posted fix for this recently:
  https://lore.kernel.org/lkml/20180702134202.17745-1-jolsa@kernel.org/

it also covers the perf_regs.c, which was failing with gcc8 for me

should be pulled in soon

thanks,
jirka

> 
> Signed-off-by: William Cohen <wcohen@redhat.com>
> ---
>  tools/perf/jvmti/jvmti_agent.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
> index 0c6d1002b524..30f14eafe4b3 100644
> --- a/tools/perf/jvmti/jvmti_agent.c
> +++ b/tools/perf/jvmti/jvmti_agent.c
> @@ -227,7 +227,7 @@ void *jvmti_open(void)
>  {
>  	char dump_path[PATH_MAX];
>  	struct jitheader header;
> -	int fd;
> +	int retlen, fd;
>  	FILE *fp;
>  
>  	init_arch_timestamp();
> @@ -249,7 +249,10 @@ void *jvmti_open(void)
>  	/*
>  	 * jitdump file name
>  	 */
> -	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
> +	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
> +			  jit_path, getpid());
> +	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
> +		return NULL;
>  
>  	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
>  	if (fd == -1)
> -- 
> 2.17.1
> 

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

* Re: [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1
  2018-07-10 22:58 ` Jiri Olsa
@ 2018-07-11  0:10   ` William Cohen
  0 siblings, 0 replies; 3+ messages in thread
From: William Cohen @ 2018-07-11  0:10 UTC (permalink / raw)
  To: Jiri Olsa; +Cc: peterz, mingo, acme, linux-kernel, alexander.shishkin, namhyung

On 07/10/2018 06:58 PM, Jiri Olsa wrote:
> On Tue, Jul 10, 2018 at 02:27:16PM -0400, William Cohen wrote:
>> Newer versions of GCC perform static analysis to determine whether
>> string truncation is possible with functions such as snprintf and
>> provide a warning if truncation could occur.  The make for
>> jvmti_agent.c uses the compiler option that treats any compiler
>> warnings as compiler errors.  For GCC-8.1.1 in Fedora 28 this causes
>> the build to fail.  The return value of the snprint is now checked to
>> ensure snprintf produced a NULL-terminated string.  If the string for
>> the path is invalid, the code does attempt to use the string.
> 
> hi,
> I posted fix for this recently:
>   https://lore.kernel.org/lkml/20180702134202.17745-1-jolsa@kernel.org/
> 
> it also covers the perf_regs.c, which was failing with gcc8 for me
> 
> should be pulled in soon
> 
> thanks,
> jirka

Hi Jirka,

Thanks.  I hadn't seen the patch, and this failing to build for the past week has been bugging me.  Glad to hear there is already a fix queued up.

-Will
> 
>>
>> Signed-off-by: William Cohen <wcohen@redhat.com>
>> ---
>>  tools/perf/jvmti/jvmti_agent.c | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/perf/jvmti/jvmti_agent.c b/tools/perf/jvmti/jvmti_agent.c
>> index 0c6d1002b524..30f14eafe4b3 100644
>> --- a/tools/perf/jvmti/jvmti_agent.c
>> +++ b/tools/perf/jvmti/jvmti_agent.c
>> @@ -227,7 +227,7 @@ void *jvmti_open(void)
>>  {
>>  	char dump_path[PATH_MAX];
>>  	struct jitheader header;
>> -	int fd;
>> +	int retlen, fd;
>>  	FILE *fp;
>>  
>>  	init_arch_timestamp();
>> @@ -249,7 +249,10 @@ void *jvmti_open(void)
>>  	/*
>>  	 * jitdump file name
>>  	 */
>> -	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
>> +	retlen = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump",
>> +			  jit_path, getpid());
>> +	if (retlen <= 0 || ((int) sizeof(dump_path)) <= retlen)
>> +		return NULL;
>>  
>>  	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
>>  	if (fd == -1)
>> -- 
>> 2.17.1
>>


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

end of thread, other threads:[~2018-07-11  0:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-10 18:27 [PATCH] Check jvmti_agent snprintf return value to avoid build failures with GCC-8.1.1 William Cohen
2018-07-10 22:58 ` Jiri Olsa
2018-07-11  0:10   ` William Cohen

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 NNTP newsgroup(s).