Skip to content

Commit 40ebcd6

Browse files
committed
fix security opts support (seccomp and unconfined)
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9bd9f17 commit 40ebcd6

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

pkg/compose/create.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
362362
return nil, nil, nil, err
363363
}
364364

365-
securityOpts, err := parseSecurityOpts(p, service.SecurityOpt)
365+
securityOpts, unconfined, err := parseSecurityOpts(p, service.SecurityOpt)
366366
if err != nil {
367367
return nil, nil, nil, err
368368
}
@@ -401,35 +401,50 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
401401
OomScoreAdj: int(service.OomScoreAdj),
402402
}
403403

404+
if unconfined {
405+
hostConfig.MaskedPaths = []string{}
406+
hostConfig.ReadonlyPaths = []string{}
407+
}
408+
404409
return &containerConfig, &hostConfig, networkConfig, nil
405410
}
406411

407412
// copy/pasted from https://github.com/docker/cli/blob/9de1b162f/cli/command/container/opts.go#L673-L697 + RelativePath
408413
// TODO find so way to share this code with docker/cli
409-
func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, error) {
410-
for key, opt := range securityOpts {
414+
func parseSecurityOpts(p *types.Project, securityOpts []string) ([]string, bool, error) {
415+
var (
416+
unconfined bool
417+
parsed []string
418+
)
419+
for _, opt := range securityOpts {
420+
if opt == "systempaths=unconfined" {
421+
unconfined = true
422+
continue
423+
}
411424
con := strings.SplitN(opt, "=", 2)
412425
if len(con) == 1 && con[0] != "no-new-privileges" {
413426
if strings.Contains(opt, ":") {
414427
con = strings.SplitN(opt, ":", 2)
415428
} else {
416-
return securityOpts, errors.Errorf("Invalid security-opt: %q", opt)
429+
return securityOpts, false, errors.Errorf("Invalid security-opt: %q", opt)
417430
}
418431
}
419432
if con[0] == "seccomp" && con[1] != "unconfined" {
420433
f, err := os.ReadFile(p.RelativePath(con[1]))
421434
if err != nil {
422-
return securityOpts, errors.Errorf("opening seccomp profile (%s) failed: %v", con[1], err)
435+
return securityOpts, false, errors.Errorf("opening seccomp profile (%s) failed: %v", con[1], err)
423436
}
424437
b := bytes.NewBuffer(nil)
425438
if err := json.Compact(b, f); err != nil {
426-
return securityOpts, errors.Errorf("compacting json for seccomp profile (%s) failed: %v", con[1], err)
439+
return securityOpts, false, errors.Errorf("compacting json for seccomp profile (%s) failed: %v", con[1], err)
427440
}
428-
securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes())
441+
parsed = append(parsed, fmt.Sprintf("seccomp=%s", b.Bytes()))
442+
} else {
443+
parsed = append(parsed, opt)
429444
}
430445
}
431446

432-
return securityOpts, nil
447+
return parsed, unconfined, nil
433448
}
434449

435450
func (s *composeService) prepareLabels(service types.ServiceConfig, number int) (map[string]string, error) {

0 commit comments

Comments
 (0)