Blame SOURCES/0181-secontext-fix-expected-SELinux-context-check-for-unl.patch

d0480e
From 3f0e5340b651da98251a58cc7923525d69f96032 Mon Sep 17 00:00:00 2001
d0480e
From: Eugene Syromyatnikov <evgsyr@gmail.com>
d0480e
Date: Fri, 1 Jul 2022 10:45:48 +0200
d0480e
Subject: [PATCH] secontext: fix expected SELinux context check for unlinked
d0480e
 FDs
d0480e
MIME-Version: 1.0
d0480e
Content-Type: text/plain; charset=UTF-8
d0480e
Content-Transfer-Encoding: 8bit
d0480e
d0480e
selinux_getfdcon open-coded a part of getfdpath_pid since it tries
d0480e
to do the same job, figure out a path associated with an FD, for slightly
d0480e
different purpose: to get the expected SELinux context for it.  As the previous
d0480e
commit shows, it's a bit more complicated in cases when the path ends
d0480e
with the " (deleted)" string, which is also used for designated unlinked paths
d0480e
in procfs.  Otherwise, it may manifest in test failures such as this:
d0480e
d0480e
     [unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023] fchmod(4</root/rpmbuild/BUILD/strace-5.13/tests/fchmod-y--secontext_full_mismatch.dir/fchmod_subdir/fchmod_sample_file> [unconfined_u:object_r:admin_home_t:s0!!system_u:object_r:admin_home_t:s0], 0600) = 0
d0480e
    -[unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023] fchmod(4</root/rpmbuild/BUILD/strace-5.13/tests/fchmod-y--secontext_full_mismatch.dir/fchmod_subdir/fchmod_sample_file (deleted)> [unconfined_u:object_r:admin_home_t:s0!!system_u:object_r:admin_home_t:s0], 051) = 0
d0480e
    -[unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023] fchmod(4</root/rpmbuild/BUILD/strace-5.13/tests/fchmod-y--secontext_full_mismatch.dir/fchmod_subdir/fchmod_sample_file (deleted)> [unconfined_u:object_r:admin_home_t:s0!!system_u:object_r:admin_home_t:s0], 004) = 0
d0480e
    +[unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023] fchmod(4</root/rpmbuild/BUILD/strace-5.13/tests/fchmod-y--secontext_full_mismatch.dir/fchmod_subdir/fchmod_sample_file (deleted)> [unconfined_u:object_r:admin_home_t:s0], 051) = 0
d0480e
    +[unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023] fchmod(4</root/rpmbuild/BUILD/strace-5.13/tests/fchmod-y--secontext_full_mismatch.dir/fchmod_subdir/fchmod_sample_file (deleted)> [unconfined_u:object_r:admin_home_t:s0], 004) = 0
d0480e
     +++ exited with 0 +++
d0480e
    + fail_ '../../src/strace -a15 -y --secontext=full,mismatch -e trace=fchmod ../fchmod-y--secontext_full_mismatch output mismatch'
d0480e
    + warn_ 'fchmod-y--secontext_full_mismatch.gen.test: failed test: ../../src/strace -a15 -y --secontext=full,mismatch -e trace=fchmod ../fchmod-y--secontext_full_mismatch output mismatch'
d0480e
    + printf '%s\n' 'fchmod-y--secontext_full_mismatch.gen.test: failed test: ../../src/strace -a15 -y --secontext=full,mismatch -e trace=fchmod ../fchmod-y--secontext_full_mismatch output mismatch'
d0480e
    fchmod-y--secontext_full_mismatch.gen.test: failed test: ../../src/strace -a15 -y --secontext=full,mismatch -e trace=fchmod ../fchmod-y--secontext_full_mismatch output mismatch
d0480e
    + exit 1
d0480e
    FAIL fchmod-y--secontext_full_mismatch.gen.test (exit status: 1)
