[PATCH 03/14] utils: move the trusted path check of the OpenSSL engines to os_unix.c

Angelo Compagnucci angelo.compagnucci at gmail.com
Mon Nov 23 06:24:37 PST 2026


tls_engine_path_trusted() only looks at the file system. It checks that
the real path is under /usr/lib/, and that the file and all its parent
directories are owned by root and cannot be written by group or others.
Nothing in it is specific to OpenSSL, and other code that loads a
shared library named in the configuration needs the same check.

Move it to os_unix.c and call it os_lib_path_trusted(). The code does
not change. It uses POSIX file owners and permissions, so, like
os_file_exists(), it is only in os_unix.c. It is still built only with
CONFIG_TLS_ENGINE_TRUSTED_PATH. The "ENGINE:" prefix is removed from
its log messages, and its comment is rewritten in simpler words.

Tested by building with and without CONFIG_TLS_ENGINE_TRUSTED_PATH=y.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci at gmail.com>
---
 src/crypto/tls_openssl.c | 80 ++--------------------------------------
 src/utils/os.h           | 23 ++++++++++++
 src/utils/os_unix.c      | 60 ++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 77 deletions(-)

diff --git a/src/crypto/tls_openssl.c b/src/crypto/tls_openssl.c
index cafeb1aa2..dcaace8c0 100644
--- a/src/crypto/tls_openssl.c
+++ b/src/crypto/tls_openssl.c
@@ -8,7 +8,6 @@
 
 #include "includes.h"
 #ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
-#include <sys/stat.h>
 #include <limits.h>
 #endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
 #ifdef CONFIG_TESTING_OPTIONS
@@ -1032,79 +1031,6 @@ static int tls_engine_load_dynamic_generic(const char *pre[],
 }
 
 
-#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
-
-#define TRUSTED_PATH "/usr/lib/"
-
-/**
- * tls_engine_path_trusted - Verify engine .so path is trusted for loading
- * @path: Engine/module path supplied via configuration or D-Bus
- * @real_path: Buffer for the canonical path (PATH_MAX bytes)
- * Returns: 0 if trusted (real_path filled), -1 otherwise
- *
- * The PKCS#11/OpenSC engine and module shared object paths are loaded with
- * dlopen() within this process, which may be running with elevated
- * privileges. Verify that the supplied path cannot be used to load an
- * attacker-controlled shared library by requiring, in addition to being
- * located under the trusted path, that the target is a regular file owned by
- * root, not writable by group or others, and that every ancestor directory up
- * to the file is itself a directory, owned by root and not writable by group
- * or others (otherwise a root-owned file could be swapped by whoever controls
- * such a directory). lstat() is used for the file check so that a symlink is
- * never followed, even though realpath() has already canonicalised the path.
- */
-static int tls_engine_path_trusted(const char *path, char *real_path)
-{
-	struct stat st;
-	char dir[PATH_MAX];
-	char *slash, *next;
-
-	if (!path)
-		return -1;
-
-	if (!realpath(path, real_path)) {
-		wpa_printf(MSG_INFO,
-			   "ENGINE: Refusing to load %s: realpath: %s",
-			   path, strerror(errno));
-		return -1;
-	}
-
-	if (os_strncmp(TRUSTED_PATH, real_path, os_strlen(TRUSTED_PATH)) != 0) {
-		wpa_printf(MSG_INFO,
-			   "ENGINE: Refusing to load %s: not in trusted path %s",
-			   real_path, TRUSTED_PATH);
-		return -1;
-	}
-
-	if (lstat(real_path, &st) != 0 || !S_ISREG(st.st_mode) ||
-	    st.st_uid != 0 || (st.st_mode & (S_IWGRP | S_IWOTH))) {
-		wpa_printf(MSG_INFO,
-			   "ENGINE: Refusing to load %s: not a root-owned, non-writable regular file",
-			   real_path);
-		return -1;
-	}
-
-	os_strlcpy(dir, real_path, sizeof(dir));
-	slash = dir;
-	while ((next = os_strchr(slash + 1, '/'))) {
-		*next = '\0';
-		if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode) ||
-		    st.st_uid != 0 || (st.st_mode & (S_IWGRP | S_IWOTH))) {
-			wpa_printf(MSG_INFO,
-				   "ENGINE: Refusing to load %s: insecure ancestor directory %s",
-				   real_path, dir);
-			return -1;
-		}
-		*next = '/';
-		slash = next;
-	}
-
-	return 0;
-}
-
-#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
-
-
 /**
  * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
  * @pkcs11_so_path: pksc11_so_path from the configuration
@@ -1135,7 +1061,7 @@ static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
 		return 0;
 
 #ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
-	if (tls_engine_path_trusted(pkcs11_so_path, real_pkcs11_so_path) < 0)
+	if (os_lib_path_trusted(pkcs11_so_path, real_pkcs11_so_path) < 0)
 		return -1;
 	pre_cmd[1] = real_pkcs11_so_path;
 #else /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
@@ -1144,7 +1070,7 @@ static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
 	pre_cmd[3] = engine_id;
 #ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
 	if (pkcs11_module_path) {
-		if (tls_engine_path_trusted(pkcs11_module_path,
+		if (os_lib_path_trusted(pkcs11_module_path,
 					    real_pkcs11_module_path) < 0)
 			return -1;
 		post_cmd[1] = real_pkcs11_module_path;
@@ -1187,7 +1113,7 @@ static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
 		return 0;
 
 #ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
-	if (tls_engine_path_trusted(opensc_so_path, real_opensc_so_path) < 0)
+	if (os_lib_path_trusted(opensc_so_path, real_opensc_so_path) < 0)
 		return -1;
 	pre_cmd[1] = real_opensc_so_path;
 #else /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
diff --git a/src/utils/os.h b/src/utils/os.h
index 049c9ad01..8b8be4dda 100644
--- a/src/utils/os.h
+++ b/src/utils/os.h
@@ -268,6 +268,29 @@ char * os_readfile(const char *name, size_t *len);
  */
 int os_file_exists(const char *fname);
 
