From fabf0c1663277be6f6c7feb7f840657b29ed87e3 Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 29 Oct 2026 22:21:12 +0100 Subject: [PATCH] Reject BLE values before all mandatory subscriptions --- lib/ble/ble_transport.c | 4 ++++ lib/ble/tests/test_ble_transport.c | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/ble/ble_transport.c b/lib/ble/ble_transport.c index 2602e077..89b3f8ca 100644 --- a/lib/ble/ble_transport.c +++ b/lib/ble/ble_transport.c @@ -394,6 +394,10 @@ void ble_backend_opened(struct ble_transport* t, ble_link_id id, unsigned int mt void ble_backend_received(struct ble_transport* t, ble_link_id id, enum ble_wire_channel wire, const uint8_t* data, size_t len) { struct ble_connection* c = lookup(t, id); if (!c) return; + if (!c->opened) { + DEBUG_WARN(CAT, "BLE value before mandatory subscriptions link=%llu channel=%d bytes=%zu", (unsigned long long)id, wire, len); + close_link(t, c, "value before subscriptions"); return; + } if (!data || !len || len > c->capacity || (unsigned)wire >= BLE_WIRE_COUNT) { DEBUG_WARN(CAT, "BLE invalid wire value link=%llu channel=%d bytes=%zu", (unsigned long long)id, wire, len); close_link(t, c, "invalid wire value"); return; diff --git a/lib/ble/tests/test_ble_transport.c b/lib/ble/tests/test_ble_transport.c index 2d3d3d8a..c5bd09f1 100644 --- a/lib/ble/tests/test_ble_transport.c +++ b/lib/ble/tests/test_ble_transport.c @@ -17,6 +17,7 @@ static unsigned int mtu = 23; static int hold, drop, reject_peer, ready_count, writable_count; static int hold_ack; static unsigned ack_timeouts; +static unsigned closed_count; static uint64_t data_wire_bytes[3]; static ble_link_id incoming_ids[3]; static void run(void* arg); @@ -85,6 +86,7 @@ static void run(void* arg) { static int validate(void* arg, ble_link_id id, uint64_t node) { (void)arg; (void)id; (void)node; return !reject_peer; } static void connection(void* arg, ble_link_id id, uint64_t node, enum ble_state s, const char* reason) { (void)arg; (void)id; (void)node; if (s == BLE_READY) ready_count++; + if (s == BLE_CLOSED) closed_count++; if (s == BLE_CLOSED && !strcmp(reason, "ACK progress timeout")) ack_timeouts++; } static void writable(void* arg, ble_link_id id) { (void)arg; (void)id; writable_count++; } @@ -156,7 +158,11 @@ static void exercise(unsigned int test_mtu) { CHECK(!ble_backend_incoming(t[2], &denied)); CHECK(!ble_rx_get(t[2], incoming_ids[2])); ble_set_incoming(t[2], 1); - CHECK(ble_backend_incoming(t[2], &denied)); + ble_link_id early = ble_backend_incoming(t[2], &denied); CHECK(early); + uint8_t early_hello[19] = {0, 1, 1}; early_hello[10] = 1; early_hello[18] = 3; + unsigned before_closed = closed_count; + ble_backend_received(t[2], early, BLE_WIRE_DATA, early_hello, sizeof(early_hello)); + CHECK(closed_count == before_closed + 1 && ready_count == 4); for (int i = 0; i < 3; i++) ble_destroy(t[i]); drain(ua); uasync_destroy(ua, 0); }