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

more syscalls from hook, tested

parent ad5df517
......@@ -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
......@@ -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));
}
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));
}
......@@ -89,114 +96,200 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
int fd = ::open(opts.pathname.c_str(), O_RDWR);
if(fd == -1) {
output ("open", fd, opts);
output("open", fd, 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);
output("faccessat", rv, opts);
return;
}
// External
// faccessat External
rv = ::faccessat(AT_FDCWD, "/tmp", F_OK, 0);
if(rv < 0) {
output ("faccessat, external", rv, opts);
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;
}
if (rv < 0) {
output ("lstat", rv, opts);
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);
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);
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);
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) {
output ("fcntl, F_GETFD", rv, opts);
if(rv < 0) {
output("fcntl, F_GETFD", rv, opts);
return;
}
rv = ::fcntl(fd, F_GETFL);
if (rv < 0 || rv != O_RDWR) {
output ("fcntl, F_GETFL", rv, opts);
if(rv < 0 || rv != O_RDWR) {
output("fcntl, F_GETFL", rv, opts);
return;
}
rv = ::fcntl(fd, F_SETFD, 0);
if (rv < 0) {
output ("fcntl, F_SETFD", rv, opts);
if(rv < 0) {
output("fcntl, F_SETFD", rv, opts);
return;
}
rv = ::fcntl(fd, F_SETFL, 0);
if (errno != ENOTSUP) {
output ("fcntl, F_SETFL", rv, opts);
if(errno != ENOTSUP) {
output("fcntl, F_SETFL", rv, opts);
return;
}
rv = ::fcntl(fd, F_DUPFD, 0);
if (rv < 0) {
output ("fcntl, F_DUPFD", rv, opts);
if(rv < 0) {
output("fcntl, F_DUPFD", rv, opts);
return;
}
rv = ::fcntl(fd, F_DUPFD_CLOEXEC, 0);
if (rv < 0) {
output ("fcntl, F_DUPFD_CLOEXEC", rv, opts);
if(rv < 0) {
output("fcntl, F_DUPFD_CLOEXEC", rv, opts);
return;
}
// Fstatfs
// Fstatfs internal
struct statfs stfs;
rv = ::fstatfs(fd, &stfs);
if (rv < 0) {
output ("fstatfs", rv, opts);
if(rv < 0) {
output("fstatfs", rv, opts);
return;
}
// Fstatfs external
rv = ::fstatfs(fdext, &stfs);
if(rv < 0) {
output("fstatfs", rv, opts);
return;
}
// fsync
rv = ::fsync(fd);
if (rv < 0) {
output ("fsync", rv, opts);
if(rv < 0) {
output("fsync", rv, opts);
return;
}
......@@ -204,61 +297,146 @@ syscall_coverage_exec(const syscall_coverage_options& opts) {
char buf[1024];
rv = ::getxattr(opts.pathname.c_str(), "user.test", buf, sizeof(buf));
if (errno != ENOTSUP) {
output ("getxattr", rv, opts);
if(errno != ENOTSUP) {
output("getxattr", rv, opts);
return;
}
// readlinkat
rv = ::readlinkat(AT_FDCWD, opts.pathname.c_str(), buf, sizeof(buf));
if (errno != ENOTSUP) {
output ("readlinkat", rv, opts);
if(errno != ENOTSUP) {
output("readlinkat", rv, 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) {
output ("fchdir", fddir, opts);
if(fddir < 0) {
output("fchdir", fddir, opts);
return;
}
rv = ::fchdir(fddir);
if (rv < 0) {
output ("fchdir", rv, opts);
if(rv < 0) {
output("fchdir", rv, opts);
return;
}
// ftruncate
rv = ::ftruncate(fd, 0);
if (rv < 0) {
output ("ftruncate", rv, opts);
if(rv < 0) {
output("ftruncate", rv, opts);
return;
}
// fchdir internal file
rv = ::fchdir(fd);
if (errno != EBADF) {
output ("fchdir", rv, opts);
if(errno != EBADF) {
output("fchdir", rv, opts);
return;
}
// fchdir directory from opts.pathname
auto fd2 = ::open(opts.pathname.substr(0,opts.pathname.find_last_of("/")).c_str(), O_RDONLY);
if (fd2 < 0) {
output ("fchdir", fd2, opts);
auto fd2 = ::open(
opts.pathname.substr(0, opts.pathname.find_last_of("/")).c_str(),
O_RDONLY);
if(fd2 < 0) {
output("fchdir", fd2, opts);
return;
}
rv = ::fchdir(fd2);
if (rv < 0) {
output ("fchdir", rv, opts);
if(rv < 0) {
output("fchdir", rv, 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,
......
......@@ -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"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment