Oracle 12c Security PDF
Oracle 12c Security PDF
Oracle 12c Security PDF
Hans Forbrich This document contains proprietary information and is protected by copyright and
other intellectual property laws. You may copy and print this document solely for your
own use in an Oracle training course. The document may not be modified or altered
Technical Contributors in any way. Except where your use constitutes "fair use" under copyright law, you
and Reviewers may not use, share, download, upload, copy, print, display, perform, reproduce,
publish, license, post, transmit, or distribute this document in whole or in part without
Jean-Francois Verrier the express authorization of Oracle.
Mark Fuller
The information contained in this document is subject to change without notice. If you
James Spiller find any problems in the document, please report them in writing to: Oracle University,
500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
Lachlan Williams warranted to be error-free.
Jody Glover
Restricted Rights Notice
Jeff Ferriera
Peter Fusek If this documentation is delivered to the United States Government or anyone using
Background:
In the practices of this course, you assume the role of a database administrator (DBA) and of
the security officer. The operating system (OS) accounts on your computer are:
• The oracle user with a password of oracle
• The root user with a password of oracle
Simple and easy-to-remember passwords will be used in order to not detract from the purpose
of the exercise. In real development and production environments, use strong passwords
following the guidelines presented in this course and in the Oracle Database Security Guide
12c.
Tasks
1. Verify that you are logged in as the oracle user when you right-click the desktop and click
Open in Terminal to open a terminal window. The UID and GID may have different values
than yours. Do not care about the values but do care about the user used to log in.
$ id
uid=54321(oracle) gid=54321(oinstall)
groups=54321(oinstall),54322(dba),54323(oper),54324(backupdba),5
4325(dgdba),54326(kmdba),54327(asmdba)
INSTANCE_NAME
----------------
orcl
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SYS cdb1 >
c. View the instance name.
SYS cdb1 > select instance_name from v$instance;
INSTANCE_NAME
----------------
cdb1
NAME
----------------
PDB1_1
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
INSTANCE_NAME
----------------
CON_NAME
------------------------------
PDB1_2
SYS pdb1_2 >
d. Quit the SYS session.
SYS pdb1_2 > EXIT
$
Tasks
1. Launch a browser and enter: file:////home/oracle/labs/SQL_Injection/index.htm.
Note: You may get an Adobe Flash Player 10 settings window when launching demos in
Tasks
1. Create a definer’s rights procedure in the orcl instance. The CHANGE_PASSWORD
procedure is created under the SYS schema. It accepts two parameters and uses them in
the ALTER USER statement.
a. Use the oraenv utility to set the ORACLE_SID environment variable to the orcl value.
$ . oraenv
ORACLE_SID = [cdb1] ? orcl
The Oracle base for
ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 is
/u01/app/oracle
$
b. Use SQL*Plus to connect to the instance.
$ sqlplus / as sysdba
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SYS orcl >
c. Create the CHANGE_PASSWORD procedure.
SYS orcl > CREATE OR REPLACE
PROCEDURE change_password(p_username VARCHAR2 DEFAULT NULL,
p_new_password VARCHAR2 DEFAULT NULL)
IS
v_sql_stmt VARCHAR2(500);
BEGIN
v_sql_stmt := 'ALTER USER '||p_username ||' IDENTIFIED BY '
|| p_new_password;
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Procedure created.
SYS orcl >
Note the use of dynamic SQL with concatenated input values within the v_sql_stmt
character string.
2. As the SYS user, grant OE, HR, and SH the ability to execute the CHANGE_PASSWORD
procedure.
SYS orcl > GRANT EXECUTE ON change_password to OE, HR, SH;
OE orcl >
4. Check that the password of SYS has changed.
OE orcl > CONNECT sys@orcl as sysdba
Enter password: ****** (oracle_4U)
ERROR:
ORA-01017: invalid username/password; logon denied
*
ERROR at line 1:
ORA-01031: Insufficient privileges
ORA-06512: at "SYS.CHANGE_PASSWORD", at line 10
ORA-06512: at line 1
OE orcl >
OE orcl >
.
Tasks
1. Define two LIST_PRODUCTS procedures. The LIST_PRODUCTS_DYNAMIC procedure
does not use bind arguments but contains concatenated input values. The
LIST_PRODUCTS_STATIC procedure uses bind arguments.
Create the LIST_PRODUCTS_DYNAMIC procedure containing dynamic SQL with
concatenated input values. Why is the SQL considered as dynamic? The ‘SELECT
product_name, min_price, list_price FROM products WHERE product_name like
‘’%’||p_product_name||’%’’’’ statement is unresolved at compile-time.
OE orcl > CONNECT oe
Enter password: ******
Connected.
OE orcl > SET SERVEROUTPUT ON
OE orcl > CREATE OR REPLACE PROCEDURE list_products_dynamic
(p_product_name VARCHAR2 DEFAULT NULL)
AS
TYPE cv_prodtyp IS REF CURSOR;
cv cv_prodtyp;
v_prodname products.product_name%TYPE;
v_minprice products.min_price%TYPE;
v_listprice products.list_price%TYPE;
v_stmt VARCHAR2(400);
BEGIN
v_stmt := 'SELECT product_name, min_price, list_price
FROM products
WHERE product_name like ''%'||p_product_name||'%''';
OPEN cv FOR v_stmt;
dbms_output.put_line(v_stmt);
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
OE orcl >
The result is correct because the user entered an appropriate product name.
3. Execute the procedure performing a SQL injection attack and see that you can retrieve the
list of database accounts.
OE orcl > EXEC list_products_dynamic(''' and 1=0 union select
cast(username as nvarchar2(100)), null, null from all_users --')
SELECT product_name, min_price, list_price
FROM products
WHERE product_name like '%' and 1=0 union select cast(username
as
nvarchar2(100)), null, null from all_users --%'
Product Info: ANONYMOUS, ,
Product Info: APEX_040200, ,
Product Info: APEX_PUBLIC_USER, ,
Product Info: APPQOSSYS, ,
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
OE orcl >
Notice the SQL injection attack succeeded through the concatenation of a UNION set
operator to the dynamic SQL statement.
OE orcl >
5. Execute the procedure.
OE orcl > EXEC list_products_static('Laptop')
Product Info: Laptop 128/12/56/v90/110,2606, 3219
Product Info: Laptop 16/8/110,800, 999
Product Info: Laptop 32/10/56,1542, 1749
Product Info: Laptop 48/10/56/110,2073, 2556
Product Info: Laptop 64/10/56/220,2275, 2768
OE orcl >
Notice that the SQL injection attempt failed.
Tasks
1. Create the GET_AVG_SALARY function containing a dynamic PL/SQL block used to
retrieve the average salary with a concatenated input parameter p_job. This is a SQL
injection vulnerability.
OE orcl > CONNECT hr
Enter password: ******
Code: BEGIN
SELECT AVG(salary) INTO :avgsal
FROM
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HR orcl >
It works fine and provides the correct result.
3. You will now attempt to change the salary of an employee although the function exists to
show the average of a salary for a job.
HR orcl > select salary from employees where email='PFAY';
Code: BEGIN
SELECT AVG(salary) INTO :avgsal
FROM
hr.employees
WHERE job_id = 'SH_CLERK'; UPDATE hr.employees SET
salary=4500 WHERE email='PFAY'; COMMIT; END;--'; END;
HR orcl >
The UPDATE statement was injected successfully.
4. Check the salary of the PFAY employee.
HR orcl > select salary from employees where email='PFAY';
SALARY
----------
4500
HR orcl >
1 row updated.
Commit complete.
HR orcl >
HR orcl >
Code: BEGIN
SELECT AVG(salary) INTO :avgsal
FROM
hr.employees
WHERE job_id = :p_job; END;
HR orcl >
8. Retest the new function and verify that the new code does not work for an invalid input with
the same SQL injection attack.
HR orcl > select salary from employees where email='PFAY';
SALARY
----------
6000
Code: BEGIN
SELECT AVG(salary) INTO :avgsal
FROM
hr.employees
WHERE job_id = :p_job; END;
HR orcl >
The block executes but returns a NULL value for the average salary because no JOB_ID
column value matched the 'SH_CLERK''; UPDATE hr.employees SET
salary=4500 WHERE email=''PFAY''; COMMIT; END;-- ' value.
SALARY
----------
6000
HR orcl >
The UPDATE statement was not executed. The SQL injection failed.
Tasks
1. The input for the user name is user-supplied and so is in a normal identifier format. It needs
to be pre-processed using a conversion routine. Create a function that converts a normal
quoted value to an internal format value when a user-supplied value is to be used as a bind
argument for a lookup of an internal object name.
HR orcl > CONNECT / AS SYSDBA
Connected.
SYS orcl > CREATE OR REPLACE FUNCTION toInternal(Id varchar2)
RETURN varchar2 IS
Temp varchar2(40);
begin
Temp := trim(Id);
-- See comments in text re trimming
-- Remove quotes
IF substr(Temp,1,1) = '"' AND
*
ERROR at line 1:
ORA-44003: invalid SQL name
ORA-06512: at "SYS.CHANGE_PASSWORD", line 16
ORA-06512: at line 1
HR orcl >
4. Check that the procedure does not allow any invalid input value for the user name.
HR orcl > EXEC sys.change_password('hr oe','hr')
BEGIN sys.change_password('hr oe','hr'); END;
*
ERROR at line 1:
ORA-44001: invalid schema
ORA-06512: at "SYS.CHANGE_PASSWORD", line 16
ORA-06512: at line 1
HR orcl >
Scenario 1
Your company sends backup tapes off site to a disaster recovery site. Payment information
(including credit card numbers, customer names, and addresses) is in the data files included on
the tapes. The PCI_DSS requirement 3 says “Protect stored cardholder data” and requirement 4
says “Encrypt transmission of cardholder data across open, public networks.” The chief
information officer (CIO) wants to secure this information to prevent bad publicity if the backup
tapes are lost or stolen, or if any cardholder information is acquired by intercepting network
traffic.
Answer
Scenario 2
The network security officer has detected abnormal activity involving port 1521 through a
firewall and several desktop machines inside the firewall. The normal activity is for users outside
the firewall to contact an application server; therefore, all the database activity should be
through the application server and not on port 1521 through the firewall.
Answer
Port 1521 is the default port for the Oracle database listener. This may indicate an attempt to
attack the database. Some or all of the following protections can be implemented.
• Port 1521 should be closed through the firewall. The only outside users allowed
through the firewall contact the application server on its listener port (usually, this is an
HTTP or HTTPS port, not port 1521).
• The database can be configured to accept connections only from the application server
and to reject connections from any other machine.
• A good practice is to place the application server in one zone and the database in
another zone with a firewall between them.
Scenario 3
The company is considering outsourcing the DBA activities to a third party. The concern is that
a DBA who is not an employee will be able to access company-proprietary information,
customer financial information, and employee medical information.
Answer
There are powerful system privileges assigned to the DBA role that allow the DBA to view data.
There are two main solutions:
• Oracle Database Vault can be very easily configured to limit the data that the DBA can
view.
Scenario 4
The current DBA has been granted the SYSDBA role to effectively start up and shut down the
database instance, and use RMAN to make database backups. There have been some
incidents in the past when company confidential information has been discovered on the Web.
How can the current DBA protect himself or herself from accusations that he or she is the most
likely suspect for any further security breaches because he or she had access?
Answer
There are two situations:
• The DBA has not yet migrated to unified auditing:
− He can enable the AUDIT_SYS_OPERATIONS parameter to record every command
Tasks
1. You check that the Enterprise Manager Cloud Control is available. Click the Firefox icon on
the top panel (toolbar region) above the desktop to open a browser to access the Enterprise
Manager Cloud Control console.
7. Then the “Select Enterprise Manager Home” page appears with choices, such as:
− Summary
− Databases
− Incidents
− SOA
b. In “Add Targets Manually”, choose “Add Non-Host Targets Using Guided Process
(Also Adds Related Targets)”. Then in “Target Types”, choose “Oracle Database,
Listener and Automatic Storage Management” for “Target Type”. Click “Add Using
d. In the “Databases” list, deselect all databases except orcl. Deselect the listener.
1) Unlock the DBSNMP user. This user is the monitoring user used to test the
connection once the target is being added. Open a terminal window.
$ . oraenv
ORACLE_SID = [em12rep] ? orcl
The Oracle base for
ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 is
/u01/app/oracle
$ sqlplus / as sysdba
Connected to:
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
User altered.
f. Click the “Finish” then “Save” buttons to complete the operation, and finally “OK”.
10. To create the monitoring credentials for your orcl database credentials, navigate to Setup
> Security > Named Credentials. Click Create.
a. Enter the following values, then complete the Access Control section:
Field Choice or Value
General Properties
Credential Name credorcl
Credential description Credentials for Database
Authenticating Target Type Database Instance
Credential type Database Credentials
Scope Target
Target type Database Instance
Target Name orcl (Click the magnifying glass
Credential Properties
Username SYSTEM
Password oracle_4U
Confirm Password oracle_4U
Role NORMAL
b. Specify who can share, edit or even delete this shared credential by using one of the
three privileges (Full, Edit, View).
• SYS user with Full privilege will be able to use, edit, and delete the credential.
Tasks
1. To review predefined compliance objects, navigate to Enterprise > Compliance > Library.
6. The hierarchy nodes displays several levels. Review the descriptions; then click Done.
8. Review the predefined standards and then select Basic Security Configuration For
Oracle Database (which is applicable to the Database Instance target type).
11. Review any other descriptions that may interest you, and then click Done.
15. Click Done when you are finished reviewing the rule details.
16. Click Enterprise then Summary to return to the Enterprise Summary page.
Tasks
1. Display the existing constraints on HR.EMPLOYEES table in the orcl database.
$ sqlplus hr
Enter password: ******
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
HR orcl >
2. Insert a new employee in the HR.EMPLOYEES table as follows:
HR orcl > INSERT INTO hr.employees (EMPLOYEE_ID, LAST_NAME ,
EMAIL, HIRE_DATE , JOB_ID, DEPARTMENT_ID)
HR orcl >
The statement fails because the department does not exist. The referential constraint
controls that invalid data is not inserted into the table.
3. Delete the department 30 in the HR.DEPARTMENTS table as follows:
HR orcl >
The statement fails because the referential constraint does not permit that the department
deletion deletes all employees working in that department in cascade. The referential
constraint controls that you first move the employees working in this department to another
department before you can delete the department.
a. Move the employees to another department.
HR orcl > UPDATE hr.employees SET department_id=40
WHERE department_id=30;
2
6 rows updated.
HR orcl >
b. Reattempt to remove the department.
HR orcl > DELETE FROM hr.departments WHERE department_id=30;
DELETE FROM hr.departments WHERE department_id=30
*
ERROR at line 1:
ORA-02292: integrity constraint (HR.JHIST_DEPT_FK) violated -
child record
found
HR orcl >
6 rows deleted.
1 row deleted.
HR orcl >
4. Insert a new employee with a salary below the minimum legally allowed.
HR orcl > INSERT INTO hr.employees (EMPLOYEE_ID,
LAST_NAME,EMAIL,
HIRE_DATE, JOB_ID, SALARY, DEPARTMENT_ID)
VALUES (9, 'VERRIER', 'VERRIER@test', sysdate,
'ST_MAN',0, 30);
2 3 4 INSERT INTO hr.employees (EMPLOYEE_ID, LAST_NAME
, EMAIL,
*
ERROR at line 1:
ORA-02290: check constraint (HR.EMP_SALARY_MIN) violated
HR orcl >
The statement fails because a CHECK constraint checks that the salary is higher than a
minimum. Invalid value cannot be inserted into the table.
a. Examine the HR.SALARY_MIN constraint.
HR orcl >
COL table_name format a10
HR orcl >
COL search_condition format a14
HR orcl >
COL constraint_name format a18
HR orcl >
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME,
SEARCH_CONDITION
FROM user_constraints
WHERE CONSTRAINT_NAME='EMP_SALARY_MIN';
2 3 4
CONSTRAINT_N C TABLE_NAME SEARCH_CONDITI
--------------- - ---------- --------------
EMP_SALARY_MIN C EMPLOYEES salary > 0
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
HR orcl >
Tasks
1. Find if any trigger already exist to maintain the stock inventory when products are sold.
HR orcl > CONNECT oe
Enter password: ******
Connected.
OE orcl > SELECT table_name, trigger_name, status, trigger_body
FROM user_triggers
OE orcl >
2. Create a simple trigger (for test purposes only) that updates the QUANTITY_ON_HAND in
the stock when ordering product is 3515.
CREATE OR REPLACE TRIGGER oe.update_stock
AFTER INSERT ON order_items
FOR EACH ROW
WHEN (NEW.product_id = 3515)
DECLARE
prod_id NUMBER;
BEGIN
prod_id := :NEW.product_id;
UPDATE inventories
SET quantity_on_hand = quantity_on_hand - 100
WHERE product_id = prod_id;
END;
/
OE orcl > CREATE OR REPLACE TRIGGER oe.update_stock
AFTER INSERT ON order_items
FOR EACH ROW
WHEN (NEW.product_id = 3515)
DECLARE
prod_id NUMBER;
BEGIN
prod_id := :NEW.product_id;
UPDATE inventories
SET quantity_on_hand = quantity_on_hand - 100
OE orcl >
3. Display the amount of remaining items of the product ID 3515 in the stock.
OE orcl > SELECT QUANTITY_ON_HAND FROM OE.INVENTORIES
WHERE PRODUCT_ID=3515;
2
QUANTITY_ON_HAND
OE orcl >
4. Order 100 items of the product ID 3515.
OE orcl > INSERT INTO oe.orders (
ORDER_ID, ORDER_DATE, CUSTOMER_ID, ORDER_TOTAL)
VALUES (17, sysdate, 980, 100);
2 3
1 row created.
Commit complete.
OE orcl >
QUANTITY_ON_HAND
----------------
113
OE orcl >
Tasks
1. Create the HR_ASSISTANT view.
OE orcl > CONNECT HR
Enter password: ******
Grant succeeded.
HR orcl >
2. Create the HR_CLERK view.
HR orcl > CREATE VIEW hr_clerk
AS SELECT first_name, last_name, department_name
FROM hr.employees e, hr.departments d
WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID;
2 3 4
View created.
Grant succeeded.
HR orcl >
3. Verify that only JIM can view all information of any employees except the president, and
that TOM can only view some information of the employees.
HR orcl > CONNECT jim
Enter password: ******
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
COUNT(*)
----------
106
View dropped.
Tasks
1. Make sure you are in the ~/labs/DV directory and your environment points to the orcl
instance.
$ cd ~/labs/DV
$ . oraenv
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
User created.
User created.
Connected.
Connected to:
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics, Oracle Database Vault and Real Application Testing
options
HR orcl >
HR orcl > select * from hr.employees;
select * from hr.employees
*
ERROR at line 1:
ORA-01031: insufficient privileges
HR orcl >
6. Verify that HR can access to other tables owned in his schema.
HR orcl > select * from hr.jobs;
19 rows selected.
HR orcl >
7. Select from a non-existing table.
HR orcl > select * from hr.test_tab;
select * from hr.test_tab
*
ERROR at line 1:
ORA-00942: table or view does not exist
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics, Oracle Database Vault and Real Application Testing
options
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics, Oracle Database Vault and Real Application Testing
options
Connected.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics, Oracle Database Vault and Real Application Testing
options
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
COUNT(*)
----------
107
Tasks
1. Connect as SYSTEM in orcl instance to create the SEC user, giving it the following
properties:
− Name is SEC
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
User created.
SQL>
SQL> GRANT create session
2 TO sec
3 WITH ADMIN OPTION;
Grant succeeded.
SQL>
SQL> GRANT select_catalog_role, select any table,
2 create any context, drop any context,
3 create user, alter user, drop user,
4 create role, alter any role, drop any role,
5 create table, create procedure,
6 create any trigger, administer database trigger,
7 create any directory, alter profile, create profile,
8 drop profile, audit system, alter system,
9 grant any object privilege, grant any privilege,
grant any role
Grant succeeded.
SQL>
SQL> GRANT execute on DBMS_SESSION to sec;
Grant succeeded.
Grant succeeded.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SQL> ALTER USER PM PASSWORD EXPIRE ACCOUNT LOCK;
User altered.
User altered.
SQL>
b. Because it is dangerous to work with UTL_FILE_DIR parameter set to *, you reset the
UTL_FILE_DIR parameter to NULL, so that no one can read from or write to any
directory using the UTL_FILE package. Then you configure the database so that users
can write to the /home/oracle/student directory:
1) Reset the UTL_FILE_DIR parameter to NULL.
SQL> ALTER SYSTEM SET utl_file_dir='' SCOPE=spfile;
System altered.
Directory created.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
Notice the error. The /home/oracle OS directory is not a directory object defined in the
database. Use a directory defined in the database.
SQL> DECLARE
file_handle UTL_FILE.FILE_TYPE;
file_mode VARCHAR2(1) := 'w';
file_name VARCHAR2(15) := 'db_time.lst';
file_location VARCHAR2(80) := '&1';
file_data VARCHAR2(100);
BEGIN
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
4) Verify that the db_time.lst file is written to the directory after executing the
PL/SQL block.
SQL> HOST cat /home/oracle/student/db_time.lst
05-JUL-13 10.01.49.700632000 AM +00:00
SQL>
c. Do any users in your database have the DBA role, SYSOPER, SYSDBA, SYSKM, SYSDG,
or SYSBACKUP privilege that they do not need? Fix this problem.
1) Find users who are granted the DBA role by querying the DBA_ROLE_PRIVS view.
SQL> COL grantee FORMAT a12
SQL> COL granted_role FORMAT a12
SQL> SELECT * FROM dba_role_privs WHERE granted_role='DBA';
SQL>
2) SCOTT has no need for the DBA role because this is a demo account that has been
locked and the password expired. Revoke the DBA role from SCOTT. To revoke a
role, you must have been granted the role with ADMIN OPTION. You can revoke
any role if you have the GRANT ANY ROLE system privilege.
SQL> REVOKE DBA FROM scott;
SQL>
d. The users with the SYSDBA or SYSOPER privilege are listed in the oracle password file.
6 rows selected.
Revoke succeeded.
Revoke succeeded.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
SQL>
2) Find other users who may be granted the UNLIMITED TABLESPACE privilege by
querying the DBA_SYS_PRIVS view.
SQL> SELECT grantee FROM dba_sys_privs
WHERE privilege = 'UNLIMITED TABLESPACE'
AND grantee NOT IN (SELECT grantee
FROM dba_sys_privs JOIN dba_role_privs USING (grantee)
WHERE granted_role='RESOURCE'
AND privilege = 'UNLIMITED TABLESPACE');
8 rows selected.
SQL>
3) If necessary, revoke the UNLIMITED TABLESPACE privilege from TOM user.
SQL> REVOKE unlimited tablespace FROM tom;
Revoke succeeded.
SQL> EXIT
$
Tasks
1. Determine what limits are applied with the DEFAULT profile. Then, set up password
management by performing the following steps:
a. List the rows related to password management from the current profiles in the system.
Use the SEC account. Save the command that you use.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL> set pagesize 40
SQL> col profile format A10
SQL> col limit format A22
SQL> col resource_name format A25
SQL> SELECT profile, resource_name, limit
FROM dba_profiles
WHERE PROFILE = 'DEFAULT'
AND resource_type = 'PASSWORD';
2 3 4
PROFILE RESOURCE_NAME LIMIT
---------- ------------------------ ----------------------
DEFAULT FAILED_LOGIN_ATTEMPTS 10
DEFAULT PASSWORD_LIFE_TIME 180
DEFAULT PASSWORD_REUSE_TIME UNLIMITED
DEFAULT PASSWORD_REUSE_MAX UNLIMITED
DEFAULT PASSWORD_VERIFY_FUNCTION NULL
DEFAULT PASSWORD_LOCK_TIME 1
DEFAULT PASSWORD_GRACE_TIME 7
7 rows selected.
/**
The below set of password profile parameters would take into
consideration
recommendations from Center for Internet Security[CIS Oracle
11g].
/**
The below set of password profile parameters would take into
consideration recommendations from Department of Defense
Database
Security Technical Implementation Guide[STIG v8R1].
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL> SET ECHO ON
SQL> SELECT object_name, object_type
FROM dba_objects
WHERE object_name LIKE '%VERIFY_FUNCTION%';
2 3
no rows selected.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Function created.
Function created.
Grant succeeded.
Function created.
Grant succeeded.
Function created.
Grant succeeded.
Function created.
Grant succeeded.
Profile altered.
The output has been modified to show only the results.
OBJECT_NAME OBJECT_TYPE
---------------------------------------- --------------------
ORA12C_VERIFY_FUNCTION FUNCTION
ORA12C_STRONG_VERIFY_FUNCTION FUNCTION
SQL>
3) Update the DEFAULT profile with the password verify function.
SQL> ALTER PROFILE default LIMIT
PASSWORD_VERIFY_FUNCTION ora12c_strong_verify_function;
2
Profile altered.
SQL>
e. View the changes applied. Repeat the command from step 2a as the SEC user and
note the differences.
SQL> CONNECT SEC
Enter password: ******
Connected.
SQL> COL profile format A7
SQL> COL resource_name format A32
SQL> COL limit format A30
SQL> @$HOME/labs/default_profile.sql
SQL> SELECT profile, resource_name, limit
FROM dba_profiles
WHERE PROFILE = 'DEFAULT'
AND resource_type = 'PASSWORD';
7 rows selected.
SQL>
2. Create a user and verify that the password is secure with the verify function applied in the
profile.
SQL> CREATE USER ann IDENTIFIED BY xxx12345;
CREATE USER ann IDENTIFIED BY xxx12345
User created.
SQL>
SQL>
Notice that SYS is not under the rules of any password checking function even if defined in
the DEFAULT profile.
4. What happens to a user being granted the SYSDBA privilege when he alters his own
password?
SQL> GRANT sysdba TO tom;
User altered.
SQL>
Notice that TOM falls under the rules of the password checking function defined in the
DEFAULT profile even if being granted the SYSDBA privilege.
User altered.
Revoke succeeded.
SQL> EXIT
$
7. The security officer will now define different DEFAULT profiles within pdb1_1 and pdb1_2
setting the following password limits:
− In pdb1_1: A life time period set to 1 minute (for the practice purpose) and no
password verify function
− In pdb1_2: Account locked after 2 failed login attempts only and the password
verify function set to ora12c_strong_verify_function
a. Set the ORACLE_SID and ORACLE_HOME to point to the CDB instance.
$ . oraenv
ORACLE_SID = [orcl] ? cdb1
The Oracle base for
ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 is
/u01/app/oracle
$ sqlplus / as sysdba
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
SQL>
or create the following trigger that will open them at each instance startup. You can use
the following trigger code:
SQL>
c. Connect to pdb1_1 as SYSTEM to alter the DEFAULT profile.
SQL> CONNECT system@pdb1_1
Enter password: ******
Connected.
SQL> ALTER PROFILE default LIMIT
PASSWORD_LIFE_TIME 1/1440
PASSWORD_VERIFY_FUNCTION null;
2 3
Profile altered.
SQL>
d. Connect to pdb1_2 as SYSTEM to alter the DEFAULT profile.
SQL> CONNECT system@pdb1_2
Enter password: ******
Connected.
SQL> ALTER PROFILE default LIMIT
FAILED_LOGIN_ATTEMPTS 10
PASSWORD_VERIFY_FUNCTION ora12c_strong_verify_function;
Function created.
Function created.
Function created.
Grant succeeded.
Grant succeeded.
Function created.
Grant succeeded.
Grant succeeded.
Profile altered.
SQL> @$HOME/labs/default_profile.sql
7 rows selected.
SQL>
e. Connect to the root container of cdb1 as SYSTEM and display the DEFAULT profile.
SQL> CONNECT system
Enter password: ******
Connected.
SQL> @$HOME/labs/default_profile.sql
7 rows selected.
SQL>
Notice that the root container has its own DEFAULT profile.
SQL>
SQL> CONNECT system@pdb1_1
Enter password:
Connected.
SQL> ALTER PROFILE default LIMIT
FAILED_LOGIN_ATTEMPTS unlimited
PASSWORD_LIFE_TIME unlimited
PASSWORD_VERIFY_FUNCTION null;
2 3 4
Profile altered.
SQL> EXIT
$
Tasks
1. After creating an Oracle database, what action do you need to take to prevent users with
the *ANY* privilege from using their privileges against the data dictionary? Which types of
users require the *ANY* privilege?
Verify that the O7_DICTIONARY_ACCESSIBILITY parameter is set to FALSE. This
restricts access to the data dictionary to users with the SELECT_CATALOG_ROLE or
SELECT ANY DICTIONARY privilege. Users who require the *ANY* privilege may be DBAs
who need privileges to create, alter, and drop objects, perform data manipulation language
(DML), and select objects in any schema. Note that in Oracle Database 12c, the default
10 rows selected.
SQL>
3. Which users have the SELECT ANY DICTIONARY privilege?
SQL> SELECT * FROM dba_sys_privs
9 rows selected.
SQL>
COUNT(*)
----------
2446
SQL>
5. Verify that SYS can view the SYS.ENC$.
SQL> CONNECT / AS SYSDBA
Connected.
SQL> SELECT * FROM SYS.ENC$;
no rows selected
SQL> EXIT
$
Tasks
1. To assign compliance standards to your database instances, navigate to Enterprise >
Compliance > Library.
2. Click the Compliance Standards tabbed page, and then the ">" icon before Search.
Tasks
1. Configure the listener to use an alternate port. Your network configuration files are stored in
the $TNS_ADMIN directory (/home/oracle/labs/NET). Then, start your listener.
a. Create the /home/oracle/labs/NET directory.
$ mkdir /home/oracle/labs/NET
$
b. Set the TNS_ADMIN environment variable to /home/oracle/labs/NET directory.
Starting /u01/app/oracle/product/12.1.0/dbhome_1/bin/tnslsnr:
please wait...
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<your
hostname>)(PORT=13001)))
STATUS of the LISTENER
------------------------
Alias LISTEN1
Version TNSLSNR for Linux: Version 12.1.0.1.0
- Production
Start Date 14-JUN-2013 06:49:04
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /home/oracle/labs/NET/listener.ora
Listener Log File /home/oracle/labs/NET/listen1.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=<your
hostname>)(PORT=13001)))
The listener supports no services
The command completed successfully
$
3. Display the new network configuration files and the first log created.
$ ls /home/oracle/labs/NET
listen1.log listener.ora sqlnet.ora
$
a. View the listener.ora file.
$ more /home/oracle/labs/NET/listener.ora
LOG_DIRECTORY_LISTEN1 = /home/oracle/labs/NET
LISTEN1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = <your hostname>)(PORT =
13001))
)
LOG_FILE_LISTEN1 = listen1.log
$
b. View the sqlnet.ora file.
$ more /home/oracle/labs/NET/sqlnet.ora
# sqlnet.ora Network Configuration File:
/home/oracle/labs/NET/sqlnet.ora
# Generated by Oracle configuration tools.
ADR_BASE = /u01/app/oracle
$
c. View the listen1.log file.
$ more /home/oracle/labs/NET/listen1.log
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL> EXIT
$
b. Create a net service name. Invoke NETMGR.
$ netmgr
Enter password:
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
NAME
---------
ORCL
Tasks
1. Prevent online administration of the listener and test the setting by performing the following
steps:
a. Set up the listener to prevent online administration. Do not forget to include your
listener name. Add the line ADMIN_RESTRICTIONS_LISTEN1=ON to the
listener.ora file. Edit the listener.ora file on the server with your favorite
LOG_DIRECTORY_LISTEN1 = /home/oracle/labs/NET
LISTEN1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = <Your hostname>)(PORT =
13001))
)
ADMIN_RESTRICTIONS_LISTEN1=ON
LOG_FILE_LISTEN1 = LISTEN1.log
DIAG_ADR_ENABLED_LISTEN1 = OFF
$
b. Stop and start your listener to force the listener.ora file to be read.
$ lsnrctl
LSNRCTL> start
Starting /u01/app/oracle/product/12.1.0/dbhome_1/bin/tnslsnr:
please wait...
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<Your
hostname>)(PORT=13001)))
STATUS of the LISTENER
------------------------
Alias LISTEN1
Version TNSLSNR for Linux: Version 12.1.0.1.0
- Production
Start Date 14-JUN-2013 07:54:37
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /home/oracle/labs/NET/listener.ora
Listener Log File /home/oracle/labs/NET/listen1.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=<Your
hostname>)(PORT=13001)))
The listener supports no services
The command completed successfully
LSNRCTL>
c. Attempt online administration. Set the trace level by using the following command:
LSNRCTL> SET TRC_LEVEL user
This verifies that you cannot administer the listener online.
LSNRCTL> SET TRC_LEVEL user
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<Your
hostname>)(PORT=13001)))
TNS-12508: TNS:listener could not resolve the COMMAND given
LSNRCTL> exit
$
Tasks
1. Determine the IP address of your neighbor’s PC. Ask your neighbor to use nslookup
`hostname` to determine the IP address of his/her computer. This command uses the
grave (`) punctuation marks to execute the hostname command. IP address:
________________________________
$ nslookup `hostname`
Name: His/Her_servername
Address: 192.0.2.254
$
2. Set up Oracle Net Services to allow connections from his/her client computer and deny all
others. When tcp.invited_nodes is set, all nodes except those invited are excluded.
The tcp.invited_nodes and tcp.excluded_nodes parameters can be used
independently; if tcp.excluded_nodes is used by itself, only the nodes listed are
blocked. If tcp.invited_nodes is used by itself, only tcp.invited_nodes are allowed
to connect. If both are used together, the tcp.invited_nodes list takes precedence.
a. Stop the listener before applying changes to the sqlnet.ora file.
$ lsnrctl
tcp.validnode_checking = YES
tcp.invited_nodes = (<your hostname>, <neighbor’s hostname>)
$ cd $TNS_ADMIN
$ gedit sqlnet.ora
Starting /u01/app/oracle/product/12.1.0/dbhome_1/bin/tnslsnr:
please wait...
Connecting to
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=EDRSR1P1)(PORT=13001))
)
STATUS of the LISTENER
------------------------
Alias listen1
Version TNSLSNR for Linux: Version 12.1.0.1.0
- Production
Start Date 14-JUN-2013 09:15:18
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /home/oracle/labs/NET/listener.ora
Listener Log File /home/oracle/labs/NET/listen1.log
Listening Endpoints Summary...
SQL> EXIT
$
4. Ask another student, whose PC’s address is not one of the invited nodes, to use the
EZCONNECT style connection string and attempt to connect to your listener.
$ sqlplus system@\<your hostname>:13001/orcl\'
Enter password:
ERROR:
ORA-12547: TNS:lost contact
Enter user-name:
$
5. Restore the listener so that it accepts any connections by removing the two parameters or
by just removing the sqlnet.ora file.
$ cd $TNS_ADMIN
$ rm sqlnet.ora
$ lsnrctl stop listen1
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
System altered.
SQL>
c. Restart the instance.
SQL> SHUTDOWN IMMEDIATE
Database closed.
Database dismounted.
Connecting to
(DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Assumptions
In your company, there are several situations that require exceptions to the standard password
policies. Batch jobs should not have passwords embedded in the script or command line.
Tasks
1. A batch job that runs as the fred operating system user should be able to connect to the
database as the FRED database user without having to embed the database password in
the batch file.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
System altered.
User created.
SQL>
SQL> GRANT CREATE SESSION TO FRED;
SQL> EXIT
$
3. Test the connection as the fred user. Log in to the OS as the fred user. The OS
password for fred is oracle. Connect to the database with the “/” connect string.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
Tasks
1. Create and test a database link in the PDB1_1 pluggable database. Log in as the oracle
OS user. As the SYSTEM database user, create a database link for the HR user to the ORCL
database.
CREATE PUBLIC DATABASE LINK test_hr
CONNECT TO hr IDENTIFIED BY oracle_4U
USING 'ORCL';
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SQL>
SQL> CREATE PUBLIC DATABASE LINK test_hr
CONNECT TO hr IDENTIFIED BY oracle_4U
USING 'ORCL';
2 3
Database link created.
SQL>
2. Test the database connection as the database user SCOTT by selecting from the
EMPLOYEES table through the database link.
Any database user will be able to use this database link because it is declared PUBLIC.
Connected as SYSTEM, open the SCOTT account, and then test the database link.
SQL> ALTER USER scott IDENTIFIED BY oracle_4U ACCOUNT UNLOCK;
User altered.
MAX(SALARY)
-----------
24000
SQL>
3. View the data dictionary information about the database link. Find the username and
password as they are stored in the database.
a. Connect as SYSTEM and query the DBA_DB_LINKS view for the database link
information.
SQL>
The SYSTEM user is granted the SELECT ANY DICTIONARY privilege but cannot view the
SYS.LINK$ table.
4. View the base SYS table for the database links. As the SYS user, view the LINK$ table. Is
the password visible in this table? Describe the table to view all columns. Query the table to
view passwords. Note that all passwords are encrypted. None are stored in clear text.
SQL> CONNECT / as sysdba
Connected.
SQL> desc link$
SQL>
Note that you are connected to the root container. You created the database link in the
PDB1_1 container.
SQL> CONNECT sys@pdb1_1 as sysdba
Enter password: ******
Connected.
NAME
--------------------------------------------------------
AUTHUSR
--------------------------------------------------------
AUTHPWD
--------------------------------------------------------
PASSWORDX
--------------------------------------------------------
AUTHPWDX
--------------------------------------------------------
TEST_HR
SQL>
Tasks
1. While you are still connected to pdb1_1, create the MIKE user and grant him the HR_MGR
role.
SQL> SET ECHO ON
SQL> DROP ROLE HR_MGR;
DROP ROLE HR_MGR
*
Role created.
User created.
Grant succeeded.
Grant succeeded.
SQL>
2. Create the hrviewlink database link.
SQL> CONNECT hr@pdb1_1
Enter password:
User altered.
SQL>
3. Create the employees_vw view and check that it allows you to retrieve
HR.EMPLOYEES@hrviewlink rows.
SQL> CREATE VIEW employees_vw as
SELECT * FROM HR.EMPLOYEES@hrviewlink;
2
View created.
Grant succeeded.
SQL>
4. Connect as MIKE and test the view.
SQL> CONNECT mike@pdb1_1
Enter password: ******
Connected.
SQL> UPDATE hr.EMPLOYEES_VW SET SALARY = 10000
WHERE employee_id = 206;
SQL> ROLLBACK;
Rollback complete.
SQL>
5. Attempt to view some other table HR.DEPARTMENTS of the HR schema.
SQL> SELECT * FROM hr.departments@hrviewlink;
SELECT * FROM hr.departments@hrviewlink
*
ERROR at line 1:
ORA-02019: connection description for remote database not found
SQL> EXIT
$
Assumptions
You successfully completed Practice 6-1 Task 1.
Tasks
The batch processes have been moved to a client machine. The batch processes will continue
using the /@netservice_name login for database connections. However, you must follow
security best practices: hence remote OS authentication (REMOTE_OS_AUTHENT) is not
export TNS_ADMIN=/home/fred/oracle/network
SQLNET.WALLET_OVERRIDE = TRUE
The sqlnet.ora file has three parameters for configuring the secure external password
store: WALLET_LOCATION, SQLNET.WALLET_OVERRIDE, and
SQLNET.AUTHENTICATION.SERVICES.
• WALLET_LOCATION points to the directory where the wallet resides; this parameter
exists in earlier versions.
• Set the SQLNET.WALLET_OVERRIDE parameter to TRUE. This setting causes all
CONNECT /@db_connect_string statements to use the information in the wallet at
the specified location to authenticate to databases.
• If an application uses SSL for encryption, the sqlnet.ora parameter,
SQLNET.AUTHENTICATION_SERVICES, specifies SSL and an SSL wallet is created.
If this application wants to use secret store credentials to authenticate to databases
(instead of the SSL certificate), those credentials must be stored in the SSL wallet. If
SQLNET.WALLET_OVERRIDE = TRUE, the usernames and passwords from the wallet
are used to authenticate to databases. If SQLNET.WALLET_OVERRIDE = FALSE, the
SSL certificate is used.
$ cat sqlnet.ora
WALLET_LOCATION =
(SOURCE =
(METHOD = FILE)
(METHOD_DATA =
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQLNET.WALLET_OVERRIDE = TRUE
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
$
c. Test the HR_SEC net service name.
$ tnsping HR_SEC
SQL> exit
$
f. Clear the TNS_ADMIN environment variable.
$ unset TNS_ADMIN
$
12. To clean up after this practice, reset the OS_AUTHENT_PREFIX parameter to the default
values in the ORCL instance.
$ sqlplus / as sysdba
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
System altered.
Tasks
1. Create the common user C##U1 in cdb1.
$ . oraenv
ORACLE_SID = [cdb1] ? cdb1
The Oracle base for
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
User created.
Grant succeeded.
SQL>
2. Connect as C##U1 in the root.
SQL> CONNECT c##u1
Enter password: ******
Connected.
SQL> SHOW CON_NAME
CON_NAME
------------------------------
CDB$ROOT
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
CON_NAME
------------------------------
PDB1_1
SQL>
4. Connect as C##U1 in pdb1_2.
SQL> CONNECT c##u1@pdb1_2
CON_NAME
------------------------------
PDB1_2
SQL>
SQL>
Notice that the same password is used to connect to any container of cdb1.
5. Create the local user LOCAL_EMPLOYEE in pdb1_1.
a. Connect as SYSTEM in pdb1_1.
SQL> CONNECT system@pdb1_1
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SQL>
b. Create the local user LOCAL_EMPLOYEE.
SQL> CREATE USER local_employee IDENTIFIED BY pass_pdb1;
User created.
Grant succeeded.
SQL>
c. Connect as LOCAL_EMPLOYEE in pdb1_1.
SQL> CONNECT local_employee@pdb1_1
Enter password: ******
Connected.
SQL>
d. Connect as LOCAL_EMPLOYEE in pdb1_2.
SQL> CONNECT local_employee@pdb1_2
User created.
Grant succeeded.
SQL>
c. Connect as LOCAL_EMPLOYEE in pdb1_2.
SQL> CONNECT local_employee@pdb1_2
Enter password: ******
Connected.
SQL>
Task
1. If you did not create the SEC user in Practice 4-1, run the
/home/oracle/labs/USERS/create_sec.sh script to create this user. As the SEC
user, create a user to simulate a middle-tier user.
a. Create a user with the following properties:
Username: HRAPP
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
User created.
SQL>
SQL> GRANT create session TO hrapp;
Grant succeeded.
SQL>
b. Verify that HRAPP can connect. (Be aware of the uppercase password).
SQL> connect hrapp
Enter password: ******
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL>
SQL> DROP USER pfay CASCADE;
DROP USER pfay CASCADE
*
ERROR at line 1:
ORA-01918: user 'PFAY' does not exist
User created.
Grant succeeded.
User altered.
SQL> EXIT
$
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL>
SQL> COL proxy FORMAT A6
SQL> COL client FORMAT A6
SQL> COL authentication FORMAT A12 WORD
SQL>
SQL> SELECT proxy,
client,
authentication,
authorization_constraint
FROM dba_proxies
WHERE proxy = 'HRAPP';
2 3 4 5 6
PROXY CLIENT AUTHENTICATI AUTHORIZATION_CONSTRAINT
------ ------ ------------ -----------------------------------
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
6. Modify the PFAY user so that a password is required when connecting through a middle
tier.
SQL> ALTER USER pfay
GRANT CONNECT THROUGH hrapp AUTHENTICATION REQUIRED;
2
User altered.
SQL> exit
$
7. In the terminal window, run proxy_user with the following command line:
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Error - ORA-28183: proper authentication not provided by proxy
Error - OCI_INVALID_HANDLE
Hit enter to end connections:
$
8. Run proxy_user with the following command line:
Connected to:
SQL>
SQL> COL proxy FORMAT A6
SQL> COL client FORMAT A6
SQL> COL authentication FORMAT A12 WORD
SQL>
SQL> SELECT
proxy,
client,
authentication,
SQL>
10. Change the PFAY user so that she can no longer connect through the middle tier.
SQL> ALTER USER pfay REVOKE CONNECT THROUGH hrapp;
User altered.
SQL> exit
$
11. Run proxy_user with the following command:
$ ./proxy_user orcl pfay oracle_4U
This command connects PFAY with a password. Should this work? Why?
Answer: The program works because the PFAY user connects with a password.
$ ./proxy_user orcl pfay oracle_4U
Database: orcl
Username: pfay
Password: oracle_4U
Successful connection: Username: HRAPP
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Error - ORA-01017: invalid username/password; logon denied
Error - OCI_INVALID_HANDLE
Hit enter to end connections:
$
13. Display the audited connections as the proxy user.
$ sqlplus / AS SYSDBA
PFAY HRAPP 0
(TYPE=(PROXY));(CLIENT
ADDRESS=((ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=242
83))));
… rows deleted
SQL> EXIT
$
Tasks
1. Investigate the number of privileges of the DBA in the non-CDB.
a. Use the oraenv utility to set the ORACLE_SID environment variable to the orcl value.
$ . oraenv
ORACLE_SID = [orcl] ? orcl
The Oracle base for
ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 is
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
ROLE
----------------------------------------------------------------
-
AQ_ADMINISTRATOR_ROLE
CAPTURE_ADMIN
DATAPUMP_EXP_FULL_DATABASE
DATAPUMP_IMP_FULL_DATABASE
DBA
DELETE_CATALOG_ROLE
EM_EXPRESS_ALL
EM_EXPRESS_BASIC
EXECUTE_CATALOG_ROLE
EXP_FULL_DATABASE
GATHER_SYSTEM_STATISTICS
HS_ADMIN_EXECUTE_ROLE
25 rows selected.
PRIVILEGE
----------------------------------------
ADMINISTER ANY SQL TUNING SET
ADMINISTER DATABASE TRIGGER
ADMINISTER RESOURCE MANAGER
ADMINISTER SQL MANAGEMENT OBJECT
ADMINISTER SQL TUNING SET
ADVISOR
… rows deleted
UNLIMITED TABLESPACE
UPDATE ANY CUBE
UPDATE ANY CUBE BUILD PROCESS
UPDATE ANY CUBE DIMENSION
UPDATE ANY TABLE
USE ANY SQL TRANSLATION PROFILE
SQL>
Notice that the SYSTEM user is not granted the SYSDBA privilege.
c. Connect as SYS in orcl instance.
SQL> CONNECT / AS SYSDBA
Connected.
SQL> SELECT * FROM session_roles ORDER BY 1;
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
PRIVILEGE
----------------------------------------
ADMINISTER ANY SQL TUNING SET
ADMINISTER DATABASE TRIGGER
… rows deleted
SYSDBA
SYSOPER
SQL> EXIT
$
2. Now investigate if there are distinct DBAs for the root container and in the pdb1_1 and
pdb1_2 containers in cdb1 instance.
a. Use the oraenv utility to set the ORACLE_SID environment variable to the cdb1 value.
$ . oraenv
ORACLE_SID = [orcl] ? cdb1
The Oracle base for
ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1 is
/u01/app/oracle
$
b. Connect as SYSTEM in cdb1 instance.
$ sqlplus system
24 rows selected.
SQL>
There are two types of DBA roles. The common DBA role systematically granted to any
SYSTEM user created in a new PDB: the DBA role owns many system privileges. The
common PDB_DBA role is also systematically granted to any SYSTEM user created in a new
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
USERNAME CON_ID
-------------- ----------
SYSTEM 1
SYSTEM 4
SYSTEM 3
SQL>
There are as many DBAs as containers: one for the root container and one DBA for each
PDB.
c. Connect as the pdb1_1 DBA to create a junior DBA who you grant the local PDB_DBA
role.
SQL> CONNECT system@pdb1_1
Enter password: ******
Connected.
SQL> COL grantee FORMAT A16
SQL> COL privilege FORMAT A26
SQL> SELECT * FROM dba_sys_privs WHERE grantee='PDB_DBA';
User created.
Grant succeeded.
PRIVILEGE
----------------------------------------
CREATE SESSION
7 rows selected.
SQL>
d. Connect as the pdb1_2 DBA to create a junior DBA who you grant the local PDB_DBA
role with different privileges.
SQL> CONNECT system@pdb1_2
Enter password: ******
Connected.
SQL> CREATE USER dba_junior IDENTIFIED BY oracle_4U;
User created.
Grant succeeded.
PRIVILEGE
--------------------------
SET CONTAINER
CREATE PLUGGABLE DATABASE
CREATE ROLE
CREATE USER
CREATE TABLESPACE
CREATE SESSION
SQL> EXIT
$
Tasks
1. Make sure you are in the ~/labs/PRIV directory and your environment points to the orcl
instance.
$ cd ~/labs/PRIV
$ . oraenv
ORACLE_SID = [cdb1] ? orcl
The Oracle base for
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
PRIVILEGE
----------------------------------------
SYSBACKUP
SELECT ANY TRANSACTION
SELECT ANY DICTIONARY
RESUMABLE
CREATE ANY DIRECTORY
ALTER DATABASE
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
14 rows selected.
SQL>
PRIVILEGE
----------------------------------------
EXEMPT DDL REDACTION POLICY
EXEMPT DML REDACTION POLICY
LOGMINING
rows deleted …
AUDIT SYSTEM
ALTER SYSTEM
SQL>
6. Display from the V$PWFILE_USERS view. SYS user is the only user defined in the
password file with SYSDBA and SYSOPER privileges only. SYSBACKUP user is not registered
in the password file.
SQL> select * from v$pwfile_users;
SQL>
7. Create a new user JOHN that will be granted the SYSBACKUP privilege in order to perform
backup, restore, and recover operations, hence act as the SYSBACKUP user.
User created.
SQL> EXIT
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
Grant succeeded.
SQL>
RMAN-00571: ==================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS
RMAN-00571: ==================================================
RMAN-00554: initialization of internal recovery manager package
failed
RMAN-04005: error from target database:
ORA-01031: insufficient privileges
$
$ rman target '"john@orcl AS SYSBACKUP"'
RMAN> exit
This practice depends on Practices 4-1 and 8-1 for users and roles. It assumes that the SEC
user has been created and granted certain privileges, and that the PFAY and HRAPP users have
also been created.
Tasks
1. As the SEC user, create the HR_EMP_CLERK and HR_EMP_MGR roles. If you need to create
the SEC user, use the /home/oracle/labs/USERS/create_sec.sh shell script.
$ sqlplus sec
Enter password: ******
Role created.
Role created.
SQL>
2. Grant PFAY the HR_EMP_CLERK and HR_EMP_MGR roles. The PFAY user was created in
Practice 8-1.
SQL> GRANT hr_emp_clerk, hr_emp_mgr TO pfay;
Grant succeeded.
SQL>
3. Give PFAY the ability to enable the HR_EMP_CLERK role through the HRAPP middle tier.
SQL> ALTER USER pfay
GRANT CONNECT THROUGH hrapp
WITH ROLE hr_emp_clerk;
2 3
User altered.
SQL> EXIT
$
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Be sure to use the name of your database instead of orcl. This works because PFAY can
enable the HR_EMP_CLERK role through HRAPP.
Note: Because each connection has its own thread, the following output is not sequential
and the order of the output lines may differ for each execution.
$ /home/oracle/labs/PROXY/proxy_role orcl hr_emp_clerk pfay
Database: orcl
Role: hr_emp_clerk
Username: pfay
Password:
Successful connection: Username: HRAPP
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Successful connection: Username: pfay
Role successfully enabled: hr_emp_clerk
Successful connection: Username: pfay
Role successfully enabled: hr_emp_clerk
Successful connection: Username: pfay
Successful connection: Username: pfay
Role successfully enabled: hr_emp_clerk
Role successfully enabled: hr_emp_clerk
SQL>
7. Look at the tab_app_roles.sql script. It creates a table similar to the one presented in
the lesson, which is used to limit the IP addresses from which users can enable roles.
Execute the script. Note that the SEC user connects through the listener. The
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
SQL> DROP TABLE app_roles;
DROP TABLE app_roles
*
ERROR at line 1:
ORA-00942: table or view does not exist
Table created.
1 row created.
SQL> COMMIT;
Commit complete.
SQL>
8. As the SEC user, drop the HR_EMP_MGR role.
SQL> DROP ROLE hr_emp_mgr;
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
9. Create a secure application role with the following properties:
Name: HR_EMP_MGR
Enabled in the SEC.APP_ROLES_PKG package
SQL> CREATE ROLE hr_emp_mgr IDENTIFIED USING sec.app_roles_pkg;
Role created.
SQL>
SQL>
Package created.
SQL>
SQL> CREATE OR REPLACE PACKAGE BODY app_roles_pkg IS
PROCEDURE set_role (
p_role_name VARCHAR2 )
AS
v_id app_roles.id%TYPE;
BEGIN
SELECT id
INTO v_id
FROM sec.app_roles
WHERE username =
sys_context('userenv','current_user')
AND role = p_role_name
AND ip_address = sys_context('userenv','ip_address');
dbms_session.set_role(p_role_name);
END;
END;
/
SQL>
11. As the SEC user, allow anyone to execute the SEC.APP_ROLES_PKG package and select
from the SEC.APP_ROLES table. The user needs read access to the table because the
package runs by using the privileges of the current user. What security problems does this
create, and how can they be resolved?
SQL> GRANT execute ON app_roles_pkg TO public;
Grant succeeded.
Grant succeeded.
SQL>
12. Allowing anyone to execute the SEC.APP_ROLES_PKG package does not create any
security problems because the appropriate row must appear in the APP_ROLES table
before a role can be enabled. Giving read access to SEC.APP_ROLES allows any user to
see which users can enable which roles from a client. If this is determined to be a security
risk, you can create a view that shows only those rows that are related to the current user.
The view would include the following predicate:
WHERE username = sys_context('userenv','current_user')
Test by performing the following steps:
a. Connect as PFAY through the listener (you must use a service name orcl). Be sure
to use your instance name instead of orcl.
b. Query SESSION_ROLES to see which roles are enabled.
c. Use the SEC.APP_ROLES_PKG package to enable the role.
d. Query SESSION_ROLES to see which roles are enabled.
Note: The HR_EMP_CLERK role that is enabled after the initial connection is from a
previous step.
SQL> CONNECT pfay@orcl
Enter password: ******
Connected.
SQL>
SQL> SELECT * FROM session_roles;
ROLE
------------------------------
HR_EMP_CLERK
SQL>
SQL> SELECT * FROM session_roles;
ROLE
------------------------------
HR_EMP_MGR
SQL>
SQL>
14. As the SEC user, select the secure application role information from the data dictionary.
SQL> CONNECT sec
Enter password: ******
Connected.
SQL>
SQL> COL role FORMAT A12
SQL> COL schema FORMAT A12
SQL> COL package FORMAT A30
SQL>
SQL> SELECT *
FROM dba_application_roles
WHERE ROLE = 'HR_EMP_MGR';
2 3 4
SQL>
Tasks
1. Before testing the CBAC feature, execute the CBAC_priv.sql script. This script creates
the end users U1 and the schema APP, and the APP.T1 table.
SQL> CONNECT / as sysdba
Connected.
SQL> @/home/oracle/labs/PRIV/CBAC_priv.sql
SQL>
SQL> create user u1 identified by oracle_4U default tablespace
users;
User created.
Grant succeeded.
User created.
Grant succeeded.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Table created.
1 row created.
SQL>
SQL> commit;
SQL>
SQL>
2. The APP schema creates two procedures: an invoker’s right procedure, IVPROC and a
definer’s right procedure, DFPROC.
a. Create the two procedures using the following codes:
CREATE OR REPLACE PROCEDURE app.ivproc (CODE in varchar2)
AUTHID CURRENT_USER AS
v_code number;
BEGIN
SELECT code INTO v_code FROM app.t1;
dbms_output.put_line('Code is: '||v_code);
END ivproc;
/
SQL> CONNECT app
Enter password: ******
Connected.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
3. You create the ROLE1 role. Grant SELECT on APP.T1 to the role. Create ROLE2. Grant
SELECT on SH.SALES to the role and grant the role directly to the end user U1.
SQL> CONNECT / as sysdba
Connected.
Role created.
Grant succeeded.
Role created.
Grant succeeded.
SQL>
4. Grant the ROLE1 role to invoker’s right procedure, IVPROC and to the definer’s right
procedure, DFPROC.
SQL> CONNECT app
Enter password: ******
Connected.
SQL>
5. Because the CBAC roles can only be granted to a program unit when the role is directly
granted to the procedures’ owner, grant the ROLE1 role to the APP procedures’ owner.
SQL> CONNECT / as sysdba
Connected.
SQL> GRANT role1 TO app;
Grant succeeded.
SQL>
6. Now grant the role to the procedural units.
SQL> CONNECT app
Enter password: ******
Connected.
SQL> GRANT role1 TO PROCEDURE app.ivproc, PROCEDURE app.dfproc ;
Grant succeeded.
SQL>
7. Grant the EXECUTE privilege on both procedures to the U1 end user.
SQL> GRANT execute ON app.ivproc TO u1;
Grant succeeded.
Grant succeeded.
SQL>
8. Connect as U1 and test how the CBAC enables roles at run time.
a. Test the app.ivproc procedure.
SQL> CONNECT u1
Enter password: ******
Connected.
SQL> SELECT * FROM session_roles;
ROLE
----------------------------------------------------------------
-
ROLE2
SQL>
Notice that the active role at login time is ROLE2 only.
b. Test the app.dfproc procedure.
SQL> EXEC app.dfproc(1)
Code is from Definer right procedure: 1
ROLE
SQL>
Notice that the execution completes as in 8.a.
c. Drop ROLE1 and retest.
SQL> CONNECT system
Enter password: ******
Connected.
SQL> DROP ROLE role1;
Role dropped.
ROLE
----------------------------------------------------------------
-
ROLE2
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at "APP.IVPROC", line 5
ORA-06512: at line 1
SQL>
Tasks
1. Connected as SYSTEM, execute the inherit_priv.sql script to create U1, U2 and KATE users
and the U2.T1 table.
SQL> CONNECT system
Enter password: ******
Connected.
User dropped.
User created.
Grant succeeded.
Revoke succeeded.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
User created.
Grant succeeded.
Grant succeeded.
Table created.
1 row created.
SQL> commit;
Commit complete.
Grant succeeded.
Grant succeeded.
SQL>
SQL>
SQL>
c. Execute the procedure to test that it works successfully.
SQL> set serveroutput on
SQL> exec U1.PROC2('Code')
Code is: 1
SQL>
d. The developer U1 grants the EXECUTE privilege to the KATE user.
SQL> grant execute on U1.PROC2 to KATE;
Grant succeeded.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
*
ERROR at line 1:
ORA-06598: insufficient INHERIT PRIVILEGES privilege
SQL>
b. KATE grants the INHERIT PRIVILEGES on user KATE to procedure owner U1 thus
allowing U1 to inherit her privileges during the execution of the procedure
SQL> grant INHERIT PRIVILEGES ON USER kate TO U1;
Grant succeeded.
SQL>
c. KATE re-executes the procedure.
SQL> exec U1.PROC2('Code')
Code is: 1
SQL>
4. Display the users being granted the INHERIT PRIVILEGES privilege. There is a new
object type ‘USER’ and the table name is the user name controlling who can access his
privileges when he runs an invoker’s rights procedure.
SQL> connect / as sysdba
Connected.
SQL>
5. Be aware that newly created users are granted the INHERIT PRIVILEGES privilege
because the INHERIT PRIVILEGES privilege is granted to PUBLIC. The user KATE was
revoked the INHERIT PRIVILEGES privilege at the beginning of the practice.
a. Create a new user.
SQL> CREATE USER newuser IDENTIFIED BY newuser;
SQL>
b. Check the privileges granted to NEWUSER.
SQL> select PRIVILEGE, TYPE, TABLE_NAME, GRANTEE
from DBA_TAB_PRIVS
where grantor='NEWUSER';
2 3 4
SQL> EXIT
$
Assumption
The bequeath_setup.sql script is successfully completed.
Tasks
1. Make sure you are at the ~/labs/PRIV directory and your environment points to the orcl
instance. Connect under SYSTEM user.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL>
SQL> @bequeath_setup.sql
Connected.
REVOKE select any table from OE
*
ERROR at line 1:
ORA-01952: system privileges not granted to 'OE'
User dropped.
User dropped.
User created.
Grant succeeded.
Revoke succeeded.
User created.
Grant succeeded.
SQL>
SQL>
b. The developer checks that the view V_WHOAMI works successfully.
SQL> select * from U1.V_WHOAMI;
WHOAMI
---------------------------------------------------------
U1
SQL>
4. The same developer U1 creates an BEQUEATH DEFINER view. The view displays the
current user connected.
a. The user U1 connects and creates the view V_WHOAMI_DEF.
SQL> CREATE OR REPLACE VIEW u1.v_whoami_def
BEQUEATH DEFINER
AS SELECT ORA_INVOKING_USER "WHOAMI" FROM DUAL;
2 3
View created.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
WHOAMI
---------------------------------------------------------
U1
SQL>
5. The developer U1 grants the SELECT privilege to KATE on both views.
SQL> grant SELECT on U1.V_WHOAMI to KATE;
Grant succeeded.
Grant succeeded.
SQL>
6. KATE connects and selects data from the BEQUEATH DEFINER view.
SQL> CONNECT kate
Enter password: ******
Connected.
SQL> select * from U1.V_WHOAMI_DEF;
WHOAMI
--------------------------------------------------------
KATE
SQL>
7. KATE selects data from the BEQUEATH CURRENT_USER view.
SQL> SELECT * FROM U1.V_WHOAMI;
select * from U1.V_WHOAMI
*
ERROR at line 1:
ORA-06598: insufficient INHERIT PRIVILEGES privilege
SQL>
Grant succeeded.
SQL>
9. KATE attempts the statement on the BEQUEATH CURRENT_USER view.
SQL> select * from U1.V_WHOAMI;
WHOAMI
----------------------------------------------------------
SQL> EXIT
$
Assumptions
The following users have been successfully created from previous practice 6-5.
• C##U1 common user in cdb1
• LOCAL_EMPLOYEE local user in pdb1_1 (password pass_pdb1)
• LOCAL_EMPLOYEE local user in pdb1_2 (password pass_pdb2)
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real
Application Testing options
SQL>
The common role is replicated in each container. The container ID 1 is the root. The
container ID 2 is the seed. The container ID 3 is the pdb1_1. The container ID 4 is the
pdb1_2.
2. View all common roles of the root.
SQL> select ROLE, COMMON from cdb_roles
WHERE CON_ID = 1
order by role;
2 3
ROLE COM
------------------------------ ---
ADM_PARALLEL_EXECUTE_TASK YES
APEX_ADMINISTRATOR_ROLE YES
APEX_GRANTS_FOR_NEW_USERS_ROLE YES
AQ_ADMINISTRATOR_ROLE YES
AQ_USER_ROLE YES
AUDIT_ADMIN YES
AUDIT_VIEWER YES
…
CDB_DBA YES
CONNECT YES
…
DBA YES
…
XS_RESOURCE YES
XS_SESSION_ADMIN YES
84 rows selected.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
4. Create a common C##_ROLE in root.
SQL> create role c##_role container=ALL;
SQL>
5. Attempt to create a LOCAL_ROLE local role in root.
SQL> create role local_role container=CURRENT;
create role local_role container=CURRENT
*
ERROR at line 1:
ORA-65049: creation of local user or role is not allowed in
CDB$ROOT
SQL>
You get an error message because no local role is authorized in the root.
6. Create a common role in pdb1_2.
SQL> CONNECT system@pdb1_2
Enter password: ******
Connected.
SQL> CREATE ROLE c##_role_PDB1_2 container=ALL;
create role c##_role_PDB1_2 container=ALL
*
ERROR at line 1:
ORA-65050: Common DDLs only allowed in CDB$ROOT
SQL>
You get an error message because no common role can be created from a PDB.
Role created.
ROLE COM
------------------------------ ---
ADM_PARALLEL_EXECUTE_TASK YES
APEX_ADMINISTRATOR_ROLE YES
…
86 rows selected.
SQL>
8. Grant common or local roles as common or local.
a. Grant a common role to a common user from the root.
SQL> connect / as sysdba
Connected.
SQL> grant c##_role to c##u1;
Grant succeeded.
SQL>
Note that the common role is granted locally to the common user. The granted role is only
applicable in the root.
SQL> connect c##u1
Enter password: ******
Connected.
SQL> select * from session_roles;
no rows selected
SQL>
b. Now grant the common role to a common user from the root as common, to be
applicable in all containers.
SQL> connect / as sysdba
Connected.
SQL> grant c##_role to c##u1 container=all;
Grant succeeded.
SQL>
ROLE
------------------------------
ROLE
------------------------------
C##_ROLE
SQL>
9. Revoke the common role from the common user so that the role cannot be used in any
container.
SQL> connect / as sysdba
Connected.
SQL> revoke c##_role from c##u1 container=all;
Revoke succeeded.
no rows selected
SQL>
10. Grant a common role to a local user from the root.
SQL> connect / as sysdba
Connected.
SQL> grant c##_role to local_employee;
grant c##_role to local_employee
*
SQL>
Note that the user is unknown in root. It is a local user in pdb1_2.
11. Grant a common role to a local user in pdb1_2.
SQL> connect system@PDB1_2
Enter password: ******
Connected.
SQL> grant c##_role to local_employee;
Grant succeeded.
SQL>
Note that the user is granted a common role locally (common column = NO) applicable only
in the pdb1_2.
12. Test the connection as the local user. The password is pass_pdb2.
SQL> connect local_employee@PDB1_2
Enter password: ******
Connected.
SQL> select * from session_roles;
ROLE
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
13. Grant a common role to a local user from pdb1_2 applicable in all containers.
SQL> connect system@PDB1_2
Enter password: ******
Connected.
SQL> grant c##_role to local_employee container=all;
grant c##_role to local_user_pdb2 container=all
*
ERROR at line 1:
SQL>
Notice that a common role cannot be granted globally from a PDB.
14. Grant a local role to a local user from pdb1_2.
SQL> grant local_role_pdb1_2 to local_employee;
Grant succeeded.
SQL>
ROLE
------------------------------
C##_ROLE
LOCAL_ROLE_PDB1_2
SQL> EXIT
Tasks
1. Make sure you are at the ~/labs/PRIV directory and your environment points to the orcl
instance.
$ cd ~/labs/PRIV
$ . oraenv
ORACLE_SID = [cdb1] ? orcl
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL> @priv_setup.sql
Connected.
User dropped.
User created.
User dropped.
User created.
Role created.
Grant succeeded.
Grant succeeded.
Role created.
Grant succeeded.
Grant succeeded.
User dropped.
User created.
Grant succeeded.
Revoke succeeded.
User created.
Grant succeeded.
User created.
Grant succeeded.
Table created.
1 row created.
Grant succeeded.
Grant succeeded.
SQL>
3. Define a capture of privileges used by all users. Use the following procedure.
exec SYS.DBMS_PRIVILEGE_CAPTURE.CREATE_CAPTURE ( -
SQL>
SQL>
b. Run the priv_used_by_users.sql script. The script connects as JIM who deletes
rows from HR.EMPLOYEES table and TOM who selects rows from SH.SALES table.
SQL> @priv_used_by_users.sql
24 rows deleted.
Commit complete.
Connected.
SQL>
5. Stop the capture.
SQL> connect system
Enter password: ******
Connected.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.DISABLE_CAPTURE ( -
name => 'All_privs')
SQL>
SQL>
7. Display the object privileges used during the capture period.
SQL> COL username FORMAT A10
SQL> COL object_owner FORMAT A12
SQL> COL object_name FORMAT A30
SQL> COL obj_priv FORMAT A25
13 rows selected.
SQL>
8. Display the system privileges used.
SQL> COL sys_privs form a20
SQL> SELECT username, sys_priv FROM dba_used_sysprivs
WHERE username IN ('JIM', 'TOM');
2
USERNAME SYS_PRIV
---------- --------------------
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
9. Display the path of the privileges used if the privileges were granted to roles, and roles to
users.
SQL> COL object_name FORMAT A10
SQL> COL path FORMAT A32
SQL> COL obj_priv FORMAT A10
SQL> SELECT username, obj_priv, object_name, path
FROM dba_used_objprivs_path
WHERE username IN ('TOM','JIM')
SQL>
10. JIM is granted select, update, delete, insert privileges on HR.EMPLOYEES table through
HR_MGR role. He used the DELETE and SELECT privileges until now.
The unused privileges are visible in DBA_UNUSED_PRIVS view.
SQL> SELECT username, sys_priv, obj_priv, object_name, path
FROM dba_unused_privs
WHERE username='JIM';
SQL>
Role created.
Grant succeeded.
SQL>
c. Finally revoke the powerful privileged role HR_MGR from JIM.
SQL> revoke HR_MGR from JIM;
Revoke succeeded.
SQL>
12. Display the definition of the capture. The ENABLED column ensures that the All_privs
capture has been stopped.
SQL> COL name FORMAT A12
SQL> COL type FORMAT A12
SQL> COL enabled FORMAT A2
SQL> COL roles FORMAT A26
SQL> COL context FORMAT a20
SQL> SELECT name, type, enabled,roles, context
FROM dba_priv_captures;
2
NAME TYPE EN ROLES CONTEXT
------------ ------------ -- -------------------------- --------
-
All_privs DATABASE N
SQL>
SQL>
b. Verify that there is no data left of the All_privs capture.
SQL> SELECT username, sys_priv, obj_priv, object_name, path
FROM dba_unused_privs
WHERE username='JIM';
SQL>
Tasks
1. Define a capture of privileges used by roles HR_MGR_JUNIOR and SALES_CLERK. Use the
following procedure.
exec SYS.DBMS_PRIVILEGE_CAPTURE.CREATE_CAPTURE ( -
name => 'Role_privs', -
description => 'Privs used by HR_MGR_JUNIOR, SALES_CLERK', -
SQL>
2. Start capturing the privileges while users perform their daily work.
a. Start the capture.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.ENABLE_CAPTURE ( -
name => 'Role_privs')
SQL>
b. Run the priv_used_by_users.sql script. The script connects as JIM who deletes
rows from HR.EMPLOYEES table and TOM who selects rows from SH.SALES table.
SQL> @priv_used_by_users.sql
Connected.
0 rows deleted.
Commit complete.
Connected.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
3. Stop the capture.
SQL>
4. Generate the capture results.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.GENERATE_RESULT ( -
name => 'Role_privs')
SQL>
5. Display the object privileges used by the roles HR_MGR_JUNIOR and SALES_CLERK during
the capture period.
SQL> col username FORMAT a8
SQL> col used_role FORMAT a20
SQL> col own FORMAT a4
SQL> SELECT username, object_owner "OWN", object_name,
obj_priv, used_role
FROM dba_used_objprivs
WHERE used_role IN ('HR_MGR_JUNIOR', 'SALES_CLERK');
2 3
USERNAME OWN OBJECT_NAME OBJ_PRIV USED_ROLE
-------- ---- -------------- ---------- -------------------
JIM HR EMPLOYEES SELECT HR_MGR_JUNIOR
TOM SH SALES SELECT SALES_CLERK
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
6. Display the system privileges used by the roles HR_MGR_JUNIOR and SALES_CLERK.
SQL> SELECT username, sys_priv, used_role
FROM dba_used_sysprivs
WHERE used_role IN ('HR_MGR_JUNIOR', 'SALES_CLERK');
2 3
no rows selected
SQL>
7. HR_MGR_JUNIOR is granted select, update, delete on HR.EMPLOYEES table. The role used
SQL>
View the list of unused privileges: this list helps you decide whether to revoke or not the
UPDATE privileges granted through the HR_MGR_JUNIOR role.
8. Display the definition of the capture. The ENABLED column shows that the Role_privs
capture has been stopped. The numbers displayed in the roles list can be different from
those here.
SQL> SELECT name, type, enabled,roles, context
FROM dba_priv_captures;
SQL>
9. Delete the capture so as to remove all previous captured information from the views.
a. Execute the procedure.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.DROP_CAPTURE ( -
name=> 'Role_privs')
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
b. Verify that there is no data left of the Role_privs capture.
SQL> SELECT sys_priv, obj_priv, object_name, path
FROM dba_unused_privs
WHERE rolename IN ('HR_MGR_JUNIOR', 'SALES_CLERK');
2 3
no rows selected
SQL>
Tasks
1. Define a capture of privileges used by the user TOM or by the specific role SALES_CLERK.
Use the following procedure.
exec SYS.DBMS_PRIVILEGE_CAPTURE.CREATE_CAPTURE ( -
name => 'Special_capt', -
description => 'Special', -
SQL>
2. Start capturing privileges while users perform their daily work using the privileges.
a. Start the capture.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.ENABLE_CAPTURE ( -
name => 'Special_capt')
SQL>
b. Run the priv_used_by_users.sql script. The script connects as JIM who deletes
rows from HR.EMPLOYEES table and TOM who selects rows from SH.SALES table.
SQL> @priv_used_by_users.sql
Connected.
0 rows deleted.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Connected.
SQL>
4. Generate the capture results. It may take a few minutes.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.GENERATE_RESULT ( -
name => 'Special_capt')
SQL>
5. Display the object privileges used.
SQL> SELECT username, object_owner, object_name, obj_priv,
used_role
FROM dba_used_objprivs
WHERE username ='TOM' OR used_role='SALES_CLERK';
2 3
USERNAME OBJECT_OWNER OBJECT_NAME OBJ_PRIV USED_ROLE
-------- ------------ -------------- ---------- --------------
TOM SH SALES SELECT SALES_CLERK
SQL>
no rows selected
SQL>
7. TOM is granted the select privilege on the SH.SALES table through SALES_CLERK role. He
used the privilege.
The unused privileges are visible in DBA_UNUSED_PRIVS view.
There are no unused privileges. So there is no privilege that has been unnecessarily
granted.
SQL> SELECT username, sys_priv, obj_priv, object_name, path
FROM dba_unused_privs
SQL>
8. Delete the capture so as to remove all previous captured information from the views.
SQL> exec SYS.DBMS_PRIVILEGE_CAPTURE.DROP_CAPTURE ( -
name=> 'Special_capt')
SQL> EXIT
$
Task
1. Match the following terms with their descriptions:
1. Namespace A. An application context that is accessible only
by the current session
2. Attribute B. An application context whose values can be
shared among sessions
3. USERENV C. The identifier of an application context
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SYS_CONTEXT('USERENV','SESSION_USER')
--------------------------------------------------------------
PFAY
SYS_CONTEXT('USERENV','PROXY_USER')
--------------------------------------------------------------
SYS_CONTEXT('USERENV','IP_ADDRESS')
--------------------------------------------------------------
127.0.0.1(loopback)
SYS_CONTEXT('USERENV','NETWORK_PROTOCOL')
--------------------------------------------------------------
tcp
SYS_CONTEXT('USERENV','AUTHENTICATION_TYPE')
--------------------------------------------------------------
DATABASE
SYS_CONTEXT('USERENV','AUTHENTICATION_DATA')
--------------------------------------------------------------
SYS_CONTEXT('USERENV','CLIENT_IDENTIFIER')
--------------------------------------------------------------
SYS_CONTEXT('USERENV','EXTERNAL_NAME')
--------------------------------------------------------------
SQL>
If the user PFAY was a user known in an LDAP directory, the external name would display
the DN known in the directory, like ‘uid=pfay, ou=People, dc=example, dc=com’.
The session user would display PFAY being the global user name in the database.
SQL> EXEC dbms_output.put_line(sys_context( -
'USERENV', 'CURRENT_USER'));
PFAY
SQL>
If the user PFAY was a user known in an LDAP directory, the external name would display
the DN known in the directory, like ‘uid=pfay, ou=People, dc=example, dc=com’.
4. The security officer grants new roles to PFAY. Use the built-in SYS_SESSION_ROLES
context to indicate whether the roles are enabled after PFAY’s connection.
Note: The SEC user was created in Practice 4-1, step 1.
SQL> CONNECT sec
Enter password: ******
Connected.
SQL> CREATE ROLE role_test;
Role created.
Grant succeeded.
ROLE
Role dropped.
Role dropped.
SQL>
Context created.
SQL>
SQL> CREATE OR REPLACE PACKAGE BODY current_emp IS
PROCEDURE set_emp_info
IS
v_employee_id hr.employees.employee_id%TYPE;
v_first_name hr.employees.first_name%TYPE;
v_last_name hr.employees.last_name%TYPE;
BEGIN
SELECT employee_id,
first_name,
last_name
INTO v_employee_id,
v_first_name,
v_last_name
FROM hr.employees
WHERE email = SYS_CONTEXT('USERENV', 'SESSION_USER');
DBMS_SESSION.SET_CONTEXT('emp_user', 'id',
v_employee_id);
DBMS_SESSION.SET_CONTEXT('emp_user', 'name',
v_first_name || ' ' || v_last_name);
DBMS_SESSION.SET_CONTEXT('emp_user', 'email',
SYS_CONTEXT('USERENV', 'SESSION_USER'));
SQL>
b. Create the logon trigger.
SQL> CREATE or REPLACE TRIGGER emp_logon
SQL>
7. Test the context that you created by performing the following steps:
a. Grant the CREATE SESSION privilege to the user named SKING.
b. Log in as SKING.
c. Use SYS_CONTEXT to verify that the EMP_USER context attributes are set. If you use
DBMS_OUTPUT, remember to issue the SET SERVEROUTPUT ON command.
SQL> GRANT create session TO sking;
Grant succeeded.
SQL>
SQL> CONNECT sking
Enter Password: ******
Connected.
SQL>
SQL> SET SERVEROUTPUT ON
SQL> EXEC dbms_output.put_line(sys_context('emp_user', 'id'))
100
SQL>
8. Still connected as SKING, list all the application context attributes set in the current session.
If Oracle Label Security is installed, the LBAC$LABELS and LBAC$LASTSEQ attributes are
SQL>
SQL>
10. What happens when you call DBMS_SESSION.SET_CONTEXT to set an attribute in the
EMP_USER context? Assume that SKING wants to change the context setting.
Because the application context is set with a package, SKING does not have sufficient
privileges to execute the DBMS_SESSION.SET_CONTEXT procedure.
SQL> CONNECT sking
Enter password:
Connected.
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
list dbms_session.AppCtxTabTyp;
cnt number;
BEGIN
dbms_session.list_context (list, cnt);
IF cnt = 0
THEN dbms_output.put_line('No contexts active.');
ELSE
FOR i IN 1..cnt LOOP
dbms_output.put_line(list(i).namespace
||' ' || list(i).attribute
|| ' = ' || list(i).value);
END LOOP;
END IF;
END;
/
*
ERROR at line 1:
SQL> EXIT
$
Task
1. How does FGAC determine which rows belong in the VPD for the current user?
Fine-grained access control adds a predicate (condition) to the WHERE clause on a SELECT
or DML statement with an AND operator.
2. How does FGAC know which tables are defined in the VPD?
You include a table name or view name when the fine-grained access control policy is
created.
3. In this practice, you implement a security policy that allows users to see only their own rows
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL>
SQL> GRANT execute ON dbms_rls TO sec;
Grant succeeded.
SQL>
5. What privilege exempts the user from access policies? Why does the SEC user need this
privilege? Grant it to SEC.
The EXEMPT ACCESS POLICY privilege is very powerful. Statements that are issued by a
user with this privilege do not have any FGAC policies applied. This privilege can also be
granted by SYSTEM.
Grant succeeded.
SQL>
6. Create the package that is used by the security policy to return a predicate.
a. Create the package specification.
SQL> CONNECT sec
Enter Password: ******
Connected.
SQL>
SQL> CREATE OR REPLACE PACKAGE hr_policy_pkg IS
SQL>
b. Create the package body.
SQL> CREATE OR REPLACE PACKAGE BODY hr_policy_pkg IS
FUNCTION limit_emp_emp (
object_schema IN VARCHAR2,
object_name VARCHAR2 )
RETURN VARCHAR2
IS
v_emp_id NUMBER;
BEGIN
RETURN 'employee_id = SYS_CONTEXT(''emp_user'', ''id'')';
END;
END;
/
2 3 4 5 6 7 8 9 10 11 12
Package body created.
SQL>
c. What predicate does the policy use to limit the rows returned from the EMPLOYEE
table?
employee_id = SYS_CONTEXT('emp_user', 'id')
HR_POLICY_PKG.LIMIT_EMP_EMP('A','B')
----------------------------------------------------------
employee_id = SYS_CONTEXT('emp_user', 'id')
SQL>
*
ERROR at line 1:
ORA-28102: policy does not exist
ORA-06512: at "SYS.DBMS_RLS", line 126
ORA-06512: at line 1
SQL>
9. Set up the SKING user so that he can access the HR.EMPLOYEES table. Because SEC has
GRANT ANY OBJECT PRIVILEGE, the SEC user can grant this privilege. Grant the same
privilege to PFAY.
SQL> GRANT select ON hr.employees TO sking;
Grant succeeded.
SQL>
10. As SKING, display the current context attributes.
SQL> connect sking
Enter Password: ******
Connected.
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
list dbms_session.AppCtxTabTyp;
cnt number;
BEGIN
SQL>
11. Which rows are returned when SKING queries the HR.EMPLOYEES table without a WHERE
clause? Try it.
SQL> select employee_id, first_name, last_name, email
from HR.EMPLOYEES;
2
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL
----------- --------------- ----------------- -----------------
100 Steven King SKING
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
b. You can also use SQL tracing. The user must have the ALTER SESSION privilege to
turn on this type of tracing. SYS has the ability to grant this privilege, but this ability has
not been granted to SEC. To enable a trace that will capture the predicate, execute the
following command:
ALTER SESSION SET EVENTS '10730 TRACE NAME CONTEXT FOREVER,
LEVEL 1';
Grant succeeded.
SQL> EXIT
$
14. View the trace file. The trace file will be created in the Automatic Diagnostics Directory by
default. Look for the file in the $ORACLE_BASE/diag/rdbms/orcl/orcl/trace
directory.
Hint: The ls -ltr command lists the trace files in reverse order by time, so the most
recent files will be at the end of the listing. Also, the trace file will have a .trc extension.
$ cd $ORACLE_BASE/diag/rdbms/orcl/orcl/trace
$ ls -ltr *ora*.trc
…
lines deleted
…
-rw-r----- 1 oracle oinstall 915 Apr 25 03:03
orcl_ora_11899.trc
-rw-r----- 1 oracle oinstall 1033 Apr 25 05:43
orcl_ora_2762.trc
-rw-r----- 1 oracle oinstall 1348 Apr 25 06:06
orcl_ora_5814.trc
$
$ cat orcl_ora_5814.trc
-------------------------------------------------------------
Logon user : SKING
Table/View : HR.EMPLOYEES
VPD Policy name: HR_EMP_POL
Policy function: SEC.HR_POLICY_PKG.LIMIT_EMP_EMP
RLS view :
SELECT
"EMPLOYEE_ID","FIRST_NAME","LAST_NAME","EMAIL","PHONE_NUMBER","H
IRE_DATE","JOB_ID","SALARY","COMMISSION_PCT","MANAGER_ID","DEPAR
TMENT_ID" FROM "HR"."EMPLOYEES" "EMPLOYEES" WHERE (employee_id
= SYS_CONTEXT('emp_user', 'id'))
-------------------------------------------------------------
$
15. Using Enterprise Manager Cloud Control, delete the HR_EMP_POL fine-grained access
control policy.
Step Page Action
a. In the browser, enter the following URL:
https://localhost:7802/em
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 -
64bit Production
With the Partitioning, Oracle Label Security, OLAP, Advanced
Analytics and Real Application Testing options
SQL>
SQL> BEGIN
dbms_rls.add_policy(object_schema => 'HR',
object_name => 'EMPLOYEES',
policy_name => 'HR_EMP_POL',
function_schema => 'SEC',
policy_function => 'HR_POLICY_PKG.LIMIT_EMP_EMP',
statement_types =>'SELECT',
sec_relevant_cols => 'SALARY,COMMISSION_PCT',
sec_relevant_cols_opt => dbms_rls.ALL_ROWS);
END;
/
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
17. Test this new policy with the SKING user. Note that in the first SELECT statement, all the
rows and columns that are requested are shown. In the second SELECT statement, SKING
sees his own salary but no other salary is displayed. Set tracing so that you can view the
changed SQL statement later.
SQL> connect sking
Enter password: *****
Connected.
SQL> COL first_name FORMAT A12
SQL> COL LAST_NAME FORMAT A12
Session altered.
83 rows selected.
83 rows selected.
SQL> EXIT
$
-------------------------------------------------------------
Logon user : SKING
Table/View : HR.EMPLOYEES
VPD Policy name : HR_EMP_POL
Policy function: SEC.HR_POLICY_PKG.LIMIT_EMP_EMP
RLS view :
SELECT
"EMPLOYEE_ID","FIRST_NAME","LAST_NAME","EMAIL","PHONE_NUMBER","H
IRE_DATE","JOB_ID", CASE WHEN (employee_id =
SYS_CONTEXT('emp_user', 'id')) THEN "SALARY" ELSE NULL END
"SALARY", CASE WHEN (employee_id = SYS_CONTEXT('emp_user',
'id')) THEN "COMMISSION_PCT" ELSE NULL END
SQL>
Tasks
1. Create a static policy. The policy calls a function displaying rows in a table depending on
the time.
SQL> exec DBMS_RLS.DROP_POLICY ('HR', 'EMPLOYEES','POL_TIME');
BEGIN DBMS_RLS.DROP_POLICY ('HR', 'EMPLOYEES','POL_TIME'); END;
SQL>
2. Create the function used by the security policy to return a predicate. If the user executes
the query on the HR.EMPLOYEES table after a certain authorized time, the query returns
only the rows where the EMAIL matches the session username, else it returns all rows
whose SALARY is less than 3100. Adapt the time to an appropriate time in the function
according to the current time so that the test becomes relevant.
SQL> !date
Thu Apr 25 10:29:28 UTC 2013
SQL> create or replace function PREDICATE
(obj_schema varchar2, obj_name varchar2)
return varchar2 is d_predicate varchar2(2000);
begin
SQL> !date
Thu Apr 25 10:30:47 UTC 2013
SQL>
4. Test under another user.
SQL> connect pfay
Enter password: ******
Connected.
SQL> !date
Thu Apr 25 10:36:43 UTC 2013
SQL>
SQL>
6. Recreate the function with an appropriate time.
SQL> !date
Thu Apr 25 10:40:54 UTC 2013
SQL> create or replace function PREDICATE
(obj_schema varchar2, obj_name varchar2)
return varchar2 is d_predicate varchar2(2000);
begin
if to_char(sysdate, 'HH24') >= '10'
and to_char(sysdate, 'MI')<'45'
then
d_predicate := 'email = sys_context (''USERENV'' ,
''SESSION_USER'')';
else d_predicate := 'salary <= 3100';
end if;
return d_predicate;
end predicate;
/
2 3 4 5 6 7 8 9 10 11 12 13
Function created.
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
8. Wait 5 minutes and retest to verify that the function is reexecuted.
SQL> !date
Thu Apr 25 10:45:48 UTC 2013
SQL> SELECT email, last_name, salary FROM hr.employees;
6 rows selected.
SQL>
SQL> connect sking
Enter password: ******
Connected.
SQL> SELECT email, last_name, salary FROM hr.employees;
6 rows selected.
SQL>
SQL>
10. Drop the EMP_USER context, the CURRENT_EMP package and the logon trigger.
SQL> DROP CONTEXT EMP_USER;
Context dropped.
Package dropped.
Trigger dropped.
SQL>
Tasks
1. Create a VPD policy using the FUN function as follows:
a. Create the function.
SQL> create or replace function fun
(object_schema varchar2, object_name varchar2)
return varchar2
is
SQL>
b. Create the VPD policy.
SQL> EXEC dbms_rls.drop_policy('HR', 'EMPLOYEES','FUN_POLICY')
BEGIN dbms_rls.drop_policy('HR', 'EMPLOYEES', 'FUN_POLICY')
END;
*
ERROR at line 1:
ORA-28102: policy does not exist
ORA-06512: at "SYS.DBMS_RLS", line 126
ORA-06512: at line 1
SQL> BEGIN
dbms_rls.add_policy
(object_schema => 'HR', object_name => 'EMPLOYEES',
policy_name => 'fun_policy',
function_schema => 'SEC',
policy_function => 'FUN',
statement_types => 'select, index',
policy_type => dbms_rls.CONTEXT_SENSITIVE);
SQL>
2. Connect as SKING to test the policy.
SQL> conn sking
Enter password: ******
Connected.
SQL> SELECT email FROM hr.employees;
SELECT email FROM hr.employees
SQL>
You did not get an error at the policy creation but at run time.
3. Trace the statement and analyze the trace file.
a. Trace your session and reexecute the statement.
SQL> ALTER SESSION SET EVENTS '10730 TRACE NAME CONTEXT FOREVER,
LEVEL 1';
Session altered.
SQL> SELECT email FROM hr.employees;
SELECT email FROM hr.employees
*
ERROR at line 1:
ORA-28113: policy predicate has error
SQL> EXIT
$
b. Analyze the trace file.
$ ls -ltr *ora*.trc
…
lines deleted
…
-rw-r----- 1 oracle oinstall 6083 Apr 25 11:46
orcl_mmon_6671.trc
-rw-r----- 1 oracle oinstall 119 Apr 25 11:49
orcl_ora_21114.trm
SQL>
5. Connect as SKING to retest the policy.
SQL> conn sking
Enter password: ********
Connected.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
SQL>
6. There is still an error. Proceed as in the previous steps.
a. Trace your session and reexecute the statement.
SQL> ALTER SESSION SET EVENTS '10730 TRACE NAME CONTEXT FOREVER,
LEVEL 1';
SQL> EXIT
$
b. Analyze the trace file.
$ cd $ORACLE_BASE/diag/rdbms/orcl/orcl/trace
$ ls -ltr *ora*.trc
…
lines deleted
…
-rw-r----- 1 oracle oinstall 6345 Apr 25 11:56
orcl_mmon_6671.trc
-rw-r----- 1 oracle oinstall 100 Apr 25 11:59
orcl_ora_22796.trm
-rw-r----- 1 oracle oinstall 3258 Apr 25 11:59
orcl_ora_22796.trc
$ cat orcl_ora_22796.trc
...
*** 2013-04-25 11:59:04.588
-------------------------------------------------------------
-------------------------------------------------------------
Error information for ORA-28113:
Logon user : SKING
Table/View : HR.EMPLOYEES
VPD Policy name : FUN_POLICY
SQL>
8. Connect as SKING to retest the policy.
SQL> conn sking
Enter password: ********
Connected.
SQL> SELECT email FROM hr.employees;
EMAIL
-------------------------
SKING
SQL>
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Tasks
1. Find all VPD policies.
SQL> conn sec
Enter password: ******
Connected.
SQL> SELECT policy_name FROM dba_policies;
SQL>
2. Drop each VPD policy listed in step 1.
SQL> exec DBMS_RLS.DROP_POLICY ('HR','EMPLOYEES','FUN_POLICY')
SQL> EXIT
$