linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf/core: use strndup_user() instead of buggy open-coded version
@ 2019-02-18 22:07 Jann Horn
  2019-02-19 11:45 ` Masami Hiramatsu
  0 siblings, 1 reply; 5+ messages in thread
From: Jann Horn @ 2019-02-18 22:07 UTC (permalink / raw)
  To: Ingo Molnar, Steven Rostedt, jannh
  Cc: linux-kernel, Song Liu, Masami Hiramatsu

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()")
Signed-off-by: Jann Horn <jannh@google.com>
---
compile-tested only

 kernel/trace/trace_event_perf.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
index 76217bbef815..c744b02081c3 100644
--- a/kernel/trace/trace_event_perf.c
+++ b/kernel/trace/trace_event_perf.c
@@ -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;
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf/core: use strndup_user() instead of buggy open-coded version
  2019-02-18 22:07 [PATCH] perf/core: use strndup_user() instead of buggy open-coded version Jann Horn
@ 2019-02-19 11:45 ` Masami Hiramatsu
  2019-02-19 14:51   ` Masami Hiramatsu
  0 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2019-02-19 11:45 UTC (permalink / raw)
  To: Jann Horn
  Cc: Ingo Molnar, Steven Rostedt, linux-kernel, Song Liu, Masami Hiramatsu

On Mon, 18 Feb 2019 23:07:23 +0100
Jann Horn <jannh@google.com> 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.

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Thank you!

> Fixes: 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
> compile-tested only
> 
>  kernel/trace/trace_event_perf.c | 14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
> index 76217bbef815..c744b02081c3 100644
> --- a/kernel/trace/trace_event_perf.c
> +++ b/kernel/trace/trace_event_perf.c
> @@ -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;
> -- 
> 2.21.0.rc0.258.g878e2cd30e-goog
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf/core: use strndup_user() instead of buggy open-coded version
  2019-02-19 11:45 ` Masami Hiramatsu
@ 2019-02-19 14:51   ` Masami Hiramatsu
  2019-02-20 15:19     ` Jann Horn
  0 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2019-02-19 14:51 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Jann Horn, Ingo Molnar, Steven Rostedt, linux-kernel, Song Liu

On Tue, 19 Feb 2019 20:45:02 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:

> On Mon, 18 Feb 2019 23:07:23 +0100
> Jann Horn <jannh@google.com> 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.
> 
> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

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,

> 
> Thank you!
> 
> > Fixes: 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")
> > Signed-off-by: Jann Horn <jannh@google.com>
> > ---
> > compile-tested only
> > 
> >  kernel/trace/trace_event_perf.c | 14 +++++---------
> >  1 file changed, 5 insertions(+), 9 deletions(-)
> > 
> > diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
> > index 76217bbef815..c744b02081c3 100644
> > --- a/kernel/trace/trace_event_perf.c
> > +++ b/kernel/trace/trace_event_perf.c
> > @@ -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;
> > -- 
> > 2.21.0.rc0.258.g878e2cd30e-goog
> > 
> 
> 
> -- 
> Masami Hiramatsu <mhiramat@kernel.org>


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf/core: use strndup_user() instead of buggy open-coded version
  2019-02-19 14:51   ` Masami Hiramatsu
@ 2019-02-20 15:19     ` Jann Horn
  2019-02-20 16:00       ` Masami Hiramatsu
  0 siblings, 1 reply; 5+ messages in thread
From: Jann Horn @ 2019-02-20 15:19 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: Ingo Molnar, Steven Rostedt, kernel list, Song Liu

On Tue, Feb 19, 2019 at 3:51 PM Masami Hiramatsu <mhiramat@kernel.org> wrote:
> On Tue, 19 Feb 2019 20:45:02 +0900
> Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> > On Mon, 18 Feb 2019 23:07:23 +0100
> > Jann Horn <jannh@google.com> 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.
> >
> > Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
>
> 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?

> > > Fixes: 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")
> > > Signed-off-by: Jann Horn <jannh@google.com>
> > > ---
> > > compile-tested only
> > >
> > >  kernel/trace/trace_event_perf.c | 14 +++++---------
> > >  1 file changed, 5 insertions(+), 9 deletions(-)
> > >
> > > diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
> > > index 76217bbef815..c744b02081c3 100644
> > > --- a/kernel/trace/trace_event_perf.c
> > > +++ b/kernel/trace/trace_event_perf.c
> > > @@ -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;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] perf/core: use strndup_user() instead of buggy open-coded version
  2019-02-20 15:19     ` Jann Horn
@ 2019-02-20 16:00       ` Masami Hiramatsu
  0 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu @ 2019-02-20 16:00 UTC (permalink / raw)
  To: Jann Horn; +Cc: Ingo Molnar, Steven Rostedt, kernel list, Song Liu

Hi Jann,

On Wed, 20 Feb 2019 16:19:10 +0100
Jann Horn <jannh@google.com> wrote:

> On Tue, Feb 19, 2019 at 3:51 PM Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > On Tue, 19 Feb 2019 20:45:02 +0900
> > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> >
> > > On Mon, 18 Feb 2019 23:07:23 +0100
> > > Jann Horn <jannh@google.com> 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.
> > >
> > > Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
> >
> > 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,

> 
> > > > Fixes: 0eadcc7a7bc0 ("perf/core: Fix perf_uprobe_init()")
> > > > Signed-off-by: Jann Horn <jannh@google.com>
> > > > ---
> > > > compile-tested only
> > > >
> > > >  kernel/trace/trace_event_perf.c | 14 +++++---------
> > > >  1 file changed, 5 insertions(+), 9 deletions(-)
> > > >
> > > > diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
> > > > index 76217bbef815..c744b02081c3 100644
> > > > --- a/kernel/trace/trace_event_perf.c
> > > > +++ b/kernel/trace/trace_event_perf.c
> > > > @@ -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;


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-02-20 16:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-18 22:07 [PATCH] perf/core: use strndup_user() instead of buggy open-coded version Jann Horn
2019-02-19 11:45 ` Masami Hiramatsu
2019-02-19 14:51   ` Masami Hiramatsu
2019-02-20 15:19     ` Jann Horn
2019-02-20 16:00       ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).