Blame SOURCES/0032-restorecond-Fix-redundant-console-log-output-error.patch

9bbebd
From 76371721bafed56efcb7a83b3fa3285383ede5b7 Mon Sep 17 00:00:00 2001
9bbebd
From: Baichuan Kong <kongbaichuan@huawei.com>
9bbebd
Date: Thu, 14 Nov 2019 10:48:07 +0800
9bbebd
Subject: [PATCH] restorecond: Fix redundant console log output error
9bbebd
9bbebd
When starting restorecond without any option the following redundant
9bbebd
console log is outputed:
9bbebd
9bbebd
/dev/log 100.0%
9bbebd
/var/volatile/run/syslogd.pid 100.0%
9bbebd
...
9bbebd
9bbebd
This is caused by two global variables of same name r_opts. When
9bbebd
executes r_opts = opts in restore_init(), it originally intends
9bbebd
to assign the address of struct r_opts in "restorecond.c" to the
9bbebd
pointer *r_opts in "restore.c".
9bbebd
9bbebd
However, the address is assigned to the struct r_opts and covers
9bbebd
the value of low eight bytes in it. That causes unexpected value
9bbebd
of member varibale 'nochange' and 'verbose' in struct r_opts, thus
9bbebd
affects value of 'restorecon_flags' and executes unexpected operations
9bbebd
when restorecon the files such as the redundant console log output or
9bbebd
file label nochange.
9bbebd
9bbebd
Cause restorecond/restore.c is copied from policycoreutils/setfiles,
9bbebd
which share the same pattern. It also has potential risk to generate
9bbebd
same problems, So fix it in case.
9bbebd
9bbebd
Signed-off-by: Baichuan Kong <kongbaichuan@huawei.com>
9bbebd
9bbebd
(cherry-picked from SElinuxProject
9bbebd
commit ad2208ec220f55877a4d31084be2b4d6413ee082)
9bbebd
9bbebd
Resolves: rhbz#1626468
9bbebd
---
9bbebd
 policycoreutils/setfiles/restore.c | 42 ++++++++++++++----------------
9bbebd
 restorecond/restore.c              | 40 +++++++++++++---------------
9bbebd
 2 files changed, 37 insertions(+), 45 deletions(-)
9bbebd
9bbebd
diff --git a/policycoreutils/setfiles/restore.c b/policycoreutils/setfiles/restore.c
9bbebd
index 9dea5656..d3335d1a 100644
9bbebd
--- a/policycoreutils/setfiles/restore.c
9bbebd
+++ b/policycoreutils/setfiles/restore.c
9bbebd
@@ -17,40 +17,37 @@
9bbebd
 char **exclude_list;
9bbebd
 int exclude_count;
9bbebd
 
9bbebd
-struct restore_opts *r_opts;
9bbebd
-
9bbebd
 void restore_init(struct restore_opts *opts)
