Skip to contentSkip to conversation

tools/perf/util: null-terminate version char array upon error

PATCH Superseded Donald Yandt 3 participants 3 messages May 14, 2019 – May 18, 2019 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/perf/util: null-terminate version char array upon error

If fgets fails due to any other error besides end-of-file, the version char array may not even be null-terminated.

---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
index 3c520baa1..28a9541c4 100644
@@ -1234,8 +1234,9 @@ static char *get_kernel_version(const char *root_dir)
 	if (!file)
 		return NULL;
 
-	version[0] = '\0';
 	tmp = fgets(version, sizeof(version), file);
+	if (!tmp)
+		*version = '\0';
 	fclose(file);
 
 	name = strstr(version, prefix);

--
2.20.1

#1 lore raw

Em Tue, May 14, 2019 at 07:01:00AM -0400, Donald Yandt escreveu:

Quoted 1 line

If fgets fails due to any other error besides end-of-file, the version char array may not even be null-terminated.

Thanks, but out of curiosity, was this found just by visual inspection? Some static analysis tool? An actual problem you stumbled when processing some /proc/version in a custom kernel?

Also please consider adding a:

Fixes a1645ce12adb ("perf: 'perf kvm' tool for monitoring guest performance from host")

So that we can get this picked up by the stable kernel trees. I'm adding it now.

Thanks!

- Arnaldo

Quoted 22 lines
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -2
index 3c520baa1..28a9541c4 100644
@@ -1234,8 +1234,9 @@ static char *get_kernel_version(const char *root_dir)
 	if (!file)
 		return NULL;
 
-	version[0] = '\0';
 	tmp = fgets(version, sizeof(version), file);
+	if (!tmp)
+		*version = '\0';
 	fclose(file);
 
 	name = strstr(version, prefix);
--

2.20.1

Signature · 1 line
- Arnaldo
#2 lore raw

[tip:perf/core] perf machine: Null-terminate version char array upon fgets(/proc/version) error

In reply to Donald Yandt
perf machine: Null-terminate version char array upon fgets(/proc/version) error
Commit-ID:  30ba5b0e66c872faa416a458d32cc3956ecb548a
Gitweb:     https://git.kernel.org/tip/30ba5b0e66c872faa416a458d32cc3956ecb548a
Author:     Donald Yandt <[email protected]>
AuthorDate: Tue, 14 May 2019 07:01:00 -0400
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 15 May 2019 16:36:47 -0300

perf machine: Null-terminate version char array upon fgets(/proc/version) error

If fgets() fails due to any other error besides end-of-file, the version char array may not even be null-terminated.

Cc Alexander Shishkin <[email protected]>
Cc Avi Kivity <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Yanmin Zhang <[email protected]>
Fixes a1645ce12adb ("perf: 'perf kvm' tool for monitoring guest performance from host")
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
index 3c520baa198c..28a9541c4835 100644
@@ -1234,8 +1234,9 @@ static char *get_kernel_version(const char *root_dir)
 	if (!file)
 		return NULL;
 
-	version[0] = '\0';
 	tmp = fgets(version, sizeof(version), file);
+	if (!tmp)
+		*version = '\0';
 	fclose(file);
 
 	name = strstr(version, prefix);