Skip to contentSkip to conversation

libselinux: export selinux_policy_open()

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

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/3 libselinux: export selinux_policy_open()

The configuration-root search list added by the stateless series is used inside libselinux via the internal selinux_policy_fopen()/ selinux_policy_open() helpers, but external readers of policy configuration such as mcstransd and run_init still open the paths returned by selinux_translations_path()/selinux_colors_path()/ selinux_contexts_path() directly and so do not fall back to a vendor root under /usr/lib/selinux.

Export the fd-returning selinux_policy_open() so those callers can use the same search without duplicating it. The FILE * variant stays internal so that <selinux/selinux.h> does not need to grow a <stdio.h> dependency; callers that want a stream can fdopen() the result.

The search remaps paths under selinux_poicy_root() only. Files that live directly under /etc/selinux (semanage.conf, restorecond.conf, newrole_pam.conf) are per-installation rather than per-policy-type; the former already has its own search in libsemanage and the latter two are handled separately without needing this interface.

---
 libselinux/include/selinux/selinux.h      | 12 +++++
 libselinux/man/man3/selinux_policy_open.3 | 57 +++++++++++++++++++++++
 libselinux/src/libselinux.map             |  5 ++
 libselinux/src/selinux_internal.h         |  3 +-
 4 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 libselinux/man/man3/selinux_policy_open.3
libselinux/include/selinux/selinux.h +12 -0
index 364f955d..b789ee08 100644
@@ -565,6 +565,18 @@ extern const char *selinux_policy_root(void);
  */
 extern int selinux_set_policy_root(const char *rootpath);
 
+/*
+ * selinux_policy_open opens @path for reading, and if @path is under
+ * selinux_policy_root() and yields ENOENT or EACCES, retries the open
+ * under each fallback configuration root in turn (e.g. /usr/lib/selinux
+ * when a distribution ships policy configuration there instead of /etc).
+ * @path is normally a path returned by one of the selinux_*_path()
+ * functions below. Returns a file descriptor on success or -1 with
+ * errno set (ENOENT if no configuration root had the file, EACCES if
+ * at least one root had it but refused access).
+ */
+extern int selinux_policy_open(const char *path, int flags);
+
 /* These functions return the paths to specific files under the 
    policy root directory. */
 extern const char *selinux_current_policy_path(void);
libselinux/man/man3/selinux_policy_open.3 new file +57 -0

57 lines not on this page · raw message

libselinux/src/libselinux.map +5 -0
index 95cd53b0..9b439d8e 100644
@@ -267,3 +267,8 @@ LIBSELINUX_3.10 {
   global:
     selinux_restorecon_get_relabeled_files;
 } LIBSELINUX_3.9;
+
+LIBSELINUX_3.12 {
+  global:
+    selinux_policy_open;
+} LIBSELINUX_3.10;
libselinux/src/selinux_internal.h +1 -2
index 718beb6e..c32bb4c0 100644
@@ -117,10 +117,9 @@ const char *const *selinux_policy_roots(void);
 /*
  * Open @path (as returned by one of the selinux_*_path() accessors),
  * falling back through selinux_policy_roots() when the primary path
- * yields ENOENT or EACCES. Internal-only.
+ * yields ENOENT or EACCES. selinux_policy_open() is the public form.
  */
 FILE *selinux_policy_fopen(const char *path, const char *mode);
-int selinux_policy_open(const char *path, int flags);
 
 /*
  * Resolve @path against the configuration-root list: write into @out
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/3 mcstrans: search fallback configuration roots for setrans.conf and secolor.conf

Open setrans.conf and secolor.conf using the newly exported selinux_policy_open() so the same fallback search that libselinux internally uses applies here.

Include= targets in setrans.conf are user-supplied absolute paths and are opened as written after glob() expansion; a vendor setrans.conf that ships under /usr/lib/selinux should therefore reference its own Include= fragments by their vendor-root path rather than /etc, since the search is not applied to Include= arguments.

---
 mcstrans/src/mcscolor.c | 9 +++++++--
 mcstrans/src/mcstrans.c | 9 ++++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)
mcstrans/src/mcscolor.c +7 -2
index e4a0db74..0b2dfef1 100644
@@ -263,9 +263,14 @@ int init_colors(void)
 	if (getcon(&my_context) < 0)
 		return 1;
 
-	cfg = fopen(selinux_colors_path(), "r");
-	if (!cfg)
+	int fd = selinux_policy_open(selinux_colors_path(), O_RDONLY);
+	if (fd < 0)
 		return 1;
+	cfg = fdopen(fd, "r");
+	if (!cfg) {
+		close(fd);
+		return 1;
+	}
 
 	__fsetlocking(cfg, FSETLOCKING_BYCALLER);
 	while (getline(&buffer, &size, cfg) > 0) {
mcstrans/src/mcstrans.c +8 -1
index 9704f5dc..20b074cd 100644
@@ -921,9 +921,16 @@ int read_translations(const char *filename)
 		return -1;
 	}
 
-	FILE *cfg = fopen(filename, "r");
+	int fd = selinux_policy_open(filename, O_RDONLY);
+	if (fd < 0) {
+		syslog(LOG_ERR, "%s file open failed", filename);
+		return -1;
+	}
+
+	FILE *cfg = fdopen(fd, "r");
 	if (!cfg) {
 		syslog(LOG_ERR, "%s file open failed", filename);
+		close(fd);
 		return -1;
 	}
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 3/3 policycoreutils/run_init: search fallback configuration roots for initrc_context

In reply to Stephen Smalley

Use the newly exported selinux_policy_open() so a stateless install that ships the contexts tree under /usr/lib/selinux is found the same way libselinux itself finds default_contexts and friends.

---
 policycoreutils/run_init/run_init.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
policycoreutils/run_init/run_init.c +10 -2
index 89921b23..6c427dc5 100644
@@ -302,12 +302,20 @@ static int get_init_context(char **context)
 {
 	FILE *fp;
 	char buf[255], *bufp;
-	int buf_len;
+	int buf_len, fd;
 	char context_file[PATH_MAX];
+
 	snprintf(context_file, sizeof(context_file) - 1, "%s/%s",
 		 selinux_contexts_path(), CONTEXT_FILE);
-	fp = fopen(context_file, "r");
+	fd = selinux_policy_open(context_file, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, _("Could not open file %s\n"), context_file);
+		return -1;
+	}
+
+	fp = fdopen(fd, "r");
 	if (!fp) {
+		close(fd);
 		fprintf(stderr, _("Could not open file %s\n"), context_file);
 		return -1;
 	}
Signature · 1 line
2.55.0