Skip to content

Commit 499f86d

Browse files
ahjf-07akpm00
authored andcommitted
init/main: read bootconfig header with get_unaligned_le32()
get_boot_config_from_initrd() scans up to 3 bytes before initrd_end to handle GRUB 4-byte alignment. As a result, the bootconfig header immediately preceding the magic may be unaligned. Read the size and checksum fields with get_unaligned_le32() instead of casting to u32 * and using le32_to_cpu(), avoiding potential unaligned access and silencing sparse "cast to restricted __le32" warnings. Sparse warnings (gcc + C=1): init/main.c:292:16: warning: cast to restricted __le32 init/main.c:293:16: warning: cast to restricted __le32 No functional change intended. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Sun Jian <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent a906f3a commit 499f86d

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

init/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#include <linux/pidfs.h>
105105
#include <linux/ptdump.h>
106106
#include <linux/time_namespace.h>
107+
#include <linux/unaligned.h>
107108
#include <net/net_namespace.h>
108109

109110
#include <asm/io.h>
@@ -270,7 +271,7 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
270271
{
271272
u32 size, csum;
272273
char *data;
273-
u32 *hdr;
274+
u8 *hdr;
274275
int i;
275276

276277
if (!initrd_end)
@@ -289,9 +290,9 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
289290
return NULL;
290291

291292
found:
292-
hdr = (u32 *)(data - 8);
293-
size = le32_to_cpu(hdr[0]);
294-
csum = le32_to_cpu(hdr[1]);
293+
hdr = (u8 *)(data - 8);
294+
size = get_unaligned_le32(hdr);
295+
csum = get_unaligned_le32(hdr + 4);
295296

296297
data = ((void *)hdr) - size;
297298
if ((unsigned long)data < initrd_start) {

0 commit comments

Comments
 (0)