Commit Graph

549 Commits

Author SHA1 Message Date
Jason A. Donenfeld
273cbe2d14 driver: format
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-06-25 13:51:57 +02:00
Jason A. Donenfeld
479bbdc50a api: only mark GUID as in-use if Status != NotPresent
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-11 19:43:34 +02:00
Jason A. Donenfeld
0d96b900c3 version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 19:23:28 +02:00
Jason A. Donenfeld
6cf9ac71c3 driver: do not assume aligned addresses when allocating MDLs
IoAllocateMdl allocates a different size structure depending on the
bottom in-page bits of the address. By passing null, it assumes that the
address is aligned within the page, which it might not be. Fix this by
passing the eventual virtual address to the allocation function so that
the right amount is always allocated.

Reported-by: Oleksandr Muzychuk <oleksandr.muzychuk@apriorit.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 19:02:49 +02:00
Simon Rozman
f19945b3c6 driver: move init-only functions into INIT segment
Reference: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/writing-a-driverentry-routine
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-05-10 11:24:44 +02:00
Simon Rozman
71e1ab6940 driver: fix memory leak on pre-Windows 7
Should NDIS version check fail the DriverEntry() bailed out without
releasing the TunDispatchSecurityDescriptor.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-05-10 11:24:44 +02:00
Simon Rozman
b28a721d9e driver: cleanup unused DBG define
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-05-10 11:24:44 +02:00
Jason A. Donenfeld
02f19b8c90 version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 11:24:44 +02:00
Jason A. Donenfeld
3c8b92e80e api: use simpler problem status checking
This reworks commit e51b49604b.

Link: https://www.reddit.com/r/WireGuard/comments/n6yocf/unable_to_create_wintun_on_windows_7_laptop_with/
Reported-by: Alirz
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 11:23:58 +02:00
Jason A. Donenfeld
1efbd14c2c api: check that GUID is valid before assuming it's in use
ROOT/NET/000X could have been claimed by a different driver, so we want
to double check.

Link: https://lists.zx2c4.com/pipermail/wireguard/2021-May/006716.html
Reported-by: Piotr Sobczak <piotrs@glosol.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 11:23:58 +02:00
Jason A. Donenfeld
d9555bea1b api: discourage UaF on teardown
While it does make sense to make readers unblock by setting the read
event on teardown, this is something that consumers of the library
should do _before_ calling EndSession, not something that makes sense
for the library to do itself. The reason is that, in the hypothetical
case in which this makes sense, immediately after unblocking the reader
via SetEvent, the function goes on to free all of the memory that that
reader might want to use. So, rather, the proper shutdown flow is from
the application side, and looks like:

    Closing = true;
    SetEvent(WintunGetReadWaitEvent());
    WaitForReadersToReturn();
    WintunEndSession();

Alternatively, rather than using WaitForSingleObject on the read event,
consumers can WaitForMultipleObjects and include a shutdown event, which
is what the example code does.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-10 11:23:58 +02:00
Jason A. Donenfeld
d2db3b9977 version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-05 11:25:44 +02:00
Jason A. Donenfeld
0c9a87b8a2 api: skip requested GUID if !win10
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-05 11:25:44 +02:00
Jason A. Donenfeld
747ba7121d api: clean up NetSetup2 GUIDs
Recent versions of Windows fail to tidy up, causing issues when reusing
GUIDs. Check to see if a GUID might be orphaned, and forcibly clear out
the registry state if so.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-05 11:17:39 +02:00
Jason A. Donenfeld
4480d32011 api: don't pass bogus previous buffer size argument
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-05 10:54:33 +02:00
Jason A. Donenfeld
e51b49604b api: don't return ERROR_SUCCESS if adapter doesn't come up
Otherwise we fail to remove the zombie adapter, and then the problem
repeats itself. Note that this is part of a larger issue of clearing out
bad GUIDs in NetSetup2 on recent Windows builds

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-05-04 12:20:43 +02:00
Simon Rozman
5e48e4196e project: fix typo in .editorconfig
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-05-04 12:20:35 +02:00
Jason A. Donenfeld
5edd8d2517 version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-04-13 15:55:08 -06:00
Simon Rozman
51a4f299c3 vs: put .pdb files in the intermediate folders
Wondering, why WinDbg is refusing to load symbols for wintun.sys
recently...

By default, building puts .pdb files to the output folder. Next to the
final binaries: wintun.sys, wintun.dll, example.exe... Wait?! But the
wintun.pdb from wintun.dll overwrites the wintun.pdb from wintun.sys
then.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-04-13 15:50:50 -06:00
Simon Rozman
227b76480c vs: remove api->wintun project dependency
The time stamping with Inf2Cat never makes the wintun output really "up-
to-date". This keeps triggering wintun project rebuilds over and over
again. Including all the projects depending on wintun.

Thou, the api project really depends on the driver built by wintun
project, those needless driver rebuilds are utterly annoying.

To be correct, the amd64 api project also depends on the arm64 api
project - a dependency which cannot be described using .sln. So, one way
or another, a developer must build projects inside the .sln in a
specific order.

