Example of synchronization between processes among multiple producers-one consumer-outputdebugstring

Source: Internet
Author: User
Document directory
  • Content directory

Applicable scenarios of outputdebugstring:
At the same time, multiple application processes (producers) can send debugging information. Only one debugging process (consumer, egdebugview, and vs2005) can be accepted.

Implementation Method:
The producer synchronizes data through mutex.
The producer and consumer are synchronized through two events.
Consumers can exit to ensure that there is only one consumer once the event or buffer already exists.

Reference
Meaning: Win32 "outputdebugstring"
Http://www.cnblogs.com/xfxsworld/archive/2008/03/22/1117794.html

Translation: Win32 "outputdebugstring"

Strong Win32 developers may Outputdebugstring ()You are familiar with API functions, which allow your program to communicate with the debugger. It is easier than creating a log file and can be used by all "real" debuggers. The mechanism for applications to talk to the debugger is quite simple, and this article will reveal how the whole thing works.

This article is first prompted by the following events. We observe thatOutputdebugstring ()It does not always work reliably (at least on win2000) When administrators and non-administrator users try to work together or play games ). We suspect that it is related to the permission of kernel objects, which involves a lot of information that has to be written down.

Please note that although we use the term "Debugger", it is not used in the sense of debugging the API: there is no such thing as "one-step execution", "breakpoint", or "attach to process" that can be found in ms visual c or in real interactive development environments. In a sense, any program that implements the protocol is a "Debugger ". It may be a very small command line tool, or something as advanced as the debugview of the smart guys from sysinternals.

Content directory
  • Application Usage
  • Protocol
  • Permission problems
  • Implementation Details
  • Confused
  • Unixwiz.net tool: dbmutex

Application Usage

<Windows. h>File declaresOutputdebugstring ()Two versions of the function-one for ASCII and the other for Unicode-unlike the vast majority of Win32 APIs, the original version is ASCII. The original version of most Win32 APIS is Unicode.

Simple call using a string buffer ending with nullOutputdebugstring ()This will cause information to appear in the debugger, if any. The common usage of building and sending a piece of information is:

sprintf(msgbuf, "Cannot open file %s [err=%ld]/n", fname, GetLastError());OutputDebugString(msgbuf);

However, in the actual environment, many of us will create a front-end function to allow us to use printf-style formatting. The followingOdprintf ()The function is used to format the string, ensure that there is a proper line break at the end (delete the end of the original line), and send the information to the debugger.

#include <stdio.h>#include <stdarg.h>#include <ctype.h>void __cdecl odprintf(const char *format, ...){char buf[4096], *p = buf;va_list args;va_start(args, format);p += _vsnprintf(p, sizeof buf - 1, format, args);va_end(args);while ( p > buf  &&  isspace(p[-1]) )*--p = '/0';*p++ = '/r';*p++ = '/n';*p   = '/0';OutputDebugString(buf);}

Therefore, using it in code is simple:

        ...odprintf("Cannot open file %s [err=%ld]", fname, GetLastError());...

We have been using this for many years.

Protocol

Data is transmitted between the application and the debugger through a 4 kb shared memory block, and there is a mutex and two event objects to protect access to them. The following are four kernel objects:

Object Name Object Type
Dbwinmutex Mutex
Dbwin_buffer Section (shared memory)
Dbwin_buffer_ready Event
Dbwin_data_ready Event

Mutex is usually kept in the system, and the other three objects only appear when the debugger wants to receive information. In fact, if a debugger finds that the last three objects already exist, it will refuse to run.

When dbwin_buffer appears, it is organized into the following structure. The process ID displays the source of information, and the string data is filled with the remaining 4 K. According to the Conventions, the end of the informationAlwaysContains a NULL byte.

struct dbwin_buffer {                DWORD   dwProcessId;                char    data[4096-sizeof(DWORD)];                };                

WhenOutputdebugstring ()When the application is called, it performs the following steps. Note that errors at any location will discard the entire process and the debugging request will be considered as nothing (no strings will be sent ).

  1. OpenDbwinmutexAnd wait until we have obtained exclusive access.
  2. IngDbwin_bufferSegment to memory: If not found, no debugger is running and the entire request is ignored.
  3. OpenDbwin_buffer_readyAndDbwin_data_readyEvent object. Like shared memory segments, the absence of objects means that no debugger is available.
  4. WaitDbwin_buffer_readyThe event object is in a signal state, indicating that the memory buffer is no longer occupied. Most of the time, this event object is in a signal state as soon as it is checked, but wait for the buffer to be ready for no more than 10 seconds (timeout will discard the request ).
  5. Copy the data until the memory buffer is close to 4 kb, and then save the current process ID. Always place a NULL byte to the end of the string.
  6. SetDbwin_data_readyThe event object tells the debugger that the buffer is ready. The debugger removes it from there.
  7. Release mutex.
  8. Close the event object and segment object, but retain the mutex handle for future use.

