Skip to contentSkip to conversation

Attempt to prevent selinux related failures

PATCH New Mark Hatle 1 participant 3 messages Nov 16 Kbuild

lore
Share & tools
lore

Mark Hatle

author
#0 lore raw

cover Attempt to prevent selinux related failures

[YOCTO #6580]

This is the oldest open pseudo related bug in the bugzilla. The change adds some code that will tell users that SELinux is always in a non-enforcing mode. This should cause various tools to avoid doing stupid things, even if the user is in a configuration that otherwise works.

Additionally, a check was added that tries to identify if we are in a confined environment, and logs this with a warning. The idea is if someone is trying to debug what is going wrong, they'll at least get information that the environment is confined, and it might not be a pseudo bug.

Mark Hatle (2):
ports/linux: Report SELinux as disabled under pseudo
pseudo_server: Log when running in a confined SELinux domain

 .gitignore                                |  1 +
 Makefile.in                               | 10 +++-
 README.security                           | 20 ++++++++
 ports/linux/guts/is_selinux_enabled.c     | 24 +++++++++
 ports/linux/guts/is_selinux_mls_enabled.c | 16 ++++++
 ports/linux/pseudo_wrappers.c             | 23 +++++++++
 ports/linux/wrapfuncs.in                  |  2 +
 pseudo_server.c                           | 60 +++++++++++++++++++++++
 test/fake-selinux.c                       | 15 ++++++
 test/test-selinux-disabled.c              | 49 ++++++++++++++++++
 test/test-selinux-disabled.sh             | 20 ++++++++
 11 files changed, 239 insertions(+), 1 deletion(-)
 create mode 100644 ports/linux/guts/is_selinux_enabled.c
 create mode 100644 ports/linux/guts/is_selinux_mls_enabled.c
 create mode 100644 test/fake-selinux.c
 create mode 100644 test/test-selinux-disabled.c
 create mode 100755 test/test-selinux-disabled.sh
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 ports/linux: Report SELinux as disabled under pseudo

SELinux-aware programs such as install label the files they create from the host's policy. pseudo only records the label, which is wrong for target files and lets the host's policy leak into the build. Programs which then pass a recorded label to the kernel through /proc/self/attr/fscreate, as sed -i does, can be refused when the build runs in a confined SELinux domain, and fail.

Wrap is_selinux_enabled() and is_selinux_mls_enabled() to report SELinux as disabled, so these programs behave as on a host without SELinux. Labels set explicitly, e.g. by setfiles when labelling an image, are still recorded. The host's answer is only given while pseudo is disabled; libselinux may not be loaded then, so it is looked up when needed, and SELinux is reported as disabled without it.

test-selinux-disabled checks both answers with the host's libselinux, and with a stand-in reporting SELinux as enabled, to emulate a host where it is. It is skipped if the host has no libselinux.

Based on a patch by Enrico Scholz attached to the bug.

[YOCTO #6580]

AI-Generated: Uses GitHub Copilot

---
 .gitignore                                |  1 +
 Makefile.in                               | 10 ++++-
 ports/linux/guts/is_selinux_enabled.c     | 24 +++++++++++
 ports/linux/guts/is_selinux_mls_enabled.c | 16 ++++++++
 ports/linux/pseudo_wrappers.c             | 23 +++++++++++
 ports/linux/wrapfuncs.in                  |  2 +
 test/fake-selinux.c                       | 15 +++++++
 test/test-selinux-disabled.c              | 49 +++++++++++++++++++++++
 test/test-selinux-disabled.sh             | 20 +++++++++
 9 files changed, 159 insertions(+), 1 deletion(-)
 create mode 100644 ports/linux/guts/is_selinux_enabled.c
 create mode 100644 ports/linux/guts/is_selinux_mls_enabled.c
 create mode 100644 test/fake-selinux.c
 create mode 100644 test/test-selinux-disabled.c
 create mode 100755 test/test-selinux-disabled.sh
.gitignore +1 -0
index bc4cbe8..79c0328 100644
@@ -17,3 +17,4 @@ func_deps.mk
 port_deps.mk
 test/test-*
 !test/test-*.*
+test/*.so
Makefile.in +9 -1
index 4c873d6..38ec23f 100644
@@ -84,6 +84,14 @@ test-verbose: all $(TESTS) | $(BIN) $(LIB)
 test/test-%: test/test-%.c
 	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $@ $<
 
+# test-selinux-disabled loads libselinux, or a stand-in reporting SELinux as
+# enabled, at run time.
+test/libfake-selinux.so: test/fake-selinux.c
+	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -shared -o $@ $<
+
+test/test-selinux-disabled: test/test-selinux-disabled.c test/libfake-selinux.so
+	$(CC) $(CFLAGS) $(CFLAGS_PSEUDO) -o $@ $< -ldl
+
 install-lib: $(LIBPSEUDO)
 	mkdir -p $(DESTDIR)$(LIBDIR)
 	cp $(LIBPSEUDO) $(DESTDIR)$(LIBDIR)
@@ -176,7 +184,7 @@ pseudo_profile: Makefile pseudo_profile.c tables wrappers
 
 clean:
 	rm -f *.o $(LIBPSEUDO) $(PSEUDO) $(PSEUDODB) $(PSEUDOLOG) \
-		$(TESTS) \
+		$(TESTS) test/libfake-selinux.so \
 		pseudo_wrapfuncs.h pseudo_wrapfuncs.c \
 		pseudo_wrapper_table.c \
 		pseudo_tables.c pseudo_tables.h \
ports/linux/guts/is_selinux_enabled.c new file +24 -0
new file mode 100644
index 0000000..de96da2
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int is_selinux_enabled(void)
+ *	int rc = -1;
+ */
+
+	/* SELinux-aware programs label the files they create or install
+	 * using the host's policy.  pseudo only records the label, which is
+	 * wrong for target files and leaks the host policy into the build,
+	 * and programs which then hand it back to the kernel, e.g. through
+	 * /proc/self/attr/fscreate, can be refused (YOCTO #6580).  So report
+	 * SELinux as disabled, as on a host without it.  Labels set
+	 * explicitly, e.g. by setfiles, are still recorded.  The host's
+	 * answer is only given while pseudo is disabled.
+	 */
+	rc = 0;
+
+/*	return rc;
+ * }
+ */
ports/linux/guts/is_selinux_mls_enabled.c new file +16 -0
new file mode 100644
index 0000000..fff934f
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2026 Yocto Project
+ * guts/COPYRIGHT for information.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * int is_selinux_mls_enabled(void)
+ *	int rc = -1;
+ */
+
+	/* see is_selinux_enabled() */
+	rc = 0;
+
+/*	return rc;
+ * }
+ */
ports/linux/pseudo_wrappers.c +23 -0
index bba1577..ebf73b3 100644
@@ -47,6 +47,29 @@ pseudo_mknodat(int dirfd, const char *path, mode_t mode, dev_t dev) {
 	return real___xmknodat(_MKNOD_VER, dirfd, path, mode, &dev);
 }
 
+/* The host's is_selinux_enabled() and is_selinux_mls_enabled(), used only
+ * while pseudo is disabled.  They are in libselinux, which may not be loaded
+ * when the wrappers are set up, or at all, so look them up when they are
+ * needed.  Without libselinux, SELinux isn't in use.
+ */
+int
+pseudo_is_selinux_enabled(void) {
+	static int (*f)(void) = NULL;
+
+	if (!f)
+		f = (int (*)(void)) dlsym(RTLD_NEXT, "is_selinux_enabled");
+	return f ? f() : 0;
+}
+
+int
+pseudo_is_selinux_mls_enabled(void) {
+	static int (*f)(void) = NULL;
+
+	if (!f)
+		f = (int (*)(void)) dlsym(RTLD_NEXT, "is_selinux_mls_enabled");
+	return f ? f() : 0;
+}
+
 int pseudo_capset(cap_user_header_t hdrp, const cap_user_data_t datap) {
 	(void)hdrp;
 	(void)datap;
ports/linux/wrapfuncs.in +2 -0
index dbbf2f7..9f1e013 100644
@@ -14,6 +14,8 @@ int open_tree(int dirfd, const char *path, unsigned int flags); /* preserve_path
 int open_tree_attr(int dirfd, const char *path, unsigned int flags, struct mount_attr *attr, size_t size); /* preserve_paths=1, noignore_path=1*/
 int name_to_handle_at(int dirfd, const char *name, struct file_handle *handle, int *mount_id, int flags);
 int open_by_handle_at(int mount_fd, struct file_handle *handle, int flags);
+int is_selinux_enabled(void); /* real_func=pseudo_is_selinux_enabled */
+int is_selinux_mls_enabled(void); /* real_func=pseudo_is_selinux_mls_enabled */
 int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod, path_lock=1 */
 int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat, path_lock=1 */
 int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW, path_lock=1 */
test/fake-selinux.c new file +15 -0
new file mode 100644
index 0000000..8f3253d
@@ -0,0 +1,15 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * A stand-in for libselinux which reports SELinux as enabled, so that
+ * test-selinux-disabled can emulate a host where it is.
+ */
+int
+is_selinux_enabled(void) {
+	return 1;
+}
+
+int
+is_selinux_mls_enabled(void) {
+	return 1;
+}
test/test-selinux-disabled.c new file +49 -0

49 lines not on this page · raw message

test/test-selinux-disabled.sh new file +20 -0
new file mode 100755
index 0000000..fdace34
@@ -0,0 +1,20 @@
+#!/bin/sh
+#
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# SELinux is reported as disabled under pseudo, and the host's answer is
+# given while PSEUDO_DISABLED is set.  Skipped without libselinux.
+
+t=./test/test-selinux-disabled
+lib=libselinux.so.1
+
+# The host's libselinux: pseudo reports SELinux as disabled, and with
+# PSEUDO_DISABLED gives the same answer as without pseudo at all.
+$t $lib 0,0
+case $? in 0) ;; 77) exit 255 ;; *) exit 1 ;; esac
+host=$(PSEUDO_UNLOAD=1 $t $lib print) || exit 1
+PSEUDO_DISABLED=1 $t $lib "$host" || exit 1
+
+# Emulate a host with SELinux enabled, whatever this one does.
+$t ./test/libfake-selinux.so 0,0 || exit 1
+PSEUDO_DISABLED=1 $t ./test/libfake-selinux.so 1,1 || exit 1
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 pseudo_server: Log when running in a confined SELinux domain

