external/wpa_supplicant_8
Révision | 0a217de1802bc5cf785524c1913af7ae3faa9c54 (tree) |
---|---|
l'heure | 2015-02-06 08:46:52 |
Auteur | Vinit Deshpande <vinitd@goog...> |
Commiter | Vinit Deshpande |
Don't write to wpa_supplicant.conf directly
There is a chance that wpa_supplicant may get killed during
the time it is writing config file. If this happens, user
information like SSIDs and passwords can be lost forever.
This change works around that by writing config to a
temporary file and then renaming the file to the correct name.
Bug: 19224089
Change-Id: I1709cdd5e5c6dfa3073e42c644fae941b43401cc
Signed-off-by: Vinit Deshpande <vinitd@google.com>
@@ -20,6 +20,9 @@ | ||
20 | 20 | #include "eap_peer/eap_methods.h" |
21 | 21 | #include "eap_peer/eap.h" |
22 | 22 | |
23 | +#ifdef ANDROID | |
24 | +#include <sys/stat.h> | |
25 | +#endif | |
23 | 26 | |
24 | 27 | static int newline_terminated(const char *buf, size_t buflen) |
25 | 28 | { |
@@ -1205,12 +1208,21 @@ int wpa_config_write(const char *name, struct wpa_config *config) | ||
1205 | 1208 | struct wpa_config_blob *blob; |
1206 | 1209 | #endif /* CONFIG_NO_CONFIG_BLOBS */ |
1207 | 1210 | int ret = 0; |
1211 | + int tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */ | |
1212 | + char *tmp_name = os_malloc(tmp_len); | |
1208 | 1213 | |
1209 | - wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); | |
1214 | + if (tmp_name == NULL) | |
1215 | + tmp_name = (char *)name; | |
1216 | + else | |
1217 | + os_snprintf(tmp_name, tmp_len, "%s.tmp", name); | |
1218 | + | |
1219 | + wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", tmp_name); | |
1210 | 1220 | |
1211 | - f = fopen(name, "w"); | |
1221 | + f = fopen(tmp_name, "w"); | |
1212 | 1222 | if (f == NULL) { |
1213 | - wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name); | |
1223 | + wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", tmp_name); | |
1224 | + if (tmp_name != name) | |
1225 | + os_free(tmp_name); | |
1214 | 1226 | return -1; |
1215 | 1227 | } |
1216 | 1228 |
@@ -1245,6 +1257,17 @@ int wpa_config_write(const char *name, struct wpa_config *config) | ||
1245 | 1257 | |
1246 | 1258 | fclose(f); |
1247 | 1259 | |
1260 | + if (tmp_name != name) { | |
1261 | + int chmod_ret = 0; | |
1262 | +#ifdef ANDROID | |
1263 | + chmod_ret = chmod(tmp_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
1264 | +#endif | |
1265 | + if (chmod_ret != 0 || rename(tmp_name, name) != 0) | |
1266 | + ret = -1; | |
1267 | + | |
1268 | + os_free(tmp_name); | |
1269 | + } | |
1270 | + | |
1248 | 1271 | wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully", |
1249 | 1272 | name, ret ? "un" : ""); |
1250 | 1273 | return ret; |