github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/specgen/generate/config_linux_cgo.go (about)

     1  // +build linux,cgo
     2  
     3  package generate
     4  
     5  import (
     6  	"context"
     7  	"io/ioutil"
     8  
     9  	"github.com/containers/libpod/libpod/image"
    10  	"github.com/containers/libpod/pkg/seccomp"
    11  	"github.com/containers/libpod/pkg/specgen"
    12  	spec "github.com/opencontainers/runtime-spec/specs-go"
    13  	"github.com/pkg/errors"
    14  	goSeccomp "github.com/seccomp/containers-golang"
    15  	"github.com/sirupsen/logrus"
    16  )
    17  
    18  func getSeccompConfig(s *specgen.SpecGenerator, configSpec *spec.Spec, img *image.Image) (*spec.LinuxSeccomp, error) {
    19  	var seccompConfig *spec.LinuxSeccomp
    20  	var err error
    21  	scp, err := seccomp.LookupPolicy(s.SeccompPolicy)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	if scp == seccomp.PolicyImage {
    27  		labels, err := img.Labels(context.Background())
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		imagePolicy := labels[seccomp.ContainerImageLabel]
    32  		if len(imagePolicy) < 1 {
    33  			return nil, errors.New("no seccomp policy defined by image")
    34  		}
    35  		logrus.Debug("Loading seccomp profile from the security config")
    36  		seccompConfig, err = goSeccomp.LoadProfile(imagePolicy, configSpec)
    37  		if err != nil {
    38  			return nil, errors.Wrap(err, "loading seccomp profile failed")
    39  		}
    40  		return seccompConfig, nil
    41  	}
    42  
    43  	if s.SeccompProfilePath != "" {
    44  		logrus.Debugf("Loading seccomp profile from %q", s.SeccompProfilePath)
    45  		seccompProfile, err := ioutil.ReadFile(s.SeccompProfilePath)
    46  		if err != nil {
    47  			return nil, errors.Wrapf(err, "opening seccomp profile (%s) failed", s.SeccompProfilePath)
    48  		}
    49  		seccompConfig, err = goSeccomp.LoadProfile(string(seccompProfile), configSpec)
    50  		if err != nil {
    51  			return nil, errors.Wrapf(err, "loading seccomp profile (%s) failed", s.SeccompProfilePath)
    52  		}
    53  	} else {
    54  		logrus.Debug("Loading default seccomp profile")
    55  		seccompConfig, err = goSeccomp.GetDefaultProfile(configSpec)
    56  		if err != nil {
    57  			return nil, errors.Wrapf(err, "loading seccomp profile (%s) failed", s.SeccompProfilePath)
    58  		}
    59  	}
    60  
    61  	return seccompConfig, nil
    62  }