开发者

Stored Procedures access when no access in a table

开发者 https://www.devze.com 2023-02-18 12:58 出处:网络
All, In SQL Server, can a stored procedure (beeing开发者_如何学Go ran from a user) write to table where the user doesn\'t have access to write directly to the table?

All,

In SQL Server, can a stored procedure (beeing开发者_如何学Go ran from a user) write to table where the user doesn't have access to write directly to the table?

Rgds,

MK


The correct answer is NO, a stored procedure does not have access to write into a table. However most users perceive it, incorrectly, as YES because of Ownership Chains:

When multiple database objects access each other sequentially, the sequence is known as a chain. Although such chains do not independently exist, when SQL Server traverses the links in a chain, SQL Server evaluates permissions on the constituent objects differently than it would if it were accessing the objects separately. These differences have important implications for managing security. Ownership chaining enables managing access to multiple objects, such as multiple tables, by setting permissions on one object, such as a view.

So a procedure will be able to write into a table that the user has no permissions to write into if they form an ownership chain. This means that if the owner of the schema that contains the table is the same as the owner of the schema that contains the procedure, an ownership chain is formed and the procedure is allowed to write into the table. Since the vast majority of objects deployed in practice belong to the dbo schema, an ownership chain is almost always formed.

It is important to comprehend these details, so you can troubleshoot problems and understand why is the procedure allowed to write into the table. Erland Sommarskog has an excellent comprehensive write up on this topic: Giving Permissions through Stored Procedures. This article goes into great detail explaining all the options available. Your best option, far better than ownership chaining, is code signing.

Understanding how this works also helps understanding why dynamic SQL seems to 'break': running dynamic SQL is an automatic break in the ownership chain, which causes all 'magic' to disappear. And it also helps understand why this 'magic' appears not to work outside the database: cross db ownership chaining Option default value is 0.


In short, yes.

The main restriction in play in this scenario is whether the user can execute the stored procedure.


When creating the stored procedure, it needs to be done with a login/user that has the necessary write access to the table in question.


Yes, if the user is GRANTed EXEC permissions on the SP, any actions it takes (within that database) are allowed.

Going over to another database will require permissions on the underlying user to be examined.

Also, dynamic SQL built within the SP will require the underlying user to have permissions.

0

精彩评论

暂无评论...
验证码 换一张
取 消