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 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C960DC982DE for ; Mon, 21 Sep 2026 09:04:57 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4hpHNJ4T35z2yK7; Mon, 21 Sep 2026 19:04:16 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=113.46.200.224 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1789981456; cv=none; b=VZg7c0OlauNQxkOUUWGb0hrx6YqbcA2uSXxjjQdQlf8hXRaAdCr2fLhu1nHQuJymMVHBEpMPlObqYNJfJHcQ6cEblOBrfWPWyqkOkOwG/tg8UOLixOO8N9GHt60UDhY1UzNzCR9q4JESIS5qQso4uyTNZue9+cmqJH/kzECE50K8th9TngboT6hJ7EugdyIo3VLwGC8jdMNvuGLez9k6JupE9DaxIq5boF2Y7T2Lcvx+oTsX1bjnICkrHYA3VLy8qLJKisqWOdNHHByd0k3EfTzIo+XLDHXwfkHXbNR4h0TulEFxzm2v7iIIB8M+BvKoYvyYNKI7sBvNk3IbkJR29g== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1789981456; c=relaxed/relaxed; bh=up4/oktyfAdON3km3wdZWwjp1/r+vQu8s3QiiQB06UU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HsglO3/N8lJczWX3x7D2GkNpVKS2EQo9KEZL8NohZhiWSFqlMkBkmO+YCeI943dSkU+Ud//gtyGqID1SQeRlKPsy5bJAMKXkDXnge9SJC5TJvbkClkcuHuc9rWB7O55aGnHmcelNcKrF69yj5x8y2ZPVAyxSmj4PlHNPz+yp592Mo7hZ0YsqOf5iJM0vICpQXpP5M0/gHZ8l/8/je0GaP1Z03OBeCFVBtA/kckmOgemoq92m5f6UFB8ciXFMyP2RROCyAgPr4uehohl9M3R19XXwbjToZb7ZocYjv5+LxbASfYCBr05tWq0YI7CBVwsF98m/vuj4qOdy9A8/onncwg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=WJsU5ZLP; dkim-atps=neutral; spf=pass (client-ip=113.46.200.224; helo=canpmsgout09.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) smtp.mailfrom=huawei.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=WJsU5ZLP; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=huawei.com (client-ip=113.46.200.224; helo=canpmsgout09.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) Received: from canpmsgout09.his.huawei.com (canpmsgout09.his.huawei.com [113.46.200.224]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4hpHNH3N3Xz3bYx for ; Mon, 21 Sep 2026 19:04:15 +1000 (AEST) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=up4/oktyfAdON3km3wdZWwjp1/r+vQu8s3QiiQB06UU=; b=WJsU5ZLPqqnObamEl+eDb1OQLn8fzTm2lS+jqZvsjkBOcZoPNvOCq137VskBlGKfkXIRHLxz0 r3TIwLJgnYKX+hhXcyhYsMrcfZw4l3uKg5x5sTenzOpFogQlp8Dx4ywI3SARgUR1LDQ5r9Wjz1F GJzxQD5oYk6LTh2w3OIGcw8= Received: from mail.maildlp.com (unknown [172.19.163.214]) by canpmsgout09.his.huawei.com (SkyGuard) with ESMTPS id 4hpH7S5vyBz1cyVy; Mon, 21 Sep 2026 16:53:08 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id E3ED14056C; Mon, 21 Sep 2026 17:04:09 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Mon, 21 Sep 2026 17:04:07 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v6 07/14] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop Date: Mon, 21 Sep 2026 17:04:43 +0800 Message-ID: <20260921090450.807575-8-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260921090450.807575-1-ruanjinjie@huawei.com> References: <20260921090450.807575-1-ruanjinjie@huawei.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.90.53.73] X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemk200008.china.huawei.com (7.202.194.74) If load_other_segments() fails after image->elf_headers is assigned, the memory lifecycle is safely managed by the global kimage object and will be freed in arch_kimage_file_post_load_cleanup(). However, during a retry loop in efi_kexec_load(), a subsequent iteration will allocate a new buffer and overwrite image->elf_headers. This permanently leaks the stale memory from the previous iteration before the global cleanup can track it. Fix this by explicitly freeing the stale `image->elf_headers` buffer before assigning the newly allocated headers. Cc: Huacai Chen Cc: WANG Xuerui Cc: Youling Tang Cc: "Mike Rapoport (Microsoft)" Cc: Sourabh Jain Cc: Kees Cook Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/kernel/machine_kexec_file.c index 62a5be102065..3beb6977ecc6 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -166,6 +166,10 @@ int load_other_segments(struct kimage *image, vfree(headers); goto out_err; } + + if (unlikely(image->elf_headers)) + vfree(image->elf_headers); + image->elf_headers = headers; image->elf_load_addr = kbuf.mem; image->elf_headers_sz = headers_sz; -- 2.34.1