Skip to contentSkip to conversation

restorecond: search fallback configuration roots for the watch lists

PATCH In review Stephen Smalley 1 participant 3 messages Sep 22 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 1/2 restorecond: search fallback configuration roots for the watch lists

Search the fallback configuration roots for the restorecond.conf and restorecond_user.conf configuration files. Resolve both at startup so the resolved path is used for both the initial read and the inotify watch. A path given with -f is used as-is. If the primary location is later created, restarting the daemon picks it up, matching the resolve-once behavior of the other stateless consumers.

These files live directly under /etc/selinux/ rather than under the active policy root, so the libselinux selinux_policy_open() search does not apply and a local list is used, mirroring semanage_conf_path() in libsemanage. The list can be overridden at build time via RESTORECOND_CONFDIRS.

---
 restorecond/restorecond.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
restorecond/restorecond.c +33 -2
index 6548634d..495bf197 100644
@@ -65,8 +65,32 @@
 const char *homedir;
 static int master_fd = -1;
 
-static const char *server_watch_file = "/etc/selinux/restorecond.conf";
-static const char *user_watch_file = "/etc/selinux/restorecond_user.conf";
+#ifndef RESTORECOND_CONFDIRS
+#define RESTORECOND_CONFDIRS "/etc/selinux/", "/usr/lib/selinux/"
+#endif
+
+static const char *resolve_watch_file(const char *name, char *out, size_t len)
+{
+	static const char *dirs[] = { RESTORECOND_CONFDIRS, NULL };
+	unsigned int i;
+
+	for (i = 0; dirs[i]; i++) {
+		snprintf(out, len, "%s%s", dirs[i], name);
+		if (access(out, R_OK) == 0)
+			return out;
+	}
+	/*
+	 * Nothing found: return the primary path so the eventual open()
+	 * error names the location an administrator would create.
+	 */
+	snprintf(out, len, "%s%s", dirs[0], name);
+	return out;
+}
+
+static char server_watch_path[PATH_MAX];
+static char user_watch_path[PATH_MAX];
+static const char *server_watch_file;
+static const char *user_watch_file;
 static const char *watch_file;
 struct restore_opts r_opts;
 
@@ -152,6 +176,13 @@ int main(int argc, char **argv)
 	if (is_selinux_enabled() != 1)
 		return 0;
 
+	server_watch_file = resolve_watch_file("restorecond.conf",
+					       server_watch_path,
+					       sizeof(server_watch_path));
+
+	user_watch_file = resolve_watch_file("restorecond_user.conf",
+					     user_watch_path,
+					     sizeof(user_watch_path));
 	watch_file = server_watch_file;
 
 	/* Set all options to zero/NULL except for ignore_noent, digest, and skip_multilink. */
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v2 2/2 policycoreutils/newrole: search fallback location for newrole_pam.conf

newrole_pam.conf is optional and lives directly under /etc/selinux, outside the active policy root. On a stateless install a vendor may ship it under /usr/lib/selinux; try that as a fallback so per-command PAM service mappings are honored there too.

The fallback list is a build-time constant (PAM_SERVICE_CONFIG_FALLBACKS) rather than a libselinux call for the same reason as restorecond: the file is per-installation, not per- policy-type, so selinux_policy_open() would not remap it.

---
v2 breaks the loop if (cfg) so that paths[i] still refers to the correct path for error messages in process_pam_config().

 policycoreutils/newrole/newrole.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
policycoreutils/newrole/newrole.c +14 -5
index 741d30a1..d1385142 100644
@@ -100,6 +100,9 @@
 
 #ifdef USE_PAM
 #define PAM_SERVICE_CONFIG "/etc/selinux/newrole_pam.conf"
+#ifndef PAM_SERVICE_CONFIG_FALLBACKS
+#define PAM_SERVICE_CONFIG_FALLBACKS "/usr/lib/selinux/newrole_pam.conf"
+#endif
 #endif
 
 #define DEFAULT_PATH "/usr/bin:/bin"
@@ -249,9 +252,8 @@ static int reqsymcmp(hashtab_t h __attribute__((unused)),
 static hashtab_t app_service_names = NULL;
 #define PAM_SERVICE_SLOTS 64
 
-static int process_pam_config(FILE *cfg)
+static int process_pam_config(const char *config_file_path, FILE *cfg)
 {
-	const char *config_file_path = PAM_SERVICE_CONFIG;
 	char *line_buf = NULL;
 	unsigned long lineno = 0;
 	size_t len = 0;
@@ -305,16 +307,23 @@ err:
  */
 static int read_pam_config(void)
 {
-	const char *config_file_path = PAM_SERVICE_CONFIG;
+	static const char *paths[] = { PAM_SERVICE_CONFIG,
+				       PAM_SERVICE_CONFIG_FALLBACKS, NULL };
 	FILE *cfg = NULL;
-	cfg = fopen(config_file_path, "r");
+	unsigned int i;
+
+	for (i = 0; paths[i]; i++) {
+		cfg = fopen(paths[i], "re");
+		if (cfg)
+			break;
+	}
 	if (!cfg)
 		return 0; /* This configuration is optional. */
 	app_service_names =
 		hashtab_create(reqsymhash, reqsymcmp, PAM_SERVICE_SLOTS);
 	if (!app_service_names)
 		goto err;
-	if (process_pam_config(cfg))
+	if (process_pam_config(paths[i], cfg))
 		goto err;
 	fclose(cfg);
 	return 0;
Signature · 1 line
2.55.0
author maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 8 lines · Stephen Smalley

Search the fallback configuration roots for the restorecond.conf and restorecond_user.conf configuration files. Resolve both at startup so the resolved path is used for both the initial read and the inotify watch. A path given with -f is used as-is. If the primary location is later created, restarting the daemon picks it up, matching the resolve-once behavior of the other stateless consumers.

Quoted 7 lines

These files live directly under /etc/selinux/ rather than under the active policy root, so the libselinux selinux_policy_open() search does not apply and a local list is used, mirroring semanage_conf_path() in libsemanage. The list can be overridden at build time via RESTORECOND_CONFDIRS.

This series is merged.