From patchwork Mon Oct 24 15:44:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: lizetao X-Patchwork-Id: 13017687 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B42FECAAA1 for ; Mon, 24 Oct 2022 14:55:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12EA6940008; Mon, 24 Oct 2022 10:55:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DEB1940007; Mon, 24 Oct 2022 10:55:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0F1B940008; Mon, 24 Oct 2022 10:55:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E4A57940007 for ; Mon, 24 Oct 2022 10:55:43 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AF23880C2A for ; Mon, 24 Oct 2022 14:55:43 +0000 (UTC) X-FDA: 80056142166.02.6AE87C0 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf02.hostedemail.com (Postfix) with ESMTP id D12758000D for ; Mon, 24 Oct 2022 14:55:41 +0000 (UTC) Received: from kwepemi500012.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Mwyjc5gFkzJn76; Mon, 24 Oct 2022 22:52:52 +0800 (CST) Received: from huawei.com (10.67.175.21) by kwepemi500012.china.huawei.com (7.221.188.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 24 Oct 2022 22:55:37 +0800 From: Li Zetao To: , , , , CC: , , , , , Subject: [PATCH] fs/binfmt_elf: Fix memory leak in load_elf_binary() Date: Mon, 24 Oct 2022 23:44:21 +0800 Message-ID: <20221024154421.982230-1-lizetao1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.21] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500012.china.huawei.com (7.221.188.12) X-CFilter-Loop: Reflected ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666623343; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=P8qrzBiCtzgpV17F0kfQoDUcnyn67++xmKQQlbOgKNY=; b=1nzf0pH7xbLmjtkuGnb4q1Q7jL68meZYcZ9rWF3RPtRilN7oBBLwZklQ9U5f11MLNLM6wx vVZg15yAHSqCq8aggzxipSfOl3y9rDbyosXthXergQi6xg6qybKpF+CnnPBXQR5pj3iyfa vTkPwv1nxgwTGhqmEa83hq7eKpdC4EQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of lizetao1@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=lizetao1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666623343; a=rsa-sha256; cv=none; b=d2s63aZNwdYK6ip5U3mURFNxaB9THLTRp1pttIKZJbhwYsLSmz+rVSZhBvgV97W9AEd0Ft DP3SwBOo7Nb40hDAsTN0RcfJ+xEycTwsL0yb6rG02TwkxvVaOkjgDzEgdpHePUIF9+aOIv cNUaKVYDD4k53hAbbNsa6JHdL6kD3c4= X-Stat-Signature: rxhbocz1p6xxrd3118p11rxwjzripdc6 X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of lizetao1@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=lizetao1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D12758000D X-HE-Tag: 1666623341-637382 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: There is a memory leak reported by kmemleak: unreferenced object 0xffff88817104ef80 (size 224): comm "xfs_admin", pid 47165, jiffies 4298708825 (age 1333.476s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 60 a8 b3 00 81 88 ff ff a8 10 5a 00 81 88 ff ff `.........Z..... backtrace: [] __alloc_file+0x21/0x250 [] alloc_empty_file+0x41/0xf0 [] path_openat+0xea/0x3d30 [] do_filp_open+0x1b9/0x290 [] do_open_execat+0xce/0x5b0 [] open_exec+0x27/0x50 [] load_elf_binary+0x510/0x3ed0 [] bprm_execve+0x599/0x1240 [] do_execveat_common.isra.0+0x4c7/0x680 [] __x64_sys_execve+0x88/0xb0 [] do_syscall_64+0x35/0x80 If "interp_elf_ex" fails to allocate memory in load_elf_binary(), the program will take the "out_free_ph" error handing path, resulting in "interpreter" file resource is not released. Fix it by adding an error handing path "out_free_file", which will release the file resource when "interp_elf_ex" failed to allocate memory. Fixes: 0693ffebcfe5 ("fs/binfmt_elf.c: allocate less for static executable") Signed-off-by: Li Zetao Reviewed-by: Alexey Dobriyan --- fs/binfmt_elf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 63c7ebb0da89..6a11025e5850 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -911,7 +911,7 @@ static int load_elf_binary(struct linux_binprm *bprm) interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL); if (!interp_elf_ex) { retval = -ENOMEM; - goto out_free_ph; + goto out_free_file; } /* Get the exec headers */ @@ -1354,6 +1354,7 @@ static int load_elf_binary(struct linux_binprm *bprm) out_free_dentry: kfree(interp_elf_ex); kfree(interp_elf_phdata); +out_free_file: allow_write_access(interpreter); if (interpreter) fput(interpreter);