Skip to contentSkip to conversation

eal/trace: fix coverity issues

PATCH In review Sunil Kumar Kori 2 participants 6 messages Apr 27, 2020 – Apr 30, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/trace: fix coverity issues

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration parameter")
---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +2 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)
 {
 	struct trace *trace = trace_obj_get();
-	size_t len = strlen(optarg);
 	unsigned long tmp;
 	char *pattern;
+	size_t len;
 
 	if (optarg == NULL) {
 		trace_err("no optarg is passed");
 		return -EINVAL;
 	}
 
+	len = strlen(optarg);
 	if (len == 0) {
 		trace_err("value is not provided with option");
 		return -EINVAL;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 24 lines · Sunil Kumar Kori

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration parameter")
---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +1 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)
 {
        struct trace *trace = trace_obj_get();
-       size_t len = strlen(optarg);
        unsigned long tmp;
        char *pattern;
+       size_t len;

lib/librte_eal/common/eal_common_trace_utils.c int · line 242

Quoted 10 lines
lib/librte_eal/common/eal_common_trace_utils.c +1 -0
 
        if (optarg == NULL) {
                trace_err("no optarg is passed");
                return -EINVAL;
        }
 
+       len = strlen(optarg);
        if (len == 0) {
                trace_err("value is not provided with option");
                return -EINVAL;

I was looking at some gcc 10 complaints on string manipulation later in eal_trace_dir_args_save().

https://build.opensuse.org/package/live_build_log/home:dmarchan:branches:home:bluca:dpdk/dpdk/Fedora_Rawhide/x86_64

[ 126s] CC rte_malloc.o
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk-1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:
In function 'eal_trace_dir_args_save':
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk-1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:290:24:
error: 'sprintf' may write a terminating nul past the end of the
destination [-Werror=format-overflow=]

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |                        ^
[  127s] /home/abuild/rpmbuild/BUILD/dpdk-1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:290:2:
note: 'sprintf' output between 2 and 4097 bytes into a destination of
size 4096
[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[  127s] cc1: all warnings being treated as errors

Could we use asprintf in all this code and avoid malloc + sprintf ?

Something like the _untested_ snippet?

(quoted diff) +0 -0

b/lib/librte_eal/common/eal_common_trace_utils.c
index fce8892c38..4769bade97 100644

lib/librte_eal/common/eal_common_trace_utils.c +10 -12
@@ -267,8 +267,7 @@ int
 eal_trace_dir_args_save(char const *optarg)
 {
        struct trace *trace = trace_obj_get();
-       uint32_t size = sizeof(trace->dir);
-       char *dir_path = NULL;
+       char *dir_path;
        int rc;
 
        if (optarg == NULL) {
@@ -276,19 +275,18 @@ eal_trace_dir_args_save(char const *optarg)
                return -EINVAL;
        }
 
-       if (strlen(optarg) >= size) {
-               trace_err("input string is too big");
-               return -ENAMETOOLONG;
-       }
-
-       dir_path = (char *)calloc(1, size);
-       if (dir_path == NULL) {
-               trace_err("fail to allocate memory");
+       rc = asprintf(&dir_path, "%s/", optarg);
+       if (rc == -1) {
+               trace_err("failed to copy directory: %s", strerror(errno));
                return -ENOMEM;
        }
 
-       sprintf(dir_path, "%s/", optarg);
-       rc = trace_dir_update(dir_path);
+       if ((size_t)rc >= sizeof(trace->dir)) {
+               trace_err("input string is too big");
+               rc = -ENAMETOOLONG;
+       } else {
+               rc = trace_dir_update(dir_path);
+       }
 
        free(dir_path);
        return rc;
Signature · 1 line
David Marchand
#2 lore raw
Quoted 73 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 5:56 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

External Email

----------------------------------------------------------------------

Quoted 24 lines · Sunil Kumar Kori

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration parameter")
---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +1 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)  {
        struct trace *trace = trace_obj_get();
-       size_t len = strlen(optarg);
        unsigned long tmp;
        char *pattern;
+       size_t len;

lib/librte_eal/common/eal_common_trace_utils.c int · line 241

Quoted 10 lines
lib/librte_eal/common/eal_common_trace_utils.c +1 -0
 
        if (optarg == NULL) {
                trace_err("no optarg is passed");
                return -EINVAL;
        }
 
+       len = strlen(optarg);
        if (len == 0) {
                trace_err("value is not provided with option");
                return -EINVAL;

I was looking at some gcc 10 complaints on string manipulation later in eal_trace_dir_args_save().

https://urldefense.proofpoint.com/v2/url?u=https-
3A__build.opensuse.org_package_live-5Fbuild-5Flog_home-3Admarchan- 3Abranches-3Ahome-3Abluca-3Adpdk_dpdk_Fedora-5FRawhide_x86- 5F64&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=dXeXaAMkP5COgn1zxHMy aF1_d9IIuq6vHQO6NrIPjaE&m=NZ72Sr2OMEYZD7PIY59lshlAxZJJJepF5oxbHv0j 5Zg&s=yOCA3PfhZojqJv0iVKlzeqM7tYGVv0jjrnVcajUx_qA&e=

[ 126s] CC rte_malloc.o
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:
In function 'eal_trace_dir_args_save':
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:29 0:24:
error: 'sprintf' may write a terminating nul past the end of the destination [-Werror=format-overflow=]

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |                        ^
[  127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:29 0:2:
note: 'sprintf' output between 2 and 4097 bytes into a destination of size 4096

Quoted 7 lines
[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[  127s] cc1: all warnings being treated as errors

Could we use asprintf in all this code and avoid malloc + sprintf ?

As I understood from above warnings/errors, real problem is writing beyond destination i.e. dir_path. If this is the case then it can be simply handled using snprintf(); with correct "size" information. Suggested code changes are correct. I am just trying to achieve this with lesser code changes.

Also I think, fix for this should be a separate patch. Suggestions please ?

Quoted 52 lines

Something like the _untested_ snippet?

lib/librte_eal/common/eal_common_trace_utils.c +10 -13
index fce8892c38..4769bade97 100644
@@ -267,8 +267,7 @@ int
 eal_trace_dir_args_save(char const *optarg)
 {
       struct trace *trace = trace_obj_get();
-       uint32_t size = sizeof(trace->dir);
-       char *dir_path = NULL;
+       char *dir_path;
       int rc;
 
       if (optarg == NULL) {
@@ -276,19 +275,18 @@ eal_trace_dir_args_save(char const *optarg)
               return -EINVAL;
       }
 
-       if (strlen(optarg) >= size) {
-               trace_err("input string is too big");
-               return -ENAMETOOLONG;
-       }
-
-       dir_path = (char *)calloc(1, size);
-       if (dir_path == NULL) {
-               trace_err("fail to allocate memory");
+       rc = asprintf(&dir_path, "%s/", optarg);
+       if (rc == -1) {
+               trace_err("failed to copy directory: %s", strerror(errno));
               return -ENOMEM;
       }
 
-       sprintf(dir_path, "%s/", optarg);
-       rc = trace_dir_update(dir_path);
+       if ((size_t)rc >= sizeof(trace->dir)) {
+               trace_err("input string is too big");
+               rc = -ENAMETOOLONG;
+       } else {
+               rc = trace_dir_update(dir_path);
+       }
 
       free(dir_path);
       return rc;
 
 
 
--

David Marchand

#3 lore raw
Quoted 79 lines · Sunil Kumar Kori
Quoted 79 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 5:56 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

External Email

----------------------------------------------------------------------

Quoted 24 lines · Sunil Kumar Kori

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration parameter")
---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +1 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)  {
        struct trace *trace = trace_obj_get();
-       size_t len = strlen(optarg);
        unsigned long tmp;
        char *pattern;
+       size_t len;

lib/librte_eal/common/eal_common_trace_utils.c int · line 241

Quoted 10 lines
lib/librte_eal/common/eal_common_trace_utils.c +1 -0
 
        if (optarg == NULL) {
                trace_err("no optarg is passed");
                return -EINVAL;
        }
 
+       len = strlen(optarg);
        if (len == 0) {
                trace_err("value is not provided with option");
                return -EINVAL;

I was looking at some gcc 10 complaints on string manipulation later in eal_trace_dir_args_save().

https://urldefense.proofpoint.com/v2/url?u=https-
3A__build.opensuse.org_package_live-5Fbuild-5Flog_home-3Admarchan- 3Abranches-3Ahome-3Abluca-3Adpdk_dpdk_Fedora-5FRawhide_x86- 5F64&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=dXeXaAMkP5COgn1zxHMy aF1_d9IIuq6vHQO6NrIPjaE&m=NZ72Sr2OMEYZD7PIY59lshlAxZJJJepF5oxbHv0j 5Zg&s=yOCA3PfhZojqJv0iVKlzeqM7tYGVv0jjrnVcajUx_qA&e=

[ 126s] CC rte_malloc.o
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:
In function 'eal_trace_dir_args_save':
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:29 0:24:
error: 'sprintf' may write a terminating nul past the end of the destination [-Werror=format-overflow=]

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |                        ^
[  127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:29 0:2:
note: 'sprintf' output between 2 and 4097 bytes into a destination of size 4096

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[  127s] cc1: all warnings being treated as errors
Quoted 8 lines
Quoted 2 lines

Could we use asprintf in all this code and avoid malloc + sprintf ?

As I understood from above warnings/errors, real problem is writing beyond destination i.e. dir_path. If this is the case then it can be simply handled using snprintf(); with correct "size" information. Suggested code changes are correct. I am just trying to achieve this with lesser code changes.

Also I think, fix for this should be a separate patch. Suggestions please ?

If the code was not combining this malloc + sprintf + wrong checks (caught by coverity), we would avoid both issues and it would be more consistent.
No more suggestion from me.

Signature · 1 line
David Marchand
#4 lore raw
Quoted 106 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 7:22 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: Re: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

Quoted 54 lines · Sunil Kumar Kori
Quoted 54 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 5:56 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

External Email

---------------------------------------------------------------------
- On Mon, Apr 27, 2020 at 2:04 PM Sunil Kumar Kori <[email protected]>

Quoted 25 lines · wrote:

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration

parameter")

---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +1 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)  {
        struct trace *trace = trace_obj_get();
-       size_t len = strlen(optarg);
        unsigned long tmp;
        char *pattern;
+       size_t len;

lib/librte_eal/common/eal_common_trace_utils.c int · line 241

Quoted 10 lines
lib/librte_eal/common/eal_common_trace_utils.c +1 -0
 
        if (optarg == NULL) {
                trace_err("no optarg is passed");
                return -EINVAL;
        }
 
+       len = strlen(optarg);
        if (len == 0) {
                trace_err("value is not provided with option");
                return -EINVAL;

I was looking at some gcc 10 complaints on string manipulation later in eal_trace_dir_args_save().

Quoted 5 lines
Quoted 3 lines

https://urldefense.proofpoint.com/v2/url?u=https-
3A__build.opensuse.org_package_live-5Fbuild-5Flog_home-3Admarchan- 3Abranches-3Ahome-3Abluca-3Adpdk_dpdk_Fedora-5FRawhide_x86-

5F64&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=dXeXaAMkP5COgn1zxHM

y

Quoted 2 lines

aF1_d9IIuq6vHQO6NrIPjaE&m=NZ72Sr2OMEYZD7PIY59lshlAxZJJJepF5oxbHv0

j

Quoted 27 lines
Quoted 7 lines

5Zg&s=yOCA3PfhZojqJv0iVKlzeqM7tYGVv0jjrnVcajUx_qA&e=

[ 126s] CC rte_malloc.o
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:
In function 'eal_trace_dir_args_save':
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:2

Quoted 7 lines

9
0:24:
error: 'sprintf' may write a terminating nul past the end of the destination [-Werror=format-overflow=]

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |                        ^
[  127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:2

Quoted 9 lines

9
0:2:
note: 'sprintf' output between 2 and 4097 bytes into a destination of size 4096

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[  127s] cc1: all warnings being treated as errors
Quoted 3 lines
Quoted 2 lines

Could we use asprintf in all this code and avoid malloc + sprintf ?

As I understood from above warnings/errors, real problem is writing beyond

destination i.e. dir_path.

Quoted 1 line

If this is the case then it can be simply handled using snprintf(); with correct

"size" information.

Quoted 1 line

Suggested code changes are correct. I am just trying to achieve this with

Quoted 8 lines

lesser code changes.

Quoted 3 lines

Also I think, fix for this should be a separate patch. Suggestions please ?

If the code was not combining this malloc + sprintf + wrong checks (caught by coverity), we would avoid both issues and it would be more consistent. No more suggestion from me.

Agreed but It must be a separate patch because there could be other warnings too thrown by GCC 10.x which may require handling.

Quoted 4 lines

--
David Marchand

#5 lore raw
In reply to David Marchand

Hello David,

Mentioned patch (http://patches.dpdk.org/patch/69467/ ) takes care of your input to resolve GCC 10 build. Now I think, this patch is good to go. Please take care of this.

Regards
Sunil Kumar Kori

Quoted 118 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 7:22 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: Re: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

Quoted 54 lines · Sunil Kumar Kori
Quoted 54 lines

-----Original Message-----
From: David Marchand <[email protected]>
Sent: Monday, April 27, 2020 5:56 PM
To: Sunil Kumar Kori <[email protected]>
Cc: Jerin Jacob Kollanukkaran <[email protected]>; dev <[email protected]>
Subject: [EXT] Re: [dpdk-dev] [PATCH] eal/trace: fix coverity issues

External Email

---------------------------------------------------------------------
- On Mon, Apr 27, 2020 at 2:04 PM Sunil Kumar Kori <[email protected]>

Quoted 25 lines · wrote:

Pointer was being dereferenced without NULL checking.

Coverity issue: 357768

Fixes 8c8066ea6a7b ("trace: add trace mode configuration

parameter")

---
 lib/librte_eal/common/eal_common_trace_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_trace_utils.c +1 -1
index fce8892c3..119e97119 100644
@@ -227,15 +227,16 @@ int
 eal_trace_mode_args_save(const char *optarg)  {
        struct trace *trace = trace_obj_get();
-       size_t len = strlen(optarg);
        unsigned long tmp;
        char *pattern;
+       size_t len;

lib/librte_eal/common/eal_common_trace_utils.c int · line 241

Quoted 10 lines
lib/librte_eal/common/eal_common_trace_utils.c +1 -0
 
        if (optarg == NULL) {
                trace_err("no optarg is passed");
                return -EINVAL;
        }
 
+       len = strlen(optarg);
        if (len == 0) {
                trace_err("value is not provided with option");
                return -EINVAL;

I was looking at some gcc 10 complaints on string manipulation later in eal_trace_dir_args_save().

Quoted 5 lines
Quoted 3 lines

https://urldefense.proofpoint.com/v2/url?u=https-
3A__build.opensuse.org_package_live-5Fbuild-5Flog_home-3Admarchan- 3Abranches-3Ahome-3Abluca-3Adpdk_dpdk_Fedora-5FRawhide_x86-

5F64&d=DwIFaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=dXeXaAMkP5COgn1zxHM

y

Quoted 2 lines

aF1_d9IIuq6vHQO6NrIPjaE&m=NZ72Sr2OMEYZD7PIY59lshlAxZJJJepF5oxbHv0

j

Quoted 27 lines
Quoted 7 lines

5Zg&s=yOCA3PfhZojqJv0iVKlzeqM7tYGVv0jjrnVcajUx_qA&e=

[ 126s] CC rte_malloc.o
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk- 1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:
In function 'eal_trace_dir_args_save':
[ 127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:2

Quoted 7 lines

9
0:24:
error: 'sprintf' may write a terminating nul past the end of the destination [-Werror=format-overflow=]

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |                        ^
[  127s] /home/abuild/rpmbuild/BUILD/dpdk-

1587835122.b13ace300/lib/librte_eal/common/eal_common_trace_utils.c:2

Quoted 9 lines

9
0:2:
note: 'sprintf' output between 2 and 4097 bytes into a destination of size 4096

[  127s]   290 |  sprintf(dir_path, "%s/", optarg);
[  127s]       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[  127s] cc1: all warnings being treated as errors
Quoted 3 lines
Quoted 2 lines

Could we use asprintf in all this code and avoid malloc + sprintf ?

As I understood from above warnings/errors, real problem is writing beyond

destination i.e. dir_path.

Quoted 1 line

If this is the case then it can be simply handled using snprintf(); with correct

"size" information.

Quoted 1 line

Suggested code changes are correct. I am just trying to achieve this with

lesser code changes.

Quoted 3 lines

Also I think, fix for this should be a separate patch. Suggestions please ?

If the code was not combining this malloc + sprintf + wrong checks (caught by coverity), we would avoid both issues and it would be more consistent. No more suggestion from me.

--
David Marchand