@@ -6,7 +6,7 @@ use std::rc::Rc;
66
77use libcgroups:: common:: CgroupManager ;
88use nix:: unistd:: Pid ;
9- use oci_spec:: runtime:: Spec ;
9+ use oci_spec:: runtime:: { Linux , Spec } ;
1010
1111use super :: { Container , ContainerStatus } ;
1212use crate :: error:: { CreateContainerError , LibcontainerError , MissingSpecError } ;
@@ -162,7 +162,7 @@ impl ContainerBuilderImpl {
162162 // going to be switching to a different security context. Thus setting
163163 // ourselves to be non-dumpable only breaks things (like rootless
164164 // containers), which is the recommendation from the kernel folks.
165- if linux . namespaces ( ) . is_some ( ) {
165+ if joins_existing_namespaces ( linux ) {
166166 prctl:: set_dumpable ( false ) . map_err ( |e| {
167167 LibcontainerError :: Other ( format ! (
168168 "error in setting dumpable to false : {}" ,
@@ -272,3 +272,62 @@ impl ContainerBuilderImpl {
272272 Ok ( ( ) )
273273 }
274274}
275+
276+ /// Returns true if we join an existing namespace, like runc's check.
277+ fn joins_existing_namespaces ( linux : & Linux ) -> bool {
278+ let namespaces = match linux. namespaces ( ) {
279+ Some ( namespaces) => namespaces,
280+ None => return false ,
281+ } ;
282+ for ns in namespaces {
283+ if ns. path ( ) . is_some ( ) {
284+ return true ;
285+ }
286+ }
287+
288+ false
289+ }
290+
291+ #[ cfg( test) ]
292+ mod tests {
293+ use std:: path:: PathBuf ;
294+
295+ use oci_spec:: runtime:: { Linux , LinuxNamespace , LinuxNamespaceBuilder , LinuxNamespaceType } ;
296+
297+ use super :: joins_existing_namespaces;
298+
299+ fn new_namespace ( typ : LinuxNamespaceType ) -> LinuxNamespace {
300+ LinuxNamespaceBuilder :: default ( ) . typ ( typ) . build ( ) . unwrap ( )
301+ }
302+
303+ #[ test]
304+ fn test_no_namespaces_does_not_join ( ) {
305+ let mut linux = Linux :: default ( ) ;
306+ linux. set_namespaces ( None ) ;
307+ assert ! ( !joins_existing_namespaces( & linux) ) ;
308+ }
309+
310+ #[ test]
311+ fn test_only_new_namespaces_does_not_join ( ) {
312+ let mut linux = Linux :: default ( ) ;
313+ linux. set_namespaces ( Some ( vec ! [
314+ new_namespace( LinuxNamespaceType :: Pid ) ,
315+ new_namespace( LinuxNamespaceType :: Mount ) ,
316+ ] ) ) ;
317+ assert ! ( !joins_existing_namespaces( & linux) ) ;
318+ }
319+
320+ #[ test]
321+ fn test_namespace_with_path_joins ( ) {
322+ let mut linux = Linux :: default ( ) ;
323+ linux. set_namespaces ( Some ( vec ! [
324+ new_namespace( LinuxNamespaceType :: Pid ) ,
325+ LinuxNamespaceBuilder :: default ( )
326+ . typ( LinuxNamespaceType :: Network )
327+ . path( PathBuf :: from( "/proc/1234/ns/net" ) )
328+ . build( )
329+ . unwrap( ) ,
330+ ] ) ) ;
331+ assert ! ( joins_existing_namespaces( & linux) ) ;
332+ }
333+ }
0 commit comments