Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4360807pxu; Mon, 12 Oct 2020 17:33:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZxOmcHSyjTr3x42QkqVzWs6TuBQ2UEhYQiYWvbc8QcLiCdMkk+R7bpdj83bfs56PVkE/Y X-Received: by 2002:aa7:d349:: with SMTP id m9mr16981891edr.51.1602549221361; Mon, 12 Oct 2020 17:33:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602549221; cv=none; d=google.com; s=arc-20160816; b=XO1OmW1YPGfKWLzeNqNTo7TtvgcIJYEemlFvpecSgoPpMhwz6EOQKfPNT82XjFZYwd LG5kIHxtXwA9BJcD8+658eliXd3uvgXts8YtMZ9hXpc5wHr1+JKtU8hGYnerAunHj3z4 IN46lo/KTl6m81cfKMrfU+7elU9lO20PiC4lRc9eB8ZJWKNN/kF1GScM24wSxwaOJtSM xHjaaIyLGNRomoFV+/oKQubgNtkz+wcCLnZlDwyR7t/m6YNvcNQQ8uJy4ikVWRspH/fU D1wA1GadmFa3wZse8jMSiUBxSPN2cW2GzR48n9hIYQc0hFN5ero8u2pQYnkZEurykoyX RzQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bCxj5U57rMYC5+gI6LKTPC5WmowndXoq0S5n4LamBGc=; b=ow93GDQc6iVOlnRyMApr7sW03G6fEp5i6zQQS17gLBmPD+7zAIwJQq7i6BCZT8qmu1 DzqqDZbhUNJH2+5vp4kdSsFBNYf2LGWDCzwPtrbphwLixFeHHipg4CBsyYPRQN80rrLE S6gs0VQeXv6yZhxRPl2iAy/AYrLHCdaXF2ufcmCI8ngPiCCpJvooNCIE06EN+MhbBJtS PV7RYBUIX3pGaZwlkjeFZTKiYueoRzL3J7XPficfxqWrlPYK9iHILWgtJYWDhaQM4HGK 8sF1PlcJm8n3XcUnCYJE/pCggxGm7LhPIAB0Tq/ZQ4XBn7pu9iEJiXSUMI7i8rRtwNwW AoAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Dx0W2/0x"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o11si15585952ejx.443.2020.10.12.17.33.17; Mon, 12 Oct 2020 17:33:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Dx0W2/0x"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390228AbgJLNwa (ORCPT + 99 others); Mon, 12 Oct 2020 09:52:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:55730 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731830AbgJLNsM (ORCPT ); Mon, 12 Oct 2020 09:48:12 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 61B16206D9; Mon, 12 Oct 2020 13:46:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602510417; bh=umLZUFZadFKyR9l6EetllRon0TRqQ8w0P/ZClO1UZTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dx0W2/0xTKMMpy8AXh3ZJACj4OUdBfO5fqVr4Bv4dtzR0sNzcqyTe+34lyeS1YLUC V90BuhKIV0mFxPiOcfIttqJaXUAUrWr6GO2JHF1mNEzCXPI8TiCSVVvzHdA0moG64J UDpIN5Mq2OxfYKpcSWGz0MUt0JPGy1Zi4IrYSqW0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qian Cai , Al Viro , Sasha Levin Subject: [PATCH 5.8 083/124] pipe: Fix memory leaks in create_pipe_files() Date: Mon, 12 Oct 2020 15:31:27 +0200 Message-Id: <20201012133150.881895405@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201012133146.834528783@linuxfoundation.org> References: <20201012133146.834528783@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qian Cai [ Upstream commit 8a018eb55e3ac033592afbcb476b0ffe64465b12 ] 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 Signed-off-by: Al Viro Signed-off-by: Sasha Levin --- fs/pipe.c | 11 +++++------ include/linux/watch_queue.h | 6 ++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index 117db82b10af5..0ac197658a2d6 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -894,19 +894,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, "", diff --git a/include/linux/watch_queue.h b/include/linux/watch_queue.h index 5e08db2adc319..c994d1b2cdbaa 100644 --- a/include/linux/watch_queue.h +++ b/include/linux/watch_queue.h @@ -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 */ -- 2.25.1