Skip to content

Commit 4b82a86

Browse files
committed
5-11 C2
1 parent ccd3db4 commit 4b82a86

File tree

5 files changed

+145
-157
lines changed

5 files changed

+145
-157
lines changed

contrib/win32/win32compat/ssh-agent/agent-main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ int main(int argc, char **argv) {
100100
/* console app - start in debug mode*/
101101
SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
102102
log_init("ssh-agent", 7, 1, 1);
103-
return agent_start(TRUE, FALSE, 0, 0);
103+
agent_start(TRUE, FALSE, 0, 0);
104+
return 0;
104105
}
105106
else {
106107
char* h = 0;
107108
h += atoi(*(argv + 1));
108109
log_init("ssh-agent", config_log_level(), 1, 0);
109-
return agent_start(FALSE, TRUE, h, atoi(*(argv+2)));
110+
agent_start(FALSE, TRUE, h, atoi(*(argv+2)));
111+
return 0;
110112
}
111113
}
112114
else
@@ -122,6 +124,7 @@ int scm_start_servie(DWORD num, LPWSTR* args) {
122124
ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 300);
123125
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
124126
log_init("ssh-agent", config_log_level(), 1, 0);
125-
return agent_start(FALSE, FALSE, 0, 0);
127+
agent_start(FALSE, FALSE, 0, 0);
128+
return 0;
126129
}
127130

contrib/win32/win32compat/ssh-agent/agent.c

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,19 @@ iocp_work(LPVOID lpParam) {
112112
}
113113

114114

115-
static int
115+
static void
116116
process_connection(HANDLE pipe, int type) {
117117
struct agent_connection* con;
118118

119-
if ((con = malloc(sizeof(struct agent_connection))) == NULL) {
120-
debug("out of memory");
121-
return ERROR_OUTOFMEMORY;
122-
}
119+
if ((con = malloc(sizeof(struct agent_connection))) == NULL)
120+
fatal("failed to alloc");
121+
123122
memset(con, 0, sizeof(struct agent_connection));
124123
con->connection = pipe;
125124
con->type = type;
126-
CreateIoCompletionPort(pipe, ioc_port, (ULONG_PTR)con, 0);
125+
if (CreateIoCompletionPort(pipe, ioc_port, (ULONG_PTR)con, 0) != ioc_port)
126+
fatal("failed to assign pipe to ioc_port");
127+
127128
agent_connection_on_io(con, 0, &con->ol);
128129
return iocp_work(NULL);
129130
}
@@ -153,16 +154,18 @@ agent_listen_loop() {
153154
&listeners[i].sa);
154155

155156
if (listeners[i].pipe == INVALID_HANDLE_VALUE) {
156-
debug("cannot create listener pipe ERROR:%d", GetLastError());
157+
verbose("cannot create listener pipe ERROR:%d", GetLastError());
157158
SetEvent(event_stop_agent);
158159
}
159160
else if (ConnectNamedPipe(listeners[i].pipe, &listeners[i].ol) != FALSE) {
160-
debug("ConnectNamedPipe returned unexpectedly");
161+
verbose("ConnectNamedPipe returned TRUE unexpectedly ");
161162
SetEvent(event_stop_agent);
162163
}
163164

164-
if (GetLastError() == ERROR_PIPE_CONNECTED)
165+
if (GetLastError() == ERROR_PIPE_CONNECTED) {
166+
debug("Client has already connection to %d", i);
165167
SetEvent(listeners[i].ol.hEvent);
168+
}
166169