Another solution would be to split the solution file (pun intended). One
.sln for driver, another for the api and example projects. Then open the
one the developer is focused on.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-04-13 15:50:50 -06:00
Simon Rozman
d35312d33f api: log Windows error message too when creating folder or file fails
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-04-13 15:50:50 -06:00
Simon Rozman
3a8dbb6ae2 api: fix fallback log line printf template
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-04-10 13:37:22 +02:00
Simon Rozman
2f1d1ab827 Fix © in resources
The \xa9 is © on Windows-125x code pages. When Wintun was compiled on a
Windows computer using UTF-8 as default code page (for "non-Unicode"
programs), the Copyright notice in resources was wrong.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-03-19 12:04:23 +01:00
Simon Rozman
a6fa9c1b67 project: add support for intermediate versioning
While the Wintun driver is typically released only at <major>.<minor>
milestones, the wintun.dll did see some intermediate releases.

To make MSI logic correctly decide to upgrade local wintun.dll file, the
version inscribed in wintun.dll file resources should increment in those
intermediate releases. MSI ignores file timestamps. One could use
REINSTALLMODE=emus Installer property to force copying wintun.dll when
its version doesn't change. But REINSTALLMODE applies to all files in
the MSI session and would be an additional requirement when authoring
MSI packages with wintun.dll.

Bumping only the final ZIP filename version is not sufficient.

Therefore, a <major>.<minor> or <major>.<minor>.<build> versioning is
introduced. Furthermore, we no longer distinguish between WintunVersion
and WintunVersionStr. All our releases used strictly numeric
<major>.<minor> notation, making WintunVersion and WintunVersionStr
always the same.

When the driver didn't change, just bump the version in wintun.proj and
run `msbuild wintun.proj /t:Zip` to rebuild the wintun.dll and make the
new ZIP file.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-03-19 11:52:11 +01:00
Simon Rozman
1ccd623e94 api: make .h filenames lowercase for building with MinGW on Linux
MinGW supplies all Windows header files using lowercase filenames.
This makes some of the #include lines in wintun.h fail to resolve the
.h files correctly on a case-sensitive filesystem.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-03-16 10:11:00 +01:00
Simon Rozman
cef7922556 api: elevate to SYSTEM in WintunEnumAdapters()
The WintunEnumAdapters() requires namespace mutex. However,
NamespaceTakePoolMutex() works as SYSTEM user only.

WireGuard is using the WintunEnumAdapters() in its manager service to
cleanup stale adapters. As the WireGuard manager service is running as
SYSTEM, this requirement was not apparent before.

This commit also extends the example project to list its existing
adapters at start.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-03-08 13:48:29 +01:00
Simon Rozman
bc5cde8916 api: upgrade logging
Log runtime information to quickly check whether the values are sane
when analyzing error logs sent in by users.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-16 04:19:21 +01:00
Simon Rozman
542a281808 api: tighten function parameter code analysis annotations
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 19:00:17 +01:00
Simon Rozman
41b77964ca api: truncate long log lines with …
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 18:39:10 +01:00
Simon Rozman
7a6bab7f04 api: unify NetCfgInstanceId registry retrieval
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 18:39:10 +01:00
Simon Rozman
49a106b13e README: document the Windows SDK version requirement
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 18:39:10 +01:00
Jason A. Donenfeld
fb416747dc api: ensure that device object exists before returning from open/create
Some users are seeing errors like this after rebooting from Windows
Update:

2021-01-28 18:39:45.220197: [TUN] Creating Wintun interface
2021-01-28 18:39:49.420116: [TUN] [Wintun] CreateAdapter: Creating adapter
2021-01-28 18:39:53.704007: [TUN] [Wintun] OpenDeviceObject: Failed to connect to adapter: The system cannot find the path specified. (Code 0x00000003)
2021-01-28 18:39:53.704007: [TUN] [Wintun] WintunStartSession: Failed to open adapter device object
2021-01-28 18:39:54.097037: [TUN] Unable to create Wintun interface: Error starting session: The system cannot find the path specified.

It appears that creation of the device object file might happen
asynchronously, so this commit polls for it.

Reported-by: Artem Kuroptev <artem@kuroptev.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-02-03 22:36:41 +01:00
Jason A. Donenfeld
2628412d71 global: bump copyright
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-01-30 16:45:26 +01:00
Jason A. Donenfeld
49b7692d5b api: use custom devpkey for pool
It seems like the friendly name is still getting reset sometimes. Rather
than swimming upstream, it turns out we can just use a custom devpkey
that, according to msdn, is respected.

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/creating-custom-device-properties

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-01-30 16:41:16 +01:00
Jason A. Donenfeld
7710ff187b api: close private namespace when unloading DLL
Prior, people making calls to LoadLibrary/FreeLibrary would experience
a failure to create or open adapters, because the private namespace was
already loaded into the process and not cleaned up on DLL detachment.
While this pattern is probably a misuse of the library, we should still
support cleaning that up. This commit makes the right calls to free the
boundary descriptor and close the private namespace. It does not,
however, destroy the private namespace using the flag on the second
parameter, in case of races with other processes.

