From patchwork Thu Jul 11 17:11:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 812135 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28AF8157469; Thu, 11 Jul 2024 17:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720717875; cv=none; b=oyedrRpl9JQTu0GGc+thK7EZxJqylvcD6JXpbFMd+jP9HTKT81v03DFTxuBUWeTi9ROdeNjftBaIoV86lcUZvKqZVxqFDWd1R3kx475e1zpXytwweuWdb04CwFfYvKnV2UVWE+n8u5kMuL8oJ5bxRQ24Jb8K1Ud1AIpWy9JTItE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720717875; c=relaxed/simple; bh=RXXoWLs0cwMxdM/Qi4v8p1p2NcO5f77RYkQqcvzTQug=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=okCmBnvaLkQ+01V18IMtB2/prBLgg+A5YCK10eiZdr547IVhu5lGOTfX6UX1ITMRI1Nmks/0QB3mKsnOuq+BDzKHV7+FVyJkc8AuAA3dBJ1bDNVOrQRu3nWoWW7W2zijaw1jhApRAx/KEG295x/ufJlrcj3K/xc7Z1bQiiaOJWE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TpaL++/f; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TpaL++/f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C06BAC32786; Thu, 11 Jul 2024 17:11:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720717874; bh=RXXoWLs0cwMxdM/Qi4v8p1p2NcO5f77RYkQqcvzTQug=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TpaL++/fkd/rV/wjVLLb+raq5EjkslntvsC3m1OHCzO39guyKO54WGWo1QD5mN4W8 Dk8jJyKUzWyvARG4OFG8W135wCDnwER6YGpyE80cFhiNqw+fyxmPiElrGRriDIHVER HGMf4nwFoYwBvbbzy1JvNkTCUB7gyuuSPuuXJ8f0kmVk0qScrAEnYKxYB4r4XhNQuF 8Rnq5NOYu0mvI8+qzvJmofS8MTimzpw3vJZz/RhU+A/Nsmf0yEdE0hfQoJFESX2+yQ VE4tIVzIvI0jnxBKxtRQjqpWzvxt/NZ25y7+Jx1g0spaChk2NVJnAfwljqPwPM2GmJ bfjuLc+Z+XQ3w== From: Kees Cook To: Ard Biesheuvel Cc: Kees Cook , "Kirill A. Shutemov" , "Borislav Petkov (AMD)" , Baskov Evgeniy , linux-efi@vger.kernel.org, Ingo Molnar , Radek Podgorny , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2 1/2] efi: Rename efi_early_memdesc_ptr() to efi_memdesc_ptr() Date: Thu, 11 Jul 2024 10:11:12 -0700 Message-Id: <20240711171114.121313-1-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240711170727.it.194-kees@kernel.org> References: <20240711170727.it.194-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3571; i=kees@kernel.org; h=from:subject; bh=RXXoWLs0cwMxdM/Qi4v8p1p2NcO5f77RYkQqcvzTQug=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmkBIxM12T+ItZE6IFIzzlKUhAM0dN5KvycQr6w tLQEcT7kTCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZpASMQAKCRCJcvTf3G3A Jh9OD/0UM+UTvKLR4vPibX/Exo0SOL8C9pleDjW859SF3zHF5g/1OsfMoSW0efHNxJbek+HxeLs DQAbpXWxwW/M6CsNHfdkythtMnjP6BMM/UyM6dFw59aXDz944fdUe8C1LRU3sGJ7ER8kJpZzjvi aBkbXnFmn7/R2SmxI4Qj9ENcCsbt9LE5m4RcwlU0dDI2Q8VUEV02qAIiJOKfh5ND2fruX8ZllDb 03nu2PoqqqoL2ePB4t6GLI1a6SXTSEe3ohZJEBIIahyyeYrkQColsllLAJ3WNW9p8rSPOcx9MpU YVX+yMDnghJ+ycc/x0yaY62PrHjy9W98gSys6oS8MWuXpLkvTTRpVfojp2mwDsr96pHude10T2Z bExe74xo2vz5BQcUIswEubUvWvGeZHH3G+c8SJT41XoYsaB2JvdYoSLL+BDjnpnTejKOLYSYG/s ydc5A1Z2ychI0S77PEZSqV3gjGd9SBqdFEQNmqxXEH1cjw+HIBG+cccMtusIvok5PL0sGqhjWHa OSffq/9tVkl+V9uCXIRQP0kOAXOUbUVAaT4a6YoX5jO3YGwoXXH760JsvBK6lP4nAH41GLxmOdx yMMhrJlRqNQj1p+9Chojue4+B2ZGZG8tLhckXlVwhmnHGSIiIIhiO9W5mamxnUH0VDxxlqCb3Q5 SKr9h+8Qh7bC1mw== X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 The "early" part of the helper's name isn't accurate[1]. Drop it in preparation for adding a new (not early) usage. Suggested-by: Ard Biesheuvel Link: https://lore.kernel.org/lkml/CAMj1kXEyDjH0uu3Z4eBesV3PEnKGi5ArXXMp7R-hn8HdRytiPg@mail.gmail.com [1] Signed-off-by: Kees Cook --- Cc: Ard Biesheuvel Cc: "Kirill A. Shutemov" Cc: "Borislav Petkov (AMD)" Cc: Baskov Evgeniy Cc: linux-efi@vger.kernel.org --- drivers/firmware/efi/libstub/relocate.c | 2 +- drivers/firmware/efi/libstub/unaccepted_memory.c | 2 +- drivers/firmware/efi/libstub/x86-stub.c | 2 +- include/linux/efi.h | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/efi/libstub/relocate.c b/drivers/firmware/efi/libstub/relocate.c index bf6fbd5d22a1..d694bcfa1074 100644 --- a/drivers/firmware/efi/libstub/relocate.c +++ b/drivers/firmware/efi/libstub/relocate.c @@ -48,7 +48,7 @@ efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, unsigned long m = (unsigned long)map->map; u64 start, end; - desc = efi_early_memdesc_ptr(m, map->desc_size, i); + desc = efi_memdesc_ptr(m, map->desc_size, i); if (desc->type != EFI_CONVENTIONAL_MEMORY) continue; diff --git a/drivers/firmware/efi/libstub/unaccepted_memory.c b/drivers/firmware/efi/libstub/unaccepted_memory.c index 9a655f30ba47..c295ea3a6efc 100644 --- a/drivers/firmware/efi/libstub/unaccepted_memory.c +++ b/drivers/firmware/efi/libstub/unaccepted_memory.c @@ -29,7 +29,7 @@ efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc, efi_memory_desc_t *d; unsigned long m = (unsigned long)map->map; - d = efi_early_memdesc_ptr(m, map->desc_size, i); + d = efi_memdesc_ptr(m, map->desc_size, i); if (d->type != EFI_UNACCEPTED_MEMORY) continue; diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 1983fd3bf392..8d94484f3a20 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -555,7 +555,7 @@ setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_s m |= (u64)efi->efi_memmap_hi << 32; #endif - d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); + d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i); switch (d->type) { case EFI_RESERVED_TYPE: case EFI_RUNTIME_SERVICES_CODE: diff --git a/include/linux/efi.h b/include/linux/efi.h index 418e555459da..fbdfcc9a81e6 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -783,7 +783,7 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm, efi_memattr_perm_setter fn); /* - * efi_early_memdesc_ptr - get the n-th EFI memmap descriptor + * efi_memdesc_ptr - get the n-th EFI memmap descriptor * @map: the start of efi memmap * @desc_size: the size of space for each EFI memmap descriptor * @n: the index of efi memmap descriptor @@ -801,7 +801,7 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm, * during bootup since for_each_efi_memory_desc_xxx() is available after the * kernel initializes the EFI subsystem to set up struct efi_memory_map. */ -#define efi_early_memdesc_ptr(map, desc_size, n) \ +#define efi_memdesc_ptr(map, desc_size, n) \ (efi_memory_desc_t *)((void *)(map) + ((n) * (desc_size))) /* Iterate through an efi_memory_map */ From patchwork Thu Jul 11 17:11:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 812134 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C93A16D313; Thu, 11 Jul 2024 17:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720717875; cv=none; b=oCoC9M+sZx1Nqm7IuvsUFZh7prML+m610skDReTOqPWOvUEaeRdhchlR8I8bKXQ0retw8LH0ZdJLOl7YRl9ZIE4zaOKQwPUARY8nGb5YllW2RRn5zP5MDDoeP89BHcZRapVHAy/HrNTVb+6BDhX+Kq6gGNhnkmj0A6mrvEnb6hk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720717875; c=relaxed/simple; bh=PnCjovdovXk9cCG240pvFyOatuK6fCFNWcfBvIcgqTE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ESH799+GaSdntfv2M0APgeFK/OFUd/vlzDqZ9jGSIPjlOQysJjrPfZKTEvBf28oisks2X3o2KC3BPbzrIOeL9fZeulmbdZhZUONHwBc7yHKV6lvs0zn+1nf0zbcJobIx7e6PM8Tml4VMY3Ep0OviaQo3VGxnRjBy987jw4ubt2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nNjprgW/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nNjprgW/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF0B5C4AF07; Thu, 11 Jul 2024 17:11:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720717874; bh=PnCjovdovXk9cCG240pvFyOatuK6fCFNWcfBvIcgqTE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nNjprgW/2jpNHNaDpcjA4OnmlFkC5G1rvr/2uDzwRpP01/JEDwupaQ/Y+ONE1kgwG CaJ35xc4Vza09SZ8iu8BkCr/jW9hSFWZUgGlRVMJfGM7T7kVU7cxxgt0APVajZeZLb zb0tHYRruSW+ix6stIHsUEK6J3lWpdNYroF2VJtkah5Gg35336fOaCXeNgakb7UYiG ZY/gihcKITUubrCKxfdMTvY5PYB7fjqiUkuYkZR7inh3edid8w/JbnGYIyVrqv2FCd aijKZgHxiO+iIv5YNhiHYsFvbGszFFqQJbTDUnu+HWNbwYwABjKri+6617YArDW0Ff KhLOQQ4Hj5G4A== From: Kees Cook To: Ard Biesheuvel Cc: Kees Cook , linux-efi@vger.kernel.org, "Kirill A. Shutemov" , "Borislav Petkov (AMD)" , Baskov Evgeniy , Ingo Molnar , Radek Podgorny , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2 2/2] efi: Replace efi_memory_attributes_table_t 0-sized array with flexible array Date: Thu, 11 Jul 2024 10:11:13 -0700 Message-Id: <20240711171114.121313-2-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240711170727.it.194-kees@kernel.org> References: <20240711170727.it.194-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1748; i=kees@kernel.org; h=from:subject; bh=PnCjovdovXk9cCG240pvFyOatuK6fCFNWcfBvIcgqTE=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmkBIxTZOLrENfCsAvyVVDL8bZ4k0hY9cZ8bQnc IzNZMEQ/S6JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZpASMQAKCRCJcvTf3G3A JnOmEACGwHHHeKkIjpVqzImdgW7pYpvw2dwfpQXro7NiYCbZRan+muQ5C+LH10AwVGFwwNBykvi 4d4qAtUUeMWQaTWxqxgrgrpL4ZDtPVgJxLNqbQ+Wz9I9QoOJuJgxAxKjUbhywqTkJ//0Sfe8Njr LIQb3jzTyjRtCYFffwT5MWrqImyIEAvGtRGDFkOzGwEacT/nUi17eqTc5iVBhwf4dsuAYQiW+Ue TsBLrScfZkKPifyuL1ncWoQskj66zU6POtYcjAVRkbCP06Y2hIyhUThwGzWRfrMu4N02JYD5i2W q1rzsIyqW97mZjz9IsqRDMYpjWjCttw1RaiNeXQzKCiMBA+dLOrCuV+RFv7LQq2/q5V4AUVLN1j /lfegKZxCb15Gcmq7FZvN1LiIdCZWNIuTLjHHq1tRXlX6nvEf3xtnxLZfDqo2pLrBUSsMZWYzsg Wj4dTsu5t6dhCCj4l3SdPXHoFDynqKzYQ3h/enYhilEUDmHwukphqY91Vsk10Rq8DOIRtyKqLBo sG9ORcmuFS8KPomMzW54JOZbYMPVliMVV/tCTQWGkHimYtXmQccdTW7FdFP6GIO6gBHwsF1ozMl vKqzXz1pyj+48rvbwDxP2Af5IV8LVm3NioYq93fL0BfEJ8/rAgZ4Su9gvWVpNiq+bJPD/Qo5Ii1 sZWkESA7YjCwTew== X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 While efi_memory_attributes_table_t::entry isn't used directly as an array, it is used as a base for pointer arithmetic. The type is wrong as it's not technically an array of efi_memory_desc_t's; they could be larger. Regardless, leave the type unchanged and remove the old style "0" array size. Additionally replace the open-coded entry offset code with the existing efi_memdesc_ptr() helper. Signed-off-by: Kees Cook --- Cc: Ard Biesheuvel Cc: linux-efi@vger.kernel.org --- drivers/firmware/efi/memattr.c | 2 +- include/linux/efi.h | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/efi/memattr.c b/drivers/firmware/efi/memattr.c index ab85bf8e165a..164203429fa7 100644 --- a/drivers/firmware/efi/memattr.c +++ b/drivers/firmware/efi/memattr.c @@ -164,7 +164,7 @@ int __init efi_memattr_apply_permissions(struct mm_struct *mm, bool valid; char buf[64]; - valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size, + valid = entry_is_valid(efi_memdesc_ptr(tbl->entry, tbl->desc_size, i), &md); size = md.num_pages << EFI_PAGE_SHIFT; if (efi_enabled(EFI_DBG) || !valid) diff --git a/include/linux/efi.h b/include/linux/efi.h index fbdfcc9a81e6..40475d77231c 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -607,7 +607,11 @@ typedef struct { u32 num_entries; u32 desc_size; u32 flags; - efi_memory_desc_t entry[0]; + /* + * There are @num_entries following, each of size @desc_size bytes, + * including an efi_memory_desc_t header. See efi_memdesc_ptr(). + */ + efi_memory_desc_t entry[]; } efi_memory_attributes_table_t; typedef struct {