Commit Graph

578 Commits

Author SHA1 Message Date
Ryan Prichard
5a65fdc11c Use more sensible executable flags for source files
My motivation at the moment is that I'm trying to share a
git checkout between multiple VMs using VirtualBox's Shared
Folders feature.  git in the guest VM isn't able to see the
executable bits from the host due to the VirtualBox/SMB/CIFS
layer.  Instead, it thinks text files are non-executable,
unless they have a shebang line.  That's a sensible way to
set the flags anyway, so set them like that.

With this commit, there's still one file that isn't handled:
src/shared/GetCommitHash.cmd.  It's still marked executable,
but it lacks a shebang line, so the guest thinks it's
non-executable.  I'm not sure it should be changed.
2016-04-20 21:44:44 -07:00
Ryan Prichard
2e661f15fd Add a test case exploring freeze behavior on an inactive console buffer
So far, I've only tested it on Windows 7.  The test passed on that OS:
creating a screen buffer isn't blocked by selection, but writing to an
inactive buffer *is* blocked, regardless of whether the buffer was created
before or after selection began.  The use of Mark or SelectAll doesn't
affect behavior.
2016-04-19 15:00:39 -07:00
Ryan Prichard
71795edf77 Update the release notes. 2016-04-10 18:14:21 -05:00
Ryan Prichard
6fba69d602 Also recognize ERROR_RESOURCE_DATA_NOT_FOUND for ModuleNotFound. 2016-04-10 17:52:59 -05:00
Ryan Prichard
8a769b19f5 Avoid infinite loop in readFontTable when ConEmu has adopted the console
If we're only examining the table for debugging purposes, then read only
first 1000 fonts and dump them.

If we're reading the font table on XP, then use the undocumented
GetNumberOfConsoleFonts API.  Avoid this API on Vista and up, because it's
undocumented.  It ought to be stable enough on XP.

