Skip to contentSkip to conversation

libsemanage: add ro-store-root and configuration-root search list

PATCH Superseded Stephen Smalley 1 participant 10 messages Sep 2 SELinux security module

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

v3 cover libsemanage: add ro-store-root and configuration-root search list

This series is the second half of addressing:
https://github.com/SELinuxProject/selinux/issues/33

The first half can be found at
https://lore.kernel.org/selinux/[email protected]/

This series adds a ro-store-root option to semanage.conf for defining a search list of one or more read-only store roots that are then searched for all read operations when there is no corresponding file under the writable store-root. By default there is no change in behavior.

This series adds a configuration-root search list to libsemanage so that it searches an ordered list of configuration roots for semanage.conf. This list can be overridden at build time via -DSEMANAGE_CONF_DIRS=. There is no change in behavior as long as semanage.conf is found in its default location under /etc/selinux; the fallbacks are only used when it is missing.

Stephen Smalley (9):
libsemanage: accept a list of read-only fallback store roots
libsemanage: add semanage_module_find_path() and use it for reads
libsemanage: list modules across every store root
libsemanage: read the active store through the fallback list
libsemanage: create the writable store skeleton on first transaction
libsemanage: create the store root when taking a lock
libsemanage: refuse to remove or enable a module owned by a RO store
libsemanage: search /run and /usr/lib for semanage.conf
libsemanage: document semanage.conf search path and ro-store-root

 libsemanage/man/man5/semanage.conf.5 |  36 ++-
 libsemanage/src/conf-parse.y         |  57 +++-
 libsemanage/src/conf-scan.l          |   1 +
 libsemanage/src/direct_api.c         | 458 +++++++++++++++++----------
 libsemanage/src/modules.c            |  63 +++-
 libsemanage/src/modules.h            |  10 +
 libsemanage/src/semanage_conf.h      |   2 +
 libsemanage/src/semanage_store.c     | 185 +++++++++--
 libsemanage/src/semanage_store.h     |   6 +
 9 files changed, 611 insertions(+), 207 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v3 1/9 libsemanage: accept a list of read-only fallback store roots

Add a semanage.conf option:

    ro-store-root = /usr/lib/selinux[:/another/root...]

naming one or more read-only module-store roots to search after store-root when reading. Only the ACTIVE tree of a fallback root is used - TMP/PREVIOUS and both lock files stay in the writable store. With no ro-store-root the behavior is unchanged.

New helper functions:

semanage_ro_root_count() - number of RO roots semanage_ro_active_path(i, X) - <ro_root_i>/<type>/active/<X> semanage_path_read(store, X) - for ACTIVE, first-existing across
[store-root, ro-store-root...]; for TMP/PREVIOUS, semanage_path()

The subsequent patches switch the ACTIVE readers to semanage_path_read(), teach module enumeration to union across the roots, and stop the sandbox from copying anything but the writable ACTIVE, so /var (or /etc, depending on store-root configuration) holds only the admin delta over a vendor /usr/lib store.

---
 libsemanage/src/conf-parse.y     | 57 +++++++++++++++++++++++-
 libsemanage/src/conf-scan.l      |  1 +
 libsemanage/src/semanage_conf.h  |  2 +
 libsemanage/src/semanage_store.c | 76 ++++++++++++++++++++++++++++++++
 libsemanage/src/semanage_store.h |  6 +++
 5 files changed, 141 insertions(+), 1 deletion(-)
libsemanage/src/conf-parse.y +56 -1
index dee17b2f..264ccf70 100644
@@ -43,6 +43,7 @@ extern char *semanage_text;
 
 static int parse_module_store(char *arg);
 static int parse_store_root_path(char *arg);
+static int parse_ro_store_root_paths(char *arg);
 static int parse_compiler_path(char *arg);
 static void semanage_conf_external_prog_destroy(external_prog_t *ep);
 static int new_external_prog(external_prog_t **chain);
@@ -62,7 +63,7 @@ static int parse_errors;
         char *s;
 }
 
-%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT OPTIMIZE_POLICY MULTIPLE_DECLS
+%token MODULE_STORE VERSION EXPAND_CHECK FILE_MODE SAVE_PREVIOUS SAVE_LINKED TARGET_PLATFORM COMPILER_DIR IGNORE_MODULE_CACHE STORE_ROOT RO_STORE_ROOT OPTIMIZE_POLICY MULTIPLE_DECLS
 %token LOAD_POLICY_START SETFILES_START SEFCONTEXT_COMPILE_START DISABLE_GENHOMEDIRCON HANDLE_UNKNOWN USEPASSWD IGNOREDIRS
 %token BZIP_BLOCKSIZE BZIP_SMALL RELABEL_STORE REMOVE_HLL
 %token VERIFY_MOD_START VERIFY_LINKED_START VERIFY_KERNEL_START BLOCK_END
@@ -85,6 +86,7 @@ single_opt:     module_store
         |       version
         |       target_platform
         |       store_root
+        |       ro_store_root
         |       compiler_dir
         |       ignore_module_cache
         |       expand_check
@@ -122,6 +124,15 @@ store_root:     STORE_ROOT '=' ARG  {
                 }
         ;
 
