Skip to content

Conversation

mhightower83
Copy link
Contributor

No description provided.

@earlephilhower
Copy link
Collaborator

Can you please explain the logic here, @mhightower83 ? I believe the current code dumps the proper stuff.

My test case:

I've applied something to make the BSSL CertStore crash:

earle@server:~/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src$ git diff diff --git a/libraries/ESP8266WiFi/src/CertStoreBearSSL.cpp b/libraries/ESP8266WiFi/src/CertStoreBearSSL.cpp index 905efde2..e7d6312b 100644 --- a/libraries/ESP8266WiFi/src/CertStoreBearSSL.cpp +++ b/libraries/ESP8266WiFi/src/CertStoreBearSSL.cpp @@ -212,7 +212,7 @@ const br_x509_trust_anchor *CertStore::findHashedTA(void *ctx, void *hashed_dn, br_x509_trust_anchor *ta = (br_x509_trust_anchor*)cs->_x509->getTrustAnchors(); memcpy(ta->dn.data, ci.sha256, sizeof(ci.sha256)); ta->dn.len = sizeof(ci.sha256); - +ta=(br_x509_trust_anchor*)1; return ta; } } 

and run the BSSL_CertStore example. It crashes, as expected, giving a dump:

 Connecting to NOBABIES . WiFi connected IP address: 192.168.1.154 Waiting for NTP time sync: . Current time: Sun Feb 16 02:09:24 2020 Number of CA certs read: 149 Attempting to fetch https://www.github.com/... Trying: www.github.com:443... Exception (9): epc1=0x4022e0a4 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000001 depc=0x00000000 >>>stack>>> ctx: bearssl sp: 3fff1738 end: 3fff1c58 offset: 0190 3fff18c8: 3fff6e54 3fff6d84 00000000 4022e096 3fff18d8: 8477bde1 32ce0337 819ac8b5 4e924a1a 3fff18e8: 859a463b f9a283fe cca38c9e 3db35e0d 3fff18f8: 8f7804cf deadbeef 00000d00 00000000 3fff1908: 51cd85fd 59771cc0 c06ad9ab 4396cd84 3fff1918: 4025e3b7 522daf2c f6f3c118 5123a06c 3fff1928: 3fff6d9c 000000ba 000006a3 3fff6db0 3fff1938: a78cb215 2e5f44dd 3bcd3d87 5b36d405 .... 3fff1c28: 00003a98 00000000 3fff1c64 40226d21 3fff1c38: 000000db deadbeef deadbeef deadbeef 3fff1c48: deadbeef 00000000 3ffeff5c 40204394 ctx: cont sp: 3ffffe50 end: 3fffffc0 offset: 0000 3ffffe50: 00003a98 00000000 00000008 40203dba 3ffffe60: 0000000d 00000005 5845ffaa 00000000 ... 3fffffa0: 3fffdad0 00000000 3ffee780 402061e0 3fffffb0: feefeffe feefeffe 3ffe84f0 40100c2d <<<stack<<< 

Which, with git head, seems to decode just fine:

 Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address PC: 0x4022e0a4: br_x509_minimal_run at src/x509/x509_minimal.c line 1254 EXCVADDR: 0x00000001 Decoding stack results 0x4022e096: br_x509_minimal_run at src/x509/x509_minimal.c line 1252 0x4022e8b6: xm_append at src/x509/x509_minimal.c line 285 0x40228850: br_ssl_hs_client_run at src/ssl/ssl_hs_client.c line 1871 0x4022683c: jump_handshake at src/ssl/ssl_engine.c line 1081 0x40226d21: br_ssl_engine_recvrec_ack at src/ssl/ssl_engine.c line 1206 0x40203dba: BearSSL::WiFiClientSecure::_run_until(unsigned int, bool) at /home/earle/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp line 540 0x40226e46: br_ssl_engine_hs_reset at src/ssl/ssl_engine.c line 1305 0x40203ff8: BearSSL::WiFiClientSecure::_wait_for_handshake() at /home/earle/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp line 564 0x402041db: BearSSL::WiFiClientSecure::_connectSSL(char const*) at /home/earle/Arduino/hardware/esp8266com/esp8266/tools/xtensa-lx106-elf/xtensa-lx106-elf/include/c++/4.8.2/bits/shared_ptr.h line 291 0x402060d3: __esp_yield() at /home/earle/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_main.cpp line 107 0x40206686: __delay(unsigned long) at /home/earle/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring.cpp line 54 0x40202dc8: WiFiClient::connect(IPAddress, unsigned short) at /home/earle/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/include/ClientContext.h line 133 0x402042f9: BearSSL::WiFiClientSecure::connect(char const*, unsigned short) at /home/earle/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp line 232 0x40201145: fetchURL(BearSSL::WiFiClientSecure*, char const*, unsigned short, char const*) at /tmp/arduino_modified_sketch_713573/BearSSL_CertStore.ino line 80 0x40203b30: BearSSL::WiFiClientSecure::_clearAuthenticationSettings() at /home/earle/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp line 104 0x40201356: setup() at /tmp/arduino_modified_sketch_713573/BearSSL_CertStore.ino line 153 0x402061e0: loop_wrapper() at /home/earle/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_main.cpp line 177 
@mhightower83
Copy link
Contributor Author

Sorry to create confusion.

stack_thunk_ptr = (uint32_t *)malloc(_stackSize * sizeof(uint32_t));
stack_thunk_top = stack_thunk_ptr + _stackSize - 1;

What I was looking at, was that stack_thunk_top is larger stack_thunk_ptr.
Which means pos starting at a value greater than stack_thunk_ptr in the while loops below.
So they are skipped and never run.

void stack_thunk_dump_stack()
{
uint32_t *pos = stack_thunk_top;
while (pos < stack_thunk_ptr) {
if ((pos[0] != _stackPaint) || (pos[1] != _stackPaint) || (pos[2] != _stackPaint) || (pos[3] != _stackPaint))
break;
pos += 4;
}
ets_printf(">>>stack>>>\n");
while (pos < stack_thunk_ptr) {
ets_printf("%08x: %08x %08x %08x %08x\n", (int32_t)pos, pos[0], pos[1], pos[2], pos[3]);
pos += 4;
}

BearSSL stack printing is handled locally in postmortem. The function stack_thunk_dump_stack() is not called.

if (sp_dump > stack_thunk_get_stack_bot() && sp_dump <= stack_thunk_get_stack_top()) {
// BearSSL we dump the BSSL second stack and then reset SP back to the main cont stack
ets_printf_P(PSTR("\nctx: bearssl\nsp: %08x end: %08x offset: %04x\n"), sp_dump, stack_thunk_get_stack_top(), offset);
print_stack(sp_dump + offset, stack_thunk_get_stack_top());
offset = 0; // No offset needed anymore, the exception info was stored in the bssl stack
sp_dump = stack_thunk_get_cont_sp();
}

@earlephilhower
Copy link
Collaborator

I suppose there is some use for the (uncalled-in-the-core) function, so thanks for the explanation and the fix!

@earlephilhower earlephilhower merged commit 321d256 into esp8266:master Feb 16, 2020
@mhightower83 mhightower83 deleted the pr-bear-stack-dump branch January 13, 2021 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants