Skip to content

Commit 41c930d

Browse files
committed
\- HIGH-1 fix: Applied same bit-index correction to wolfsentry_route_init_by_exports (lines 903-904, 920-921) — swapped left_over_bits and (BITS_PER_BYTE - left_over_bits) in
check/clear masks - HIGH-1 test: Added /25 subnet test via wolfsentry_route_insert_by_exports with a dirty low bit to validate masking through the exports path - MEDIUM-1: Added comment clarifying the route pointer remains valid after drop_reference because the table holds its own reference
1 parent 527e588 commit 41c930d

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

src/routes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ static wolfsentry_errcode_t wolfsentry_route_init_by_exports(
900900
int left_over_bits = route_exports->remote.addr_len % BITS_PER_BYTE;
901901
if (left_over_bits) {
902902
byte *remote_lsb = WOLFSENTRY_ROUTE_REMOTE_ADDR(new) + WOLFSENTRY_BITS_TO_BYTES(route_exports->remote.addr_len) - 1;
903-
if (*remote_lsb & (0xffU >> (BITS_PER_BYTE - left_over_bits)))
904-
*remote_lsb = (byte)(*remote_lsb & (0xffU << left_over_bits));
903+
if (*remote_lsb & (0xffU >> left_over_bits))
904+
*remote_lsb = (byte)(*remote_lsb & (0xffU << (BITS_PER_BYTE - left_over_bits)));
905905
}
906906
}
907907

@@ -917,8 +917,8 @@ static wolfsentry_errcode_t wolfsentry_route_init_by_exports(
917917
int left_over_bits = route_exports->local.addr_len % BITS_PER_BYTE;
918918
if (left_over_bits) {
919919
byte *local_lsb = WOLFSENTRY_ROUTE_LOCAL_ADDR(new) + WOLFSENTRY_BITS_TO_BYTES(route_exports->local.addr_len) - 1;
920-
if (*local_lsb & (0xffU >> (BITS_PER_BYTE - left_over_bits)))
921-
*local_lsb = (byte)(*local_lsb & (0xffU << left_over_bits));
920+
if (*local_lsb & (0xffU >> left_over_bits))
921+
*local_lsb = (byte)(*local_lsb & (0xffU << (BITS_PER_BYTE - left_over_bits)));
922922
}
923923
}
924924

tests/unittests.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,9 @@ static int test_static_routes(void) {
23572357
checked_out_route,
23582358
&action_results));
23592359

