Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp599144ybz; Fri, 24 Apr 2020 06:09:37 -0700 (PDT) X-Google-Smtp-Source: APiQypKohOoZ6QUUGhrpveni98oASATuU7+gxYREDcSDdEGerA2gtsO59r0YxcYoJSnHNc6al2Wb X-Received: by 2002:a17:906:4c8e:: with SMTP id q14mr7338660eju.208.1587733777748; Fri, 24 Apr 2020 06:09:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587733777; cv=none; d=google.com; s=arc-20160816; b=vXEmDIxl5Jb9jSz1ggMD2YWVLFfmT6oetv5XNqXHGCmp9WqVXaTX7zxMhr3eC1qqR/ ReX5yZ3gAEKywEZJ04eNuBUN4KcVFJiS9yg5w5SKNoKghRGZ8jt8twyAEUjtZSPq962J 5c6ZvG8oIVKOUYdnP8XdIEcoHGVz2c2b15TKBYZLifePd1zyXaZ/FHV3bJgyEd3aTzZy fDbBVV2DJ87BtOozk0g6LGej4UR5oCtoFYU+quHlIceVlR4yV57yOO+wuY05pjoTgboz 6EhcIb/a6oZ+8v9ppVDBLjeTyspt6bE3+n2jqn/a73M0QHagq+nUPi2ypyhrVKR0p0lb UEYQ== 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:dkim-signature; bh=LP+FgzTzzmMZa4enkKiuFBZ5+rTKPHm2uYmiqxGgUU4=; b=0DQeuEQ+a2IQXirxFQMtugDTPyKn2Lw+csDfOVvImvLxdmsXMwKzyuV0NlcJsSWd5Z UFTKD/+0SBEC87AODej+2QiS9vDSBIJbJ+Z50aPfuskl2SZh6ffeNtAVR6uJciBawRFZ CIr7DUNERYq06mzLx+ErVizS2/qaqaoyjme7YQBlX74+JYEBzsfHajoX6VANipu0R+Mj ivh9hHX75aTHH+jOu/qH5Xv/m9MW3za7hdS+T8a91z/gmgSx/azIn+ELBTrar8LKRsCZ vFvmxcba6N7xRBa9k/zr3baz8vlB1JuE4TwbEpnveOpATQaRLysVVwHgY2JkAZwVGuU6 0Dig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oKl3xONk; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bu17si2963818edb.276.2020.04.24.06.09.08; Fri, 24 Apr 2020 06:09:37 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=oKl3xONk; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728172AbgDXNG4 (ORCPT + 99 others); Fri, 24 Apr 2020 09:06:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:57508 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728142AbgDXNGy (ORCPT ); Fri, 24 Apr 2020 09:06:54 -0400 Received: from e123331-lin.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr [109.210.65.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C0BC82084D; Fri, 24 Apr 2020 13:06:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587733613; bh=BUDRicBn8f0ZY8ppIfG0csRmOCIXJIw2tlzx7xNVDnw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oKl3xONkNiDUd2ZKrEORtN9Bk3G5TXp4RXuuOYmjrFEYRaILFHZHvVJuArYuOeTTZ NCutFaTFElJ7as7jaccxAeg0UYCGUwJBRYceY1zZ9Ui9iWMCwe1ZCAfTv555D/0/iD JOa+Ia/M6HJ464ejX0NslrN2dQpPmWmgI57ZH+8g= From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Arvind Sankar , Atish Patra , Palmer Dabbelt , Zou Wei Subject: [PATCH 19/33] efi/libstub/random: Align allocate size to EFI_ALLOC_ALIGN Date: Fri, 24 Apr 2020 15:05:17 +0200 Message-Id: <20200424130531.30518-20-ardb@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200424130531.30518-1-ardb@kernel.org> References: <20200424130531.30518-1-ardb@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The EFI stub uses a per-architecture #define for the minimum base and size alignment of page allocations, which is set to 4 KB for all architecures except arm64, which uses 64 KB, to ensure that allocations can always be (un)mapped efficiently, regardless of the page size used by the kernel proper, which could be a kexec'ee The API wrappers around page based allocations assume that this alignment is always taken into account, and so efi_free() will also round up its size argument to EFI_ALLOC_ALIGN. Currently, efi_random_alloc() does not honour this alignment for the allocated size, and so freeing such an allocation may result in unrelated memory to be freed, potentially leading to issues after boot. So let's round up size in efi_random_alloc() as well. Fixes: 2ddbfc81eac84a29 ("efi: stub: add implementation of efi_random_alloc()") Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/randomalloc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index 4578f59e160c..6200dfa650f5 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -74,6 +74,8 @@ efi_status_t efi_random_alloc(unsigned long size, if (align < EFI_ALLOC_ALIGN) align = EFI_ALLOC_ALIGN; + size = round_up(size, EFI_ALLOC_ALIGN); + /* count the suitable slots in each memory map entry */ for (map_offset = 0; map_offset < map_size; map_offset += desc_size) { efi_memory_desc_t *md = (void *)memory_map + map_offset; @@ -109,7 +111,7 @@ efi_status_t efi_random_alloc(unsigned long size, } target = round_up(md->phys_addr, align) + target_slot * align; - pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; + pages = size / EFI_PAGE_SIZE; status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, pages, &target); -- 2.17.1