Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
7.8 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. Extend.cpp
  5. Abstract:
  6. Contains a list of extended bad functions.
  7. These are not compatibility problems, but
  8. can be used for training purposes.
  9. Notes:
  10. ANSI only - must run on Win9x.
  11. History:
  12. 05/16/01 rparsons Created
  13. 01/10/02 rparsons Revised
  14. --*/
  15. #include "demoapp.h"
  16. extern APPINFO g_ai;
  17. /*++
  18. Routine Description:
  19. Performs a simple access violation.
  20. Arguments:
  21. None.
  22. Return Value:
  23. None.
  24. --*/
  25. void
  26. AccessViolation(
  27. void
  28. )
  29. {
  30. // We could perform hundreds of different operations to get an
  31. // access violation. The one below attempts to copy the string
  32. // 'foo' to a NULL address.
  33. memcpy(NULL, "foo", 3);
  34. /* Here's another:
  35. WCHAR szArray[10];
  36. int nCount = 0;
  37. for (nCount = 0; nCount != 12; nCount++)
  38. {
  39. wcscpy((LPWSTR)szArray[nCount], L"A");
  40. }*/
  41. }
  42. /*++
  43. Routine Description:
  44. Throws a EXCEPTION_ARRAY_BOUNDS_EXCEEDED exception.
  45. Arguments:
  46. None.
  47. Return Value:
  48. None.
  49. --*/
  50. //
  51. // Disable the warning: 'frame pointer register 'ebp' modified by
  52. // inline assembly code. We're doing this intentionally.
  53. //
  54. #pragma warning( disable : 4731 )
  55. void
  56. ExceedArrayBounds(
  57. void
  58. )
  59. {
  60. _asm {
  61. push ebp; // Save the base pointer
  62. mov ebp, esp; // Set up the stack frame
  63. sub esp, 8; // Adjust for 8 bytes worth of
  64. // local variables
  65. push ebx; // Save EBX for later
  66. mov dword ptr [ebp-8], 1; // Initialize a DWORD to 1
  67. mov dword ptr [ebp-4], 2; // Initialize a DWORD to 2
  68. mov eax, 0; // Zero out EAX
  69. bound eax, [ebp-8]; // If [ebp-8] is out of EAX bounds,
  70. // an exception will occur
  71. xor eax, eax; // Zero out EAX
  72. pop ebx; // Restore EBX from earlier
  73. mov esp, ebp; // Restore the stack frame
  74. pop ebp; // Restore the base pointer
  75. ret; // We're done
  76. }
  77. }
  78. #pragma warning( default : 4731 )
  79. /*++
  80. Routine Description:
  81. Frees memory allocated from the heap twice.
  82. Arguments:
  83. None.
  84. Return Value:
  85. None.
  86. --*/
  87. void
  88. FreeMemoryTwice(
  89. void
  90. )
  91. {
  92. LPSTR lpMem = NULL;
  93. lpMem = (LPSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
  94. if (!lpMem) {
  95. return;
  96. }
  97. HeapFree(GetProcessHeap(), 0, lpMem);
  98. HeapFree(GetProcessHeap(), 0, lpMem);
  99. }
  100. /*++
  101. Routine Description:
  102. Allocates memory from the heap, moves the pointer,
  103. then tries to free it.
  104. Arguments:
  105. None.
  106. Return Value:
  107. None.
  108. --*/
  109. void
  110. FreeInvalidMemory(
  111. void
  112. )
  113. {
  114. LPSTR lpMem = NULL;
  115. lpMem = (LPSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH);
  116. if (!lpMem) {
  117. return;
  118. }
  119. lpMem++;
  120. lpMem++;
  121. HeapFree(GetProcessHeap(), 0, lpMem);
  122. }
  123. /*++
  124. Routine Description:
  125. Executes a privileged instruction, which causes an exception.
  126. Arguments:
  127. None.
  128. Return Value:
  129. None.
  130. --*/
  131. void
  132. PrivilegedInstruction(
  133. void
  134. )
  135. {
  136. _asm {
  137. cli;
  138. }
  139. }
  140. /*++
  141. Routine Description:
  142. Allocates memory from the heap, then walks all over it.
  143. Arguments:
  144. None.
  145. Return Value:
  146. None.
  147. --*/
  148. void
  149. HeapCorruption(
  150. void
  151. )
  152. {
  153. UINT n, nTotalChars;
  154. char szCorruptChars[] = {'B','A','D','A','P','P','#'};
  155. char* pChar = NULL;
  156. //
  157. // Allocate a small block of heap to contain a string of length STRINGLENGTH.
  158. //
  159. pChar = (char*)GlobalAlloc(GPTR, 13);
  160. if (!pChar) {
  161. return;
  162. } else {
  163. //
  164. // Determine the total number of chars to copy (including Corrupt Bytes)
  165. //
  166. nTotalChars = 12 + 1;
  167. //
  168. // Write a string of STRINGLENGTH length, plus some extra (common mistake).
  169. //
  170. for (n = 0; n < nTotalChars; n++) {
  171. //
  172. // Write the characters of CorruptChars into memory.
  173. //
  174. if (n < (UINT)12) {
  175. pChar[n] = szCorruptChars[n % 6];
  176. } else {
  177. //
  178. // Corruption will be written with a nice pattern of # chars...
  179. //
  180. pChar[n] = szCorruptChars[6];
  181. }
  182. }
  183. //
  184. // Of course, we should NULL terminate the string.
  185. //
  186. pChar[n] = 0;
  187. GlobalFree((HGLOBAL)pChar);
  188. }
  189. }
  190. /*++
  191. Routine Description:
  192. Launches a child process (version.exe) which is embedded
  193. within our DLL.
  194. Arguments:
  195. cchSize - Size of the output buffer in characters.
  196. pszOutputFile - Points to a buffer that will be filled
  197. with the path to the output file.
  198. Return value:
  199. On success, pszOutputFile points to the file.
  200. On failure, pszOutputFile is NULL.
  201. --*/
  202. void
  203. ExtractExeFromLibrary(
  204. IN DWORD cchSize,
  205. OUT LPSTR pszOutputFile
  206. )
  207. {
  208. HRSRC hRes;
  209. HANDLE hFile;
  210. HRESULT hr;
  211. HGLOBAL hLoaded = NULL;
  212. BOOL bResult = FALSE;
  213. HMODULE hModule = NULL;
  214. PVOID pExeData = NULL;
  215. DWORD cbFileSize, cbWritten, cchBufSize;
  216. __try {
  217. //
  218. // Find the temp directory and set up a path to
  219. // the file that we'll be creating.
  220. //
  221. cchBufSize = GetTempPath(cchSize, pszOutputFile);
  222. if (cchBufSize > cchSize || cchBufSize == 0) {
  223. __leave;
  224. }
  225. hr = StringCchCat(pszOutputFile, cchSize, "version.exe");
  226. if (FAILED(hr)) {
  227. __leave;
  228. }
  229. //
  230. // Obtain a handle to the resource and lock it so we can write
  231. // the bits to a file.
  232. //
  233. hModule = LoadLibraryEx("demodll.dll",
  234. NULL,
  235. LOAD_LIBRARY_AS_DATAFILE);
  236. if (!hModule) {
  237. __leave;
  238. }
  239. hRes = FindResource(hModule, (LPCSTR)105, "EXE");
  240. if (!hRes) {
  241. __leave;
  242. }
  243. cbFileSize = SizeofResource(hModule, hRes);
  244. if (cbFileSize == 0) {
  245. __leave;
  246. }
  247. hLoaded = LoadResource(hModule, hRes);
  248. if (!hLoaded) {
  249. __leave;
  250. }
  251. pExeData = LockResource(hLoaded);
  252. if (!pExeData) {
  253. __leave;
  254. }
  255. hFile = CreateFile(pszOutputFile,
  256. GENERIC_WRITE,
  257. 0,
  258. NULL,
  259. CREATE_ALWAYS,
  260. FILE_ATTRIBUTE_NORMAL,
  261. NULL);
  262. if (INVALID_HANDLE_VALUE == hFile) {
  263. __leave;
  264. }
  265. WriteFile(hFile, (LPCVOID)pExeData, cbFileSize, &cbWritten, NULL);
  266. CloseHandle(hFile);
  267. bResult = TRUE;
  268. } // __try
  269. __finally {
  270. if (hModule) {
  271. FreeLibrary(hModule);
  272. }
  273. if (hLoaded) {
  274. UnlockResource(hLoaded);
  275. }
  276. if (!bResult) {
  277. *pszOutputFile = 0;
  278. }
  279. }
  280. }