Skip to contentSkip to conversation

libselinux: add selinux_unshare() and is_selinux_unshared()

RFC PATCH Superseded Stephen Smalley 1 participant 1 message Oct 3, 2025 SELinux security module

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

RFC PATCH v2 1/1 libselinux: add selinux_unshare() and is_selinux_unshared()

RFC only, this demonstrates how to implement unsharing of the SELinux namespace using the lsm_set_self_attr(LSM_ATTR_UNSHARE, ...) system call and how to implement detection of whether one is in an unshared SELinux namespace that has not yet been fully initialized using the lsm_get_self_attr(LSM_ATTR_UNSHARE, ...) system call.

Provide a selinux_unshare() wrapper for lsm_set_self_attr(LSM_ATTR_UNSHARE, ...) and other required processing when unsharing the SELinux namespace, and an is_selinux_unshared() wrapper for lsm_get_self_attr(LSM_ATTR_UNSHARE, ...) for detecting whether one is in an unshared SELinux namespace that has not yet been fully initialized.

Add a selinux_unshare() interface to unshare the SELinux namespace, and a unshareselinux utility to run a shell or command in its own SELinux and mount namespaces. The selinux_unshare() interface expects the caller to have already unshared its mount namespace and created a MS_REC|MS_PRIVATE mount of / prior to invoking it so that it can freely modify the selinuxfs mount as needed by the unshare operation. The unshareselinux utility demonstrates how to do this prior to calling selinux_unshare(). Upon a successful return from selinux_unshare(), the SELinux namespace will be unshared and there will be no selinuxfs mount on /sys/fs/selinux. The caller can then proceed to mount a new selinuxfs filesystem private to the new namespace, load a policy, set enforcing mode, etc, as is commonly handled by init/systemd during boot.

Add an is_selinux_unshared() interface to detect whether one is in an unshared SELinux namespace that has not yet been fully initialized (i.e. no policy loaded yet), and a selinuxunshared utility to use it from a shell or script.

---
 libselinux/include/selinux/selinux.h |  22 ++++++
 libselinux/src/libselinux.map        |   6 ++
 libselinux/src/unshare.c             | 114 +++++++++++++++++++++++++++
 libselinux/utils/.gitignore          |   2 +
 libselinux/utils/selinuxunshared.c   |  24 ++++++
 libselinux/utils/unshareselinux.c    |  42 ++++++++++
 6 files changed, 210 insertions(+)
 create mode 100644 libselinux/src/unshare.c
 create mode 100644 libselinux/utils/selinuxunshared.c
 create mode 100644 libselinux/utils/unshareselinux.c
libselinux/include/selinux/selinux.h +22 -0
index b1431e5d..4cac6283 100644
@@ -760,6 +760,28 @@ extern int selinux_lsetfilecon_default(const char *path);
  */
 extern void selinux_reset_config(void);
 
+/*
+ * Unshare the SELinux namespace.
+ * Prior to invoking this API, the caller must have unshared the
+ * mount namespace (CLONE_NEWNS) and mounted a MS_REC|MS_PRIVATE
+ * / filesystem so that selinux_unshare() can freely modify any
+ * existing selinuxfs mount as needed for the unshare.
+ * Returns 0 on success, in which case the SELinux namespace has
+ * been unshared and any old selinuxfs mount will have been unmounted.
+ * The caller can then proceed to mount a new selinuxfs filesystem
+ * for the new namespace, load a policy, set enforcing mode, etc.
+ */
+extern int selinux_unshare(void);
+
+/*
+ * Returns 1 if the SELinux namespace was unshared and has not
+ * yet been fully initialized (i.e. policy not yet loaded).
+ * Returns 0 if the SELinux namespace was either not unshared or
+ * has been fully initialized.
+ * Returns -1 if not supported.
+ */
+extern int is_selinux_unshared(void);
+
 #ifdef __cplusplus
 }
 #endif
libselinux/src/libselinux.map +6 -0
index ab002f01..77f5790c 100644
@@ -262,3 +262,9 @@ LIBSELINUX_3.9 {
   global:
     context_to_str;
 } LIBSELINUX_3.8;
+
+LIBSELINUX_4.0 {
+  global:
+    selinux_unshare;
+    is_selinux_unshared;
+} LIBSELINUX_3.9;
libselinux/src/unshare.c new file +114 -0

114 lines not on this page · raw message

libselinux/utils/.gitignore +2 -0
index 2e10b14f..ffd2bc8e 100644
@@ -30,3 +30,5 @@ setfilecon
 togglesebool
 selinux_check_access
 validatetrans
+unshareselinux
+selinuxunshared
libselinux/utils/selinuxunshared.c new file +24 -0
new file mode 100644
index 00000000..965f7ba3
@@ -0,0 +1,24 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <selinux/selinux.h>
+
+int main(int argc, char **argv)
+{
+	int ret;
+
+	if (argc != 1) {
+		fprintf(stderr, "usage: %s\n", argv[0]);
+		exit(-1);
+	}
+
+	ret = is_selinux_unshared();
+	if (ret < 0) {
+		perror(argv[0]);
+		exit(-1);
+	}
+
+	printf("%d\n", ret);
+
+	exit(!ret);
+}
libselinux/utils/unshareselinux.c new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.51.0