pipe: Fix memory leaks in create_pipe_files()

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>

authored by Qian Cai and committed by Al Viro 8a018eb5 933a3752

Changed files
+11 -6
fs
include
+5 -6
fs/pipe.c
··· 913 { 914 struct inode *inode = get_pipe_inode(); 915 struct file *f; 916 917 if (!inode) 918 return -ENFILE; 919 920 if (flags & O_NOTIFICATION_PIPE) { 921 - #ifdef CONFIG_WATCH_QUEUE 922 - if (watch_queue_init(inode->i_pipe) < 0) { 923 iput(inode); 924 - return -ENOMEM; 925 } 926 - #else 927 - return -ENOPKG; 928 - #endif 929 } 930 931 f = alloc_file_pseudo(inode, pipe_mnt, "",
··· 913 { 914 struct inode *inode = get_pipe_inode(); 915 struct file *f; 916 + int error; 917 918 if (!inode) 919 return -ENFILE; 920 921 if (flags & O_NOTIFICATION_PIPE) { 922 + error = watch_queue_init(inode->i_pipe); 923 + if (error) { 924 + free_pipe_info(inode->i_pipe); 925 iput(inode); 926 + return error; 927 } 928 } 929 930 f = alloc_file_pseudo(inode, pipe_mnt, "",
+6
include/linux/watch_queue.h
··· 122 */ 123 #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT) 124 125 #endif 126 127 #endif /* _LINUX_WATCH_QUEUE_H */
··· 122 */ 123 #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT) 124 125 + #else 126 + static inline int watch_queue_init(struct pipe_inode_info *pipe) 127 + { 128 + return -ENOPKG; 129 + } 130 + 131 #endif 132 133 #endif /* _LINUX_WATCH_QUEUE_H */