From patchwork Wed Feb 23 11:32:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 12756768 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 99B80C433F5 for ; Wed, 23 Feb 2022 11:32:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D3598D0005; Wed, 23 Feb 2022 06:32:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 084B08D0001; Wed, 23 Feb 2022 06:32:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E64718D0005; Wed, 23 Feb 2022 06:32:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0181.hostedemail.com [216.40.44.181]) by kanga.kvack.org (Postfix) with ESMTP id D91C08D0001 for ; Wed, 23 Feb 2022 06:32:56 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 939939F30E for ; Wed, 23 Feb 2022 11:32:56 +0000 (UTC) X-FDA: 79173832752.15.EC7DAC4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 27B0680012 for ; Wed, 23 Feb 2022 11:32:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645615975; 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: in-reply-to:in-reply-to:references:references; bh=8J0LUKv+K2TGv09wM1r8lu6xSXwGvPQQUKfT1lSG0mk=; b=TSmwSIfb6yMrtnOxEbCnt5N4JxszeKXSmpP8C6zk3TXo9KEzfF4gWCJ1C/s84xt8f0Dokc GIcurIeqMRUZug87UJJUuexQmmiJp14E4as7uW4xFzSusGBXlGpf4EooxccJ/KydDLKO3N IvxfCFokEugTMGm7tAW4alRf0yBc1IU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-376-_MIicgExN0egWxn7o6VzRw-1; Wed, 23 Feb 2022 06:32:52 -0500 X-MC-Unique: _MIicgExN0egWxn7o6VzRw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3657A801AAD; Wed, 23 Feb 2022 11:32:51 +0000 (UTC) Received: from MiWiFi-R3L-srv.smartont.net (ovpn-13-167.pek2.redhat.com [10.72.13.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id A34A0797C4; Wed, 23 Feb 2022 11:32:48 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: kexec@lists.infradead.org, dyoung@redhat.com, akpm@linux-foundation.org, bhe@redhat.com, x86@kernel.org, linux-mm@kvack.org Subject: [PATCH v2 1/2] x86/kexec: fix memory leak of elf header buffer Date: Wed, 23 Feb 2022 19:32:24 +0800 Message-Id: <20220223113225.63106-2-bhe@redhat.com> In-Reply-To: <20220223113225.63106-1-bhe@redhat.com> References: <20220223113225.63106-1-bhe@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TSmwSIfb; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf02.hostedemail.com: domain of bhe@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=bhe@redhat.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 27B0680012 X-Stat-Signature: sxzdki8hdf4tm5ogt933etm6ti8ab9be X-HE-Tag: 1645615976-647448 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: This is reported by kmemleak detector: unreferenced object 0xffffc900002a9000 (size 4096): comm "kexec", pid 14950, jiffies 4295110793 (age 373.951s) hex dump (first 32 bytes): 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 .ELF............ 04 00 3e 00 01 00 00 00 00 00 00 00 00 00 00 00 ..>............. backtrace: [<0000000016a8ef9f>] __vmalloc_node_range+0x101/0x170 [<000000002b66b6c0>] __vmalloc_node+0xb4/0x160 [<00000000ad40107d>] crash_prepare_elf64_headers+0x8e/0xcd0 [<0000000019afff23>] crash_load_segments+0x260/0x470 [<0000000019ebe95c>] bzImage64_load+0x814/0xad0 [<0000000093e16b05>] arch_kexec_kernel_image_load+0x1be/0x2a0 [<000000009ef2fc88>] kimage_file_alloc_init+0x2ec/0x5a0 [<0000000038f5a97a>] __do_sys_kexec_file_load+0x28d/0x530 [<0000000087c19992>] do_syscall_64+0x3b/0x90 [<0000000066e063a4>] entry_SYSCALL_64_after_hwframe+0x44/0xae In crash_prepare_elf64_headers(), a buffer is allocated via vmalloc() to store elf headers. While it's not freed back to system correctly when kdump kernel is reloaded or unloaded. Then memory leak is caused. Fix it by introducing x86 specific function arch_kimage_file_post_load_cleanup(), and freeing the buffer there. And also remove the incorrect elf header buffer freeing code. Before calling arch specific kexec_file loading function, the image instance has been initialized. So 'image->elf_headers' must be NULL. It doesn't make sense to free the elf header buffer in the place. Signed-off-by: Baoquan He Acked-by: Dave Young --- arch/x86/kernel/machine_kexec_64.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c index f5da4a18070a..1f0eb0eed546 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -374,9 +374,6 @@ void machine_kexec(struct kimage *image) #ifdef CONFIG_KEXEC_FILE void *arch_kexec_kernel_image_load(struct kimage *image) { - vfree(image->elf_headers); - image->elf_headers = NULL; - if (!image->fops || !image->fops->load) return ERR_PTR(-ENOEXEC); @@ -512,6 +509,15 @@ int arch_kexec_apply_relocations_add(struct purgatory_info *pi, (int)ELF64_R_TYPE(rel[i].r_info), value); return -ENOEXEC; } + +int arch_kimage_file_post_load_cleanup(struct kimage *image) +{ + vfree(image->elf_headers); + image->elf_headers = NULL; + image->elf_headers_sz = 0; + + return kexec_image_post_load_cleanup_default(image); +} #endif /* CONFIG_KEXEC_FILE */ static int From patchwork Wed Feb 23 11:32:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 12756769 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 BDAADC433EF for ; Wed, 23 Feb 2022 11:32:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F3198D0006; Wed, 23 Feb 2022 06:32:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 452768D0001; Wed, 23 Feb 2022 06:32:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F3C78D0006; Wed, 23 Feb 2022 06:32:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id 219FB8D0001 for ; Wed, 23 Feb 2022 06:32:58 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E0AD59F30E for ; Wed, 23 Feb 2022 11:32:57 +0000 (UTC) X-FDA: 79173832794.11.2360840 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 6775140005 for ; Wed, 23 Feb 2022 11:32:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645615977; 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: in-reply-to:in-reply-to:references:references; bh=hwQLMtEcBC3XALlAa6IT7v4zUNwzhN/4Kn6SmvYX9qs=; b=S0SRgkw5V8Ax3+k0P1mNA/xdqevH5kC2wSYqVl0GxWrBKobPrJJEofPPyiTrPEubmniWeW H++spNB+oanteowRJKA7C2SLwW0H8Cvhb8J+uoVqeFrwqIMQjJrbD7v86qGLn/7am3DInq tZEVNIvYvZz+srOUYyVO20BiEjnupJA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-377-fFlfqDnAPRC_m8p-UZVHVQ-1; Wed, 23 Feb 2022 06:32:55 -0500 X-MC-Unique: fFlfqDnAPRC_m8p-UZVHVQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CFC41091DA0; Wed, 23 Feb 2022 11:32:54 +0000 (UTC) Received: from MiWiFi-R3L-srv.smartont.net (ovpn-13-167.pek2.redhat.com [10.72.13.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id B00FA76C2B; Wed, 23 Feb 2022 11:32:51 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: kexec@lists.infradead.org, dyoung@redhat.com, akpm@linux-foundation.org, bhe@redhat.com, x86@kernel.org, linux-mm@kvack.org Subject: [PATCH v2 2/2] kexec_file: clean up arch_kexec_kernel_image_load Date: Wed, 23 Feb 2022 19:32:25 +0800 Message-Id: <20220223113225.63106-3-bhe@redhat.com> In-Reply-To: <20220223113225.63106-1-bhe@redhat.com> References: <20220223113225.63106-1-bhe@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Rspamd-Queue-Id: 6775140005 X-Stat-Signature: dppmuj3h7o5yzz9xjp1naix4wjozo4ug X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=S0SRgkw5; spf=none (imf17.hostedemail.com: domain of bhe@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-HE-Tag: 1645615977-58914 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: Function arch_kexec_kernel_image_load() has a common weak version which is a wrapper of kexec_image_load_default() , and has only one arch dependent version in x86_64. Now the x86_64 dependent function is not needed any more. So clean it up. And also rename kexec_image_load_default() to kexec_kernel_image_load() for better reflecting its functionality. Signed-off-by: Baoquan He Acked-by: Dave Young --- arch/x86/kernel/machine_kexec_64.c | 11 ----------- include/linux/kexec.h | 1 - kernel/kexec_file.c | 9 ++------- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c index 1f0eb0eed546..6b14d9274def 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -372,17 +372,6 @@ void machine_kexec(struct kimage *image) /* arch-dependent functionality related to kexec file-based syscall */ #ifdef CONFIG_KEXEC_FILE -void *arch_kexec_kernel_image_load(struct kimage *image) -{ - if (!image->fops || !image->fops->load) - return ERR_PTR(-ENOEXEC); - - return image->fops->load(image, image->kernel_buf, - image->kernel_buf_len, image->initrd_buf, - image->initrd_buf_len, image->cmdline_buf, - image->cmdline_buf_len); -} - /* * Apply purgatory relocations. * diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 0c994ae37729..47b70402c0a4 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -186,7 +186,6 @@ void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name); /* Architectures may override the below functions */ int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len); -void *arch_kexec_kernel_image_load(struct kimage *image); int arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section, const Elf_Shdr *relsec, diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 8347fc158d2b..b6a1347f546e 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -60,7 +60,7 @@ int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, return kexec_image_probe_default(image, buf, buf_len); } -static void *kexec_image_load_default(struct kimage *image) +static void *kexec_kernel_image_load(struct kimage *image) { if (!image->fops || !image->fops->load) return ERR_PTR(-ENOEXEC); @@ -71,11 +71,6 @@ static void *kexec_image_load_default(struct kimage *image) image->cmdline_buf_len); } -void * __weak arch_kexec_kernel_image_load(struct kimage *image) -{ - return kexec_image_load_default(image); -} - int kexec_image_post_load_cleanup_default(struct kimage *image) { if (!image->fops || !image->fops->cleanup) @@ -279,7 +274,7 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, ima_add_kexec_buffer(image); /* Call arch image load handlers */ - ldata = arch_kexec_kernel_image_load(image); + ldata = kexec_kernel_image_load(image); if (IS_ERR(ldata)) { ret = PTR_ERR(ldata);