It is easier on the debugger. Mutex is not required at all. If the event object and/or shared memory object already exist, it is assumed that other debuggers are already running. The system can only have one debugger at any time.

  1. Create shared memory segments and two event objects. If it fails, exit.
  2. SetDbwin_buffer_readyEvent object, the application knows that the buffer zone is available.
  3. WaitDbwin_data_readyThe event object changes to a signal state.
  4. Extract the string ending with the process ID and null from the memory buffer.
  5. Go to step 2.

This makes us think that this is by no means a low-consumption Method for sending information, and the running speed of the application will be affected by the debugger.

Permission problems

We found thatOutputdebugstring ()Sometimes unreliableSeveral yearsAnd we have no idea why Microsoft hasn't done it well for so long. The strange thing is that the problem is always aroundDbwinmutexThis requires us to check the license system to find out why it is so troublesome.

The mutex object remains alive until the last program that uses it closes its handle, so it can be retained for a long time after the application that initially created it exits. Because this object is widely shared, it must be explicitly authorized to allow anyone to use it. In fact, the "default" license is almost never applicable, and this problem is counted as the first problem we observed in NT 3.51 and NT 4.0.

The correction method at that time was to use a widely open DACL to create mutex to allow anyone to access it, but it seems that these licenses have been enhanced in Win2000. On the surface, it looks correct, as we can see in the following table:

System Mutex_all_access
Administrators Mutex_all_access
Everybody Synchronize | read_control | mutex_query_state

Applications that want to send debugging information only need to wait for and obtain the mutex, that is, to haveSynchronizePermission. The permission listed above is completely correct for all participating users.

However, if someone observesCreatemutex ()When an object already exists, you will find something strange. In this case, Win32 is like calling the following code:

OpenMutex(MUTEX_ALL_ACCESS, FALSE, "DBWinMutex");                        

Although we only needSynchronizeBut it still assumes that the caller wantsAnything(Mutex_all_access). Because non-administrators do not have these permissions-only a few of the above columns-the mutex cannot be opened or obtainedOutputdebugstring ()Return quietly without doing anything.

Even running all software development as an administrator is not a complete correction method: if other users (such as services) are running as non-Administrators, the configuration is not permitted to be correct, their debugging information will be lost.

We feel that the real correction requires MicrosoftCreatemutex ()Add a parameter-if the object already exists, it is used for implicitOpenmutex ()The access mask of the call. Maybe one day we will seeCreatemutexex ()But we must use another method during this period. When the object is already in memory, we will change the license configuration on it.

