SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ownership Chains, Dynamic Queries, and SQL Injection


Ownership Chains, Dynamic Queries, and SQL Injection

Author
Message
Deque
Deque
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 1744
I was hoping to confirm that while SQL injection is possible with dynamic queries that don't use parameterized queries, ownership chains are broken when using the dynamic query, so the only risk would be if that account had more than SELECT permissions on the underlying tables.

For instance, if I had a user called "TestUser" that had db_datareader access to a database (e.g., DatabaseA") and execute permissions on a stored procedure (e.g., spA), it would not be possible for "TestUser" to pass a parameter to spA that could modify any tables in DatabaseA if spA was using only dynamic queries?

Here is an example:

CREATE DATABASE DatabaseA;
GO

CREATE TABLE TableA ( ColumnA VARCHAR(255) )
GO


INSERT INTO TableA VALUES ('ABC');
INSERT INTO TableA VALUES ('DEF');
INSERT INTO TableA VALUES ('GHI');
INSERT INTO TableA VALUES ('JKL');

GRANT SELECT ON OBJECT::dbo.TableA TO [Test\TestUser];
GO

CREATE PROCEDURE spA
@paramA VARCHAR(255)
AS
BEGIN
EXEC('SELECT * FROM tableA WHERE ColumnA = ''' + @paramA + '''');
END
GO

GRANT EXECUTE ON dbo.spA TO [Test\TestUser];



Then to test whether or not we can do bad things, I ran this query as Test\TestUser:

EXEC dbo.spA 'ABC'';DROP TABLE TableA;SELECT ''';



I just want to confirm whether or not there is no risk, other than SELECTing data that we've already granted access to. The stored procedure comes from a vendor and it does not use parameterized queries because they have a dynamic list of columns. They do however use only dynamic queries, and their account only has read-only access to the underlying tables and only execute permissions on the stored procedure.

I know that my specific example doesn't work because I do get an error about insufficient permissions, but I just wanted to confirm my understanding that there isn't a risk that I'm unaware of because this is all new to me.
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14873 Visits: 14396
Dynamic SQL does break the ownership chain established when exectuing the proc. A new ownership chain can be established later in the call stack but at the end of the day the login/user executing the proc only has the rights they have in the instance, i.e. Dynamic SQL will not elevate their rights in any way.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Nils Gustav Stråbø
Nils Gustav Stråbø
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2433 Visits: 3575
Just a little side comment. If you want to avoid having to grant permissions to the explicit tables, views etc used in the dynamic query, you can sign the procedure with a certificate that is linked to a user with permissions on the explicit objects in the dynamic SQL.

http://msdn.microsoft.com/en-us/library/ms181700.aspx

The downside is that the signature is removed if you ALTER the procedure, and you have to sign it again.
But if you are using any kind of automated deployment, then you can add the signing steps in there.

We have used it successfully on many procedures with dynamic sql.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search