Skip to contentSkip to conversation

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

PATCH Superseded Jann Horn 2 participants 5 messages Feb 18, 2019 – Feb 20, 2019 Tracing

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 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()")

---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +5 -9
index 76217bbef815..c744b02081c3 100644
@@ -299,15 +299,11 @@ 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))
+		return PTR_ERR(path);
 	if (path[0] == '\0') {
 		ret = -EINVAL;
 		goto out;
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Mon, 18 Feb 2019 23:07:23 +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.

Thank you!

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

---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +5 -9
index 76217bbef815..c744b02081c3 100644
@@ -299,15 +299,11 @@ 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))
+		return PTR_ERR(path);
 	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
#2 lore raw
Quoted 9 lines · On Tue, 19 Feb 2019 20:45:02 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 5 lines · On Mon, 18 Feb 2019 23:07:23 +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 3 lines

This looks good to me.

BTW, I think it should note that if the string is longer than PATH_MAX. caller will receive -EINVAL instead of -E2BIG with this fix.

Thank you,

Quoted 43 lines

Thank you!

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

---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +5 -9
index 76217bbef815..c744b02081c3 100644
@@ -299,15 +299,11 @@ 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))
+		return PTR_ERR(path);
 	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]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
author
#3 lore raw
Quoted 12 lines · Masami Hiramatsu
Quoted 9 lines · On Tue, 19 Feb 2019 20:45:02 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 5 lines · On Mon, 18 Feb 2019 23:07:23 +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 6 lines
Quoted 3 lines

This looks good to me.

BTW, I think it should note that if the string is longer than PATH_MAX. caller will receive -EINVAL instead of -E2BIG with this fix.

Oh, whoops, I hadn't noticed that.

Do you want me to just add a note to the changelog, or do I have to work around this in the code?

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

---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +5 -9
index 76217bbef815..c744b02081c3 100644
@@ -299,15 +299,11 @@ 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))
+           return PTR_ERR(path);
    if (path[0] == '\0') {
            ret = -EINVAL;
            goto out;
maintainer
#4 lore raw

Hi Jann,

Quoted 17 lines · On Wed, 20 Feb 2019 16:19:10 +0100 Jann Horn <[email protected]> wrote:
Quoted 16 lines · Masami Hiramatsu
Quoted 12 lines · On Tue, 19 Feb 2019 20:45:02 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 5 lines · On Mon, 18 Feb 2019 23:07:23 +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 7 lines
Quoted 2 lines

BTW, I think it should note that if the string is longer than PATH_MAX. caller will receive -EINVAL instead of -E2BIG with this fix.

Oh, whoops, I hadn't noticed that.

Do you want me to just add a note to the changelog, or do I have to work around this in the code?

I think work around will be good if this goes to stable, since I guess this return code may go to user space.

Thank you,

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

---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
kernel/trace/trace_event_perf.c +5 -9
index 76217bbef815..c744b02081c3 100644
@@ -299,15 +299,11 @@ 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))
+           return PTR_ERR(path);
    if (path[0] == '\0') {
            ret = -EINVAL;
            goto out;
Signature · 1 line
Masami Hiramatsu <[email protected]>