This requires callingSetkernelobjectsecurity ()The following program snippets show how a program can open mutex and install a new DACL. This DACL is maintained even after the program exits, As long as any other program maintains a handle with it (this should be a mutex.

...                        // open the mutex that we're going to adjust                        HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "DBWinMutex");                        // create SECURITY_DESCRIPTOR with an explicit, empty DACL                        // that allows full access to everybody                        SECURITY_DESCRIPTOR     sd;                        InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);                        SetSecurityDescriptorDacl(                        &sd,            // addr of SD                        TRUE,           // TRUE=DACL present                        NULL,           // ... but it's empty (wide open)                        FALSE);         // DACL explicitly set, not defaulted                        // plug in the new DACL                        SetKernelObjectSecurity(hMutex, DACL_SECURITY_INFORMATION, &sd);                        ...                        

This method clearly follows the correct path, but we still need to find a place to place this logic. It is possible to put it in a small program that runs upon a request, but it seems that it may be interrupted. Our solution is to write a Win32 service to do this.

This is what our dbmutex tool does: it starts during system boot, opens or creates mutex, and then sets the object security to allow extensive access. Then sleep until the system is shut down and the mutex is turned on during this process. It does not consume any CPU time.

Implementation Details

We spent a lot of time using IDA pro to go deep into the implementation of Windows 2000 kernel32.dll. We believe that we have a good grasp of how it works on a more accurate basis. Here we provideOutputdebugstring ()The pseudo code of the function (we have not compiled it) and the function that creates the mutex.

We intentionally omitted most of the error checks: if something gets worse, it will release all allocated resources and exit, just as there is no debugger. The purpose is to demonstrate General behaviors rather than complete reverse engineering of the Code.

"Setup" function-name is ours-create a mutex or open it if it already exists. After some efforts to set the security of mutex object so that anyone can use it, although we will see that it is not completely correct.

• Outputdebugstring.txt

Confused

Some may feel that this is a security issue, but it is not. Non-administrator userIndeedHave proper useOutputdebugstring ()All permissions, but a reasonable request is rejected due to an incorrect form due to the common problem of "more permissions than required.

However, this is not intended as it is in most cases. Most of the errors are that the developer explicitly requests more (such as "mutex_all_access"), and the mask isCreatemutex ()ActionImplicit. This makes it more difficult to avoid Win32 API changes.

---

 

When analyzingOutputdebugstringa ()How can a non-Administrator weaken the system becomes apparent. Once the mutex is obtained, an application that sends debugging information will wait for a maximum of 10 seconds for the dbwin_buffer_ready event object to be ready. If it times out, it will give up. This seems to be a cautious precaution. If the debugging system is busy, it is used to avoid starvation.

But in the earlier step, wait for the mutex, and there is no such timeout setting. If any process in the system-including a non-privileged process-can open the mutex with the synchronize permission and do not release it, all other processes that attempt to obtain the mutex will stop the process infinitely.

Our research shows that all types of programs send random debugging information (for example, musicmatch jukebox has a choppy keyboard hook ), these threads can be stopped by a few lines of code. There is no need to stop the entire program-there may be other threads-but in reality, developers do not plan to useOutputdebugstring ()This will be a path to denial of service (for more information, see the original article ).

---

 

The most strange thing is that we foundOutputdebugstring ()It is not a natural Unicode function. Most Win32 APIs use Unicode functions ("W" version). If you call functions of the "A" version, they are automatically converted from ASCII to Unicode.

However, becauseOutputdebugstringThe data in the memory buffer is finally passed to the debugger as ASCII, which has a pair opposite to the conventional A/W pair. This implies that if you want to send a shortcut message to the debugger In the Unicode program, you can directly call the "A" version to implement it:

OutputDebugStringA("Got here to place X");                        

Simple implementation of outputdebugstring for monitoring the user layer
VC Knowledge Base blog-coder jozu-simple debugview

Http://blog.vckbase.com/jozu/archive/2005/03/23/3900.aspx

Sysinternals dbgview must have been used by everyone. I wrote a simple program that only monitors the output of outputdebugstring on the user layer. The principle is included in the code. I hope it will be helpful to you, if no, forget it: P.

 // Debugview. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H" </P> <p> // dbmntor. cpp: Little dbgview .... <Br/> // coder jozu <br/> // <br/> # include <windows. h> <br/> # include <tchar. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> # define page_size 4096 </P> <p> # define dbwin_map "dbwin_buffer" <br/> # define dbwin_writevent "dbwin_buffer_ready" <br/> # define dbwin_readevent "dbwin_data_ready" </P> <p> typedef struct _ tag_dbmap <br/>{< br/> DWORD dwprocessid; <br/> char szstring [page_size-sizeof (DWORD)]; <Br/>} db_map, * pdb_map; </P> <p> int main (INT argc, char * argv []) <br/>{ <br/> handle hmap; <br/> pdb_map pdbmap; </P> <p> handle heventread; <br/> handle heventwrite; <br/> handle hmutex; </P> <p> outputdebugstring ("Hello World"); </P> <p> hmutex = createmutex (null, false, "dbwinmutex"); <br/> If (! Hmutex) <br/>{< br/> printf ("createmutex ng. err = % d ", getlasterror (); <br/> system (" pause "); <br/> return 0; <br/>}</P> <p> heventread = createevent (null, false, false, dbwin_readevent); <br/> If (! Heventread) <br/>{< br/> printf ("createevent (read) ng. err = % d ", getlasterror (); <br/> system (" pause "); <br/> return 0; <br/>}< br/> // a debugger is already running and exits <br/> else if (error_already_exists = getlasterror ()) <br/> {<br/> printf ("createevent (read) ng. error_already_exists "); <br/> system (" pause "); <br/> return 0; <br/>}</P> <p> heventwrite = createevent (null, false, false, dbwin_writevent); <br/> If (! Heventwrite) <br/>{< br/> printf ("createevent (write) ng. err = % d ", getlasterror (); <br/> system (" pause "); <br/> return 0; <br/>}< br/> // a debugger is already running and exits <br/> else if (error_already_exists = getlasterror ()) <br/> {<br/> printf ("createevent (write) ng. error_already_exists "); <br/> system (" pause "); <br/> return 0; <br/>}</P> <p> hmap = createfilemapping (invalid_handle_value, <br/> null, <br/> page_readwr ITE, <br/> 0, <br/> page_size, <br/> dbwin_map); <br/> If (! Hmap) <br/>{< br/> printf ("createfilemapping ng. err = % d ", getlasterror (); <br/> system (" pause "); <br/> return 0; <br/>}< br/> // a debugger is already running and exits <br/> else if (error_already_exists = getlasterror ()) <br/> {<br/> printf ("createfilemapping ng. error_already_exists "); <br/> system (" pause "); <br/> return 0; <br/>}</P> <p> pdbmap = (pdb_map) mapviewoffile (hmap, <br/> file_map_read, <br/> 0, <br/> 0, <br/> 0); <Br/> If (! Pdbmap) <br/>{< br/> closehandle (hmap); <br/> system ("pause"); <br/> return 0; <br/>}</P> <p> while (true) <br/>{</P> <p> setevent (heventwrite ); <br/> waitforsingleobject (heventread, infinite); <br/> // resetevent (heventread); // if the second parameter of createevent is true, you need to manually set it to nosignel </P> <p> printf ("% d => % s/n", pdbmap-> dwprocessid, pdbmap-> szstring ); </P> <p >}</P> <p> system ("pause"); <br/> return 0; <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.