9bbebd
 {
9bbebd
 	int rc;
9bbebd
 
9bbebd
-	r_opts = opts;
9bbebd
 	struct selinux_opt selinux_opts[] = {
9bbebd
-		{ SELABEL_OPT_VALIDATE, r_opts->selabel_opt_validate },
9bbebd
-		{ SELABEL_OPT_PATH, r_opts->selabel_opt_path },
9bbebd
-		{ SELABEL_OPT_DIGEST, r_opts->selabel_opt_digest }
9bbebd
+		{ SELABEL_OPT_VALIDATE, opts->selabel_opt_validate },
9bbebd
+		{ SELABEL_OPT_PATH, opts->selabel_opt_path },
9bbebd
+		{ SELABEL_OPT_DIGEST, opts->selabel_opt_digest }
9bbebd
 	};
9bbebd
 
9bbebd
-	r_opts->hnd = selabel_open(SELABEL_CTX_FILE, selinux_opts, 3);
9bbebd
-	if (!r_opts->hnd) {
9bbebd
-		perror(r_opts->selabel_opt_path);
9bbebd
+	opts->hnd = selabel_open(SELABEL_CTX_FILE, selinux_opts, 3);
9bbebd
+	if (!opts->hnd) {
9bbebd
+		perror(opts->selabel_opt_path);
9bbebd
 		exit(1);
9bbebd
 	}
9bbebd
 
9bbebd
-	r_opts->restorecon_flags = 0;
9bbebd
-	r_opts->restorecon_flags = r_opts->nochange | r_opts->verbose |
9bbebd
-			   r_opts->progress | r_opts->set_specctx  |
9bbebd
-			   r_opts->add_assoc | r_opts->ignore_digest |
9bbebd
-			   r_opts->recurse | r_opts->userealpath |
9bbebd
-			   r_opts->xdev | r_opts->abort_on_error |
9bbebd
-			   r_opts->syslog_changes | r_opts->log_matches |
9bbebd
-			   r_opts->ignore_noent | r_opts->ignore_mounts |
9bbebd
-			   r_opts->mass_relabel;
9bbebd
+	opts->restorecon_flags = 0;
9bbebd
+	opts->restorecon_flags = opts->nochange | opts->verbose |
9bbebd
+			   opts->progress | opts->set_specctx  |
9bbebd
+			   opts->add_assoc | opts->ignore_digest |
9bbebd
+			   opts->recurse | opts->userealpath |
9bbebd
+			   opts->xdev | opts->abort_on_error |
9bbebd
+			   opts->syslog_changes | opts->log_matches |
9bbebd
+			   opts->ignore_noent | opts->ignore_mounts |
9bbebd
+			   opts->mass_relabel;
9bbebd
 
9bbebd
 	/* Use setfiles, restorecon and restorecond own handles */
9bbebd
-	selinux_restorecon_set_sehandle(r_opts->hnd);
9bbebd
+	selinux_restorecon_set_sehandle(opts->hnd);
9bbebd
 
9bbebd
-	if (r_opts->rootpath) {
9bbebd
-		rc = selinux_restorecon_set_alt_rootpath(r_opts->rootpath);
9bbebd
+	if (opts->rootpath) {
9bbebd
+		rc = selinux_restorecon_set_alt_rootpath(opts->rootpath);
9bbebd
 		if (rc) {
9bbebd
 			fprintf(stderr,
9bbebd
 				"selinux_restorecon_set_alt_rootpath error: %s.\n",
9bbebd
@@ -81,7 +78,6 @@ int process_glob(char *name, struct restore_opts *opts)
9bbebd
 	size_t i = 0;
9bbebd
 	int len, rc, errors;
9bbebd
 
9bbebd
-	r_opts = opts;
9bbebd
 	memset(&globbuf, 0, sizeof(globbuf));
9bbebd
 
9bbebd
 	errors = glob(name, GLOB_TILDE | GLOB_PERIOD |
9bbebd
@@ -96,7 +92,7 @@ int process_glob(char *name, struct restore_opts *opts)
9bbebd
 		if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0)
9bbebd
 			continue;
9bbebd
 		rc = selinux_restorecon(globbuf.gl_pathv[i],
9bbebd
-					r_opts->restorecon_flags);
9bbebd
+					opts->restorecon_flags);
9bbebd
 		if (rc < 0)
9bbebd
 			errors = rc;
9bbebd
 	}
9bbebd
diff --git a/restorecond/restore.c b/restorecond/restore.c
9bbebd
index f6e30001..b93b5fdb 100644
9bbebd
--- a/restorecond/restore.c
9bbebd
+++ b/restorecond/restore.c
9bbebd
@@ -12,39 +12,36 @@
9bbebd
 char **exclude_list;
9bbebd
 int exclude_count;
9bbebd
 
9bbebd
-struct restore_opts *r_opts;
9bbebd
-
9bbebd
 void restore_init(struct restore_opts *opts)
9bbebd
 {
9bbebd
 	int rc;
9bbebd
 
9bbebd
-	r_opts = opts;
9bbebd
 	struct selinux_opt selinux_opts[] = {
9bbebd
-		{ SELABEL_OPT_VALIDATE, r_opts->selabel_opt_validate },
9bbebd
-		{ SELABEL_OPT_PATH, r_opts->selabel_opt_path },
9bbebd
-		{ SELABEL_OPT_DIGEST, r_opts->selabel_opt_digest }
9bbebd
+		{ SELABEL_OPT_VALIDATE, opts->selabel_opt_validate },
9bbebd
+		{ SELABEL_OPT_PATH, opts->selabel_opt_path },
9bbebd
+		{ SELABEL_OPT_DIGEST, opts->selabel_opt_digest }
9bbebd
 	};
9bbebd
 
9bbebd
-	r_opts->hnd = selabel_open(SELABEL_CTX_FILE, selinux_opts, 3);
9bbebd
-	if (!r_opts->hnd) {
9bbebd
-		perror(r_opts->selabel_opt_path);
9bbebd
+	opts->hnd = selabel_open(SELABEL_CTX_FILE, selinux_opts, 3);
9bbebd
+	if (!opts->hnd) {
9bbebd
+		perror(opts->selabel_opt_path);
9bbebd
 		exit(1);
9bbebd
 	}
9bbebd
 
9bbebd
-	r_opts->restorecon_flags = 0;
9bbebd
-	r_opts->restorecon_flags = r_opts->nochange | r_opts->verbose |
9bbebd
-			   r_opts->progress | r_opts->set_specctx  |
9bbebd
-			   r_opts->add_assoc | r_opts->ignore_digest |
9bbebd
-			   r_opts->recurse | r_opts->userealpath |
9bbebd
-			   r_opts->xdev | r_opts->abort_on_error |
9bbebd
-			   r_opts->syslog_changes | r_opts->log_matches |
9bbebd
-			   r_opts->ignore_noent | r_opts->ignore_mounts;
9bbebd
+	opts->restorecon_flags = 0;
9bbebd
+	opts->restorecon_flags = opts->nochange | opts->verbose |
9bbebd
+			   opts->progress | opts->set_specctx  |
9bbebd
+			   opts->add_assoc | opts->ignore_digest |
9bbebd
+			   opts->recurse | opts->userealpath |
9bbebd
+			   opts->xdev | opts->abort_on_error |
9bbebd
+			   opts->syslog_changes | opts->log_matches |
9bbebd
+			   opts->ignore_noent | opts->ignore_mounts;
9bbebd
 
9bbebd
 	/* Use setfiles, restorecon and restorecond own handles */
9bbebd
-	selinux_restorecon_set_sehandle(r_opts->hnd);
9bbebd
+	selinux_restorecon_set_sehandle(opts->hnd);
9bbebd
 
9bbebd
-	if (r_opts->rootpath) {
9bbebd
-		rc = selinux_restorecon_set_alt_rootpath(r_opts->rootpath);
9bbebd
+	if (opts->rootpath) {
9bbebd
+		rc = selinux_restorecon_set_alt_rootpath(opts->rootpath);
9bbebd
 		if (rc) {
9bbebd
 			fprintf(stderr,
9bbebd
 				"selinux_restorecon_set_alt_rootpath error: %s.\n",
9bbebd
@@ -75,7 +72,6 @@ int process_glob(char *name, struct restore_opts *opts)
9bbebd
 	size_t i = 0;
9bbebd
 	int len, rc, errors;
9bbebd
 
9bbebd
-	r_opts = opts;
9bbebd
 	memset(&globbuf, 0, sizeof(globbuf));
9bbebd
 
9bbebd
 	errors = glob(name, GLOB_TILDE | GLOB_PERIOD |
9bbebd
@@ -90,7 +86,7 @@ int process_glob(char *name, struct restore_opts *opts)
9bbebd
 		if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0)
9bbebd
 			continue;
9bbebd
 		rc = selinux_restorecon(globbuf.gl_pathv[i],
9bbebd
-					r_opts->restorecon_flags);
9bbebd
+					opts->restorecon_flags);
9bbebd
 		if (rc < 0)
9bbebd
 			errors = rc;
9bbebd
 	}
9bbebd
-- 
9bbebd
2.21.0
9bbebd