Bug 182401 - [pf] pf state for some IPs reaches 4294967295 suspicously
Summary: [pf] pf state for some IPs reaches 4294967295 suspicously
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 10.1-STABLE
Hardware: Any Any
: Normal Affects Only Me
Assignee: Gleb Smirnoff
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-26 09:40 UTC by oguz
Modified: 2017-07-26 12:11 UTC (History)
10 users (show)

See Also:


Attachments
patch-races-pf-state-tracking.patch (10.71 KB, patch)
2014-11-20 10:05 UTC, johans
no flags Details | Diff
access pf_src_node->states only under pf_srchash lock (7.12 KB, patch)
2015-02-17 21:33 UTC, Gleb Smirnoff
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description oguz 2013-09-26 09:40:00 UTC
I have found one of my NMS monitoring point is blocked with my tested FreeBSD 10  Alpha 2 server.

After inspection, I have found it is blocked because of max-src-conn overload pf rule. However, It is not possible that host to open such high number of states.

When I inspected I have found several other clients are blocked with this router.

# pfctl -sS  | grep 4294967295
No ALTQ support in kernel
ALTQ related functions disabled
95.6.50.84 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
188.38.79.212 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
141.0.11.129 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
95.10.221.139 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
212.252.119.108 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
198.72.108.244 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
198.72.108.244 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
46.1.140.55 -> 0.0.0.0 ( states 4294967294, connections 4294967295, rate 0.0/3s )
81.214.44.73 -> 0.0.0.0 ( states 4294967295, connections 4294967295, rate 0.0/3s )
46.197.233.175 -> 0.0.0.0 ( states 4294967289, connections 4294967295, rate 0.0/3s )
78.177.41.73 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
95.0.207.25 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )

However, in real the host only has 5 states:

[root@myhost ~]# pfctl -ss  | grep 95.6.50.84
No ALTQ support in kernel
ALTQ related functions disabled
all tcp 95.6.50.84:3881 -> 94.102.10.229:80       ESTABLISHED:ESTABLISHED
all tcp 95.6.50.84:3759 -> 94.102.10.229:80       ESTABLISHED:ESTABLISHED
all tcp 95.6.50.84:3882 -> 94.102.10.229:80       ESTABLISHED:ESTABLISHED
all tcp 95.6.50.84:3849 -> 94.102.10.229:80       ESTABLISHED:ESTABLISHED
all tcp 95.6.50.84:3828 -> 94.102.10.229:80       ESTABLISHED:ESTABLISHED

Fix: 

None.
How-To-Repeat: When I flush all states, in a few minutes several other 4294967295-states appears.
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2013-09-29 22:42:44 UTC
Responsible Changed
From-To: freebsd-amd64->freebsd-pf

reclassify.
Comment 2 oguz 2014-10-29 11:25:25 UTC
This bug continues on 10 release.

Sample rule creating such entries:

@60 pass in log (all) quick on ix0 proto tcp from any to <protectzone_table:4> port = https flags S/SA keep state (source-track rule, max-src-conn 800, max-src-conn-rate 4000/3, overload <tcp_syn_https> flush global, src.track 3)
  [ Evaluations: 31341832  Packets: 2368992857  Bytes: 1561551774393  States: 4294956463]
  [ Inserted: uid 0 pid 9899 State Creations: 29985791]
Comment 3 Jason Unovitch freebsd_committer freebsd_triage 2014-10-31 01:07:23 UTC
May be fixed in STABLE for 10.1.  Take a look at https://svnweb.freebsd.org/base?view=revision&revision=263029
Comment 4 oguz 2014-10-31 13:10:02 UTC
 We have tried with 10.1 rc3 . The problem exist also in this version.

 172.16.2.2 -> 0.0.0.0 ( states 4294967295, connections 0, rate 0.0/3s )
Comment 5 johans 2014-11-18 16:51:14 UTC
I can confirm that we are seeing the same issues on both 10.0 and 10.1 on a DNS cluster. We've had to disable source-tracking to prevent random ip's from being blocked.

When we clear the state tracking via pfcsl -FS a blocked IP immediately starts working again.
Comment 6 johans 2014-11-18 19:19:19 UTC
The possible commit mentioned earlier in this thread only fixes counters for rules. When I take a look at 'sys/net/pfvar.h' in releng/10.1 the struct 'pf_src_node' is still a u_int32_t for states and conn here.

Refactoring these and related code to counter_u64_t would probably fix this issue.
Comment 7 johans 2014-11-20 10:05:57 UTC
Created attachment 149638 [details]
patch-races-pf-state-tracking.patch

glebius@ was kind enough to send us his work in progress on this bug.

We created a patch (attached) for ourselves which combines the use of counter(9) with the work done by glebius@. Now that these states are protected by the PF_STATE_LOCK the counter(9) changes are probably no longer needed, but leaving them in place definitely won't make things worse.

