Commit Graph
43 Commits
Author SHA1 Message Date
Aaron Klotz 1963303610 pipe.go: add DialPipeAccessImpLevel
I would like to use impersonation on the server side of the pipe, but I
currently cannot because all pipes are connected using the anonymous
impersonation level.

This patch adds a new function that provides the ability to explicitly
specify the desired impersonation level.

Fixes #297

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
2023-09-28 15:49:51 -06: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
David Golub b29bbd58a6 Add support for flushing and disconnecting named pipes (#292)
* Add support for disconnecting pipes

Signed-off-by: David Golub <david.golub@mongodb.com>

* Expose Flush

Signed-off-by: David Golub <david.golub@mongodb.com>

* Add test

Signed-off-by: David Golub <david.golub@mongodb.com>

* Fix lint errors

Signed-off-by: David Golub <david.golub@mongodb.com>

---------

Signed-off-by: David Golub <david.golub@mongodb.com>
2023-07-24 15:25:19 -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
rcarman-r7 41915dceb0 apply ObjectAttributes fix for certain Windows environments (#280)
Signed-off-by: Robert Carman <robert_carman@rapid7.com>
2023-03-16 13:30:07 -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
Daniel Canter 02e8aff16e Assign ErrPipeListenerClosed to net.ErrClosed
This error used to just be a `errors.New` with the text exactly matching
the "use of closed network connection" that you'd get from the Go stdlib.
Now that that error was exported as `net.ErrClosed` in Go 1.16 we should
be able to swap to this safely, and anyone who relied on checking the
string explicitly should still be fine.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
2022-04-05 18:07:44 -07:00
Danny Canter 1e35b45f32 Bump go version to 1.17 in go.mod/CI (#230)
1.13 is sufficiently old at this point and net.ErrClosed from 1.16 will be nice
to use.

This change additionally runs go fmt to bring in the new 1.17 build tag syntax
and builds the binaries in our CI with 1.17+.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
2022-04-05 15:56:47 -07:00
Gabriel Aszalos 68390cf0c2 Correct spelling in PipeConfig {Input,Output}BufferSize doc.
Signed-off-by: Gabriel Aszalos <gabriel.aszalos@gmail.com>
2020-11-23 23:01:29 +02:00
Maxim Fominykh 06bcafdc24 Duration order consistency when multiplying number by time unit 2020-01-11 05:26:35 +05:00
Pablo Ruiz Fischer Bennetts 3886dbb163 Expanded dial to allow to provide access level
Enable anonymous access

Remove non-context overload

Remove error being rethrown
2019-12-04 10:15:56 -08:00
John Howard 84b4ab48a5 Merge pull request #125 from jstarks/listenpipe
pipe: Fix ListenPipe/DialPipe race
2019-04-08 10:36:21 -07:00
John Starks bd71ef0e5d pipe: Fix ListenPipe/DialPipe race
ListenPipe can fail if there is a concurrent DialPipe because there is a
race window where DialPipe can connect to the initial server named pipe
before it is connected to and closed by ListenPipe.

To fix this, use the lower-level NT API for creating the server pipe,
since this API allows for specifying that a server pipe should initially
be in the disconnected state instead of the listening state. This allows
us to avoid the race condition by creating the pipe in the correct state
initially, so there is no longer a need to create a dummy client
connection.
2019-04-08 10:08:23 -07:00
sime1 6e309c3584 remove newOpenError 2019-03-26 18:39:51 +01:00
sime1 498f830763 formatting, inline Dial returned error 2019-03-26 18:37:37 +01:00
Simeone b9aaa11ea0 errors coercion to pass the tests 2019-03-26 10:09:27 +01:00
Simeone 5fdbdcc2ae add DialPipeContext 2019-03-26 09:43:43 +01:00
John Starks a6d595ae73 Merge pull request #80 from nanomsg/norace2
fixes #67 DialPipe problem with multiple calls / waiting for busy pipe
2018-07-19 15:37:18 -07:00
Garrett D'Amore effecfb659 Reduce the timeout to 2 seconds, and close the client handle.
We can safely close the client handle, because the server side
is not closed, and thsi keeps our hold on the pipe instance.
2018-07-18 12:07:29 -07:00
John Starks 3eabd1969d Clean up message mode pipe handling, add test 2018-06-25 09:31:55 -07:00
Garrett D'Amore ecd994be06 fixes #67 DialPipe problem with multiple calls / waiting for busy pipe
This changes a few things to try to ensure that we never wind up with
a result of ERROR_FILE_NOT_FOUND, due to a race between closing the
last pipe instance and opening the next.

First we keep an "open" client instance (unused) while the listener
is open, so that we are guaranteed to always have an active pipe
instance.  This means attempts to open while no other instances exist
result in ERROR_PIPE_BUSY instead of ERROR_FILE_NOT_FOUND.

Second we have changed the loop for dialing to eliminate a race condition
that is more or less inherent in WaitNamedPipe when synchronizing with
CreateFile.  The real timeout needs to be some larger value than the
WaitNamedPipe timeout, and furthermore WaitNamedPipe is not very nice
with the Go runtime, since it is a blocking system call.  Instead we
just put the goroutine to sleep for 10 milliseconds, and keep retrying
the CreateFile until the maximum timeout is reached.  If no timeout is
specified we assume a reasonable and large default of 5 seconds, which is
similar to a TCP connection timeout.

This isn't perfect, as a client attempting to connect to an extremely
busy pipe server can be starved out by other clients coming in while
it is in that brief sleep, but this potential race was already present
with WaitNamedPipe.  The numerous retries (by default 500 retries!)
mean its pretty unlikely to occur, and if a single client hits the
race once, it has an excellent chance of getting in the next cycle.

(A real "fix" that is completely race free and fair would require
changes in the underlying Named Pipe implementation, or some other
kind of external coordination.)
2018-06-24 11:39:09 -07:00
Tejaswini Duggaraju 211d6ebb39 Allow for message pipes in message readmode for Windows named pipes. 2018-05-25 12:13:39 -07:00
John Starks ae842e48e0 pipe: Resolve race between close and connect
This change resolves the issue where Accept() returns ERROR_NO_DATA
because the client closes the connection immediately. It resolves
the race by ignoring that particular connection and waiting on a
new one.
2018-01-09 13:28:13 -08:00
Francisco Souza 6f65f5291e Copy pointer to prevent data race on pipe connection
Closes #62.
2017-07-25 23:33:44 -04:00
Darren Stahl 18d7347919 RtlCopyMemory is not available on Nanoserver use builtin copy instead
Signed-off-by: Darren Stahl <darst@microsoft.com>
2017-05-23 17:26:29 -07:00
Darren Stahl a64f284add Move f.wg.Done() to Read and Write to avoid leaking handles
Signed-off-by: Darren Stahl <darst@microsoft.com>
2017-05-19 15:42:58 -07:00
John Starks d311c76e77 Merge pull request #51 from darrenstahlmsft/NoCGO
Remove cgo as a dependency added by #49
2017-05-08 14:01:43 -07:00
Darren Stahl de045f765f Remove cgo as a dependency added by #48
Signed-off-by: Darren Stahl <darst@microsoft.com>
2017-05-05 16:39:41 -07:00
Darren Stahl 899dbf3ba5 Apply read and write deadlines to pending IO
In order to upgrade to go1.8 all IO operations
including previous pending IO must have the deadline
set when SetReadDeadline or SetWriteDeadline is called

Signed-off-by: Darren Stahl <darst@microsoft.com>
2017-05-05 14:53:42 -07:00
Darren Stahl 23ad5a714c Allocate securityDescriptor in C memory
Signed-off-by: Darren Stahl <darst@microsoft.com>
2017-05-03 12:46:54 -07:00
Simon Ferquel 7c7d6b461c Flush file buffers on CloseWrite
This forces any pending data to be sent to the remote party before
emitting the 0-length EOF message, to make sure it appears as a seperate
message.

This fixes the "TestEchoWithMessaging" test

Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
2017-03-17 16:57:20 +01:00
Cezar Sa Espinola 94c0518b08 Include build tags to allow go get on non-Windows platforms. Fixes #30 2016-10-06 18:00:50 -03:00
John Starks b12bd3ac52 Let clients set pipe buffer sizes 2016-03-15 11:59:27 -07:00
John Starks 36f4e701c4 Add support for CloseWrite() to pipes
This change adds support for message mode pipes and uses them to support
CloseWrite() to better match TCP and UNIX sockets. Message mode pipes
support writing and (optionally) reading data in message-sized chunks.
This is useful for us because when in this mode a zero-sized message can
be read. We use this zero-sized message to signal that no more writes will
arrive. This is not standard practice in Windows, but it is a reasonable
compromise.
2016-03-15 11:58:02 -07:00
John Starks f011a81ddc Add interface to BackupRead/BackupWrite 2016-02-18 20:40:32 -08:00
John Starks 89bdf52a5b Add routine for looking up account names
Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 16:08:10 -08:00
John Starks fe517a5118 Misc. cleanup
Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 14:18:04 -08:00
John Starks 3aa597fc55 Open pipe client with anonymous security
This ensures that the pipe server cannot impersonate or even identify
the user that has connected to the pipe. This is a good default to
prevent malicious squatting on a named pipe name.

Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 14:03:47 -08:00
John Starks 99eef7be8d Ensure DialPipe times eventually by default
Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 14:00:21 -08:00
John Starks 3191792462 Fix errors to better match net package
Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 13:47:08 -08:00
John Starks b0d265e6c2 Ensure win32PipeListener.Close() correctly aborts Listen() calls
Signed-off-by: John Starks <jostarks@microsoft.com>
2016-01-31 13:46:55 -08:00
John Starks f001f808a9 Add file and pipe Windows wrappers
This includes Reader, Writer, and Closer interfaces on top of
Windows file handles in a way that does not block the system thread
while the IO is outstanding.

This also includes net.Listener and net.Conn for named pipes.
2016-01-30 18:11:47 -08:00