167170
if (GetLastError() != ERROR_IO_PENDING) {
168171
debug("ConnectNamedPipe failed ERROR: %d", GetLastError());
@@ -183,7 +186,7 @@ agent_listen_loop() {
183186
/* process incoming connection */
184187
HANDLE con = listeners[r - 1].pipe;
185188
listeners[r - 1].pipe = INVALID_HANDLE_VALUE;
186-
189+
verbose("client connected on %ls", pipe_ids[r-1]);
187190
if (debug_mode) {
188191
process_connection(con, listeners[r - 1].type);
189192
agent_cleanup();
@@ -198,25 +201,23 @@ agent_listen_loop() {
198201
si.cb = sizeof(STARTUPINFOW);
199202
memset(&si, 0, sizeof(STARTUPINFOW));
200203
GetModuleFileNameW(NULL, module_path, MAX_PATH);
201-
swprintf_s(path, MAX_PATH, L"%s %d %d", module_path, con, listeners[r - 1].type);
202-
if (CreateProcessW(NULL, path, NULL, NULL, TRUE,
204+
if ((swprintf_s(path, MAX_PATH, L"%s %d %d", module_path, con, listeners[r - 1].type) == -1 ) ||
205+
(CreateProcessW(NULL, path, NULL, NULL, TRUE,
203206
DETACHED_PROCESS, NULL, NULL,
204-
&si, &pi) == FALSE) {
205-
debug("CreateProcess failure: %d", GetLastError());
206-
CloseHandle(con);
207-
agent_cleanup();
208-
return;
207+
&si, &pi) == FALSE)) {
208+
verbose("Failed to create child process %ls ERROR:%d", module_path, GetLastError());
209209
}
210-
211-
CloseHandle(con);
212-
210+
else {
211+
debug("spawned child %d to process %d", pi.dwProcessId, i);
212+
CloseHandle(pi.hProcess);
213+
CloseHandle(pi.hThread);
214+
}
215+
CloseHandle(con);
213216
}
214217

215218
}
216219
else {
217-
debug("wait on events ended with %d ERROR:%d", r, GetLastError());
218-
agent_cleanup();
219-
return;
220+
fatal("wait on events ended with %d ERROR:%d", r, GetLastError());
220221
}
221222

222223
}
@@ -231,33 +232,35 @@ void agent_cleanup_connection(struct agent_connection* con) {
231232
}
232233

233234
void agent_shutdown() {
235+
verbose("shutdown");
234236
SetEvent(event_stop_agent);
235237
}
236238

237-
int agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe, enum agent_type type) {
239+
void
240+
agent_start(BOOL dbg_mode, BOOL child, HANDLE pipe, enum agent_type type) {
238241
int i, r;
239-
HKEY agent_root;
242+
HKEY agent_root = NULL;
240243
DWORD process_id = GetCurrentProcessId();
241244

242-
debug("agent_start pid:%d, dbg:%d, child:%d, pipe:%d", process_id, dbg_mode, child, pipe);
245+
verbose("agent_start pid:%d, dbg:%d, child:%d, pipe:%d", process_id, dbg_mode, child, pipe);
243246
debug_mode = dbg_mode;
244247

245-
if ((ioc_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0)) == NULL) {
246-
debug("cannot create ioc port ERROR:%d", GetLastError());
247-
return GetLastError();
248-
}
248+
if ((ioc_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0)) == NULL)
249+
fatal("cannot create ioc port ERROR:%d", GetLastError());
249250

250251
if (child == FALSE) {
251-
RegCreateKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, 0, 0, KEY_WRITE, 0, &agent_root, 0);
252-
RegSetValueExW(agent_root, L"ProcessID", 0, REG_DWORD, (BYTE*)&process_id, 4);
252+
if ((r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, 0, 0, KEY_WRITE, 0, &agent_root, 0)) != ERROR_SUCCESS)
253+
fatal("cannot create agent root reg key, ERROR:%d", r);
254+
if ((r = RegSetValueExW(agent_root, L"ProcessID", 0, REG_DWORD, (BYTE*)&process_id, 4)) != ERROR_SUCCESS)
255+
fatal("cannot publish agent master process id ERROR:%d", r);
253256
if ((event_stop_agent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
254-
return GetLastError();
257+
fatal("cannot create global stop event ERROR:%d", GetLastError());
255258
if ((r = init_listeners()) != 0)
256-
return r;
259+
fatal("failed to create server pipes ERROR:%d", r);
257260
agent_listen_loop();
258261
}
259-
else {
260-
return process_connection(pipe, type);
262+
else { /* this is a child process that processes one connection */
263+
process_connection(pipe, type);
261264
}
262265

263266
return 0;

contrib/win32/win32compat/ssh-agent/agent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <Windows.h>
22
#include <stdio.h>
3-
#define MAX_MESSAGE_SIZE 5 * 1024
3+
#define MAX_MESSAGE_SIZE 256 * 1024
44

55
#define SSH_ROOT L"SOFTWARE\\SSH"
66
#define SSH_AGENT_ROOT SSH_ROOT L"\\Agent"
@@ -37,7 +37,7 @@ void agent_connection_on_io(struct agent_connection*, DWORD, OVERLAPPED*);
3737
void agent_connection_on_error(struct agent_connection* , DWORD );
3838
void agent_connection_disconnect(struct agent_connection*);
3939

40-
int agent_start(BOOL, BOOL, HANDLE, enum agent_type);
40+
void agent_start(BOOL, BOOL, HANDLE, enum agent_type);
4141
void agent_shutdown();
4242
void agent_cleanup_connection(struct agent_connection*);
4343

contrib/win32/win32compat/ssh-agent/connection.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void agent_connection_on_io(struct agent_connection* con, DWORD bytes, OVERLAPPE
5353
if (con->state == DONE)
5454
DebugBreak();
5555

56-
//while (1)
5756
{
5857
switch (con->state) {
5958
case LISTENING:

0 commit comments

Comments
 (0)