* Bug: Close hvsock handle on listen error; fix tests
Close the socket created in
`github.com/Microsoft/go-winio/pkg/ListenHvsock` if either the `Bind` or
`Listen` calls fail.
Go changed `filepath.VolumeName` code, resulting in different behavior in
`github.com/Microsoft/go-winio/pkg/fs.GetFileSystemType`.
Update test accordingly.
Also add more debug logs to `pkg\fs\resolve_test.go`.
Also, move add skip for fuzzing on WS2019 or older to `FuzzHvSockRxTx`
code directly, instead of in ci.yml.
See: https://go-review.googlesource.com/c/go/+/540277
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* PR: unskip TestResolvePath
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
---------
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Where ever possible, use `golang.org/x/sys/windows` instead of `syscall`
(which has been deprecated since go1.11).
Using `windows.LocalFree` requires using `unsafe.Pointer`, which ensures
that the Go garbage collector does not try to free memory pre-maturely
if it was previously declared as a pointer.
Since `syscall.Handle` is part of API for `vhd` package, it was left
unchanged.
For security descriptor functions, switch to using
`windows.SECURITY_DESCRIPTOR` to avoid unnecessary byte manipulation and
panics due to missing input validation and error checking.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Run fuzzing tests in CI.
Use race detector when running tests.
Add missing `t.Helper()` calls.
Update test helpers in `pkg/bindfilter` to use `RtlGetNtVersionNumbers`
instead of reading registry, and skip tests if not running as admin.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Add `String()` functions to `etw.Level` and `etw.Opcode` types.
Add `etw.JSONStringField()`, which denotes a string field containing
JSON data.
Fix doc comment casing.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Add `fs.ResolvePath` to resolve symbolic links
`filepath.EvalSymlinks` does not work well on Windows, and can enter
infinite loops in certain situations and error out.
Use Win32 API GetFinalPathNameByHandle to handle path resolution.
Implementation based off on: https://github.com/containerd/containerd/pull/5411
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* PR: types, documentation
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* remove unneded constant groups
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Attempt normalized path first
Update logic to try querying for normalized path initially, then use
opened path if access is denied.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
---------
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Add some basic bind filter functions
This change adds the ability to mount a a single folder or a volume
inside another folder, using the bind filter API.
While the API allows mounting multiple sources inside a single mount
point, acting as an overlay, we disable this functionality in the ApplyFileBinding
function.
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Add some tests
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Move bind filter to different package
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Use string in signature and fix getFinalPath
* Properly close handle in getFinalPath()
* Use string in function signature. mksyscall generates proper code to
convert to utf16
* Enable TestRemoveFileBinding on Windows Server 2019
Windows Server 2019 only exposes 2 function in bindfltapi.dll:
* BfRemoveMapping
* BfSetupFilter
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Use windows.UTF16ToString to decode string
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Optimize bfGetMappings signature
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Skip unsupported tests on ltsc2019
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Fix typo, add testcase
* Additionally check if we can write to a read-only mount point, not
just delete from it
* No need to set FILE_FLAG_OPEN_REPARSE_POINT when opening a file
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Remove extra flags
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
* Add test to account for symlinks as sources
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
---------
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
It looks like for the purpose of the example "a" logger was needed, so not
strictly logrus (probably `fmt.Println()` would've worked even).
This patch removes logrus as dependency for the example. The only remaining
use of logrus in this repository is now in the pkg/etc/etwlogrus package, which
_does_ need logrus, but (possibly) could become its own module if we want to
remove logrus as dependency of go-winio itself.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Switched to subtests using `t.Run()` for GUID testing, rather than using
log statements to demarcate different tests.
Added `.String` to GUID `Variant` and `Version` using
`golang.org/x/tools/cmd/stringer`.
Added `tools.go` to version `stringer` in go.mod and allow
`go generate ./...` to be run without needing a `go get` call.
Based off of [go wiki](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module)
recommendation.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Add lint and go generate stages to CI
Add CI step to verify `go generate` was run on repo.
Add linter stage to CI along with linter config file,
`.golangci.yml`.
Will likely prefer revive over static-check.
Updated README Contributing section on linting requirements.
Added sequence ordering to make sure lint and go generate stages run
before tests and build.
This way, build and tests are not run on code that could potentially:
1. not build due to `gofmt` issues;
2. contain bugs;
3. have to be re-submitted after issues are fixed; or
4. contain outdated Win32 syscall or other auto-generated files.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* Fixed linter issues
Code changes to satisfy linters:
- Ran `gofmt -s -w` on repo.
- Broke up long lines.
- When possible, changed names with incorrect initialism formatting
- Added exceptions for exported variables.
- Added exceptions for ALL_CAPS_WITH_UNDERSCORES code.
- Switched to using `windows` or `syscall` definitions if possible;
especially if some constants were unused.
- Added `_ =` to satisfy error linter, and acknowledge that errors are
being ignored.
- Switched to using `errors.Is` and `As` in places, elsewhere added
exceptions if error value was known to be `syscall.Errno`.
- Removed bare returns.
- Prevented variables from being overshadowed in certain places
(ignoring cases of overshadowing `err`).
- Renamed variables and functions (eg, `len`, `eventMetadata.bytes`) to
prevent shadowing pre-built functions and imported pacakges.
- Removed unused method receivers.
- Added exceptions to certain unused (unexported) constants and
functions.
- Deleted unused `once` from `pkg/etw.providerMap`.
- Renamed `noop.go` files to `main_other.go` or `doc.go`, to better fit
style recommendations.
- Added exceptions for non-secure use of SHA1 and weak crypto
libraries.
- Replaced `ioutil` with `io` and `os` (and `t.TempDir` in tests).
- Added fully exhaustive checks for `switch` statements in `pkg/etw`.
- Defined constant strings for `tools/mkwinsyscall`.
- Removed unnecessary conversions.
- Made sure `context.Cancel` was called.
Additionally, added `//go:build windows" constraints on files with
unexported code, since linter will complain about unused code on
non-Windows platforms.
Added a stub `main() {}` for `mkwinsyscall` for non-Windows builds, just in
case `//go:generate` directives are added to OS-agnostic files.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* PR: spelling, constants, fuzzing
Moved HVSocket fuzzing tests to separate file with go 1.18 build
constraint.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* ETW name and options functionality
Added functionality to set the event (task) name and options (such as
event or associated event ID) for ETW events created by the hook based
on the logrus.Entry fields.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
* PR: export and comment
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
pkg/guid is not windows specific, as it can run on Linux.
Build constraints on guid_windows and guid_nonwindows are redundant with
file names, but added for consistency.
Added missing build constraints.
Added Ubuntu to test matrix, along with windows 2022.
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Since Go version 1.13, the standard library has built-in support for
wrapping and unwrapping of error values. This commit bumps the minimum
version required by the module from 1.12 to 1.13, replaces all calls to
errors.Wrap/f with fmt.Errorf and removes the now unneeded dependency on
github.com/pkg/errors.
Signed-off-by: Michael Hofmann <michael.hofmann@bitgestalt.com>
These packages had incorrect error handling for their generated syscall
bindings. The functions they were calling returned errors directly, but
the binding was written such that the generated code was calling
GetLastError instead.
Thankfully, this did not affect the detection of whether or not an error
had occurred, it only caused the value returned in the case of an error
to be not the right error code.
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
We don't support actually using ETW on windows/arm, but to make things
easier for downstream dependencies, we want to still allow the package
to compile and just no-op on this architecture. We do this by returning
a nil Provider, and implementing its methods to no-op when the receiver
is nil.
Previously this was implemented by putting NewProviderWithID in
provider_unsupported.go, but when we refactored the code so that the
actual work was done in NewProviderWithOptions instead, we didn't fix up
provider_unsupported. This change fixes this by putting only
NewProviderWithOptions in provider_unsupported.go, since the other
provider creation functions call into this one.
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
At this point points of difference between the three local
mksyscall_windows.go implementations and
golang.org/x/sys/windows/mkwinsyscall v0.0.0-20210104204734-6f8348627aad
are:
- pkg/etw: Deduplicates imported functions due to multiple calling APIs
- pkg/security and vhd: Forced UTF-16 mode, not checking A/W suffix. I
also removed cosmetic differences between these two implementations.
`go generate` reports no changes with these updates.
Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
This moves all the current users of
$GOROOT/src/syscall/mksyscall_windows.go to instead use
golang.org/x/sys/windows/mkwinsyscall, as directed by the version of the
former in Go 1.15.
It also syncs the local forks of mksyscall_windows.go with the latest
version of golang.org/x/sys/windows/mkwinsyscall/mkwinsyscall.go, so
that the local patches can be easily seen in a side-by-side comparison.
Significant changes compared to the in-tree forked versions:
* *bool parameters are read back through a temp-var, not directly like
other pointer parameters.
* ?-suffixed function names support testing for function presence before
calling. This replaces a local implementation of this in
pkg/security, which was not actually used anyway. The upstream version
correctly supports functions that don't already have an error return.
* `errnoErr(0)` is now useful, so each call of `errnoErr` doesn't need
to be protected with a check for 0 first.
* The generated functions are now sorted. This of course produced a
*lot* of churn in the generated files.
vhd\vhd.go was changed to generate syscalls into zvhd_windows.go, since
regeneration removes the build tag added by hand in
9d8277341f.
After all that, I also ran
```
go generate . .\pkg\etw\ .\pkg\process\ .\pkg\security\ .\vhd\
```
to update all the existing generated code.
Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
On Windows, the os/exec.{Command,CommandContext,LookPath} functions resolve command
names that have neither path separators nor file extension (e.g., "git") by first
looking in the current working directory before looking in the PATH environment
variable.
Go maintainers intended to match cmd.exe's historical behavior.
However, this is pretty much never the intended behavior and as an abundance of
precaution this patch prevents that when executing commands.
This patch was prompted by the [Go 1.15.7 security fixes](https://blog.golang.org/path-security).
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
The _docs_ say it returns 0 for failure, non-zero for success.
This implementation is looking for <0 as HRESULT failure, and deriving
an Errno from that.
The new tests fail with GetFileSystemType as it was defined, e.g.,
```
--- FAIL: TestGetFSTypeOfValidButAbsentDrive (0.00s)
fs_windows_test.go:41: GetFileSystemType a:\ unexpectedly succeeded
```
when I definitely do not have an A:\ drive.
Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
`golang.org/x/sys/windows.GUID` is currently only available to builds
targeting `GOOS=windows` (see golang/go#36485). This change makes it
so `windows` builds continue to use `golang.org/x/sys/windows.GUID`,
while non-`windows` builds get a different structure that has all
the same fields and methods.