[PATCH v2] config_file: Honor permissions from the target file

ShengYi Hung aokblast at FreeBSD.org
Sun Apr 19 02:02:02 PDT 2026


fwrite creates file with default permission (rw-r--r--) while the file
does not exist. However, the wpa config file may be installed as
(rw-------), which doesn't want other user to see the password.

As a result, we honor these permissions from the original file by using
stat and chmod syscalls.

Signed-off-by: ShengYi Hung <aokblast at FreeBSD.org>
---
 wpa_supplicant/config_file.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/wpa_supplicant/config_file.c b/wpa_supplicant/config_file.c
index 45b16d43a..062b41ec3 100644
--- a/wpa_supplicant/config_file.c
+++ b/wpa_supplicant/config_file.c
@@ -1856,6 +1856,7 @@ int wpa_config_write(const char *name, struct wpa_config *config)
 	const char *orig_name = name;
 	int tmp_len;
 	char *tmp_name;
+	struct stat file_stat;
 
 	if (!name) {
 		wpa_printf(MSG_ERROR, "No configuration file for writing");
@@ -1878,6 +1879,10 @@ int wpa_config_write(const char *name, struct wpa_config *config)
 		return -1;
 	}
 
+	if (stat(orig_name, &file_stat) == 0)
+		chmod(name, (file_stat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) |
+			  S_IRUSR | S_IWUSR);
+
 	wpa_config_write_global(f, config);
 
 	for (cred = config->cred; cred; cred = cred->next) {
-- 
2.53.0




More information about the Hostap mailing list