|
| 1 | +/* |
| 2 | + * To implement this patch, the SDK module `eap.o` from archive `libwpa2.a` must |
| 3 | + * be patched to call `z2EapFree` instead of `vPortFree`. This limits extending |
| 4 | + * the execution time of vPortFree to that module only. Not impacting other |
| 5 | + * modules. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#include <string.h> |
| 10 | +#include <ets_sys.h> |
| 11 | +#include <pgmspace.h> |
| 12 | +#include "coredecls.h" |
| 13 | + |
| 14 | +#ifdef DEBUG_WPA2_EAP_PATCH |
| 15 | +#include "esp8266_undocumented.h" |
| 16 | +#define DEBUG_PRINTF ets_uart_printf |
| 17 | +#else |
| 18 | +#define DEBUG_PRINTF(...) |
| 19 | +#endif |
| 20 | + |
| 21 | +extern "C" { |
| 22 | + |
| 23 | +// extern "C" void z2EapFree(void *ptr, const char* file, int line) __attribute__((weak, alias("vPortFree"))); |
| 24 | + |
| 25 | +/* |
| 26 | + * Limited 2-part wrapper for vPortFree calls made in SDK module `eap.o` from |
| 27 | + * archive `libwpa2.a`. |
| 28 | + * |
| 29 | + * vPortFree calls from eap.o are monitored for calls from line 799. This is |
| 30 | + * the location of the memory leak. At entry register a12 contains the structure |
| 31 | + * address which has the addresses of the allocations that will be leaked. |
| 32 | + * |
| 33 | + * Part 1 of this wrapper, z2EapFree, appends the value of register a12 as a |
| 34 | + * 4th argument to part2 of this wrapper, patch_wpa2_eap_vPortFree_a12(). Which |
| 35 | + * in turn checks and frees the additional allocations, that would have been |
| 36 | + * lost. |
| 37 | + * |
| 38 | + * extern "C" z2EapFree(void*); |
| 39 | + */ |
| 40 | + |
| 41 | +/* |
| 42 | + * Part 1 of Limited vPortFree Wrapper |
| 43 | + */ |
| 44 | +asm( |
| 45 | + // ".section .iram.text.z2EapFree,\"ax\",@progbits\n\t" |
| 46 | + // Since all the possible callers in eap.o are in sections starting with |
| 47 | + // .text and not .iram.text we should be safe putting these wrappers in .text. |
| 48 | + ".section .text.z2EapFree,\"ax\",@progbits\n\t" |
| 49 | + ".literal_position\n\t" |
| 50 | + ".literal .patch_wpa2_eap_vPortFree_a12, patch_wpa2_eap_vPortFree_a12\n\t" |
| 51 | + ".align 4\n\t" |
| 52 | + ".global z2EapFree\n\t" |
| 53 | + ".type z2EapFree, @function\n\t" |
| 54 | + "\n" |
| 55 | +"z2EapFree:\n\t" |
| 56 | + "addi a1, a1, -16\n\t" |
| 57 | + "s32i a0, a1, 0\n\t" |
| 58 | + "mov a5, a12\n\t" |
| 59 | + "l32r a0, .patch_wpa2_eap_vPortFree_a12\n\t" |
| 60 | + "callx0 a0\n\t" |
| 61 | + "l32i a0, a1, 0\n\t" |
| 62 | + "addi a1, a1, 16\n\t" |
| 63 | + "ret\n\t" |
| 64 | + ".size z2EapFree, .-z2EapFree\n\t" |
| 65 | +); |
| 66 | + |
| 67 | +/* |
| 68 | + * While some insight can be gained from the ESP32 repo for this structure. |
| 69 | + * It does not match exactly. This alternate structure focuses on correct offset |
| 70 | + * rather than trying to exactly reconstruct the original labels. |
| 71 | + * These offset were found in libwpa2.a:eap.o .text.eap_peer_config_init |
| 72 | + */ |
| 73 | +struct StateMachine { // size 200 bytes |
| 74 | + void* beforeConfig[16]; |
| 75 | + void* config[26]; |
| 76 | + // 0 - s32i a2, a12, 64 // username / Identity |
| 77 | + // 1 - s32i a2, a12, 68 // length |
| 78 | + // 2 - s32i a2, a12, 72 // anonymous Identity |
| 79 | + // 3 - s32i a2, a12, 76 |
| 80 | + // 4 - s32i a2, a12, 80 // password |
| 81 | + // 5 - s32i a2, a12, 84 |
| 82 | + // |
| 83 | + // "new password" - From wifi_station_set_enterprise_new_password(), we see |
| 84 | + // global saved value .bss+32 and .bss+36 which are later used to populate |
| 85 | + // ".config" in eap_peer_config_init(). I do not have an environment to |
| 86 | + // exercise this parameter. In my tests, the "new password" element in the |
| 87 | + // ".config" is never initialized. At the moment, I don't see any code that |
| 88 | + // would free the allocation. |
| 89 | + // allocated via pvPortZalloc from line 0x30f, 783 |
| 90 | + // 21 - s32i a2, a12, 148 // new password |
| 91 | + // 22 - s32i a2, a12, 152 |
| 92 | + |
| 93 | + void* afterConfig[8]; |
| 94 | +}; |
| 95 | + |
| 96 | +/* |
| 97 | + * Part 2 of Limited vPortFree Wrapper |
| 98 | + * |
| 99 | + * Presently, all SDKs have the same memory leaks in the same module at the |
| 100 | + * same line. |
| 101 | + */ |
| 102 | +void patch_wpa2_eap_vPortFree_a12(void *ptr, const char* file, int line, void* a12) { |
| 103 | + if (799 == line) { |
| 104 | + // This caller is eap_peer_config_deinit() |
| 105 | + struct StateMachine* sm = (struct StateMachine*)a12; |
| 106 | + if (ptr == sm->config[0]) { |
| 107 | + // Fix leaky frunction - eap.o only frees one out of 4 config items |
| 108 | + // finish the other 3 first |
| 109 | + vPortFree(sm->config[2], file, line); |
| 110 | + vPortFree(sm->config[4], file, line); |
| 111 | + vPortFree(sm->config[21], file, line); |
| 112 | + // ptr is sm->config[0], let fall through handle it |
| 113 | + } |
| 114 | +#ifdef DEBUG_WPA2_EAP_PATCH |
| 115 | + DEBUG_PRINTF("\nz2EapFree/vPortFree patch struct StateMachine * = %8p\n", a12); |
| 116 | + DEBUG_PRINTF(" config[0] vPortFree(%8p, file, line);\n", ptr); |
| 117 | + DEBUG_PRINTF(" config[2] vPortFree(%8p, file, line);\n", sm->config[2]); |
| 118 | + DEBUG_PRINTF(" config[4] vPortFree(%8p, file, line);\n", sm->config[4]); |
| 119 | + DEBUG_PRINTF(" config[21] vPortFree(%8p, file, line);\n", sm->config[21]); |
| 120 | + if (a12) { |
| 121 | + void** pw = (void**)a12; |
| 122 | + DEBUG_PRINTF("\nhexdump struct StateMachine:\n"); |
| 123 | + for (size_t i=0; i<200/4; i+=4) { |
| 124 | + DEBUG_PRINTF("%03u: %8p %8p %8p %8p\n", i*4, pw[i], pw[i+1], pw[i+2], pw[i+3]); |
| 125 | + } |
| 126 | + } |
| 127 | +#endif |
| 128 | + } |
| 129 | +#if 0 |
| 130 | + // This is not needed because the call was NO-OPed in the library. This code |
| 131 | + // snippit is just to show how a future memory free issue might be resolved. |
| 132 | + else if (672 == line) { |
| 133 | + // This caller is wpa2_sm_rx_eapol() |
| 134 | + // 1st of a double free |
| 135 | + // let the 2nd free handle it. |
| 136 | + return; |
| 137 | + } |
| 138 | +#endif |
| 139 | + vPortFree(ptr, file, line); |
| 140 | +} |
| 141 | + |
| 142 | +}; |
| 143 | + |
| 144 | +/* |
| 145 | + * This will minimize code space for non-wifi enterprise sketches which do not |
| 146 | + * need the patch and disable_extra4k_at_link_time(). |
| 147 | + */ |
| 148 | +void enable_wifi_enterprise_patch(void) { |
| 149 | + /* |
| 150 | + * Calling this from setup or anywhere ensures that the patch code is |
| 151 | + * included in the build. |
| 152 | + * |
| 153 | + * Also, WiFi Enterprise uses a lot of system stack space and may crash |
| 154 | + * unless we: |
| 155 | + */ |
| 156 | + disable_extra4k_at_link_time(); |
| 157 | +} |
0 commit comments