The vulnerability exists because the CloudNativePG metrics exporter connects to the PostgreSQL database as the postgres superuser. It then executes SQL queries defined in a ConfigMap to collect metrics. The core of the vulnerability lies in the execution of these queries with elevated privileges.
The provided patches show that the default monitoring queries contained unqualified function and table names (e.g., current_database(), pg_stat_user_tables). This allows a low-privileged user to perform a search path attack. By creating a function or view with the same name in a schema they control, they can trick the metrics exporter into executing arbitrary code with superuser privileges.
The function current_database in config/manager/default-monitoring.yaml is a prime example of this vulnerability. It is part of the default configuration, making any cluster with default monitoring enabled susceptible to this attack. An attacker can shadow this function to gain superuser access and execute commands on the underlying operating system of the pod.
While the patches schema-qualify the names (e.g., pg_catalog.current_database()) to mitigate this specific attack vector, the underlying root cause is the high privilege level of the metrics exporter's database connection. The advisory notes a second patch, not detailed in the provided commits, that addresses this by using a dedicated, non-superuser role for the exporter. The functions identified are the triggers for the exploit, and their presence in a runtime profile during a metrics scrape would be an indicator of compromise if the malicious shadowing object exists.