Commit Graph

209 Commits

Author SHA1 Message Date
Jason A. Donenfeld
7c5233a80e api: account for adapter disappearing during deletion
This makes the race less fatal.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 13:11:07 +01:00
Jason A. Donenfeld
3dbaafd4ae api: manipulate process token if thread token didn't require impersonation
Otherwise rundll32.exe fails if we're already SYSTEM.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-07 15:04:33 +01:00
Simon Rozman
c581a9f6cd api: fix Function and Prefix logging order
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-07 15:04:33 +01:00
Simon Rozman
b0ebb6d0ad api: fix LastError override
The LastError was overridden by the stdout reader thread exit code
masking the true reason why ExecuteRunDll32() failed and even worse: as
the thread exited gracefully, the true reason was overridden by
ERROR_SUCCESS and returning TRUE (success).

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-07 15:04:33 +01:00
Jason A. Donenfeld
ed0465b8a7 api: avoid loading version.dll if not used
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-06 10:25:15 +01:00
Jason A. Donenfeld
c891e84259 api: include arm64 in amd64
ARM64 will still run AMD64 apps.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
9f3d466791 api: remove WintunOpenAdapterDeviceObject
Discourage use of kernel interface, which gives us more flexibility if
we ever want to change it.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
82c41bdb4b api: rearrange wintun.h to have better grouping and improve docs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
1285b8f528 api: rename WintunGetAdapter to WintunOpenAdapter
"Create" and "Open" natural names for these.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
e9e790605a api: rename ReceiveRelease to ReleaseReceivePacket
This makes the API parallel:
Wintun*Allocate*SendPacket -> WintunSendPacket
WintunReceivePacket -> Wintun*Release*ReceivePacket

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
8861fdc316 api: add cfgmgr32.dll to delayed load list
It's in the registry but not in the NT object key.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Simon Rozman
abbf658211 api: fix typo in ring-management function prototype declarations
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-05 08:36:28 +01:00
Jason A. Donenfeld
3158b553c4 api: document enum argument properly
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
fc27d8ccd2 api: document log enum
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
65dee05229 api: remove enum name for logger level
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
2b4e164d69 api: document adapter handle return value properly
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 16:19:53 +01:00
Jason A. Donenfeld
c26b16e06f api: CALLBACK_FUNC -> CALLBACK
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 16:46:24 +01:00
Simon Rozman
b8401ad25b api: upgrade nci.lib and wintun-inf.h building
The additional build steps performed are now attached to the build
process using BeforeTargets/AfterTargets.

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 16:44:01 +01:00
Jason A. Donenfeld
dc99f96da7 api: rename GetVersion to GetRunningDriverVersion
This makes our intentions a lot more clear, and in case we ever add
other version functions, makes the forward path simpler.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 14:53:39 +01:00
Jason A. Donenfeld
5d1efa847f api: use a logging alloc function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 13:21:43 +01:00
Jason A. Donenfeld
9c349273f5 api: concatenate function name at runtime
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 13:21:43 +01:00
Simon Rozman
0a51e26730 api: include the rundll32 helpers the MSVC-typical way
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Simon Rozman
552821f59a api: translate NTSTATUS to Win32 error codes
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Simon Rozman
f657e6fd27 api: use GetLastError() to report failures like standard Win32
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Jason A. Donenfeld
677ba8680f wintun: extract inf driverver at compile time into C header
This requires us to make some insane conversions between INF date,
JavaScript time, and finally Windows file time. The point is to mimic
SystemTimeToFileTime, which is what SpInf.dll's pSetupStringToDriverDate
does on the YYYY-MM-DD from the INF.

