* [PATCH] bootconfig: Fix unaligned access when building footer
@ 2025-07-23 10:49 Ben Hutchings
2025-07-23 15:37 ` Masami Hiramatsu
0 siblings, 1 reply; 4+ messages in thread
From: Ben Hutchings @ 2025-07-23 10:49 UTC (permalink / raw)
To: Masami Hiramatsu; +Cc: linux-kernel, linux-trace-kernel
[-- Attachment #1: Type: text/plain, Size: 2158 bytes --]
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
Signed-off-by: Ben Hutchings <benh@debian.org>
---
tools/bootconfig/main.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 4988e23a1422..57c669d2aa90 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -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) {
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] bootconfig: Fix unaligned access when building footer
2025-07-23 10:49 [PATCH] bootconfig: Fix unaligned access when building footer Ben Hutchings
@ 2025-07-23 15:37 ` Masami Hiramatsu
2025-07-23 20:20 ` Ben Hutchings
0 siblings, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2025-07-23 15:37 UTC (permalink / raw)
To: Ben Hutchings; +Cc: linux-kernel, linux-trace-kernel
On Wed, 23 Jul 2025 12:49:25 +0200
Ben Hutchings <benh@debian.org> 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.
>
> 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
> Signed-off-by: Ben Hutchings <benh@debian.org>
> ---
> tools/bootconfig/main.c | 24 +++++++++++++-----------
> 1 file changed, 13 insertions(+), 11 deletions(-)
>
> diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
> index 4988e23a1422..57c669d2aa90 100644
> --- a/tools/bootconfig/main.c
> +++ b/tools/bootconfig/main.c
> @@ -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;
Don't we need __attribute__((__packed__)) for the footer?
Thank you,
> + 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) <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bootconfig: Fix unaligned access when building footer
2025-07-23 15:37 ` Masami Hiramatsu
@ 2025-07-23 20:20 ` Ben Hutchings
2025-07-24 1:10 ` Masami Hiramatsu
0 siblings, 1 reply; 4+ messages in thread
From: Ben Hutchings @ 2025-07-23 20:20 UTC (permalink / raw)
To: Masami Hiramatsu; +Cc: linux-kernel, linux-trace-kernel
[-- Attachment #1: Type: text/plain, Size: 942 bytes --]
On Thu, 2025-07-24 at 00:37 +0900, Masami Hiramatsu wrote:
> On Wed, 23 Jul 2025 12:49:25 +0200
> Ben Hutchings <benh@debian.org> wrote:
[...]
> > 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?
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:
[...]
> > + memcpy(footer.magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
> > + static_assert(sizeof(footer) == BOOTCONFIG_FOOTER_SIZE);
> > + memcpy(data + size, &footer, BOOTCONFIG_FOOTER_SIZE);
[...]
Ben.
--
Ben Hutchings - Debian developer, member of kernel, installer and LTS
teams
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bootconfig: Fix unaligned access when building footer
2025-07-23 20:20 ` Ben Hutchings
@ 2025-07-24 1:10 ` Masami Hiramatsu
0 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu @ 2025-07-24 1:10 UTC (permalink / raw)
To: Ben Hutchings; +Cc: linux-kernel, linux-trace-kernel
On Wed, 23 Jul 2025 22:20:35 +0200
Ben Hutchings <benh@debian.org> wrote:
> On Thu, 2025-07-24 at 00:37 +0900, Masami Hiramatsu wrote:
> > On Wed, 23 Jul 2025 12:49:25 +0200
> > Ben Hutchings <benh@debian.org> wrote:
> [...]
> > > 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?
>
> 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:
>
> [...]
> > > + 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!
>
> Ben.
>
> --
> Ben Hutchings - Debian developer, member of kernel, installer and LTS
> teams
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-07-24 1:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-23 10:49 [PATCH] bootconfig: Fix unaligned access when building footer Ben Hutchings
2025-07-23 15:37 ` Masami Hiramatsu
2025-07-23 20:20 ` Ben Hutchings
2025-07-24 1:10 ` Masami Hiramatsu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).