Skip to contentSkip to conversation

perf: fix event leak when perf_event_open() failed to create event_file

PATCH In review Leon Yu 3 participants 4 messages Mar 20, 2016 – Mar 31, 2016

Subsystems (2)
lore
Share & tools
lore

Leon Yu

author
#0 lore raw

PATCH 1/1 perf: fix event leak when perf_event_open() failed to create event_file

If something went wrong in anon_inode_getfile, event_file will be set to non-zero error number and able to bypass the NULL test afterward.

Consolidate the error path by testing event_file with handly IS_ERR_OR_NULL() helper since we do want to free event in both cases.

Fixes 130056275ade ("perf: Do not double free")
Cc <[email protected]> # v4.5
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -1
index 6146148..5f2e19f 100644
@@ -8617,7 +8617,7 @@ err_alloc:
 	 * If event_file is set, the fput() above will have called ->release()
 	 * and that will take care of freeing the event.
 	 */
-	if (!event_file)
+	if (IS_ERR_OR_NULL(event_file))
 		free_event(event);
 err_cpus:
 	put_online_cpus();
Signature · 1 line
2.7.1
#1 lore raw
Quoted 18 lines · Leon Yu

If something went wrong in anon_inode_getfile, event_file will be set to non-zero error number and able to bypass the NULL test afterward.

Consolidate the error path by testing event_file with handly IS_ERR_OR_NULL() helper since we do want to free event in both cases.

Fixes 130056275ade ("perf: Do not double free")
Cc <[email protected]> # v4.5
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +0 -0
index 6146148..5f2e19f 100644

kernel/events/core.c err_alloc: · line 8621

Quoted 6 lines
kernel/events/core.c +1 -1
@@ -8617,7 +8617,7 @@ err_alloc:
 	 * If event_file is set, the fput() above will have called ->release()
 	 * and that will take care of freeing the event.
 	 */
-	if (!event_file)
+	if (IS_ERR_OR_NULL(event_file))
 		free_event(event);

By this time, we have already checked for IS_ERR(event_file) once, why not just fix it up there like so:

---
From: Alexander Shishkin <[email protected]>
Date: Mon, 21 Mar 2016 09:55:09 +0200
Subject: perf: Don't leak event in the syscall error path

In the error path, event_file not being NULL is used to determine whether the event itself still needs to be free'd, so fix it up to avoid leaking.

Fixes 130056275ade ("perf: Do not double free")
---
 kernel/events/core.c | 1 +
 1 file changed, 1 insertion(+)
kernel/events/core.c +1 -0
index ab83640a50..cc34d55f1e 100644
@@ -9414,6 +9414,7 @@ SYSCALL_DEFINE5(perf_event_open,
 					f_flags);
 	if (IS_ERR(event_file)) {
 		err = PTR_ERR(event_file);
+		event_file = NULL;
 		goto err_context;
 	}
 
Signature · 1 line
2.7.0
author
#2 lore raw
Quoted 15 lines · Alexander Shishkin
Quoted 13 lines · Leon Yu

If something went wrong in anon_inode_getfile, event_file will be set to non-zero error number and able to bypass the NULL test afterward.

Consolidate the error path by testing event_file with handly IS_ERR_OR_NULL() helper since we do want to free event in both cases.

Fixes 130056275ade ("perf: Do not double free")
Cc <[email protected]> # v4.5
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

kernel/events/core.c err_alloc: · line 8621

Quoted 11 lines
kernel/events/core.c +1 -1
index 6146148..5f2e19f 100644
@@ -8617,7 +8617,7 @@ err_alloc:
       * If event_file is set, the fput() above will have called ->release()
       * and that will take care of freeing the event.
       */
-     if (!event_file)
+     if (IS_ERR_OR_NULL(event_file))
              free_event(event);

By this time, we have already checked for IS_ERR(event_file) once, why not just fix it up there like so:

My patch is more straightforward, isn't it? :p
Anyway I don't mind which one will get merged as long as the problem is fixed.

(sorry forgot to CC all recipients)

-Leon

Quoted 11 lines
kernel/events/core.c +1 -1
@@ -9414,6 +9414,7 @@ SYSCALL_DEFINE5(perf_event_open,
                                        f_flags);
        if (IS_ERR(event_file)) {
                err = PTR_ERR(event_file);
+               event_file = NULL;
                goto err_context;
        }
 
--

2.7.0

#3 lore raw

[tip:perf/core] perf/core: Don't leak event in the syscall error path

In reply to Alexander Shishkin
perf/core: Don't leak event in the syscall error path
Commit-ID:  201c2f85bd0bc13b712d9c0b3d11251b182e06ae
Gitweb:     http://git.kernel.org/tip/201c2f85bd0bc13b712d9c0b3d11251b182e06ae
Author:     Alexander Shishkin <[email protected]>
AuthorDate: Mon, 21 Mar 2016 10:02:42 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 31 Mar 2016 09:54:07 +0200

perf/core: Don't leak event in the syscall error path

In the error path, event_file not being NULL is used to determine whether the event itself still needs to be free'd, so fix it up to avoid leaking.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vince Weaver <[email protected]>
Fixes 130056275ade ("perf: Do not double free")
---
 kernel/events/core.c | 1 +
 1 file changed, 1 insertion(+)
kernel/events/core.c +1 -0
index 8c11388..52bedc5 100644
@@ -8542,6 +8542,7 @@ SYSCALL_DEFINE5(perf_event_open,
 					f_flags);
 	if (IS_ERR(event_file)) {
 		err = PTR_ERR(event_file);
+		event_file = NULL;
 		goto err_context;
 	}