2360+
/* checked_out_route pointer is still valid -- the route table holds
2361+
* its own reference, so the object is not freed by drop_reference.
2362+
*/
23602363
WOLFSENTRY_EXIT_UNLESS_EXPECTED_FAILURE(
23612364
ITEM_NOT_FOUND,
23622365
wolfsentry_route_event_dispatch_by_route(
@@ -2619,6 +2622,97 @@ static int test_static_routes(void) {
26192622
WOLFSENTRY_EXIT_ON_FALSE(n_deleted == 1);
26202623
}
26212624

2625+
/* test partial-byte subnet masking via the insert_by_exports path
2626+
* (wolfsentry_route_init_by_exports). 192.168.1.0/25 should match
2627+
* 192.168.1.100 but not 192.168.1.200.
2628+
*/
2629+
{
2630+
struct {
2631+
struct wolfsentry_sockaddr sa;
2632+
byte addr_buf[4];
2633+
} dispatch_remote_exp, dispatch_local_exp;
2634+
struct wolfsentry_route_exports exp_route;
2635+
byte exp_remote_addr[4];
2636+
wolfsentry_ent_id_t exp_id;
2637+
2638+
memset(&exp_route, 0, sizeof exp_route);
2639+
memset(&dispatch_remote_exp, 0, sizeof dispatch_remote_exp);
2640+
memset(&dispatch_local_exp, 0, sizeof dispatch_local_exp);
2641+
2642+
exp_route.sa_family = AF_INET;
2643+
exp_route.sa_proto = IPPROTO_TCP;
2644+
exp_route.flags = WOLFSENTRY_ROUTE_FLAG_TCPLIKE_PORT_NUMBERS
2645+
| WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN
2646+
| WOLFSENTRY_ROUTE_FLAG_PENALTYBOXED
2647+
| WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD
2648+
| WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_PORT_WILDCARD
2649+
| WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_PORT_WILDCARD
2650+
| WOLFSENTRY_ROUTE_FLAG_LOCAL_INTERFACE_WILDCARD
2651+
| WOLFSENTRY_ROUTE_FLAG_REMOTE_INTERFACE_WILDCARD;
2652+
2653+
/* 192.168.1.0/25 -- intentionally set a low bit that must be masked */
2654+
memcpy(exp_remote_addr, "\xC0\xA8\x01\x01", 4);
2655+
exp_route.remote_address = exp_remote_addr;
2656+
exp_route.remote.addr_len = 25;
2657+
exp_route.remote.sa_port = 0;
2658+
exp_route.local.addr_len = 0;
2659+
exp_route.local.sa_port = 0;
2660+
2661+
WOLFSENTRY_EXIT_ON_FAILURE(
2662+
wolfsentry_route_insert_by_exports(
2663+
WOLFSENTRY_CONTEXT_ARGS_OUT,
2664+
NULL /* caller_arg */,
2665+
&exp_route,
2666+
&exp_id, &action_results));
2667+
2668+
/* 192.168.1.100 (0x64) -- same /25 subnet, should match */
2669+
dispatch_remote_exp.sa.sa_family = AF_INET;
2670+
dispatch_remote_exp.sa.sa_proto = IPPROTO_TCP;
2671+
dispatch_remote_exp.sa.addr_len = 32;
2672+
memcpy(dispatch_remote_exp.sa.addr, "\xC0\xA8\x01\x64", 4);
2673+
2674+
dispatch_local_exp.sa.sa_family = AF_INET;
2675+
dispatch_local_exp.sa.sa_proto = IPPROTO_TCP;
2676+
dispatch_local_exp.sa.addr_len = 0;
2677+
2678+
WOLFSENTRY_EXIT_ON_FAILURE(
2679+
wolfsentry_route_event_dispatch(
2680+
WOLFSENTRY_CONTEXT_ARGS_OUT,
2681+
&dispatch_remote_exp.sa, &dispatch_local_exp.sa,
2682+
exp_route.flags,
2683+
NULL /* event_label */,
2684+
0 /* event_label_len */,
2685+
NULL /* caller_arg */,
2686+
&route_id, &inexact_matches,
2687+
&action_results));
2688+
WOLFSENTRY_EXIT_ON_FALSE(route_id == exp_id);
2689+
2690+
/* 192.168.1.200 (0xC8) -- different /25, should NOT match */
2691+
memcpy(dispatch_remote_exp.sa.addr, "\xC0\xA8\x01\xC8", 4);
2692+
2693+
WOLFSENTRY_EXIT_ON_FALSE(
2694+
WOLFSENTRY_SUCCESS_CODE_IS(
2695+
wolfsentry_route_event_dispatch(
2696+
WOLFSENTRY_CONTEXT_ARGS_OUT,
2697+
&dispatch_remote_exp.sa, &dispatch_local_exp.sa,
2698+
exp_route.flags,
2699+
NULL /* event_label */,
2700+
0 /* event_label_len */,
2701+
NULL /* caller_arg */,
2702+
&route_id, &inexact_matches,
2703+
&action_results),
2704+
USED_FALLBACK));
2705+
2706+
WOLFSENTRY_EXIT_ON_FAILURE(
2707+
wolfsentry_route_delete_by_id(
2708+
WOLFSENTRY_CONTEXT_ARGS_OUT,
2709+
NULL /* caller_arg */,
2710+
exp_id,
2711+
NULL /* event_label */,
2712+
0 /* event_label_len */,
2713+
&action_results));
2714+
}
2715+
26222716
printf("all subtests succeeded -- %u distinct ents inserted and deleted.\n",wolfsentry->mk_id_cb_state.id_counter);
26232717

26242718
WOLFSENTRY_EXIT_ON_FAILURE(wolfsentry_shutdown(WOLFSENTRY_CONTEXT_ARGS_OUT_EX(&wolfsentry)));

0 commit comments

Comments
 (0)