@@ -520,7 +520,7 @@ static int compare_match_exactness(const struct wolfsentry_route *target, const
520520 } else
521521#endif
522522 {
523- right_match_score = addr_prefix_match_size (WOLFSENTRY_ROUTE_LOCAL_ADDR (target ), WOLFSENTRY_ROUTE_LOCAL_ADDR_BITS (target ), WOLFSENTRY_ROUTE_LOCAL_ADDR (right ), WOLFSENTRY_ROUTE_LOCAL_ADDR_BITS (right ));
523+ right_match_score = addr_prefix_match_size (WOLFSENTRY_ROUTE_REMOTE_ADDR (target ), WOLFSENTRY_ROUTE_REMOTE_ADDR_BITS (target ), WOLFSENTRY_ROUTE_REMOTE_ADDR (right ), WOLFSENTRY_ROUTE_REMOTE_ADDR_BITS (right ));
524524 }
525525 }
526526
@@ -2366,6 +2366,9 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
23662366 wolfsentry_route_flags_t current_rule_route_flags ;
23672367 wolfsentry_errcode_t ret ;
23682368 wolfsentry_time_t now ;
2369+ int penalty_triggered = 0 ;
2370+ wolfsentry_hitcount_t derog_snap ;
2371+ wolfsentry_hitcount_t commend_snap ;
23692372
23702373 if (target_route == NULL )
23712374 WOLFSENTRY_ERROR_RETURN (INVALID_ARG );
@@ -2553,18 +2556,25 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_0(
25532556 }
25542557 }
25552558
2559+ /* Snapshot atomic counts once so the guard and arithmetic operate on the
2560+ * same values (avoid TOCTOU between successive loads). */
2561+ derog_snap = WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count );
2562+ commend_snap = WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .commendable_count );
2563+ if (config -> config .derogatory_threshold_for_penaltybox > 0 ) {
2564+ if (config -> config .flags & WOLFSENTRY_EVENTCONFIG_FLAG_DEROGATORY_THRESHOLD_IGNORE_COMMENDABLE ) {
2565+ penalty_triggered = (derog_snap >= config -> config .derogatory_threshold_for_penaltybox );
2566+ } else {
2567+ penalty_triggered = (derog_snap >= commend_snap )
2568+ && ((derog_snap - commend_snap )
2569+ >= config -> config .derogatory_threshold_for_penaltybox );
2570+ }
2571+ }
2572+
25562573 if (current_rule_route_flags & WOLFSENTRY_ROUTE_FLAG_PENALTYBOXED ) {
25572574 * action_results |= WOLFSENTRY_ACTION_RES_REJECT ;
25582575 ret = WOLFSENTRY_ERROR_ENCODE (OK );
25592576 goto done ;
2560- } else if ((config -> config .derogatory_threshold_for_penaltybox > 0 )
2561- && ((config -> config .flags & WOLFSENTRY_EVENTCONFIG_FLAG_DEROGATORY_THRESHOLD_IGNORE_COMMENDABLE ) ?
2562- (WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count )
2563- >= config -> config .derogatory_threshold_for_penaltybox )
2564- :
2565- (WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .derogatory_count )
2566- - WOLFSENTRY_ATOMIC_LOAD (rule_route -> meta .commendable_count )
2567- >= (int )config -> config .derogatory_threshold_for_penaltybox )))
2577+ } else if (penalty_triggered )
25682578 {
25692579 wolfsentry_route_flags_t flags_before ;
25702580 WOLFSENTRY_WARN_ON_FAILURE (
@@ -2961,6 +2971,10 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_by_route_1(
29612971 goto out ;
29622972 }
29632973
2974+ if (route -> header .parent_table == NULL ) {
2975+ ret = WOLFSENTRY_ERROR_ENCODE (INTERNAL_CHECK_FATAL );
2976+ goto out ;
2977+ }
29642978 if (route -> header .parent_table -> ent_type != WOLFSENTRY_OBJECT_TYPE_ROUTE ) {
29652979 ret = WOLFSENTRY_ERROR_ENCODE (WRONG_OBJECT );
29662980 goto out ;
@@ -3095,7 +3109,7 @@ static wolfsentry_errcode_t wolfsentry_route_stale_purge_1(
30953109 (! (route -> flags & WOLFSENTRY_ROUTE_FLAG_PENDING_DELETE )) &&
30963110 ((table -> max_purgeable_idle_time == 0 ) || (now - route -> meta .last_hit_time > table -> max_purgeable_idle_time )))
30973111 {
3098- continue ;
3112+ break ;
30993113 }
31003114 }
31013115#ifdef WOLFSENTRY_THREADSAFE
@@ -3545,9 +3559,9 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_table_get_default_event(
35453559 char * event_label ,
35463560 int * event_label_len )
35473561{
3548- if (table -> default_event == NULL )
3549- WOLFSENTRY_ERROR_RETURN (ITEM_NOT_FOUND );
35503562 WOLFSENTRY_SHARED_OR_RETURN ();
3563+ if (table -> default_event == NULL )
3564+ WOLFSENTRY_ERROR_UNLOCK_AND_RETURN (ITEM_NOT_FOUND );
35513565 if (table -> default_event -> label_len >= * event_label_len )
35523566 WOLFSENTRY_ERROR_UNLOCK_AND_RETURN (BUFFER_TOO_SMALL );
35533567 memcpy (event_label , table -> default_event -> label , (size_t )(table -> default_event -> label_len + 1 ));
@@ -3776,7 +3790,7 @@ WOLFSENTRY_API int wolfsentry_inet6_ntoa(const byte *addr, unsigned int addr_bit
37763790 int i ;
37773791 const char * start_buf = buf ;
37783792 int this_zerospan_length = 0 ;
3779- int this_zerospan_offset ;
3793+ int this_zerospan_offset = 0 ;
37803794 int longest_zerospan_length = 0 ;
37813795 int longest_zerospan_offset = 0 ;
37823796
@@ -4433,8 +4447,6 @@ static wolfsentry_errcode_t wolfsentry_route_render_address(WOLFSENTRY_CONTEXT_A
44334447 int fmt_buf_len = (int )sizeof (fmt_buf );
44344448 int ret = wolfsentry_inet6_ntoa (addr , addr_bits , fmt_buf , & fmt_buf_len );
44354449 WOLFSENTRY_RERETURN_IF_ERROR (ret );
4436- if (fprintf (f , "%.*s/%u" , fmt_buf_len , fmt_buf , addr_bits ) < 0 )
4437- WOLFSENTRY_ERROR_RETURN (IO_FAILED );
44384450 if (fprintf (f , "[%.*s]/%u" , fmt_buf_len , fmt_buf , addr_bits ) < 0 )
44394451 WOLFSENTRY_ERROR_RETURN (IO_FAILED );
44404452 } else if (sa_family == WOLFSENTRY_AF_LOCAL ) {
@@ -4517,7 +4529,7 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_render_flags(wolfsentry_rou
45174529 } else
45184530 already = 1 ;
45194531 if (rendername == NULL ) {
4520- if (fprintf (stderr , "unk-0x%x" , masked_flags ) < 0 )
4532+ if (fprintf (f , "unk-0x%x" , masked_flags ) < 0 )
45214533 WOLFSENTRY_ERROR_RETURN (IO_FAILED );
45224534 } else {
45234535 if (fputs (rendername , f ) < 0 )
@@ -4536,7 +4548,7 @@ static wolfsentry_errcode_t wolfsentry_route_render_endpoint(WOLFSENTRY_CONTEXT_
45364548 const byte * addr = (sa_local_p ? WOLFSENTRY_ROUTE_LOCAL_ADDR (r ) : WOLFSENTRY_ROUTE_REMOTE_ADDR (r ));
45374549
45384550 if (sa_local_p ? (r -> flags & WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD ) : (r -> flags & WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_ADDR_WILDCARD )) {
4539- if (fputs ("*" , stdout ) < 0 )
4551+ if (fputs ("*" , f ) < 0 )
45404552 WOLFSENTRY_ERROR_RETURN (IO_FAILED );
45414553 }
45424554#ifdef WOLFSENTRY_ADDR_BITMASK_MATCHING
@@ -4642,7 +4654,7 @@ static wolfsentry_errcode_t wolfsentry_route_exports_render_endpoint(WOLFSENTRY_
46424654 const byte * addr = (sa_local_p ? r -> local_address : r -> remote_address );
46434655
46444656 if (sa_local_p ? (r -> flags & WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD ) : (r -> flags & WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_ADDR_WILDCARD )) {
4645- if (fputs ("*" , stdout ) < 0 )
4657+ if (fputs ("*" , f ) < 0 )
46464658 WOLFSENTRY_ERROR_RETURN (IO_FAILED );
46474659 }
46484660#ifdef WOLFSENTRY_ADDR_BITMASK_MATCHING
0 commit comments