Skip to contentSkip to conversation

perf/core: use strndup_user() instead of buggy open-coded version

PATCH In review Jann Horn 4 participants 4 messages Feb 20, 2019 – Feb 21, 2019 Tracing

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH v2 1/1 perf/core: use strndup_user() instead of buggy open-coded version

The first version of this method was missing the check for ret == PATH_MAX; then such a check was added, but it didn't call kfree() on error, so there was still a small memory leak in the error case. Fix it by using strndup_user() instead of open-coding it.

Fixes 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")

---
v2:
- be compatible with existing error codes (Masami Hiramatsu)

 kernel/trace/trace_event_perf.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +7 -9
index 76217bbef815..4629a6104474 100644
@@ -299,15 +299,13 @@ int perf_uprobe_init(struct perf_event *p_event,
 
 	if (!p_event->attr.uprobe_path)
 		return -EINVAL;
-	path = kzalloc(PATH_MAX, GFP_KERNEL);
-	if (!path)
-		return -ENOMEM;
-	ret = strncpy_from_user(
-		path, u64_to_user_ptr(p_event->attr.uprobe_path), PATH_MAX);
-	if (ret == PATH_MAX)
-		return -E2BIG;
-	if (ret < 0)
-		goto out;
+
+	path = strndup_user(u64_to_user_ptr(p_event->attr.uprobe_path),
+			    PATH_MAX);
+	if (IS_ERR(path)) {
+		ret = PTR_ERR(path);
+		return (ret == -EINVAL) ? -E2BIG : ret;
+	}
 	if (path[0] == '\0') {
 		ret = -EINVAL;
 		goto out;
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
maintainer Reviewed-by on v2 1/1
#1 lore raw

Hi Jann,

Quoted 5 lines · On Wed, 20 Feb 2019 17:54:43 +0100 Jann Horn <[email protected]> wrote:

The first version of this method was missing the check for ret == PATH_MAX; then such a check was added, but it didn't call kfree() on error, so there was still a small memory leak in the error case. Fix it by using strndup_user() instead of open-coding it.

This looks good to me.

BTW, for stable, this is good. For the long term, I think we should fix strndup_user() to return -E2BUG when the user string is longer than max.

Thank you,

Quoted 39 lines
Fixes 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")

---
v2:
- be compatible with existing error codes (Masami Hiramatsu)

 kernel/trace/trace_event_perf.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +7 -9
index 76217bbef815..4629a6104474 100644
@@ -299,15 +299,13 @@ int perf_uprobe_init(struct perf_event *p_event,
 
 	if (!p_event->attr.uprobe_path)
 		return -EINVAL;
-	path = kzalloc(PATH_MAX, GFP_KERNEL);
-	if (!path)
-		return -ENOMEM;
-	ret = strncpy_from_user(
-		path, u64_to_user_ptr(p_event->attr.uprobe_path), PATH_MAX);
-	if (ret == PATH_MAX)
-		return -E2BIG;
-	if (ret < 0)
-		goto out;
+
+	path = strndup_user(u64_to_user_ptr(p_event->attr.uprobe_path),
+			    PATH_MAX);
+	if (IS_ERR(path)) {
+		ret = PTR_ERR(path);
+		return (ret == -EINVAL) ? -E2BIG : ret;
+	}
 	if (path[0] == '\0') {
 		ret = -EINVAL;
 		goto out;
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Signature · 1 line
Masami Hiramatsu <[email protected]>
Acked-by
#2 lore raw
Quoted 17 lines · Masami Hiramatsu

Hi Jann,

Quoted 5 lines · On Wed, 20 Feb 2019 17:54:43 +0100 Jann Horn <[email protected]> wrote:

The first version of this method was missing the check for ret == PATH_MAX; then such a check was added, but it didn't call kfree() on error, so there was still a small memory leak in the error case. Fix it by using strndup_user() instead of open-coding it.

This looks good to me.

Quoted 10 lines

BTW, for stable, this is good. For the long term, I think we should fix strndup_user() to return -E2BUG when the user string is longer than max.

Thank you,

Quoted 2 lines
Fixes 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")

Thanks for the fix!

Quoted 41 lines
Quoted 37 lines

---
v2:
- be compatible with existing error codes (Masami Hiramatsu)

kernel/trace/trace_event_perf.c | 16 +++++++---------

1 file changed, 7 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +7 -9
index 76217bbef815..4629a6104474 100644
@@ -299,15 +299,13 @@ int perf_uprobe_init(struct perf_event *p_event,
 
 	if (!p_event->attr.uprobe_path)
 		return -EINVAL;
-	path = kzalloc(PATH_MAX, GFP_KERNEL);
-	if (!path)
-		return -ENOMEM;
-	ret = strncpy_from_user(
-		path, u64_to_user_ptr(p_event->attr.uprobe_path), PATH_MAX);
-	if (ret == PATH_MAX)
-		return -E2BIG;
-	if (ret < 0)
-		goto out;
+
+	path = strndup_user(u64_to_user_ptr(p_event->attr.uprobe_path),
+			    PATH_MAX);
+	if (IS_ERR(path)) {
+		ret = PTR_ERR(path);
+		return (ret == -EINVAL) ? -E2BIG : ret;
+	}
 	if (path[0] == '\0') {
 		ret = -EINVAL;
 		goto out;
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#3 lore raw
Quoted 13 lines · On Thu, 21 Feb 2019 07:12:10 +0000 Song Liu <[email protected]> wrote:
Quoted 13 lines · Masami Hiramatsu

Hi Jann,

Quoted 5 lines · On Wed, 20 Feb 2019 17:54:43 +0100 Jann Horn <[email protected]> wrote:

The first version of this method was missing the check for ret == PATH_MAX; then such a check was added, but it didn't call kfree() on error, so there was still a small memory leak in the error case. Fix it by using strndup_user() instead of open-coding it.

Quoted 7 lines
Quoted 7 lines

This looks good to me.

BTW, for stable, this is good. For the long term, I think we should fix strndup_user() to return -E2BUG when the user string is longer than max.

OK, I'll add a stable tag (but it wont go until the merge window).

It also shouldn't be called "perf/core" as its in the tracing directory, and thus "tracing/perf" would be more appropriate. But I see that's what the patch it fixes calls it too :-/

I'll pull it in.

Thanks everyone!

-- Steve

Quoted 9 lines
Quoted 5 lines

Thank you,

Quoted 2 lines
Fixes 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")

Thanks for the fix!