Commit Graph
90 Commits
Author SHA1 Message Date
Hamza El-Saawy bc421d9108 Bug: Close hvsock handle on listen error; fix tests (#310)
* 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>
2023-12-18 11:11:12 -05:00
Hamza El-Saawy 9f0d5dc7d2 Switch from sycall to windows (#295)
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>
2023-08-07 14:06:05 -04:00
Hamza El-Saawy fec52bd5aa [lint] Fix errors from #276 (#296)
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2023-08-03 13:31:02 -04:00
Hamza El-Saawy 704d6c6359 Add ResolvePath tests (#276)
Add required computestorage APIs to allow for running tests of
`ResolvePath`.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2023-08-03 12:10:39 -04:00
Hamza El-Saawy 19a9f656e1 Update tests; run fuzzing (#294)
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>
2023-07-21 18:06:41 -04:00
Hamza El-Saawy 6a0f8b69d1 [etw] Add String() functions, JSON field option (#285)
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>
2023-04-26 12:56:36 -04:00
Hamza El-Saawy 070c828abb update linter and fix lints (#284)
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2023-04-14 15:24:28 -04:00
Hamza El-Saawy b884eb77db Add fs.ResolvePath to resolve symbolic links (#275)
* 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>
2023-04-14 12:58:14 -04:00
Gabriel dd5de6900b Add some basic bind filter functions (#274)
* 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>
2023-02-28 11:37:19 -05:00
Sebastiaan van Stijn cbf4dd9782 pkg/etw/sample: remove dependency on github.com/sirupsen/logrus (#267)
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>
2023-02-13 11:25:49 -05:00
Hamza El-Saawy 376b2013fe GUID tests to use t.Run (#256)
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>
2022-08-29 16:27:34 -04:00
Hamza El-Saawy e268c11e27 Add lint and go generate steps to CI (#254)
* 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>
2022-08-23 15:05:05 -04:00
Hamza El-Saawy 35837cff61 Update mkwinsyscall and make stand alone tool (#248)
* Add latest mkwinsyscall file

Copied file from:
https://github.com/golang/sys/blob/bc2c85ada10aa9b6aa9607e9ac9ad0761b95cf1d/windows/mkwinsyscall/mkwinsyscall.go

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* Update to match hcsshim mksyscall_windows

Added hr error return type, cmdline flags for winio import, utf16, and
sorting.

Generated file has build constrain comment.

Input files can be specified with a glob pattern.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: update comment, use glob

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2022-06-16 14:05:19 -04:00
Hamza El-Saawy 87532d1cfe Set ETW event name and options for logrus hook (#245)
* 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>
2022-04-27 15:00:13 -04:00
Hamza El-Saawy b52bba4773 Updating windows build constraints
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>
2022-03-29 12:38:23 -04:00
Michael Hofmann c4cf81c481 Replace github.com/pkg/errors with stdlib errors
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>
2021-12-01 11:09:15 +01:00
Kathryn Baldauf 60c1574cd7 Merge pull request #169 from dcormier/dc/guid
Allow guid package to be used on non-Windows GOOS targets
2021-10-25 17:23:25 -07:00
Kyle Wojtaszek 4c72048b5c FIxing corruption in callbacks
Signed-off-by: Kyle Wojtaszek <kylewo@microsoft.com>
2021-10-12 14:31:04 -07:00
Kyle W 0b148d19cf Fix corruption in x86 callback 2021-06-14 13:44:35 -07:00
Kevin Parsons 9f0ab2cc0a Fix syscall error handling in vhd and pkg/security
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>
2021-04-26 15:08:11 -07:00
Kathryn Baldauf 085c1a94ab Add CI github action for testing on push and PR
Signed-off-by: Kathryn Baldauf <kabaldau@microsoft.com>
2021-04-21 15:35:36 -07:00
Kevin Parsons 941861fe1a pkg/etw: Fix to build on windows/arm
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>
2021-03-24 15:05:25 -07:00
Paul "TBBle" Hampson bc48b61dc9 Make local mksyscall_windows.go forks less forked
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>
2021-02-11 22:50:55 +11:00
Paul "TBBle" Hampson 1390d1a07b Pull sync generated syscalls
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>
2021-02-11 22:47:40 +11:00
Kathryn Baldauf 68cdd9bd9d Merge pull request #191 from uhthomas/master
Add build guards for all Windows imports
2021-02-10 17:11:08 -08:00
Thomas e09292102f Add noop mains for non windows builds 2021-02-11 00:05:48 +00:00
Thomas d4c02ad896 Add missing build constraints 2021-02-10 23:54:19 +00:00
Thomas f0d2c76ab3 Fix missing platform/architecture separation 2021-02-10 23:09:41 +00:00
Thomas eb9d8c9346 Separate platform from architecture build constraints 2021-02-10 23:05:19 +00:00
Sebastiaan van Stijn f2a56450f4 Use golang.org/x/sys/execabs instead of os/exec
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>
2021-02-10 09:21:40 +01:00
Kathryn Baldauf a38e1dc8bb Merge pull request #188 from TBBle/correct-GetVolumeInformation-return-handling
Correct GetVolumeInformation return type handling
2021-02-09 18:46:35 -08:00
Kyle Wojtaszek 652abba813 Removing unneeded NewProviderWithGroup function 2021-01-27 14:43:00 -08:00
Kyle Wojtaszek 30844eed75 Add ability to set provider group 2021-01-27 13:12:37 -08:00
Thomas Way 9d8277341f Add build guards for all Windows imports 2021-01-21 23:24:01 +00:00
Paul "TBBle" Hampson 0966e1ad56 Correct GetVolumeInformation return type handling
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>
2021-01-16 17:11:37 +11:00
Daniel Cormier 892f7849c3 Allow guid package to be used on non-Windows GOOS targets
`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.
2020-03-04 09:29:26 -05:00
Kevin Parsons f272bf30b3 Add new process package
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-09-18 18:03:14 -07:00
Kevin Parsons c20fb68775 etw: Add test for provider name to GUID conversion
Also simplified the name to GUID conversion code, and improved the
comments.

Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-07-10 15:12:20 -07:00
Kevin Parsons 177d2ec7ea guid: Add V5 GUID support
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-07-10 15:08:22 -07:00
Kevin Parsons c5294c98c0 guid: Improve version and variant handling
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-07-10 15:03:44 -07:00
Eric Hotinger 28396ed851 Updates the error message in GetFileSystemType to match the function's
casing and exports the error.

Signed-off-by: Eric Hotinger <ehotinger@gmail.com>
2019-07-01 14:25:30 -07:00
Eric Hotinger 9fe4f5c769 Adds a file system helper file for determining the file system type.
Signed-off-by: Eric Hotinger <ehotinger@gmail.com>
2019-06-25 13:42:09 -07:00
Justin d2ef9cfdac Merge pull request #149 from microsoft/etw-logrus-time
etwlogrus: Pass through entry time as ETW field
2019-06-25 10:40:15 -07:00
Kevin Parsons 09baa54393 etwlogrus: Pass through entry time as ETW field
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-06-25 10:36:45 -07:00
Kevin Parsons 2ae559ad19 Replace ETW Logrus hook levels with AllLevels
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-06-25 10:32:12 -07:00
Kevin Parsons 3dac69f6ea pkg/guid: Switch from json.Marshaler to encoding.TextMarshaler
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-05-13 14:31:21 -07:00
Kevin Parsons b940a6f7ae pkg/guid: Improve error messages
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-05-13 14:30:09 -07:00
Kevin Parsons 2dc6637e2c pkg/guid: Support big-endian and Windows encodings
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-05-13 14:28:49 -07:00
Kevin Parsons 6dd84a2574 pkg/guid: Add helper functions to tests
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-05-13 14:25:53 -07:00
Kevin Parsons 1197d54849 pkg/guid: Add variant and version support
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2019-05-13 14:22:42 -07:00