For those looking to quickly fix this issue, we are running 10.1-RELEASE with this patch now and can confirm we are no longer seeing the problems at hand.
Comment 8 Gleb Smirnoff freebsd_committer freebsd_triage 2015-02-17 21:33:07 UTC
Created attachment 153124 [details]
access pf_src_node->states only under pf_srchash lock

Hi! Can you please backout your current patch, that you derived from my WIP, and instead try out this patch?
Comment 9 Marcus von Appen freebsd_committer freebsd_triage 2015-02-18 11:54:22 UTC
Updated 10.1-BETA and 10.1-RC versioned bugs to 10.1-STABLE.
Comment 10 kredaxx 2015-03-17 11:53:05 UTC
(In reply to Gleb Smirnoff from comment #8)
Hi,

I had a bunch of rules following this pattern:

table <ip_addresses> { IP1, IP2, IP3, ... }
pass in quick on $lanif from <ip_addresses> to any keep state (source-track rule, max-src-conn 500, max-src-conn-rate 100/1)

There wasn't a day without several IPs getting blocked.

I have been running the patch for around 5 days now without any issues thus far on 2 systems:

FreeBSD freebsd-lms 10.1-STABLE FreeBSD 10.1-STABLE #4 r279781M: (Around 250K states in peak times)
FreeBSD freebsd-zew 10.1-RELEASE-p5 FreeBSD 10.1-RELEASE-p5 (Around 30K states in peak times)
Comment 11 Gleb Smirnoff freebsd_committer freebsd_triage 2015-03-17 11:58:21 UTC
Good! Since Johan is also in progress of testing, I will put the patch to head today. Thanks to all involved. Keep me informed if anything goes wrong.
Comment 12 commit-hook freebsd_committer freebsd_triage 2015-03-17 12:20:18 UTC
A commit references this bug:

Author: glebius
Date: Tue Mar 17 12:19:30 UTC 2015
New revision: 280169
URL: https://svnweb.freebsd.org/changeset/base/280169

Log:
  Always lock the hash row of a source node when updating its 'states' counter.

  PR:		182401
  Sponsored by:	Nginx, Inc.

Changes:
  head/sys/net/pfvar.h
  head/sys/netpfil/pf/pf.c
  head/sys/netpfil/pf/pf_ioctl.c
Comment 13 johans 2015-04-06 07:19:12 UTC
(In reply to kredaxx from comment #10)

We've been seeing random reboots on our systems, and we had a panic on our systems which was:

panic: pf_hashsrc: unknown address family 0
cpuid = 2

#0  doadump (textdump=<value optimized out>) at pcpu.h:219
#1  0xffffffff80927d82 in kern_reboot (howto=260) at /usr/src/sys/kern/kern_shutdown.c:452
#2  0xffffffff80928144 in panic (fmt=<value optimized out>) at /usr/src/sys/kern/kern_shutdown.c:759
#3  0xffffffff81c16bf5 in pf_src_tree_remove_state (s=<value optimized out>) at /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:400
#4  0xffffffff81c164f9 in pf_unlink_state (s=0xfffff8001ad28a68, flags=<value optimized out>) at /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1579
#5  0xffffffff81c160fd in pf_purge_expired_states (i=<value optimized out>, maxcheck=<value optimized out>) at /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1631
#6  0xffffffff81c15c95 in pf_purge_thread (v=<value optimized out>) at /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1428
#7  0xffffffff808f8b6a in fork_exit (callout=0xffffffff81c15bc0 <pf_purge_thread>, arg=0x0, frame=0xfffffe03dd053c00) at /usr/src/sys/kern/kern_fork.c:996
#8  0xffffffff80d0aede in fork_trampoline () at /usr/src/sys/amd64/amd64/exception.S:606
#9  0x0000000000000000 in ?? ()
Current language:  auto; currently minimal


Have you been seeing anything similar?

I'm uncertain at this moment wether this is related to this patch or completely unrelated. If you are seeing similar problems would greatly help finding out more details.
Comment 14 kredaxx 2015-04-06 11:50:07 UTC
(In reply to johans from comment #13)

Hi.

No I have not noticed any such problems:

Dell PowerEdge R610 Hyper-Threading off.
FreeBSD freebsd-lms 10.1-STABLE FreeBSD 10.1-STABLE #4 r279781M: (Around 250K states in peak times and 750Mb/s, about 1700 users)

- Uptime 11 days. (Had to reboot it once for maintenence. (But was previously working for 11 days with the patch).

Dell T3400 Hyper-Threading off.
FreeBSD freebsd-zew 10.1-RELEASE-p5 FreeBSD 10.1-RELEASE-p5 (Around 30K states in peak times, 40Mb/s, a handful of users)

- Uptime 22 days with the patch.

I will of course report if anything serious like a crash would occur.
Comment 15 Glen Barber freebsd_committer freebsd_triage 2015-07-08 16:52:07 UTC
Gleb,

Is this something that should be merged to stable/10 before 10.2-RELEASE?
Comment 16 Glen Barber freebsd_committer freebsd_triage 2015-07-08 16:59:58 UTC
(In reply to johans from comment #13)
> (In reply to kredaxx from comment #10)
> 
> We've been seeing random reboots on our systems, and we had a panic on our
> systems which was:
> 
> panic: pf_hashsrc: unknown address family 0
> cpuid = 2
> 
> #0  doadump (textdump=<value optimized out>) at pcpu.h:219
> #1  0xffffffff80927d82 in kern_reboot (howto=260) at
> /usr/src/sys/kern/kern_shutdown.c:452
> #2  0xffffffff80928144 in panic (fmt=<value optimized out>) at
> /usr/src/sys/kern/kern_shutdown.c:759
> #3  0xffffffff81c16bf5 in pf_src_tree_remove_state (s=<value optimized out>)
> at /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:400
> #4  0xffffffff81c164f9 in pf_unlink_state (s=0xfffff8001ad28a68,
> flags=<value optimized out>) at
> /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1579
> #5  0xffffffff81c160fd in pf_purge_expired_states (i=<value optimized out>,
> maxcheck=<value optimized out>) at
> /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1631
> #6  0xffffffff81c15c95 in pf_purge_thread (v=<value optimized out>) at
> /usr/src/sys/modules/pf/../../netpfil/pf/pf.c:1428
> #7  0xffffffff808f8b6a in fork_exit (callout=0xffffffff81c15bc0
> <pf_purge_thread>, arg=0x0, frame=0xfffffe03dd053c00) at
> /usr/src/sys/kern/kern_fork.c:996
> #8  0xffffffff80d0aede in fork_trampoline () at
> /usr/src/sys/amd64/amd64/exception.S:606
> #9  0x0000000000000000 in ?? ()
> Current language:  auto; currently minimal
> 
> 
> Have you been seeing anything similar?
> 
> I'm uncertain at this moment wether this is related to this patch or
> completely unrelated. If you are seeing similar problems would greatly help
> finding out more details.

Have you run into this panic again since using the patch?
Comment 17 kredaxx 2015-07-13 17:34:46 UTC
Hi. At least on my machines I had no issues since I applied this patch. Uptimes are approx. 120 days so far.
Comment 18 Renato Botelho freebsd_committer freebsd_triage 2015-07-23 16:35:56 UTC
(In reply to Glen Barber from comment #16)
We are using this patch for some time in pfSense, and a user reported the same issue today.
Comment 19 Glen Barber freebsd_committer freebsd_triage 2015-07-23 16:37:50 UTC
(In reply to Renato Botelho from comment #18)
> (In reply to Glen Barber from comment #16)
> We are using this patch for some time in pfSense, and a user reported the
> same issue today.

Once Gleb returns from being "away", we'll address this in 10.x, hopefully for inclusion in 10.2-RELEASE.
Comment 20 Renato Botelho freebsd_committer freebsd_triage 2015-07-23 17:47:28 UTC
(In reply to Glen Barber from comment #19)
Great! We are working to collect relevant data from pfSense user that got the issue
Comment 21 jimp 2015-07-23 18:03:32 UTC
Details from the box that hit the panic today:

panic: pf_hashsrc: unknown address family 0
cpuid = 0
KDB: enter: panic

db:0:kdb.enter.default> show pcpu
cpuid = 0
dynamic pcpu = 0x63a800
curthread = 0xfffff80003468920: pid 5 "pf purge"
curpcb = 0xfffffe0000246cc0
fpcurthread = none
idlethread = 0xfffff80003211000: tid 100003 "idle: cpu0"
curpmap = 0xffffffff82181fd8
tssp = 0xffffffff8219cf90
commontssp = 0xffffffff8219cf90
rsp0 = 0xfffffe0000246cc0
gs32p = 0xffffffff8219e9e8
ldt = 0xffffffff8219ea28
tss = 0xffffffff8219ea18
db:0:kdb.enter.default> bt
Tracing pid 5 tid 100043 td 0xfffff80003468920
kdb_enter() at kdb_enter+0x3e/frame 0xfffffe0000246a00
panic() at panic+0x175/frame 0xfffffe0000246a80
pf_src_tree_remove_state() at pf_src_tree_remove_state+0x292/frame 0xfffffe0000246ab0
pf_unlink_state() at pf_unlink_state+0x147/frame 0xfffffe0000246b20
pf_purge_expired_states() at pf_purge_expired_states+0x7d/frame 0xfffffe0000246b80
pf_purge_thread() at pf_purge_thread+0xc6/frame 0xfffffe0000246bb0
fork_exit() at fork_exit+0x9a/frame 0xfffffe0000246bf0
fork_trampoline() at fork_trampoline+0xe/frame 0xfffffe0000246bf0
--- trap 0, rip = 0, rsp = 0xfffffe0000246cb0, rbp = 0 ---

The system in question has:
* A rule with "max-src-conn-rate 150 /3, overload <virusprot> flush global"
* set limit src-nodes 1900000
* set timeout src.track 14400
* relayd running with "sticky-address" set in a redirect
Comment 22 johans 2015-07-28 07:46:19 UTC
So far I haven't seen this panic on my machine anymore, but we are running a few more patches that are in upstream stable/10. (https://svnweb.freebsd.org/base?view=revision&revision=282679 which was causing us all sorts of trouble, in general the amount of panics we have seen since this patch has greatly reduced)

What kind of machine are you running on which you received the panic? Could this be a virtual machine by any chance?
Comment 23 commit-hook freebsd_committer freebsd_triage 2015-07-28 09:14:01 UTC
A commit references this bug:

Author: glebius
Date: Tue Jul 28 09:13:57 UTC 2015
New revision: 285940
URL: https://svnweb.freebsd.org/changeset/base/285940

Log:
  Merge 280169: always lock the hash row of a source node when updating
  its 'states' counter.

  PR:		182401

Changes:
_U  stable/10/
  stable/10/sys/net/pfvar.h
  stable/10/sys/netpfil/pf/pf.c
  stable/10/sys/netpfil/pf/pf_ioctl.c
Comment 24 Gleb Smirnoff freebsd_committer freebsd_triage 2015-07-28 09:19:00 UTC
I have strong belief that reported panics are irrelevant to the patch, while the patch has strong evidence of fixing the problem. That's why I'm merging the patch to 10 and closing the bug.

The discussion of panics not related to the problem should continue in a new bug.
Comment 25 Renato Botelho freebsd_committer freebsd_triage 2015-07-28 13:30:04 UTC
(In reply to johans from comment #22)

Did you get that panic running the patch you originally submitted to this bug or the one glebius has committed? We were using the first version you submitted on pfSense when we got that panic.
Comment 26 johans 2015-07-28 13:40:06 UTC
I was using the version from glebius when we got that panic. I'll e-mail you outside this bug report, maybe we can come to some mutual information without polluting this bug report.
Comment 27 commit-hook freebsd_committer freebsd_triage 2015-07-29 14:17:26 UTC
A commit references this bug:

Author: glebius
Date: Wed Jul 29 14:16:27 UTC 2015
New revision: 286014
URL: https://svnweb.freebsd.org/changeset/base/286014

Log:
  Merge r285939-285941,285943,286004 from stable/10:
  - Protect against ioctl() vs ioctl() races.
  - Always lock hash row of a source node when updating
    its 'states' counter. [1]
  - Don't dereference NULL is pf_get_mtag() fails. [2]
  - During module unload drop locks before destroying UMA zone.

  PR:		182401 [1]
  PR:		200222 [2]
  Approved by:	re (gjb)

Changes:
_U  releng/10.2/
  releng/10.2/sys/net/pfvar.h
  releng/10.2/sys/netpfil/pf/pf.c
  releng/10.2/sys/netpfil/pf/pf_ioctl.c
Comment 28 frank 2017-07-26 12:11:03 UTC
Hi,

I am using FreeBSD 11.0-RELEASE-p10 with pf as a firewall for jails running on 127.0.3.0/24.

In pf.conf I configured rdr and pass for http as following:

...
rdr proto tcp from any to $ext_ip port { 80, 443 } -> 127.0.3.3
...
pass in on $ext_if proto tcp from any to 127.0.3.3 port { 80, 443 } flags S/SA keep state (max-src-conn 100, max-src-conn-rate 100/3, overload <abusive_hosts> flush)
...


In pf state output I see somtimes:

...
all tcp 127.0.3.3:80 (138.201.149.13:80) <- 178.24.68.177:55063       FIN_WAIT_2:FIN_WAIT_2
all tcp 127.0.3.3:80 (138.201.149.13:80) <- 178.24.68.177:55064       FIN_WAIT_2:FIN_WAIT_2
all tcp 127.0.3.3:80 (138.201.149.13:80) <- 178.24.68.177:55068       FIN_WAIT_2:FIN_WAIT_2
...
178.24.68.177 -> 0.0.0.0 ( states 3, connections 4294967287, rate 0.0/3s )
...


So I think that the bug is not fixed in FreeBSD 11. I had to remove "max-src-conn 100" from rule. Otherwise I have blocked IPs by mistake.