+ro_store_root:  RO_STORE_ROOT '=' ARG  {
+                        if (parse_ro_store_root_paths($3) != 0) {
+                                parse_errors++;
+                                YYABORT;
+                        }
+                        free($3);
+                }
+        ;
+
 compiler_dir:       COMPILER_DIR '=' ARG  {
                         if (parse_compiler_path($3) != 0) {
                                 parse_errors++;
@@ -534,6 +545,9 @@ void semanage_conf_destroy(semanage_conf_t * conf)
 		free(conf->store_path);
 		free(conf->ignoredirs);
 		free(conf->store_root_path);
+		while (conf->n_ro_store_roots)
+			free(conf->ro_store_root_paths[--conf->n_ro_store_roots]);
+		free(conf->ro_store_root_paths);
 		free(conf->compiler_directory_path);
 		semanage_conf_external_prog_destroy(conf->load_policy);
 		semanage_conf_external_prog_destroy(conf->setfiles);
@@ -612,6 +626,47 @@ static int parse_store_root_path(char *arg)
 	return 0;
 }
 
+/* ro-store-root = /usr/lib/selinux[:/another/root...] */
+static int parse_ro_store_root_paths(char *arg)
+{
+	char *p, *saveptr = NULL;
+	unsigned int n = 0;
+	char **v;
+
+	if (arg == NULL) {
+		return -1;
+	}
+
+	for (p = arg; *p; p++)
+		if (*p == ':')
+			n++;
+	n++;
+
+	v = calloc(n, sizeof(*v));
+	if (!v)
+		return -1;
+
+	n = 0;
+	for (p = strtok_r(arg, ":", &saveptr); p;
+	     p = strtok_r(NULL, ":", &saveptr)) {
+		v[n] = strdup(p);
+		if (!v[n]) {
+			while (n)
+				free(v[--n]);
+			free(v);
+			return -1;
+		}
+		n++;
+	}
+
+	while (current_conf->n_ro_store_roots)
+		free(current_conf->ro_store_root_paths[--current_conf->n_ro_store_roots]);
+	free(current_conf->ro_store_root_paths);
+	current_conf->ro_store_root_paths = v;
+	current_conf->n_ro_store_roots = n;
+	return 0;
+}
+
 static int parse_compiler_path(char *arg)
 {
 	if (arg == NULL) {
libsemanage/src/conf-scan.l +1 -0
index c592fb63..d3f74b66 100644
@@ -39,6 +39,7 @@ static char *my_qstrdup (char * s);
 #.*               /* ignore comments */
 module-store      return MODULE_STORE;
 store-root        return STORE_ROOT;
+ro-store-root     return RO_STORE_ROOT;
 compiler-directory	return COMPILER_DIR;
 ignore-module-cache return IGNORE_MODULE_CACHE;
 policy-version    return VERSION;
libsemanage/src/semanage_conf.h +2 -0
index 5a24ea87..b2059962 100644
@@ -57,6 +57,8 @@ typedef struct semanage_conf {
 	struct external_prog *sefcontext_compile;
 	struct external_prog *mod_prog, *linked_prog, *kernel_prog;
 	char *store_root_path;
+	char **ro_store_root_paths;
+	unsigned int n_ro_store_roots;
 } semanage_conf_t;
 
 /* A linked list of verification programs.  Each one is called in
libsemanage/src/semanage_store.c +76 -0

100 lines not on this page · raw message

libsemanage/src/semanage_store.h +6 -0
index 86ae0936..36cef62c 100644
@@ -101,6 +101,12 @@ extern const char *semanage_fname(enum semanage_sandbox_defs file_enum);
 extern const char *semanage_path(enum semanage_store_defs store,
 				 enum semanage_sandbox_defs file);
 
+extern unsigned int semanage_ro_root_count(void);
+extern const char *semanage_ro_active_path(unsigned int idx,
+					   enum semanage_sandbox_defs file);
+extern const char *semanage_path_read(enum semanage_store_defs store,
+				      enum semanage_sandbox_defs file);
+
 extern const char *semanage_final_path(enum semanage_final_defs root,
 				       enum semanage_final_path_defs suffix);
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH v3 2/9 libsemanage: add semanage_module_find_path() and use it for reads

In reply to Stephen Smalley

Add a semanage_module_find_path() helper which tries the writable modules root (TMP in a transaction, ACTIVE otherwise) and then each ro-store-root ACTIVE modules root, filling @path with the first location that exists. If the file doesn't exist anywhere, leave the writable-root path in @path so error messages refer to the write target. semanage_module_get_path() itself is unchanged and remains the write-side accessor.

Convert the read-side callers to use semanage_module_find_path(). With no ro-store-root configured this is a single extra access() per call.

semanage_compile_module() may now read a HLL from a read-only root and write the compiled CIL into the writable sandbox; guard the remove-hll unlink so it only happens when the HLL actually lives in the sandbox.

---
 libsemanage/src/direct_api.c     | 53 ++++++++++++++++-----------
 libsemanage/src/modules.c        | 63 +++++++++++++++++++++++++++-----
 libsemanage/src/modules.h        | 10 +++++
 libsemanage/src/semanage_store.c |  6 +--
 4 files changed, 99 insertions(+), 33 deletions(-)
libsemanage/src/direct_api.c +32 -21
index 132e2a2a..ded6bb9e 100644
@@ -998,8 +998,9 @@ static int semanage_compile_module(semanage_handle_t *sh,
 		goto cleanup;
 	}
 
-	status = semanage_module_get_path(sh, modinfo, SEMANAGE_MODULE_PATH_HLL,
-					  hll_path, sizeof(hll_path));
+	status = semanage_module_find_path(sh, modinfo,
+					   SEMANAGE_MODULE_PATH_HLL, hll_path,
+					   sizeof(hll_path));
 	if (status != 0) {
 		goto cleanup;
 	}
@@ -1050,15 +1051,25 @@ static int semanage_compile_module(semanage_handle_t *sh,
 	}
 
 	if (sh->conf->remove_hll) {
-		status = unlink(hll_path);
-		if (status != 0) {
-			ERR(sh, "Error while removing HLL file %s.", hll_path);
-			goto cleanup;
-		}
+		const char *rw = semanage_path(SEMANAGE_TMP, SEMANAGE_MODULES);
 
-		status = semanage_direct_write_langext(sh, "cil", modinfo);
-		if (status != 0) {
-			goto cleanup;
+		/*
+		 * Only remove a HLL that lives in the writable sandbox;
+		 * a ro-store-root vendor module keeps its HLL.
+		 */
+		if (strncmp(hll_path, rw, strlen(rw)) == 0) {
+			status = unlink(hll_path);
+			if (status != 0) {
+				ERR(sh, "Error while removing HLL file %s.",
+				    hll_path);
+				goto cleanup;
+			}
+
+			status = semanage_direct_write_langext(sh, "cil",
+							       modinfo);
+			if (status != 0) {
+				goto cleanup;
+			}
 		}
 	}
 
@@ -1118,9 +1129,9 @@ semanage_compile_hll_modules(semanage_handle_t *sh,
 	/* prefix with module count to avoid collisions */
 	update_checksum_with_len(&context, num_modinfos);
 	for (i = 0; i < num_modinfos; i++) {
-		status = semanage_module_get_path(sh, &modinfos[i],
-						  SEMANAGE_MODULE_PATH_CIL,
-						  cil_path, sizeof(cil_path));
+		status = semanage_module_find_path(sh, &modinfos[i],
+						   SEMANAGE_MODULE_PATH_CIL,
+						   cil_path, sizeof(cil_path));
 		if (status != 0)
 			return -1;
 
@@ -1932,10 +1943,10 @@ static int semanage_direct_extract(semanage_handle_t *sh,
 	struct file_contents contents = {};
 
 	/* get path of module */
-	rc = semanage_module_get_path(sh,
-				      (const semanage_module_info_t *)modkey,
-				      SEMANAGE_MODULE_PATH_NAME, module_path,
-				      sizeof(module_path));
+	rc = semanage_module_find_path(sh,
+				       (const semanage_module_info_t *)modkey,
+				       SEMANAGE_MODULE_PATH_NAME, module_path,
+				       sizeof(module_path));
 	if (rc != 0) {
 		goto cleanup;
 	}
@@ -1958,8 +1969,8 @@ static int semanage_direct_extract(semanage_handle_t *sh,
 	}
 
 	/* get path of what to extract */
-	rc = semanage_module_get_path(sh, _modinfo, file_type, input_file,
-				      sizeof(input_file));
+	rc = semanage_module_find_path(sh, _modinfo, file_type, input_file,
+				       sizeof(input_file));
 	if (rc != 0) {
 		goto cleanup;
 	}
@@ -2377,7 +2388,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 	}
 
 	/* lookup module ext */
-	ret = semanage_module_get_path(
+	ret = semanage_module_find_path(
 		sh, *modinfo, SEMANAGE_MODULE_PATH_LANG_EXT, fn, sizeof(fn));
 	if (ret != 0) {
 		status = -1;
@@ -2420,7 +2431,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
 	fp = NULL;
 
 	/* lookup enabled/disabled status */
-	ret = semanage_module_get_path(
+	ret = semanage_module_find_path(
 		sh, *modinfo, SEMANAGE_MODULE_PATH_DISABLED, fn, sizeof(fn));
 	if (ret != 0) {
 		status = -1;
libsemanage/src/modules.c +54 -9

83 lines not on this page · raw message

libsemanage/src/modules.h +10 -0
index fdd771a4..22e6803e 100644
@@ -100,6 +100,16 @@ int semanage_module_get_path(semanage_handle_t *sh,
 			     enum semanage_module_path_type type, char *path,
 			     size_t len);
 
+/*
+ * Read-side variant: fill @path with the first location that exists
+ * across the writable and any ro-store-root modules roots. Falls back
+ * to the writable-root path when the file is not found anywhere.
+ */
+int semanage_module_find_path(semanage_handle_t *sh,
+			      const semanage_module_info_t *modinfo,
+			      enum semanage_module_path_type type, char *path,
+			      size_t len);
+
 extern const size_t CHECKSUM_CONTENT_SIZE;
 void semanage_hash_to_checksum_string(const uint8_t *hash, char *checksum);
 
libsemanage/src/semanage_store.c +3 -3
index 187fd6b6..a9b471a2 100644
@@ -1195,9 +1195,9 @@ int semanage_get_cil_paths(semanage_handle_t *sh,
 	}
 
 	for (i = 0; i < num_modinfos; i++) {
-		ret = semanage_module_get_path(sh, &modinfos[i],
-					       SEMANAGE_MODULE_PATH_CIL, path,
-					       sizeof(path));
+		ret = semanage_module_find_path(sh, &modinfos[i],
+						SEMANAGE_MODULE_PATH_CIL, path,
+						sizeof(path));
 		if (ret != 0) {
 			status = -1;
 			goto cleanup;
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH v3 3/9 libsemanage: list modules across every store root

In reply to Stephen Smalley

semanage_direct_list_all() now scans the writable modules root (TMP in a transaction, ACTIVE otherwise) and each ro-store-root ACTIVE modules root, collecting every (priority, name) pair. The result is sorted by (priority descending, name ascending) so that semanage_get_active_modules() still picks the highest priority one, and adjacent duplicates are collapsed.

For each unique pair, semanage_direct_get_module_info() reads lang_ext and the disabled marker via semanage_module_find_path(), so a module present only in a read-only root is fully described, and a writable- store copy at the same priority/name shadows a fallback.

An ENOENT from any root's modules directory is treated as an empty contribution rather than a hard error, so a fresh /var or /etc with no active/ tree does not break 'semodule -l'.

---
 libsemanage/src/direct_api.c | 293 ++++++++++++++++++++---------------
 1 file changed, 168 insertions(+), 125 deletions(-)
libsemanage/src/direct_api.c +168 -125
index ded6bb9e..dae9d003 100644
@@ -2622,6 +2622,105 @@ static int semanage_modules_filename_select(const struct dirent *d)
 	return 1;
 }
 
+struct raw_module {
+	uint16_t priority;
+	char *name;
+};
+
+static int raw_module_cmp(const void *a, const void *b)
+{
+	const struct raw_module *ra = a, *rb = b;
+
+	if (ra->priority != rb->priority)
+		return (int)rb->priority - (int)ra->priority;
+	return strcmp(ra->name, rb->name);
+}
+
+static void free_scandir_result(struct dirent **entries, int n)
+{
+	int i;
+
+	for (i = 0; i < n; i++)
+		free(entries[i]);
+	free(entries);
+}
+
+static int semanage_scan_modules_root(semanage_handle_t *sh,
+				      const char *toplevel,
+				      struct raw_module **raw, int *raw_len)
+{
+	struct dirent **priorities = NULL, **modules = NULL;
+	int priorities_len, modules_len;
+	char priority_path[PATH_MAX];
+	uint16_t priority;
+	int i, j, status = 0;
+	void *tmp;
+
+	priorities_len = scandir(toplevel, &priorities,
+				 semanage_priorities_filename_select,
+				 versionsort);
+	if (priorities_len < 0) {
+		if (errno == ENOENT)
+			return 0;
+		ERR(sh, "Error while scanning directory %s.", toplevel);
+		return -1;
+	}
+
+	for (i = 0; i < priorities_len; i++) {
+		if (semanage_string_to_priority(priorities[i]->d_name,
+						&priority)) {
+			status = -1;
+			break;
+		}
+
+		snprintf(priority_path, sizeof(priority_path), "%s/%s",
+			 toplevel, priorities[i]->d_name);
+		modules_len = scandir(priority_path, &modules,
+				      semanage_modules_filename_select,
+				      versionsort);
+		if (modules_len < 0) {
+			ERR(sh, "Error while scanning directory %s.",
+			    priority_path);
+			status = -1;
+			break;
+		}
+		if (modules_len == 0) {
+			free(modules);
+			modules = NULL;
+			continue;
+		}
+
+		tmp = realloc(*raw,
+			      sizeof(**raw) * ((size_t)*raw_len + modules_len));
+		if (tmp == NULL) {
+			ERR(sh, "Error allocating memory for module list.");
+			free_scandir_result(modules, modules_len);
+			status = -1;
+			break;
+		}
+		*raw = tmp;
+
+		for (j = 0; j < modules_len; j++) {
+			(*raw)[*raw_len].priority = priority;
+			(*raw)[*raw_len].name = strdup(modules[j]->d_name);
+			if ((*raw)[*raw_len].name == NULL) {
+				ERR(sh, "Out of memory.");
+				status = -1;
+				break;
+			}
+			(*raw_len)++;
+		}
+
+		free_scandir_result(modules, modules_len);
+		modules = NULL;
+		if (status)
+			break;
+	}
+
+	free_scandir_result(priorities, priorities_len);
+	return status;
+}
+
 static int semanage_direct_list_all(semanage_handle_t *sh,
 				    semanage_module_info_t **modinfos,
 				    int *modinfos_len)
@@ -2634,22 +2733,13 @@ static int semanage_direct_list_all(semanage_handle_t *sh,
 	int ret = 0;
 
 	int i = 0;
-	int j = 0;
+	unsigned int r;
+
+	struct raw_module *raw = NULL;
+	int raw_len = 0;
 
 	*modinfos = NULL;
 	*modinfos_len = 0;
-	void *tmp = NULL;
-
-	const char *toplevel = NULL;
-
-	struct dirent **priorities = NULL;
-	int priorities_len = 0;
-	char priority_path[PATH_MAX];
-
-	struct dirent **modules = NULL;
-	int modules_len = 0;
-
-	uint16_t priority = 0;
 
 	semanage_module_info_t *modinfo_tmp = NULL;
 
@@ -2660,158 +2750,111 @@ static int semanage_direct_list_all(semanage_handle_t *sh,
 		goto cleanup;
 	}
 
-	if (sh->is_in_transaction) {
-		toplevel = semanage_path(SEMANAGE_TMP, SEMANAGE_MODULES);
-	} else {
-		toplevel = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);
+	/*
+	 * Collect (priority, name) from the writable store and every
+	 * read-only fallback root. A missing modules directory in
+	 * any root contributes nothing rather than failing the listing.
+	 */
+	status = semanage_scan_modules_root(
+		sh,
+		semanage_path(sh->is_in_transaction ? SEMANAGE_TMP :
+						      SEMANAGE_ACTIVE,
+			      SEMANAGE_MODULES),
+		&raw, &raw_len);
+	if (status)
+		goto cleanup;
+
+	for (r = 0; r < semanage_ro_root_count(); r++) {
+		status = semanage_scan_modules_root(
+			sh, semanage_ro_active_path(r, SEMANAGE_MODULES), &raw,
+			&raw_len);
+		if (status)
+			goto cleanup;
 	}
 
-	/* find priorities */
-	priorities_len = scandir(toplevel, &priorities,
-				 semanage_priorities_filename_select,
-				 versionsort);
-	if (priorities_len == -1) {
-		ERR(sh, "Error while scanning directory %s.", toplevel);
+	if (raw_len == 0)
+		goto cleanup;
+
+	/*
+	 * Sort by (priority descending, name ascending) so
+	 * semanage_get_active_modules() still sees the highest priority
+	 * first for each name. Duplicates are adjacent after the sort;
+	 * get_module_info() resolve them to the first-existing root via
+	 * semanage_module_find_path(), so which duplicate we keep is
+	 * immaterial.
+	 */
+	qsort(raw, raw_len, sizeof(*raw), raw_module_cmp);
+
+	*modinfos = calloc(raw_len, sizeof(**modinfos));
+	if (*modinfos == NULL) {
+		ERR(sh, "Error allocating memory for module array.");
 		status = -1;
 		goto cleanup;
 	}
 
-	/* for each priority directory */
-	/* loop through in reverse so that highest priority is first */
-	for (i = priorities_len - 1; i >= 0; i--) {
-		/* convert priority string to uint16_t */
-		ret = semanage_string_to_priority(priorities[i]->d_name,
-						  &priority);
+	for (i = 0; i < raw_len; i++) {
+		if (i > 0 && raw[i].priority == raw[i - 1].priority &&
+		    strcmp(raw[i].name, raw[i - 1].name) == 0)
+			continue;
+
+		ret = semanage_module_info_set_priority(sh, &modinfo,
+							raw[i].priority);
 		if (ret != 0) {
 			status = -1;
 			goto cleanup;
 		}
 
-		/* set our priority */
-		ret = semanage_module_info_set_priority(sh, &modinfo, priority);
+		ret = semanage_module_info_set_name(sh, &modinfo, raw[i].name);
 		if (ret != 0) {
 			status = -1;
 			goto cleanup;
 		}
 
-		/* get the priority path */
-		ret = semanage_module_get_path(sh, &modinfo,
-					       SEMANAGE_MODULE_PATH_PRIORITY,
-					       priority_path,
-					       sizeof(priority_path));
+		ret = semanage_direct_get_module_info(
+			sh, (const semanage_module_key_t *)(&modinfo),
+			&modinfo_tmp);
 		if (ret != 0) {
 			status = -1;
 			goto cleanup;
 		}
 
-		/* cleanup old modules */
-		if (modules != NULL) {
-			for (j = 0; j < modules_len; j++) {
-				free(modules[j]);
-				modules[j] = NULL;
-			}
-			free(modules);
-			modules = NULL;
-			modules_len = 0;
-		}
-
-		/* find modules at this priority */
-		modules_len = scandir(priority_path, &modules,
-				      semanage_modules_filename_select,
-				      versionsort);
-		if (modules_len == -1) {
-			ERR(sh, "Error while scanning directory %s.",
-			    priority_path);
+		ret = semanage_module_info_init(sh,
+						&((*modinfos)[*modinfos_len]));
+		if (ret != 0) {
 			status = -1;
 			goto cleanup;
 		}
 
-		if (modules_len == 0)
-			continue;
-
-		/* add space for modules */
-		tmp = realloc(*modinfos, sizeof(semanage_module_info_t) *
-						 (*modinfos_len + modules_len));
-		if (tmp == NULL) {
-			ERR(sh, "Error allocating memory for module array.");
+		ret = semanage_module_info_clone(sh, modinfo_tmp,
+						 &((*modinfos)[*modinfos_len]));
+		if (ret != 0) {
 			status = -1;
 			goto cleanup;
 		}
-		*modinfos = tmp;
 
-		/* for each module directory */
-		for (j = 0; j < modules_len; j++) {
-			/* set module name */
-			ret = semanage_module_info_set_name(sh, &modinfo,
-							    modules[j]->d_name);
-			if (ret != 0) {
-				status = -1;
-				goto cleanup;
-			}
+		semanage_module_info_destroy(sh, modinfo_tmp);
+		free(modinfo_tmp);
+		modinfo_tmp = NULL;
 
-			/* get module values */
-			ret = semanage_direct_get_module_info(
-				sh, (const semanage_module_key_t *)(&modinfo),
-				&modinfo_tmp);
-			if (ret != 0) {
-				status = -1;
-				goto cleanup;
-			}
-
-			/* copy into array */
-			ret = semanage_module_info_init(
-				sh, &((*modinfos)[*modinfos_len]));
-			if (ret != 0) {
-				status = -1;
-				goto cleanup;
-			}
-
-			ret = semanage_module_info_clone(
-				sh, modinfo_tmp, &((*modinfos)[*modinfos_len]));
-			if (ret != 0) {
-				status = -1;
-				goto cleanup;
-			}
-
-			semanage_module_info_destroy(sh, modinfo_tmp);
-			free(modinfo_tmp);
-			modinfo_tmp = NULL;
-
-			*modinfos_len += 1;
-		}
+		*modinfos_len += 1;
 	}
 
 cleanup:
 	semanage_module_info_destroy(sh, &modinfo);
 
-	if (priorities != NULL) {
-		for (i = 0; i < priorities_len; i++) {
-			free(priorities[i]);
-		}
-		free(priorities);
-	}
-
-	if (modules != NULL) {
-		for (i = 0; i < modules_len; i++) {
-			free(modules[i]);
-		}
-		free(modules);
-	}
+	for (i = 0; i < raw_len; i++)
+		free(raw[i].name);
+	free(raw);
 
 	semanage_module_info_destroy(sh, modinfo_tmp);
 	free(modinfo_tmp);
-	modinfo_tmp = NULL;
 
 	if (status != 0) {
-		if (modinfos != NULL) {
-			for (i = 0; i < *modinfos_len; i++) {
-				semanage_module_info_destroy(sh,
-							     &(*modinfos)[i]);
-			}
-			free(*modinfos);
-			*modinfos = NULL;
-			*modinfos_len = 0;
-		}
+		for (i = 0; i < *modinfos_len; i++)
+			semanage_module_info_destroy(sh, &(*modinfos)[i]);
+		free(*modinfos);
+		*modinfos = NULL;
+		*modinfos_len = 0;
 	}
 
 	return status;
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH v3 4/9 libsemanage: read the active store through the fallback list

In reply to Stephen Smalley

The file-backed dbase objects semanage_direct_connect() sets up take a read path (used by semanage <object> -l and by the modify path to seed the transaction) and a write path (always in the sandbox). Update the read side to use semanage_path_read() so a missing /var/lib/selinux/<type>/active/<file> falls back to the corresponding path in a ro-store-root.

Do the same for the disable_dontaudit check, semanage_read_policydb() (policydb-based dbases), the commit_num read in semanage_direct_get_serial(), and the read-access check in semanage_store_access_check(); the write-access and lock checks in the latter still target the writable store and are handled separately.

---
 libsemanage/src/direct_api.c     | 59 ++++++++++++++++++++------------
 libsemanage/src/semanage_store.c |  9 ++---
 2 files changed, 42 insertions(+), 26 deletions(-)
libsemanage/src/direct_api.c +37 -22
index dae9d003..a930dac1 100644
@@ -163,14 +163,16 @@ int semanage_direct_connect(semanage_handle_t *sh)
 	/* Object databases: local modifications */
 	if (user_base_file_dbase_init(
 		    sh,
-		    semanage_path(SEMANAGE_ACTIVE, SEMANAGE_USERS_BASE_LOCAL),
+		    semanage_path_read(SEMANAGE_ACTIVE,
+				       SEMANAGE_USERS_BASE_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_BASE_LOCAL),
 		    semanage_user_base_dbase_local(sh)) < 0)
 		goto err;
 
 	if (user_extra_file_dbase_init(
 		    sh,
-		    semanage_path(SEMANAGE_ACTIVE, SEMANAGE_USERS_EXTRA_LOCAL),
+		    semanage_path_read(SEMANAGE_ACTIVE,
+				       SEMANAGE_USERS_EXTRA_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LOCAL),
 		    semanage_user_extra_dbase_local(sh)) < 0)
 		goto err;
@@ -181,58 +183,69 @@ int semanage_direct_connect(semanage_handle_t *sh)
 		goto err;
 
 	if (port_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_PORTS_LOCAL),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_PORTS_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_PORTS_LOCAL),
 		    semanage_port_dbase_local(sh)) < 0)
 		goto err;
 
-	if (iface_file_dbase_init(
-		    sh,
-		    semanage_path(SEMANAGE_ACTIVE, SEMANAGE_INTERFACES_LOCAL),
-		    semanage_path(SEMANAGE_TMP, SEMANAGE_INTERFACES_LOCAL),
-		    semanage_iface_dbase_local(sh)) < 0)
+	if (iface_file_dbase_init(sh,
+				  semanage_path_read(SEMANAGE_ACTIVE,
+						     SEMANAGE_INTERFACES_LOCAL),
+				  semanage_path(SEMANAGE_TMP,
+						SEMANAGE_INTERFACES_LOCAL),
+				  semanage_iface_dbase_local(sh)) < 0)
 		goto err;
 
-	if (bool_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_BOOLEANS_LOCAL),
-		    semanage_path(SEMANAGE_TMP, SEMANAGE_BOOLEANS_LOCAL),
-		    semanage_bool_dbase_local(sh)) < 0)
+	if (bool_file_dbase_init(sh,
+				 semanage_path_read(SEMANAGE_ACTIVE,
+						    SEMANAGE_BOOLEANS_LOCAL),
+				 semanage_path(SEMANAGE_TMP,
+					       SEMANAGE_BOOLEANS_LOCAL),
+				 semanage_bool_dbase_local(sh)) < 0)
 		goto err;
 
 	if (fcontext_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC_LOCAL),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE,
+				       SEMANAGE_STORE_FC_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
 		    semanage_fcontext_dbase_local(sh)) < 0)
 		goto err;
 
 	if (fcontext_file_dbase_init(
 		    sh,
-		    semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC_HOMEDIRS),
+		    semanage_path_read(SEMANAGE_ACTIVE,
+				       SEMANAGE_STORE_FC_HOMEDIRS),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_HOMEDIRS),
 		    semanage_fcontext_dbase_homedirs(sh)) < 0)
 		goto err;
 
 	if (seuser_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_SEUSERS_LOCAL),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_SEUSERS_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LOCAL),
 		    semanage_seuser_dbase_local(sh)) < 0)
 		goto err;
 
 	if (node_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_NODES_LOCAL),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_NODES_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_NODES_LOCAL),
 		    semanage_node_dbase_local(sh)) < 0)
 		goto err;
 
 	if (ibpkey_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_IBPKEYS_LOCAL),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_IBPKEYS_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_IBPKEYS_LOCAL),
 		    semanage_ibpkey_dbase_local(sh)) < 0)
 		goto err;
 
 	if (ibendport_file_dbase_init(
 		    sh,
-		    semanage_path(SEMANAGE_ACTIVE, SEMANAGE_IBENDPORTS_LOCAL),
+		    semanage_path_read(SEMANAGE_ACTIVE,
+				       SEMANAGE_IBENDPORTS_LOCAL),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_IBENDPORTS_LOCAL),
 		    semanage_ibendport_dbase_local(sh)) < 0)
 		goto err;