+#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
+/**
+ * os_lib_path_trusted - Check that a shared library path is safe to load
+ * @path: Shared library path from the configuration or from D-Bus
+ * @real_path: Buffer for the real path (PATH_MAX bytes)
+ * Returns: 0 if the path is trusted and real_path is set, -1 otherwise
+ *
+ * Shared libraries, such as the PKCS#11 and OpenSC engines and the PKCS#11
+ * modules, are loaded with dlopen() into this process. The process may run
+ * with high privileges, so the path must not let it load a library that an
+ * attacker controls. The path is trusted only when all of these are true:
+ * - the real path is under the trusted path (/usr/lib/)
+ * - the file is a regular file owned by root, and group and others cannot
+ *   write to it
+ * - every parent directory is owned by root, and group and others cannot
+ *   write to it. Otherwise, whoever controls a parent directory could
+ *   replace the file.
+ * The file is checked with lstat(), so a symlink is never followed, even
+ * though realpath() already resolved the path.
+ */
+int os_lib_path_trusted(const char *path, char *real_path);
+#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
+
 /**
  * os_fdatasync - Sync a file's (for a given stream) state with storage device
  * @stream: the stream to be flushed
diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c
index b2de091ee..c85ad8321 100644
--- a/src/utils/os_unix.c
+++ b/src/utils/os_unix.c
@@ -7,6 +7,10 @@
  */
 
 #include "includes.h"
+#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
+#include <sys/stat.h>
+#include <limits.h>
+#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
 
 #include <time.h>
 #include <sys/wait.h>
@@ -483,6 +487,62 @@ int os_file_exists(const char *fname)
 }
 
 
+#ifdef CONFIG_TLS_ENGINE_TRUSTED_PATH
+
+#define TRUSTED_PATH "/usr/lib/"
+
+int os_lib_path_trusted(const char *path, char *real_path)
+{
+	struct stat st;
+	char dir[PATH_MAX];
+	char *slash, *next;
+
+	if (!path)
+		return -1;
+
+	if (!realpath(path, real_path)) {
+		wpa_printf(MSG_INFO,
+			   "Refusing to load %s: realpath: %s",
+			   path, strerror(errno));
+		return -1;
+	}
+
+	if (os_strncmp(TRUSTED_PATH, real_path, os_strlen(TRUSTED_PATH)) != 0) {
+		wpa_printf(MSG_INFO,
+			   "Refusing to load %s: not in trusted path %s",
+			   real_path, TRUSTED_PATH);
+		return -1;
+	}
+
+	if (lstat(real_path, &st) != 0 || !S_ISREG(st.st_mode) ||
+	    st.st_uid != 0 || (st.st_mode & (S_IWGRP | S_IWOTH))) {
+		wpa_printf(MSG_INFO,
+			   "Refusing to load %s: not a root-owned, non-writable regular file",
+			   real_path);
+		return -1;
+	}
+
+	os_strlcpy(dir, real_path, sizeof(dir));
+	slash = dir;
+	while ((next = os_strchr(slash + 1, '/'))) {
+		*next = '\0';
+		if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode) ||
+		    st.st_uid != 0 || (st.st_mode & (S_IWGRP | S_IWOTH))) {
+			wpa_printf(MSG_INFO,
+				   "Refusing to load %s: insecure ancestor directory %s",
+				   real_path, dir);
+			return -1;
+		}
+		*next = '/';
+		slash = next;
+	}
+
+	return 0;
+}
+
+#endif /* CONFIG_TLS_ENGINE_TRUSTED_PATH */
+
+
 int os_fdatasync(FILE *stream)
 {
 	if (!fflush(stream)) {
-- 
2.53.0




More information about the Hostap mailing list