Commit 91668525 authored by Ramon Nou's avatar Ramon Nou Committed by Ramon Nou
Browse files

more syscalls from hook, tested

parent ad5df517
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -49,8 +49,10 @@ def test_syscalls(gkfs_daemon, gkfs_client):


    ret = gkfs_client.syscall_coverage(file)
    assert ret.syscall == "ALLOK"
    assert ret.retval == 0
    assert ret.errno == 0
    

    
+241 −64
Original line number Diff line number Diff line
@@ -44,6 +44,9 @@
#include <unistd.h>
#include <sys/xattr.h>

// Syscall numbers
#include <sys/syscall.h>

using json = nlohmann::json;

struct syscall_coverage_options {
@@ -58,9 +61,11 @@ struct syscall_coverage_options {
struct syscall_coverage_output {
    int retval;
    int errnum;
    std::string syscall;

    REFL_DECL_STRUCT(syscall_coverage_output, REFL_DECL_MEMBER(int, retval),
                     REFL_DECL_MEMBER(int, errnum));
                     REFL_DECL_MEMBER(int, errnum),
                     REFL_DECL_MEMBER(std::string, syscall));
};

void
@@ -68,14 +73,16 @@ to_json(json& record, const syscall_coverage_output& out) {
    record = serialize(out);
}

void output (const std::string syscall, const int ret, const syscall_coverage_options& opts) {
void
output(const std::string syscall, const int ret,
       const syscall_coverage_options& opts) {
    if(opts.verbose) {
        fmt::print(
                "syscall_coverage[{}](pathname=\"{}\") = {}, errno: {} [{}]\n",
                syscall, opts.pathname, ret, errno, ::strerror(errno));
    }

    json out = syscall_coverage_output{ret, errno};
    json out = syscall_coverage_output{ret, errno, syscall};
    fmt::print("{}\n", out.dump(2));
}

@@ -93,58 +100,137 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
        return;
    }

    // Internal
    // create external
    int fdext = ::open("tmpfile", O_RDWR | O_CREAT, 0666);

    //  faccessat Internal

    auto rv = ::faccessat(AT_FDCWD, opts.pathname.c_str(), F_OK, 0);

    if(rv < 0) {
        output("faccessat", rv, opts);

        return;
    }

    // External
    // faccessat External

    rv = ::faccessat(AT_FDCWD, "/tmp", F_OK, 0);

    if(rv < 0) {
        output("faccessat, external", rv, opts);

        return;
    }

    // lstat

    struct stat st;
    rv = ::lstat(opts.pathname.c_str(), &st);
    if(rv < 0) {
        output("lstat", rv, opts);
        return;
    }

    rv = ::lstat("tmpfile", &st);
    if(rv < 0) {
        output("lstat", rv, opts);
        return;
    }

    // pwrite external
    rv = ::pwrite(fdext, "test", 4, 0);
    if(rv < 0) {
        output("pwrite", rv, opts);
        return;
    }

    // pread external
    char bufext[4];
    rv = ::pread(fdext, bufext, 4, 0);
    if(rv < 0) {
        output("pread", rv, opts);
        return;
    }

    // lssek external
    rv = ::lseek(fdext, 0, SEEK_SET);
    if(rv < 0) {
        output("lseek", rv, opts);
        return;
    }

    // ftruncate external
    rv = ::ftruncate(fdext, 0);
    if(rv < 0) {
        output("ftruncate", rv, opts);
        return;
    }

    // truncate exterlan
    rv = ::truncate("tmpfile", 0);
    if(rv < 0) {
        output("truncate", rv, opts);
        return;
    }


    // dup external
    int fdext2 = ::dup(fdext);
    if(fdext2 < 0) {
        output("dup", fdext2, opts);
        return;
    }

    // dup2 external
    int ffdext3 = 0;
    rv = ::dup2(fdext, ffdext3);
    if(rv < 0) {
        output("dup2", rv, opts);
        return;
    }

    
    // fchmod internal
    rv = ::fchmod(fd, 0777);
    if(errno != ENOTSUP) {
        output("fchmod", rv, opts);
        return;
    }

    // fchmod external
    rv = ::fchmod(fdext, 0777);
    if(rv < 0) {
        output("fchmod", rv, opts);
        return;
    }

    // fchmodat internal
    rv = ::fchmodat(AT_FDCWD, opts.pathname.c_str(), 0777, 0);
    if(errno != ENOTSUP) {
        output("fchmodat", rv, opts);
        return;
    }
    // fchmodat external
    rv = ::fchmodat(AT_FDCWD, "tmpfile", 0777, 0);
    if(rv < 0) {
        output("fchmodat, external", rv, opts);
        return;
    }

    // dup3 internal
    rv = ::dup3(fd, 0, 0);
    if(errno != ENOTSUP) {
        output("dup3", rv, opts);
        return;
    }

    // dup3 external
    int ffdext4 = 0;
    rv = ::dup3(fdext, ffdext4, 0);
    if(rv < 0) {
        output("dup3", rv, opts);
        return;
    }


