From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0EB2BC4363D for ; Thu, 1 Oct 2020 02:53:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B36412087D for ; Thu, 1 Oct 2020 02:53:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FhDXKehU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730338AbgJACx1 (ORCPT ); Wed, 30 Sep 2020 22:53:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59758 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725800AbgJACx1 (ORCPT ); Wed, 30 Sep 2020 22:53:27 -0400 Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601520805; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3onzANrKcElOeJRqYfauqyHXTE9OT8+U7H5RZIzMJqY=; b=FhDXKehUF5QlUBpM8IFdqNuDVtzxDZF8iiNSQl042YlemDJLD0epoglIyEknVA5CX/v6tn Dvg+pAPUCKgn86E6JVxPZATktWkPzt85QR3Kmfqmlsns5uViGsjIbyWxZsCc/8hSNYVoyT SZrKsw0NBwxXKfs9yUkwZCjsjsBtUy0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-365-sPFtqgRcMsiEurG6ebSxsQ-1; Wed, 30 Sep 2020 22:53:23 -0400 X-MC-Unique: sPFtqgRcMsiEurG6ebSxsQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 22BB61868416; Thu, 1 Oct 2020 02:53:21 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-115-71.rdu2.redhat.com [10.10.115.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 811BE5C1CF; Thu, 1 Oct 2020 02:53:19 +0000 (UTC) From: Qian Cai To: David Howells , Alexander Viro Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] pipe: Fix memory leaks in create_pipe_files() Date: Wed, 30 Sep 2020 22:52:55 -0400 Message-Id: <20201001025255.29560-1-cai@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Calling pipe2() with O_NOTIFICATION_PIPE could results in memory leaks in an error path or CONFIG_WATCH_QUEUE=n. Plug them. unreferenced object 0xc00000141114a0d8 (size 992): comm "trinity-c61", pid 1353192, jiffies 4296255779 (age 25989.560s) hex dump (first 32 bytes): 80 11 00 00 e8 03 00 00 00 00 00 00 00 00 00 00 ................ ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ backtrace: [<00000000abff13d7>] kmem_cache_alloc+0x1b4/0x470 [<000000009502e5d5>] alloc_inode+0xd0/0x130 [<00000000ca1c1a21>] new_inode_pseudo+0x1c/0x80 new_inode_pseudo at fs/inode.c:932 [<000000000c01d1d6>] create_pipe_files+0x48/0x2d0 get_pipe_inode at fs/pipe.c:874 (inlined by) create_pipe_files at fs/pipe.c:914 [<00000000d13ff4c4>] __do_pipe_flags+0x50/0x120 __do_pipe_flags at fs/pipe.c:965 [<0000000003941e42>] do_pipe2+0x3c/0x100 do_pipe2 at fs/pipe.c:1013 [<00000000a006b818>] sys_pipe2+0x1c/0x30 __se_sys_pipe2 at fs/pipe.c:1028 [<00000000a6925b55>] system_call_exception+0xf8/0x1d0 [<000000001c6b0740>] system_call_common+0xe8/0x218 unreferenced object 0xc000001f575ce600 (size 512): comm "trinity-c61", pid 1353192, jiffies 4296255779 (age 25989.560s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N.. ff ff ff ff 00 00 00 00 ff ff ff ff ff ff ff ff ................ backtrace: [<00000000d74d5e3a>] kmem_cache_alloc_trace+0x1c4/0x2d0 [<0000000061cbc9cb>] alloc_pipe_info+0x88/0x2c0 kmalloc at include/linux/slab.h:554 (inlined by) kzalloc at include/linux/slab.h:666 (inlined by) alloc_pipe_info at fs/pipe.c:793 [<00000000efd6129c>] create_pipe_files+0x6c/0x2d0 get_pipe_inode at fs/pipe.c:883 (inlined by) create_pipe_files at fs/pipe.c:914 [<00000000d13ff4c4>] __do_pipe_flags+0x50/0x120 [<0000000003941e42>] do_pipe2+0x3c/0x100 [<00000000a006b818>] sys_pipe2+0x1c/0x30 [<00000000a6925b55>] system_call_exception+0xf8/0x1d0 [<000000001c6b0740>] system_call_common+0xe8/0x218 unreferenced object 0xc000000d94f20400 (size 1024): comm "trinity-c61", pid 1353192, jiffies 4296255779 (age 25989.560s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<00000000e60ee00f>] __kmalloc+0x1e4/0x330 [<00000000130e8cc8>] alloc_pipe_info+0x154/0x2c0 kmalloc_array at include/linux/slab.h:594 (inlined by) kcalloc at include/linux/slab.h:605 (inlined by) alloc_pipe_info at fs/pipe.c:810 [<00000000efd6129c>] create_pipe_files+0x6c/0x2d0 [<00000000d13ff4c4>] __do_pipe_flags+0x50/0x120 [<0000000003941e42>] do_pipe2+0x3c/0x100 [<00000000a006b818>] sys_pipe2+0x1c/0x30 [<00000000a6925b55>] system_call_exception+0xf8/0x1d0 [<000000001c6b0740>] system_call_common+0xe8/0x218 Fixes: c73be61cede5 ("pipe: Add general notification queue support") Signed-off-by: Qian Cai --- fs/pipe.c | 11 +++++------ include/linux/watch_queue.h | 4 ++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index 60dbee457143..6fbfbb8f32e1 100644 --- 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 ret; if (!inode) return -ENFILE; if (flags & O_NOTIFICATION_PIPE) { -#ifdef CONFIG_WATCH_QUEUE - if (watch_queue_init(inode->i_pipe) < 0) { + ret = watch_queue_init(inode->i_pipe); + if (ret < 0) { + free_pipe_info(inode->i_pipe); iput(inode); - return -ENOMEM; + return ret; } -#else - return -ENOPKG; -#endif } f = alloc_file_pseudo(inode, pipe_mnt, "", diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index 5e08db2adc31..20665fbe0552 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -123,5 +123,9 @@ static inline void remove_watch_list(struct watch_list *wlist, u64 id) #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT) #endif +static inline int watch_queue_init(struct pipe_inode_info *pipe) +{ + return -ENOPKG; +} #endif /* _LINUX_WATCH_QUEUE_H */ -- 2.28.0