The result is that we no longer have to parse an ancient text format in
C at runtime.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 18:29:52 +01:00
Jason A. Donenfeld
55345ae386 api: move nci.lib generation to custom step
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 16:10:16 +01:00
Jason A. Donenfeld
90a33d9cc6 api: move undocumented ntdll symbols to ntdll.h
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 15:28:17 +01:00
Jason A. Donenfeld
080b6896e0 api: constify pool argument
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:47:26 +01:00
Jason A. Donenfeld
8963f7258e api: remove guid getter
The iphlpapi takes both LUIDs and GUIDs but prefers LUIDs, so exposing
the NET_LUID makes sense. However, we were previously exposing the
configuration GUID, rather than the net GUID, which is confusing, so
just make it all go away.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:34:19 +01:00
Jason A. Donenfeld
2af7fbd64a api: use 'open' name since caller must close handle
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:31:49 +01:00
Jason A. Donenfeld
7dede73406 api: add pool/driver removal for uninstaller semantics
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:27:42 +01:00
Jason A. Donenfeld
0c85a2ebf1 api: move _L macro where it belongs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 11:42:48 +01:00
Jason A. Donenfeld
04437fd668 api: conditionalize quasi expensive prelinkevent
It would be better to do this only if either nci.def or nci.h are newer
than nci.lib, but who knows if msbuild is expressive enough for this.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 11:17:50 +01:00
Jason A. Donenfeld
64f39cd95d api: only return top two version nibbles
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 11:01:20 +01:00
Simon Rozman
63b99bc2b4 api: bulk-disable unreferenced param warning where it nags the most
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-03 10:58:58 +01:00
Simon Rozman
aa6e540811 api: prevent double SetupDiDestroyDriverInfoList calls on error
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-03 10:40:09 +01:00
Jason A. Donenfeld
19d6227c1d api: rundll32: repair token spawning semantics
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 10:34:25 +01:00
Simon Rozman
a73927ea6c api: refactor .inf parsing and check SystemTimeToFileTime for errors
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-03 10:34:23 +01:00
Simon Rozman
77ff03f621 api: simplify IsOurDrvInfoDetail()
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-03 10:34:23 +01:00
Jason A. Donenfeld
1201c9f346 api: ensure more code compiles by using dead code elimination
It'd be nicer to do this via

if (is_defined(HAVE_WHATEVER))

But MSVC won't work with the linux kernel macros for this. Ongoing
research.

Nevertheless, this makes most of the program always pass through the
compiler's type checker, only to have dead code removed later.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 02:25:57 +01:00
Jason A. Donenfeld
353cfa562e api: begin to separate rundll32 jumps
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 02:25:20 +01:00
Jason A. Donenfeld
9a937c7a49 example: rewrite and replace api's debug rundll32 functionality
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 23:42:43 +01:00
Jason A. Donenfeld
0d7b9c7319 api: free beginning of heap object
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 23:42:43 +01:00
Jason A. Donenfeld
ddacb5491a api: pass around NET_LUID instead of LUID
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 23:33:20 +01:00
Jason A. Donenfeld
f0f18ea4fb api: consistancy in exported interface
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 23:33:20 +01:00
Jason A. Donenfeld
351349b4c4 api: return correct error when there's a duplicate
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 23:33:20 +01:00
Simon Rozman
283f6c8e63 api: install driver on demand
...and add API for uninstalling it.

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-02 23:33:20 +01:00
Jason A. Donenfeld
c20e1683c2 api: separate read-wait handle into other function
Makes the API a bit more clear.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 16:38:56 +01:00
Jason A. Donenfeld
78bad1436e api: statically compile devpkey constants
I don't like this solution, but I don't see much other choice. It's not
in uuid.lib or anywhere like that.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 13:52:47 +01:00
Jason A. Donenfeld
007db8bd94 api: serialize driver installation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 12:07:05 +01:00
Jason A. Donenfeld
724508d61f api: make nci.lib work on x86
This is an appalling hack. We need the lib tool to generate a coff lib
with the "undecorate" parameter. And apparently the only way to do this
with the MSVC tools is by making a stub object, and then rewriting the
symbols using the def file. See Q131313 for Microsoft-sanctioned
details.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 11:38:02 +01:00
Jason A. Donenfeld
7f35f0dbab api: map cr errors to win32 errors
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 12:38:37 +01:00
Simon Rozman
47a241e5d8 api: simplify and unify error messages
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-02 12:19:36 +01:00
Jason A. Donenfeld
13494e979e api: pnf param is none
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 12:19:36 +01:00
Jason A. Donenfeld
b5836e9fb9 api: make version function uniform
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 09:17:47 +01:00
Jason A. Donenfeld
99097022fe api: avoid call to CM_Get_DevNode_Status
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 09:17:47 +01:00
Jason A. Donenfeld
dfa53185d0 api: check for duplicate adapter status
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 09:17:47 +01:00
Jason A. Donenfeld
9adb49e13d api: separate out driver installation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-02 09:17:47 +01:00
Jason A. Donenfeld
334a5e619f api: cleanup names
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-01 00:00:14 +01:00
Simon Rozman
a8f82d5cbf test: introduce a demo client
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-01 00:00:14 +01:00
Jason A. Donenfeld
b65e35569b api: account for nt path to module
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-01 00:00:14 +01:00
Simon Rozman
60ad907b99 api: check buffer overflows in runtime
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:57 +01:00
Jason A. Donenfeld
8edd627f4d api: fix up console writing for debug
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:57 +01:00
Jason A. Donenfeld
378e8eec27 api: clear up argument evaluation order
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:56 +01:00
Jason A. Donenfeld
17bd620a36 api: do not require RebootRequired
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:56 +01:00
Jason A. Donenfeld
ffab1eabe3 api: RebootRequire is an _Out_
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:56 +01:00
Jason A. Donenfeld
6c40f24498 api: add debugging rundll32 entry point
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:56 +01:00
Jason A. Donenfeld
f6d8b694eb api: add missing header for rundll32 mode
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:52 +01:00
Jason A. Donenfeld
26c5478042 api: opencode bin2hex in same manner as random directory creation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:51 +01:00
Jason A. Donenfeld
62f21f2fdd api: link to nci.dll at compile time
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:51 +01:00
Jason A. Donenfeld
937eb44727 api: get rid of pch and make headers sane
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:51 +01:00
Jason A. Donenfeld
c5b05150c6 api: simplify build settings
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:51 +01:00
Jason A. Donenfeld
0faba6c3e8 api: use NT api directly for enumerating kernel modules
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:51 +01:00
Simon Rozman
efbc70635b api expose Send.TailMoved event to clients
This allows clients to use it in WaitForMultipleObjects().

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:50 +01:00
Simon Rozman
c8004933c1 api: rename MAX_POOL to WINTUN_MAX_POOL
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:50 +01:00
Simon Rozman
9957396dc7 api: upgrade source analysis annotations
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:50 +01:00
Simon Rozman
08440580c3 api: refuse to load DLL on init failure
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:50 +01:00
Jason A. Donenfeld
e7a85b7b28 api: attempt to upgrade currently running adapters
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:50 +01:00
Jason A. Donenfeld
f947205cee api: use proper iso atomic semantics
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:50 +01:00
Jason A. Donenfeld
b4a1494fb2 api: rework pool hashing
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:49 +01:00
Jason A. Donenfeld
0a8bf9d1ff api: only force close handles if requested
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:49 +01:00
Jason A. Donenfeld
a332f54a1b api: only sleep after force closing handles if required
Also force close handles when deleting the adapter, in case the function
is called from another process, for example an uninstaller.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:49 +01:00
Simon Rozman
1b3af95be3 api: selectively use temporary variable to prepare output
Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 19:11:49 +01:00
Jason A. Donenfeld
8c935ce151 api: remove security attributes debug trap door
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 10:41:49 +01:00
Jason A. Donenfeld
7964694e1e api: elevate only when needed for system operations
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 10:41:49 +01:00
Simon Rozman
779d0e0c38 api: simplify driver selection by always including EV driver
When Windows 7-8.1 support is sunset, there will be no more EV driver
deployment at all.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:49 +01:00
Jason A. Donenfeld
c928ad4de7 api: fix dll hijacking vulns
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 10:41:48 +01:00
Simon Rozman
abf6962144 api: simplify driver removal
When we install the Wintun driver to the store, we get exact oem<nn>.inf
filename of the driver in the store we just installed. Since the
installation should be only temporarily, we should uninstall only the
driver we installed.

