Giter Site home page Giter Site logo

Comments (8)

ndmitchell avatar ndmitchell commented on September 25, 2024

Concretely:

diff --git a/src/win/hooks.c b/src/win/hooks.c
index eb1e66a..529612b 100644
--- a/src/win/hooks.c
+++ b/src/win/hooks.c
@@ -3,6 +3,8 @@
 #endif
 #include <winternl.h>
 #include <limits.h>
+#include <processthreadsapi.h>
+
 
 #undef ASSERT
 #include "dbg.h"
@@ -213,7 +215,7 @@ static NTSTATUS NTAPI hNtQueryFullAttributesFile(POBJECT_ATTRIBUTES oa, PFILE_NE
 static NTSTATUS NTAPI hNtResumeThread(HANDLE th, PULONG sc) {
 	NTSTATUS r;
 	D;
-	if (!patchInstalled())
+	if (!patchInstalled(GetProcessIdOfThread(th)))
 		injectThread(th);
 	r = oNtResumeThread(th, sc);
 	return r;
diff --git a/src/win/patch.c b/src/win/patch.c
index 6fdc26d..7b63f0a 100644
--- a/src/win/patch.c
+++ b/src/win/patch.c
@@ -95,12 +95,12 @@ void patchInstall(void *orig, void *hook, void **preal, const char *nm) {
 	dbg("modules patched\n");
 }
 
-int patchInstalled() {
+int patchInstalled(DWORD old) {
 	int ret;
 	ASSERT(s_hooked);
-	ret = (int)(intptr_t)TlsGetValue(s_hooked);
-	CHK(TlsSetValue(s_hooked, (void *)1));
-	return ret;
+	ret = (DWORD)(intptr_t)TlsGetValue(s_hooked);
+	CHK(TlsSetValue(s_hooked, (void *)old));
+	return ret == old;
 }
 
 void patchInit() {
diff --git a/src/win/patch.h b/src/win/patch.h
index a9628ec..64ba3d0 100644
--- a/src/win/patch.h
+++ b/src/win/patch.h
@@ -1,5 +1,4 @@
 void patchInit();
 void patchTerm();
 void patchInstall(void *orig, void *hook, void **preal, const char *nm);
-int patchInstalled();
-
+int patchInstalled(DWORD old);

from fsatrace.

jacereda avatar jacereda commented on September 25, 2024

Looks certainly better than what we have now, but I guess it isn't bulletproof either. A different thread could call ResumeThread() and we could end up with 2 patches installed.
I guess a 4 KB bit mask indexed by the lower bits of (pid/4) could be quite robust.

from fsatrace.

jacereda avatar jacereda commented on September 25, 2024

Here's what I thought could be a solution, but it didn't pass the tests.

7abb16b

Unfortunately I can't check with a windows box yet.

from fsatrace.

ndmitchell avatar ndmitchell commented on September 25, 2024

I'll try tomorrow on a real windows box. Why do the pid byte masking and not just have an actual array with the values seen?

from fsatrace.

jacereda avatar jacereda commented on September 25, 2024

OK, I think I found the bug. Please, check if the fix-23 branch works for you.

from fsatrace.

jacereda avatar jacereda commented on September 25, 2024

Yeah, it's kind of silly to worry about that table size considering the amount of memory I reserve for the logs :)

from fsatrace.

jacereda avatar jacereda commented on September 25, 2024

Oh, you mean doing a lookup in the array? Yes, that would probably be a better option. Feel free to change it.

from fsatrace.

ndmitchell avatar ndmitchell commented on September 25, 2024

It worked for me, thanks a lot. Given it's currently working I'd probably just leave it at that.

from fsatrace.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.