In reply to Mark Hatle

SELinux-aware programs are now told SELinux is disabled, but the kernel still enforces the host's policy on what pseudo does for real. A build running in a confined SELinux domain, such as user_t, can have file operations refused that an unconfined user is allowed, which isn't a supported environment.

When the server starts with SELinux enforcing in a domain other than unconfined_t or unconfined_service_t, log a warning saying so, so that failures caused by the policy can be recognized. The server runs in the domain of the build that started it, and reads /sys/fs/selinux/enforce and /proc/self/attr/current directly, as pseudo doesn't use libselinux. Document both this and SELinux being reported as disabled in README.security.

Tested with the two files replaced by test copies, as this host doesn't use SELinux.

[YOCTO #6580]

AI-Generated: Uses GitHub Copilot

---
 README.security | 20 +++++++++++++++++
 pseudo_server.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
README.security +20 -0
index 496524f..4067f96 100644
@@ -169,6 +169,25 @@ to programs pseudo does not intercept (see section 1), and the Landlock
 calls reach the kernel while PSEUDO_DISABLED is set.
 
 
+8. SELINUX IS REPORTED AS DISABLED
+
+SELinux-aware programs, such as install, label the files they create
+from the host's policy.  Under pseudo the label is only recorded, which
+is wrong for target files, and a program that hands a recorded label
+back to the kernel, as sed -i does through /proc/self/attr/fscreate, can
+be refused (YOCTO #6580).  So under pseudo, is_selinux_enabled() and
+is_selinux_mls_enabled() report SELinux as disabled, and these programs
+behave as on a host without SELinux.  Labels set explicitly, for
+example by setfiles when labelling an image, are still recorded.  The
+host's answer is only given while PSEUDO_DISABLED is set.
+
+The kernel still enforces the host's policy on everything pseudo does
+for real.  A build running in a confined SELinux domain, such as
+user_t, can have file operations refused that an unconfined user is
+allowed, and is not supported.  When the server starts with SELinux
+enforcing in a confined domain, it says so in its log.
+
+
 RECOMMENDATIONS:
 
 * Run pseudo only on build output that is private to the build and is
@@ -183,3 +202,4 @@ RECOMMENDATIONS:
   security relevant files in the final image.
 * Where pseudo cannot install its io_uring filter, disable io_uring on
   the build host with the kernel.io_uring_disabled sysctl.
+* On a host with SELinux enforcing, run builds in an unconfined domain.
pseudo_server.c +60 -0

72 lines not on this page · raw message

Signature · 1 line
2.34.1