Commit f1db8976 authored by Paul Gofman's avatar Paul Gofman Committed by Alexandre Julliard

kernelbase: Don't inherit all the handles in conhost.exe.

parent 66dfe504
...@@ -393,10 +393,12 @@ BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid ) ...@@ -393,10 +393,12 @@ BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid )
static BOOL alloc_console( BOOL headless ) static BOOL alloc_console( BOOL headless )
{ {
SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE }; SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE };
STARTUPINFOW app_si, console_si; STARTUPINFOEXW console_si;
STARTUPINFOW app_si;
HANDLE server, console = NULL; HANDLE server, console = NULL;
WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH]; WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH];
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
SIZE_T size;
void *redir; void *redir;
BOOL ret; BOOL ret;
...@@ -412,42 +414,50 @@ static BOOL alloc_console( BOOL headless ) ...@@ -412,42 +414,50 @@ static BOOL alloc_console( BOOL headless )
return FALSE; return FALSE;
} }
memset( &console_si, 0, sizeof(console_si) );
console_si.StartupInfo.cb = sizeof(console_si);
InitializeProcThreadAttributeList( NULL, 1, 0, &size );
if (!(console_si.lpAttributeList = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
InitializeProcThreadAttributeList( console_si.lpAttributeList, 1, 0, &size );
if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error; if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error;
GetStartupInfoW(&app_si); GetStartupInfoW(&app_si);
memset(&console_si, 0, sizeof(console_si));
console_si.cb = sizeof(console_si);
/* setup a view arguments for conhost (it'll use them as default values) */ /* setup a view arguments for conhost (it'll use them as default values) */
if (app_si.dwFlags & STARTF_USECOUNTCHARS) if (app_si.dwFlags & STARTF_USECOUNTCHARS)
{ {
console_si.dwFlags |= STARTF_USECOUNTCHARS; console_si.StartupInfo.dwFlags |= STARTF_USECOUNTCHARS;
console_si.dwXCountChars = app_si.dwXCountChars; console_si.StartupInfo.dwXCountChars = app_si.dwXCountChars;
console_si.dwYCountChars = app_si.dwYCountChars; console_si.StartupInfo.dwYCountChars = app_si.dwYCountChars;
} }
if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE) if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE)
{ {
console_si.dwFlags |= STARTF_USEFILLATTRIBUTE; console_si.StartupInfo.dwFlags |= STARTF_USEFILLATTRIBUTE;
console_si.dwFillAttribute = app_si.dwFillAttribute; console_si.StartupInfo.dwFillAttribute = app_si.dwFillAttribute;
} }
if (app_si.dwFlags & STARTF_USESHOWWINDOW) if (app_si.dwFlags & STARTF_USESHOWWINDOW)
{ {
console_si.dwFlags |= STARTF_USESHOWWINDOW; console_si.StartupInfo.dwFlags |= STARTF_USESHOWWINDOW;
console_si.wShowWindow = app_si.wShowWindow; console_si.StartupInfo.wShowWindow = app_si.wShowWindow;
} }
if (app_si.lpTitle) if (app_si.lpTitle)
console_si.lpTitle = app_si.lpTitle; console_si.StartupInfo.lpTitle = app_si.lpTitle;
else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer))) else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer)))
{ {
buffer[ARRAY_SIZE(buffer) - 1] = 0; buffer[ARRAY_SIZE(buffer) - 1] = 0;
console_si.lpTitle = buffer; console_si.StartupInfo.lpTitle = buffer;
} }
UpdateProcThreadAttribute( console_si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
&server, sizeof(server), NULL, NULL );
swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir ); swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server )); swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server ));
if (headless) wcscat( cmd, L" --headless" ); if (headless) wcscat( cmd, L" --headless" );
Wow64DisableWow64FsRedirection( &redir ); Wow64DisableWow64FsRedirection( &redir );
ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &console_si, &pi ); ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | EXTENDED_STARTUPINFO_PRESENT,
NULL, NULL, &console_si.StartupInfo, &pi );
Wow64RevertWow64FsRedirection( redir ); Wow64RevertWow64FsRedirection( redir );
if (!ret || !create_console_connection( console)) goto error; if (!ret || !create_console_connection( console)) goto error;
...@@ -456,6 +466,7 @@ static BOOL alloc_console( BOOL headless ) ...@@ -456,6 +466,7 @@ static BOOL alloc_console( BOOL headless )
RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console; RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console;
TRACE( "Started conhost pid=%08lx tid=%08lx\n", pi.dwProcessId, pi.dwThreadId ); TRACE( "Started conhost pid=%08lx tid=%08lx\n", pi.dwProcessId, pi.dwThreadId );
HeapFree( GetProcessHeap(), 0, console_si.lpAttributeList );
CloseHandle( server ); CloseHandle( server );
RtlLeaveCriticalSection( &console_section ); RtlLeaveCriticalSection( &console_section );
SetLastError( ERROR_SUCCESS ); SetLastError( ERROR_SUCCESS );
...@@ -463,6 +474,7 @@ static BOOL alloc_console( BOOL headless ) ...@@ -463,6 +474,7 @@ static BOOL alloc_console( BOOL headless )
error: error:
ERR("Can't allocate console\n"); ERR("Can't allocate console\n");
HeapFree( GetProcessHeap(), 0, console_si.lpAttributeList );
NtClose( console ); NtClose( console );
NtClose( server ); NtClose( server );
FreeConsole(); FreeConsole();
...@@ -2175,9 +2187,12 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA ...@@ -2175,9 +2187,12 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA
DWORD flags, HANDLE *process ) DWORD flags, HANDLE *process )
{ {
WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH]; WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH];
unsigned int inherit_count;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
HANDLE server, console; HANDLE server, console;
HANDLE inherit[2];
STARTUPINFOEXW si; STARTUPINFOEXW si;
SIZE_T attr_size;
void *redir; void *redir;
BOOL res; BOOL res;
...@@ -2196,6 +2211,21 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA ...@@ -2196,6 +2211,21 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA
si.StartupInfo.hStdOutput = output; si.StartupInfo.hStdOutput = output;
si.StartupInfo.hStdError = output; si.StartupInfo.hStdError = output;
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES; si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
inherit[0] = server;
inherit[1] = signal;
inherit_count = signal ? 2 : 1;
InitializeProcThreadAttributeList( NULL, inherit_count, 0, &attr_size );
if (!(si.lpAttributeList = HeapAlloc( GetProcessHeap(), 0, attr_size )))
{
NtClose( console );
NtClose( server );
return FALSE;
}
InitializeProcThreadAttributeList( si.lpAttributeList, inherit_count, 0, &attr_size );
UpdateProcThreadAttribute( si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
inherit, sizeof(*inherit) * inherit_count, NULL, NULL );
swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir ); swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
if (signal) if (signal)
{ {
...@@ -2210,8 +2240,9 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA ...@@ -2210,8 +2240,9 @@ static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HA
conhost_path, size.X, size.Y, server ); conhost_path, size.X, size.Y, server );
} }
Wow64DisableWow64FsRedirection( &redir ); Wow64DisableWow64FsRedirection( &redir );
res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS | EXTENDED_STARTUPINFO_PRESENT,
&si.StartupInfo, &pi ); NULL, NULL, &si.StartupInfo, &pi );
HeapFree( GetProcessHeap(), 0, si.lpAttributeList );
Wow64RevertWow64FsRedirection( redir ); Wow64RevertWow64FsRedirection( redir );
NtClose( server ); NtClose( server );
if (!res) if (!res)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment