{"id":129,"date":"2007-05-11T16:27:09","date_gmt":"2007-05-11T21:27:09","guid":{"rendered":"http:\/\/www.nynaeve.net\/?p=129"},"modified":"2019-12-13T17:38:37","modified_gmt":"2019-12-13T22:38:37","slug":"beware-getthreadcontext-on-wow64","status":"publish","type":"post","link":"http:\/\/www.nynaeve.net\/?p=129","title":{"rendered":"Beware GetThreadContext on Wow64"},"content":{"rendered":"<p>If you&#8217;re planning on running a 32-bit program of yours under Wow64, one of the things that you may need to watch out for is a subtle change in how <a title=\"GetThreadContext\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms679362.aspx\">GetThreadContext<\/a> \/ <a title=\"SetThreadContext\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms680632.aspx\">SetThreadContext<\/a> operate for Wow64 processes.<\/p>\n<p>Specifically, these operations require additional access rights when operating on a Wow64 context (as is the case of a Wow64 process calling Get\/SetThreadContext).  This is hinted at in MSDN in the documentation for GetThreadContext, with the following note:<\/p>\n<blockquote><p>WOW64:  The handle must also have THREAD_QUERY_INFORMATION access.<\/p><\/blockquote>\n<p>However, the reality of the situation is not completely documented by MDSN.<\/p>\n<p>Under the hood, the Wow64 context (e.g. x86 context) for a thread on Windows x64 is actually stored at a relatively well-known location in the virtual address space of the thread&#8217;s process, in user-mode.  Specifically, one of the TLS slots in thread&#8217;s TLS array is repurposed to point to a block of memory that contains the Wow64 context for the thread (used to read or write the context when the Wow64 &#8220;portion&#8221; of the thread is not currently executing).  This is presumably done for performance reasons, as the Wow64 thunk layer needs to be able to quickly transition to\/from x86 mode and x64 mode.  By storing the x86 context in user mode, this transition can be managed without a kernel mode call.  Instead, a simple far call is used to make the transition from x86 to x64 (and an iretq is used to transition from x64 to x86).  For example, the following is what you might see when stepping into a Wow64 layer call with the 64-bit debugger while debugging a 32-bit process:<\/p>\n<pre>ntdll_777f0000!ZwWaitForMultipleObjects+0xe:\r\n00000000`7783aebe 64ff15c0000000  call    dword ptr fs:[0C0h]\r\n0:000:x86&gt; t\r\nwow64cpu!X86SwitchTo64BitMode:\r\n00000000`759c31b0 ea27369c753300  jmp     0033:759C3627\r\n0:012:x86&gt; t\r\nwow64cpu!CpupReturnFromSimulatedCode:\r\n00000000`759c3627 67448b0424      mov     r8d,dword ptr [esp]\r\n<\/pre>\n<p>A side effect of the Wow64 register context being stored in user mode, however, is that it is not so easily accessible to a remote process.  In order to access the Wow64 context, what needs to occur is that the TEB for a thread in question must be located, then read out of the thread&#8217;s process&#8217;s address space.  From there, the TLS array is processed to locate the pointer to the Wow64 context structure, which is then read (or written) from the thread&#8217;s process&#8217;s address space.<\/p>\n<p>If you&#8217;ve been following along so far, you might see the potential problem here.  From the perspective of GetThreadContext, there is no handle to the process associated with the thread in question.  In other words, we have a missing link here: In order to retrieve the Wow64 context of the thread whose handle we are given, we need to perform a VM read operation on its process.  However, to do that, we need a <em>process<\/em> handle, but we&#8217;ve only got a <em>thread<\/em> handle.<\/p>\n<p>The way that the Wow64 layer solves this problem is to query the process ID associated with the requested thread, open a new handle to the process with the required access rights, and then performs the necessary VM read \/ write operations.<\/p>\n<p>Now, normally, this works fine; if you can get a handle to the thread of a process, then you should almost always be able to get a handle to the process itself.<\/p>\n<p>However, the devil is in the details, here.  There <em>are<\/em> situations where you might not have access to open a handle to a process, even though you have a handle to the thread (or even an existing process handle, but you can&#8217;t open a new one).  These situations are relatively rare, but they do occur from time to time (in fact, we ran into one at work here recently).<\/p>\n<p>The most likely scenario for this issue is when you are dealing with (e.g. creating) a process that is operating under a different security context than your process.  For example, if you are creating a process operating as a different user, or are working with a process that has a higher integrity level than the current process, then you might not have access to open a new handle to the process (even if you might already have an existing handle returned by a <em>CreateProcess*<\/em> family routine.<\/p>\n<p>This turns into a rather frustrating problem, especially if you have a handle to both the process and a thread in the process that you&#8217;re modifying; in that case, you already have the handle that the Wow64 layer is trying to open, but you have no way to communicate it to Wow64 (and Wow64 will try and fail to open the handle on its own when you make the GetThreadContext \/ SetThreadContext call).<\/p>\n<p>There are two effective solutions to this problem, neither of which are particularly pretty.<\/p>\n<p>First, you could reverse engineer the Wow64 layer and figure out the exact specifics behind how it locates the PWOW64_CONTEXT, and implement that logic inline (using your already-existing process handle instead of creating a new one).  This has the downside that you&#8217;re way into undocumented implementation details land, so there isn&#8217;t a guarantee that your code will continue to operate on future Windows versions.<\/p>\n<p>The other option is to temporarily modify the security descriptor of the process to allow you to open a second handle to it for the duration of the GetThreadContext \/ SetThreadContext calls.  Although this works, it&#8217;s definitely a pain to have to go muddle around with security descriptors just to get the Wow64 layer to work properly.<\/p>\n<p>Note that if you&#8217;re a native x64 process on x64 Windows, and you&#8217;re setting the 64-bit context of a 64-bit process, this problem does not apply.  (Similarly, if you&#8217;re a 32-bit process on 32-bit Windows, things work &#8220;as expected&#8221; as well.)<\/p>\n<p>So, in case you&#8217;ve been getting mysterious STATUS_ACCESS_DENIED errors out of GetThreadContext \/ SetThreadContext in a Wow64 program, now you know why.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re planning on running a 32-bit program of yours under Wow64, one of the things that you may need to watch out for is a subtle change in how GetThreadContext \/ SetThreadContext operate for Wow64 processes. Specifically, these operations require additional access rights when operating on a Wow64 context (as is the case of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,4,5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/129"}],"collection":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":1,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":591,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/591"}],"wp:attachment":[{"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nynaeve.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}