On Mon, Jul 20, 2015 at 04:27:45AM +0900, Ryan McBride wrote:
> ok mcbride@
> 
err....

I took a look at the patch one more time. I've realized PF must bind the rules
to state before STATE_INC_COUNTERS() gets called.  Not doing so makes PF to
play games with dangling pointers to rule from state. State still references
rules via match_rules list, but it has not actually bumped states_cur for
them. 

from caller point of view the PF_SYNPROXY_DROP actually means success.
we have a state in table, but want still drop the packet as we are proxying
handshake.

updated patch is below.

sorry for extra round trip...
regards
sasha

---------8<----------------8<----------------8<---------------8<--------

Index: pf.c
===================================================================
RCS file: /cvs/src/sys/net/pf.c,v
retrieving revision 1.931
diff -u -p -r1.931 pf.c
--- pf.c        19 Jul 2015 05:48:11 -0000      1.931
+++ pf.c        19 Jul 2015 21:08:31 -0000
@@ -3068,6 +3068,7 @@ pf_test_rule(struct pf_pdesc *pd, struct
        int                      state_icmp = 0, icmp_dir = 0;
        u_int16_t                virtual_type, virtual_id;
        u_int8_t                 icmptype = 0, icmpcode = 0;
+       int                      action = PF_DROP;
 
        bzero(&act, sizeof(act));
        bzero(sns, sizeof(sns));
@@ -3351,7 +3352,6 @@ pf_test_rule(struct pf_pdesc *pd, struct
 
        if (pd->virtual_proto != PF_VPROTO_FRAGMENT
            && !state_icmp && r->keep_state) {
-               int action;
 
                if (r->rule_flag & PFRULE_SRCTRACK &&
                    pf_insert_src_node(&sns[PF_SN_NONE], r, PF_SN_NONE, pd->af,
@@ -3370,7 +3370,7 @@ pf_test_rule(struct pf_pdesc *pd, struct
                    sm, tag, &rules, &act, sns);
 
                if (action != PF_PASS)
-                       return (action);
+                       goto cleanup;
                if (sks != skw) {
                        struct pf_state_key     *sk;
 
@@ -3428,7 +3428,7 @@ cleanup:
                pool_put(&pf_rule_item_pl, ri);
        }
 
-       return (PF_DROP);
+       return (action);
 }
 
 static __inline int
@@ -3451,7 +3451,6 @@ pf_create_state(struct pf_pdesc *pd, str
        s->rule.ptr = r;
        s->anchor.ptr = a;
        s->natrule.ptr = nr;
-       memcpy(&s->match_rules, rules, sizeof(s->match_rules));
        if (r->allow_opts)
                s->state_flags |= PFSTATE_ALLOWOPTS;
        if (r->rule_flag & PFRULE_STATESLOPPY)
@@ -3580,6 +3579,11 @@ pf_create_state(struct pf_pdesc *pd, str
        } else
                *sm = s;
 
+       /*
+        * Make state responsible for rules it binds here.
+        */
+       memcpy(&s->match_rules, rules, sizeof(s->match_rules));
+       bzero(rules, sizeof(*rules));
        STATE_INC_COUNTERS(s);
 
        if (tag > 0) {

Reply via email to