PostgreSQL public exposure risk via listen_addresses and pg_hba.conf mismatch
0 reputation · 29 Aug 2022, 12:04 UTC
0 reputation · 29 Aug 2022, 12:04 UTC
listen_addresses setting in postgresql.conf and the host-based authentication in pg_hba.conf. While listen_addresses = '*' enables the server to bind to all network interfaces, the security posture relies entirely on the HBA file to restrict access.
A common vulnerability arises when a server is exposed to a public--facing interface, but the pg_hba.conf rules are overly permissive, such as using the trust method or overly wide CIDR blocks. Even with strict rules, the database remains vulnerable to unauthorized connection attempts if the OS-level firewall is bypassed or misconfigured.
How does the engine prioritize pg_hba.conf entries when multiple interfaces are defined in listen_addresses? Furthermore, are there specific configuration patterns to ensure that a public-facing binding cannot accidentally grant access if a specific HBA rule is missing or incomplete?
35275 reputation · 29 Aug 2022, 21:55 UTC
pg_hba.conf from top to bottom; the first line whose host, database, user, and address fields match the incoming connection is used.listen_addresses setting only determines which IP addresses the server will accept connections on; it does not influence the order or selection of pg_hba.conf rules.Because the engine treats all incoming connections the same regardless of which network interface they arrived on, there is no interface‑based prioritization of HBA entries. The security posture therefore depends entirely on the ordering and content of pg_hba.conf.
listen_addresses to the specific interfaces you need (e.g., listen_addresses = 'localhost, 203.0.113.5') rather than * unless absolutely required.hostssl (or hostnossl if SSL is not used) rules at the top of pg_hba.conf for those interfaces, using strong authentication methods such as md5 or scram-sha-256.host all all 0.0.0.0/0 reject.ssl = on) and configure a valid server certificate; then use hostssl rules to enforce encrypted connections.pg_ctl reload or SELECT pg_reload_conf();) and verify the active settings with SHOW listen_addresses; and SELECT * FROM pg_hba_file_rules;.If SSL is not currently enabled on your instance, please confirm whether you can enable it; without SSL the recommendation shifts to relying on firewall restrictions and ensuring that no broad trust or wide CIDR rules appear above your specific interface rules.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.