This also eliminates the need for iterating driver store speeding up
things.

The code we removed was inherited from the installer.dll, where it made
perfect sense to remove all installed Wintun drivers in the update
process.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
eeb42a5f12 api: make #ifdef-s more descriptive
Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
4b0470210d api: cleanup driver selection logic
With all the platforms (including ARM) having at least one driver now,
the dead-code removal #ifdef-s are no longer required.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
77459a130a Add ARM driver compilation
This adds the ARM driver to the list of Wintun drivers we compile for
future deployment. Since we're not in position to test it in the real
world, any feedback is greatly appreciated.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
44cfac5d63 api: add a cleanup note when Windows 7 support is discontinued
DriverMajorVersion and DriverMinorVersion registry values were
introduced in Windows 8.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
5a3a217b0b api: split adapter creation/deletion to delegator and worker functions
Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:48 +01:00
Simon Rozman
3dacd1c6ce api: make pipe handles non-inheritable by default
...and change to inheritable only the ones really needed, eliminating a
window where we'd have inheritable handles that are not supposed to be
inheritable.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
fb6d5b62f1 api: check the stdout reader thread exit status for failures
...and refactor the ExecuteRunDll32().

Reported-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
44568f81cb api: make architecture-dependent wintun.dll selection an explicit select
Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
8b0409ce2a api: fix adapter name numbering termination
Reported-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
254a900a76 api: bail out on _TRUNCATE truncation
Silently ignoring truncation of the strings(like adapter and pool names,
registry paths etc.) leads to strange failures later down the road (like
registry key not found) masking the true reason of the failure. This
makes troubleshooting difficult.

Reported-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
13e90b52cc api: simplify RemoveNumberedSuffix()
Reported-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00
Simon Rozman
f389fd5629 api: use C locale for adapter and pool name case-insensitive comparing
Reported-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-31 10:41:47 +01:00