Commit 32f30a59 authored by Ramon Nou's avatar Ramon Nou Committed by Ramon Nou
Browse files

First working libc interception version, init_libc should be still ifdef if we use the user library

Added closes_range libc implementation for ssh
moving CTX->interception at the end of the initialization (MN5 issue)
Added missing interception (pread/pwrite)
Finished missing libc calls (mkdir, pread...unlink), bypass fd protection
parent 0f007003
Loading
Loading
Loading
Loading
+282 −0
Original line number Diff line number Diff line
/*
 *  Copyright 2000-2024 Felix Garcia Carballeira, Diego Camarmas Alonso,
 * Alejandro Calderon Mateos, Luis Miguel Sanchez Garcia, Borja Bergua Guerra
 *
 *  This file is part of Expand.
 *
 *  Expand is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Expand is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Expand.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Modifications to support GekkoFS done by :
 *   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
 *   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
 *
 * This software was partially supported by the
 * EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
 *
 * This software was partially supported by the
 * ADA-FS project under the SPPEXA project funded by the DFG.
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */


#ifdef __cplusplus
extern "C" {
#endif

#include <dlfcn.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>


// File API
int
dlsym_open(char* path, int flags);
int
dlsym_open2(char* path, int flags, mode_t mode);
int
dlsym_open64(char* path, int flags, mode_t mode);
int
dlsym_openat(int fd, const char* pathname, int flags, mode_t mode);
int
dlsym___open_2(char* path, int flags);
int
dlsym_close(int fd);
int
dlsym_close_range(unsigned int low, unsigned int high, int flags);
int
dlsym_creat(const char* path, mode_t mode);
int
dlsym_ftruncate(int fd, off_t length);
int
dlsym_mkstemp(char* templates);

ssize_t
dlsym_read(int fd, void* buf, size_t nbyte);
ssize_t
dlsym_write(int fd, void* buf, size_t nbyte);

ssize_t
dlsym_pread(int fd, void* buf, size_t count, off_t offset);
ssize_t
dlsym_pwrite(int fd, const void* buf, size_t count, off_t offset);
ssize_t
dlsym_pread64(int fd, void* buf, size_t count, off_t offset);
ssize_t
dlsym_pwrite64(int fd, const void* buf, size_t count, off_t offset);

off_t
dlsym_lseek(int fd, off_t offset, int whence);
off64_t
dlsym_lseek64(int fd, off64_t offset, int whence);

int
dlsym_fstat(int ver, int fd, struct stat* buf);
int
dlsym_fxstat64(int ver, int fd, struct stat64* buf);
int
dlsym_stat(int ver, const char* path, struct stat* buf);
int
dlsym_lstat(int ver, const char* path, struct stat* buf);
int
dlsym_lxstat64(int ver, const char* path, struct stat64* buf);
int
dlsym_xstat64(int ver, const char* path, struct stat64* buf);
int
dlsym_fstatat(int dfd, const char* path, struct stat* buf, int flags);
int
dlsym_fstatat64(int dfd, const char* path, struct stat64* buf, int flags);

int
dlsym_rename(const char* old_path, const char* new_path);
int
dlsym_unlink(char* path);
int
dlsym_remove(char* path);


// File API (stdio)
FILE*
dlsym_fopen(const char* filename, const char* mode);
FILE*
dlsym_fdopen(int fd, const char* mode);
int
dlsym_fclose(FILE* stream);

size_t
dlsym_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
size_t
dlsym_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);

int
dlsym_fseek(FILE* stream, long int offset, int whence);
long
dlsym_ftell(FILE* stream);
void
dlsym_rewind(FILE* stream);
int
dlsym_feof(FILE* stream);


// Directory API
DIR*
dlsym_opendir(char* dirname);
DIR*
dlsym_opendir64(char* dirname);
int
dlsym_closedir(DIR* dirp);

long
dlsym_telldir(DIR* dirp);
void
dlsym_seekdir(DIR* dirp, long loc);

struct dirent*
dlsym_readdir(DIR* dirp);
struct dirent64*
dlsym_readdir64(DIR* dirp);

int
dlsym_mkdir(char* path, mode_t mode);
int
dlsym_rmdir(char* path);


// Proccess API
int
dlsym_fork(void);

int
dlsym_pipe(int pipefd[2]);

int
dlsym_dup(int fd);
int
dlsym_dup2(int fd, int fd2);

void
dlsym_exit(int status);


// Manager API
int
dlsym_chdir(char* path);
int
dlsym_chmod(char* path, mode_t mode);
int
dlsym_fchmod(int fd, mode_t mode);
int
dlsym_chown(char* path, uid_t owner, gid_t group);
int
dlsym_fcntl(int fd, int cmd, long arg);
int
dlsym_access(const char* path, int mode);
int
dlsym_fsync(int fd);
int
dlsym_flock(int fd, int operation);


// Memory API
void*
dlsym_mmap(void* addr, size_t length, int prot, int flags, int fd,
           off_t offset);

int
open64(const char* path, int flags, ...);
int
open(const char* path, int flags, ...);
int
close(int fd);
int
creat(const char* path, mode_t mode);
int
ftruncate(int fd, off_t length) noexcept;
int
mkstemp(char* templates);

