V-72929

Severity: Medium

Generated

2019-05-20 15:48:11.984914

Status

Passed

PostgreSQL must generate audit records when privileges/permissions are added.

NIST 800-53

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

Guidance

Changes in the permissions, privileges, and roles granted to users and roles must be tracked. Without an audit trail, unauthorized elevation or restriction of privileges could go undetected. Elevated privileges give users access to information and functionality that they should not have; restricted privileges wrongly deny access to authorized users. In an SQL environment, adding permissions is typically done via the GRANT command, or, in the negative, the REVOKE command.

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 by running the following SQL: Change the privileges of another user: $ sudo su - postgres $ psql -c “CREATE ROLE bob” Next, GRANT then REVOKE privileges from the role: $ psql -c “GRANT CONNECT ON DATABASE postgres TO bob” $ psql -c “REVOKE CONNECT ON DATABASE postgres FROM bob” postgres=# REVOKE CONNECT ON DATABASE postgres FROM bob; REVOKE postgres=# GRANT CONNECT ON DATABASE postgres TO bob; GRANT Now, as the database administrator (shown here as “postgres”), verify the events were logged: $ sudo su - postgres $ cat ${PGDATA?}/pg_log/ < 2016-07-13 16:25:21.103 EDT postgres postgres LOG: > AUDIT: SESSION,1,1,ROLE,GRANT,,,GRANT CONNECT ON DATABASE postgres TO bob, < 2016-07-13 16:25:25.520 EDT postgres postgres LOG: > AUDIT: SESSION,1,1,ROLE,REVOKE,,,REVOKE CONNECT ON DATABASE postgres FROM bob, If the above steps cannot verify that audit records are produced when privileges/permissions/role memberships are added, this is a finding.

Fix

Note: The following instructions use the PGDATA environment variable. See supplementary content APPENDIX-F for instructions on configuring PGDATA. Using pgaudit PostgreSQL can be configured to audit these requests. See supplementary content APPENDIX-B for documentation on installing pgaudit. With pgaudit installed the following configurations can be made: $ sudo su - postgres $ vi ${PGDATA?}/postgresql.conf Add the following parameters (or edit existing parameters): pgaudit.log = ‘role’ Now, as the system administrator, reload the server with the new configuration: # SYSTEMD SERVER ONLY $ sudo systemctl reload postgresql-9.5 # INITD SERVER ONLY $ sudo service postgresql-9.5 reload

Test Results

  Result
PostgreSQL query: GRANT CONNECT ON DATABASE stig_test_db TO inspec_v72929_role; output should eq "GRANT" passed
PostgreSQL query: REVOKE CONNECT ON DATABASE stig_test_db FROM inspec_v72929_role; output should eq "REVOKE" passed
Command: `sed -nre '/2019-05-16 08:11.*LOG:\s+starting tests for V-72929/,$p' /var/vcap/sys/log/postgresql/Thu.pg_log` stdout should match /LOG:\s+AUDIT:\s+SESSION,[0-9]+,[0-9]+,ROLE,GRANT(,.*){2},GRANT CONNECT ON DATABASE stig_test_db TO inspec_v72929_role;/ passed
Command: `sed -nre '/2019-05-16 08:11.*LOG:\s+starting tests for V-72929/,$p' /var/vcap/sys/log/postgresql/Thu.pg_log` stdout should match /LOG:\s+AUDIT:\s+SESSION,[0-9]+,[0-9]+,ROLE,REVOKE(,.*){2},REVOKE CONNECT ON DATABASE stig_test_db FROM inspec_v72929_role;/ passed

Code

control "V-72929" do
  title "PostgreSQL must generate audit records when privileges/permissions are
  added."
  desc  "Changes in the permissions, privileges, and roles granted to users and
  roles must be tracked. Without an audit trail, unauthorized elevation or
  restriction of privileges could go undetected. Elevated privileges give users
  access to information and functionality that they should not have; restricted
  privileges wrongly deny access to authorized users.
  In an SQL environment, adding permissions is typically done via the GRANT
  command, or, in the negative, the REVOKE command."
  impact 0.5
  tag "severity": "medium"
  tag "gtitle": "SRG-APP-000495-DB-000326"
  tag "gid": "V-72929"
  tag "rid": "SV-87581r1_rule"
  tag "stig_id": "PGS9-00-004900"
  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 by running the following SQL:
  Change the privileges of another user:
  $ sudo su - postgres
  $ psql -c \"CREATE ROLE bob\"
  Next, GRANT then REVOKE privileges from the role:
  $ psql -c \"GRANT CONNECT ON DATABASE postgres TO bob\"
  $ psql -c \"REVOKE CONNECT ON DATABASE postgres FROM bob\"
  postgres=# REVOKE CONNECT ON DATABASE postgres FROM bob;
  REVOKE
  postgres=# GRANT CONNECT ON DATABASE postgres TO bob;
  GRANT
  Now, as the database administrator (shown here as \"postgres\"), verify the
  events were logged:
  $ sudo su - postgres
  $ cat ${PGDATA?}/pg_log/<latest_log>
< 2016-07-13 16:25:21.103 EDT postgres
  postgres LOG: > AUDIT: SESSION,1,1,ROLE,GRANT,,,GRANT CONNECT ON DATABASE
  postgres TO bob,<none>
  < 2016-07-13 16:25:25.520 EDT postgres postgres LOG: > AUDIT:
  SESSION,1,1,ROLE,REVOKE,,,REVOKE CONNECT ON DATABASE postgres FROM bob,<none>
  If the above steps cannot verify that audit records are produced when
  privileges/permissions/role memberships are added, this is a finding."
  tag "fix": "Note: The following instructions use the PGDATA environment
  variable. See supplementary content APPENDIX-F for instructions on configuring
  PGDATA.
  Using pgaudit PostgreSQL can be configured to audit these requests. See
  supplementary content APPENDIX-B for documentation on installing pgaudit.
  With pgaudit installed the following configurations can be made:
  $ sudo su - postgres
  $ vi ${PGDATA?}/postgresql.conf
  Add the following parameters (or edit existing parameters):
  pgaudit.log = 'role'
  Now, as the system administrator, reload the server with the new configuration:
  # SYSTEMD SERVER ONLY
  $ sudo systemctl reload postgresql-9.5
  # INITD SERVER ONLY
  $ sudo service postgresql-9.5 reload"

  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;'
  grant_role = "GRANT CONNECT ON DATABASE #{PG_DB} TO bob;"
  revoke_role = "REVOKE CONNECT ON DATABASE #{PG_DB} FROM bob;"
  drop_role = 'DROP ROLE bob;'
  grant_role_message = "LOG:\\s+AUDIT:\\s+SESSION,[0-9]+,[0-9]+,ROLE,"\
    "GRANT(,.*){2},#{grant_role}"
  grant_role_regex = Regexp.new(grant_role_message)
  revoke_role_message = "LOG:\\s+AUDIT:\\s+SESSION,[0-9]+,[0-9]+,ROLE,"\
    "REVOKE(,.*){2},#{revoke_role}"
  revoke_role_regex = Regexp.new(revoke_role_message)

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

  describe sql.query(grant_role, [PG_DB]) do
    its('output') { should eq 'GRANT' }
  end

  describe sql.query(revoke_role, [PG_DB]) do
    its('output') { should eq 'REVOKE' }
  end

  sql.query(drop_role, [PG_DB])

  describe command(get_logs) do
    its('stdout') { should match grant_role_regex }
    its('stdout') { should match revoke_role_regex }
  end
end