Skip to contentSkip to conversation

device-tree/kernel: Use memcpy in output_length()

PATCH In review Michal Orzel 3 participants 4 messages Jul 22, 2025 – Jul 23, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device-tree/kernel: Use memcpy in output_length()

Trying to boot a compressed kernel with UBSAN enabled, results in the following warning:
(XEN) UBSAN: Undefined behaviour in common/device-tree/kernel.c:21:12 (XEN) load of misaligned address 00000a0040f89867 for type 'uint32_t' (XEN) which requires 4 byte alignment
...

(XEN)    [<00000a0000529964>] kernel_decompress+0x2bc/0x5bc
(XEN)    [<00000a000052a354>] kernel_probe+0x6f0/0x734
(XEN)    [<00000a0000528714>] dom0less-build.c#construct_domU+0x188/0x9d8

If &image[image_len - 4] is not aligned to 4B boundary it causes unaligned access which is undefined behavior on Arm. Use memcpy instead to be safe.

Fixes c1be0b102e0e ("xen/arm: support gzip compressed kernels")
---
 xen/common/device-tree/kernel.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/device-tree/kernel.c +5 -1
index ef393182b691..28096121a52d 100644
@@ -18,7 +18,11 @@
 
 static uint32_t __init output_length(char *image, unsigned long image_len)
 {
-    return *(uint32_t *)&image[image_len - 4];
+    uint32_t val;
+
+    memcpy(&val, &image[image_len - 4], sizeof(val));
+
+    return val;
 }
 
 int __init kernel_decompress(struct boot_module *mod, uint32_t offset)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 25 lines · Michal Orzel

Trying to boot a compressed kernel with UBSAN enabled, results in the following warning:
(XEN) UBSAN: Undefined behaviour in common/device-tree/kernel.c:21:12 (XEN) load of misaligned address 00000a0040f89867 for type 'uint32_t' (XEN) which requires 4 byte alignment
...

(XEN)    [<00000a0000529964>] kernel_decompress+0x2bc/0x5bc
(XEN)    [<00000a000052a354>] kernel_probe+0x6f0/0x734
(XEN)    [<00000a0000528714>] dom0less-build.c#construct_domU+0x188/0x9d8

If &image[image_len - 4] is not aligned to 4B boundary it causes unaligned access which is undefined behavior on Arm. Use memcpy instead to be safe.

Fixes c1be0b102e0e ("xen/arm: support gzip compressed kernels")
---
  xen/common/device-tree/kernel.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/device-tree/kernel.c +0 -0
index ef393182b691..28096121a52d 100644

xen/common/device-tree/kernel.c line 21

Quoted 4 lines
xen/common/device-tree/kernel.c +0 -1
@@ -18,7 +18,11 @@
 
  static uint32_t __init output_length(char *image, unsigned long image_len)
  {
-    return *(uint32_t *)&image[image_len - 4];

Maybe just:

    return get_unaligned_le32(&image[image_len - 4]);

You'll also need:
#include <xen/unaligned.h>

The gzip size is little endian, https://datatracker.ietf.org/doc/html/rfc1952:
Within a computer, a number may occupy multiple bytes. All multi-byte numbers in the format described here are stored with the least-significant byte first (at the lower memory address).

Regards,
Jason

Quoted 8 lines
xen/common/device-tree/kernel.c +5 -0
+    uint32_t val;
+
+    memcpy(&val, &image[image_len - 4], sizeof(val));
+
+    return val;
  }
 
  int __init kernel_decompress(struct boot_module *mod, uint32_t offset)
#2 lore raw
Quoted 32 lines · Jason Andryuk
Quoted 25 lines · Michal Orzel

Trying to boot a compressed kernel with UBSAN enabled, results in the following warning:
(XEN) UBSAN: Undefined behaviour in common/device-tree/kernel.c:21:12 (XEN) load of misaligned address 00000a0040f89867 for type 'uint32_t' (XEN) which requires 4 byte alignment
...

(XEN)    [<00000a0000529964>] kernel_decompress+0x2bc/0x5bc
(XEN)    [<00000a000052a354>] kernel_probe+0x6f0/0x734
(XEN)    [<00000a0000528714>] dom0less-build.c#construct_domU+0x188/0x9d8

If &image[image_len - 4] is not aligned to 4B boundary it causes unaligned access which is undefined behavior on Arm. Use memcpy instead to be safe.

Fixes c1be0b102e0e ("xen/arm: support gzip compressed kernels")
---
  xen/common/device-tree/kernel.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/device-tree/kernel.c +0 -0
index ef393182b691..28096121a52d 100644

xen/common/device-tree/kernel.c line 21

Quoted 4 lines
xen/common/device-tree/kernel.c +0 -1
@@ -18,7 +18,11 @@
 
  static uint32_t __init output_length(char *image, unsigned long image_len)
  {
-    return *(uint32_t *)&image[image_len - 4];

Maybe just:

Quoted 6 lines
     return get_unaligned_le32(&image[image_len - 4]);

You'll also need:
#include <xen/unaligned.h>

The gzip size is little endian,

I thought about this solution but decided to use memcpy because one day we may want to support other compression algorithms and forcing LE could cause issues.

I'm ok either way. Let other maintainers decide.

~Michal

#3 lore raw

Hi,

Quoted 39 lines · Michal
Quoted 34 lines · Jason Andryuk
Quoted 25 lines · Michal Orzel

Trying to boot a compressed kernel with UBSAN enabled, results in the following warning:
(XEN) UBSAN: Undefined behaviour in common/device-tree/kernel.c:21:12 (XEN) load of misaligned address 00000a0040f89867 for type 'uint32_t' (XEN) which requires 4 byte alignment
...

(XEN)    [<00000a0000529964>] kernel_decompress+0x2bc/0x5bc
(XEN)    [<00000a000052a354>] kernel_probe+0x6f0/0x734
(XEN)    [<00000a0000528714>] dom0less-build.c#construct_domU+0x188/0x9d8

If &image[image_len - 4] is not aligned to 4B boundary it causes unaligned access which is undefined behavior on Arm. Use memcpy instead to be safe.

Fixes c1be0b102e0e ("xen/arm: support gzip compressed kernels")
---
 xen/common/device-tree/kernel.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/device-tree/kernel.c +0 -0
index ef393182b691..28096121a52d 100644

xen/common/device-tree/kernel.c line 21

Quoted 4 lines
xen/common/device-tree/kernel.c +0 -1
@@ -18,7 +18,11 @@
 
 static uint32_t __init output_length(char *image, unsigned long image_len)
 {
-    return *(uint32_t *)&image[image_len - 4];

Maybe just:

    return get_unaligned_le32(&image[image_len - 4]);
Quoted 6 lines
Quoted 4 lines

You'll also need:
#include <xen/unaligned.h>

The gzip size is little endian,

I thought about this solution but decided to use memcpy because one day we may want to support other compression algorithms and forcing LE could cause issues.

That makes sense so:

Cheers
Bertrand

Quoted 5 lines

I'm ok either way. Let other maintainers decide.

~Michal