Bug 230640 - pf_map_addr operates on unlocked src_nodes and pools
Summary: pf_map_addr operates on unlocked src_nodes and pools
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 11.2-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-pf (Nobody)
Keywords: patch
Depends on:
Reported: 2018-08-15 10:39 UTC by Kajetan Staszkiewicz
Modified: 2018-08-16 17:37 UTC (History)
1 user (show)

See Also:

Fix locking issues around pf_map_addr. (26.53 KB, patch)
2018-08-15 10:39 UTC, Kajetan Staszkiewicz
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.