Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp950005pxb; Tue, 3 Nov 2020 17:41:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJzhUIqRTXjfGNeD7upS3dHovyXVQdEF+unW3HYJZMtzIVtMPNaIOBjsLZnAcsCY7JpMWbNu X-Received: by 2002:aa7:df81:: with SMTP id b1mr16951913edy.365.1604454109268; Tue, 03 Nov 2020 17:41:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604454109; cv=none; d=google.com; s=arc-20160816; b=WeDRB+CSxnPpeuaJ0qC1twA6eT/ZFVmt/8meir1StAseu7ObLEj1Y8oi9Y05xM584j Refxv2C25E6JJ8dQH5mP9nTEZpZm0e6n9OKFJvpYMCCq3NSqKI7pI8p79UPwF92Hjg+2 /LQ/BXlczEXzs4MDfspWSVDZ5CWiTL813soSqMzf+fBsPe//I5sAodVJdASpj/CHuweh VFVP1I5yXpINgEDcmSUwwvpbISFiApNoi43Qi/bd+ytUmbBxin576FW1mfS7hgovXZ26 JKnQV8+x0+/ljIzNxPKtsZAjLy40x75MCg1img9U2AaTE+XwGcs4inCWvH8ObltrsgWZ GBOA== 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 :message-id:date:subject:cc:to:from; bh=GrhjcRC/a5iQ/2U6uWzWAZUgyI0niBElG2svmAv/tfU=; b=S0OlYvG8RyOmiMsG7+Hf0ZREK5oRERZD21HyATACpp1pkPzPNnJAFXqCPSTEUjKQSH aPbTTemfEVBH512i/KvvIoUYmgFHtF0weGMY2PbVH6bsyrEnhU+w9PS1CYa6WiUOYKH4 n/KZ+sXyEvp/izQCmC6ph/ShxfRF+HxoeHEUh7uIlkXxCgjoUCFPoweM8d44bMius/IH qQquVcFmbcXw67YJrL0MVPnhbpPppBtR29SRo15ZftaN16fHXfRtheZzpC3RLlkvbxnF AsJpXg7Fsop/gBuBezyHgmOOuZGLgeBqAudJsatxcMoqnfI7paefu1t/1Ja+EX4YCDbs hsMA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g20si298860ejm.505.2020.11.03.17.41.26; Tue, 03 Nov 2020 17:41:49 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730431AbgKDBj6 (ORCPT + 99 others); Tue, 3 Nov 2020 20:39:58 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:7044 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728567AbgKDBj5 (ORCPT ); Tue, 3 Nov 2020 20:39:57 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4CQq8R6N6pzhgNS; Wed, 4 Nov 2020 09:39:51 +0800 (CST) Received: from huawei.com (10.175.113.32) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.487.0; Wed, 4 Nov 2020 09:39:44 +0800 From: Liu Shixin To: Alexander Viro CC: , , "Liu Shixin" Subject: [PATCH] fs/binfmt_elf: free interpreter in load_elf_binary Date: Wed, 4 Nov 2020 10:06:35 +0800 Message-ID: <20201104020635.1906023-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.32] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The file interpreter is allocated in load_elf_binary, but not freed in the case interp_elf_ex is NULL. We add a new mark out_free_file for this case to free interpreter. This memory leak is catched when kmemleak is enabled in kernel, the report looks like below: unreferenced object 0xffff8b6e9fd41400 (size 488): comm "service", pid 4095, jiffies 4300970844 (age 49.618s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ e0 08 be b9 6e 8b ff ff 00 13 04 b7 6e 8b ff ff ....n.......n... backtrace: [<00000000eacadaa2>] kmem_cache_alloc+0x164/0x320 [<0000000090fb7bf2>] __alloc_file+0x2a/0x140 [<00000000ff8fab86>] alloc_empty_file+0x4b/0x100 [<000000003ab9b00d>] path_openat+0x4a/0xe20 [<0000000027e3a067>] do_filp_open+0xb9/0x150 [<000000000edebcac>] do_open_execat+0xa6/0x250 [<000000008845564e>] open_exec+0x31/0x60 [<00000000e6e6e1ca>] load_elf_binary+0x1dd/0x1b60 [<000000004515d8f0>] do_execveat_common.isra.39+0xaa0/0x1000 [<000000002ca5e83f>] __x64_sys_execve+0x37/0x40 [<00000000beb519e4>] do_syscall_64+0x56/0xa0 [<000000009cf54d51>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Signed-off-by: Liu Shixin --- 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 fa50e8936f5f..e223d798e5d8 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -907,7 +907,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 */ @@ -1316,6 +1316,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); -- 2.25.1