Using AppVerifier to find heap corruption

Source: Internet
Author: User

Let's look at the following code first

void Ui::wnd::cdesktopwnd::exe2shortcut (Lpcwstr strFullPath, Lpcwstr strFileName, Lpcwstr shelllink_path) {CString str  Descname = strFileName;  if (Strdescname = = _t ("Iexplore.exe")) {Strdescname = _t ("Internet Explorer");    } pathrenameextension (Strdescname.getbuffer (0), _t (""));  CString strfilenametmp = strFileName;  CString Strlnk = Getlnkpath ();   Pathrenameextension (Strfilenametmp.getbuffer (0), desktopwnd_shortcut_ext);   CString strlnkfiletxt = Strlnk + _t ("\ \") + strfilenametmp;  Strfilenametmp = strFileName; Pathrenameextension (Strfilenametmp.getbuffer (0), _t (". bmp")); The same problem as above CString Stricopath = Strlnk + _t ("\ \") + strfilenametmp; Ciconsnap is; Is.  GetIcon (strFullPath);  Ciconsnap Iconsnap; if (FALSE = = Iconsnap. Save (strFullPath, Stricopath)) {Simple_trace (L "extract icon failed!)    "); :: MessageBox (M_hwnd, _t ("Extract icon failed!  "), _t (" message "), 0);    } else {Applogicalcontainer iconcontainer;    BOOL baddsuc = TRUE; if (shelllink_path==0) {if (Strdescname! = _t ("inTernet Explorer ") {baddsuc = Iconcontainer.addapp (Strfullpath,stricopath,strdescname, Shelllink_path);    } if (BADDSUC) {CString Strskinpath = Getskinpath (); M_iconlistview.addbuttonex (Strdescname, strFullPath, M_hwnd, Handlerparam (this, onbtnmessage), StrIcoPath, StrSkinPa   th + _t ("\\tools\\desktop_icon_back3.png"), Strskinpath + _t ("\\tools\\desktop_icon_back1.png");     }} else if (Shelllink_path) {CString Strskinpath = Getskinpath (); M_iconlistview.addbuttonex (Strdescname, Shelllink_path, M_hwnd, Handlerparam (this, onbtnmessage), StrIcoPath,    Strskinpath + _t ("\\tools\\desktop_icon_back3.png"), Strskinpath + _t ("\\tools\\desktop_icon_back1.png");  }  }}


This code is problematic, trace debugging occurs when memory is released, the following is the stack

Pathrenameextension (str. Getbuffer thread fffffa8002791b60 Cid 0ab0.0ad8 teb:000007fffff8e000 win32thread:fffff900c22cf600 WAIT: ( userrequest) UserMode non-alertable            fffffa800291c5b0 processobject            FFFFFA80038D2E90 notificationevent        not impersonating    & nbsp   DeviceMap fffff8a000f67550        owning Process fffffa8002777060 image:sandboxmgr.exe        attached Process N/a image:n/a        Wait Start TickCount 36688 ticks:8719 (0: 00:02:16.017)         Context Switch Count 1282 largestack        Usertime 00:00:00 .124        kerneltime 00:00:00.374        Win32 Start Address sandboxmgr!_threads Tartex (0x000000013f35a670)         Stack Init fffff88003c62d70 current fffff88003c61f90        Base fffff88003c63000 Limit fffff88003c51000 call 0        Priority basepriority 8 Unusualb Oost 0 foregroundboost 2 iopriority 2 pagepriority 5        Kernel stack not resident.    &N Bsp   CHILD-SP RetAddr Call site        fffff880 ' 03c61fd0 fffff800 ' 03ed0052 nt! kiswapcontext+0x7a        fffff880 ' 03c62110 fffff800 ' 03ecc54b nt! kicommitthreadwait+0x1d2        fffff880 ' 03c621a0 fffff800 ' 041C1BCF nt! kewaitformultipleobjects+0x271        fffff880 ' 03c62450 fffff800 ' 041c24d6 nt! obpwaitformultipleobjects+0x294        fffff880 ' 03c62920 fffff800 ' 03ec8153 nt! ntwaitformultipleobjects+0xe5        fffff880 ' 03c62b70 00000000 ' 7749046a nt! kisystemservicecopyend+0x13 (trapframe @ fffff880 ' 03c62be0)         00000000 ' 04b9cac8 000007fe ' Fd6213a6 ntdll! ntwaitformultipleobjects+0xa        00000000 ' 04b9cad0 00000000 ' 77243143 kernelbase! waitformultipleobjectsex+0xe8        00000000 ' 04b9cbd0 00000000 ' 772b9025 kernel32! waitformultipleobjectseximplementation+0xb3        00000000 ' 04b9cc60 00000000 ' 772b91a7 kernel32! werpreportfaultinternal+0x215        00000000 ' 04b9cd00 00000000 ' 772b91ff kernel32! werpreportfault+0x77        00000000 ' 04b9cd30 00000000 ' 772b941c kernel32! basepreportfault+0x1f        00000000 ' 04b9cd60 00000000 ' 774d573c kernel32! unhandledexceptionfilter+0x1fc        00000000 ' 04b9ce40 00000000 ' 77455148 ntdll!?? :: Fnodobfm:: ' String ' +0x2365        00000000 ' 04b9ce70 00000000 ' 7747554d ntdll!_c_specific_handler +0x8c        00000000 ' 04b9cee0 00000000 ' 77455d1c ntdll! rtlpexecutehandlerforexception+0xd        00000000 ' 04b9cf10 00000000 ' 7748fe48 ntdll! rtldispatchexception+0x3cb        00000000 ' 04b9d5f0 00000000 ' 77491da0 ntdll! KIUSEREXCEPTIONDISPATCHER+0X2E (trapframe @ 00000000 ' 04b9da18)         00000000 ' 04b9dbb0 000007fe ' fbd6254a ntdll! rtlfreeheap+0xd0        00000000 ' 04b9dc30 000007fe ' F6cd13ca fltlib! filterconnectcommunicationport+0x1da        00000000 ' 04b9dd30 000007fe ' f6cd4033 estsbfile! estsbfilemanager+0x6a [E:\ronggf\work\branches\minsheng\client_windows\src\estsbfile08\devicectrol.cpp @ 345]         00000000 ' 04b9dda0 00000001 ' 3f1c43d1 estsbfile! estloginformationprint+0x103 [E:\ronggf\work\branches\minsheng\client_windows\src\estsbfile08\estsbfile.cpp @ 1684]        00000000 ' 04b9de20 00000001 ' 3f1c4545 sandboxmgr!myout::p rint+0x31 [E:\ronggf\work\ Branches\minsheng\client_windows\src\include\misc\myout.h @ 100]        00000000 ' 04b9de50 00000001 ' 3f1fa789 sandboxmgr!global::trace::output_trace+0x165 [E:\ronggf\work\branches\minsheng\client_Windows\src\include\misc\trace.h @ 18]        00000000 ' 04b9ef40 00000001 ' 3f1f27c9 sandboxmgr! ciconsnap::save+0x109 [E:\ronggf\work\branches\minsheng\client_windows\src\sandboxmgr\iconsnap.cpp @ 122]        00000000 ' 04b9f2b0 00000001 ' 3f1f6782 sandboxmgr!ui::wnd::cdesktopwnd::exe2shortcut+0x1c9 [e:\ Ronggf\work\branches\minsheng\client_windows\src\sandboxmgr\desktopwnd.cpp @ 579]        00000000 ' 04b9f430 00000001 ' 3f1f06e0 sandboxmgr!ui::wnd::cdesktopwnd::onrecvopendialogdata+0x42 [e:\ronggf\work\ Branches\minsheng\client_windows\src\sandboxmgr\desktopwnd.cpp @ 1574]        00000000 ' 04b9f480 00000001 ' 3f1f9f96 sandboxmgr!ui::wnd::cdesktopwnd::P rocesswindowmessage+0x4c0 [e:\ronggf\work\branches\minsheng\ Client_windows\src\sandboxmgr\desktopwnd.h @ 130]        00000000 ' 04b9f4d0 00000000 ' 7735c3c1 sandboxmgr! Atl::cwindowimplbaset<atl::cwindow,atl::cwintraits<1442840576,0> >::windowproc+0xc6 [C:\Program Files (x86) \microsoft Visual Studio 9.0\vc\atlmfc\include\atlwin.h @ 3081]        00 000000 ' 04b9f590 00000000 ' 7735c60a user32! usercallwinproccheckwow+0x1ad        00000000 ' 04b9f650 00000001 ' 3f208478 user32! dispatchmessageworker+0x3b5        00000000 ' 04b9f6d0 00000001 ' 3f35a65b sandboxmgr! cvirtualdesktop::newdesktoprunloop+0x478 [e:\ronggf\work\branches\minsheng\client_windows\src\sandboxmgr\ Virtualdesktop.cpp @ 328]        00000000 ' 04b9fac0 00000001 ' 3f35a70f Sandboxmgr!_callthreadstartex +0x17 [f:\dd\vctools\crt_bld\self_64_amd64\crt\src\threadex.c @ 348]        00000000 ' 04b9faf0 00000000 ' 7723f56d sandboxmgr!_threadstartex+0x9f [f:\dd\vctools\crt_bld\self_64_amd64\crt\src\threadex.c @ 326]         00000000 ' 04b9fb20 00000000 ' 77473281 kernel32! basethreadinitthunk+0xd        00000000 ' 04b9fb50 00000000 ' 00000000 ntdll! rtluserthreadstart+0x1d  a problem in the rtlfreeheap. What about this time? From the scene must be piled up problems stomach, that FLB or ntdll on the search is unable to find the clue. At this point, AppVerifier and gflags can be used for heap allocation validation and release validation. AppVerifier is easy to use, so it uses AppVerifier.   After using AppVerifier, there was a problem, promptingpathrenameextensionThere is a problem at the place. Just say the result: because CString's GetBuffer directly 0, no extra space is allocated to cause pathrenameextension A. txt four byte is appended to it, causing the write memory to exceed the allocated memory range, and then, when memory is freed, the memory management checks to the heap structure boundary to be corrupted and checked out. The original crash was caused by a few bytes written that destroyed the heap allocation table and collapsed. Then look at the above code has the following question (analysis), there are any questions please add.
void Ui::wnd::cdesktopwnd::exe2shortcut (Lpcwstr strFullPath, Lpcwstr strFileName, Lpcwstr shelllink_path) {CString str  Descname = strFileName;  if (Strdescname = = _t ("Iexplore.exe"))//not rigorous {Strdescname = _t ("Internet Explorer");  } pathrenameextension (Strdescname.getbuffer (0), _t (""));  The above conditions are not necessary at the time of establishment CString strfilenametmp = strFileName;  CString Strlnk = Getlnkpath ();  Pathrenameextension (Strfilenametmp.getbuffer (0), desktopwnd_shortcut_ext); There is a problem code, because//cstring::getbuffer does not increase memory cause when the strFileName extension does not have or is smaller than the length of the replaced extension, it will appear to write data to unallocated memory CString strlnkfiletxt = StrL    NK + _t ("\ \") + strfilenametmp;  The efficiency is very low, two temporary variables, two times the destruction strfilenametmp = strFileName; Pathrenameextension (Strfilenametmp.getbuffer (0), _t (". bmp")); The same problem as above CString Stricopath = Strlnk + _t ("\ \") + strfilenametmp; Ciconsnap is; Is.  GetIcon (strFullPath);  Ciconsnap Iconsnap; if (FALSE = = Iconsnap. Save (strFullPath, Stricopath)) {Simple_trace (L "extract icon failed!)    "); :: MessageBox (M_hwnd, _t ("Extract icon failed! "), _t (" message "), 0);    } else {Applogicalcontainer iconcontainer;    BOOL baddsuc = TRUE; if (shelllink_path==0) {if (Strdescname! = _t ("Internet Explorer")) {baddsuc = Iconcontainer.addapp (strfull      Path,stricopath,strdescname, Shelllink_path);    } if (BADDSUC) {CString Strskinpath = Getskinpath (); M_iconlistview.addbuttonex (Strdescname, strFullPath, M_hwnd, Handlerparam (this, onbtnmessage), StrIcoPath, StrSkinPa   th + _t ("\\tools\\desktop_icon_back3.png"), Strskinpath + _t ("\\tools\\desktop_icon_back1.png");     }} else if (Shelllink_path) {CString Strskinpath = Getskinpath (); M_iconlistview.addbuttonex (Strdescname, Shelllink_path, M_hwnd, Handlerparam (this, onbtnmessage), StrIcoPath,    Strskinpath + _t ("\\tools\\desktop_icon_back3.png"), Strskinpath + _t ("\\tools\\desktop_icon_back1.png"); }  }}

After analysis, this writer has not mastered the use of CString and the APIpathrenameextensionof carelessness. This code can be used as an example of error correction.

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.