eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
313 stars 179 forks source link

Crash (segmentation fault) when getaddrinfo fails #229

Closed vera closed 3 years ago

vera commented 3 years ago

Log + valgrind output:

20210203 183156.972   CONNECT           <---  example                 19 04 04 01 01 68 73 75 69 74 5F 75 70 64 61 74 65 5F 65 78 61 6D 70 6C 65
20210203 183157.065   CONNECT           ===>  example                 10 1F 00 04 4D 51 54 54 04 02 01 68 00 13 73 75 69 74 5F 75 70 64 61 74 65 5F 65 78 61 6D 70 6C 65
20210203 183157.153   CONNACK           <===  example                 20 02 00 00
20210203 183157.158   CONNACK           --->  example                 03 05 00
==122554== Thread 4:
==122554== Use of uninitialised value of size 8
==122554==    at 0x1276BB: MQTTSNGW::UDPPort6::unicast(unsigned char const*, unsigned int, MQTTSNGW::SensorNetAddress*) (SensorNetwork.cpp:403)
==122554==    by 0x124AF0: MQTTSNGW::AdapterManager::unicastToClient(MQTTSNGW::Client*, MQTTSNGW::MQTTSNPacket*, MQTTSNGW::ClientSendTask*) (MQTTSNGWAdapterManager.cpp:158)
==122554==    by 0x118348: MQTTSNGW::ClientSendTask::run() (MQTTSNGWClientSendTask.cpp:76)
==122554==    by 0x1183FC: MQTTSNGW::ClientSendTask::EXECRUN() (MQTTSNGWClientSendTask.h:31)
==122554==    by 0x1298F5: MQTTSNGW::Thread::_run(void*) (Threading.cpp:517)
==122554==    by 0x486E608: start_thread (pthread_create.c:477)
==122554==    by 0x4F11292: clone (clone.S:95)

In my case, this problem appeared because I had accidentally set an GatewayUDP6If in the configuration file that didn't exist, but getaddrinfo could probably also fail for other reasons. If it fails, then res->ai_addr is uninitialised.

In the code, the return code of getaddrinfo should be checked before res is used.

https://github.com/eclipse/paho.mqtt-sn.embedded-c/blob/ca467548b437394f69773d736b1eddc03c4a7cdd/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp#L385-L403

ty4tw commented 3 years ago

Fixed