V-72941

Severity: Medium

Generated

2019-05-20 15:48:11.984914

Status

Passed

PostgreSQL must generate audit records when unsuccessful attempts to retrieve privileges/permissions occur.

NIST 800-53

STIG # Description Result
AU-12 AU-12: Audit Generation failed

Guidance

Under some circumstances, it may be useful to monitor who/what is reading privilege/permission/role information. Therefore, it must be possible to configure auditing to do this. PostgreSQLs typically make such information available through views or functions. This requirement addresses explicit requests for privilege/permission/role membership information. It does not refer to the implicit retrieval of privileges/permissions/role memberships that PostgreSQL continually performs to determine if any and every action on the database is permitted. To aid in diagnosis, it is necessary to keep track of failed attempts in addition to the successful ones.

Check

Note: The following instructions use the PGDATA environment variable. See supplementary content APPENDIX-F for instructions on configuring PGDATA. First, as the database administrator (shown here as “postgres”), create a role ‘bob’ by running the following SQL: $ sudo su - postgres $ psql -c “CREATE ROLE bob” Next, attempt to retrieve information from the pg_authid table: $ psql -c “SET ROLE bob; SELECT * FROM pg_authid” Now, as the database administrator (shown here as “postgres”), verify the event was logged in pg_log: $ sudo su - postgres $ cat ${PGDATA?}/pg_log/ < 2016-07-13 16:49:58.864 EDT postgres postgres ERROR: > permission denied for relation pg_authid < 2016-07-13 16:49:58.864 EDT postgres postgres STATEMENT: > SELECT * FROM pg_authid; If the above steps cannot verify that audit records are produced when PostgreSQL denies retrieval of privileges/permissions/role memberships, this is a finding.

Fix

Configure PostgreSQL to produce audit records when unsuccessful attempts to access privileges occur. All denials are logged if logging is enabled. To ensure that logging is enabled, review supplementary content APPENDIX-C for instructions on enabling logging.

Test Results

  Result
Command: `sed -nre '/2019-05-16 08:11.*LOG:\s+starting tests for V-72941/,$p' /var/vcap/sys/log/postgresql/Thu.pg_log` stdout should match /ERROR:\s+permission denied for (relation|table) pg_authid/ passed
Command: `sed -nre '/2019-05-16 08:11.*LOG:\s+starting tests for V-72941/,$p' /var/vcap/sys/log/postgresql/Thu.pg_log` stdout should match /STATEMENT:\s+SET\ ROLE\ inspec_v72941_role;\ SELECT\ \*\ FROM\ pg_authid;/ passed

Code

control "V-72941" do
  title "PostgreSQL must generate audit records when unsuccessful attempts to
  retrieve privileges/permissions occur."
  desc  "Under some circumstances, it may be useful to monitor who/what is
  reading privilege/permission/role information. Therefore, it must be possible
  to configure auditing to do this. PostgreSQLs typically make such information
  available through views or functions.
  This requirement addresses explicit requests for privilege/permission/role
  membership information. It does not refer to the implicit retrieval of
  privileges/permissions/role memberships that PostgreSQL continually performs
  to determine if any and every action on the database is permitted.
  To aid in diagnosis, it is necessary to keep track of failed attempts in
  addition to the successful ones."
  impact 0.5
  tag "severity": "medium"
  tag "gtitle": "SRG-APP-000091-DB-000325"
  tag "gid": "V-72941"
  tag "rid": "SV-87593r1_rule"
  tag "stig_id": "PGS9-00-005300"
  tag "cci": "CCI-000172"
  tag "nist": ["AU-12 c", "Rev_4"]
  tag "check": "Note: The following instructions use the PGDATA environment
  variable. See supplementary content APPENDIX-F for instructions on configuring
  PGDATA.
  First, as the database administrator (shown here as \"postgres\"), create a
  role 'bob' by running the following SQL:
  $ sudo su - postgres
  $ psql -c \"CREATE ROLE bob\"
  Next, attempt to retrieve information from the pg_authid table:
  $ psql -c \"SET ROLE bob; SELECT * FROM pg_authid\"
  Now, as the database administrator (shown here as \"postgres\"), verify the
  event was logged in pg_log:
  $ sudo su - postgres
  $ cat ${PGDATA?}/pg_log/<latest_log>
  < 2016-07-13 16:49:58.864 EDT postgres postgres ERROR: > permission denied for
  relation pg_authid
  < 2016-07-13 16:49:58.864 EDT postgres postgres STATEMENT: >
  SELECT * FROM pg_authid;
  If the above steps cannot verify that audit records are produced when
  PostgreSQL denies retrieval of privileges/permissions/role memberships, this
  is a finding."
  tag "fix": "Configure PostgreSQL to produce audit records when unsuccessful
  attempts to access privileges occur.
  All denials are logged if logging is enabled. To ensure that logging is
  enabled, review supplementary content APPENDIX-C for instructions on enabling
  logging."

  sql = postgres_session(PG_DBA, PG_DBA_PASSWORD, PG_HOST)
  log_directory_query = sql.query('SHOW log_directory;', [PG_DB])
  log_directory = log_directory_query.output
  current_log_command = "ls -1t #{log_directory}/*.pg_log | head -1"
  current_log = command(current_log_command).stdout.strip
  control = File.basename(__FILE__, File.extname(__FILE__))
  message = "starting tests for #{control}"
  message_sql = "DO language plpgsql $$ BEGIN "\
    "RAISE LOG '#{message}'; END $$;"
  start = Time.now.strftime('%Y-%m-%d %H:%M')
  get_logs = "sed -nre '/#{start}.*LOG:\\s+#{message}/,$p' #{current_log}"

  create_role =  'CREATE ROLE bob;'
  drop_role =  'DROP ROLE bob;'
  command = 'SET ROLE bob; SELECT * FROM pg_authid;'
  error = 'permission denied for relation pg_authid'

  sql.query(message_sql, [PG_DB])
  sql.query(create_role, [PG_DB])
  sql.query(command, [PG_DB])
  sql.query(drop_role, [PG_DB])

  describe command(get_logs) do
    its('stdout') { should match /ERROR:\s+#{error}/ }
    its('stdout') { should match /STATEMENT:\s+#{Regexp.escape(command)}/ }
  end
end