MCPcopy Create free account
hub / github.com/containerd/nerdctl / generateSecurityOpts

Function generateSecurityOpts

pkg/cmd/container/run_security_linux.go:59–159  ·  view source on GitHub ↗
(privileged bool, selinuxEnabled bool, securityOptsMap map[string]string)

Source from the content-addressed store, hash-verified

57)
58
59func generateSecurityOpts(privileged bool, selinuxEnabled bool, securityOptsMap map[string]string) ([]oci.SpecOpts, error) {
60 for k := range securityOptsMap {
61 switch k {
62 case "seccomp", "apparmor", "no-new-privileges", "systempaths", "privileged-without-host-devices", "writable-cgroups", "label":
63 default:
64 log.L.Warnf("unknown security-opt: %q", k)
65 }
66 }
67 var opts []oci.SpecOpts
68 if seccompProfile, ok := securityOptsMap["seccomp"]; ok && seccompProfile != defaults.SeccompProfileName {
69 if seccompProfile == "" {
70 return nil, errors.New("invalid security-opt \"seccomp\"")
71 }
72
73 if seccompProfile != "unconfined" {
74 opts = append(opts, seccomp.WithProfile(seccompProfile))
75 }
76 } else {
77 opts = append(opts, seccomp.WithDefaultProfile())
78 }
79
80 canLoadNewAppArmor := apparmorutil.CanLoadNewProfile()
81 canApplyExistingProfile := apparmorutil.CanApplyExistingProfile()
82 if aaProfile, ok := securityOptsMap["apparmor"]; ok {
83 if aaProfile == "" {
84 return nil, errors.New("invalid security-opt \"apparmor\"")
85 }
86 if aaProfile != "unconfined" {
87 if !canApplyExistingProfile {
88 log.L.Warnf("the host does not support AppArmor. Ignoring profile %q", aaProfile)
89 } else {
90 opts = append(opts, apparmor.WithProfile(aaProfile))
91 }
92 }
93 } else {
94 if canLoadNewAppArmor {
95 if err := apparmor.LoadDefaultProfile(defaults.AppArmorProfileName); err != nil {
96 return nil, err
97 }
98 }
99 if apparmorutil.CanApplySpecificExistingProfile(defaults.AppArmorProfileName) {
100 opts = append(opts, apparmor.WithProfile(defaults.AppArmorProfileName))
101 }
102 }
103 // TODO: should set unique MCS categorie.
104 if !privileged && selinuxEnabled {
105 var labelOpts []string
106 if selinuxLabel, ok := securityOptsMap["label"]; ok {
107 labelOpts = append(labelOpts, selinuxLabel)
108 }
109 processLabel, mountLabel, err := label.InitLabels(labelOpts)
110 if err != nil {
111 return nil, err
112 }
113 opts = append(opts, WithSelinuxLabel(processLabel, mountLabel))
114 }
115
116 nnp, err := maputil.MapBoolValueAsOpt(securityOptsMap, "no-new-privileges")

Callers 1

setPlatformOptionsFunction · 0.85

Calls 5

CanLoadNewProfileFunction · 0.92
CanApplyExistingProfileFunction · 0.92
MapBoolValueAsOptFunction · 0.92
WithSelinuxLabelFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…