ssize_t
read(int fd, void* buf, size_t nbyte);
ssize_t
write(int fd, const void* buf, size_t nbyte);

int
mkdir(const char* path, mode_t mode);

int
rmdir(const char* path) noexcept;

ssize_t
pread(int fd, void* buf, size_t count, off_t offset);
ssize_t
pwrite(int fd, const void* buf, size_t count, off_t offset);
ssize_t
pread64(int fd, void* buf, size_t count, off_t offset);
ssize_t
pwrite64(int fd, const void* buf, size_t count, off_t offset);

off_t
lseek(int fd, off_t offset, int whence) __THROW;
off64_t
lseek64(int fd, off64_t offset, int whence) __THROW;

int
fstat(int fd, struct stat* buf);
int
fstat64(int fd, struct stat64* buf);
int
stat(const char* path, struct stat* buf);
int
lstat(const char* path, struct stat* buf);
int
stat64(const char* path, struct stat64* buf);
int
lstat64(const char* path, struct stat64* buf);
int
fstatat(int dfd, const char* path, struct stat* buf, int flags);
int
fstatat64(int dfd, const char* path, struct stat64* buf, int flags);

int
rename(const char* old_path, const char* new_path);
int
unlink(const char* path) __THROW __nonnull((1));
int
remove(const char* path);

DIR*
opendir(const char* dirname);
DIR*
opendir64(const char* dirname);
int
closedir(DIR* dirp);

long
telldir(DIR* dirp);
void
seekdir(DIR* dirp, long loc);

struct dirent*
readdir(DIR* dirp);
struct dirent64*
readdir64(DIR* dirp);


/* ................................................................... */

#ifdef __cplusplus
}
#endif
+7 −0
Original line number Diff line number Diff line
@@ -47,6 +47,13 @@ init_preload() __attribute__((constructor));

void
destroy_preload() __attribute__((destructor));
#else
void
init_libc() __attribute__((constructor));

void
destroy_libc() __attribute__((destructor));

#endif

#endif // IOINTERCEPT_PRELOAD_HPP
+8 −2
Original line number Diff line number Diff line
@@ -128,7 +128,11 @@ private:
    std::bitset<MAX_USER_FDS> protected_fds_;
    std::string hostname;
    int replicas_;
#ifdef BYPASS_SYSCALL
    bool protect_fds_{false};
#else
    bool protect_fds_{true};
#endif

    std::shared_ptr<gkfs::messagepack::ClientMetrics> write_metrics_;
    std::shared_ptr<gkfs::messagepack::ClientMetrics> read_metrics_;
@@ -302,9 +306,11 @@ public:
    int
    get_replicas();

    bool protect_fds() const;
    bool
    protect_fds() const;

    void protect_fds(bool protect);
    void
    protect_fds(bool protect);

    const std::shared_ptr<gkfs::messagepack::ClientMetrics>
    write_metrics();
+54 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@

add_library(gkfs_intercept SHARED)
add_library(gkfs_user_lib SHARED)
add_library(gkfs_libc_intercept SHARED)


target_sources(gkfs_intercept
@@ -80,9 +81,38 @@ target_sources(
    syscalls/detail/syscall_info.c syscalls/util.S
)

target_sources(
    gkfs_libc_intercept
    PRIVATE gkfs_functions.cpp
    gkfs_libc.cpp
    intercept.cpp
    hooks.cpp
    logging.cpp
    open_file_map.cpp
    open_dir.cpp
    path.cpp
    preload.cpp
    preload_context.cpp
    preload_util.cpp
    malleability.cpp
    cache.cpp
    rpc/rpc_types.cpp
    rpc/forward_data.cpp
    rpc/forward_data_proxy.cpp
    rpc/forward_management.cpp
    rpc/forward_metadata.cpp
    rpc/forward_metadata_proxy.cpp
    rpc/forward_malleability.cpp
    syscalls/detail/syscall_info.c syscalls/util.S
   
)

target_compile_definitions(gkfs_user_lib PUBLIC BYPASS_SYSCALL)
target_link_options(gkfs_user_lib PRIVATE -z noexecstack)

target_compile_definitions(gkfs_libc_intercept PUBLIC BYPASS_SYSCALL)
target_link_options(gkfs_libc_intercept PRIVATE) #-z noexecstack

if (GKFS_ENABLE_AGIOS)
    target_compile_definitions(gkfs_intercept PUBLIC GKFS_ENABLE_AGIOS)
endif ()
@@ -120,6 +150,17 @@ target_link_libraries(
    Microsoft.GSL::GSL
)

target_link_libraries(
    gkfs_libc_intercept
    PRIVATE metadata distributor env_util arithmetic path_util rpc_utils
    PUBLIC dl
    Mercury::Mercury
    hermes
    fmt::fmt
    Threads::Threads
)


install(
    TARGETS gkfs_intercept
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -141,3 +182,16 @@ install(
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gkfs
)

set_target_properties(gkfs_libc_intercept
    PROPERTIES
    PUBLIC_HEADER "../../include/client/void_syscall_intercept.hpp"
    PUBLIC_HEADER "../../include/client/user_functions.hpp"
)

install(
    TARGETS gkfs_libc_intercept
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gkfs
)
 No newline at end of file
+821 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading