Intercepting system API calls6959441Abstract A system for intercepting API calls in a virtual memory system comprises an activation module comprising an executable program and an interception module comprising a dynamic link library (DLL). The activation module is first executed at system initialization time, its prime purposes being to: parse user configuration information supplied in a configuration file; act as a daemon to launch and thus retain the interception module in shared memory; and hook system APIs to redirect calls via the interception module. Claims 1. A system for intercepting one or more application program interface (API) calls in a virtual memory environment comprising: an activation module and an interception module: Description BACKGROUND OF THE INVENTION with each field being comma delimited, as in the following example:
If a field is omitted, as in the case of Process Name above, then it is assumed to be a nonspecific match criterion. In the example configuration line above, it is specified that any process which runs a module called INOTES and that makes named shared allocations with a name beginning \SHAREMEM\NOTESMEM will have the OBJ—ANY flag forced on by the interception module to request preferential memory allocation from high memory. As mentioned above, in order to enter the kernel using a 32-bit call from an application, it is required that this call is indirected via DOSCALL1. DOSCALL1 provides the calling application with a regular near32 C-calling convention call. The kernel is entered using a far32 call using an Intel CallGate since a privilege level switch is required. Thus, in DOSCALL1 the instruction used to enter the kernel will be of the form:
Encoded in memory as hexadecimal digits, this will appear as:
For each API call that is to be intercepted (in the present embodiment only one type of call is intercepted), the second instance of the activation module redirects the API calls to the interception module by creating R/W aliases to the entry points in DOSCALL1 and changing the far32 pointers to point to the interception module entry points. An alias is merely a region of virtual memory that maps to the same physical memory as the original memory object (in this case the location of the DosAllocMem far32 pointer address) by referencing the same physical address in its page table entry. If the memory is in a global shared region of virtual memory, then any update made to a memory object using the alias address will at the same time be made for all processes at the original address location. Since the access rights are also determined from the page table entry it is possible for the alias to have different access rights to the original memory object. Under OS/2, aliasing is achieved by calling the DosAliasMem API. DosAliasMem provides a writable alias to the location in memory where the far call operands are stored in DOSCALL1. The interception module then writes the interception module's entry point expressed as a far32 address to this alias location and so replaces the operand values (x and y values) of the original far call instruction in physical memory with those of the interception module. It should be noted that because the activation module needs to change 6 bytes of memory, it should take into account two additional things: The first two instances of the activation module have been broadly described above, however, there may in fact be three instances of the activation module: Each instance determines which instance it is and thus how it should perform by testing for the existence of named semaphores created by earlier instances. Named semaphores are system-wide unique objects, which may be created once but accessed multiple times. The second instance creates a so called termination semaphore; and the first instance, creates a communications semaphore. On instantiation, the activation module tries to open the (second instance's) termination semaphore, if successful it knows it is a third instance invocation. If not, then it tries to open the (first instance's) communications semaphore. If successful, it knows it is the second instance invocation, so performs its initialization tasks and creates its termination semaphore. If opening the communications semaphore is unsuccessful then first instance invocation is assumed, whereupon the communications semaphore is created and second instance launched. Alternatively, the activation module could first try to open the termination semaphore and if that succeeds, it assumes it is a third instance, otherwise it creates the communications semaphore. If that succeeds, it assumes it is a first instance, otherwise it assumes it is a second instance. This means that the first instance creates the communications semaphore at the beginning of its processing, while the second instance can create the termination semaphore at the end of its processing after completing other necessary tasks. Making the tests in this way avoids pathological problems caused by timing circumstances. In any case, either scheme allows the first instance to terminate as well as allowing the second and subsequent instances to identify their instantiation hierarchy. To make communication between instances of the activation module easy and to avoid recursion complications with minimal performance overhead, the activation module specifies a shared R/W segment, which is used as a communications buffer. The R/W private segment is shared by virtue of the fact that it is mapped to common physical memory. Access to the buffer uses a compare-and-exchange technique on a flag field prefix to the buffer. The flag prefix acts as a command directive to the first instance of the activation module which performs one of the following operations based on the prefix: a) wait for a change in prefix; b) print a message and wait for a new prefix; c) print a message and terminate; d) print a message and terminate with error. Turning now to the operation of the interception module. This performs the following tasks:
It will be seen that in order to be able to call the original kernel entry points, the interception module must be able to remember the original API call address. When a DLL is loaded by a calling process such as the activation module, it comprises three portions in memory: a code segment common to all calling processes; an instance data segment; and a global data segment. Data stored in an instance's instance data segment is essentially inaccessible to other instances of the DLL. Plural instances of the DLL can on the other hand share information via the global data segment. However, the global data segment does not become accessible to the DLL unless it is formally loaded by a process. Processes making API calls which the interception module is adapted to intercept will not be aware of its presence and so do not formally load the interception module. So, in the preferred embodiment of the invention, the interception module formally loads itself to make information accessible across all instances of the interception module. As will be explained later, however, it is not necessarily desirable for the interception module to immediately formally load itself whenever it is called as a result of a third party process call to DOSCALL1 or in particular if it is called directly from DOSCALL1. The interception module may on the other hand wish to allow the API to proceed as normal to the kernel. Without access to some form of data available to all instances of the interception module, however, it would be extremely difficult to provide this information, in particular, to instances of the interception module called from third party processes. In order to overcome this problem, one or more predetermined interception module global variables are used to store the original kernel entry point(s). The code segment, however, is read-only, and so the interception module exports these variables to the activation module (this happens automatically when the interception module is implicitly loaded by the activation module when OS performs a process known a fixing-up where external references are resolved) and the second instance of the activation module creates an alias to the location of these interception module variables and populates this alias with the address of the kernel entry point(s) which are intercepted by the interception module. Thus, the second instance of the activation module is able to store the original kernel entry point addresses in a read/write alias available to all instances of the interception module code segment before modifying operands of the call instructions in DOSCALL1. Location of these global variables in the interception module code segment instead of the data segment permits the interception module to call the original kernel entry points before being formally loaded should any of tests made on entry to the interception module fail. This incidentally allows the third instance of the activation module to deactivate the API hooks by restoring the call instruction operands in DOSCALL1 from the exported global variables saved in the interception module's code segment alias. Nonetheless, in order to process the configuration data, accumulate statistics, make C run-time library and API calls and most importantly serialize access to the configuration data, the interception module uses the global data segment. A DLL's data segments are not made available to a process until the DLL is loaded by that process. Only the code is accessible, adventitiously, if it happens to be loaded into the GSR. Using initialization techniques described earlier for the activation module the interception module is guaranteed to be loaded into the GSR by the activation module. To load the interception module in any other process only requires therefore that the interception module load itself using DosLoadModule. (As will be explained below, this should only be done after stack and recursion checks have been made.) In any case, once DosLoadModule has been called by the interception module it acquires the status of a loaded DLL in the process which is unwittingly calling the interception module through having the DOSCALL1 APIs hooked. As mentioned above, the interception module should check if it is being called from DOSCALL1 before formally loading itself In OS/2, a call to DosQueryModuleFromEIP tells the interception module if the calling module is DOSCALL1. This is useful because it is possible that the interception module itself may make calls to DOSCALL1, and there would therefore be a danger of recursion if the interception module continued its processing. So, by only proceeding if the calling module is not DOSCALL1, the possibility of recursion is eliminated. This also avoids a further potential problem—during process initialization and the execution of some system APIs in DOSCALL1, the system makes use of temporary stacks which prohibit the registration of exception handlers. As explained below, in the preferred embodiment, the interception module uses exception handlers to obtain configuration data and so this would prevent the interception module obtaining the information it needed to modify (or not) an API call. This simple check is a particularly good way to avoid recursing through the interception module, because, as explained above, until a module becomes a formally loaded module in the process in which it is executing, it has no global or instance data in which to record state and variables. It can only use local data, i.e. stack-based data, where it is difficult for other instances of even the same module locate data. As mentioned above, the interception module of the preferred embodiment also carries out checks on the system stack before loading itself The system records information about the original stack in the thread information block (TIB), which is available for use by the application. The interception module accesses the TIB for the current thread and compares the extended stack pointer (ESP) register with the stack pointer and size. In addition the interception module checks that the current stack corresponds to the originally allocated stack. This tells the interception module whether the original application is a 32-bit application or if, for example, the calling process is a 16-bit application switched to 32-bit code and a 32-bit stack. In this case, where the API call being intercepted is DosAllocMem, it would be undesirable to intercept and alter memory allocation parameters because of the need to retain 16/32-bit addressing compatibility, as it can be said with near certainty that a 16-bit application will not wish to have its memory allocations forced high since there is a strong chance they will be accessed by 16-bit code and so this possibility is disallowed. This check simplifies configuration information by obviating the need to code multiple exceptional cases into the configuration file where API modification should not be done. So, once the interception module has made the above checks and has determined that it is free to load itself, it needs to obtain access to the configuration data supplied by the activation module. In order to serialize access to the configuration data, the interception module implements its own multiple-read, single-write non-recursive spin lock package. The spin lock mechanism is non-reentrant, shared and exclusive and can be used by C and Assembler language code, of which the embodiment of this invention is a mixture of both. The spin lock mechanism, which is well known, essentially involves a memory location within the interception module global data segment, FIG. 3, that is set to zero to indicate write access allowed and nonzero to indicate write access prohibited. Acquiring a spin lock involves looping while testing the spin lock to become zero. The test and update must be accomplished using an atomic instruction. All processor platforms that support multiprocessing provide such instructions for this purpose, for example, the Compare and Exchange instruction on Intel. This allows multiple read accesses to the configuration data and exclusive write access and allows the interception module to be multiprocessor safe with minimal performance overheads. To access the configuration data, the interception module uses DosGetSharedMem, and to do that efficiently, the interception module activates an exception handler that attempts to access the configuration data. While this can be done on every call to the interception module, a performance advantage is gained by doing this only once in each process that formally loads the interception module. This is achieved, as described below, by using an exception handler to intercept an access violation on a first attempted access from which a call DosGetSharedMem is made. An exception handler is a subroutine, registered with the operating system, that is given control whenever a potentially fatal exception occurs under the thread on which the handler is registered. For example, a page fault exception generated by the processor when memory access is attempted to a location for which the present flag is not set in the corresponding page table entry for that virtual memory location. If the interception module page faults (that is generates a page fault exception because the page table entry for this address does not have the present flag set) then the exception handler is invoked and the interception module calls DosGetSharedMem from there. (The effect of DosGetSharedMem is to cause the page table entries that give access to the shared data to be built for the calling process). So having dealt with the considerations behind the implementation of the preferred embodiment, FIGS. 4 and 5 illustrate the operation of the embodiment: To begin, the first instance of the activation module, having determined it is such, sets the communications semaphore, step 40, FIG. 4a. The first instance then launches the second instance, step 42 and then simply waits on changes to the communications buffer prefix, step 44. Whenever such a message arrives, for example, AM#2 saying it has hooked the API successfully, it is displayed for the end user, step 46. In the meantime, the second instance determines it is such by successfully opening the communications semaphore, step 48. It then makes some basic checks to determine for example, that the versions of interception module and activation module are compatible, step 50, and that the interception module is indeed loaded in the global shared region, step 52. If this is not the case, a message can be sent to the user via the first instance of the activation module. The interception module and its global data segment are of course available to the activation module and as such it is able to store a "hooked" flag in the global data segment, which is initially reset, step 54. A named area of low shared memory "AM Config", FIG. 3, is then allocated, step 56, and the activation module writes a pointer to this area in the interception module global data segment, step 58, thus making this information available to any formally loaded instance of the interception module. The activation module then initializes some statistical counters, again within the interception module global data segment, step 60, and these can be reported at any stage to, for example, third and subsequent instances of the activation module. The activation module then acquires the configuration data spin lock, step 62, again stored in the interception module global data segment, writes configuration data read from the configuration file to AM Config, step 64, and releases the spin lock, step 66. The activation module now begins the process of hooking the API calls that are to be intercepted. It first aliases the variable within the interception module which is to store the original kernel entry point, step 68, and then aliases the pages within DOSCALL1 which contain the addresses of the kernel entry points for the API calls to be intercepted, step 70. Then, for reasons explained above, other processors are taken off-line and interrupts disabled, steps 72 and 74. Then to avoid page faults, the activation module performs a dummy store into interception module original kernel entry point alias and performs a read from the DOSCALL1 API call instruction addresses, steps 76 and 78. Then the actual hooking takes place, when the activation module saves the DOSCALL1 API call kernel entry point into the interception module code segment alias and stores the interception module entry point into the corresponding DOSCALL1 API call instruction alias, step 80 and 82. Once complete, interrupts are re-enabled, multiprocessors are brought back online and the hooked flag is set, steps 84 to 88. Once the hooked flag is set, the activation module can now create the termination semaphore, step 90, and subsequently indicate to the first instance of the activation module via the communications buffer that it can terminate, step 92. The second instance now remains in memory simply waiting for an instruction to unhook the interception module. To unhook the interception module, steps 72 to 88 are simply reversed with the API call addresses stored in the interception module aliases being written back into the DOSCALL1 aliases, FIG. 4b. While hooked, the interception module operates as shown in FIG. 5. When called, the module first determines if it is being called from DOSCALL1, step 100. If so, then the original kernel entry point is called, step 102. If not, then the TIB and stack are checked as described above to determine if the calling application is a 32-bit application, step 104. If not, the original kernel entry point is called, step 102. Otherwise the module proceeds to formally load itself, step 106. This gives access to statistical variables, the hooked flag, the configuration data spin lock, the pointer to the configuration data etc. It also allows APIs and run-time routines that require the calling process to use instance and global data to be called by the interception module. The exception handler is then created, step 108, and, if an exception is created, configuration data is made accessible by calling DosGetSharedMem, step 110. In the present case, the interception module again uses DosQueryModuleFromEIP to determine the calling module and the calling process's name, steps 112 and 114. Use DosQueryModuleFromEIP against address 0x10000 to determine process name, works because all process's executables are loaded at 0x10000. If this ever changes the process's name could be determined by a number of alternative means. If the process name and calling module match the criteria set in the configuration data, then the appropriate manipulation of the API call is carried out by the interception module, and if necessary the kernel is called, steps 116 and 102. Finally, it should be noted that if the kernel is called, as in the case of intercepted DosAllocMem API calls, the kernel always returns directly to original calling process. In summary, the preferred embodiment deals with many problems associated with intercepting API calls, particularly in a multiprocessor virtual memory system. By having the interception module make a couple of checks and then load itself using DosLoadModule, an API normally used for loading DLLs other than oneself, the interception module becomes loaded in all address spaces so making the alteration of the intercepted API call for all calling processes. Again, having the interception module load itself enables each instance of the module to receive common information from the activation module including configuration information. It also enables the interception module to set up a spin lock common to all instances of the module thus enabling access to the interception module's configuration information to be serialized. Using two instances of the activation module enables communication with the user and yet allows the activation module to run as a daemon process and so maintain the interception module in memory. The communication/termination semaphore mechanism enabling instances of the activation module to identify themselves in turn allows a third instance to disable interception, possibly reconfigure and then re-enable interception dynamically. It should be noted that all code and data segments of an executable module are loaded into or allocated from a private area. Normally code as well as read-only data segments have a read-only attribute and so they acquire the additional attribute of shared. Read/write data segments are normally used for data which is private to a particular instance of a process and hence acquire the additional default attribute private. The shared attribute instructs the operating system to make all instances of a memory object use the same physical memory. However, the private attribute causes a separate areas of physical memory to be mapped to each instance of the memory object. The attributes of a module's segments may be specified explicitly as parameters to a linkage editor when the module is compiled. By specifying explicitly that a read/write segment of the activation module be shared guarantees that a common read/write area is automatically created when the first instance of a module is loaded and subsequent instances will share the existing instance. This avoids the ramifications of using memory allocation APIs explicitly which this code is in the process of altering. It should be seen that while the preferred embodiment has been described in terms of the OS/2 operating system, the invention is not necessarily limited to this particular system. As explained above, neither is the invention limited to altering the operation of the DosAllocMem API call, and is equally adapted to intercept any other API call.
|
Same subclass Same class Consider this |
||||||||||
