diff options
author | Elliott Hughes <enh@google.com> | 2015-02-17 10:16:04 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-02-17 10:25:23 -0800 |
commit | 9d1f515ed1066d095b923c538e8ac19c73e045f4 (patch) | |
tree | f2acee23a529ca45233f719b1665ba01bcc85b55 /libutils/file.cpp | |
parent | 29576ae8901eb95d4c8a34f242a282b2fb73d35f (diff) | |
download | system_core-9d1f515ed1066d095b923c538e8ac19c73e045f4.zip system_core-9d1f515ed1066d095b923c538e8ac19c73e045f4.tar.gz system_core-9d1f515ed1066d095b923c538e8ac19c73e045f4.tar.bz2 |
Fix the WriteStringToFile overload that takes mode/owner/group.
The actual bug is == instead of !=, but the real cause was me trying to be
too clever. This patch switches to much simpler code, and -- since the
intended use of this code is security anyway -- adds logging if anything
goes wrong.
Bug: 19361774
Change-Id: If2af07d31a5002f9010b838247b691f6b28bdfb1
Diffstat (limited to 'libutils/file.cpp')
-rw-r--r-- | libutils/file.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/libutils/file.cpp b/libutils/file.cpp index 577df78..0690bc2 100644 --- a/libutils/file.cpp +++ b/libutils/file.cpp @@ -14,6 +14,9 @@ * limitations under the License. */ +#define LOG_TAG "utils.file" +#include <cutils/log.h> + #include "utils/file.h" #include <errno.h> @@ -75,14 +78,26 @@ bool android::WriteStringToFile(const std::string& content, const std::string& p O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, mode)); if (fd == -1) { + ALOGE("android::WriteStringToFile open failed: %s", strerror(errno)); return false; } // We do an explicit fchmod here because we assume that the caller really meant what they // said and doesn't want the umask-influenced mode. - bool result = (fchmod(fd, mode) != -1 && fchown(fd, owner, group) == -1 && WriteStringToFd(content, fd)); + if (fchmod(fd, mode) == -1) { + ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } + if (fchown(fd, owner, group) == -1) { + ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } + if (!WriteStringToFd(content, fd)) { + ALOGE("android::WriteStringToFile write failed: %s", strerror(errno)); + return CleanUpAfterFailedWrite(path); + } TEMP_FAILURE_RETRY(close(fd)); - return result || CleanUpAfterFailedWrite(path); + return true; } #endif |