top of page

Oracle Fine-Grained Auditing Step by Step: A Real Use Case and Its Implementation

OmerNarman
Aug 12
5 min read

Updated: Oct 24

Standard auditing answers one question: who touched this table? For most regulators that is not enough. They want to know who read the salary column, whether that was an application account or a person, what statement they ran, and they want it without drowning in a million records of normal application traffic. That is the job of Oracle Fine-Grained Auditing, and it takes about an hour to set up properly.


The use case

An HR schema holds EMPLOYEES with SALARY and COMMISSION_PCT. The HR application reads those columns thousands of times an hour through its own account, HR_APP, and nobody needs an audit record for that. What the security team wants is a record every time any other account selects or updates those two columns, with the SQL text and bind values, plus an alert when it happens outside business hours.

A standard audit policy on SELECT against EMPLOYEES would log every application read. Fine-grained auditing audits a statement only when it touches specific columns and only when a condition you define is true.


How fine-grained auditing works

  • A policy is attached to one table or view with DBMS_FGA.ADD_POLICY.

  • The policy names the columns of interest. A statement is audited only if it references at least one of them (ANY_COLUMNS) or all of them (ALL_COLUMNS).

  • The audit condition is a SQL boolean expression. It can use SYS_CONTEXT and the columns of the table itself, and a record is written when at least one row processed by the statement satisfies it. A null condition means always.

  • Records land in the unified audit trail with an AUDIT_TYPE of FineGrainedAudit, and the policy can call a PL/SQL handler of yours on every hit.


Prerequisites

  • Oracle Database 12c or later. On 23ai, unified auditing is the only audit mode.

  • An account with EXECUTE on DBMS_FGA to create policies, typically one holding the AUDIT_ADMIN role, and AUDIT_VIEWER or AUDIT_ADMIN to read the trail.

  • Confirm the audit mode before you start:

SQL

SELECT value FROM v$option WHERE parameter = 'Unified Auditing';

-- TRUE means pure unified auditing: FGA records are written to UNIFIED_AUDIT_TRAIL


Step 1: create the policy

The policy below audits SELECT and UPDATE statements that reference SALARY or COMMISSION_PCT, from any session that is not the application account, and captures the SQL text and bind values.

SQL

BEGIN

DBMS_FGA.ADD_POLICY(

object_schema => 'HR',

object_name => 'EMPLOYEES',

policy_name => 'FGA_SALARY_ACCESS',

audit_condition => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') <> ''HR_APP''',

audit_column => 'SALARY,COMMISSION_PCT',

audit_column_opts => DBMS_FGA.ANY_COLUMNS,

statement_types => 'SELECT,UPDATE',

audit_trail => DBMS_FGA.DB_EXTENDED,

enable => TRUE);

END;

/


Two parameters do the filtering. audit_column limits auditing to statements that reference at least one of the named columns. audit_condition narrows it to sessions other than HR_APP. Swap the condition for a row-based one, for example SALARY > 150000, and only statements that return or change at least one such row are recorded.


Step 2: test it

Run a query as the application account, then as a named user, and read the trail.

SQL

-- As HR_APP: no audit record expected

SELECT employee_id, salary FROM hr.employees WHERE employee_id = 100;

-- As a named DBA or analyst account: one record expected

SELECT employee_id, salary FROM hr.employees WHERE department_id = 90;

-- Read the trail

SELECT event_timestamp, dbusername, os_username, client_program_name,

fga_policy_name, sql_text, sql_binds

FROM unified_audit_trail

WHERE audit_type = 'FineGrainedAudit'

AND fga_policy_name = 'FGA_SALARY_ACCESS'

ORDER BY event_timestamp DESC;


A query that selects only EMPLOYEE_ID and LAST_NAME produces nothing, which is the point: the policy stays silent until a protected column is involved. If a record does not appear immediately on an older release, flush the queue with DBMS_AUDIT_MGMT.FLUSH_UNIFIED_AUDIT_TRAIL.


Step 3: add an alert handler

The handler is a PL/SQL procedure with a fixed signature that Oracle calls every time the policy fires. Keep it small, keep it fast and never let it raise: an unhandled error in the handler fails the user's statement with ORA-28144.

SQL

