Reject invalid local router payloads and report delivery failures
This commit is contained in:
1 parent
07bfe69685
commit
e705d1e6e6
2 files changed
+63
-7
No files matched your search
@@ -1026,14 +1026,24 @@ static void router_idle_ack_timer_cb(void* arg) {
|
||||
|
||||
// Доставка сервисной кодограммы в едином формате [svc_id][src][dst][rx_flags][group_id][payload].
|
||||
// Для loopback (dst == self) src и dst оба = self, rx_flags = 0.
|
||||
static void router_deliver_loopback(struct UTUN_INSTANCE* inst, uint64_t group_id, uint8_t svc_id, struct ll_entry* entry) {
|
||||
etcp_recv_fn cb = inst->router_bindings.callbacks[svc_id];
|
||||
if (!cb) { queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
static int router_deliver_loopback(struct UTUN_INSTANCE* inst, uint64_t group_id, uint8_t svc_id, struct ll_entry* entry, int mode) {
|
||||
size_t payload_len = entry->len - 1;
|
||||
if (!payload_len || payload_len > UINT16_MAX - ROUTER_SVC_HDR_SIZE || (mode & ~(ROUTE_CRYPTO_SIGN | ROUTE_CRYPTO_ENCRYPT))) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "router loopback: invalid payload svc=%u bytes=%zu mode=%x", svc_id, payload_len, mode);
|
||||
free_entry(entry); return -1;
|
||||
}
|
||||
etcp_recv_fn cb = inst->router_bindings.callbacks[svc_id];
|
||||
if (!cb) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCPROUTE, "router loopback: no handler svc=%u group=%016llx", svc_id, (unsigned long long)group_id);
|
||||
free_entry(entry); return -1;
|
||||
}
|
||||
struct ll_entry* e = queue_entry_new(0);
|
||||
if (!e) { queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "router loopback: entry allocation failed svc=%u", svc_id); free_entry(entry); return -1; }
|
||||
e->dgram = u_malloc(ROUTER_SVC_HDR_SIZE + payload_len);
|
||||
if (!e->dgram) { queue_entry_free(e); queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
if (!e->dgram) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "router loopback: payload allocation failed svc=%u bytes=%zu", svc_id, payload_len);
|
||||
queue_entry_free(e); free_entry(entry); return -1;
|
||||
}
|
||||
e->dgram[0] = svc_id;
|
||||
memcpy(e->dgram + ROUTER_SVC_SRC_OFF, &inst->node_id, 8);
|
||||
memcpy(e->dgram + ROUTER_SVC_DST_OFF, &inst->node_id, 8);
|
||||
@@ -1044,6 +1054,7 @@ static void router_deliver_loopback(struct UTUN_INSTANCE* inst, uint64_t group_i
|
||||
queue_dgram_free(entry); queue_entry_free(entry);
|
||||
struct ETCP_CONN local = {.instance = inst, .peer_node_id = inst->node_id};
|
||||
cb(&local, e);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Доставка: декодирует (подпись/шифрование) перед вызовом сервисного коллбэка.
|
||||
@@ -1509,8 +1520,7 @@ int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t dst_
|
||||
|
||||
if (dst_node_id == inst->node_id) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCPROUTE, "loopback svc_id=%u len=%zu", svc_id, payload_len);
|
||||
router_deliver_loopback(inst, group_id, svc_id, entry);
|
||||
return 0;
|
||||
return router_deliver_loopback(inst, group_id, svc_id, entry, mode);
|
||||
}
|
||||
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(inst, group_id, dst_node_id, svc_id);
|
||||
|
||||
@@ -1507,11 +1507,57 @@ static int test_nested_loopback(void) {
|
||||
PASS(); return 0;
|
||||
}
|
||||
|
||||
static unsigned loopback_calls;
|
||||
static size_t loopback_expected_bytes;
|
||||
static int loopback_size_error;
|
||||
|
||||
static void loopback_size_handler(struct ETCP_CONN* conn, struct ll_entry* entry) {
|
||||
(void)conn; loopback_calls++;
|
||||
if (entry->len != ROUTER_SVC_HDR_SIZE + loopback_expected_bytes) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "[regression] local delivery length=%u expected=%zu",
|
||||
entry->len, ROUTER_SVC_HDR_SIZE + loopback_expected_bytes);
|
||||
loopback_size_error = 1;
|
||||
} else if (entry->dgram[ROUTER_SVC_FLAGS_OFF]) loopback_size_error = 1;
|
||||
queue_dgram_free(entry); queue_entry_free(entry);
|
||||
}
|
||||
|
||||
static struct ll_entry* local_message(size_t bytes, uint8_t service) {
|
||||
struct ll_entry* e = ll_alloc_lldgram(bytes + 1);
|
||||
if (e) { memset(e->dgram, 0x7c, bytes + 1); e->dgram[0] = service; e->len = bytes + 1; }
|
||||
return e;
|
||||
}
|
||||
|
||||
static int test_loopback_length_and_errors(void) {
|
||||
TEST("self-delivery reports failures and does not wrap the delivered uint16 length");
|
||||
struct UASYNC* ua; struct UTUN_INSTANCE inst; etcp_recv_fn recv_cb;
|
||||
SETUP(); loopback_calls = 0; loopback_size_error = 0;
|
||||
TESTASSERT(!etcp_router_bind(&inst, TEST_SVC_ID, loopback_size_handler));
|
||||
size_t maximum = UINT16_MAX - ROUTER_SVC_HDR_SIZE;
|
||||
loopback_expected_bytes = maximum;
|
||||
int valid = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(maximum, TEST_SVC_ID), 0, 0);
|
||||
loopback_expected_bytes = maximum + 1;
|
||||
int oversized = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(maximum + 1, TEST_SVC_ID), 0, 0);
|
||||
loopback_expected_bytes = 0;
|
||||
int empty = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(0, TEST_SVC_ID), 0, 0);
|
||||
loopback_expected_bytes = 1;
|
||||
int unbound = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(1, TEST_SVC_ID2), 0, 0);
|
||||
int bad_mode = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(1, TEST_SVC_ID), 0, 0x100);
|
||||
TESTASSERT(!etcp_router_require_crypto(&inst, TEST_SVC_ID, ROUTER_FLAG_SIGNED | ROUTER_FLAG_ENCRYPTED));
|
||||
ready_checks = 0; etcp_router_set_receive_ready(&inst, TEST_SVC_ID, receive_ready);
|
||||
int trusted_local = etcp_route_send(&inst, TOPO_GROUP_UTUN, inst.node_id, local_message(1, TEST_SVC_ID), 0, 0);
|
||||
unsigned channels = inst.router_conns->count;
|
||||
TEARDOWN();
|
||||
TESTASSERT(!valid && !trusted_local && oversized < 0 && empty < 0 && unbound < 0 && bad_mode < 0);
|
||||
TESTASSERT(loopback_calls == 2 && !loopback_size_error && !ready_checks && !channels);
|
||||
PASS(); return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Run all tests
|
||||
test_close_group_from_close_callback();
|
||||
test_reopen_from_close_callback();
|
||||
test_nested_loopback();
|
||||
test_loopback_length_and_errors();
|
||||
test_init_destroy();
|
||||
test_bind_unbind();
|
||||
test_conn_get_create();
|
||||
|
||||
Reference in new issue
Block a user