Skip to content

Commit

Permalink
Merging dev into main (#2)
Browse files Browse the repository at this point in the history
* apply ObjectAttributes fix for certain Windows versions

* remove verbose logging for NtPath

* remove remaining DEBUG logging

* modifiying the dialer to add security (#1)

---------

Co-authored-by: Robert Carman <[email protected]>
  • Loading branch information
rtonini-r7 and rcarman-r7 authored Mar 31, 2023
1 parent 58dba89 commit 5513523
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pipe.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package winio
Expand All @@ -13,8 +14,12 @@ import (
"syscall"
"time"
"unsafe"

"golang.org/x/sys/windows"
)

const OBJ_CASE_INSENSITIVE = 0x40

//sys connectNamedPipe(pipe syscall.Handle, o *syscall.Overlapped) (err error) = ConnectNamedPipe
//sys createNamedPipe(name string, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error) [failretval==syscall.InvalidHandle] = CreateNamedPipeW
//sys createFile(name string, access uint32, mode uint32, sa *syscall.SecurityAttributes, createmode uint32, attrs uint32, templatefile syscall.Handle) (handle syscall.Handle, err error) [failretval==syscall.InvalidHandle] = CreateFileW
Expand Down Expand Up @@ -84,6 +89,9 @@ const (
cFILE_PIPE_REJECT_REMOTE_CLIENTS = 2

cSE_DACL_PRESENT = 4

LOCAL_SYSTEM_RID = "S-1-5-18"
ADMINS_RID = "S-1-5-32-544"
)

var (
Expand Down Expand Up @@ -237,6 +245,22 @@ func DialPipeAccess(ctx context.Context, path string, access uint32) (net.Conn,
return nil, err
}

info, err := windows.GetSecurityInfo(windows.Handle(h), windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION)
if err != nil {
return nil, err
}

sid, _, err := info.Owner()
if err != nil {
return nil, err
}

// only connect to named pipe if the owner sid is BUILTIN\Administrators or Local System
if !(sid.String() == ADMINS_RID || sid.String() == LOCAL_SYSTEM_RID) {
return nil, errors.New(fmt.Sprintf("refused connection to unsafe named pipe created by user with sid"+
" %s\n", sid.String()))
}

var flags uint32
err = getNamedPipeInfo(h, &flags, nil, nil, nil)
if err != nil {
Expand Down Expand Up @@ -335,6 +359,7 @@ func makeServerPipeHandle(path string, sd []byte, c *PipeConfig, first bool) (sy
h syscall.Handle
iosb ioStatusBlock
)
oa.Attributes = OBJ_CASE_INSENSITIVE // use OBJ_CASE_INSENSITIVE sys.windows
err = ntCreateNamedPipeFile(&h, access, &oa, &iosb, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, disposition, 0, typ, 0, 0, 0xffffffff, uint32(c.InputBufferSize), uint32(c.OutputBufferSize), &timeout).Err()
if err != nil {
return 0, &os.PathError{Op: "open", Path: path, Err: err}
Expand Down

0 comments on commit 5513523

Please sign in to comment.