Skip to contentSkip to conversation

pipe_rdwr_fasync: fix the error handling to prevent the leak/crash

PATCH In review [email protected] 3 participants 3 messages Mar 4, 2009 – Mar 13, 2009 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 pipe_rdwr_fasync: fix the error handling to prevent the leak/crash

From: Oleg Nesterov <[email protected]>

If the second fasync_helper() fails, pipe_rdwr_fasync() returns the error but leaves the file on ->fasync_readers.

This was always wrong, but since 233e70f4228e78eb2f80dc6650f65d3ae3dbf17c "saner FASYNC handling on file close" we have the new problem. Because in this case setfl() doesn't set FASYNC bit, __fput() will not do ->fasync(0), and we leak fasync_struct with ->fa_file pointing to the freed file.

Cc Al Viro <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc <[email protected]> [2.6.28.x]

---

 fs/pipe.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/pipe.c renamed from fs/pipe.c~pipe_rdwr_fasync-fix-the-error-handling-to-prevent-the-leak-crash +4 -3
diff -puN fs/pipe.c~pipe_rdwr_fasync-fix-the-error-handling-to-prevent-the-leak-crash fs/pipe.c
@@ -693,11 +693,12 @@ pipe_rdwr_fasync(int fd, struct file *fi
 	int retval;
 
 	mutex_lock(&inode->i_mutex);
-
 	retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
-	if (retval >= 0)
+	if (retval >= 0) {
 		retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
-
+		if (retval < 0) /* this can happen only if on == T */
+			fasync_helper(-1, filp, 0, &pipe->fasync_readers);
+	}
 	mutex_unlock(&inode->i_mutex);
 	return retval;
 }

_

#1 lore raw
Quoted 12 lines · Andrew Morton

From: Oleg Nesterov <[email protected]>

If the second fasync_helper() fails, pipe_rdwr_fasync() returns the error but leaves the file on ->fasync_readers.

This was always wrong, but since 233e70f4228e78eb2f80dc6650f65d3ae3dbf17c "saner FASYNC handling on file close" we have the new problem. Because in this case setfl() doesn't set FASYNC bit, __fput() will not do ->fasync(0), and we leak fasync_struct with ->fa_file pointing to the freed file.

Quoted 6 lines
Cc Al Viro <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc <[email protected]> [2.6.28.x]

Just in case...

This bug is minor. fasync_helper() can only fail if kmem_cache_alloc(fasync_cache, GFP_KERNEL) fails, this should "never" happen.

Perhaps -stable doesn't need this fix.

Quoted 24 lines
 fs/pipe.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff -puN fs/pipe.c~pipe_rdwr_fasync-fix-the-error-handling-to-prevent-the-leak-crash fs/pipe.c

fs/pipe.c renamed from fs/pipe.c~pipe_rdwr_fasync-fix-the-error-handling-to-prevent-the-leak-crash +4 -3
@@ -693,11 +693,12 @@ pipe_rdwr_fasync(int fd, struct file *fi
 	int retval;
 
 	mutex_lock(&inode->i_mutex);
-
 	retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
-	if (retval >= 0)
+	if (retval >= 0) {
 		retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
-
+		if (retval < 0) /* this can happen only if on == T */
+			fasync_helper(-1, filp, 0, &pipe->fasync_readers);
+	}
 	mutex_unlock(&inode->i_mutex);
 	return retval;
 }

_

#2 lore raw
Quoted 20 lines · Oleg Nesterov
Quoted 12 lines · Andrew Morton

From: Oleg Nesterov <[email protected]>

If the second fasync_helper() fails, pipe_rdwr_fasync() returns the error but leaves the file on ->fasync_readers.

This was always wrong, but since 233e70f4228e78eb2f80dc6650f65d3ae3dbf17c "saner FASYNC handling on file close" we have the new problem. Because in this case setfl() doesn't set FASYNC bit, __fput() will not do ->fasync(0), and we leak fasync_struct with ->fa_file pointing to the freed file.

Quoted 6 lines
Cc Al Viro <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc <[email protected]> [2.6.28.x]
Quoted 6 lines

Just in case...

This bug is minor. fasync_helper() can only fail if kmem_cache_alloc(fasync_cache, GFP_KERNEL) fails, this should "never" happen.

Perhaps -stable doesn't need this fix.

Can't hurt, right?

thanks,

greg k-h