Reported-by: Brad Spencer <bspencer@blackberry.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-17 19:00:25 +01:00
Jason A. Donenfeld
d440234d6e version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-16 18:23:41 +01:00
Jason A. Donenfeld
ce83279187 project: license prebuilt binaries more permissively
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-16 18:23:41 +01:00
Jason A. Donenfeld
b2374dadb6 driver: use IoAllocateMdl without being too clever
Windows 7 doesn't like our trick of sticking MDLs into the NBL context
area. So we do things more traditionally, by allocating an MDL with
IoAllocateMdl and freeing it with IoFreeMdl. This means that we have to
keep track of the MDL between allocation and free time, and we don't
have any more miniport reserved pointers left in the NBL. So instead we
walk the MdlChain field of the first NB, and free the one that has an
address living inside of the non-partial MDL.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-15 21:13:06 +01:00
Jason A. Donenfeld
1269f86c76 driver: use partial MDL for slicing ring, rather than NB's DataOffset
Providing the DataOffset member of the NBL allocation function or
setting that member in the NB header indicates to NDIS not only that the
data starts at that offset, but that there's that amount of space
*available for it to use as it wants* before that offset. This meant
that NDIS was allowed to scribble data before the packet.

This was bounded by the size of the ring, so there was never any risk of
memory corruption, and since the ring is shared by userspace as well as
the rest of the kernel, we've always taken care of reading from it
closely, checking all values, and erroring out on corruption of the
ring. So, if NDIS wrote before the first packet, this would wind up
corrupting the RingTail and Alertable fields of the ring. The receiver
thread would then notice this, error out, and set the RingHead to
MAXULONG on its way out the door, so that userspace can detect it. And
indeed wintun.dll then started returning EOF from its write function.

Mostly this was not an issue, because we're not expecting for data to be
pushed on the head of a packet on ingress. But WSL2's Hyper-V driver is
actually pushing an ethernet header onto the front of the packet before
passing it off to Linux. Practically speaking, this manifested itself in
the RingTail and Alertable fields having Linux's MAC address! And then
the adapter would be EOF'd. This was reported as happening after WSL2
sends the *first* packet, but not others, which makes sense, because it
has to be at the beginning in order to corrupt those fields.

This fixes the problem by simply using a new MDL for the span we want,
instead of using the misunderstood DataOffset field. In order to not
need to keep track of memory allocations, we allocate the MDL as part of
the NBL's context area. And in order to avoid additional mappings, we
use IoBuildPartialMdl, which returns an MDL_PARTIAL, which does not have
an additional mapping that needs to be freed or unmapped.

After making this change, WSL2 no longer appears to halt the adapter,
and all works well.

Fixes: be8d2cb ("Avoid allocating second MDL")
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-13 18:31:44 +01:00
Stefan Rinkes
dd33757718 driver: use localtime in inf2cat
Otherwise the build fails at odd hours of the day.

Signed-off-by: Stefan Rinkes <stefan.rinkes@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-02 15:03:48 +01:00
Jason A. Donenfeld
a3a1fa8931 api: delay load remaining dlls to work around forwarder gotchas
RtlGenRandom forwards to cryptbase.dll, which is not in KnownDlls.
Therefore it's not a good idea to link to advapi32.dll at link time. How
many other gotchas of unusual forwarded functions are there? I don't
really want to find out. Therefore, delay load everything else.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-27 14:52:03 +01:00
Simon Rozman
ecf70261da api: skip notifying driver when there are no receive packets yet
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-27 14:52:03 +01:00
Jason A. Donenfeld
6af568d1e0 api: allow header to be mit
This doesn't change much, but it does make it mildly more convenient
plop this into mixed-use codebases.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-27 14:52:01 +01:00
Jason A. Donenfeld
a9f8b2b764 api: do not make dead gateway detection failures fatal
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-17 16:27:11 +01:00
Simon Rozman
f7c2ea4ded api: retry on ERROR_TRANSACTION_NOT_ACTIVE when disabling dead GW detect
There seems to be a race in the TCP/IP adapter registry key. Sometimes,
the adapter TCP/IP key is created, but setting the value
EnableDeadGWDetect fails with ERROR_TRANSACTION_NOT_ACTIVE.

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-17 15:30:00 +01:00
Jason A. Donenfeld
c18dd0a216 api: remove useless line
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-17 12:46:53 +01:00
Jason A. Donenfeld
e73613ee24 api: remove return value from logger function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-10 22:06:25 +01:00
Jason A. Donenfeld
87ef399d1c driver: do not allow compiler to reload PacketSize
In theory, the compiler could reload PacketSize after the bounds check
but before it's passed to NdisAllocateNetBufferAndNetBufferList. In
practice, it's not actually doing that, but better safe than sorry.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 22:23:26 +01:00
Jason A. Donenfeld
0f6296516a version: bump
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 15:34:38 +01:00
Jason A. Donenfeld
fc348f519b api: relax IsPoolMember estimation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 15:34:38 +01:00