Skip to contentSkip to conversation

perf record: Fix failed memory allocation for get_cpuid_str

PATCH Applied Thomas Richter 3 participants 3 messages Jan 17, 2018 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf record: Fix failed memory allocation for get_cpuid_str

In x86 architecture dependend part function get_cpuid_str() mallocs a 128 byte buffer, but does not check if the memory allocation succeeded or not. When the memory allocation fails, function __get_cpuid() is called with first parameter being a NULL pointer. However this function references its first parameter and operates on a NULL pointer which might cause core dumps.

---
 tools/perf/arch/x86/util/header.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/arch/x86/util/header.c +1 -1
index 33027c5e6f92..c6b5204e0280 100644
@@ -70,7 +70,7 @@ get_cpuid_str(void)
 {
 	char *buf = malloc(128);
 
-	if (__get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
+	if (buf && __get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
 		free(buf);
 		return NULL;
 	}
Signature · 1 line
2.13.4
maintainer Applied
#1 lore raw

Em Wed, Jan 17, 2018 at 02:16:11PM +0100, Thomas Richter escreveu:

Quoted 8 lines

In x86 architecture dependend part function get_cpuid_str() mallocs a 128 byte buffer, but does not check if the memory allocation succeeded or not. When the memory allocation fails, function __get_cpuid() is called with first parameter being a NULL pointer. However this function references its first parameter and operates on a NULL pointer which might cause core dumps.

Thanks, applied.

- Arnaldo

Quoted 20 lines
---
 tools/perf/arch/x86/util/header.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/arch/x86/util/header.c +1 -1
index 33027c5e6f92..c6b5204e0280 100644
@@ -70,7 +70,7 @@ get_cpuid_str(void)
 {
 	char *buf = malloc(128);
 
-	if (__get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
+	if (buf && __get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
 		free(buf);
 		return NULL;
 	}
Signature · 1 line
2.13.4
#2 lore raw
In reply to Thomas Richter
perf record: Fix failed memory allocation for get_cpuid_str
Commit-ID:  81fccd6ca507d3b2012eaf1edeb9b1dbf4bd22db
Gitweb:     https://git.kernel.org/tip/81fccd6ca507d3b2012eaf1edeb9b1dbf4bd22db
Author:     Thomas Richter <[email protected]>
AuthorDate: Wed, 17 Jan 2018 14:16:11 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 17 Jan 2018 10:31:25 -0300

perf record: Fix failed memory allocation for get_cpuid_str

In x86 architecture dependend part function get_cpuid_str() mallocs a 128 byte buffer, but does not check if the memory allocation succeeded or not.

When the memory allocation fails, function __get_cpuid() is called with first parameter being a NULL pointer. However this function references its first parameter and operates on a NULL pointer which might cause core dumps.

---
 tools/perf/arch/x86/util/header.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/arch/x86/util/header.c +1 -1
index b626d2b..fb0d71a 100644
@@ -70,7 +70,7 @@ get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
 {
 	char *buf = malloc(128);
 
-	if (__get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
+	if (buf && __get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
 		free(buf);
 		return NULL;
 	}