Skip to contentSkip to conversation

perf kmem: Fix memory leak in compact_gfp_flags()

PATCH Applied Yunfeng Ye 3 participants 7 messages Oct 16, 2019 – Oct 21, 2019 perf events

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH 1/1 perf kmem: Fix memory leak in compact_gfp_flags()

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
#1 lore raw

Em Wed, Oct 16, 2019 at 04:38:45PM +0800, Yunfeng Ye escreveu:

Quoted 2 lines

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Are you using some tool to find out these problems? Or is it just visual inspection?

- Arnaldo

Quoted 21 lines

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
Signature · 1 line
- Arnaldo
#2 lore raw

Em Wed, Oct 16, 2019 at 10:04:03AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 6 lines

Em Wed, Oct 16, 2019 at 04:38:45PM +0800, Yunfeng Ye escreveu:

Quoted 2 lines

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Are you using some tool to find out these problems? Or is it just visual inspection?

Anyway, applied after adding this to the commit log message:

Fixes 0e11115644b3 ("perf kmem: Print gfp flags in human readable string")

- Arnaldo

Quoted 27 lines

- Arnaldo

Quoted 21 lines

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
author
#3 lore raw
Quoted 8 lines · Arnaldo Carvalho de Melo

Em Wed, Oct 16, 2019 at 10:04:03AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 6 lines

Em Wed, Oct 16, 2019 at 04:38:45PM +0800, Yunfeng Ye escreveu:

Quoted 2 lines

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Are you using some tool to find out these problems? Or is it just visual inspection?

By a static code anaylsis tool which not an open source tool. thanks.

Quoted 4 lines

Anyway, applied after adding this to the commit log message:

Fixes 0e11115644b3 ("perf kmem: Print gfp flags in human readable string")

ok, thanks.

Quoted 30 lines

- Arnaldo

Quoted 27 lines

- Arnaldo

Quoted 21 lines

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
Signature · 1 line
- Arnaldo
#4 lore raw

Em Wed, Oct 16, 2019 at 09:19:54PM +0800, Yunfeng Ye escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 8 lines

Em Wed, Oct 16, 2019 at 10:04:03AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 6 lines

Em Wed, Oct 16, 2019 at 04:38:45PM +0800, Yunfeng Ye escreveu:

Quoted 2 lines

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Are you using some tool to find out these problems? Or is it just visual inspection?

By a static code anaylsis tool which not an open source tool. thanks.

Ok, so please state that next time, just for the fullest possible disclosure and for people to realize to what extent problems in the kernel and in tooling hosted in the kernel is being fixed by such tools.

I.e. you don't need to release the tool, not even give out its name, just something like:

"Found by internal static analysis tool."

Thanks,

- Arnaldo

Quoted 36 lines
Quoted 4 lines

Anyway, applied after adding this to the commit log message:

Fixes 0e11115644b3 ("perf kmem: Print gfp flags in human readable string")

ok, thanks.

Quoted 30 lines

- Arnaldo

Quoted 27 lines

- Arnaldo

Quoted 21 lines

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
author
#5 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Wed, Oct 16, 2019 at 09:19:54PM +0800, Yunfeng Ye escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 8 lines

Em Wed, Oct 16, 2019 at 10:04:03AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 6 lines

Em Wed, Oct 16, 2019 at 04:38:45PM +0800, Yunfeng Ye escreveu:

Quoted 2 lines

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Are you using some tool to find out these problems? Or is it just visual inspection?

By a static code anaylsis tool which not an open source tool. thanks.

Quoted 9 lines

Ok, so please state that next time, just for the fullest possible disclosure and for people to realize to what extent problems in the kernel and in tooling hosted in the kernel is being fixed by such tools.

I.e. you don't need to release the tool, not even give out its name, just something like:

"Found by internal static analysis tool."

ok, thanks.

Quoted 41 lines

Thanks,

- Arnaldo

Quoted 36 lines
Quoted 4 lines

Anyway, applied after adding this to the commit log message:

Fixes 0e11115644b3 ("perf kmem: Print gfp flags in human readable string")

ok, thanks.

Quoted 30 lines

- Arnaldo

Quoted 27 lines

- Arnaldo

Quoted 21 lines

Fix this by adding free(orig_flags) on the error path.

---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e353f579..9661671cc26e 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}
 
Signature · 1 line
2.7.4.3
Signature · 1 line
- Arnaldo
merge bot Applied
#6 lore raw
In reply to Yunfeng Ye
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     1abecfcaa7bba21c9985e0136fa49836164dd8fd
Gitweb:        https://git.kernel.org/tip/1abecfcaa7bba21c9985e0136fa49836164dd8fd
Author:        Yunfeng Ye <[email protected]>
AuthorDate:    Wed, 16 Oct 2019 16:38:45 +08:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Wed, 16 Oct 2019 10:08:32 -03:00

perf kmem: Fix memory leak in compact_gfp_flags()

The memory @orig_flags is allocated by strdup(), it is freed on the normal path, but leak to free on the error path.

Fix this by adding free(orig_flags) on the error path.

Fixes 0e11115644b3 ("perf kmem: Print gfp flags in human readable string")
Cc Alexander Shishkin <[email protected]>
Cc Feilong Lin <[email protected]>
Cc Hu Shiyuan <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/builtin-kmem.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/builtin-kmem.c +1 -0
index 1e61e35..9661671 100644
@@ -691,6 +691,7 @@ static char *compact_gfp_flags(char *gfp_flags)
 			new = realloc(new_flags, len + strlen(cpt) + 2);
 			if (new == NULL) {
 				free(new_flags);
+				free(orig_flags);
 				return NULL;
 			}