99 "fmt"
1010 "os"
1111 "path/filepath"
12- "strings"
1312 "time"
1413
1514 "github.com/Microsoft/hcsshim/hcn"
@@ -25,7 +24,6 @@ import (
2524 "github.com/Microsoft/hcsshim/pkg/cimfs"
2625 "github.com/Microsoft/hcsshim/pkg/securitypolicy"
2726 "github.com/pkg/errors"
28- "golang.org/x/sys/windows"
2927)
3028
3129const (
@@ -91,11 +89,15 @@ func (b *Bridge) createContainer(req *request) (err error) {
9189 if err := b .hostState .SetupSecurityContextDir (ctx , & spec ); err != nil {
9290 return err
9391 }
92+ commandLine := len (spec .Process .Args ) > 0
9493 c := & Container {
95- id : containerID ,
96- spec : spec ,
97- processes : make (map [uint32 ]* containerProcess ),
94+ id : containerID ,
95+ spec : spec ,
96+ processes : make (map [uint32 ]* containerProcess ),
97+ commandLine : commandLine ,
98+ commandLineExec : false ,
9899 }
100+
99101 log .G (ctx ).Tracef ("Adding ContainerID: %v" , containerID )
100102 if err := b .hostState .AddContainer (req .ctx , containerID , c ); err != nil {
101103 log .G (ctx ).Tracef ("Container exists in the map." )
@@ -224,15 +226,6 @@ func (b *Bridge) shutdownForced(req *request) (err error) {
224226 return nil
225227}
226228
227- // escapeArgs makes a Windows-style escaped command line from a set of arguments.
228- func escapeArgs (args []string ) string {
229- escapedArgs := make ([]string , len (args ))
230- for i , a := range args {
231- escapedArgs [i ] = windows .EscapeArg (a )
232- }
233- return strings .Join (escapedArgs , " " )
234- }
235-
236229func (b * Bridge ) executeProcess (req * request ) (err error ) {
237230 _ , span := oc .StartSpan (req .ctx , "sidecar::executeProcess" )
238231 defer span .End ()
@@ -272,15 +265,19 @@ func (b *Bridge) executeProcess(req *request) (err error) {
272265 return fmt .Errorf ("failed to get created container: %w" , err )
273266 }
274267
275- // if this is an exec of Container command line, then it's already enforced
276- // during container creation, hence skip it here
277- containerCommandLine := escapeArgs (c .spec .Process .Args )
278- if processParams .CommandLine != containerCommandLine {
268+ c .processesMutex .Lock ()
269+ isCreateExec := c .commandLine && ! c .commandLineExec
270+ if isCreateExec {
271+ // if this is an exec of Container command line, then it's already enforced
272+ // during container creation, hence skip it here
273+ c .commandLineExec = true
279274
275+ }
276+ c .processesMutex .Unlock ()
277+ if ! isCreateExec {
280278 user := securitypolicy.IDName {
281279 Name : processParams .User ,
282280 }
283-
284281 log .G (req .ctx ).Tracef ("Enforcing policy on exec in container" )
285282 _ , _ , _ , err = b .hostState .securityPolicyEnforcer .
286283 EnforceExecInContainerPolicyV2 (
@@ -298,7 +295,7 @@ func (b *Bridge) executeProcess(req *request) (err error) {
298295 }
299296 headerID := req .header .ID
300297
301- // initiate process ID
298+ // initiate exec process response channel
302299 procRespCh := make (chan * prot.ContainerExecuteProcessResponse , 1 )
303300 b .pendingMu .Lock ()
304301 b .pending [headerID ] = procRespCh
0 commit comments