mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-08-28 04:46:50 -04:00
* 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>
132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
//go:build windows
|
|
// +build windows
|
|
|
|
package winio
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"strings"
|
|
"unicode/utf16"
|
|
"unsafe"
|
|
)
|
|
|
|
const (
|
|
reparseTagMountPoint = 0xA0000003
|
|
reparseTagSymlink = 0xA000000C
|
|
)
|
|
|
|
type reparseDataBuffer struct {
|
|
ReparseTag uint32
|
|
ReparseDataLength uint16
|
|
Reserved uint16
|
|
SubstituteNameOffset uint16
|
|
SubstituteNameLength uint16
|
|
PrintNameOffset uint16
|
|
PrintNameLength uint16
|
|
}
|
|
|
|
// ReparsePoint describes a Win32 symlink or mount point.
|
|
type ReparsePoint struct {
|
|
Target string
|
|
IsMountPoint bool
|
|
}
|
|
|
|
// UnsupportedReparsePointError is returned when trying to decode a non-symlink or
|
|
// mount point reparse point.
|
|
type UnsupportedReparsePointError struct {
|
|
Tag uint32
|
|
}
|
|
|
|
func (e *UnsupportedReparsePointError) Error() string {
|
|
return fmt.Sprintf("unsupported reparse point %x", e.Tag)
|
|
}
|
|
|
|
// DecodeReparsePoint decodes a Win32 REPARSE_DATA_BUFFER structure containing either a symlink
|
|
// or a mount point.
|
|
func DecodeReparsePoint(b []byte) (*ReparsePoint, error) {
|
|
tag := binary.LittleEndian.Uint32(b[0:4])
|
|
return DecodeReparsePointData(tag, b[8:])
|
|
}
|
|
|
|
func DecodeReparsePointData(tag uint32, b []byte) (*ReparsePoint, error) {
|
|
isMountPoint := false
|
|
switch tag {
|
|
case reparseTagMountPoint:
|
|
isMountPoint = true
|
|
case reparseTagSymlink:
|
|
default:
|
|
return nil, &UnsupportedReparsePointError{tag}
|
|
}
|
|
nameOffset := 8 + binary.LittleEndian.Uint16(b[4:6])
|
|
if !isMountPoint {
|
|
nameOffset += 4
|
|
}
|
|
nameLength := binary.LittleEndian.Uint16(b[6:8])
|
|
name := make([]uint16, nameLength/2)
|
|
err := binary.Read(bytes.NewReader(b[nameOffset:nameOffset+nameLength]), binary.LittleEndian, &name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ReparsePoint{string(utf16.Decode(name)), isMountPoint}, nil
|
|
}
|
|
|
|
func isDriveLetter(c byte) bool {
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
|
}
|
|
|
|
// EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink or
|
|
// mount point.
|
|
func EncodeReparsePoint(rp *ReparsePoint) []byte {
|
|
// Generate an NT path and determine if this is a relative path.
|
|
var ntTarget string
|
|
relative := false
|
|
if strings.HasPrefix(rp.Target, `\\?\`) {
|
|
ntTarget = `\??\` + rp.Target[4:]
|
|
} else if strings.HasPrefix(rp.Target, `\\`) {
|
|
ntTarget = `\??\UNC\` + rp.Target[2:]
|
|
} else if len(rp.Target) >= 2 && isDriveLetter(rp.Target[0]) && rp.Target[1] == ':' {
|
|
ntTarget = `\??\` + rp.Target
|
|
} else {
|
|
ntTarget = rp.Target
|
|
relative = true
|
|
}
|
|
|
|
// The paths must be NUL-terminated even though they are counted strings.
|
|
target16 := utf16.Encode([]rune(rp.Target + "\x00"))
|
|
ntTarget16 := utf16.Encode([]rune(ntTarget + "\x00"))
|
|
|
|
size := int(unsafe.Sizeof(reparseDataBuffer{})) - 8
|
|
size += len(ntTarget16)*2 + len(target16)*2
|
|
|
|
tag := uint32(reparseTagMountPoint)
|
|
if !rp.IsMountPoint {
|
|
tag = reparseTagSymlink
|
|
size += 4 // Add room for symlink flags
|
|
}
|
|
|
|
data := reparseDataBuffer{
|
|
ReparseTag: tag,
|
|
ReparseDataLength: uint16(size),
|
|
SubstituteNameOffset: 0,
|
|
SubstituteNameLength: uint16((len(ntTarget16) - 1) * 2),
|
|
PrintNameOffset: uint16(len(ntTarget16) * 2),
|
|
PrintNameLength: uint16((len(target16) - 1) * 2),
|
|
}
|
|
|
|
var b bytes.Buffer
|
|
_ = binary.Write(&b, binary.LittleEndian, &data)
|
|
if !rp.IsMountPoint {
|
|
flags := uint32(0)
|
|
if relative {
|
|
flags |= 1
|
|
}
|
|
_ = binary.Write(&b, binary.LittleEndian, flags)
|
|
}
|
|
|
|
_ = binary.Write(&b, binary.LittleEndian, ntTarget16)
|
|
_ = binary.Write(&b, binary.LittleEndian, target16)
|
|
return b.Bytes()
|
|
}
|