@@ -243,7 +256,8 @@ int semanage_direct_connect(semanage_handle_t *sh)
 		goto err;
 
 	if (user_extra_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_USERS_EXTRA),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_USERS_EXTRA),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA),
 		    semanage_user_extra_dbase_policy(sh)) < 0)
 		goto err;
@@ -271,13 +285,14 @@ int semanage_direct_connect(semanage_handle_t *sh)
 		goto err;
 
 	if (fcontext_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC),
+		    sh, semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
 		    semanage_fcontext_dbase_policy(sh)) < 0)
 		goto err;
 
 	if (seuser_file_dbase_init(
-		    sh, semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_SEUSERS),
+		    sh,
+		    semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_STORE_SEUSERS),
 		    semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
 		    semanage_seuser_dbase_policy(sh)) < 0)
 		goto err;
@@ -290,7 +305,7 @@ int semanage_direct_connect(semanage_handle_t *sh)
 		goto err;
 
 	/* set the disable dontaudit value */
-	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
+	path = semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
 
 	if (stat(path, &sb) == 0)
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
libsemanage/src/semanage_store.c +5 -4
index a9b471a2..7f7cd7d2 100644
@@ -746,7 +746,7 @@ int semanage_store_access_check(void)
 	int rc = -1;
 
 	/* read access on active store */