When ConEmu has adopted the agent console (e.g. there is a ConEmu tab for
the agent's console), then every font in the table will have the same size.
(For now, anyway.  Obviously, the ConEmu author may decide to change the
behavior.)

When `WINPTY_SHOW_CONSOLE=1` is set, and when ConEmu's "Process 'start'"
flag is set, ConEmu will still adopt the winpty-agent console.  It seems
that this case is still broken (on Vista and up), because winpty.dll
notices that its pipe clients have the wrong PID.  I'm not sure what to do
about this.  `WINPTY_SHOW_CONSOLE=1` is strictly intended for debugging
winpty, though, so there's no useful need for ConEmu to adopt the console.
There's no way for me to opt out.  I could disable the security check when
I detect ConEmu, but that's hazardous, and I'm not a fan of the API hooking
anyway.

winpty prints a content-free error, "Error creating winpty." in the above
case.  The error report could be improved to include the traced() error,
"Security check failed: pipe client pid (7492) does not match agent pid (7416)".

Second part of a fix for
https://github.com/rprichard/winpty/issues/70
2016-04-10 17:35:53 -05:00
Ryan Prichard
edc979225b Specify SW_HIDE and avoid creating a background desktop for Win7 and up
The SW_HIDE change fixes part of a ConEmu<->winpty incompatibility.  If
WINPTY_SHOW_CONSOLE=1 is set, and ConEmu's "start cmd" flag is on, then
readFontTable can still run forever.

Avoiding a background desktop would fix a ConEmu<->winpty incompatibility
if not for the previous fix.  It fixes a clipboard issue and
thread-unsafety issue, but only on Win7 and up.  (More work is needed for
XP/Vista.)

See https://github.com/rprichard/winpty/issues/58.
See https://github.com/rprichard/winpty/issues/70.
2016-04-10 17:08:37 -05:00
Ryan Prichard
ef339dcdd1 Short-circuit hasDebugFlag to make it a bit faster in the common case 2016-04-10 17:02:32 -05:00
Ryan Prichard
3e75255e6b Trace winpty version info, move agent trace output as early as possible 2016-04-10 16:22:32 -05:00
Ryan Prichard
c677b8dd0a Dump Windows version and arch information (including ConEmu hook version) 2016-04-10 16:06:56 -05:00
Ryan Prichard
f647952a34 Forbid copy-assignment on WakeupFd and Event. 2016-04-07 18:05:34 -05:00
Ryan Prichard
25076e51a7 Call Unicode versions of Windows APIs explicitly 2016-04-06 23:34:58 -05:00
Ryan Prichard
cf380b39bc Output a line each time a directory is created.
This might be helpful in diagnosing the directory creation logic in the
Makefile, which is obscure.

*Might* be related to https://github.com/rprichard/winpty/issues/71
2016-04-06 18:53:31 -05:00
Ryan Prichard
94da56e041 Amend the README.md: obviously people want to use make install!
When I initially wrote this section, there was no `install` target at all,
and I just ran `build/console.exe` right out of my checkout.  That's
actually very inconvenient, though, for day-to-day use.
2016-04-05 02:50:52 -05:00
Ryan Prichard
d984413c0a Rename the UNIX adapter from console.exe to winpty.exe.
There are three reasons for this change:

 * It's consistent with MSYS2, which already renamed console.exe to
   winpty.exe.

 * winpty.exe is less likely to clash with another program.

 * winpty.exe is easier to search for online.  If someone unfamiliar with
   winpty is asked to run console.exe, they might have a hard time figuring
   out where console.exe comes from.

The old behavior can be restored by passing UNIX_ADAPTER_EXE=console.exe
to `make`, or by simply renaming the binary after-the-fact, or with
`alias`, etc.
2016-04-05 02:43:13 -05:00
Ryan Prichard
bb23f63b34 Install dev files (importlib, headers) and docs, localize PHONY deps 2016-04-05 02:43:12 -05:00
Ryan Prichard
cc8577e3fc Update the changelog 2016-04-04 06:41:42 -05:00
Ryan Prichard
3733682d2f Fixes to the gyp files.
* Harmonize the subsystem of every binary to CONSOLE.  It apparently
   doesn't matter for DLLs, and it's easier if they're all the same.  I
   examined various DLLs on my system, and they're very inconsistent.
   (e.g. kernel32.dll is CONSOLE, but shell32.dll is GUI.)

 * When using a recent XP-targeting toolset (v120_xp or v140_xp), it's
   necessary to explicitly specify the subsystem, so list it in
   configurations.gypi.

 * node.js changed its common.gypi to disable exception handling[1], so
   explicitly turn it back on in winpty.gyp.  Placing the setting in
   winpty.gyp's 'target_defaults' block doesn't seem to override a setting
   in an included file's 'target_defaults' block, so instead enable EH on
   each target.

 * For consistency with other gyp files I see, use quoted integers instead
   of bare integers for settings.  Affects the 'RuntimeLibrary' setting.

 * Provide a documented way in configurations.gypi for setting the
   XP-specific toolsets.

[1] da9eff80a3
2016-04-04 06:22:45 -05:00
Ryan Prichard
d2dd590bbf MSVC SDL compliance: remove sprintf uses from DefaultInputMap.cc
Also: Use ASSERT to ensure that the write pointer is always within range.
2016-04-04 06:22:44 -05:00
Ryan Prichard
441228eaa3 Consolidate GetVersionEx calls and silence the MSVC deprecation warning
MSVC complained that GetVersionEx was deprecated when the /SDL checks were
enabled.

Comment added to WindowsVersion.cc:

Allow use of deprecated functions (i.e. GetVersionEx).  We need to use
GetVersionEx for the old MinGW toolchain and with MSVC when it targets XP.
Having two code paths makes code harder to test, and it's not obvious how
to detect the presence of a new enough SDK.  (Including ntverp.h and
examining VER_PRODUCTBUILD apparently works, but even then, MinGW-w64 and
MSVC seem to use different version numbers.)
2016-04-04 06:22:43 -05:00
Ryan Prichard
ab5f08dfff MSVC SDL compliance: avoid calls to strcpy and wcsncpy 2016-04-04 06:22:43 -05:00
Ryan Prichard
6bea21d48c Use GetCommandLineW/CommandLineToArgvW for winpty-agent.exe's command line
This change avoids the unnecessary round-trip to the ANSI (or OEM?) code
page, and it also allows removing the mbstowcs calls, which simplifies the
code and fixes MSVC /SDL compliance.
2016-04-04 06:22:42 -05:00
Ryan Prichard
3d466694e0 MSVC SDL compliance: Use __pragma(disable:4146) to allow unsigned negation 2016-04-04 06:22:41 -05:00
Ryan Prichard
1bb530f0a1 Specify SECURITY_IDENTIFICATION for the agent->libwinpty pipe too.
This change seems less interesting than the change specifying the flag in
DebugClient.cc.
2016-04-04 06:22:41 -05:00
Ryan Prichard
a9661c8f52 Improve trace() security: restrict debugserver impersonation
In certain situations (e.g. when it has the SeImpersonatePrivilege
privilege), a named pipe server can impersonate a named pipe client and
execute in its security context (e.g. open the client user's files).
There's no reason for a winpty debugserver to need this ability, so
restrict it in the client.

It seems unlikely to me that this commit fixes a genuine security issue,
but it also seems like a decent precaution to take.  Clients using winpty
don't connect to the DebugServer named pipe unless the WINPTY_DEBUG
environment variable is set, and Windows restricts the ability to
impersonate named clients (at least as of XP SP2).
2016-04-04 06:22:35 -05:00
Ryan Prichard
977019785c Pass USE_PCH=0 to both "make all tests" and "make install"
If we don't, then "make install" recompiles the project.
2016-04-03 18:35:57 -05:00
Ryan Prichard
a22f493982 Update README to mention MSVC/gyp and WinXP support 2016-03-30 03:20:55 -05:00
Ryan Prichard
0711d64cce Update RELEASES.md. 2016-03-30 02:59:45 -05:00
Ryan Prichard
61029c3e4d Convert README.rst to README.md. 2016-03-30 02:59:32 -05:00
Ryan Prichard
de9f54da29 Fix https://github.com/rprichard/winpty/issues/67
The str array need an extra element for the NUL terminator.  The previous
code didn't suffer from memory-unsafety, but winpty_snprintf truncated the
last character of the marker, and trying to write NUL to the WinXP console
didn't work.  (I suspect WinXP turned the NUL into a space, so
findSyncMarker couldn't find it later.)
2016-03-30 02:13:02 -05:00
Ryan Prichard
44848914fc winpty-debugserver: add an --everyone flag to let other users log messages
* In general, harmonize the debugserver named pipe with the other named
   pipe instances: don't let remote users log messages and fail if the
   pipe already exists.
2016-03-30 02:06:06 -05:00
Ryan Prichard
825483fa99 Add a GetNamedPipeClientProcessId security check on supported OSs (Vista+)
Also:

 * Removed some obsoleted comments.

 * Factor out the Handle class.  Rename to OwnedHandle.  Merge some
   improvements from libwinpty-rewrite, including fixing move-assignment
   so that it closes the existing handle, if one exists.
2016-03-29 22:52:01 -05:00
Ryan Prichard
383138a0cc Improve named pipe security
* Set the PIPE_REJECT_REMOTE_CLIENTS flag on Vista and up.

 * Set a DACL on named pipes that gives full control to the built-in
   administrators, the local system account, and the current security
   token's owner SID.  This is the same DACL that is used by default,
   except that the default also grants read access to the Everyone group
   and the anonymous account.

 * The createSecurityDescriptorOwnerFullControlEveryoneWrite function is
   not currently used (or tested), but I think I'll use it in the debug
   server to allow collecting trace output from other accounts on the
   machine.  (I think I'll make that behavior optional.)
2016-03-29 21:41:21 -05:00
Ryan Prichard
63fa213594 Fix WinptyException: make the dtor virtual and mark what() noexcept. 2016-03-29 21:41:15 -05:00
Ryan Prichard
8a899bee05 uniqueName: Increase the random string from 12 bytes to 16 bytes 2016-03-29 21:09:36 -05:00
Ryan Prichard
636a14eb55 Remove the unused ReadBuffer::getRawInt32() function. 2016-03-29 21:09:35 -05:00
Ryan Prichard
0c6a0c2a53 Forbid outputting the wrong kind of character type to [W]StringBuilder.
Previously, outputting the wrong character type tended to call one of the
other overloads, such as the int overload.
2016-03-25 04:02:09 -05:00
Ryan Prichard
1a590ce04c Strengthen unique pipe name generation.
* PIDs are recycled, so include the system time.

 * A program running on the same machine could predict the names of pipes
   winpty uses and block winpty, causing a denial-of-service.  I'm not sure
   this is really a concern, but I suppose in principle it is?  Try to
   guard against it by appending random bytes to the pipe name.  The
   CreatePrivateNamespace API looks relevant, but I'm not sure it applies
   to named pipes, and it's Vista only.
2016-03-25 04:02:08 -05:00
Ryan Prichard
87cfa4b83e Refactor mutable CreateProcess argument workarounds
* Rename modifiableWString to vectorWithNulFromString.

 * Create a vectorFromString that omits the NUL terminator.

 * Stop triply-terminated non-empty environment blocks.

 * Stop adding an extra NUL terminator to environment blocks in
   handleStartProcessPacket.  (env.data() didn't necessarily add a
   terminator in C++03, but it does in C++11.)
2016-03-24 22:41:23 -05:00
Ryan Prichard
7baffb85b8 Consistently convert UTF-16 to UTF-8 for trace output.
* Previously, most places in the codebase used "%ls", which relies on the
   CRT to do a wide-to-mbcs conversion, and I think(?) that uses locale
   settings?

 * Do the conversion explicitly using narrowString, which is renamed to
   utf8FromWide and moved to a new file, src/shared/StringUtil.cc.
2016-03-24 22:19:26 -05:00
Ryan Prichard
c2756d8a79 Move the setlocale call to the very top.
This change shouldn't affect the behavior at all, because the code in
between does not do MB<->WC string conversion.
2016-03-24 22:19:25 -05:00
Ryan Prichard
4dddde3562 Switch from std::[w]stringstream/std::cout to [W]StringBuilder/fwrite
Remove all references to the iostream and sstream headers from code in the
src directory.

This change reduces the sizes of winpty.dll and winpty-agent.exe.  For
example:

Cygwin's x86_64-w64-mingw32-g++ 4.9.2 compiler:
 - winpty.dll:       622 KiB to 513 KiB
 - winpty-agent.exe: 742 KiB to 572 KiB

MSVC 2015, using gyp + configurations.gypi targeting x64:
 - winpty.dll:       985 KiB to 545 KiB
 - winpty-agent.exe: 1108 KiB to 642 KiB
2016-03-24 22:19:25 -05:00
Ryan Prichard
ba63e95af2 Add a StringBuilder class that resembles an efficient std::stringstream 2016-03-24 22:19:24 -05:00
Ryan Prichard
9839b8581a Improvements to {Read,Write}Buffer classes and NamedPipe
* Avoid std::stringstream because it's inefficient/bloated.

 * Explicitly handle decode errors, by throwing ReadBuffer::DecodeError.
   For the time being, this exception can only be thrown in the agent, but
   I anticipate it being thrown in the libwinpty code eventually.

 * Catch encoding errors a bit better.

 * Be more precise about the integer types used for length.  e.g.
   Use 64 bits on the wire, but otherwise use size_t/uint64_t where
   appropriate.

 * Replace memcpy with std::copy to make MSVC's /SDL security checks happy.
   It's a somewhat silly/arbitrary change.  I don't think it will affect
   performance, but I haven't benchmarked it.

 * This commit changes the RPC protocol that libwinpty uses to communicate
   with winpty-agent.
2016-03-24 22:19:23 -05:00
Ryan Prichard
51893bad84 Move the toString functions into Coord.h and SmallRect.h.
I don't think these functions are actually called anywhere -- they only
exist for debugging.  Inlining them will probably reduce build times.
2016-03-02 02:56:39 -06:00
Ryan Prichard
6c7eac63b8 Replace c99_[v]snprintf with winpty_[v]snprintf and improve trace() checks
The existing snprintf functions were not C99-conformant:

 - The _vsnprintf* functions do not accept all of the C99 specifiers,
   like %zu for size_t or (on WinXP) %lld for long long.

 - MinGW always provides vsnprintf, but the implementation sometimes just
   calls MSVCRT's _vsnprintf*.  In that case, it won't handle all C99
   specifiers and the return value won't be conformant.

Changes:

 - winpty_snprintf accepts C90(?) specifiers, returns -1 on error
   (including truncation) and should work with the inttypes.h printf
   macros.

 - Enable __attribute__((printf)) on the winpty_[v]snprintf functions and
   also on trace().  Fix a HANDLE(void*) <-> unsigned int mismatch in
   NamedPipe.cc.

 - Switch most sprintf() call sites to winpty_snprintf for improved safety
   guarantees.  (This change gets the code closer to compiling with MSVC's
   /sdl flag, which forbids "unsafe" memory/string functions.)
2016-03-02 02:56:38 -06:00
Ryan Prichard
adc1a5adbf Use precompiled headers when compiling with GCC.
The unix-adapter doesn't use PCH, because MSYS1's compiler doesn't support
them.
2016-02-29 04:33:42 -06:00
Ryan Prichard
6c65edb0e4 Make WinptyAssert.h behave more sensibly, both inside and outside the agent
* In agent: if the console is frozen, then calling abort hangs forever,
   because abort tries to write to stderr, which blocks.  Instead,
   use WM_CLOSE to close the console window.  If that doesn't work after
   waiting a while, then exit.

   We need to do the same thing doing normal agent shutdown, so factor out
   an agentShutdown function.  It's a bit unhappy living in
   WinptyAssert.cc, but it works.

 * Outside agent: trace an assertion failure message, then delegate to the
   ordinary assert() function.  If that's somehow turned off, then call
   abort().
2016-02-29 04:16:05 -06:00
Ryan Prichard
fd270915c1 Compile each target's source files separately from other targets.
The gyp file already worked this way, but now the Makefile does too.

The motivation is to let the shared source files have different error
handling behavior (e.g. assert/ASSERT/stderr-print/abort/exception) in
winpty.dll and the other targets.
2016-02-29 04:16:04 -06:00
Ryan Prichard
bce511725a Add a clean-msvs target for cleaning up after gyp and VisualStudio/msbuild 2016-02-29 04:16:03 -06:00
Ryan Prichard
e390c8775c Try to fix an issue where make fails if a header file disappears
Currently, when a header file is removed (or renamed), there is a stale
dependency file still referring to the header, and GNU make aborts, because
the header is missing.  Attempt to work around this by adding a pattern
rule to generate any header in `src/`.  The rule prints a warning message.

The C source file whose header was missing will be recompiled because a
dependency was rebuilt.  Once it is, the removed header should be removed
from the dependency file.

(Aside: FWIW, the "ninja" build tool knows directly about depfiles, and it
does not have this problem.)
2016-02-29 02:58:30 -06:00