Add tests for namespaces
RFC PATCH 1/1 Add tests for namespaces
From: Daniel Durning <[email protected]>
Add tests for namespaces, exercising the namespace create and setns permissions. Requires the correspending kernel patch, and the addition of a new set of classes and permissions to the policy.
The base policy can be updated to support the new class as follows:
Namespace classes: cgroup_namespace, ipc_namespace, mnt_namespace, net_namespace, pid_namespace, time_namespace, user_namespace, uts_namespace
Permissions: create, setns
- sudo semodule -c -E base
- sudo vi base.cil
Add a common class "namespace" with all listed perms. Add each class to the classorder list.
Add each class with empty permissions. Below each add a class common declaration using "namespace". Add allow rules for the unconfined_domain_type and init_t with the listed permissions for
each class to prevent anything from breaking.
- sudo semodule -i base.cil
- sudo cp /usr/share/selinux/devel/include/support/all_perms.spt \
/usr/share/selinux/devel/include/support/all_perms.spt.orig
- sudo vi /usr/share/selinux/devel/include/support/all_perms.spt
Define "all_namespace_perms" with the listed permissions. Add each class to the all_kernel_class_perms list with all_namespace_perms for each.
When finished testing, you can undo the module change and restore the original all_perms file as follows:
- sudo semodule -r base
- sudo cp /usr/share/selinux/devel/include/support/all_perms.spt.orig \
/usr/share/selinux/devel/include/support/all_perms.spt
--- policy/Makefile | 12 ++ policy/test_cap_userns.te | 2 + policy/test_filesystem.te | 23 ++ policy/test_global.te | 8 + policy/test_namespace.te | 75 +++++++ policy/test_overlayfs.te | 1 + tests/Makefile | 4 + tests/namespace/.gitignore | 2 + tests/namespace/Makefile | 7 + tests/namespace/ns_create_test.c | 99 +++++++++ tests/namespace/setns_test.c | 352 +++++++++++++++++++++++++++++++ tests/namespace/test | 182 ++++++++++++++++ 12 files changed, 767 insertions(+) create mode 100644 policy/test_namespace.te create mode 100644 tests/namespace/.gitignore create mode 100644 tests/namespace/Makefile create mode 100644 tests/namespace/ns_create_test.c create mode 100644 tests/namespace/setns_test.c create mode 100755 tests/namespace/test
policy/Makefile +12 -0
@@ -142,6 +142,10 @@ TARGETS += test_perf_event.te endif endif +ifeq ($(shell grep -q " mnt_namespace" $(POLDEV)/include/support/all_perms.spt && echo true),true) +TARGETS += test_namespace.te +endif + # Older kernels may still have the legacy lockdown class, so we need to add # the appropriate rules when the policy declares it. ifeq ($(shell grep -q lockdown $(POLDEV)/include/support/all_perms.spt && echo true),true)
@@ -187,6 +191,14 @@ ifeq ($(shell grep -q user_namespace $(POLDEV)/include/support/all_perms.spt && export M4PARAM += -Duser_namespace_defined endif +ifeq ($(shell grep -q mnt_namespace $(POLDEV)/include/support/all_perms.spt && echo true),true) +export M4PARAM += -Dmnt_namespace_defined +endif + +ifeq ($(shell grep -q pid_namespace $(POLDEV)/include/support/all_perms.spt && echo true),true) +export M4PARAM += -Dpid_namespace_defined +endif + ifeq ($(shell grep -q checkpoint_restore $(POLDEV)/include/support/all_perms.spt && echo true),true) export M4PARAM += -Dcheckpoint_restore_defined endif
policy/test_cap_userns.te +2 -0
@@ -21,6 +21,8 @@ typeattribute test_no_cap_userns_t capusernsdomain; # Rules common to both domains. allow_userns_create(capusernsdomain) +allow_mntns_create(capusernsdomain) +allow_pidns_create(capusernsdomain) # linux >= v5.12 needs setfcap to map UID 0 allow capusernsdomain self:capability setfcap;
policy/test_filesystem.te +23 -0
@@ -397,3 +397,26 @@ allow test_filesystem_no_mount_t dosfs_t:filesystem { associate }; allow test_filesystem_no_remount_t dosfs_t:filesystem { associate }; allow test_filesystem_no_unmount_t dosfs_t:filesystem { associate }; allow test_move_mount_no_mounton_t dosfs_t:filesystem { associate }; + +# +####### Allow mount namespace creation ################# +# +allow_mntns_create(test_filesystem_fscontext_t) +allow_mntns_create(test_filesystem_no_quotaget_t) +allow_mntns_create(test_filesystem_no_quotamod_t) +allow_mntns_create(test_move_mount_no_mounton_t) +allow_mntns_create(test_filesystem_context_t) +allow_mntns_create(test_filesystem_inode_relabel_no_associate_t) +allow_mntns_create(test_filesystem_inode_setxattr_no_associate_t) +allow_mntns_create(test_filesystem_may_create_no_associate_t) +allow_mntns_create(test_filesystem_no_getattr_t) +allow_mntns_create(test_filesystem_no_inode_no_relabelfrom_t) +allow_mntns_create(test_filesystem_no_mount_t) +allow_mntns_create(test_filesystem_no_remount_t) +allow_mntns_create(test_filesystem_no_unmount_t) +allow_mntns_create(test_filesystem_no_watch_mount_t) +allow_mntns_create(test_filesystem_no_watch_sb_t) +allow_mntns_create(test_filesystem_no_watch_t) +allow_mntns_create(test_filesystem_sb_relabel_no_relabelfrom_t) +allow_mntns_create(test_filesystem_sb_relabel_no_relabelto_t) +allow_mntns_create(test_filesystem_t)
policy/test_global.te +8 -0
@@ -192,3 +192,11 @@ ifdef(`lockdown_defined', `allow $1 self:lockdown confidentiality;') define(`allow_userns_create', ifdef(`user_namespace_defined', `allow $1 self:user_namespace create;') ) + +define(`allow_mntns_create', +ifdef(`mnt_namespace_defined', `allow $1 self:mnt_namespace create;') +) + +define(`allow_pidns_create', +ifdef(`pid_namespace_defined', `allow $1 self:pid_namespace create;') +)
policy/test_namespace.te new file +75 -0
75 lines not on this page · raw message
policy/test_overlayfs.te +1 -0
@@ -36,6 +36,7 @@ files_type(test_overlay_files_noaccess_t) allow test_overlay_mounter_t self:dir list_dir_perms; allow test_overlay_mounter_t self:file read_file_perms; allow test_overlay_mounter_t self:capability { sys_admin dac_override dac_read_search }; +allow_mntns_create(test_overlay_mounter_t) kernel_read_system_state(test_overlay_mounter_t) kernel_read_proc_symlinks(test_overlay_mounter_t)
tests/Makefile +4 -0
@@ -167,6 +167,10 @@ ifeq ($(shell grep -q io_uring $(POLDEV)/include/support/all_perms.spt && echo t SUBDIRS += io_uring endif +ifeq ($(shell grep -q " mnt_namespace" $(POLDEV)/include/support/all_perms.spt && echo true),true) +SUBDIRS += namespace +endif + ifeq ($(DISTRO),RHEL4) SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS)) endif
tests/namespace/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +setns_test +ns_create_test
tests/namespace/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +TARGETS = setns_test ns_create_test + +LDLIBS += -lselinux + +all: $(TARGETS) +clean: + rm -f $(TARGETS)
tests/namespace/ns_create_test.c new file +99 -0
99 lines not on this page · raw message
tests/namespace/setns_test.c new file +352 -0
352 lines not on this page · raw message
tests/namespace/test new file +182 -0
182 lines not on this page · raw message
Signature · 1 line
2.51.0
Stephen Smalley
maintainerQuoted 20 lines · On Mon, Mar 30, 2026 at 3:38 PM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Add tests for namespaces, exercising the namespace create and setns permissions. Requires the correspending kernel patch, and the addition of a new set of classes and permissions to the policy.
The base policy can be updated to support the new class as follows:
Namespace classes:
cgroup_namespace,ipc_namespace,mnt_namespace,net_namespace,pid_namespace,time_namespace,user_namespace,uts_namespacePermissions: create, setns
- sudo semodule -c -E base
- sudo vi base.cil
Quoted 7 lines
Add a common class "namespace" with all listed perms. Add each class to the classorder list.
Add each class with empty permissions. Below each add a class common declaration using "namespace". Add allow rules for theunconfined_domain_typeandinit_twith the listed permissions for
each class to prevent anything from breaking.
It would be better to show the actual policy lines that need to be added or modified in base.cil even if you don't provide a script for doing so, e.g:
Replace the existing line:
(class user_namespace (create)) with the following new lines:
(common namespace (create setns))
(classcommon cgroup_namespace namespace)
(classcommon ipc_namespace namespace)
(classcommon mnt_namespace namespace)
(classcommon net_namespace namespace)
(classcommon pid_namespace namespace)
(classcommon time_namespace namespace)
(classcommon user_namespace namespace)
(classcommon uts_namespace namespace)
(class cgroup_namespace ())
(class ipc_namespace ())
(class mnt_namespace ())
(class net_namespace ())
(class pid_namespace ())
(class time_namespace ())
(class user_namespace ())
(class uts_namespace ())
Replace the existing (classorder (...) line with one that adds the new classes at the end:
(classorder (... user_namespace cgroup_namespace ipc_namespace mnt_namespace net_namespace pid_namespace time_namespace uts_namespace ))
NB I did not add any allow rules to base.cil itself because I wanted to see exactly which permissions were triggered by the testsuite, see below.
Quoted 5 lines
- sudo vi
/usr/share/selinux/devel/include/support/all_perms.sptDefine "
all_namespace_perms" with the listed permissions. Add each class to theall_kernel_class_permslist withall_namespace_permsfor each.
Similarly, I would include the actual lines you added or changed, e.g.:
Add the following line at the top:
define(all_namespace_perms', { create setns }')
Replace the 'class user_namespace all_user_namespace_perms;' line with the following lines within the all_kernel_class_perms definition:
class cgroup_namespace all_namespace_perms;
class ipc_namespace all_namespace_perms;
class mnt_namespace all_namespace_perms;
class net_namespace all_namespace_perms;
class pid_namespace all_namespace_perms;
class time_namespace all_namespace_perms;
class user_namespace all_namespace_perms;
class uts_namespace all_namespace_perms;Quoted 21 lines
policy/test_filesystem.te +13
-0@@ -397,3 +397,26 @@ allow test_filesystem_no_mount_t dosfs_t:filesystem { associate }; allow test_filesystem_no_remount_t dosfs_t:filesystem { associate }; allow test_filesystem_no_unmount_t dosfs_t:filesystem { associate }; allow test_move_mount_no_mounton_t dosfs_t:filesystem { associate }; + +# +####### Allow mount namespace creation ################# +# +allow_mntns_create(test_filesystem_fscontext_t) +allow_mntns_create(test_filesystem_no_quotaget_t) +allow_mntns_create(test_filesystem_no_quotamod_t) +allow_mntns_create(test_move_mount_no_mounton_t) +allow_mntns_create(test_filesystem_context_t) +allow_mntns_create(test_filesystem_inode_relabel_no_associate_t) +allow_mntns_create(test_filesystem_inode_setxattr_no_associate_t) +allow_mntns_create(test_filesystem_may_create_no_associate_t) +allow_mntns_create(test_filesystem_no_getattr_t)
policy/test_filesystem.te allow test_filesystem_no_mount_t dosfs_t:filesystem { associate }; · line 422
Quoted 10 lines
policy/test_filesystem.te +10
-0+allow_mntns_create(test_filesystem_no_inode_no_relabelfrom_t) +allow_mntns_create(test_filesystem_no_mount_t) +allow_mntns_create(test_filesystem_no_remount_t) +allow_mntns_create(test_filesystem_no_unmount_t) +allow_mntns_create(test_filesystem_no_watch_mount_t) +allow_mntns_create(test_filesystem_no_watch_sb_t) +allow_mntns_create(test_filesystem_no_watch_t) +allow_mntns_create(test_filesystem_sb_relabel_no_relabelfrom_t) +allow_mntns_create(test_filesystem_sb_relabel_no_relabelto_t) +allow_mntns_create(test_filesystem_t)
I needed this rule as well to silence an avc: denied message:
+allow_mntns_create(test_file_no_quotaon_t)
Quoted 6 lines
policy/test_global.te +0
-0@@ -192,3 +192,11 @@ ifdef(`lockdown_defined', `allow $1 self:lockdown confidentiality;') define(`allow_userns_create',
policy/test_global.te ifdef(`lockdown_defined', `allow $1 self:lockdown confidentiality;') · line 202
Quoted 10 lines
policy/test_global.te +8
-0ifdef(`user_namespace_defined', `allow $1 self:user_namespace create;') ) + +define(`allow_mntns_create', +ifdef(`mnt_namespace_defined', `allow $1 self:mnt_namespace create;') +) + +define(`allow_pidns_create', +ifdef(`pid_namespace_defined', `allow $1 self:pid_namespace create;') +)
Since I didn't add any allow rules to base.cil, I needed the following rules added here:
+allow_mntns_create(testsuite_caller_domain) +allow_pidns_create(testsuite_caller_domain)
testsuite_caller_domain expands to the calling domain for the test scripts, typically unconfined_t or sysadm_t.
Quoted 34 lines
tests/namespace/setns_test.c new file +29
-0+int change_ctx (char * type) +{ +const char *context_s; +char *context_tmp; +context_t context; +int ret; + + ret =getcon (&context_tmp); +if (ret <0 ) { +perror ("getcon" ); +return ret; + } + + context =context_new (context_tmp); +if (!context) { + ret =-1 ; +perror ("context_new" ); +return ret; + } + + ret =context_type_set (context, type); +if (ret) { +perror ("context_type_set" ); +return ret; + } + +freecon (context_tmp); + context_s =context_str (context); +if (!context_s) {
tests/namespace/setns_test.c line 143
Quoted 10 lines
tests/namespace/setns_test.c new file +10
-0+ ret =-1 ; +perror ("context_type_set" ); +return ret; + } + + ret =setcon (context_s); +if (ret <0 ) { +perror ("setcon" ); +return ret; + }
Missing return 0; here.