From 93ef90cd24bc35485f9060b83dffb648f181f0c0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 11 Apr 2020 04:05:34 -0700 Subject: [PATCH] Fix FORTIFY crashes --- native/jni/utils/file.cpp | 6 +++--- native/jni/utils/xwrap.cpp | 16 ++++++++++++++++ native/jni/utils/xwrap.hpp | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/native/jni/utils/file.cpp b/native/jni/utils/file.cpp index b73937f38..138a4c581 100644 --- a/native/jni/utils/file.cpp +++ b/native/jni/utils/file.cpp @@ -127,7 +127,7 @@ void cp_afc(const char *src, const char *dest) { unlink(dest); if (S_ISREG(a.st.st_mode)) { int sfd = xopen(src, O_RDONLY | O_CLOEXEC); - int dfd = xopen(dest, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC); + int dfd = xopen(dest, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0); xsendfile(dfd, sfd, nullptr, a.st.st_size); close(sfd); close(dfd); @@ -158,7 +158,7 @@ void clone_dir(int src, int dest) { } case DT_REG: { int sfd = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC); - int dfd = xopenat(dest, entry->d_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC); + int dfd = xopenat(dest, entry->d_name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0); xsendfile(dfd, sfd, nullptr, a.st.st_size); fsetattr(dfd, &a); close(dfd); @@ -359,7 +359,7 @@ void parse_mnt(const char *file, const function &fn) { void backup_folder(const char *dir, vector &files) { char path[4096]; - realpath(dir, path); + xrealpath(dir, path); int len = strlen(path); post_order_walk(xopen(dir, O_RDONLY), [&](int dfd, dirent *entry) -> int { int fd = xopenat(dfd, entry->d_name, O_RDONLY); diff --git a/native/jni/utils/xwrap.cpp b/native/jni/utils/xwrap.cpp index 949b5da5f..d87fd1009 100644 --- a/native/jni/utils/xwrap.cpp +++ b/native/jni/utils/xwrap.cpp @@ -53,6 +53,14 @@ int xopenat(int dirfd, const char *pathname, int flags) { return fd; } +int xopenat(int dirfd, const char *pathname, int flags, mode_t mode) { + int fd = openat(dirfd, pathname, flags, mode); + if (fd < 0) { + PLOGE("openat: %s", pathname); + } + return fd; +} + ssize_t xwrite(int fd, const void *buf, size_t count) { int ret = write(fd, buf, count); if (count != ret) { @@ -435,3 +443,11 @@ int xinotify_init1(int flags) { } return ret; } + +char *xrealpath(const char *path, char *resolved_path) { + char *ret = realpath(path, resolved_path); + if (ret == nullptr) { + PLOGE("xrealpath"); + } + return ret; +} diff --git a/native/jni/utils/xwrap.hpp b/native/jni/utils/xwrap.hpp index bdd343132..266f4ad4f 100644 --- a/native/jni/utils/xwrap.hpp +++ b/native/jni/utils/xwrap.hpp @@ -10,6 +10,7 @@ FILE *xfdopen(int fd, const char *mode); int xopen(const char *pathname, int flags); int xopen(const char *pathname, int flags, mode_t mode); int xopenat(int dirfd, const char *pathname, int flags); +int xopenat(int dirfd, const char *pathname, int flags, mode_t mode); ssize_t xwrite(int fd, const void *buf, size_t count); ssize_t xread(int fd, void *buf, size_t count); ssize_t xxread(int fd, void *buf, size_t count); @@ -57,4 +58,5 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count); pid_t xfork(); int xpoll(struct pollfd *fds, nfds_t nfds, int timeout); int xinotify_init1(int flags); +char *xrealpath(const char *path, char *resolved_path);