Fix FORTIFY crashes

This commit is contained in:
topjohnwu 2020-04-11 04:05:34 -07:00
parent e165a1e65c
commit 93ef90cd24
3 changed files with 21 additions and 3 deletions

View File

@ -127,7 +127,7 @@ void cp_afc(const char *src, const char *dest) {
unlink(dest); unlink(dest);
if (S_ISREG(a.st.st_mode)) { if (S_ISREG(a.st.st_mode)) {
int sfd = xopen(src, O_RDONLY | O_CLOEXEC); 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); xsendfile(dfd, sfd, nullptr, a.st.st_size);
close(sfd); close(sfd);
close(dfd); close(dfd);
@ -158,7 +158,7 @@ void clone_dir(int src, int dest) {
} }
case DT_REG: { case DT_REG: {
int sfd = xopenat(src, entry->d_name, O_RDONLY | O_CLOEXEC); 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); xsendfile(dfd, sfd, nullptr, a.st.st_size);
fsetattr(dfd, &a); fsetattr(dfd, &a);
close(dfd); close(dfd);
@ -359,7 +359,7 @@ void parse_mnt(const char *file, const function<bool(mntent*)> &fn) {
void backup_folder(const char *dir, vector<raw_file> &files) { void backup_folder(const char *dir, vector<raw_file> &files) {
char path[4096]; char path[4096];
realpath(dir, path); xrealpath(dir, path);
int len = strlen(path); int len = strlen(path);
post_order_walk(xopen(dir, O_RDONLY), [&](int dfd, dirent *entry) -> int { post_order_walk(xopen(dir, O_RDONLY), [&](int dfd, dirent *entry) -> int {
int fd = xopenat(dfd, entry->d_name, O_RDONLY); int fd = xopenat(dfd, entry->d_name, O_RDONLY);

View File

@ -53,6 +53,14 @@ int xopenat(int dirfd, const char *pathname, int flags) {
return fd; 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) { ssize_t xwrite(int fd, const void *buf, size_t count) {
int ret = write(fd, buf, count); int ret = write(fd, buf, count);
if (count != ret) { if (count != ret) {
@ -435,3 +443,11 @@ int xinotify_init1(int flags) {
} }
return ret; return ret;
} }
char *xrealpath(const char *path, char *resolved_path) {
char *ret = realpath(path, resolved_path);
if (ret == nullptr) {
PLOGE("xrealpath");
}
return ret;
}

View File

@ -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);
int xopen(const char *pathname, int flags, mode_t mode); 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);
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 xwrite(int fd, const void *buf, size_t count);
ssize_t xread(int fd, 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); 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(); pid_t xfork();
int xpoll(struct pollfd *fds, nfds_t nfds, int timeout); int xpoll(struct pollfd *fds, nfds_t nfds, int timeout);
int xinotify_init1(int flags); int xinotify_init1(int flags);
char *xrealpath(const char *path, char *resolved_path);