diff mbox series

[pseudo,2/2] pseudo_server: Log when running in a confined SELinux domain

Message ID 20261116190244.2205781-3-mark.hatle@amd.com
State New
Headers show
Series Attempt to prevent selinux related failures | expand

Commit Message

Mark Hatle Nov. 16, 2026, 7:02 p.m. UTC
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
Signed-off-by: Mark Hatle <mark.hatle@amd.com>
---
 README.security | 20 +++++++++++++++++
 pseudo_server.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
diff mbox series

Patch

diff --git a/README.security b/README.security
index 496524f..4067f96 100644
--- a/README.security
+++ b/README.security
@@ -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.
diff --git a/pseudo_server.c b/pseudo_server.c
index abe8897..0a30b27 100644
--- a/pseudo_server.c
+++ b/pseudo_server.c
@@ -143,6 +143,65 @@  handle_sigalrm(int sig) {
 	got_sigalrm = 1;
 }
 
+/* Read a small file into buf as a string, without a trailing newline or
+ * NUL.  Returns the length, or -1.
+ */
+static ssize_t
+pseudo_read_small_file(const char *path, char *buf, size_t size) {
+	ssize_t len;
+	int fd;
+
+	fd = open(path, O_RDONLY | O_CLOEXEC);
+	if (fd == -1)
+		return -1;
+	len = read(fd, buf, size - 1);
+	close(fd);
+	if (len < 0)
+		return -1;
+	while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\0'))
+		--len;
+	buf[len] = '\0';
+	return len;
+}
+
+/* SELinux-aware programs are told SELinux is disabled (YOCTO #6580), but
+ * the kernel still enforces the policy on what pseudo does for real, and a
+ * confined domain can refuse operations an unconfined user is allowed.  That
+ * is not a supported environment, so say so in the log.  The server runs in
+ * the domain of the build that started it.
+ */
+static void
+pseudo_server_check_selinux(void) {
+	static const char *const unconfined[] = {
+		"unconfined_t", "unconfined_service_t", NULL
+	};
+	char enforce[8], context[256];
+	char *type, *end;
+	size_t len;
+	int i;
+
+	if (pseudo_read_small_file("/sys/fs/selinux/enforce", enforce, sizeof(enforce)) <= 0 ||
+	    strcmp(enforce, "1"))
+		return;
+	if (pseudo_read_small_file("/proc/self/attr/current", context, sizeof(context)) <= 0)
+		return;
+	/* user:role:type:level */
+	type = strchr(context, ':');
+	if (type)
+		type = strchr(type + 1, ':');
+	if (!type)
+		return;
+	++type;
+	end = strchr(type, ':');
+	len = end ? (size_t) (end - type) : strlen(type);
+	for (i = 0; unconfined[i]; ++i) {
+		if (len == strlen(unconfined[i]) && !strncmp(type, unconfined[i], len))
+			return;
+	}
+	pseudo_warning("SELinux is enforcing and pseudo is running in the confined domain '%s'.  This is not a supported environment: the policy can refuse file operations pseudo performs, and builds may fail.  See README.security.\n",
+		context);
+}
+
 #define PSEUDO_CHILD_PROCESS_TIMEOUT 2
 int
 pseudo_server_start(int daemonize) {
@@ -244,6 +303,7 @@  pseudo_server_start(int daemonize) {
 
 	pseudo_info("pid %d [parent %d], doing new pid setup and server start\n", getpid(), getppid());
 	pseudo_new_pid();
+	pseudo_server_check_selinux();
 
 	pseudo_debug(PDBGF_SERVER, "opening lock.\n");
 	lockpath = pseudo_localstatedir_path(NULL);