fix(m1): harden provisioning per F2 review findings
- bound-check the portal option row snprintf against truncation - guard the pending-credential handoff with the provision mutex - selftest now restores (or clears) the saved wifi config instead of wiping it - mark the two conscious error-code ignores
This commit is contained in:
@@ -218,6 +218,11 @@ int console_service::selftest_command(int argc, char** argv) {
|
||||
}
|
||||
|
||||
void console_service::run_wifi_config_selftest() const {
|
||||
char prior_ssid[33]{};
|
||||
char prior_password[64]{};
|
||||
bool prior_hidden = false;
|
||||
const bool had_config =
|
||||
provision_.config_load(prior_ssid, prior_password, prior_hidden) == ESP_OK;
|
||||
const esp_err_t save_result = provision_.config_save(kSelftestSsid, kSelftestPassword, false);
|
||||
char ssid[33]{};
|
||||
char password[64]{};
|
||||
@@ -230,7 +235,13 @@ void console_service::run_wifi_config_selftest() const {
|
||||
std::strcmp(ssid, kSelftestSsid) == 0 &&
|
||||
std::strcmp(password, kSelftestPassword) == 0 && !hidden;
|
||||
std::printf("wifi_config: %s\n", round_trip ? "ok" : "FAIL");
|
||||
provision_.config_clear();
|
||||
if (had_config) {
|
||||
provision_.config_save(prior_ssid, prior_password, prior_hidden);
|
||||
} else {
|
||||
provision_.config_clear();
|
||||
}
|
||||
std::memset(prior_ssid, 0, sizeof(prior_ssid));
|
||||
std::memset(prior_password, 0, sizeof(prior_password));
|
||||
char bad_ssid[33]{};
|
||||
char bad_password[64]{};
|
||||
bool bad_hidden = false;
|
||||
|
||||
@@ -444,6 +444,7 @@ esp_err_t wifi_provision::start_provision() {
|
||||
} else {
|
||||
ESP_LOGW(kTag, "provision AP start failed: %s", esp_err_to_name(result));
|
||||
status_.update([](status_snapshot& value) { value.ap_active = false; });
|
||||
// best-effort recovery: return codes deliberately ignored
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_start();
|
||||
}
|
||||
@@ -562,12 +563,24 @@ void wifi_provision::monitor_loop() {
|
||||
vTaskDelay(pdMS_TO_TICKS(kMonitorTickMs));
|
||||
const pending_action action = pending_.exchange(pending_action::none);
|
||||
if (action == pending_action::sta_connect) {
|
||||
// the pending_* handoff from the portal task is serialized by mutex_
|
||||
char ssid[33]{};
|
||||
char password[64]{};
|
||||
bool hidden = false;
|
||||
xSemaphoreTake(mutex_, portMAX_DELAY);
|
||||
std::memcpy(ssid, pending_ssid_, sizeof(ssid));
|
||||
std::memcpy(password, pending_password_, sizeof(password));
|
||||
hidden = pending_hidden_;
|
||||
std::memset(pending_ssid_, 0, sizeof(pending_ssid_));
|
||||
std::memset(pending_password_, 0, sizeof(pending_password_));
|
||||
pending_hidden_ = false;
|
||||
xSemaphoreGive(mutex_);
|
||||
stop_provision();
|
||||
disconnect_count_.store(0);
|
||||
no_ip_ticks_.store(0);
|
||||
wifi_.connect_stored(pending_ssid_, pending_password_, pending_hidden_);
|
||||
std::memset(pending_ssid_, 0, sizeof(pending_ssid_));
|
||||
std::memset(pending_password_, 0, sizeof(pending_password_));
|
||||
wifi_.connect_stored(ssid, password, hidden);
|
||||
std::memset(ssid, 0, sizeof(ssid));
|
||||
std::memset(password, 0, sizeof(password));
|
||||
continue;
|
||||
}
|
||||
if (action == pending_action::restart_ap) {
|
||||
@@ -709,7 +722,10 @@ esp_err_t wifi_provision::root_handler(httpd_req_t* request) {
|
||||
const int written =
|
||||
std::snprintf(option, sizeof(option), "<option value=\"%s\">%s (%d dBm)</option>\n",
|
||||
escaped_value, escaped_text, networks[entry].rssi);
|
||||
if (written > 0) {
|
||||
if (written < 0) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (static_cast<std::size_t>(written) < sizeof(option)) {
|
||||
httpd_resp_send_chunk(request, option, static_cast<std::size_t>(written));
|
||||
}
|
||||
}
|
||||
@@ -792,10 +808,17 @@ esp_err_t wifi_provision::connect_handler(httpd_req_t* request) {
|
||||
return httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"save failed on device storage");
|
||||
}
|
||||
if (xSemaphoreTake(self->mutex_, pdMS_TO_TICKS(5000)) != pdTRUE) {
|
||||
std::memset(ssid, 0, sizeof(ssid));
|
||||
std::memset(password, 0, sizeof(password));
|
||||
return httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"provisioning busy, retry");
|
||||
}
|
||||
std::memcpy(self->pending_ssid_, ssid, ssid_length + 1);
|
||||
std::memcpy(self->pending_password_, password, password_length + 1);
|
||||
self->pending_hidden_ = hidden;
|
||||
self->pending_.store(pending_action::sta_connect);
|
||||
xSemaphoreGive(self->mutex_);
|
||||
std::memset(ssid, 0, sizeof(ssid));
|
||||
std::memset(password, 0, sizeof(password));
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ esp_err_t wifi_service::connect_sta(const char* ssid, const char* password, bool
|
||||
config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
|
||||
|
||||
configured_.store(false);
|
||||
// deliberate ignore: the upcoming set_config/connect supersedes any pending link
|
||||
esp_wifi_disconnect();
|
||||
esp_err_t result = esp_wifi_set_config(WIFI_IF_STA, &config);
|
||||
if (result == ESP_OK) {
|
||||
|
||||
Reference in New Issue
Block a user