Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp619198imm; Mon, 2 Jul 2018 18:53:11 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeVEyp+o2D1ovHX5HMCvnMPspeD6G/h5wLWKSBuvvbnmAW1vodKLzOikdIZ0Br2/PpziDQR X-Received: by 2002:a65:5bc4:: with SMTP id o4-v6mr20137038pgr.448.1530582791840; Mon, 02 Jul 2018 18:53:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530582791; cv=none; d=google.com; s=arc-20160816; b=ini+xy0gZAxQrz1vA1yG16U0+jq5dLSSNXEtBl8LHvThwOWxZzAlVjlcQj1iZhM39t 9mKol8dD4mDiuf2Z99oOB6JdnMHL+yFA3kAG6YbSIhdFMwoMwXc7L/rRwJcM+vhGe2r0 aYRtc0EPqO+O1VeMAxRGu0oplRxloimQ8KrUjkte/br5dlE7RgX2NbdgcQ9L5Sc0+kfh NkaVGyTHErQPflCaRH3UvnD5uuZUgIR2GzDyLpxwJwq59sx9CuY0hUx5SAjQNZ1k8ra4 JWV+SUO4isKjEs6dIFIZRhkkOK4N/M39ANpl92zA+3YxrMbJsptLy6Ngium6xIU9Ygfk SjIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=spKnrP9rdYkIAE2N+qKzLz58iaCsIom98YFN8qrGt8U=; b=xgIdMcOlXNHQfPZ5NR9UdZb/xs58e40/CbQu0uPR54NK8mUv6+K7OZRK72xiretIiW MZagVqHGgKejJA5CYWi3MZqn8wyNTZMHfukUj+UbMZ2d+A1QgDsUpL9ckYmCNREviE33 ZIuPhQxAIkMnCH/42Yw09mlRDyZ6dkN7V6RAs5iHZ9rtAXqvmgcH3NxnaAazwK2o2HpP eqWSNndWAFEGgrgteXbOVKtN0LuSPBf4CmXPIWA38ifUADkzcBsVO33lioClKpbMZgxY G5z8FAslGay5x8/JbgzPRlHQCzLhu0UIBGf3Rfpg8KjuNxsAkUFy71QYic3DJQf2Q80C WAVA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g6-v6si13091618plq.242.2018.07.02.18.52.57; Mon, 02 Jul 2018 18:53:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932328AbeGCBvz (ORCPT + 99 others); Mon, 2 Jul 2018 21:51:55 -0400 Received: from mga07.intel.com ([134.134.136.100]:5209 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753639AbeGCBvX (ORCPT ); Mon, 2 Jul 2018 21:51:23 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Jul 2018 18:51:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,301,1526367600"; d="scan'208";a="212956413" Received: from sai-dev-mach.sc.intel.com ([143.183.140.52]) by orsmga004.jf.intel.com with ESMTP; 02 Jul 2018 18:51:17 -0700 From: Sai Praneeth Prakhya To: linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sai Praneeth , Lee Chun-Yi , Dave Young , Borislav Petkov , Laszlo Ersek , Jan Kiszka , Dave Hansen , Bhupesh Sharma , Nicolai Stange , Naresh Bhat , Ricardo Neri , Peter Zijlstra , Taku Izumi , Ravi Shankar , Matt Fleming , Dan Williams , Ard Biesheuvel Subject: [PATCH 5/6] x86/efi: Free allocated memory if remap fails Date: Mon, 2 Jul 2018 18:50:53 -0700 Message-Id: <1530582654-25662-6-git-send-email-sai.praneeth.prakhya@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1530582654-25662-1-git-send-email-sai.praneeth.prakhya@intel.com> References: <1530582654-25662-1-git-send-email-sai.praneeth.prakhya@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth efi_memmap_alloc(), as the name suggests, allocates memory for a new efi memory map. It's referenced from couple of places, namely, efi_arch_mem_reserve() and efi_free_boot_services(). These callers, after allocating memory, remap it for further use. As usual, a routine check is performed to confirm successful remap. If the remap fails, ideally, the allocated memory should be freed but presently we just return without freeing it up. Hence, fix this bug by freeing the memory with efi_memmap_free(). Also, efi_fake_memmap() references efi_memmap_alloc() but it frees memory correctly using memblock_free(), but replace it with efi_memmap_free() to maintain consistency, as in, allocate memory with efi_memmap_alloc() and free memory with efi_memmap_free(). It's a fact that memremap() and early_memremap() might never fail and this code might never get a chance to run but to maintain good kernel programming semantics, we might need this patch. Signed-off-by: Sai Praneeth Prakhya Suggested-by: Ard Biesheuvel Cc: Lee Chun-Yi Cc: Dave Young Cc: Borislav Petkov Cc: Laszlo Ersek Cc: Jan Kiszka Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Nicolai Stange Cc: Naresh Bhat Cc: Ricardo Neri Cc: Peter Zijlstra Cc: Taku Izumi Cc: Ravi Shankar Cc: Matt Fleming Cc: Dan Williams Cc: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 10 ++++++++-- drivers/firmware/efi/fake_mem.c | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 11800f3cbb93..8fce327387e5 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -286,6 +286,7 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) new = early_memremap(new_phys, new_size); if (!new) { pr_err("Failed to map new boot services memmap\n"); + efi_memmap_free(new_phys, num_entries, alloc_type); return; } @@ -434,7 +435,7 @@ void __init efi_free_boot_services(void) new = memremap(new_phys, new_size, MEMREMAP_WB); if (!new) { pr_err("Failed to map new EFI memmap\n"); - return; + goto free_mem; } /* @@ -460,8 +461,13 @@ void __init efi_free_boot_services(void) efi.memmap.alloc_type); if (efi_memmap_install(new_phys, num_entries, alloc_type)) { pr_err("Could not install new EFI memmap\n"); - return; + goto free_mem; } + + return; + +free_mem: + efi_memmap_free(new_phys, num_entries, alloc_type); } /* diff --git a/drivers/firmware/efi/fake_mem.c b/drivers/firmware/efi/fake_mem.c index a47754efb796..09b0fabf07fd 100644 --- a/drivers/firmware/efi/fake_mem.c +++ b/drivers/firmware/efi/fake_mem.c @@ -80,7 +80,7 @@ void __init efi_fake_memmap(void) new_memmap = early_memremap(new_memmap_phy, efi.memmap.desc_size * new_nr_map); if (!new_memmap) { - memblock_free(new_memmap_phy, efi.memmap.desc_size * new_nr_map); + efi_memmap_free(new_memmap_phy, new_nr_map, alloc_type); return; } -- 2.7.4