Skip to content

Commit ffe2b1e

Browse files
Merge pull request #8685 from mheon/ignore_containersconf_sysctls_shared_net
Ignore containers.conf sysctls when sharing namespaces
2 parents 1f59276 + 864592c commit ffe2b1e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

libpod/runtime_pod_infra_linux.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@ func (r *Runtime) makeInfraContainer(ctx context.Context, p *Pod, imgName, rawIm
159159
g.AddMount(devPts)
160160
}
161161

162+
// Add default sysctls from containers.conf
163+
defaultSysctls, err := util.ValidateSysctls(r.config.Sysctls())
164+
if err != nil {
165+
return nil, err
166+
}
167+
for sysctlKey, sysctlVal := range defaultSysctls {
168+
// Ignore mqueue sysctls if not sharing IPC
169+
if !p.config.UsePodIPC && strings.HasPrefix(sysctlKey, "fs.mqueue.") {
170+
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since IPC Namespace for pod is unused", sysctlKey, sysctlVal)
171+
172+
continue
173+
}
174+
175+
// Ignore net sysctls if host network or not sharing network
176+
if (p.config.InfraContainer.HostNetwork || !p.config.UsePodNet) && strings.HasPrefix(sysctlKey, "net.") {
177+
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since Network Namespace for pod is unused", sysctlKey, sysctlVal)
178+
continue
179+
}
180+
181+
// Ignore uts sysctls if not sharing UTS
182+
if !p.config.UsePodUTS && (strings.HasPrefix(sysctlKey, "kernel.domainname") || strings.HasPrefix(sysctlKey, "kernel.hostname")) {
183+
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since UTS Namespace for pod is unused", sysctlKey, sysctlVal)
184+
continue
185+
}
186+
187+
g.AddLinuxSysctl(sysctlKey, sysctlVal)
188+
}
189+
162190
containerName := p.ID()[:IDTruncLength] + "-infra"
163191
options = append(options, r.WithPod(p))
164192
options = append(options, WithRootFSFromImage(imgID, imgName, rawImageName))

pkg/specgen/generate/security.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ func securityConfigureGenerator(s *specgen.SpecGenerator, g *generate.Generator,
178178

179179
g.SetRootReadonly(s.ReadOnlyFilesystem)
180180

181+
noUseIPC := s.IpcNS.NSMode == specgen.FromContainer || s.IpcNS.NSMode == specgen.FromPod || s.IpcNS.NSMode == specgen.Host
182+
noUseNet := s.NetNS.NSMode == specgen.FromContainer || s.NetNS.NSMode == specgen.FromPod || s.NetNS.NSMode == specgen.Host
183+
noUseUTS := s.UtsNS.NSMode == specgen.FromContainer || s.UtsNS.NSMode == specgen.FromPod || s.UtsNS.NSMode == specgen.Host
184+
181185
// Add default sysctls
182186
defaultSysctls, err := util.ValidateSysctls(rtc.Sysctls())
183187
if err != nil {
@@ -186,20 +190,20 @@ func securityConfigureGenerator(s *specgen.SpecGenerator, g *generate.Generator,
186190
for sysctlKey, sysctlVal := range defaultSysctls {
187191

188192
// Ignore mqueue sysctls if --ipc=host
189-
if s.IpcNS.IsHost() && strings.HasPrefix(sysctlKey, "fs.mqueue.") {
193+
if noUseIPC && strings.HasPrefix(sysctlKey, "fs.mqueue.") {
190194
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since IPC Namespace set to host", sysctlKey, sysctlVal)
191195

192196
continue
193197
}
194198

195199
// Ignore net sysctls if --net=host
196-
if s.NetNS.IsHost() && strings.HasPrefix(sysctlKey, "net.") {
200+
if noUseNet && strings.HasPrefix(sysctlKey, "net.") {
197201
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since Network Namespace set to host", sysctlKey, sysctlVal)
198202
continue
199203
}
200204

201205
// Ignore uts sysctls if --uts=host
202-
if s.UtsNS.IsHost() && (strings.HasPrefix(sysctlKey, "kernel.domainname") || strings.HasPrefix(sysctlKey, "kernel.hostname")) {
206+
if noUseUTS && (strings.HasPrefix(sysctlKey, "kernel.domainname") || strings.HasPrefix(sysctlKey, "kernel.hostname")) {
203207
logrus.Infof("Sysctl %s=%s ignored in containers.conf, since UTS Namespace set to host", sysctlKey, sysctlVal)
204208
continue
205209
}

0 commit comments

Comments
 (0)