V-72905

Severity: Medium

Generated

2019-05-20 15:48:11.984914

Status

Failed

Execution of software modules (to include functions and trigger procedures) with elevated privileges must be restricted to necessary cases only.

NIST 800-53

STIG # Description Result
AC-6 AC-6: Least Privilege failed

Guidance

In certain situations, to provide required functionality, PostgreSQL needs to execute internal logic (stored procedures, functions, triggers, etc.) and/or external code modules with elevated privileges. However, if the privileges required for execution are at a higher level than the privileges assigned to organizational users invoking the functionality applications/programs, those users are indirectly provided with greater privileges than assigned by organizations. Privilege elevation must be utilized only where necessary and protected from misuse. This calls for inspection of application source code, which will require collaboration with the application developers. It is recognized that in many cases, the database administrator (DBA) is organizationally separate from the application developers, and may have limited, if any, access to source code. Nevertheless, protections of this type are so important to the secure operation of databases that they must not be ignored. At a minimum, the DBA must attempt to obtain assurances from the development organization that this issue has been addressed, and must document what has been discovered.

Check

Functions in PostgreSQL can be created with the SECURITY DEFINER option. When SECURITY DEFINER functions are executed by a user, said function is run with the privileges of the user who created it. To list all functions that have SECURITY DEFINER, as, the database administrator (shown here as “postgres”), run the following SQL: $ sudo su - postgres $ psql -c “SELECT nspname, proname, proargtypes, prosecdef, rolname, proconfig FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef OR NOT proconfig IS NULL;” In the query results, a prosecdef value of “t” on a row indicates that that function uses privilege elevation. If elevation of PostgreSQL privileges is utilized but not documented, this is a finding. If elevation of PostgreSQL privileges is documented, but not implemented as described in the documentation, this is a finding. If the privilege-elevation logic can be invoked in ways other than intended, or in contexts other than intended, or by subjects/principals other than intended, this is a finding.

Fix

Determine where, when, how, and by what principals/subjects elevated privilege is needed. To change a SECURITY DEFINER function to SECURITY INVOKER, as the database administrator (shown here as “postgres”), run the following SQL: $ sudo su - postgres $ psql -c “ALTER FUNCTION SECURITY INVOKER;”

Test Results

  Result
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" passed
PostgreSQL query with errors: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should match /FATAL:\s+database "template0" is not currently accepting connections/ passed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" passed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" passed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" failed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should match /FATAL:\s+database "replicationdb" is not currently accepting connections/ failed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" passed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" failed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should match /FATAL:\s+database "testdb" is not currently accepting connections/ failed
PostgreSQL query: SELECT nspname, proname, prosecdef FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't'; output should eq "" passed

Code

control "V-72905" do
  title "Execution of software modules (to include functions and trigger
  procedures) with elevated privileges must be restricted to necessary cases
  only."
  desc  "In certain situations, to provide required functionality, PostgreSQL
  needs to execute internal logic (stored procedures, functions, triggers, etc.)
  and/or external code modules with elevated privileges. However, if the
  privileges required for execution are at a higher level than the privileges
  assigned to organizational users invoking the functionality
  applications/programs, those users are indirectly provided with greater
  privileges than assigned by organizations.
  Privilege elevation must be utilized only where necessary and protected
  from misuse.
  This calls for inspection of application source code, which will require
  collaboration with the application developers. It is recognized that in
  many cases, the database administrator (DBA) is organizationally separate
  from the application developers, and may have limited, if any, access to
  source code. Nevertheless, protections of this type are so important to the
  secure operation of databases that they must not be ignored. At a minimum,
  the DBA must attempt to obtain assurances from the development organization
  that this issue has been addressed, and must document what has been discovered."
  impact 0.5
  tag "severity": "medium"
  tag "gtitle": "SRG-APP-000342-DB-000302"
  tag "gid": "V-72905"
  tag "rid": "SV-87557r1_rule"
  tag "stig_id": "PGS9-00-003600"
  tag "cci": "CCI-002233"
  tag "nist": ["AC-6 (8)", "Rev_4"]
  tag "check": "Functions in PostgreSQL can be created with the SECURITY
  DEFINER option. When SECURITY DEFINER functions are executed by a user, said
  function is run with the privileges of the user who created it.
  To list all functions that have SECURITY DEFINER, as, the database
  administrator (shown here as \"postgres\"), run the following SQL:
  $ sudo su - postgres
  $ psql -c \"SELECT nspname, proname, proargtypes, prosecdef, rolname,
  proconfig FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid JOIN
  pg_authid a ON a.oid = p.proowner WHERE prosecdef OR NOT proconfig IS NULL;\"
  In the query results, a prosecdef value of \"t\" on a row indicates that that
  function uses privilege elevation.
  If elevation of PostgreSQL privileges is utilized but not documented, this is
  a finding.
  If elevation of PostgreSQL privileges is documented, but not implemented as
  described in the documentation, this is a finding.
  If the privilege-elevation logic can be invoked in ways other than intended,
  or in contexts other than intended, or by subjects/principals other than
  intended, this is a finding."
  tag "fix": "Determine where, when, how, and by what principals/subjects
  elevated privilege is needed.
  To change a SECURITY DEFINER function to SECURITY INVOKER, as the database
  administrator (shown here as \"postgres\"), run the following SQL:\
  $ sudo su - postgres
  $ psql -c \"ALTER FUNCTION <function_name> SECURITY INVOKER;\""

  sql = postgres_session(PG_DBA, PG_DBA_PASSWORD, PG_HOST)

  security_definer_sql = "SELECT nspname, proname, prosecdef "\
    "FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid "\
    "JOIN pg_authid a ON a.oid = p.proowner WHERE prosecdef = 't';"

  databases_sql = 'SELECT datname FROM pg_catalog.pg_database;'
  databases_query = sql.query(databases_sql, [PG_DB])
  databases = databases_query.lines

  databases.each do |database|
    connection_error = "FATAL:\\s+database \"#{database}\" is not currently "\
      "accepting connections"
    connection_error_regex = Regexp.new(connection_error)

    describe.one do
      describe sql.query(security_definer_sql, [database]) do
        its('output') { should eq '' }
      end

      describe sql.query(security_definer_sql, [database]) do
        it { should match connection_error_regex }
      end
    end
  end
end