Skip to content

Commit d7e2efe

Browse files
committed
C-WCOW: Differentiate container cmdline
Signed-off-by: Mahati Chamarthy <[email protected]>
1 parent f4d6807 commit d7e2efe

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

internal/gcs-sidecar/handlers.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
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

3129
const (
@@ -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-
236229
func (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

internal/gcs-sidecar/host.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ type Host struct {
4242
}
4343

4444
type Container struct {
45-
id string
46-
spec specs.Spec
47-
processesMutex sync.Mutex
48-
processes map[uint32]*containerProcess
45+
id string
46+
spec specs.Spec
47+
processesMutex sync.Mutex
48+
processes map[uint32]*containerProcess
49+
commandLine bool
50+
commandLineExec bool
4951
}
5052

5153
// Process is a struct that defines the lifetime and operations associated with

0 commit comments

Comments
 (0)