Search code examples
sqloraclestored-proceduresplsqldynamic-sql

ORACLE - Why does a dynamic SQL statement using DBMS_RANDOM fail when called from a stored procedure but not from an anonymous block


EDIT (8/25)

Thanks to @Alex Poole for providing the answer below but I wanted to share additional detail on these role limitations around PL/SQL objects as it helps explain not only how Oracle is managing things under the hood but why it handles permissions this way.

Now knowing where I was going wrong, I was able to identify this question which discusses the issue at length. This answer describes how the Oracle data structures store the permissions for evaluation.

In addition, someone linked an explanation from Tom Kyte which explains why this behavior was coded intentionally. Long story short: PL/SQL Definer's Rights do not respect role based permissions due to how the Oracle engine compiles these objects. If role based permissions were allowed then any REVOKE statements could have the ability of invalidating large swaths of PL/SQL objects, requiring a costly full database recompile.


Original Question

Could someone help me understand why I can call a dynamic sql script containing a reference to a DBMS_RANDOM procedure when the logic is called from an anonymous block, however, when I take that same logic and drop it into my own stored procedure, the previously runnable script fails to execute with a ORA-00904: "DBMS_RANDOM"."STRING": invalid identifier error?

I feel confident that my privileges are correct. I can run the script that is being passed as a variable directly without issue and run this logic as an anonymous PL/SQL block. Do I need to change my syntax with the stored proc or is it possible that this practice is prevented for security reasons?

Any explanation would be great but if you can point me to the Oracle documentation, I would be ecstatic. I have looked extensively, especially around Oracle's Dynamic SQL documentation but I haven't seen a description of this behavior. I am using Oracle 11g.

To recreate the behavior I am seeing:

Test Data Creation:

SPOOL ON;
SET SERVEROUTPUT ON SIZE UNLIMITED;

--Create Test Table
CREATE TABLE TEST_DYNAMIC_TBL (
ID NUMBER PRIMARY KEY,
MY_COL VARCHAR2(50));

--INSERT a line of data and confirm
INSERT INTO TEST_DYNAMIC_TBL VALUES(1, 'SOME TEXT'); 
COMMIT;
SELECT MY_COL FROM TEST_DYNAMIC_TBL;
MY_COL
SOME TEXT

PL/SQL Anonymous Block (Successful Example)

DECLARE
    l_script VARCHAR2 (32767);
BEGIN
    l_script := 'UPDATE TEST_DYNAMIC_TBL SET MY_COL = DBMS_RANDOM.STRING(''U'',5)';
    DBMS_OUTPUT.put_line ('Script sent to Exec Immediate: ' || l_script);
    EXECUTE IMMEDIATE l_script;
    COMMIT;

EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.put_line (' ERROR: ' || SUBSTR (SQLERRM, 1, 64));
        ROLLBACK;
END;
/

--Check value (This results in a successful update)
SELECT MY_COL FROM TEST_DYNAMIC_TBL;

Script sent to Exec Immediate: UPDATE TEST_DYNAMIC_TBL SET MY_COL = DBMS_RANDOM.STRING('U',5)

PL/SQL procedure successfully completed.

MY_COL
XFTKV

Your query value will vary depending on the seed that DBMS_RANDOM picked

Stored Procedure Example (Failure Example)

--Procedure created with identical logic
CREATE OR REPLACE PROCEDURE TEST_DYNAMIC
AS
    l_script VARCHAR2 (32767);
BEGIN
    l_script := 'UPDATE TEST_DYNAMIC_TBL SET MY_COL = DBMS_RANDOM.STRING(''U'',5)';    
    DBMS_OUTPUT.put_line ('Script sent to Exec Immediate: ' || l_script);    -- This string will execute successfully if run directly
    EXECUTE IMMEDIATE l_script;
    COMMIT;

EXCEPTION
    WHEN OTHERS
    THEN
        DBMS_OUTPUT.put_line (' ERROR: ' || SUBSTR (SQLERRM, 1, 64));
        ROLLBACK;
END;
/

--Reset  and verify Data
UPDATE TEST_DYNAMIC_TBL SET MY_COL = 'SOME TEXT'; 
COMMIT;
SELECT MY_COL FROM TEST_DYNAMIC_TBL;

--Execute through Procedure (Will throw error)
EXECUTE TEST_DYNAMIC;

--Check Value of Table
SELECT MY_COL FROM TEST_DYNAMIC_TBL;

Stored Procedure Results:

MY_COL
SOME TEXT

Script sent to Exec Immediate: UPDATE TEST_DYNAMIC_TBL SET MY_COL = DBMS_RANDOM.STRING('U',5)

ERROR: ORA-00904: DBMS_RANDOM: invalid identifier

PL/SQL procedure successfully completed.

MY_COL
SOME TEXT

Solution

  • It isn't about it being dynamic, it's about the privileges and how they were granted. You would see the same thing if you had a static insert using dbms_random (and in your example anyway there is no need for it to be dynamic).

    It appears that you have execute privilege on dbms_random granted through a role, not directly to the package owner. From the documentation (emphasis added):

    If the procedure owner grants to another user the right to use the procedure, then the privileges of the procedure owner (on the objects the procedure references) apply to the grantee's exercise of the procedure. The privileges of the procedure's definer must be granted directly to the procedure owner, not granted through roles. These are called definer's rights.

    The user of a procedure who is not its owner is called the invoker. Additional privileges on referenced objects are required for an invoker's rights procedure, but not for a definer's rights procedure.

    That only applies to stored PL/SQL - i.e. procedures, functions, packages, triggers etc. - not to anonymous blocks.

    You can either get the privilege on dbms_random granted directly to the package owner, or change your procedure to use invoker's rights:

    CREATE OR REPLACE PROCEDURE TEST_DYNAMIC
    AUTHID CURRENT_USER
    AS
    ...
    

    In the latter case, anyone calling your procedure will then need the privilege on dbms_random - but they can have it through a role.

    As access to that package is sometimes locked down, a direct grant to the owner might be preferable, but it depends on your security constraints.


    The reason it requires a direct grant, I believe, is that roles can be enabled and disabled, and be default or not, and can be nested. If a direct grant is revoked then it's fairly straightforward to figure out that should invalidate the procedure. And that's possibly true if a role is revoked, but quite a lot more complicated.

    But what role-derived privileges should be taken into consideration when the procedure is created - only those that are enabled in that session? Only default roles? Or all roles? (And remember there can be a chain of roles to think about to determine privileges, and you can get the same privilege from multiple roles.)

    However you do it will confuse or upset someone - if only enabled then the owner logging in a future session might not be able to perform the actions the procedure does, and what if they want to recompile it? If only default then those defaults can change, with the same issues - or should that invalidate the procedure? If all roles then including disabled ones will be confusing and could have security implications.

    And for any of those, role revocation would still have to figure out which privileges that removes - which aren't also granted directly or via another role! - and only once it's really sure which privileges have actually gone, see which objects that affects. Which could be a lot of work - think how many individual privileges could be affected by revoking DBA.

    It's much simpler for the invoker - you only need to look at the active privileges from the enabled roles at the moment then call the procedure.

    So while at first glance it seems odd that privileges granted through roles aren't included for stored PL/SQL, once you look at the implications and complications - both as it's created, but more what happens afterwards - it seems like a sensible restriction.