d0480e
d0480e
that happens due to the fact that the get_expected_filecontext() call
d0480e
is made against the path with the " (deleted)" part, which is wrong (it
d0480e
is more wrong than shown above when a file with the path that ends with
d0480e
" (deleted)" exists).  Moreover, it would be incorrect to call stat()
d0480e
on that path.
d0480e
d0480e
Let's factor out the common part of the code and simply call it
d0480e
from selinux_getfdcon, then use the st_mode from the procfs link.
d0480e
d0480e
* src/defs.h (get_proc_pid_fd_path): New declaration.
d0480e
* src/pathtrace.c (get)proc_pid_fd_path): New function, part
d0480e
of getfdpath_pid that performs link resolution and processing
d0480e
of the result.
d0480e
(getfdpath_pid): Call get_proc_pid_fd_path after PID resolution.
d0480e
* src/secontext.c (get_expected_filecontext): Add mode parameter, use
d0480e
it in selabel_lookup call instead of retrieveing file mode using stat()
d0480e
if it is not -1.
d0480e
(selinux_getfdcon): Call get_proc_pid_fd_path instead
d0480e
of open-coding path resolution code, call stat() on the procfs link
d0480e
and pass the retrieved st_mode to the get_expected_filecontext call.
d0480e
(selinux_getfilecon): Pass -1 as mode in the get_expected_filecontext
d0480e
call.
d0480e
d0480e
Reported-by: Václav Kadlčík <vkadlcik@redhat.com>
d0480e
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2087693
d0480e
---
d0480e
 src/defs.h      | 15 +++++++++++++++
d0480e
 src/pathtrace.c | 26 ++++++++++++++++++--------
d0480e
 src/secontext.c | 35 +++++++++++++++++++++--------------
d0480e
 3 files changed, 54 insertions(+), 22 deletions(-)
d0480e
d0480e
Index: strace-5.18/src/defs.h
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/defs.h	2022-07-12 18:22:01.563254140 +0200
d0480e
+++ strace-5.18/src/defs.h	2022-07-12 18:22:06.202199392 +0200
d0480e
@@ -785,6 +785,21 @@
d0480e
 	return pathtrace_match_set(tcp, &global_path_set);
d0480e
 }
d0480e
 
d0480e
+/**
d0480e
+ * Resolves a path for a fd procfs PID proc_pid (the one got from
d0480e
+ * get_proc_pid()).
d0480e
+ *
d0480e
+ * @param proc_pid PID number in /proc, obtained with get_proc_pid().
d0480e
+ * @param fd       FD to resolve path for.
d0480e
+ * @param buf      Buffer to store the resolved path in.
d0480e
+ * @param bufsize  The size of buf.
d0480e
+ * @param deleted  If non-NULL, set to true if the path associated with the FD
d0480e
+ *                 seems to have been unlinked and to false otherwise.
d0480e
+ * @return         Number of bytes written including terminating '\0'.
d0480e
+ */
d0480e
+extern int get_proc_pid_fd_path(int proc_pid, int fd, char *buf,
d0480e
+				unsigned bufsize, bool *deleted);
d0480e
+
d0480e
 extern int getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize,
d0480e
 			 bool *deleted);
d0480e
 
d0480e
Index: strace-5.18/src/pathtrace.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/pathtrace.c	2022-07-12 18:22:01.532254506 +0200
d0480e
+++ strace-5.18/src/pathtrace.c	2022-07-12 18:22:06.202199392 +0200
d0480e
@@ -77,11 +77,9 @@
d0480e
 	set->paths_selected[set->num_selected++] = path;
d0480e
 }
d0480e
 
d0480e
-/*
d0480e
- * Get path associated with fd of a process with pid.
d0480e
- */
d0480e
 int
d0480e
-getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize, bool *deleted)
d0480e
+get_proc_pid_fd_path(int proc_pid, int fd, char *buf, unsigned bufsize,
d0480e
+		     bool *deleted)
d0480e
 {
d0480e
 	char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
d0480e
 	ssize_t n;
d0480e
@@ -89,10 +87,6 @@
d0480e
 	if (fd < 0)
d0480e
 		return -1;
d0480e
 
d0480e
-	int proc_pid = get_proc_pid(pid);
d0480e
-	if (!proc_pid)
d0480e
-		return -1;
d0480e
-
d0480e
 	xsprintf(linkpath, "/proc/%u/fd/%u", proc_pid, fd);
d0480e
 	n = readlink(linkpath, buf, bufsize - 1);
d0480e
 	if (n < 0)
d0480e
@@ -143,6 +137,22 @@
d0480e
 }
