pipe: Fix memory leaks in create_pipe_files()
authorQian Cai <cai@redhat.com>
Thu, 1 Oct 2020 12:50:55 +0000 (08:50 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 1 Oct 2020 13:40:35 +0000 (09:40 -0400)
        Calling pipe2() with O_NOTIFICATION_PIPE could results in memory
leaks unless watch_queue_init() is successful.

        In case of watch_queue_init() failure in pipe2() we are left
with inode and pipe_inode_info instances that need to be freed.  That
failure exit has been introduced in commit c73be61cede5 ("pipe: Add
general notification queue support") and its handling should've been
identical to nearby treatment of alloc_file_pseudo() failures - it
is dealing with the same situation.  As it is, the mainline kernel
leaks in that case.

        Another problem is that CONFIG_WATCH_QUEUE and !CONFIG_WATCH_QUEUE
cases are treated differently (and the former leaks just pipe_inode_info,
the latter - both pipe_inode_info and inode).

        Fixed by providing a dummy wacth_queue_init() in !CONFIG_WATCH_QUEUE
case and by having failures of wacth_queue_init() handled the same way
we handle alloc_file_pseudo() ones.

Fixes: c73be61cede5 ("pipe: Add general notification queue support")
Signed-off-by: Qian Cai <cai@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/pipe.c
include/linux/watch_queue.h

index 60dbee457143674cc01651e03f213f53776aeaa1..f5d74ba1bf8c232b6316e246fe330e1998cb1068 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -913,19 +913,18 @@ int create_pipe_files(struct file **res, int flags)
 {
        struct inode *inode = get_pipe_inode();
        struct file *f;
+       int error;
 
        if (!inode)
                return -ENFILE;
 
        if (flags & O_NOTIFICATION_PIPE) {
-#ifdef CONFIG_WATCH_QUEUE
-               if (watch_queue_init(inode->i_pipe) < 0) {
+               error = watch_queue_init(inode->i_pipe);
+               if (error) {
+                       free_pipe_info(inode->i_pipe);
                        iput(inode);
-                       return -ENOMEM;
+                       return error;
                }
-#else
-               return -ENOPKG;
-#endif
        }
 
        f = alloc_file_pseudo(inode, pipe_mnt, "",
index 5e08db2adc31994c6e26264ccd67cd92ae686ff9..c994d1b2cdbaa2abb313170749d0ee396a807a90 100644 (file)
@@ -122,6 +122,12 @@ static inline void remove_watch_list(struct watch_list *wlist, u64 id)
  */
 #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT)
 
+#else
+static inline int watch_queue_init(struct pipe_inode_info *pipe)
+{
+       return -ENOPKG;
+}
+
 #endif
 
 #endif /* _LINUX_WATCH_QUEUE_H */