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