Skip to contentSkip to conversation

Add tests for namespaces

PATCH New [email protected] 1 participant 1 message May 1 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 classes as follows:

- sudo semodule -c -E base
- sudo vi base.cil

Replace the existing (class user_namespace (create)) line with the following:
(common namespace (create setns))
(class cgroup_namespace ())
(class ipc_namespace ())
(class mnt_namespace (create_anon ))
(class net_namespace ())
(class pid_namespace ())
(class time_namespace ())
(class user_namespace ())
(class uts_namespace ())
(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)

Edit the existing (classorder (...)) line by adding the new classes at the end:
(classorder (... user_namespace cgroup_namespace ipc_namespace mnt_namespace net_namespace pid_namespace time_namespace uts_namespace))

Add allow rules for the unconfined_domain_type and
init_t for each class to prevent anything from breaking:
(allow unconfined_domain_type self (cgroup_namespace (create setns))) (allow unconfined_domain_type self (ipc_namespace (create setns))) (allow unconfined_domain_type self (mnt_namespace (create create_anon setns))) (allow unconfined_domain_type self (net_namespace (create setns))) (allow unconfined_domain_type self (pid_namespace (create setns))) (allow unconfined_domain_type self (time_namespace (create setns))) (allow unconfined_domain_type self (user_namespace (create setns))) (allow unconfined_domain_type self (uts_namespace (create setns))) (allow init_t self (cgroup_namespace (create setns)))
(allow init_t self (ipc_namespace (create setns)))
(allow init_t self (mnt_namespace (create create_anon setns))) (allow init_t self (net_namespace (create setns)))
(allow init_t self (pid_namespace (create setns)))
(allow init_t self (time_namespace (create setns)))
(allow init_t self (user_namespace (create setns)))
(allow init_t self (uts_namespace (create setns)))

- 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

Replace the existing define(all_user_namespace_perms',{ create }') line with the following:
define(all_namespace_perms',{ create setns }') define(all_mnt_namespace_perms',{ create create_anon setns }')

Replace the existing class user_namespace all_user_namespace_perms line with the following:

class user_namespace all_namespace_perms;
	class cgroup_namespace all_namespace_perms;
	class ipc_namespace all_namespace_perms;
	class mnt_namespace all_mnt_namespace_perms;
	class net_namespace all_namespace_perms;
	class pid_namespace all_namespace_perms;
	class time_namespace all_namespace_perms;
	class uts_namespace all_namespace_perms;

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

---
Changes in v2:
- Added tests for anonymous mount namespaces
- Changed instructions for policy modification for clarify
- Fixed allow rules in policy to silence an avc denial
- Fixed a missing return in change_ctx()

---
 policy/Makefile                       |  12 +
 policy/test_cap_userns.te             |   2 +
 policy/test_filesystem.te             |  24 ++
 policy/test_global.te                 |  16 ++
 policy/test_namespace.te              | 298 ++++++++++++++++++++++
 policy/test_overlayfs.te              |   1 +
 tests/Makefile                        |   4 +
 tests/namespace/.gitignore            |   3 +
 tests/namespace/Makefile              |   7 +
 tests/namespace/ns_create_anon_test.c |  64 +++++
 tests/namespace/ns_create_test.c      |  99 +++++++
 tests/namespace/setns_test.c          | 354 ++++++++++++++++++++++++++
 tests/namespace/test                  | 185 ++++++++++++++
 13 files changed, 1069 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_anon_test.c
 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
index bf1ea76..5e43abe 100644
@@ -143,6 +143,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)
@@ -188,6 +192,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
index 2994312..8aa1b6b 100644
@@ -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 +24 -0
index f60b0c8..dfe72c1 100644
@@ -397,3 +397,27 @@ 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_anon(test_filesystem_fscontext_t)
+allow_mntns_create_anon(test_filesystem_no_quotaget_t)
+allow_mntns_create_anon(test_filesystem_no_quotamod_t)
+allow_mntns_create_anon(test_file_no_quotaon_t)
+allow_mntns_create_anon(test_move_mount_no_mounton_t)
+allow_mntns_create_anon(test_filesystem_context_t)
+allow_mntns_create_anon(test_filesystem_inode_relabel_no_associate_t)
+allow_mntns_create_anon(test_filesystem_inode_setxattr_no_associate_t)
+allow_mntns_create_anon(test_filesystem_may_create_no_associate_t)
+allow_mntns_create_anon(test_filesystem_no_getattr_t)
+allow_mntns_create_anon(test_filesystem_no_inode_no_relabelfrom_t)
+allow_mntns_create_anon(test_filesystem_no_mount_t)
+allow_mntns_create_anon(test_filesystem_no_remount_t)
+allow_mntns_create_anon(test_filesystem_no_unmount_t)
+allow_mntns_create_anon(test_filesystem_no_watch_mount_t)
+allow_mntns_create_anon(test_filesystem_no_watch_sb_t)
+allow_mntns_create_anon(test_filesystem_no_watch_t)
+allow_mntns_create_anon(test_filesystem_sb_relabel_no_relabelfrom_t)
+allow_mntns_create_anon(test_filesystem_sb_relabel_no_relabelto_t)
+allow_mntns_create_anon(test_filesystem_t)
policy/test_global.te +16 -0
index cb7d9c2..83eecde 100644
@@ -73,6 +73,10 @@ dontaudit testsuite_caller_domain testsuite_domain:process { noatsecure rlimitin
 # sigkill - needed for some tests to pass
 allow testsuite_caller_domain testsuite_domain:process { ptrace sigkill };
 
+# Allow test caller domain to create namespaces
+allow testsuite_caller_domain self:mnt_namespace { create };
+allow testsuite_caller_domain self:pid_namespace { create };
+
 # keys test may trigger search AVCs for root's keys
 dontaudit testsuite_domain testsuite_caller_domain:key { search };
 
@@ -192,3 +196,15 @@ 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_mntns_create_anon',
+ifdef(`mnt_namespace_defined', `allow $1 self:mnt_namespace create_anon;')
+)
+
+define(`allow_pidns_create',
+ifdef(`pid_namespace_defined', `allow $1 self:pid_namespace create;')
+)
policy/test_namespace.te new file +298 -0

298 lines not on this page · raw message

policy/test_overlayfs.te +1 -0
index c09b577..6e601b3 100644
@@ -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_anon(test_overlay_mounter_t)
 
 kernel_read_system_state(test_overlay_mounter_t)
 kernel_read_proc_symlinks(test_overlay_mounter_t)
tests/Makefile +4 -0
index 4fe6f3c..f05ed15 100644
@@ -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 +3 -0
new file mode 100644
index 0000000..f20ccff
@@ -0,0 +1,3 @@
+setns_test
+ns_create_test
+ns_create_anon_test
tests/namespace/Makefile new file +7 -0
new file mode 100644
index 0000000..7b0d217
@@ -0,0 +1,7 @@
+TARGETS = setns_test ns_create_test ns_create_anon_test
+
+LDLIBS += -lselinux
+
+all: $(TARGETS)
+clean:
+	rm -f $(TARGETS)
tests/namespace/ns_create_anon_test.c new file +64 -0

64 lines not on this page · raw message

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 +354 -0

354 lines not on this page · raw message

tests/namespace/test new file +185 -0

185 lines not on this page · raw message

Signature · 1 line
2.53.0