Home All Groups Group Topic Archive Search About

"With Execute As" Question

Author
25 Aug 2006 5:03 PM
Amos Soma
Assume I have the stored proc shown below. I have a 'With Execute As'
included. Doesn't this mean that whoever attempts to execute this stored
proc, even if it is executed from a remote server, will get authenticated as
if he were the user were the "sa" user? With all the permissions "sa" has?

ALTER PROCEDURE [dbo].[FederationData_Add] (@ItemType int, @ItemValue
nvarchar(max))
WITH EXECUTE as 'sa'
AS

Insert Into FederationData (ItemType, ItemValue)
    Values (@ItemType, @ItemValue)

Author
25 Aug 2006 5:37 PM
Dan Sullivan
No it doesn't. The principal that is using EXECUTE AS must have IMPERSONATE permission on the principal it is trying to execute as. For example for user Joe to do EXECUTE AS USER = 'Jane', an admin would have had to have done something like:

GRANT IMPERSONATE ON USER:Jane to Joe;

Typical user woundn't have that permission.
Dan
Author
26 Aug 2006 3:03 AM
Dan Guzman
A stored procedure WITH EXECUTE AS clause can only specify a database
principal.  'sa' is server level principal so it is not allowed in this
context.

In the case of a database user (e.g. WITH EXECUTE AS 'dbo'), the specified
security context will be used for the duration of the proc.  Execute
permissions on the proc are all that are required when the proc is executed
directly.

--
Hope this helps.

Dan Guzman
SQL Server MVP

Show quote
"Amos Soma" <amos_j_s***@yahoo.com> wrote in message
news:u9880hGyGHA.2036@TK2MSFTNGP05.phx.gbl...
> Assume I have the stored proc shown below. I have a 'With Execute As'
> included. Doesn't this mean that whoever attempts to execute this stored
> proc, even if it is executed from a remote server, will get authenticated
> as if he were the user were the "sa" user? With all the permissions "sa"
> has?
>
> ALTER PROCEDURE [dbo].[FederationData_Add] (@ItemType int, @ItemValue
> nvarchar(max))
> WITH EXECUTE as 'sa'
> AS
>
> Insert Into FederationData (ItemType, ItemValue)
>    Values (@ItemType, @ItemValue)
>
>
Author
26 Aug 2006 9:50 PM
Erland Sommarskog
Amos Soma (amos_j_s***@yahoo.com) writes:
> Assume I have the stored proc shown below. I have a 'With Execute As'
> included. Doesn't this mean that whoever attempts to execute this stored
> proc, even if it is executed from a remote server, will get
> authenticated as if he were the user were the "sa" user? With all the
> permissions "sa" has?
>
> ALTER PROCEDURE [dbo].[FederationData_Add] (@ItemType int, @ItemValue
> nvarchar(max))
> WITH EXECUTE as 'sa'
> AS
>
> Insert Into FederationData (ItemType, ItemValue)
>     Values (@ItemType, @ItemValue)

To add to Dan Guzman's answer, assuming that the clause reads

   WITH EXECUTE AS 'dbo'

and dbo for a certain database is sa, you may ask will the caller then
has the rights of sa?

The answer is that this depends on the trustworthy setting of the database.
By default this setting is OFF, and in this case any procedure with an
EXECUTE AS clause is sandboxed into the database - and it cannot perform
any operation outside the database where the procedure exists.

However, if the database is set to trustworthy, then all these restrictions
are waived.

The impersonation lasts for the duration of the stored procedure, and
it applies to everything - permissions and what SYSTEM_USER returns. If
the procedure calls another procedure or invokes a trigger or dynamic
SQL, the impersonation lingers.

In short, WITH EXECUTE AS is very powerful, and it is outright dangerous
to put something like WITH EXECUTE AS 'dbo'. Better is to define a
login-less user that is assigned exactly the elevated permissions you
may be looking for in your stored procedure.

I have longer article on my web site that discusses EXECUTE AS together
with other mechanisms to transfer permissions in SQL 2005,
http://www.sommarskog.se/grantperm.html#EXECUTE_AS.



--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button