lsalzman / enet

ENet reliable UDP networking library
MIT License
2.66k stars 667 forks source link

Unaligned reads and writes of the checksum #237

Closed arvid-norlander closed 4 months ago

arvid-norlander commented 4 months ago

This has been observed using UBSAN on the running code. It appears that sometimes the checksum pointer ends up unaligned. To avoid undefined behaviour due to misaligned reads/writes, use memcpy.

Issue was observed in a closed source application and not in a small test (just the full scale program).

Line numbers probably don't match, it is an old version 1.3.13 (but issue exists in current version as well, I upgraded and checked). Also someone in our company had the bright idea at some point to reformat all code, including external code.

enet/src/protocol.c:1402:19: runtime error: load of misaligned address 0x6260001e0bba for type 'enet_uint32', which requires 4 byte alignment
0x6260001e0bba: note: pointer points here
 00 00  00 00 55 5b 69 35 01 ff  00 02 00 02 5c ed 00 00  00 00 05 78 00 01 00 00  00 00 00 20 00 00
              ^ 
    #0 0x7f8a3f53e288 in enet_protocol_handle_incoming_commands (enet/src/protocol.c:1402)
    #1 0x7f8a3f540206 in enet_protocol_receive_incoming_commands (enet/src/protocol.c:1659)
    #2 0x7f8a3f54bd2a in enet_host_service (enet/src/protocol.c:2401)
    [... closed source code ...]

enet/src/protocol.c:1405:17: runtime error: store to misaligned address 0x6260001e0bba for type 'enet_uint32', which requires 4 byte alignment
0x6260001e0bba: note: pointer points here
 00 00  00 00 55 5b 69 35 01 ff  00 02 00 02 5c ed 00 00  00 00 05 78 00 01 00 00  00 00 00 20 00 00
              ^ 
    #0 0x7f8a3f53e3ad in enet_protocol_handle_incoming_commands (enet/src/protocol.c:1405)
    #1 0x7f8a3f540206 in enet_protocol_receive_incoming_commands (enet/src/protocol.c:1659)
    #2 0x7f8a3f54bd2a in enet_host_service (enet/src/protocol.c:2401)
    [... closed source code ...]

enet/src/protocol.c:2257:23: runtime error: store to misaligned address 0x7f88fafb2302 for type 'enet_uint32', which requires 4 byte alignment
0x7f88fafb2302: note: pointer points here
 00 00  00 00 1e 00 60 62 00 00  00 10 00 00 00 00 00 00  3b 3b e7 65 00 00 00 00  33 cb 0d 00 00 00
              ^ 
    #0 0x7f8a3f54a6f0 in enet_protocol_send_outgoing_commands (enet/src/protocol.c:2257)
    #1 0x7f8a3f54bd67 in enet_host_service (enet/src/protocol.c:2417)
    [... closed source code ...]

enet/src/protocol.c:2262:23: runtime error: store to misaligned address 0x7f88fafb2302 for type 'enet_uint32', which requires 4 byte alignment
0x7f88fafb2302: note: pointer points here
 00 00  00 00 06 66 39 3c 00 00  00 10 00 00 00 00 00 00  3b 3b e7 65 00 00 00 00  33 cb 0d 00 00 00
              ^ 
    #0 0x7f8a3f54a9d9 in enet_protocol_send_outgoing_commands (enet/src/protocol.c:2262)
    #1 0x7f8a3f54bd67 in enet_host_service (enet/src/protocol.c:2417)
    [... closed source code ...]

I will be posting a PR with a fix for this shortly.