Skip to contentSkip to conversation

perf probe: check for dup and fdopen failures

PATCH In review Colin King 4 participants 4 messages Aug 12, 2016 – Aug 16, 2016 perf events

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 perf probe: check for dup and fdopen failures

From: Colin Ian King <[email protected]>

dup and fdopen can potentially fail, so add some extra error handling checks rather than assuming they always work.

---
 tools/perf/util/probe-file.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/perf/util/probe-file.c +12 -4
index 9aed9c3..f3df939 100644
@@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
 /* Get raw string list of current kprobe_events  or uprobe_events */
 struct strlist *probe_file__get_rawlist(int fd)
 {
-	int ret, idx;
+	int ret, idx, fddup;
 	FILE *fp;
 	char buf[MAX_CMDLEN];
 	char *p;
@@ -144,7 +144,12 @@ struct strlist *probe_file__get_rawlist(int fd)
 
 	sl = strlist__new(NULL, NULL);
 
-	fp = fdopen(dup(fd), "r");
+	fddup = dup(fd);
+	if (fddup < 0)
+		return NULL;
+	fp = fdopen(fddup, "r");
+	if (!fp)
+		return NULL;
 	while (!feof(fp)) {
 		p = fgets(buf, MAX_CMDLEN, fp);
 		if (!p)
@@ -447,10 +452,13 @@ static int probe_cache__load(struct probe_cache *pcache)
 {
 	struct probe_cache_entry *entry = NULL;
 	char buf[MAX_CMDLEN], *p;
-	int ret = 0;
+	int ret = 0, fddup;
 	FILE *fp;
 
-	fp = fdopen(dup(pcache->fd), "r");
+	fddup = dup(pcache->fd);
+	if (fddup < 0)
+		return -errno;
+	fp = fdopen(fddup, "r");
 	if (!fp)
 		return -EINVAL;
 
Signature · 1 line
2.8.1
#1 lore raw

Em Fri, Aug 12, 2016 at 10:44:56PM +0100, Colin King escreveu:

Quoted 24 lines

From: Colin Ian King <[email protected]>

dup and fdopen can potentially fail, so add some extra error handling checks rather than assuming they always work.

---
 tools/perf/util/probe-file.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/perf/util/probe-file.c +1 -1
index 9aed9c3..f3df939 100644
@@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
 /* Get raw string list of current kprobe_events  or uprobe_events */
 struct strlist *probe_file__get_rawlist(int fd)
 {
-	int ret, idx;
+	int ret, idx, fddup;
 	FILE *fp;
 	char buf[MAX_CMDLEN];
 	char *p;

tools/perf/util/probe-file.c struct strlist *probe_file__get_rawlist(int fd) · line 151

Quoted 9 lines
tools/perf/util/probe-file.c +5 -1
@@ -144,7 +144,12 @@ struct strlist *probe_file__get_rawlist(int fd)
 
 	sl = strlist__new(NULL, NULL);
 
-	fp = fdopen(dup(fd), "r");
+	fddup = dup(fd);
+	if (fddup < 0)
+		return NULL;
+	fp = fdopen(fddup, "r");
+	if (!fp)

here we leak fddup, no?

tools/perf/util/probe-file.c struct strlist *probe_file__get_rawlist(int fd) · line 152

Quoted 1 line
tools/perf/util/probe-file.c +1 -0
+		return NULL;

I.e. this should be:

	if (!fp) {
		close(fddup);
		return NULL;
	}

Ack?

- Arnaldo

Quoted 21 lines
tools/perf/util/probe-file.c +5 -2
 	while (!feof(fp)) {
 		p = fgets(buf, MAX_CMDLEN, fp);
 		if (!p)
@@ -447,10 +452,13 @@ static int probe_cache__load(struct probe_cache *pcache)
 {
 	struct probe_cache_entry *entry = NULL;
 	char buf[MAX_CMDLEN], *p;
-	int ret = 0;
+	int ret = 0, fddup;
 	FILE *fp;
 
-	fp = fdopen(dup(pcache->fd), "r");
+	fddup = dup(pcache->fd);
+	if (fddup < 0)
+		return -errno;
+	fp = fdopen(fddup, "r");
 	if (!fp)
 		return -EINVAL;
 
Signature · 1 line
2.8.1
#2 lore raw
Quoted 5 lines · On Fri, 12 Aug 2016 18:55:17 -0300 Arnaldo Carvalho de Melo <[email protected]>…

Em Fri, Aug 12, 2016 at 10:44:56PM +0100, Colin King escreveu:

Quoted 4 lines

From: Colin Ian King <[email protected]>

dup and fdopen can potentially fail, so add some extra error handling checks rather than assuming they always work.

Thanks, I forgot to handle that.

Quoted 36 lines
Quoted 20 lines
---
 tools/perf/util/probe-file.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/perf/util/probe-file.c +1 -1
index 9aed9c3..f3df939 100644
@@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
 /* Get raw string list of current kprobe_events  or uprobe_events */
 struct strlist *probe_file__get_rawlist(int fd)
 {
-	int ret, idx;
+	int ret, idx, fddup;
 	FILE *fp;
 	char buf[MAX_CMDLEN];
 	char *p;

tools/perf/util/probe-file.c struct strlist *probe_file__get_rawlist(int fd) · line 151

Quoted 9 lines
tools/perf/util/probe-file.c +5 -1
@@ -144,7 +144,12 @@ struct strlist *probe_file__get_rawlist(int fd)
 
 	sl = strlist__new(NULL, NULL);
 
-	fp = fdopen(dup(fd), "r");
+	fddup = dup(fd);
+	if (fddup < 0)
+		return NULL;
+	fp = fdopen(fddup, "r");
+	if (!fp)

here we leak fddup, no?

tools/perf/util/probe-file.c struct strlist *probe_file__get_rawlist(int fd) · line 152

Quoted 1 line
tools/perf/util/probe-file.c +1 -0
+		return NULL;

I.e. this should be:

Quoted 6 lines
	if (!fp) {
		close(fddup);
		return NULL;
	}

Ack?

Correct, it should be closed. So both fixes look good to me.

Thanks!

Quoted 24 lines

- Arnaldo

Quoted 21 lines
tools/perf/util/probe-file.c +5 -2
 	while (!feof(fp)) {
 		p = fgets(buf, MAX_CMDLEN, fp);
 		if (!p)
@@ -447,10 +452,13 @@ static int probe_cache__load(struct probe_cache *pcache)
 {
 	struct probe_cache_entry *entry = NULL;
 	char buf[MAX_CMDLEN], *p;
-	int ret = 0;
+	int ret = 0, fddup;
 	FILE *fp;
 
-	fp = fdopen(dup(pcache->fd), "r");
+	fddup = dup(pcache->fd);
+	if (fddup < 0)
+		return -errno;
+	fp = fdopen(fddup, "r");
 	if (!fp)
 		return -EINVAL;
 
Signature · 1 line
2.8.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#3 lore raw

[tip:perf/urgent] perf probe: Check for dup and fdopen failures

In reply to Colin King
perf probe: Check for dup and fdopen failures
Commit-ID:  0325862dc364d8af524bf2db53ef4360ed55b989
Gitweb:     http://git.kernel.org/tip/0325862dc364d8af524bf2db53ef4360ed55b989
Author:     Colin Ian King <[email protected]>
AuthorDate: Fri, 12 Aug 2016 22:44:56 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 15 Aug 2016 17:06:19 -0300

perf probe: Check for dup and fdopen failures

dup and fdopen can potentially fail, so add some extra error handling checks rather than assuming they always work.

Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Wang Nan <[email protected]>
[ Free resources when those functions (now being verified) fail ]
---
 tools/perf/util/probe-file.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
tools/perf/util/probe-file.c +20 -4
index 9aed9c3..a8e7623 100644
@@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
 /* Get raw string list of current kprobe_events  or uprobe_events */
 struct strlist *probe_file__get_rawlist(int fd)
 {
-	int ret, idx;
+	int ret, idx, fddup;
 	FILE *fp;
 	char buf[MAX_CMDLEN];
 	char *p;
@@ -144,7 +144,14 @@ struct strlist *probe_file__get_rawlist(int fd)
 
 	sl = strlist__new(NULL, NULL);
 
-	fp = fdopen(dup(fd), "r");
+	fddup = dup(fd);
+	if (fddup < 0)
+		goto out_free_sl;
+
+	fp = fdopen(fddup, "r");
+	if (!fp)
+		goto out_close_fddup;
+
 	while (!feof(fp)) {
 		p = fgets(buf, MAX_CMDLEN, fp);
 		if (!p)
@@ -163,6 +170,12 @@ struct strlist *probe_file__get_rawlist(int fd)
 	fclose(fp);
 
 	return sl;
+
+out_close_fddup:
+	close(fddup);
+out_free_sl:
+	strlist__delete(sl);
+	return NULL;
 }
 
 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
@@ -447,10 +460,13 @@ static int probe_cache__load(struct probe_cache *pcache)
 {
 	struct probe_cache_entry *entry = NULL;
 	char buf[MAX_CMDLEN], *p;
-	int ret = 0;
+	int ret = 0, fddup;
 	FILE *fp;
 
-	fp = fdopen(dup(pcache->fd), "r");
+	fddup = dup(pcache->fd);
+	if (fddup < 0)
+		return -errno;
+	fp = fdopen(fddup, "r");
 	if (!fp)
 		return -EINVAL;