d0480e
 
d0480e
 /*
d0480e
+ * Get path associated with fd of a process with pid.
d0480e
+ */
d0480e
+int
d0480e
+getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize, bool *deleted)
d0480e
+{
d0480e
+	if (fd < 0)
d0480e
+		return -1;
d0480e
+
d0480e
+	int proc_pid = get_proc_pid(pid);
d0480e
+	if (!proc_pid)
d0480e
+		return -1;
d0480e
+
d0480e
+	return get_proc_pid_fd_path(proc_pid, fd, buf, bufsize, deleted);
d0480e
+}
d0480e
+
d0480e
+/*
d0480e
  * Add a path to the set we're tracing.  Also add the canonicalized
d0480e
  * version of the path.  Specifying NULL will delete all paths.
d0480e
  */
d0480e
Index: strace-5.18/src/secontext.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/secontext.c	2022-07-12 18:22:01.564254128 +0200
d0480e
+++ strace-5.18/src/secontext.c	2022-07-12 18:22:06.203199380 +0200
d0480e
@@ -62,7 +62,7 @@
d0480e
 }
d0480e
 
d0480e
 static int
d0480e
-get_expected_filecontext(const char *path, char **secontext)
d0480e
+get_expected_filecontext(const char *path, char **secontext, int mode)
d0480e
 {
d0480e
 	static struct selabel_handle *hdl;
d0480e
 
d0480e
@@ -80,12 +80,7 @@
d0480e
 		}
d0480e
 	}
d0480e
 
d0480e
-	strace_stat_t stb;
d0480e
-	if (stat_file(path, &stb) < 0) {
d0480e
-		return -1;
d0480e
-	}
d0480e
-
d0480e
-	return selabel_lookup(hdl, secontext, path, stb.st_mode);
d0480e
+	return selabel_lookup(hdl, secontext, path, mode);
d0480e
 }
d0480e
 
d0480e
 /*
d0480e
@@ -130,16 +125,22 @@
d0480e
 
d0480e
 	/*
d0480e
 	 * We need to resolve the path, because selabel_lookup() doesn't
d0480e
-	 * resolve anything.  Using readlink() is sufficient here.
d0480e
+	 * resolve anything.
d0480e
 	 */
d0480e
+	char buf[PATH_MAX + 1];
d0480e
+	ssize_t n = get_proc_pid_fd_path(proc_pid, fd, buf, sizeof(buf), NULL);
d0480e
+	if ((size_t) n >= (sizeof(buf) - 1))
d0480e
+		return 0;
d0480e
 
d0480e
-	char buf[PATH_MAX];
d0480e
-	ssize_t n = readlink(linkpath, buf, sizeof(buf));
d0480e
-	if ((size_t) n >= sizeof(buf))
d0480e
+	/*
d0480e
+	 * We retrieve stat() here since the path the procfs link resolves into
d0480e
+	 * may be reused by a different file with different context.
d0480e
+	 */
d0480e
+	strace_stat_t st;
d0480e
+	if (stat_file(linkpath, &st))
d0480e
 		return 0;
d0480e
-	buf[n] = '\0';
d0480e
 
d0480e
-	get_expected_filecontext(buf, expected);
d0480e
+	get_expected_filecontext(buf, expected, st.st_mode);
d0480e
 
d0480e
 	return 0;
d0480e
 }
d0480e
@@ -190,7 +191,13 @@
d0480e
 	if (!resolved)
d0480e
 		return 0;
d0480e
 
d0480e
-	get_expected_filecontext(resolved, expected);
d0480e
+	strace_stat_t st;
d0480e
+	if (stat_file(resolved, &st) < 0)
d0480e
+		goto out;
d0480e
+
d0480e
+	get_expected_filecontext(resolved, expected, st.st_mode);
d0480e
+
d0480e
+out:
d0480e
 	free(resolved);
d0480e
 
d0480e
 	return 0;