fix: kleinere Netzwerk-Härtungen

M2: WLAN-Passwort muss leer (offenes Netz) oder 8..64 Zeichen lang sein
(400 "invalid password" bei 1..7 Zeichen). CMake prüft NINLIGHT_WIFI_PASSWORD
ebenso (0 oder 8..63 Zeichen) und der Zeichen-Blacklist-Check bekommt `;`
dazu.

M4: Ein Authorization-Wert, der länger als NL_TOKEN_LEN-1 (64) ist, wird
von nl_http_parse() nicht mehr stillschweigend abgeschnitten, sondern setzt
`auth_too_long` in nl_http_request_t; nl_api_handle() behandelt eine solche
Anfrage als unauthentifiziert (außer bei offenem Zugriff), damit ein
abgeschnittener Wert nie zufällig auf ein kürzeres Token passt.

M5: get_int() prüfte die (long)-Cast-Gleichheit vor dem Bereichscheck -
bei sehr großen oder NaN-Werten ist der Cast von double nach long
undefiniertes Verhalten. Bereichs- und NaN-Check laufen jetzt vor dem Cast.

T1: sdk/Platform/Drivers/internalflash/wm_internal_fls.c: tls_fls_init()
ignorierte den Rückgabewert von tls_os_sem_create(&s_fls2_lock, 1) - jetzt
eine TLS_DBGPRT_ERR-Meldung, falls das fehlschlägt (dokumentiert in
sdk/PATCHES.md).

Tests: test_api.c (Passwortlängen, auth_too_long, 1e300-Brightness),
test_http.c (auth_too_long wird gesetzt). Für M5 zeigt der Host-Test kein
sichtbares RED, weil der (double)->(long)-Cast auf dieser Plattform/diesem
Compiler keinen Crash auslöst (implementation-defined statt UB-Crash) - der
Fix wird trotzdem angewendet, da er auf dem Zielcompiler (arm-none-eabi 4.9)
nicht garantiert sicher ist.
This commit is contained in:
Michael Sauer
2026-09-11 23:52:03 +02:00
parent d098623121
commit 2e90b8fd06
8 changed files with 74 additions and 8 deletions
+15 -4
View File
@@ -51,12 +51,19 @@ static bool token_ok(const char *given, const char *expected)
static int get_int(const cJSON *obj, const char *name, double lo, double hi, long *out)
{
const cJSON *v = cJSON_GetObjectItemCaseSensitive(obj, name);
double d;
if (!v)
return 0;
if (!cJSON_IsNumber(v) || v->valuedouble != (double)(long)v->valuedouble || v->valuedouble < lo ||
v->valuedouble > hi)
if (!cJSON_IsNumber(v))
return -1;
*out = (long)v->valuedouble;
d = v->valuedouble;
/* M5: reject NaN and out-of-range values before casting to (long) - a huge or NaN
* double is undefined behaviour to cast, so lo/hi must be checked first. */
if (d != d || d < lo || d > hi)
return -1;
if (d != (double)(long)d)
return -1;
*out = (long)d;
return 1;
}
@@ -277,6 +284,9 @@ static void handle_put_settings(const nl_api_ctx_t *ctx, const nl_http_request_t
err = "invalid ssid";
} else if ((pr = get_string(j, "password", 0, NL_PASS_LEN - 1, &b)) < 0) {
err = "invalid password";
} else if (pr == 1 && strlen(b) != 0 && strlen(b) < 8) {
/* M2: WPA2 passwords must be empty (open network) or 8..64 chars */
err = "invalid password";
} else {
if (ctx->wifi_configure(a, pr == 1 ? b : "") != 0)
reply_error(resp, 503, "wifi busy");
@@ -353,7 +363,8 @@ void nl_api_handle(const nl_api_ctx_t *ctx, const nl_http_request_t *req, nl_api
memset(resp, 0, sizeof *resp);
ctx->get_settings(&s);
authed = ctx->open_access() || token_ok(req->auth, s.token);
/* M4: a truncated Authorization value must never accidentally match a shorter token. */
authed = ctx->open_access() || (!req->auth_too_long && token_ok(req->auth, s.token));
if (strcmp(req->path, P_STATE) == 0) {
if (s.auth_all && !authed)
+3 -1
View File
@@ -85,8 +85,10 @@ nl_http_parse_t nl_http_parse(const char *buf, size_t len, nl_http_request_t *re
if (line_end - v > 7 && strncasecmp(v, "Bearer ", 7) == 0)
v += 7;
n = (size_t)(line_end - v);
if (n >= sizeof req->auth)
if (n >= sizeof req->auth) {
req->auth_too_long = true; /* M4: never silently truncate a bearer token */
n = sizeof req->auth - 1;
}
memcpy(req->auth, v, n);
req->auth[n] = '\0';
}
+2
View File
@@ -1,6 +1,7 @@
#ifndef NL_HTTP_H
#define NL_HTTP_H
#include <stdbool.h>
#include <stddef.h>
#include "config/nl_settings.h"
@@ -15,6 +16,7 @@ typedef struct {
nl_http_method_t method;
char path[64]; /* without query string */
char auth[NL_TOKEN_LEN]; /* Authorization value without "Bearer ", "" if absent */
bool auth_too_long; /* Authorization value did not fit into auth[] (truncated) */
const char *body; /* points into the caller's buffer */
size_t body_len;
} nl_http_request_t;