CREATE TABLE hr_sec.salary_access_alerts (

alert_time TIMESTAMP DEFAULT SYSTIMESTAMP,

db_user VARCHAR2(128),

os_user VARCHAR2(128),

client_host VARCHAR2(256),

policy_name VARCHAR2(128));

CREATE OR REPLACE PROCEDURE hr_sec.salary_access_handler (

p_schema IN VARCHAR2,

p_object IN VARCHAR2,

p_policy IN VARCHAR2)

AS

PRAGMA AUTONOMOUS_TRANSACTION;

BEGIN

IF TO_NUMBER(TO_CHAR(SYSDATE, 'HH24')) NOT BETWEEN 7 AND 19 THEN

INSERT INTO hr_sec.salary_access_alerts (db_user, os_user, client_host, policy_name)

VALUES (SYS_CONTEXT('USERENV', 'SESSION_USER'),

SYS_CONTEXT('USERENV', 'OS_USER'),

SYS_CONTEXT('USERENV', 'HOST'),

p_policy);

COMMIT;

END IF;

EXCEPTION

WHEN OTHERS THEN NULL; -- never fail the audited statement

END;

/

-- Attach the handler: drop and re-create the policy with the handler parameters

BEGIN

DBMS_FGA.DROP_POLICY('HR', 'EMPLOYEES', 'FGA_SALARY_ACCESS');

DBMS_FGA.ADD_POLICY(

object_schema => 'HR',

object_name => 'EMPLOYEES',

policy_name => 'FGA_SALARY_ACCESS',

audit_condition => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') <> ''HR_APP''',

audit_column => 'SALARY,COMMISSION_PCT',

audit_column_opts => DBMS_FGA.ANY_COLUMNS,

statement_types => 'SELECT,UPDATE',

audit_trail => DBMS_FGA.DB_EXTENDED,

handler_schema => 'HR_SEC',

handler_module => 'SALARY_ACCESS_HANDLER',

enable => TRUE);

END;

/


A scheduler job can then read the alerts table every few minutes and send the notification through whatever channel the security team already uses. Keeping the notification out of the handler keeps the audited statement fast.


Step 4: manage the policy

  • Pause and resume without dropping: DBMS_FGA.DISABLE_POLICY and DBMS_FGA.ENABLE_POLICY.

  • See what is defined: DBA_AUDIT_POLICIES for the policies and DBA_AUDIT_POLICY_COLUMNS for their columns.

  • Control growth: set a retention point with DBMS_AUDIT_MGMT.SET_LAST_ARCHIVE_TIMESTAMP and purge with DBMS_AUDIT_MGMT.CLEAN_AUDIT_TRAIL, or schedule it with CREATE_PURGE_JOB.

  • Keep the evidence off the host: a scheduled job that copies new FineGrainedAudit rows to your SIEM turns the trail into a record nobody on the database can edit.


The same policy on OCI, AWS, Azure and Google Cloud

Fine-grained auditing is a feature of the database, not of the platform, so the policy above works unchanged wherever Oracle Database runs: Base Database and Exadata services on OCI, Oracle Database@Azure and Oracle Database@Google Cloud, Autonomous Database, and Amazon RDS for Oracle. The differences are around the trail, not the policy.

  • On a managed service you do not own the operating system, so keep the trail in the database (DB_EXTENDED) rather than in XML files, and manage retention with DBMS_AUDIT_MGMT.

  • Check the service documentation for how audit records are exported or collected; on OCI, Data Safe can collect the unified audit trail centrally.

  • Across clouds, keep the policy scripts in version control and deploy them with the same pipeline, so an auditor sees one standard rather than four.

  • Write time-based conditions in the right time zone. A database in a US region auditing a Warsaw team needs the business-hours check expressed in the team's zone, not the server's.


Pitfalls worth knowing

  • A null audit_condition means audit everything that touches the columns. Make the condition explicit.

  • The condition is evaluated against the rows the statement processes, so keep it cheap and avoid calling your own functions inside it.

  • Audit the columns people should not browse, not the ones the application reads constantly. Noise is how audit trails get ignored.

  • Test the handler under load before production; it runs inside the user's statement.

  • Fine-grained auditing is for data access by ordinary accounts. Cover administrators and SYS with unified audit policies alongside it.


Soro Consulting designs audit and security controls for Oracle estates on premises and across OCI, AWS, Azure and Google Cloud. Book a consultation if you need yours reviewed.

Recent Posts

See All

Comments


bottom of page