bootconfig: Fix unaligned access when building footer
Subsystems (2)
PATCH 1/1 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.
--- tools/bootconfig/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
tools/bootconfig/main.c +13 -11
@@ -11,6 +11,7 @@#include <string.h> #include <errno.h> #include <endian.h> +#include <assert.h> #include <linux/bootconfig.h>
@@ -363,7 +364,12 @@ static int delete_xbc(const char *path)static int apply_xbc (const char *path,const char *xbc_path) { -char *buf, *data, *p; +struct { +uint32_t size; +uint32_t csum; +char magic[BOOTCONFIG_MAGIC_LEN ]; + } footer; +char *buf, *data;size_t total_size;struct stat stat;const char *msg;
@@ -433,17 +439,13 @@ static int apply_xbc(const char *path, const char *xbc_path) size += pad;/* Add a footer */ - p = data + size; - *(uint32_t *)p =htole32 (size); - p +=sizeof (uint32_t ); + footer.size =htole32 (size); + footer.csum =htole32 (csum); +memcpy (footer.magic,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); +static_assert (sizeof (footer) ==BOOTCONFIG_FOOTER_SIZE ); +memcpy (data + size, &footer,BOOTCONFIG_FOOTER_SIZE ); - *(uint32_t *)p =htole32 (csum); - p +=sizeof (uint32_t ); - -memcpy (p,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); - p +=BOOTCONFIG_MAGIC_LEN ; - - total_size = p - data; + total_size = size +BOOTCONFIG_FOOTER_SIZE ; ret =write (fd, data, total_size);if (ret < total_size) {
Masami Hiramatsu (Google)
maintainerQuoted 8 lines · On Wed, 23 Jul 2025 12:49:25 +0200 Ben Hutchings <[email protected]> wrote:
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.
Aah, got it.
Quoted 23 lines
Build the footer in a struct on the stack before adding it to the buffer.
Signed-off-by Ben Hutchings <[email protected]>--- tools/bootconfig/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)tools/bootconfig/main.c +1
-0@@ -11,6 +11,7 @@#include <string.h> #include <errno.h> #include <endian.h> +#include <assert.h> #include <linux/bootconfig.h>
tools/bootconfig/main.c static int delete_xbc(const char *path) · line 371
Quoted 9 lines
tools/bootconfig/main.c +5
-1@@ -363,7 +364,12 @@ static int delete_xbc(const char *path)static int apply_xbc (const char *path,const char *xbc_path) { -char *buf, *data, *p; +struct { +uint32_t size; +uint32_t csum; +char magic[BOOTCONFIG_MAGIC_LEN ]; + } footer;
Don't we need __attribute__((__packed__)) for the footer?
Thank you,
Quoted 28 lines
tools/bootconfig/main.c +7
-10+char *buf, *data;size_t total_size;struct stat stat;const char *msg;@@ -433,17 +439,13 @@ static int apply_xbc(const char *path, const char *xbc_path) size += pad;/* Add a footer */ - p = data + size; - *(uint32_t *)p =htole32 (size); - p +=sizeof (uint32_t ); + footer.size =htole32 (size); + footer.csum =htole32 (csum); +memcpy (footer.magic,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); +static_assert (sizeof (footer) ==BOOTCONFIG_FOOTER_SIZE ); +memcpy (data + size, &footer,BOOTCONFIG_FOOTER_SIZE ); - *(uint32_t *)p =htole32 (csum); - p +=sizeof (uint32_t ); - -memcpy (p,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); - p +=BOOTCONFIG_MAGIC_LEN ; - - total_size = p - data; + total_size = size +BOOTCONFIG_FOOTER_SIZE ; ret =write (fd, data, total_size);if (ret < total_size) {
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 2 lines · Masami Hiramatsu
On Wed, 23 Jul 2025 12:49:25 +0200
Ben Hutchings <[email protected]> wrote:
[...]
Quoted 10 lines
tools/bootconfig/main.c static int delete_xbc(const char *path) · line 371
Quoted 8 lines
static int
apply_xbc(const char *path, const char *xbc_path){tools/bootconfig/main.c +5
-1-char *buf, *data, *p; +struct { +uint32_t size; +uint32_t csum; +char magic[BOOTCONFIG_MAGIC_LEN ]; + } footer;Don't we need __attribute__((__packed__)) for the footer?
I don't see any reason for there to be padding in this structure, since it has an alignment of 4 and the size of each member is a multiple of 4.
I included an assertion that there is no padding:
[...]
Quoted 3 lines
tools/bootconfig/main.c static int apply_xbc(const char *path, const char *xbc_path) · line 446
Quoted 3 lines
tools/bootconfig/main.c +3
-0+memcpy (footer.magic,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); +static_assert (sizeof (footer) ==BOOTCONFIG_FOOTER_SIZE ); +memcpy (data + size, &footer,BOOTCONFIG_FOOTER_SIZE );
[...]
Ben.
Signature · 2 lines
Ben Hutchings - Debian developer, member of kernel, installer and LTS teams
Masami Hiramatsu (Google)
maintainerQuoted 18 lines · On Wed, 23 Jul 2025 22:20:35 +0200 Ben Hutchings <[email protected]> wrote:
Quoted 2 lines · Masami Hiramatsu
On Wed, 23 Jul 2025 12:49:25 +0200
Ben Hutchings <[email protected]> wrote:[...]
Quoted 10 lines
tools/bootconfig/main.c static int delete_xbc(const char *path) · line 371
Quoted 8 lines
static int
apply_xbc(const char *path, const char *xbc_path){tools/bootconfig/main.c +5
-1-char *buf, *data, *p; +struct { +uint32_t size; +uint32_t csum; +char magic[BOOTCONFIG_MAGIC_LEN ]; + } footer;Don't we need __attribute__((__packed__)) for the footer?
I don't see any reason for there to be padding in this structure, since it has an alignment of 4 and the size of each member is a multiple of 4.
Quoted 7 lines
I included an assertion that there is no padding:
[...]
Quoted 3 lines
Quoted 3 lines
tools/bootconfig/main.c +3
-0+memcpy (footer.magic,BOOTCONFIG_MAGIC ,BOOTCONFIG_MAGIC_LEN ); +static_assert (sizeof (footer) ==BOOTCONFIG_FOOTER_SIZE ); +memcpy (data + size, &footer,BOOTCONFIG_FOOTER_SIZE );[...]
OK, if we saw the assertion error, we'll add packed attribute. Let me pick it as it is.
Thank you!
Quoted 6 lines
Ben.
Signature · 2 lines
Ben Hutchings - Debian developer, member of kernel, installer and LTS teams
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>