{"id":30053,"date":"2006-08-18T10:00:14","date_gmt":"2006-08-18T10:00:14","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/08\/18\/applications-and-dlls-dont-have-privileges-users-do\/"},"modified":"2006-08-18T10:00:14","modified_gmt":"2006-08-18T10:00:14","slug":"applications-and-dlls-dont-have-privileges-users-do","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060818-14\/?p=30053","title":{"rendered":"Applications and DLLs don&#039;t have privileges; users do"},"content":{"rendered":"<p>\nI can&#8217;t believe you people are actually asking for backdoors.\nIf an end user can do it, then so can a bad guy.\n<\/p>\n<p>In response to the requirement that all drivers on 64-bit Windows be signed,\none commenter suggested\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/14\/551141.aspx#551242\">\nadding a backdoor that permits unsigned drivers<\/a>,\nusing some &#8220;obscure registry key&#8221;.\nBefore somebody can jump up and shouts &#8220;security through obscurity!&#8221;,\nthe commenter adds this parenthetical:\n&#8220;(that no application has privileges to do by default)&#8221;.\n<\/p>\n<p>\nWhat does that parenthetical mean?\nHow do you protect a registry key from an application?\nAnd if applications don&#8217;t have privileges to modify a key,\nthen who does?\n<\/p>\n<p>\nThe Windows security model is based on identity.\nApplications don&#8217;t have privileges.\nUsers have privileges.\nIf an application is running in your user context, then it can do\nanything you can,\nand that includes setting that &#8220;obscure registry key&#8221;.\n(This is a variation on\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/12\/12\/502719.aspx\">Your debugging code can be a security hole<\/a>&#8220;.)\nSame goes for DLLs.\nThere&#8217;s no such thing as something\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/06\/07\/426294.aspx#426645\">\nonly an individual program\/library can read\/write to<\/a>\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/06\/19\/636823.aspx#637022\">\nor do<\/a>.\nYou can&#8217;t check the &#8220;identity of the calling library&#8221; because\nyou can&#8217;t trust the return address.\nComing up with some other &#8220;magic encryption key&#8221; like the full path\nto the DLL won&#8217;t help either, because a key that anybody can guess\nwith 100% accuracy isn&#8217;t much of a key.\n<\/p>\n<p>\nYes,\nUNIX has setuid,\nbut that still doesn&#8217;t make applications security principals.\nEven in UNIX, permissions are assigned to users, not to applications.\n<\/p>\n<p>\nThat&#8217;s one of the reasons I get so puzzled when I hear people\nsay,\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/21\/556505.aspx#558271\">Windows should let me do whatever I want with my system<\/a>&#8220;,\nwhile simultaneously saying,\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/21\/556505.aspx#557618\">Windows should have used ACLs to prevent applications from\ndoing whatever they want with my system<\/a>.&#8221;\nBut when you are running an application,\n<strong>the application is you<\/strong>.\nIf you can do it, then an application can do it\nbecause the application is you.\n<\/p>\n<p>\nSome people want to extend the concept of security principal\nto a chunk of code.\n&#8220;This registry key can be written to only by this function.&#8221;\nBut how could you enforce this?\nOnce you let untrusted code enter a process,\nyou can&#8217;t trust any return addresses any more.\nHow else could you identify the caller, then?\n<\/p>\n<p>\n&#8220;Well, the DLL when it is created is given a magic cookie\nthat it can use to prove its identity by passing that cookie\nto these &#8216;super-secure functions&#8217;.\nFor example,\n<\/p>\n<pre>\n\/\/ SECRET.DLL - a DLL that protects a secret registry key\nHANDLE g_hMagicCookie;\n\/\/ this function is called by means to be determined;\n\/\/ it tells us the magic cookie to use to prove our identity.\nvoid SetMagicCookie(HANDLE hMagicCookie)\n{\n g_hMagicCookie = hMagicCookie;\n}\n<\/pre>\n<p>\nand then the program can use the magic cookie to prove\nthat it is the caller.\nFor example, you could have\n<code>RegSetValueWithCookie(g_hMagicCookie, hkey, ...)<\/code>,\nwhere passing the cookie means &#8216;It&#8217;s me calling, please give\nme access to that thing that only I have access to.&#8221;\n<\/p>\n<p>\nThat won&#8217;t stop the bad guys for long.\nThey just have to figure out where the DLL saves that cookie\nand read it, and bingo, they&#8217;re now you.\n<\/p>\n<pre>\n\/\/ bad-guy program\nint CALLBACK WinMain(...)\n{\n \/\/ call some random function from SECRET.DLL\n \/\/ so it gets loaded and the magic cookie gets\n \/\/ initialized.\n SomeFunctionFromSECRETDLL();\n \/\/ experimentation tells us that SECRET.DLL\n \/\/ keeps its magic cookie at address 0x70131970\n HANDLE hMagicCookie = *(HANDLE*)0x70131970;\n RegSetValueWithCookie(hMagicCookie, hkey, ...);\n return 0;\n}\n<\/pre>\n<p>\nTa-da, we now have a program that writes to that\nregistry key that <code>SECRET.DLL<\/code> was trying to protect.\nIt does it by merely waiting for <code>SECRET.DLL<\/code> to receive\nits magic cookie, then stealing that cookie.\n<\/p>\n<p>\n&#8220;Well, sure, but if I combine that with the check-the-return-address\ntechnique, then that&#8217;ll stop them.&#8221;\n<\/p>\n<p>\nNo, that doesn&#8217;t stop anybody.  All the bad guy has to do is\nchange the <code>RegSetValueWithCookie(hMagicCookie, hkey, ...)<\/code>\nto code that hunts for a trusted address inside <code>SECRET.DLL<\/code>\nand cooks up a fake stack so that when control reaches\n<code>RegSetValueWithCookie<\/code>, everything in memory looks just\nlike a legitimate call to the function, except that the attacker\ngot to pass different parameters.\n<\/p>\n<p>\nYou can come up with whatever technique you want,\nit won&#8217;t do any good.\nOnce untrusted code has been granted access to a process,\nthe entire process is compromised and you cannot trust it.\nWorst case, the attacker just sets a breakpoint on\n<code>RegSetValueWithCookie<\/code>, waits for the breakpoint\nto hit, then edits the stack to modify the parameters and resumes\nexecution.\n<\/p>\n<p>\nThat&#8217;s why code is not a security principal.\n<\/p>\n<p>\nCorollary: Any security policy that says &#8220;Applications cannot do X\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/06\/07\/426294.aspx#426432\">\nwithout permission from the user<\/a>&#8221; is flawed from conception.\nThe application running as the user <strong>is the user<\/strong>.\nIt&#8217;s one thing to have this rule as a recommendation,\neven a logo requirement,\nbut it&#8217;s another thing to enforce this rule in the security subsystem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I can&#8217;t believe you people are actually asking for backdoors. If an end user can do it, then so can a bad guy. In response to the requirement that all drivers on 64-bit Windows be signed, one commenter suggested adding a backdoor that permits unsigned drivers, using some &#8220;obscure registry key&#8221;. Before somebody can jump [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-30053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>I can&#8217;t believe you people are actually asking for backdoors. If an end user can do it, then so can a bad guy. In response to the requirement that all drivers on 64-bit Windows be signed, one commenter suggested adding a backdoor that permits unsigned drivers, using some &#8220;obscure registry key&#8221;. Before somebody can jump [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/30053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=30053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/30053\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=30053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=30053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=30053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}