-	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
+	path = semanage_path_read(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
 	if (access(path, R_OK | X_OK) != 0)
 		goto out;
 
@@ -2158,8 +2158,8 @@ int semanage_direct_get_serial(semanage_handle_t *sh)
 		commit_filename =
 			semanage_path(SEMANAGE_TMP, SEMANAGE_COMMIT_NUM_FILE);
 	} else {
-		commit_filename = semanage_path(SEMANAGE_ACTIVE,
-						SEMANAGE_COMMIT_NUM_FILE);
+		commit_filename = semanage_path_read(SEMANAGE_ACTIVE,
+						     SEMANAGE_COMMIT_NUM_FILE);
 	}
 
 	if ((fd = open(commit_filename, O_RDONLY | O_CLOEXEC)) == -1) {
@@ -2239,7 +2239,8 @@ int semanage_read_policydb(semanage_handle_t *sh, sepol_policydb_t *in,
 	struct sepol_policy_file *pf = NULL;
 	FILE *infile = NULL;
 
-	if ((kernel_filename = semanage_path(SEMANAGE_ACTIVE, file)) == NULL) {
+	if ((kernel_filename = semanage_path_read(SEMANAGE_ACTIVE, file)) ==
+	    NULL) {
 		goto cleanup;
 	}
 	if ((infile = fopen(kernel_filename, "re")) == NULL) {
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH v3 5/9 libsemanage: create the writable store skeleton on first transaction

In reply to Stephen Smalley

semodule calls semanage_set_create_store(1) so the writable store is populated at connect time, but semanage does not, so a fresh system with only a ro-store-root the first 'semanage <object> -a' fails at the transaction-lock or the sandbox copy.

Any transaction needs the writable skeleton so create it at the top of semanage_direct_begintrans() rather than depending on the caller. semanage_create_store() is idempotent so semodule is unaffected.

After this the sandbox copies an empty active/ (just modules/) the missing computed-file check in semanage_direct_commit() forces a full rebuild, list_all()/get_cil_paths() draw the module set from the read-only roots, and commit_sandbox() renames the freshly-created active/ to previous/ before installing the new one.

Read-only callers that never begin a transaction still need the read lock; that is handled separately.

---
 libsemanage/src/direct_api.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
libsemanage/src/direct_api.c +13 -0
index a930dac1..5646d451 100644
@@ -405,6 +405,19 @@ static int semanage_direct_disconnect(semanage_handle_t *sh)
 
 static int semanage_direct_begintrans(semanage_handle_t *sh)
 {
+	/*
+	 * A caller that never calls semanage_set_create_store()
+	 * may be running against a fresh writable store that has
+	 * no active/ tree yet. Any transaction needs a writable
+	 * skeleton - <root>/<type>/, active/, active/modules/ and
+	 * the read lock - so create it here rather than failing at
+	 * the trans-lock open or the sandbox copy.
+	 * semanage_create_store() is idempotent.
+	 */
+	if (semanage_create_store(sh, 1) < 0) {
+		return -1;
+	}
+
 	if (semanage_get_trans_lock(sh) < 0) {
 		return -1;
 	}
Signature · 1 line
2.55.0
author maintainer
#6 lore raw

PATCH v3 6/9 libsemanage: create the store root when taking a lock

In reply to Stephen Smalley

Both semanage_get_active_lock() and semanage_get_trans_lock() open their lock file with O_CREAT, but that fails with ENOENT when the <store-root>/type>/ parent has not yet been created. Writers now create the whole skeleton in begintrans() but a reader reaches enter_ro() -> semanage_get_active_lock() with no transaction and no create_store.

On ENOENT, mkdir_recursive() the store root and retry the open once. A reader on a fresh /var/lib/selinux thus creates one directory and one empty lock file and can then serialize against a concurrent commit.

Update semanage_store_access_check() to predict this: when the read lock does not exist, walk up from the store root to the first existing ancestor and check W_OK|X_OK there instead of on the missing store root itself, so the seobject.py >= SEMANAGE_CAN_READ check passes on a fresh writable store.

---
 libsemanage/src/semanage_store.c | 34 ++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)
libsemanage/src/semanage_store.c +30 -4
index 7f7cd7d2..9bece5f2 100644
@@ -759,12 +759,25 @@ int semanage_store_access_check(void)
 	 */
 	path = semanage_files[SEMANAGE_READ_LOCK];
 	if (access(path, R_OK) != 0) {
+		char anc[PATH_MAX], *slash;
+
 		if (access(path, F_OK) == 0) {
 			goto out;
 		}
 
-		path = semanage_files[SEMANAGE_ROOT];
-		if (access(path, R_OK | W_OK | X_OK) != 0) {
+		/*
+		 * semanage_get_lock() will mkdir_recursive() the store
+		 * root on demand, so predict success if any existing
+		 * ancestor of <root>/<type> is writable.
+		 */
+		snprintf(anc, sizeof(anc), "%s", semanage_files[SEMANAGE_ROOT]);
+		while (access(anc, F_OK) != 0 &&
+		       (slash = strrchr(anc, '/')) != NULL) {
+			if (slash == anc)
+				slash++;
+			*slash = '\0';
+		}
+		if (access(anc, R_OK | W_OK | X_OK) != 0) {
 			goto out;
 		}
 	}
@@ -2023,8 +2036,21 @@ static int semanage_get_lock(semanage_handle_t *sh, const char *lock_name,
 	int fd;
 	int left;
 
-	if ((fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC,
-		       S_IRUSR | S_IWUSR)) == -1) {
+	fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
+	if (fd == -1 && errno == ENOENT) {
+		/*
+		 * On a fresh writable store the <root>/<type>/ parent may
+		 * not exist yet for a reader (writers create it in
+		 * begintrans() via semanage_create_store()). Create it
+		 * here so a pure read can still take the lock and
+		 * serialize it against a concurrent commit.
+		 */
+		const char *root = semanage_files[SEMANAGE_ROOT];
+		if (mkdir_recursive(root, 0755) == 0 && chmod(root, 0700) == 0)
+			fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC,
+				  S_IRUSR | S_IWUSR);
+	}
+	if (fd == -1) {
 		ERR(sh, "Could not open direct %s at %s.", lock_name,
 		    lock_file);
 		return -1;
Signature · 1 line
2.55.0
author maintainer
#7 lore raw

PATCH v3 7/9 libsemanage: refuse to remove or enable a module owned by a RO store

In reply to Stephen Smalley

semodule -r <name> on a module that exists only in a read-only store has nothing to unlink in the writable one. Detect that case in semanage_direct_remove_key(), notify the user, and suggest using -d to disable it instead. Removing a writable-store copy that shadows a read-only one is still allowed and simply un-shadows the vendor module.

semodule -e <name> when a read-only store carries the disabled marker has no effect since get_module_info() reads the marker via semanage_module_find_path(). After removing any writable marker, re-probe; if a read-only marker remains, fail with an error message pointing to its path as the cause.

---
 libsemanage/src/direct_api.c | 40 ++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
libsemanage/src/direct_api.c +40 -0
index 5646d451..3d203bd7 100644
@@ -2276,6 +2276,23 @@ static int semanage_direct_set_enabled(semanage_handle_t *sh,
 			}
 		}
 
+		/*
+		 * A read-only store may still carry a disabled marker for
+		 * this module; the writable store cannot override it.
+		 */
+		if (semanage_ro_root_count() > 0 &&
+		    semanage_module_find_path(sh, modinfo,
+					      SEMANAGE_MODULE_PATH_DISABLED, fn,
+					      sizeof(fn)) == 0 &&
+		    access(fn, F_OK) == 0) {
+			errno = 0;
+			ERR(sh,
+			    "Module %s is disabled by the read-only store at %s and cannot be enabled here.",
+			    modkey->name, fn);
+			status = -1;
+			goto cleanup;
+		}
+
 		break;
 	case -1: /* warn about ignored setting to default */
 		WARN(sh, "Setting module %s to 'default' state has no effect",
@@ -3058,6 +3075,29 @@ static int semanage_direct_remove_key(semanage_handle_t *sh,
 		goto cleanup;
 	}
 
+	/*
+	 * A module that lives only in a read-only store has nothing to
+	 * remove in the writable one; disabling is the closest equivalent.
+	 * Removing a writable-store copy that shadows a read-only one is
+	 * still allowed and simply un-shadows the vendor module.
+	 */
+	if (semanage_ro_root_count() > 0 && access(path, F_OK) != 0) {
+		char ro_path[PATH_MAX];
+
+		if (semanage_module_find_path(
+			    sh, (const semanage_module_info_t *)modkey,
+			    SEMANAGE_MODULE_PATH_NAME, ro_path,
+			    sizeof(ro_path)) == 0 &&
+		    access(ro_path, F_OK) == 0) {
+			errno = 0;
+			ERR(sh,
+			    "Module %s at priority %d is provided by the read-only store at %s and cannot be removed; use -d to disable it.",
+			    modkey->name, modkey->priority, ro_path);
+			status = -2;
+			goto cleanup;
+		}
+	}
+
 	/* remove directory */
 	ret = semanage_remove_directory(path);
 	if (ret != 0) {
Signature · 1 line
2.55.0
author maintainer
#8 lore raw

PATCH v3 8/9 libsemanage: search /run and /usr/lib for semanage.conf

In reply to Stephen Smalley

semanage_conf_path() only tried <semanage_root()>/etc/selinux/semanage.conf and the un-rooted /etc fallback so the file that carries ro-store-root could not itself live in the read-only vendor tree.

Search the same configuration directories libselinux uses:
/etc/selinux/ /run/selinux/ /usr/lib/selinux/
overridable via -DSEMANAGE_CONF_DIRS=, rooted first and then falling back to the host directories as before. When none exist the primary /etc-based path is returned so the caller's diagnostic message refers to the canonical location.

With this a vendor image can ship /usr/lib/selinux/semanage.conf and leave /etc/selinux empty.

---
 libsemanage/src/semanage_store.c | 60 +++++++++++++++++++++++---------
 1 file changed, 44 insertions(+), 16 deletions(-)
libsemanage/src/semanage_store.c +44 -16
index 9bece5f2..10919ed5 100644
@@ -539,6 +539,16 @@ const char *semanage_final_path(enum semanage_final_defs store,
 	return semanage_final_paths[store][path_name];
 }
 
+/*
+ * Directories under which semanage.conf is searched, in order. This
+ * mirrors the libselinux configuration-root search list so a vendor
+ * can ship semanage.conf (carrying ro-store-root=) alongside the
+ * read-only runtime tree. Overridable at build time.
+ */
+#ifndef SEMANAGE_CONF_DIRS
+#define SEMANAGE_CONF_DIRS "/etc/selinux/", "/run/selinux/", "/usr/lib/selinux/"
+#endif
+
 /* Return a fully-qualified path + filename to the semanage
  * configuration file. If semanage.conf file in the semanage
  * root is cannot be read, use the default semanage.conf as a
@@ -548,24 +558,42 @@ const char *semanage_final_path(enum semanage_final_defs store,
  */
 char *semanage_conf_path(void)
 {
-	char *semanage_conf = NULL;
-	size_t len;
-	struct stat sb;
+	static const char *dirs[] = { SEMANAGE_CONF_DIRS, NULL };
+	char *conf = NULL;
+	unsigned int i;
 
-	len = strlen(semanage_root()) + strlen(selinux_path()) +
-	      strlen(SEMANAGE_CONF_FILE);
-	semanage_conf = calloc(len + 1, sizeof(char));
-	if (!semanage_conf)
+	/*
+	 * Rooted at each configuration directory, then (for a chroot
+	 * build) fall back to the host directory.
+	 */
+	for (i = 0; dirs[i]; i++) {
+		free(conf);
+		if (asprintf(&conf, "%s%s%s", semanage_root(), dirs[i],
+			     SEMANAGE_CONF_FILE) < 0)
+			return NULL;
+		if (access(conf, F_OK) == 0)
+			return conf;
+	}
+	if (semanage_root()[0] != '\0') {
+		for (i = 0; dirs[i]; i++) {
+			free(conf);
+			if (asprintf(&conf, "%s%s", dirs[i],
+				     SEMANAGE_CONF_FILE) < 0)
+				return NULL;
+			if (access(conf, F_OK) == 0)
+				return conf;
+		}
+	}
+
+	/*
+	 * Nothing found; return the primary path so the caller's
+	 * error message includes the canonical location.
+	 */
+	free(conf);
+	if (asprintf(&conf, "%s%s%s", semanage_root(), dirs[0],
+		     SEMANAGE_CONF_FILE) < 0)
 		return NULL;
-	snprintf(semanage_conf, len + 1, "%s%s%s", semanage_root(),
-		 selinux_path(), SEMANAGE_CONF_FILE);
-
-	if (stat(semanage_conf, &sb) != 0 && errno == ENOENT) {
-		snprintf(semanage_conf, len + 1, "%s%s", selinux_path(),
-			 SEMANAGE_CONF_FILE);
-	}
-
-	return semanage_conf;
+	return conf;
 }
 
 /* Recursively create a directory from a path string.
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH v3 9/9 libsemanage: document semanage.conf search path and ro-store-root

In reply to Stephen Smalley

Document the new semanage.conf search path:
/etc/selinux, /run/selinux, /usr/lib/selinux

and document the new ro-store-root option for semanage.conf.

---
 libsemanage/man/man5/semanage.conf.5 | 36 ++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
libsemanage/man/man5/semanage.conf.5 +34 -2
index 74f0d34f..1373a11a 100644
@@ -5,8 +5,18 @@ semanage.conf \- global configuration file for the SELinux Management library
 .PP
 The
 .BR semanage.conf
-file is usually located under the directory /etc/selinux and it is used for run-time configuration of the
-behavior of the SELinux Management library.
+file is used for run-time configuration of the behavior of the SELinux Management library.
+It is searched for under
+.IR /etc/selinux/ ,
+then
+.IR /run/selinux ,
+then
+.IR /usr/lib/selinux ;
+the first one found is used.
+This lets a vendor ship a default under
+.I /usr/lib/selinux/semanage.conf
+and an administrator override it under
+.IR /etc .
 
 .PP
 Each line should contain a configuration parameter followed by the equal sign ("=") and then followed by the configuration value for that
@@ -35,6 +45,28 @@ Specify an alternative root path to use for the store. The default is "/"
 .B store-root
 Specify an alternative store_root path to use. The default is "/var/lib/selinux"
 
+.TP
+.B ro-store-root
+Specify a colon-separated list of additional, read-only module-store roots to search after
+.B store-root
+when reading, e.g. "/usr/lib/selinux".
+Only the
+.I active
+subtree of a listed root is read; the sandbox, previous store, and lock files always live under
+.BR store-root .
+Module listing is the union across all roots, and a module at the same priority and name under
+.B store-root
+shadows one from a read-only root; installing at that priority overrides the vendor module and removing the local copy un-shadows it.
+.B semodule \-r
+on a module that exists only in a read-only root is refused (use
+.B \-d
+to disable it), as is
+.B semodule \-e
+on a module that a read-only root marks disabled.
+Unset by default, in which case only
+.B store-root
+is searched and behavior is unchanged.
+
 .TP
 .B compiler-directory
 Specify an alternative directory that contains HLL to CIL compilers. The default value is "/usr/libexec/selinux/hll".
Signature · 1 line
2.55.0