Bug 230640

Summary: pf_map_addr operates on unlocked src_nodes and pools
Product: Base System Reporter: Kajetan Staszkiewicz <vegeta>
Component: kernAssignee: freebsd-pf (Nobody) <pf>
Status: New ---    
Severity: Affects Some People CC: net
Priority: --- Keywords: patch
Version: 11.2-RELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
Fix locking issues around pf_map_addr. none

Description Kajetan Staszkiewicz 2018-08-15 10:39:47 UTC
Created attachment 196212 [details]
Fix locking issues around pf_map_addr.

I came across the following issues with pf_map_addr:

- It operates on unlocked src_nodes and I've seen kernel crashes attributable to this.

- For route-to targets which specify outgoing interface, interface selection happens outside of pf_map_addr and can be taken from different redirection target than was used for IP address selection.

- Hash used for locking and unlocking src_node is calculated multiple times for same src_node.

- Operations on rule->pool are unlocked, there even is a comment in code mentioning circumstances where it can cause kernel crash.


I attached a patch addressing those issues. Please note it is early version, it compiles, it runs, I'm not sure if it is really correct. The patch does the following:

- Struct pf_pool has a lock now. Round-robin algorithm in pf_map_addr locks the pool. Other algorithms don't do it, I don't think they need. Should we also have table operations check this lock?

- pf_find_src_node does not manipulate src_node's state counter. It is a "find" function so why would it manipulate other stuff? Also with new location of sn->states++ removing of unused src_node in pf_create_state will work, I'm pretty sure it was broken before.

- pf_insert_src_node returns locked. The lock is retained across pf_create_state and pf_map_addr.

- pf_insert_src_node returns hash (row) of locked src_node. It is used later for unlocking without recalculating the hash.

- Interface chosen by pf_map_addr is assigned to external variable from within this function. Struct src_node stores the interface.