debevv / nanoMODBUS

A compact MODBUS RTU/TCP C library for embedded/microcontrollers
MIT License
234 stars 47 forks source link

Stack smashing protect failure #36

Closed kopierreko closed 4 months ago

kopierreko commented 11 months ago

Hi,

I'm still working with your library on a modbus project and in this case, I can see some perturbations on the bus (because a high frequency signal is running near from the modbus cable, but it can be filtered easily by the transceiver). I have a crash in my program (which is a little difficult to produce without all the code, but I can try in the future if really needed), localized at nanomodbus.c @1085 : if (!first_byte_received && err == NMBS_ERROR_TIMEOUT) :

nmbs_error nmbs_server_poll(nmbs_t* nmbs) {
    msg_state_reset(nmbs);

    bool first_byte_received = false;
    nmbs_error err = recv_req_header(nmbs, &first_byte_received);
    if (err != NMBS_ERROR_NONE) {
        if (!first_byte_received && err == NMBS_ERROR_TIMEOUT)
            return NMBS_ERROR_NONE;

        return err;
    }

The ESP32 returns "Stack smashing protect failure!" at the moment of the crash.

Decoding stack results 0x40083b81: panic_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/panic.c line 402 0x4008bc45: esp_system_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/esp_system.c line 121 0x40090eb5: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/abort.c line 46 0x400ecf98: __stack_chk_fail at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/stack_check.c line 36 0x400e1daa: nmbs_server_poll at lib/nanomodbus/nanomodbus.c line 1085 0x400d40fe: ModBus::loop() at src/modbus.h line 358 0x400d50d7: loop() at src/main.cpp line 956 0x400e4685: loopTask(void*) at /Users/pierreko/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp line 50

Image PNG

Do you have an idea ?

Thanks 😉

debevv commented 11 months ago

I can't see any place in nmbs_server_poll() where some stack smashing could take place. Usually that happens with operations like string manipulations, memset()s, arrays, etc. that could overwrite the stack but, from a cursory look, they aren't performed anywhere in the function or in its call tree.
Can you check the function calling nmbs_server_poll()? Also, is nmbs a valid pointer?

kopierreko commented 11 months ago

I founded a way to make it working by removing the glitches on the modbus wire, I had an electronic component which made EM perturbations but it suggests that the code can crash if there is too much perturbations on the bus. I can try what you’re talking about but not before 14/08.

Thanks for your answer and I will say you if I have this error 😊

Le 27 juil. 2023 à 13:48, Valerio De Benedetto @.***> a écrit :

I can't see any place in nmbs_server_poll() where some stack smashing could take place. Usually that happens with operations like string manipulations, memset()s, arrays, etc. that could overwrite the stack but, from a cursory look, they aren't performed anywhere in the function or in its call tree. Can you check the function calling nmbs_server_poll()? Also, is nmbs a valid pointer?

— Reply to this email directly, view it on GitHub https://github.com/debevv/nanoMODBUS/issues/36#issuecomment-1653455575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEIPLEMIINDPB4KSEGE7O7DXSJIPTANCNFSM6AAAAAA2TZU47Q. You are receiving this because you authored the thread.

debevv commented 11 months ago

Can you try to reproduce the issue with NMBS_DEBUG enabled and show me the logs? Also, can you use the latest version (v1.14.3)?