Skip to content

Commit 6ed5e20

Browse files
benh-debianmhiramat
authored andcommitted
bootconfig: Fix unaligned access when building footer
Currently we add padding between the bootconfig text and footer to ensure that the footer is aligned within the initramfs image. However, because only the bootconfig data is held in memory, not the full initramfs image, the footer may not be naturally aligned in memory. This can result in an alignment fault (SIGBUS) when writing the footer on some architectures, such as sparc. Build the footer in a struct on the stack before adding it to the buffer. References: https://buildd.debian.org/status/fetch.php?pkg=linux&arch=sparc64&ver=6.16%7Erc7-1%7Eexp1&stamp=1753209801&raw=0 Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Ben Hutchings <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 9afa2e0 commit 6ed5e20

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

tools/bootconfig/main.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212
#include <errno.h>
1313
#include <endian.h>
14+
#include <assert.h>
1415

1516
#include <linux/bootconfig.h>
1617

@@ -363,7 +364,12 @@ static int delete_xbc(const char *path)
363364

364365
static int apply_xbc(const char *path, const char *xbc_path)
365366
{
366-
char *buf, *data, *p;
367+
struct {
368+
uint32_t size;
369+
uint32_t csum;
370+
char magic[BOOTCONFIG_MAGIC_LEN];
371+
} footer;
372+
char *buf, *data;
367373
size_t total_size;
368374
struct stat stat;
369375
const char *msg;
@@ -433,17 +439,13 @@ static int apply_xbc(const char *path, const char *xbc_path)
433439
size += pad;
434440

435441
/* Add a footer */
436-
p = data + size;
437-
*(uint32_t *)p = htole32(size);
438-
p += sizeof(uint32_t);
442+
footer.size = htole32(size);
443+
footer.csum = htole32(csum);
444+
memcpy(footer.magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
445+
static_assert(sizeof(footer) == BOOTCONFIG_FOOTER_SIZE);
446+
memcpy(data + size, &footer, BOOTCONFIG_FOOTER_SIZE);
439447

440-
*(uint32_t *)p = htole32(csum);
441-
p += sizeof(uint32_t);
442-
443-
memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
444-
p += BOOTCONFIG_MAGIC_LEN;
445-
446-
total_size = p - data;
448+
total_size = size + BOOTCONFIG_FOOTER_SIZE;
447449

448450
ret = write(fd, data, total_size);
449451
if (ret < total_size) {

0 commit comments

Comments
 (0)