[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 14/15] net: mark struct ip_header as QEMU_PACKED
From: |
Philippe Mathieu-Daudé |
Subject: |
[PULL 14/15] net: mark struct ip_header as QEMU_PACKED |
Date: |
Mon, 18 Nov 2024 13:46:41 +0100 |
From: Peter Maydell <peter.maydell@linaro.org>
The ip_header is not actually guaranteed to be aligned. We attempt to
deal with this in some places such as net_checksum_calculate() by
using stw_be_p and so on to access the fields, but this is not
sufficient to be correct, because even accessing a byte member
within an unaligned struct is undefined behaviour. The clang
sanitizer will emit warnings like these if net_checksum_calculate()
is called:
Stopping network: ../../net/checksum.c:106:9: runtime error: member access
within misaligned address 0x556aad9b502e for type 'struct ip_header', which
requires 4 byte alignment
0x556aad9b502e: note: pointer points here
34 56 08 00 45 00 01 48 a5 09 40 00 40 11 7c 8b 0a 00 02 0f 0a 00 02 02
00 44 00 43 01 34 19 56
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
../../net/checksum.c:106:9 in
../../net/checksum.c:106:9: runtime error: load of misaligned address
0x556aad9b502e for type 'uint8_t' (aka 'unsigned char'), which requires 4 byte
alignment
0x556aad9b502e: note: pointer points here
34 56 08 00 45 00 01 48 a5 09 40 00 40 11 7c 8b 0a 00 02 0f 0a 00 02 02
00 44 00 43 01 34 19 56
^
Fix this by marking the ip_header struct as QEMU_PACKED, so that
the compiler knows that it might be unaligned and will generate
the right code for accessing fields.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20241114141619.806652-3-peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/net/eth.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/net/eth.h b/include/net/eth.h
index 3b80b6e07f..14c34f530f 100644
--- a/include/net/eth.h
+++ b/include/net/eth.h
@@ -56,7 +56,7 @@ struct ip_header {
uint8_t ip_p; /* protocol */
uint16_t ip_sum; /* checksum */
uint32_t ip_src, ip_dst; /* source and destination address */
-};
+} QEMU_PACKED;
typedef struct tcp_header {
uint16_t th_sport; /* source port */
--
2.45.2
- [PULL 05/15] hw/display: check frame buffer can hold blob, (continued)
- [PULL 05/15] hw/display: check frame buffer can hold blob, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 06/15] MAINTAINERS: Update my email address for COLO, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 07/15] usb-hub: Fix handling port power control messages, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 08/15] tests/qtest/migration: Fix indentations, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 09/15] hw/sd/sdhci: Fix coding style, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 11/15] Revert "hw/audio/hda: fix memory leak on audio setup", Philippe Mathieu-Daudé, 2024/11/18
- [PULL 10/15] hw/misc/mos6522: Fix bad class definition of the MOS6522 device, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 12/15] hw/audio/hda: fix memory leak on audio setup, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 13/15] hw/net/virtio-net.c: Don't assume IP length field is aligned, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 15/15] hw/watchdog/cmsdk_apb_watchdog: Fix broken link, Philippe Mathieu-Daudé, 2024/11/18
- [PULL 14/15] net: mark struct ip_header as QEMU_PACKED,
Philippe Mathieu-Daudé <=
- Re: [PULL 00/15] Misc HW patches for 2024-11-18, Peter Maydell, 2024/11/18