    // fcntl
    rv = ::fcntl(fd, F_GETFD);
    if(rv < 0) {
@@ -183,7 +269,7 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
        return;
    }

    // Fstatfs
    // Fstatfs internal

    struct statfs stfs;
    rv = ::fstatfs(fd, &stfs);
@@ -192,6 +278,13 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
        return;
    }

    // Fstatfs external
    rv = ::fstatfs(fdext, &stfs);
    if(rv < 0) {
        output("fstatfs", rv, opts);
        return;
    }

    // fsync

    rv = ::fsync(fd);
@@ -216,6 +309,21 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
        return;
    }

    // chdir internal error
    rv = ::chdir(opts.pathname.c_str());
    if(errno != ENOTDIR) {
        output("chdir", rv, opts);
        return;
    }

    // chdir internal error
    std::string nonexist = opts.pathname+"x2";
    rv = ::chdir(nonexist.c_str());
    if(rv >= 0) {
        output("chdir", rv, opts);
        return;
    }

    // fchdir
    auto fddir = ::open(".", O_RDONLY);
    if(fddir < 0) {
@@ -245,7 +353,9 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {


    // fchdir directory from opts.pathname
    auto fd2 = ::open(opts.pathname.substr(0,opts.pathname.find_last_of("/")).c_str(), O_RDONLY);
    auto fd2 = ::open(
            opts.pathname.substr(0, opts.pathname.find_last_of("/")).c_str(),
            O_RDONLY);
    if(fd2 < 0) {
        output("fchdir", fd2, opts);
        return;
@@ -256,9 +366,77 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
        return;
    }

    // renameat external
    auto fdtmp = ::open("/tmp/test_rename", O_CREAT | O_WRONLY);
    ::close(fdtmp);

    rv = ::renameat(AT_FDCWD, "/tmp/test_rename", AT_FDCWD,
                    opts.pathname.c_str());
    if(errno != ENOTSUP) {
        output("renameat", rv, opts);
        return;
    }

    rv = ::renameat(AT_FDCWD, "/tmp/test_rename", AT_FDCWD,
                    "/tmp/test_rename2");
    if(rv < 0) {
        output("renameat", rv, opts);
        return;
    }
    // sys_open
    rv = ::syscall(SYS_open, opts.pathname.c_str(), O_RDONLY, 0);
    if(rv < 0) {
        output("sys_open", rv, opts);
        return;
    }

    // sys_creat
    rv = ::syscall(SYS_creat, opts.pathname.c_str(), 0777);
    if(rv < 0) {
        output("sys_creat", rv, opts);
        return;
    }

    // sys_unlinkat
    rv = ::syscall(SYS_unlinkat, AT_FDCWD, opts.pathname.c_str(), 0);
    if(errno != ENOTSUP) {
        output("sys_unlinkat", rv, opts);
        return;
    }

    // sys_mkdirat
    std::string path = opts.pathname + "path";
    rv = ::syscall(SYS_mkdirat, AT_FDCWD, opts.pathname.c_str(), 0777);
    if(rv < 0) {
        output("sys_mkdirat", rv, opts);
        return;
    }

    // SYS_chmod
    rv = ::syscall(SYS_chmod, opts.pathname.c_str(), 0777);
    if(errno != ENOTSUP) {
        output("sys_chmod", rv, opts);
        return;
    }

    // hook_faccessat coverage
    rv = ::syscall(SYS_faccessat, AT_FDCWD, opts.pathname.c_str(), F_OK, 0);
    if(rv < 0) {
        output("sys_faccessat", rv, opts);
        return;
    }

    rv = ::syscall(SYS_faccessat, AT_FDCWD, "/tmp", F_OK, 0);
    if(rv < 0) {
        output("sys_faccessat", rv, opts);
        return;
    }


    rv = 0;
    errno = 0;
    json out = syscall_coverage_output{(int) rv, errno};
    auto syscall = "ALLOK";
    json out = syscall_coverage_output{(int) rv, errno, syscall};
    fmt::print("{}\n", out.dump(2));
    return;
}
@@ -268,9 +446,8 @@ syscall_coverage_init(CLI::App& app) {

    // Create the option and subcommand objects
    auto opts = std::make_shared<syscall_coverage_options>();
    auto* cmd = app.add_subcommand(
            "syscall_coverage",
            "Execute severals syscalls");
    auto* cmd =
            app.add_subcommand("syscall_coverage", "Execute severals syscalls");

    // Add options to cmd, binding them to opts
    cmd->add_flag("-v,--verbose", opts->verbose,
+2 −1
Original line number Diff line number Diff line
@@ -429,10 +429,11 @@ class SyscallCoverageOutputSchema(Schema):

    retval = fields.Integer(required=True)
    errno = Errno(data_key='errnum', required=True)
    syscall = fields.String(required=True)

    @post_load
    def make_object(self, data, **kwargs):
        return namedtuple('SyscallCoverageReturn', ['retval', 'errno'])(**data)
        return namedtuple('SyscallCoverageReturn', ['retval', 'errno', 'syscall'])(**data)

class SymlinkOutputSchema(Schema):
    """Schema to deserialize the results of an symlink execution"""