diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9cfce5f7b2..acaa7f3b7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: - pull_request env: - GO_VERSION: "1.19.x" + GO_VERSION: "1.21.x" GOTESTSUM_VERSION: "latest" GOTESTCMD: "gotestsum --format standard-verbose --debug --" diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1e98251f64..230eaede61 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -29,7 +29,7 @@ on: # TODO: consolidate this with ci.yml so they both use the same Go version env: - GO_VERSION: "1.19.x" + GO_VERSION: "1.21.x" permissions: contents: read diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a6689896b1..2562173900 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,7 +5,7 @@ on: - 'v*' env: - GO_VERSION: "1.19.x" + GO_VERSION: "1.21.x" jobs: build: diff --git a/ext4/dmverity/dmverity_test.go b/ext4/dmverity/dmverity_test.go index a522d85344..f425500924 100644 --- a/ext4/dmverity/dmverity_test.go +++ b/ext4/dmverity/dmverity_test.go @@ -2,10 +2,10 @@ package dmverity import ( "bytes" + "crypto/rand" "encoding/binary" "errors" "io" - "math/rand" "os" "strings" "testing" diff --git a/go.mod b/go.mod index 6b869fca13..aa53de842f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Microsoft/hcsshim -go 1.18 +go 1.21 require ( github.com/Microsoft/cosesign1go v1.1.0 diff --git a/go.sum b/go.sum index 22f91ea4d5..5844b17762 100644 --- a/go.sum +++ b/go.sum @@ -163,6 +163,7 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/checkpoint-restore/go-criu/v4 v4.1.0/go.mod h1:xUQBLp4RLc5zJtWY++yjOoMoB5lihDt7fai+75m+rGw= github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M= github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E= @@ -497,6 +498,7 @@ github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68= +github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -721,6 +723,7 @@ github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lL github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= +github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= @@ -754,6 +757,7 @@ github.com/moby/sys/symlink v0.2.0/go.mod h1:7uZVF2dqJjG/NsClqul95CqKOBRQyYSNnJ6 github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo= github.com/moby/term v0.0.0-20210610120745-9d4ed1856297/go.mod h1:vgPCkQMyxTZ7IDy8SXRufE172gr8+K/JE/7hHFxHW3A= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -868,12 +872,14 @@ github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqr github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_golang v1.12.2/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= +github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= +github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20180110214958-89604d197083/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= @@ -884,6 +890,7 @@ github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9 github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.37.0 h1:ccBbHCgIiT9uSoFY0vX8H3zsNR5eLt17/RQLUvn8pXE= +github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA= github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= @@ -897,6 +904,7 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= +github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ= github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= @@ -1409,6 +1417,7 @@ golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44= +golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/cmd/io_binary.go b/internal/cmd/io_binary.go index d9e0f9749a..3647b66aae 100644 --- a/internal/cmd/io_binary.go +++ b/internal/cmd/io_binary.go @@ -28,6 +28,8 @@ const ( binaryCmdStartTimeout = 10 * time.Second ) +var ErrUnsafePath = errors.New("path is unsafe") + // NewBinaryIO runs a custom binary process for pluggable shim logging driver. // // Container's IO will be redirected to the logging driver via named pipes, which are @@ -122,14 +124,19 @@ func NewBinaryIO(ctx context.Context, id string, uri *url.URL) (_ UpstreamIO, er } // sanitizePath parses the URL object and returns a clean path to the logging driver -func sanitizePath(uri *url.URL) string { +func sanitizePath(uri *url.URL) (string, error) { path := filepath.Clean(uri.Path) + // avoid UNC paths (e.g. `\\server\share\`) + if strings.HasPrefix(path, `\\`) { + return "", ErrUnsafePath + } + if strings.Contains(path, `:\`) { - return strings.TrimPrefix(path, "\\") + return strings.TrimPrefix(path, "\\"), nil } - return path + return path, nil } func newBinaryCmd(ctx context.Context, uri *url.URL, envs []string) (*exec.Cmd, error) { @@ -145,7 +152,10 @@ func newBinaryCmd(ctx context.Context, uri *url.URL, envs []string) (*exec.Cmd, } } - execPath := sanitizePath(uri) + execPath, err := sanitizePath(uri) + if err != nil { + return nil, err + } cmd := exec.CommandContext(ctx, execPath, args...) cmd.Env = append(cmd.Env, envs...) diff --git a/internal/cmd/io_binary_test.go b/internal/cmd/io_binary_test.go index fcb00a511b..a930a8d5ac 100644 --- a/internal/cmd/io_binary_test.go +++ b/internal/cmd/io_binary_test.go @@ -4,6 +4,7 @@ package cmd import ( "context" + "errors" "net/url" "testing" ) @@ -24,11 +25,6 @@ func Test_newBinaryCmd_Key_Value_Pair(t *testing.T) { urlString: "binary:///executable?-key=value", expected: `\executable -key value`, }, - { - name: "Path_With_Back_Slashes", - urlString: `binary:///\executable?-key=value`, - expected: `\executable -key value`, - }, { name: "Clean_Path_With_Dots_And_Multiple_Fwd_Slashes", urlString: "binary:///../path/to///to/../executable", @@ -70,6 +66,45 @@ func Test_newBinaryCmd_Key_Value_Pair(t *testing.T) { } } +func Test_newBinaryCmd_Unsafe_Path(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + type config struct { + name string + urlString string + expectedError error + } + + for _, cfg := range []*config{ + { + name: "UNC_Path_With_Back_Slashes", + urlString: `binary:///\server\share\executable`, + expectedError: ErrUnsafePath, + }, + { + name: "UNC_Path_With_Forward_Slashes", + urlString: `binary:////server/share/executable`, + expectedError: ErrUnsafePath, + }, + } { + t.Run(cfg.name, func(t *testing.T) { + u, err := url.Parse(cfg.urlString) + if err != nil { + t.Fatalf("failed to parse url: %s", cfg.urlString) + } + + _, err = newBinaryCmd(ctx, u, nil) + if err == nil { + t.Fatalf("no error was returned") + } + if !errors.Is(err, cfg.expectedError) { + t.Fatalf("expected error: %s, actual: %s", cfg.expectedError, err) + } + }) + } +} + func Test_newBinaryCmd_Empty_Path(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/internal/cmd/io_npipe.go b/internal/cmd/io_npipe.go index 2fb8aef40b..2c1b67cc4a 100644 --- a/internal/cmd/io_npipe.go +++ b/internal/cmd/io_npipe.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "math/rand" "net" "sync" "syscall" @@ -20,11 +19,6 @@ import ( "golang.org/x/sys/windows" ) -func init() { - // Need to seed for the rng in backoff.NextBackoff() - rand.Seed(time.Now().UnixNano()) -} - // NewNpipeIO creates connected upstream io. It is the callers responsibility to validate that `if terminal == true`, `stderr == ""`. retryTimeout // refers to the timeout used to try and reconnect to the server end of the named pipe if the connection is severed. A value of 0 for retryTimeout // is treated as an infinite timeout. diff --git a/internal/tools/networkagent/main.go b/internal/tools/networkagent/main.go index ace37ac6dd..001e76fc8f 100644 --- a/internal/tools/networkagent/main.go +++ b/internal/tools/networkagent/main.go @@ -19,6 +19,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + crypto "crypto/rand" + "github.com/Microsoft/hcsshim/hcn" "github.com/Microsoft/hcsshim/internal/log" ncproxygrpc "github.com/Microsoft/hcsshim/pkg/ncproxy/ncproxygrpc/v1" @@ -39,7 +41,7 @@ const ( func generateMAC() (string, error) { buf := make([]byte, 6) - _, err := rand.Read(buf) + _, err := crypto.Read(buf) if err != nil { return "", err } diff --git a/internal/winapi/utils.go b/internal/winapi/utils.go index 93d633d490..70c43fc1cc 100644 --- a/internal/winapi/utils.go +++ b/internal/winapi/utils.go @@ -4,7 +4,6 @@ package winapi import ( "errors" - "reflect" "syscall" "unsafe" @@ -14,11 +13,7 @@ import ( // Uint16BufferToSlice wraps a uint16 pointer-and-length into a slice // for easier interop with Go APIs func Uint16BufferToSlice(buffer *uint16, bufferLength int) (result []uint16) { - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&result)) - hdr.Data = uintptr(unsafe.Pointer(buffer)) - hdr.Cap = bufferLength - hdr.Len = bufferLength - + result = unsafe.Slice(buffer, bufferLength) return } diff --git a/test/go.mod b/test/go.mod index a9871fbc63..d6e762d6c4 100644 --- a/test/go.mod +++ b/test/go.mod @@ -1,6 +1,6 @@ module github.com/Microsoft/hcsshim/test -go 1.18 +go 1.21 require ( github.com/Microsoft/go-winio v0.6.1