<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Think In Geek &#187; sql server</title>
	<atom:link href="http://thinkingeek.com/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Wed, 01 Feb 2012 14:45:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Controlling the commands executed with xp_cmdshell on SQL Server 2005</title>
		<link>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/</link>
		<comments>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 17:04:44 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=81</guid>
		<description><![CDATA[SQL Server has a special extended stored procedure called xp_cmdshell. This procedure has a lot of power: it allows to execute any command line code on the machine hosting the SQL Server. Imagine you want to list all the files on C: on the SQL Server Windows host: you could write a T-SQL statement like [...]]]></description>
			<content:encoded><![CDATA[<p>
SQL Server has a special extended stored procedure called <em>xp_cmdshell</em>. This procedure has a lot of power: it allows to execute any command line code on the machine hosting the SQL Server.
</p>
<p>
Imagine you want to list all the files on <em>C:</em> on the SQL Server Windows host: you could write a T-SQL statement like this one:
</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">EXECUTE</span> master<span style="color: #66cc66;">..</span>xp_cmdshell <span style="color: #ff0000;">'dir c:'</span></pre></div></div>

<p>
This stored procedure, however, is a very dangerous one, as it would allow to execute harmful code. This is the reason why it&#8217;s disabled by default. Even when enabled, only users on the <em>sysadmin</em> role can use it.
</p>
<p>
If you ever need some users the ability to run only some specific commands with xp_cmdshell, you can use the method I&#8217;ll explain below, making use of the <em>EXECUTE AS</em> modifier of the stored procedure definitions in T-SQL.
</p>
<p><span id="more-81"></span></p>
<p>
The proposed solution involves five steps:</p>
<ul>
<li>Enabling the <em>xp_cmdshell</em> extended procedure.</li>
<li>Adding a procedure on the database with the <em>EXECUTE AS</em> modifier as an administrator, controlling which commands are allowed to be executed.</li>
<li>Modifying or creating the <em>xp_cmdshell_proxy_account</em>, associating it to a user with <em>sysadmin</em> privileges.</li>
<li>Giving the user(s) you want the <em>EXECUTE</em> privileges to the procedure.</li>
<li>Grant the proxy account user the privilege to log on as a batch in the Windows server.</li>
</ul>
<p>
The execution of <em>xp_cmdshell</em> must be enabled on the SQL Server. This can be done through the SQL Surface Area Configuration utility or by code. Refer to Figure below on how to activate xp_cmdshell through the SQL Surface Area Configuration.
</p>
<div id="attachment_82" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/sql1.gif"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/sql1-300x226.gif" alt="SQL Surface Area" title="sqlsurface" width="300" height="226" class="size-medium wp-image-82" /></a><p class="wp-caption-text">SQL Surface Area</p></div>
<p>
To enable <em>xp_cmdshell</em> using SQL code, use the sentences below:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">EXEC</span> master<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sp_configure <span style="color: #ff0000;">'show advanced options'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>
RECONFIGURE
<span style="color: #993333; font-weight: bold;">EXEC</span> master<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sp_configure <span style="color: #ff0000;">'xp_cmdshell'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>
RECONFIGURE</pre></div></div>

</p>
<p>
<b>This will allow users of the <em>sysadmin</em> role, and no one else, to execute <em>xp_cmdshell</em>.</b>
</p>
<p>
Now we have to create a special stored procedure that will control the actions used as parameters to <em>xp_cmdshell</em>. This will allow the administrators of the database to have control over which commands they allow to be executed on their servers. The most important part of this procedure is the <em>EXECUTE AS OWNER</em> modifier. By using this modifier, everyone that runs that procedure will be able to run it as if it was the owner of the database, thus having execute permissions to <em>xp_cmdshell</em> (we&#8217;re assuming the procedure will be created in the <em>master</em> schema. By granting execute permissions on that procedure, you will allow specific users an indirect way to call the <em>xp_cmdshell</em>.
</p>
<p>
Using this method, only the users of the <em>sysadmin</em> role will be able to execute <em>xp_cmdshell</em>, and only the users you grant <em>EXECUTE</em> permissions on the stored procedure will be able to execute the specific commands that you allow.
</p>
<p>
To insert the store procedure, log in as a <em>sysadmin</em> on the database and create it with the <a href="http://msdn.microsoft.com/en-us/library/ms188354.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/ms188354.aspx?referer=');"><em>EXECUTE AS OWNER</em></a> modifier on it.
</p>
<p>
For the above procedure to work on non <em>sysadmin</em> accounts there is another step that has to be done. By default, even if you have permissions on the store procedure, you won’t be able to execute it if you’re not on the <em>sysadmin</em> role. This is because those users need a proxy account that is used as the account in which the <em>xp_cmdshell</em> is executed.
</p>
<p>
So, for this procedure to work, you must create or modify the <em>xp_cmdshell_proxy_account</em> with a user within the <em>sysadmin</em> role. To setup this account, proceed with the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">EXEC</span> sp_xp_cmdshell_proxy_account <span style="color: #ff0000;">'MyDomain<span style="color: #000099; font-weight: bold;">\M</span>yUserName'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'myDomainPassword'</span></pre></div></div>

</p>
<p>
If the above code does not work, try this one:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> credential ##xp_cmdshell_proxy_account## <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Domain<span style="color: #000099; font-weight: bold;">\D</span>omainUser'</span><span style="color: #66cc66;">,</span> secret <span style="color: #66cc66;">=</span>  password<span style="color: #ff0000;">'</span></pre></div></div>

</p>
<p>
After the procedure and the proxy account have been set, the users we want to be able to execute the procedure must be granted <em>EXECUTE</em> permission on it. To do so, execute this statement for every user you want to grant permissions:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">EXECUTE</span> <span style="color: #993333; font-weight: bold;">ON</span> Tango_xp_cmdshell <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #66cc66;">&lt;</span>username<span style="color: #66cc66;">&gt;</span>;
<span style="color: #993333; font-weight: bold;">GO</span></pre></div></div>

</p>
<p>
To grant this permission, use the <em>Local Security Settings</em> on the <em>Administrative Tools</em> interface of the <em>Windows Control Panel</em>. Once there locate the property shown on the screenshot and add the user you gave permissions to the user list.
</p>
<div id="attachment_83" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/sql2.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/sql2-300x211.png" alt="Local Security Policy" title="LocalSecurityPolicies" width="300" height="211" class="size-medium wp-image-83" /></a><p class="wp-caption-text">Local Security Policy</p></div>
<p>
<b>Note that enabling the <em>xp_cmdshell</em> command may still have some security implications, so try to avoid it when possible.</b></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2008%2F11%2F13%2Fcontrolling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005%2F&amp;title=Controlling%20the%20commands%20executed%20with%20xp_cmdshell%20on%20SQL%20Server%202005" id="wpa2a_2" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2008_2F11_2F13_2Fcontrolling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005_2F_amp_title=Controlling_20the_20commands_20executed_20with_20xp_cmdshell_20on_20SQL_20Server_202005?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Repeatable read and deadlocks in SQL Server</title>
		<link>http://thinkingeek.com/2007/09/23/repeatable-read-and-deadlocks-in-sql-server/</link>
		<comments>http://thinkingeek.com/2007/09/23/repeatable-read-and-deadlocks-in-sql-server/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 17:32:01 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=78</guid>
		<description><![CDATA[This week we had a bug report of one of our products regarding some strange deadlocks in our database access. For those of you who don&#8217;t know what a deadlock is, I&#8217;ll try to summarize here what a transaction is in a relational database environment and why those transactions might lead to those nasty errors, [...]]]></description>
			<content:encoded><![CDATA[<p>This week we had a bug report of one of our products regarding some strange deadlocks in our database access. For those of you who don&#8217;t know what a deadlock is, I&#8217;ll try to summarize here what a transaction is in a relational database environment and why those transactions might lead to those nasty errors, and try to explain what was causing this deadlock in our SQL Server 2005 engine.</p>
<p><span id="more-78"></span></p>
<p>
One of the most interesting features of any good database engine is what we call concurrency. Concurrency means than the database engine should be able to perform a lot of data operations at the same time. This leads to some interesting problems that have a critical impact on another of the features that any database engine must ensure: data consistency.
</p>
<p>
Sometimes you need to perform more than one non atomic operation in a database that only make sense in a block. This is, all those operations should be executed from the start to the end without any other interference from other operations, because those other operations may have a non desired impact in your operations. The simulation of the atomic execution of more than one operation in databases is done by transactions. When you embrace a set of operations in a transaction, you are telling the database engine that you want those operations to be executed as if they were executed in an isolated mode, this is, with no interference from other operations.
</p>
<p>
While this might seem easy to accomplish, it can also have a big negative performance impact, because you can&#8217;t plan on executing all the transactions that arrive at the same time in a sequential way, at least not if you want your database engine to be slow, very slow.
</p>
<p>
This is where locks ans isolation levels intervene. Locks are protections made to some resources to allow or forbid further access to those resources by another processes. An exclusive lock, for example, done by a process, means that no other process should be able to access that resource until the first process has freed the lock. There are a lot of different locks in SQL Server, which you can review at <a href="http://msdn2.microsoft.com/en-us/library/ms175519.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/ms175519.aspx?referer=');">http://msdn2.microsoft.com/en-us/library/ms175519.aspx</a>.
</p>
<p>
One of the problems we can face when working with transactions is the well known repeatable read. Suppose a process A starts a transaction and reads a registry with a select clause in a table. After that, another transaction comes in and modifies that data. The first transaction reads the same data again and gets a different value, becuase it was modified by the second transaction. This is inconsistent, because as we are executing a set of operations inside a transaction, we expect them to be executed as if no one else was modifying the data we need to use.
</p>
<p>
Because of that, there&#8217;s a special isolation level called, precisely, Repeatable Read, which has a locking policy to avoid those problems. But be careful with this, since this can cause a nasty deadlock in your SQL Server if used the wrong way.
</p>
<p>
A deadlock is a special situation where two or more transactions are waiting each other. Imagine trasaction A starts some operations and locks some resources. Later, transaction B comes in, locks another set of resources, and then tries to access resources locked by A, so it waits for A to free those resources. After that, A tries to lock resources locked by B and wait for B to free them. A is waiting for B to free its resources, and B is waiting for A to do the very same thing. We have a deadlock. One of the transactions must be killed by the database engine and rolled back so the other one can continue. Altough this is something you might be able to control by code and issue a relaunch of the killed transaction, this is something usually not desired.
</p>
<p>
I&#8217;m going to talk now about a deadlock you may face when using the Repeatable Read transaction isolation level with SQL Server 2005. Imagine you have two transactions that do the same thing: read a value, modify it, and read it again. Note that you will be modifying the value, but you set the isolation mode to Repeatable Read (this is, setting the wrong isolation mode by mistake or by ignorance). Transaction A starts and reads the value. SQL Server puts a shared lock on that resource. Transaction B starts and reads the value. Because the lock in A was shared, B can also read that value, gaining a shared lock too. Now transaction B tries to write that value. Because the shared lock put by A was read only, B has to wait for A to release its shared lock to be able to gain write acces to it, so it blocks. After that, A tries to write the value, and because now it&#8217;s transaction B that has a shared lock on the resource, it has to wait too. And there it is, the deadlock.
</p>
<p>
The key in avoiding this is that, although we perform a read-write-read operation, we don&#8217;t need the second read to be the same as the first, because we are modifying that resource. In this case, the lock our transactions should get is an update lock, which knows that we&#8217;ll be modifying that data after having read it. This way, when transaction A locks the resource, the lock will not be shared, so transaction B will have to wait for A to write the new value before B gets the lock, thus avoiding the deadlock. Another option is use an isolation level that frees the locks just after having read the value, and not maintaining it until the next update.
</p>
<p>
The conclusion to this is: be careful when working with transactions on which isolation level you use on them, and be sure you&#8217;re using the right one if you don&#8217;t want to have bug reports when a lof of concurrency starts to stress the database engine!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fthinkingeek.com%2F2007%2F09%2F23%2Frepeatable-read-and-deadlocks-in-sql-server%2F&amp;title=Repeatable%20read%20and%20deadlocks%20in%20SQL%20Server" id="wpa2a_4" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save_url=http_3A_2F_2Fthinkingeek.com_2F2007_2F09_2F23_2Frepeatable-read-and-deadlocks-in-sql-server_2F_amp_title=Repeatable_20read_20and_20deadlocks_20in_20SQL_20Server?referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2007/09/23/repeatable-read-and-deadlocks-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

