| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | 5 #include <windows.h> |
| 6 #include <Tlhelp32.h> | 6 #include <Tlhelp32.h> |
| 7 #include "sandbox/win/sandbox_poc/pocdll/exports.h" | 7 #include "sandbox/win/sandbox_poc/pocdll/exports.h" |
| 8 #include "sandbox/win/sandbox_poc/pocdll/utils.h" | 8 #include "sandbox/win/sandbox_poc/pocdll/utils.h" |
| 9 | 9 |
| 10 // This file contains the tests used to verify the security of threads and | 10 // This file contains the tests used to verify the security of threads and |
| 11 // processes. | 11 // processes. |
| 12 | 12 |
| 13 void POCDLL_API TestProcesses(HANDLE log) { | 13 void POCDLL_API TestProcesses(HANDLE log) { |
| 14 HandleToFile handle2file; | 14 HandleToFile handle2file; |
| 15 FILE *output = handle2file.Translate(log, "w"); | 15 FILE *output = handle2file.Translate(log, "w"); |
| 16 | 16 |
| 17 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); | 17 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); |
| 18 if (INVALID_HANDLE_VALUE == snapshot) { | 18 if (INVALID_HANDLE_VALUE == snapshot) { |
| 19 fprintf(output, "[BLOCKED] Cannot list all processes on the system. " | 19 fprintf(output, "[BLOCKED] Cannot list all processes on the system. " |
| 20 "Error %d\r\n", ::GetLastError()); | 20 "Error %ld\r\n", ::GetLastError()); |
| 21 return; | 21 return; |
| 22 } | 22 } |
| 23 | 23 |
| 24 PROCESSENTRY32 process_entry = {0}; | 24 PROCESSENTRY32 process_entry = {0}; |
| 25 process_entry.dwSize = sizeof(PROCESSENTRY32); | 25 process_entry.dwSize = sizeof(PROCESSENTRY32); |
| 26 | 26 |
| 27 BOOL result = ::Process32First(snapshot, &process_entry); | 27 BOOL result = ::Process32First(snapshot, &process_entry); |
| 28 | 28 |
| 29 while (result) { | 29 while (result) { |
| 30 HANDLE process = ::OpenProcess(PROCESS_VM_READ, | 30 HANDLE process = ::OpenProcess(PROCESS_VM_READ, |
| 31 FALSE, // Do not inherit handle. | 31 FALSE, // Do not inherit handle. |
| 32 process_entry.th32ProcessID); | 32 process_entry.th32ProcessID); |
| 33 if (NULL == process) { | 33 if (NULL == process) { |
| 34 fprintf(output, "[BLOCKED] Found process %S:%d but cannot open it. " | 34 fprintf(output, "[BLOCKED] Found process %S:%ld but cannot open it. " |
| 35 "Error %d\r\n", | 35 "Error %ld\r\n", |
| 36 process_entry.szExeFile, | 36 process_entry.szExeFile, |
| 37 process_entry.th32ProcessID, | 37 process_entry.th32ProcessID, |
| 38 ::GetLastError()); | 38 ::GetLastError()); |
| 39 } else { | 39 } else { |
| 40 fprintf(output, "[GRANTED] Found process %S:%d and open succeeded.\r\n", | 40 fprintf(output, "[GRANTED] Found process %S:%ld and open succeeded.\r\n", |
| 41 process_entry.szExeFile, process_entry.th32ProcessID); | 41 process_entry.szExeFile, process_entry.th32ProcessID); |
| 42 ::CloseHandle(process); | 42 ::CloseHandle(process); |
| 43 } | 43 } |
| 44 | 44 |
| 45 result = ::Process32Next(snapshot, &process_entry); | 45 result = ::Process32Next(snapshot, &process_entry); |
| 46 } | 46 } |
| 47 | 47 |
| 48 DWORD err_code = ::GetLastError(); | 48 DWORD err_code = ::GetLastError(); |
| 49 if (ERROR_NO_MORE_FILES != err_code) { | 49 if (ERROR_NO_MORE_FILES != err_code) { |
| 50 fprintf(output, "[ERROR] Error %d while looking at the processes on " | 50 fprintf(output, "[ERROR] Error %ld while looking at the processes on " |
| 51 "the system\r\n", err_code); | 51 "the system\r\n", err_code); |
| 52 } | 52 } |
| 53 | 53 |
| 54 ::CloseHandle(snapshot); | 54 ::CloseHandle(snapshot); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void POCDLL_API TestThreads(HANDLE log) { | 57 void POCDLL_API TestThreads(HANDLE log) { |
| 58 HandleToFile handle2file; | 58 HandleToFile handle2file; |
| 59 FILE *output = handle2file.Translate(log, "w"); | 59 FILE *output = handle2file.Translate(log, "w"); |
| 60 | 60 |
| 61 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL); | 61 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL); |
| 62 if (INVALID_HANDLE_VALUE == snapshot) { | 62 if (INVALID_HANDLE_VALUE == snapshot) { |
| 63 fprintf(output, "[BLOCKED] Cannot list all threads on the system. " | 63 fprintf(output, "[BLOCKED] Cannot list all threads on the system. " |
| 64 "Error %d\r\n", ::GetLastError()); | 64 "Error %ld\r\n", ::GetLastError()); |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 THREADENTRY32 thread_entry = {0}; | 68 THREADENTRY32 thread_entry = {0}; |
| 69 thread_entry.dwSize = sizeof(THREADENTRY32); | 69 thread_entry.dwSize = sizeof(THREADENTRY32); |
| 70 | 70 |
| 71 BOOL result = ::Thread32First(snapshot, &thread_entry); | 71 BOOL result = ::Thread32First(snapshot, &thread_entry); |
| 72 int nb_success = 0; | 72 int nb_success = 0; |
| 73 int nb_failure = 0; | 73 int nb_failure = 0; |
| 74 | 74 |
| 75 while (result) { | 75 while (result) { |
| 76 HANDLE thread = ::OpenThread(THREAD_QUERY_INFORMATION, | 76 HANDLE thread = ::OpenThread(THREAD_QUERY_INFORMATION, |
| 77 FALSE, // Do not inherit handles. | 77 FALSE, // Do not inherit handles. |
| 78 thread_entry.th32ThreadID); | 78 thread_entry.th32ThreadID); |
| 79 if (NULL == thread) { | 79 if (NULL == thread) { |
| 80 nb_failure++; | 80 nb_failure++; |
| 81 } else { | 81 } else { |
| 82 nb_success++; | 82 nb_success++; |
| 83 fprintf(output, "[GRANTED] Found thread %d:%d and able to open it.\r\n", | 83 fprintf(output, "[GRANTED] Found thread %ld:%ld and able to open it.\r\n", |
| 84 thread_entry.th32OwnerProcessID, | 84 thread_entry.th32OwnerProcessID, |
| 85 thread_entry.th32ThreadID); | 85 thread_entry.th32ThreadID); |
| 86 ::CloseHandle(thread); | 86 ::CloseHandle(thread); |
| 87 } | 87 } |
| 88 | 88 |
| 89 result = Thread32Next(snapshot, &thread_entry); | 89 result = Thread32Next(snapshot, &thread_entry); |
| 90 } | 90 } |
| 91 | 91 |
| 92 DWORD err_code = ::GetLastError(); | 92 DWORD err_code = ::GetLastError(); |
| 93 if (ERROR_NO_MORE_FILES != err_code) { | 93 if (ERROR_NO_MORE_FILES != err_code) { |
| 94 fprintf(output, "[ERROR] Error %d while looking at the processes on " | 94 fprintf(output, "[ERROR] Error %ld while looking at the processes on " |
| 95 "the system\r\n", err_code); | 95 "the system\r\n", err_code); |
| 96 } | 96 } |
| 97 | 97 |
| 98 fprintf(output, "[INFO] Found %d threads. Able to open %d of them\r\n", | 98 fprintf(output, "[INFO] Found %d threads. Able to open %d of them\r\n", |
| 99 nb_success + nb_failure, nb_success); | 99 nb_success + nb_failure, nb_success); |
| 100 | 100 |
| 101 ::CloseHandle(snapshot); | 101 ::CloseHandle(snapshot); |
| 102 } | 102 } |
| OLD | NEW |