Solved

An API detour question

Posted on 2016-09-13
7
79 Views
Last Modified: 2016-09-17
When I am load a DLL library in my process, is the shared DLL code shared same memory with other process that us the same DLL?

Here is the use case,
When I detour and API, say "CreateProcess" from "kernel32.dll", will it intercept the function calls only from this process or it can come from all process?

Thank you for any answer.
0
Comment
Question by:Evan Li
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 41797361
shared DLL code shared same memory with other process that us the same DLL?
that is true for static data. it is false for local variables used or for dynamic memory allocated.

if using global or static data in the dll you should always make sure that it is constant data valid for all processes using the dll.

"CreateProcess" from "kernel32.dll", will it intercept the function calls only from this process or it can come from all process?
don't know exactly what you mean by that. kernel32.dll never would be unloaded and each call of one of the exported dll functions is independent to other calls running parallel or later regardless of whether the calls were coming from same process and same thread, or from other thread or from other process. of course you need to care for thread-safety if using global or shared data for input arguments but the code of the dll function is always safe since each call runs a copy of the original code.

Sara
0
 

Author Comment

by:Evan Li
ID: 41797377
Thank you Sara,
I am not asking questions of data. I am asking questions of the code, as when we do the detour of an API we are changing the code of that API for the first 5-6 bytes, here is the reference about what happens:
https://www.microsoft.com/en-us/research/project/detours/

My question is that when we do the detour, are we going to detour only my the calls from my process or all calls from the system for this API.

Looks that it is only affect one my process, and I do not understand that.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41797422
detours creates new binary code for each dll function detoured and creates a safe copy of the code for each call. the new code is never a subject of to being shared with other threads let alone with other processes.

you may not worry about code but only about data.

Sara
0
Swamped with email signature updates?

Have you been given a load of changes to make to your users’ email signatures? Having to manually implement multiple signatures for every department? Let Exclaimer save you from being swamped with email signature updates!

 

Author Comment

by:Evan Li
ID: 41798717
Hi Sara,

Thank for your answer.

I am little more confused.

Here is more question now:

When we loadlibrary, will the OS duplicate the code that other people has loaded for other process in my process space? If yes, it will be too many GDI.dll, kernel.dll etc.

If not, when we do detour, we'll change other process space, is it possible?

Or are you saying that when we call getprocaddress, the function code gets duplicated in my process space.

Anyway, I need to know what is happening underline. I could not find the answer from Microsoft paper. Thanks for further help.

Evan
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 41799237
If yes, it will be too many GDI.dll, kernel.dll etc.
LoadLibrary doesn't copy code. it was copied after GetProcAddress returned a valid function pointer and if your code contains a call by using this pointer. detour would intercept directly at this point.

note, copying of code is essential since the process address spaces are strictly separated. even if your process calls into a COM function or into .NET the executed code always would be mapped into your process space.

Sara
0
 

Author Comment

by:Evan Li
ID: 41800159
You are right. if we get an address that is in my process space, it must be copied. But, I could not find any document that talk about when I get the address successfully, the function code is copied to process space?
0
 

Author Closing Comment

by:Evan Li
ID: 41803486
Thank you for your help.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now