Verified Commit 92cdbe05 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

intercept basic syscalls

parent d0e8d84a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ int adafs_dup(int oldfd);

int adafs_dup2(int oldfd, int newfd);

ssize_t adafs_write(int fd, void* buf, size_t count);

ssize_t adafs_pwrite_ws(int fd, const void* buf, size_t count, off64_t offset);

ssize_t adafs_read(int fd, void* buf, size_t count);
+6 −0
Original line number Diff line number Diff line
@@ -5,5 +5,11 @@


int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode);
int hook_close(int fd);
int hook_stat(const char* path, struct stat* buf);
int hook_read(int fd, void* buf, size_t count);
int hook_write(int fd, void* buf, size_t count);
int hook_unlink(const char* path);


#endif
+12 −0
Original line number Diff line number Diff line
@@ -284,6 +284,18 @@ int adafs_dup2(const int oldfd, const int newfd) {
    return CTX->file_map()->dup2(oldfd, newfd);
}

ssize_t adafs_write(int fd, void* buf, size_t count) {
            auto adafs_fd = CTX->file_map()->get(fd);
            auto pos = adafs_fd->pos(); //retrieve the current offset
            if (adafs_fd->get_flag(OpenFile_flags::append))
                adafs_lseek(adafs_fd, 0, SEEK_END);
            auto ret = adafs_pwrite_ws(fd, buf, count, pos);
            // Update offset in file descriptor in the file map
            if (ret > 0) {
                adafs_fd->pos(pos + count);
            }
            return ret;
}

ssize_t adafs_pwrite_ws(int fd, const void* buf, size_t count, off64_t offset) {
    auto adafs_fd = CTX->file_map()->get(fd);
+60 −3
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode) {
            if(ret < 0) {
                return -errno;
            }
            return ret;
        }
    } else {
        // cpath is relative
@@ -44,8 +45,7 @@ int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode) {
        auto dir = CTX->file_map()->get_dir(dirfd);
        if(dir == nullptr) {
            CTX->log()->error("{}() dirfd is not a directory ", __func__);
            errno = ENOTDIR;
            return -1;
            return -ENOTDIR;
        }

        std::string path = CTX->mountdir();
@@ -57,7 +57,64 @@ int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode) {
            if(ret < 0) {
                return -errno;
            }
            return ret;
        }
    }
    return syscall_no_intercept(SYS_openat, dirfd, resolved.c_str(), flags, mode);
}

int hook_close(int fd) {
    CTX->log()->trace("{}() called with fd {}", __func__, fd);
    if(CTX->file_map()->exist(fd)) {
        // No call to the daemon is required
        CTX->file_map()->remove(fd);
        return 0;
    }
    return syscall_no_intercept(SYS_close, fd);
}

int hook_stat(const char* path, struct stat* buf) {
    CTX->log()->trace("{}() called with path '{}'", __func__, path);
    std::string rel_path;
    if (CTX->relativize_path(path, rel_path)) {
            return with_errno(adafs_stat(rel_path, buf));
    }
    return syscall_no_intercept(SYS_stat, rel_path.c_str(), buf);
}

int hook_read(int fd, void* buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
    if (CTX->file_map()->exist(fd)) {
        auto ret = adafs_read(fd, buf, count);
        if(ret < 0) {
            return -errno;
        }
        return ret;
    }
    return syscall_no_intercept(SYS_read, fd, buf, count);
}

int hook_write(int fd, void* buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
    if (CTX->file_map()->exist(fd)) {
        auto ret = adafs_write(fd, buf, count);
        if(ret < 0) {
            return -errno;
        }
        return ret;
    }
    return syscall_no_intercept(SYS_write, fd, buf, count);
}

int hook_unlink(const char* path) {
    CTX->log()->trace("{}() called with path '{}'", __func__, path);
    std::string rel_path;
    if (CTX->relativize_path(path, rel_path)) {
        auto ret = adafs_rm_node(rel_path);
        if(ret < 0) {
            return -errno;
        }
        return ret;
    }
    return syscall_no_intercept(SYS_unlink, rel_path.c_str());
}
+32 −0
Original line number Diff line number Diff line
@@ -30,6 +30,38 @@ static inline int hook(long syscall_number,
                              static_cast<mode_t>(arg2));
        break;

    case SYS_openat:
        *result = hook_openat(static_cast<int>(arg0),
                              reinterpret_cast<const char*>(arg1),
                              static_cast<int>(arg2),
                              static_cast<mode_t>(arg3));
        break;

    case SYS_close:
        *result = hook_close(static_cast<int>(arg0));
        break;

    case SYS_stat:
        *result = hook_stat(reinterpret_cast<char*>(arg0),
                            reinterpret_cast<struct stat*>(arg1));
        break;

    case SYS_read:
        *result = hook_read(static_cast<int>(arg0),
                            reinterpret_cast<void*>(arg1),
                            static_cast<size_t>(arg2));
        break;

    case SYS_write:
        *result = hook_write(static_cast<int>(arg0),
                             reinterpret_cast<void*>(arg1),
                             static_cast<size_t>(arg2));
        break;

    case SYS_unlink:
        *result = hook_unlink(reinterpret_cast<char*>(arg0));
        break;

    default:
        /*
         * Ignore any other syscalls