[PATCH] policycoreutils/setfiles: fix -f handling

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
Only trim a delimiter if we are consuming it; otherwise we may
truncate the last character of a file name being read if it is on the
final input line with no trailing newline.

Since the -f processing occurs after restore_init() has already
converted the r_opts flags to restorecon flags, we need to explicitly
set the restorecon flag when a mass relabel is triggered via a "/"
line.

Initialize buf_len for good measure; glibc ignores it when the line
pointer is NULL but we shouldn't rely on this.

Signed-off-by: Stephen Smalley <[email protected]>
---
 policycoreutils/setfiles/setfiles.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
index 2d38ea8f..e0953708 100644
--- a/policycoreutils/setfiles/setfiles.c
+++ b/policycoreutils/setfiles/setfiles.c
@@ -142,7 +142,7 @@ int main(int argc, char **argv)
 	const char *input_filename = NULL;
 	int use_input_file = 0;
 	char *buf = NULL, *endptr;
-	size_t buf_len, nthreads = 1;
+	size_t buf_len = 0, nthreads = 1;
 	const char *base;
 	int errors = 0;
 	const char *ropts = "ce:f:hiIDlmno:pqrsvFURW0xT:";
@@ -455,10 +455,14 @@ int main(int argc, char **argv)
 
 		delim = (null_terminated != 0) ? '\0' : '\n';
 		while ((len = getdelim(&buf, &buf_len, delim, f)) > 0) {
-			buf[len - 1] = '\0';
-			if (!strcmp(buf, "/"))
+			if (buf[len - 1] == delim)
+				buf[len - 1] = '\0';
+			if (!strcmp(buf, "/")) {
 				r_opts.mass_relabel =
 					SELINUX_RESTORECON_MASS_RELABEL;
+				r_opts.restorecon_flags |=
+					SELINUX_RESTORECON_MASS_RELABEL;
+			}
 			errors |= process_glob(buf, &r_opts, nthreads,
 					       &skipped_errors,
 					       &relabeled_files) < 0;
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.